2 * vim:ts=4:sw=4:expandtab
11 #include <sys/types.h>
19 * Reads /proc/<pid>/stat and returns (via pointers) the name and parent pid of
21 * When false is returned, parsing failed and the contents of outname and
22 * outpid are undefined.
25 static bool parse_proc_stat(pid_t pid, char **outname, pid_t *outppid) {
27 /* the relevant contents (for us) are:
28 * <pid> (<program name>) <status> <ppid>
29 * which should well fit into one page of 4096 bytes */
32 if (snprintf(path, sizeof(path), "/proc/%d/stat", pid) == -1 ||
33 !slurp(path, buffer, sizeof(buffer)))
36 char *leftbracket = strchr(buffer, '(');
37 char *rightbracket = strrchr(buffer, ')');
40 sscanf(rightbracket + 2, "%*c %d", outppid) != 1)
43 *outname = strdup(leftbracket + 1);
47 static char *format_for_process(const char *name) {
48 if (strcasecmp(name, "i3bar") == 0 || strcasecmp(name, "swaybar") == 0)
50 else if (strcasecmp(name, "dzen2") == 0)
52 else if (strcasecmp(name, "xmobar") == 0)
59 * This function tries to automatically find out where i3status is being piped
60 * to and choses the appropriate output format.
62 * It is a little hackish but should work for most setups :).
64 * By iterating through /proc/<number>/stat and finding out the parent process
65 * id (just like pstree(1) or ps(1) work), we can get all children of our
66 * parent. When the output of i3status is being piped somewhere, the shell
67 * (parent process) spawns i3status and the destination process, so we will
68 * find our own process and the pipe target.
70 * We then check whether the pipe target’s name is known and chose the format.
73 char *auto_detect_format(void) {
74 /* If stdout is a tty, we output directly to a terminal. */
75 if (isatty(STDOUT_FILENO)) {
79 pid_t myppid = getppid();
80 pid_t mypid = getpid();
90 if (!parse_proc_stat(myppid, &parentname, &parentpid))
93 if (strcmp(parentname, "sh") == 0) {
94 pid_t tmp_ppid = parentpid;
96 fprintf(stderr, "i3status: auto-detection: parent process is \"sh\", looking at its parent\n");
97 if (!parse_proc_stat(tmp_ppid, &parentname, &parentpid))
101 /* Some shells, for example zsh, open a pipe in a way which will make the
102 * pipe target the parent process of i3status. If we detect that, we set
103 * the format and we are done. */
104 if ((format = format_for_process(parentname)) != NULL)
107 if (!(dir = opendir("/proc")))
110 while ((entry = readdir(dir)) != NULL) {
111 pid_t pid = (pid_t)atoi(entry->d_name);
112 if (pid == 0 || pid == mypid)
118 /* Now we need to find out the name of the process.
119 * To avoid the possible race condition of the process existing already
120 * but not executing the destination (shell after fork() and before
121 * exec()), we check if the name equals its parent.
123 * We try this for up to 0.5 seconds, then we give up.
126 /* give the scheduler a chance between each iteration, don’t hog
127 * the CPU too much */
133 if (!parse_proc_stat(pid, &name, &ppid))
137 } while (strcmp(parentname, name) == 0 && loopcnt++ < 10000);
142 /* Check for known destination programs and set format */
143 char *newfmt = format_for_process(name);
146 if (newfmt && format && strcmp(newfmt, format) != 0) {
147 fprintf(stderr, "i3status: cannot auto-configure, situation ambiguous (format \"%s\" *and* \"%s\" detected)\n", newfmt, format);