]> git.sur5r.net Git - i3/i3/blob - i3bar/src/child.c
Merge branch 'master' into next
[i3/i3] / i3bar / src / child.c
1 /*
2  * i3bar - an xcb-based status- and ws-bar for i3
3  *
4  * © 2010-2011 Axel Wagner and contributors
5  *
6  * See file LICNSE for license information
7  *
8  * src/child.c: Getting Input for the statusline
9  *
10  */
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <signal.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <errno.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 = malloc(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 = realloc(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         pipe(fd);
133         child_pid = fork();
134         switch (child_pid) {
135             case -1:
136                 ELOG("Couldn't fork(): %s\n", strerror(errno));
137                 exit(EXIT_FAILURE);
138             case 0:
139                 /* Child-process. Reroute stdout and start shell */
140                 close(fd[0]);
141
142                 dup2(fd[1], STDOUT_FILENO);
143
144                 static const char *shell = NULL;
145
146                 if ((shell = getenv("SHELL")) == NULL)
147                     shell = "/bin/sh";
148
149                 execl(shell, shell, "-c", command, (char*) NULL);
150                 return;
151             default:
152                 /* Parent-process. Rerout stdin */
153                 close(fd[1]);
154
155                 dup2(fd[0], STDIN_FILENO);
156
157                 /* If hide-on-modifier is set, we start of by sending the
158                  * child a SIGSTOP, because the bars aren't mapped at start */
159                 if (config.hide_on_modifier) {
160                     stop_child();
161                 }
162
163                 break;
164         }
165     }
166
167     /* We set O_NONBLOCK because blocking is evil in event-driven software */
168     fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
169
170     stdin_io = malloc(sizeof(ev_io));
171     ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
172     ev_io_start(main_loop, stdin_io);
173
174     /* We must cleanup, if the child unexpectedly terminates */
175     child_sig = malloc(sizeof(ev_child));
176     ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
177     ev_child_start(main_loop, child_sig);
178
179 }
180
181 /*
182  * kill()s the child-process (if existent) and closes and
183  * free()s the stdin- and sigchild-watchers
184  *
185  */
186 void kill_child() {
187     if (child_pid != 0) {
188         kill(child_pid, SIGCONT);
189         kill(child_pid, SIGTERM);
190         int status;
191         waitpid(child_pid, &status, 0);
192         child_pid = 0;
193         cleanup();
194     }
195 }
196
197 /*
198  * Sends a SIGSTOP to the child-process (if existent)
199  *
200  */
201 void stop_child() {
202     if (child_pid != 0) {
203         kill(child_pid, SIGSTOP);
204     }
205 }
206
207 /*
208  * Sends a SIGCONT to the child-process (if existent)
209  *
210  */
211 void cont_child() {
212     if (child_pid != 0) {
213         kill(child_pid, SIGCONT);
214     }
215 }