2010-08-07 12:05:16 -04:00
|
|
|
/*
|
|
|
|
* i3bar - an xcb-based status- and ws-bar for i3
|
|
|
|
*
|
|
|
|
* © 2010 Axel Wagner and contributors
|
|
|
|
*
|
|
|
|
* See file LICNSE for license information
|
|
|
|
*
|
|
|
|
* src/child.c: Getting Input for the statusline
|
|
|
|
*
|
|
|
|
*/
|
2010-08-04 23:09:59 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <signal.h>
|
2010-11-10 12:46:47 -05:00
|
|
|
#include <sys/wait.h>
|
2010-08-04 23:09:59 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ev.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2010-09-16 21:03:43 -04:00
|
|
|
/* Global variables for child_*() */
|
|
|
|
pid_t child_pid;
|
|
|
|
|
2010-08-06 20:10:05 -04:00
|
|
|
/* stdin- and sigchild-watchers */
|
|
|
|
ev_io *stdin_io;
|
2010-08-04 23:09:59 -04:00
|
|
|
ev_child *child_sig;
|
|
|
|
|
2010-08-06 20:10:05 -04:00
|
|
|
/*
|
|
|
|
* Stop and free() the stdin- and sigchild-watchers
|
|
|
|
*
|
|
|
|
*/
|
2010-08-04 23:09:59 -04:00
|
|
|
void cleanup() {
|
2010-08-06 20:10:05 -04:00
|
|
|
ev_io_stop(main_loop, stdin_io);
|
2010-08-04 23:09:59 -04:00
|
|
|
ev_child_stop(main_loop, child_sig);
|
2010-08-06 20:10:05 -04:00
|
|
|
FREE(stdin_io);
|
2010-08-04 23:09:59 -04:00
|
|
|
FREE(child_sig);
|
|
|
|
FREE(statusline);
|
|
|
|
}
|
|
|
|
|
2010-08-06 20:10:05 -04:00
|
|
|
/*
|
2010-08-24 08:01:48 -04:00
|
|
|
* Callbalk for stdin. We read a line from stdin and store the result
|
|
|
|
* in statusline
|
2010-08-06 20:10:05 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
|
2010-08-04 23:09:59 -04:00
|
|
|
int fd = watcher->fd;
|
|
|
|
int n = 0;
|
|
|
|
int rec = 0;
|
|
|
|
int buffer_len = STDIN_CHUNK_SIZE;
|
|
|
|
char *buffer = malloc(buffer_len);
|
|
|
|
memset(buffer, '\0', buffer_len);
|
|
|
|
while(1) {
|
|
|
|
n = read(fd, buffer + rec, buffer_len - rec);
|
|
|
|
if (n == -1) {
|
|
|
|
if (errno == EAGAIN) {
|
2010-08-24 22:58:28 -04:00
|
|
|
/* remove trailing newline and finish up */
|
|
|
|
buffer[rec-1] = '\0';
|
2010-08-04 23:09:59 -04:00
|
|
|
break;
|
|
|
|
}
|
2010-09-17 00:49:28 -04:00
|
|
|
ELOG("read() failed!\n");
|
2010-08-04 23:09:59 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (n == 0) {
|
|
|
|
if (rec == buffer_len) {
|
|
|
|
buffer_len += STDIN_CHUNK_SIZE;
|
2010-09-16 20:28:56 -04:00
|
|
|
buffer = realloc(buffer, buffer_len);
|
2010-08-04 23:09:59 -04:00
|
|
|
} else {
|
2010-08-24 22:58:28 -04:00
|
|
|
/* remove trailing newline and finish up */
|
|
|
|
buffer[rec-1] = '\0';
|
2010-08-04 23:09:59 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rec += n;
|
|
|
|
}
|
|
|
|
if (strlen(buffer) == 0) {
|
|
|
|
FREE(buffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FREE(statusline);
|
|
|
|
statusline = buffer;
|
2010-09-17 00:49:28 -04:00
|
|
|
DLOG("%s\n", buffer);
|
2010-08-04 23:09:59 -04:00
|
|
|
draw_bars();
|
|
|
|
}
|
|
|
|
|
2010-08-06 20:10:05 -04:00
|
|
|
/*
|
|
|
|
* We received a sigchild, meaning, that the child-process terminated.
|
|
|
|
* We simply free the respective data-structures and don't care for input
|
|
|
|
* anymore
|
|
|
|
*
|
|
|
|
*/
|
2010-08-04 23:09:59 -04:00
|
|
|
void child_sig_cb(struct ev_loop *loop, ev_child *watcher, int revents) {
|
2010-09-17 00:49:28 -04:00
|
|
|
DLOG("Child (pid: %d) unexpectedly exited with status %d\n",
|
2010-08-06 20:10:05 -04:00
|
|
|
child_pid,
|
|
|
|
watcher->rstatus);
|
2010-08-04 23:09:59 -04:00
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
2010-08-06 20:10:05 -04:00
|
|
|
/*
|
|
|
|
* Start a child-process with the specified command and reroute stdin.
|
|
|
|
* We actually start a $SHELL to execute the command so we don't have to care
|
2010-11-10 12:46:47 -05:00
|
|
|
* about arguments and such.
|
|
|
|
* We also double-fork() to avoid zombies and pass the pid of the child through a
|
|
|
|
* temporary pipe back to i3bar
|
2010-08-06 20:10:05 -04:00
|
|
|
*
|
|
|
|
*/
|
2010-08-04 23:09:59 -04:00
|
|
|
void start_child(char *command) {
|
2010-08-05 21:32:05 -04:00
|
|
|
child_pid = 0;
|
|
|
|
if (command != NULL) {
|
2010-11-10 12:46:47 -05:00
|
|
|
int fd[2], tmp[2];
|
|
|
|
/* This pipe will be used to communicate between e.g. i3status and i3bar */
|
2010-08-05 21:32:05 -04:00
|
|
|
pipe(fd);
|
2010-11-10 12:46:47 -05:00
|
|
|
/* We also need this temporary pipe to get back the pid of i3status */
|
|
|
|
pipe(tmp);
|
|
|
|
switch (fork()) {
|
2010-08-05 21:32:05 -04:00
|
|
|
case -1:
|
2010-09-17 00:49:28 -04:00
|
|
|
ELOG("Couldn't fork()\n");
|
2010-08-05 21:32:05 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
case 0:
|
2010-11-10 12:46:47 -05:00
|
|
|
/* Double-fork(), so the child gets reparented to init */
|
|
|
|
switch(child_pid = fork()) {
|
|
|
|
case -1:
|
|
|
|
ELOG("Couldn't fork() twice\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
case 0:
|
|
|
|
/* Child-process. Reroute stdout and start shell */
|
|
|
|
close(fd[0]);
|
|
|
|
|
|
|
|
dup2(fd[1], STDOUT_FILENO);
|
|
|
|
|
|
|
|
static const char *shell = NULL;
|
|
|
|
|
|
|
|
if ((shell = getenv("SHELL")) == NULL)
|
|
|
|
shell = "/bin/sh";
|
|
|
|
|
|
|
|
execl(shell, shell, "-c", command, (char*) NULL);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
/* Temporary parent. We tell i3bar about the pid of i3status and exit */
|
|
|
|
write(tmp[1], &child_pid, sizeof(int));
|
|
|
|
close(tmp[0]);
|
|
|
|
close(tmp[1]);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2010-08-05 21:32:05 -04:00
|
|
|
default:
|
2010-09-16 23:26:31 -04:00
|
|
|
/* Parent-process. Rerout stdin */
|
2010-08-05 21:32:05 -04:00
|
|
|
close(fd[1]);
|
2010-08-04 23:09:59 -04:00
|
|
|
|
2010-08-05 21:32:05 -04:00
|
|
|
dup2(fd[0], STDIN_FILENO);
|
|
|
|
|
2010-11-10 12:46:47 -05:00
|
|
|
/* We also need to get the pid of i3status from the temporary pipe */
|
|
|
|
size_t rec = 0;
|
|
|
|
while (rec < sizeof(int)) {
|
|
|
|
rec += read(tmp[0], &child_pid, sizeof(int) - rec);
|
|
|
|
}
|
|
|
|
/* The temporary pipe is no longer needed */
|
|
|
|
close(tmp[0]);
|
|
|
|
close(tmp[1]);
|
2010-08-05 21:32:05 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-11-10 12:46:47 -05:00
|
|
|
wait(0);
|
2010-08-04 23:09:59 -04:00
|
|
|
|
2010-09-16 23:26:31 -04:00
|
|
|
/* We set O_NONBLOCK because blocking is evil in event-driven software */
|
2010-08-05 21:32:05 -04:00
|
|
|
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
|
2010-08-04 23:09:59 -04:00
|
|
|
|
2010-08-06 20:10:05 -04:00
|
|
|
stdin_io = malloc(sizeof(ev_io));
|
|
|
|
ev_io_init(stdin_io, &stdin_io_cb, STDIN_FILENO, EV_READ);
|
|
|
|
ev_io_start(main_loop, stdin_io);
|
2010-08-04 23:09:59 -04:00
|
|
|
|
2010-08-05 21:32:05 -04:00
|
|
|
/* We must cleanup, if the child unexpectedly terminates */
|
2010-09-07 11:28:15 -04:00
|
|
|
child_sig = malloc(sizeof(ev_child));
|
2010-08-05 21:32:05 -04:00
|
|
|
ev_child_init(child_sig, &child_sig_cb, child_pid, 0);
|
|
|
|
ev_child_start(main_loop, child_sig);
|
2010-08-04 23:09:59 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-06 20:10:05 -04:00
|
|
|
/*
|
2010-08-25 12:31:03 -04:00
|
|
|
* kill()s the child-process (if existent) and closes and
|
2010-08-06 20:10:05 -04:00
|
|
|
* free()s the stdin- and sigchild-watchers
|
|
|
|
*
|
|
|
|
*/
|
2010-08-04 23:09:59 -04:00
|
|
|
void kill_child() {
|
2010-08-05 21:32:05 -04:00
|
|
|
if (child_pid != 0) {
|
|
|
|
kill(child_pid, SIGQUIT);
|
|
|
|
}
|
2010-08-04 23:09:59 -04:00
|
|
|
cleanup();
|
|
|
|
}
|
2010-08-25 12:31:03 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sends a SIGSTOP to the child-process (if existent)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void stop_child() {
|
|
|
|
if (child_pid != 0) {
|
|
|
|
kill(child_pid, SIGSTOP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sends a SIGCONT to the child-process (if existent)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void cont_child() {
|
|
|
|
if (child_pid != 0) {
|
|
|
|
kill(child_pid, SIGCONT);
|
|
|
|
}
|
|
|
|
}
|