2 * i3bar - an xcb-based status- and ws-bar for i3
4 * © 2010 Axel Wagner and contributors
6 * See file LICNSE for license information
8 * src/child.c: Getting Input for the statusline
13 #include <sys/types.h>
24 /* Global variables for child_*() */
27 /* stdin- and sigchild-watchers */
32 * Stop and free() the stdin- and sigchild-watchers
36 ev_io_stop(main_loop, stdin_io);
37 ev_child_stop(main_loop, child_sig);
44 * Callbalk for stdin. We read a line from stdin and store the result
48 void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
52 int buffer_len = STDIN_CHUNK_SIZE;
53 char *buffer = malloc(buffer_len);
54 memset(buffer, '\0', buffer_len);
56 n = read(fd, buffer + rec, buffer_len - rec);
58 if (errno == EAGAIN) {
59 /* remove trailing newline and finish up */
63 ELOG("read() failed!\n");
67 if (rec == buffer_len) {
68 buffer_len += STDIN_CHUNK_SIZE;
69 buffer = realloc(buffer, buffer_len);
71 /* remove trailing newline and finish up */
78 if (strlen(buffer) == 0) {
89 * We received a sigchild, meaning, that the child-process terminated.
90 * We simply free the respective data-structures and don't care for input
94 void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
95 DLOG("Child (pid: %d) unexpectedly exited with status %d\n",
102 * Start a child-process with the specified command and reroute stdin.
103 * We actually start a $SHELL to execute the command so we don't have to care
104 * about arguments and such.
105 * We also double-fork() to avoid zombies and pass the pid of the child through a
106 * temporary pipe back to i3bar
109 void start_child(char *command) {
111 if (command != NULL) {
113 /* This pipe will be used to communicate between e.g. i3status and i3bar */
115 /* We also need this temporary pipe to get back the pid of i3status */
119 ELOG("Couldn't fork()\n");
122 /* Double-fork(), so the child gets reparented to init */
123 switch(child_pid = fork()) {
125 ELOG("Couldn't fork() twice\n");
128 /* Child-process. Reroute stdout and start shell */
131 dup2(fd[1], STDOUT_FILENO);
133 static const char *shell = NULL;
135 if ((shell = getenv("SHELL")) == NULL)
138 execl(shell, shell, "-c", command, (char*) NULL);
141 /* Temporary parent. We tell i3bar about the pid of i3status and exit */
142 write(tmp[1], &child_pid, sizeof(int));
148 /* Parent-process. Rerout stdin */
151 dup2(fd[0], STDIN_FILENO);
153 /* We also need to get the pid of i3status from the temporary pipe */
155 while (rec < sizeof(int)) {
156 rec += read(tmp[0], &child_pid, sizeof(int) - rec);
158 /* The temporary pipe is no longer needed */
166 /* We set O_NONBLOCK because blocking is evil in event-driven software */
167 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
169 stdin_io = malloc(sizeof(ev_io));
170 ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
171 ev_io_start(main_loop, stdin_io);
173 /* We must cleanup, if the child unexpectedly terminates */
174 child_sig = malloc(sizeof(ev_child));
175 ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
176 ev_child_start(main_loop, child_sig);
181 * kill()s the child-process (if existent) and closes and
182 * free()s the stdin- and sigchild-watchers
186 if (child_pid != 0) {
187 kill(child_pid, SIGTERM);
193 * Sends a SIGSTOP to the child-process (if existent)
197 if (child_pid != 0) {
198 kill(child_pid, SIGSTOP);
203 * Sends a SIGCONT to the child-process (if existent)
207 if (child_pid != 0) {
208 kill(child_pid, SIGCONT);