]> git.sur5r.net Git - i3/i3/blob - i3bar/src/child.c
Merge branch 'next'
[i3/i3] / i3bar / src / child.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3bar - an xcb-based status- and ws-bar for i3
5  * © 2010-2011 Axel Wagner and contributors (see also: LICENSE)
6  *
7  * child.c: Getting Input for the statusline
8  *
9  */
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <signal.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <err.h>
20 #include <ev.h>
21
22 #include "common.h"
23
24 /* Global variables for child_*() */
25 pid_t child_pid;
26
27 /* stdin- and sigchild-watchers */
28 ev_io    *stdin_io;
29 ev_child *child_sig;
30
31 /* The buffer statusline points to */
32 char *statusline_buffer = NULL;
33
34 /*
35  * Stop and free() the stdin- and sigchild-watchers
36  *
37  */
38 void cleanup() {
39     if (stdin_io != NULL) {
40         ev_io_stop(main_loop, stdin_io);
41         FREE(stdin_io);
42         FREE(statusline_buffer);
43         /* statusline pointed to memory within statusline_buffer */
44         statusline = NULL;
45     }
46
47     if (child_sig != NULL) {
48         ev_child_stop(main_loop, child_sig);
49         FREE(child_sig);
50     }
51 }
52
53 /*
54  * Callbalk for stdin. We read a line from stdin and store the result
55  * in statusline
56  *
57  */
58 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
59     int fd = watcher->fd;
60     int n = 0;
61     int rec = 0;
62     int buffer_len = STDIN_CHUNK_SIZE;
63     char *buffer = smalloc(buffer_len);
64     buffer[0] = '\0';
65     while(1) {
66         n = read(fd, buffer + rec, buffer_len - rec);
67         if (n == -1) {
68             if (errno == EAGAIN) {
69                 /* remove trailing newline and finish up */
70                 buffer[rec-1] = '\0';
71                 break;
72             }
73             ELOG("read() failed!: %s\n", strerror(errno));
74             exit(EXIT_FAILURE);
75         }
76         if (n == 0) {
77             if (rec != 0) {
78                 /* remove trailing newline and finish up */
79                 buffer[rec-1] = '\0';
80             }
81
82             /* end of file, kill the watcher */
83             ELOG("stdin: received EOF\n");
84             cleanup();
85             draw_bars();
86             return;
87         }
88         rec += n;
89
90         if (rec == buffer_len) {
91             buffer_len += STDIN_CHUNK_SIZE;
92             buffer = srealloc(buffer, buffer_len);
93         }
94     }
95     if (*buffer == '\0') {
96         FREE(buffer);
97         return;
98     }
99     FREE(statusline_buffer);
100     statusline = statusline_buffer = buffer;
101     for (n = 0; buffer[n] != '\0'; ++n) {
102         if (buffer[n] == '\n')
103             statusline = &buffer[n + 1];
104     }
105     DLOG("%s\n", statusline);
106     draw_bars();
107 }
108
109 /*
110  * We received a sigchild, meaning, that the child-process terminated.
111  * We simply free the respective data-structures and don't care for input
112  * anymore
113  *
114  */
115 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
116     ELOG("Child (pid: %d) unexpectedly exited with status %d\n",
117            child_pid,
118            watcher->rstatus);
119     cleanup();
120 }
121
122 /*
123  * Start a child-process with the specified command and reroute stdin.
124  * We actually start a $SHELL to execute the command so we don't have to care
125  * about arguments and such
126  *
127  */
128 void start_child(char *command) {
129     child_pid = 0;
130     if (command != NULL) {
131         int fd[2];
132         if (pipe(fd) == -1)
133             err(EXIT_FAILURE, "pipe(fd)");
134
135         child_pid = fork();
136         switch (child_pid) {
137             case -1:
138                 ELOG("Couldn't fork(): %s\n", strerror(errno));
139                 exit(EXIT_FAILURE);
140             case 0:
141                 /* Child-process. Reroute stdout and start shell */
142                 close(fd[0]);
143
144                 dup2(fd[1], STDOUT_FILENO);
145
146                 static const char *shell = NULL;
147
148                 if ((shell = getenv("SHELL")) == NULL)
149                     shell = "/bin/sh";
150
151                 execl(shell, shell, "-c", command, (char*) NULL);
152                 return;
153             default:
154                 /* Parent-process. Rerout stdin */
155                 close(fd[1]);
156
157                 dup2(fd[0], STDIN_FILENO);
158
159                 /* If hide-on-modifier is set, we start of by sending the
160                  * child a SIGSTOP, because the bars aren't mapped at start */
161                 if (config.hide_on_modifier) {
162                     stop_child();
163                 }
164
165                 break;
166         }
167     }
168
169     /* We set O_NONBLOCK because blocking is evil in event-driven software */
170     fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
171
172     stdin_io = smalloc(sizeof(ev_io));
173     ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
174     ev_io_start(main_loop, stdin_io);
175
176     /* We must cleanup, if the child unexpectedly terminates */
177     child_sig = smalloc(sizeof(ev_child));
178     ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
179     ev_child_start(main_loop, child_sig);
180
181 }
182
183 /*
184  * kill()s the child-process (if existent) and closes and
185  * free()s the stdin- and sigchild-watchers
186  *
187  */
188 void kill_child() {
189     if (child_pid != 0) {
190         kill(child_pid, SIGCONT);
191         kill(child_pid, SIGTERM);
192         int status;
193         waitpid(child_pid, &status, 0);
194         child_pid = 0;
195         cleanup();
196     }
197 }
198
199 /*
200  * Sends a SIGSTOP to the child-process (if existent)
201  *
202  */
203 void stop_child() {
204     if (child_pid != 0) {
205         kill(child_pid, SIGSTOP);
206     }
207 }
208
209 /*
210  * Sends a SIGCONT to the child-process (if existent)
211  *
212  */
213 void cont_child() {
214     if (child_pid != 0) {
215         kill(child_pid, SIGCONT);
216     }
217 }