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