2 * vim:ts=4:sw=4:expandtab
4 * i3bar - an xcb-based status- and ws-bar for i3
6 * © 2010-2011 Axel Wagner and contributors
8 * See file LICNSE for license information
10 * src/child.c: Getting Input for the statusline
15 #include <sys/types.h>
27 /* Global variables for child_*() */
30 /* stdin- and sigchild-watchers */
34 /* The buffer statusline points to */
35 char *statusline_buffer = NULL;
38 * Stop and free() the stdin- and sigchild-watchers
42 if (stdin_io != NULL) {
43 ev_io_stop(main_loop, stdin_io);
45 FREE(statusline_buffer);
46 /* statusline pointed to memory within statusline_buffer */
50 if (child_sig != NULL) {
51 ev_child_stop(main_loop, child_sig);
57 * Callbalk for stdin. We read a line from stdin and store the result
61 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
65 int buffer_len = STDIN_CHUNK_SIZE;
66 char *buffer = smalloc(buffer_len);
69 n = read(fd, buffer + rec, buffer_len - rec);
71 if (errno == EAGAIN) {
72 /* remove trailing newline and finish up */
76 ELOG("read() failed!: %s\n", strerror(errno));
81 /* remove trailing newline and finish up */
85 /* end of file, kill the watcher */
86 ELOG("stdin: received EOF\n");
93 if (rec == buffer_len) {
94 buffer_len += STDIN_CHUNK_SIZE;
95 buffer = srealloc(buffer, buffer_len);
98 if (*buffer == '\0') {
102 FREE(statusline_buffer);
103 statusline = statusline_buffer = buffer;
104 for (n = 0; buffer[n] != '\0'; ++n) {
105 if (buffer[n] == '\n')
106 statusline = &buffer[n + 1];
108 DLOG("%s\n", statusline);
113 * We received a sigchild, meaning, that the child-process terminated.
114 * We simply free the respective data-structures and don't care for input
118 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
119 ELOG("Child (pid: %d) unexpectedly exited with status %d\n",
126 * Start a child-process with the specified command and reroute stdin.
127 * We actually start a $SHELL to execute the command so we don't have to care
128 * about arguments and such
131 void start_child(char *command) {
133 if (command != NULL) {
136 err(EXIT_FAILURE, "pipe(fd)");
141 ELOG("Couldn't fork(): %s\n", strerror(errno));
144 /* Child-process. Reroute stdout and start shell */
147 dup2(fd[1], STDOUT_FILENO);
149 static const char *shell = NULL;
151 if ((shell = getenv("SHELL")) == NULL)
154 execl(shell, shell, "-c", command, (char*) NULL);
157 /* Parent-process. Rerout stdin */
160 dup2(fd[0], STDIN_FILENO);
162 /* If hide-on-modifier is set, we start of by sending the
163 * child a SIGSTOP, because the bars aren't mapped at start */
164 if (config.hide_on_modifier) {
172 /* We set O_NONBLOCK because blocking is evil in event-driven software */
173 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
175 stdin_io = smalloc(sizeof(ev_io));
176 ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
177 ev_io_start(main_loop, stdin_io);
179 /* We must cleanup, if the child unexpectedly terminates */
180 child_sig = smalloc(sizeof(ev_child));
181 ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
182 ev_child_start(main_loop, child_sig);
187 * kill()s the child-process (if existent) and closes and
188 * free()s the stdin- and sigchild-watchers
192 if (child_pid != 0) {
193 kill(child_pid, SIGCONT);
194 kill(child_pid, SIGTERM);
196 waitpid(child_pid, &status, 0);
203 * Sends a SIGSTOP to the child-process (if existent)
207 if (child_pid != 0) {
208 kill(child_pid, SIGSTOP);
213 * Sends a SIGCONT to the child-process (if existent)
217 if (child_pid != 0) {
218 kill(child_pid, SIGCONT);