]> git.sur5r.net Git - i3/i3status/blob - src/auto_detect_format.c
5e17e172316ed7b298bec5e7c3307456b7972610
[i3/i3status] / src / auto_detect_format.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdbool.h>
9 #include <stdint.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <dirent.h>
14
15 #include "i3status.h"
16
17 /*
18  * Reads /proc/<pid>/stat and returns (via pointers) the name and parent pid of
19  * the specified pid.
20  * When false is returned, parsing failed and the contents of outname and
21  * outpid are undefined.
22  *
23  */
24 static bool parse_proc_stat(pid_t pid, char **outname, pid_t *outppid) {
25     char path[255];
26     /* the relevant contents (for us) are:
27      * <pid> (<program name>) <status> <ppid>
28      * which should well fit into one page of 4096 bytes */
29     char buffer[4096];
30
31     if (snprintf(path, sizeof(path), "/proc/%d/stat", pid) == -1 ||
32         !slurp(path, buffer, sizeof(buffer)))
33         return false;
34
35     char *leftbracket = strchr(buffer, '(');
36     char *rightbracket = strrchr(buffer, ')');
37     if (!leftbracket ||
38         !rightbracket ||
39         sscanf(rightbracket + 2, "%*c %d", outppid) != 1)
40         return false;
41     *rightbracket = '\0';
42     *outname = strdup(leftbracket + 1);
43     return true;
44 }
45
46 static char *format_for_process(const char *name) {
47     if (strcasecmp(name, "i3bar") == 0)
48         return "i3bar";
49     else if (strcasecmp(name, "dzen2") == 0)
50         return "dzen2";
51     else if (strcasecmp(name, "xmobar") == 0)
52         return "xmobar";
53     else
54         return NULL;
55 }
56
57 /*
58  * This function tries to automatically find out where i3status is being piped
59  * to and choses the appropriate output format.
60  *
61  * It is a little hackish but should work for most setups :).
62  *
63  * By iterating through /proc/<number>/stat and finding out the parent process
64  * id (just like pstree(1) or ps(1) work), we can get all children of our
65  * parent. When the output of i3status is being piped somewhere, the shell
66  * (parent process) spawns i3status and the destination process, so we will
67  * find our own process and the pipe target.
68  *
69  * We then check whether the pipe target’s name is known and chose the format.
70  *
71  */
72 char *auto_detect_format(void) {
73     /* If stdout is a tty, we output directly to a terminal. */
74     if (isatty(STDOUT_FILENO)) {
75         return "term";
76     }
77
78     pid_t myppid = getppid();
79     pid_t mypid = getpid();
80
81     DIR *dir;
82     struct dirent *entry;
83
84     char *format = NULL;
85
86     char *parentname;
87     pid_t parentpid;
88
89     if (!parse_proc_stat(myppid, &parentname, &parentpid))
90         return NULL;
91
92     if (strcmp(parentname, "sh") == 0) {
93         pid_t tmp_ppid = parentpid;
94         free(parentname);
95         fprintf(stderr, "i3status: auto-detection: parent process is \"sh\", looking at its parent\n");
96         if (!parse_proc_stat(tmp_ppid, &parentname, &parentpid))
97             return NULL;
98     }
99
100     /* Some shells, for example zsh, open a pipe in a way which will make the
101      * pipe target the parent process of i3status. If we detect that, we set
102      * the format and we are done. */
103     if ((format = format_for_process(parentname)) != NULL)
104         goto out;
105
106     if (!(dir = opendir("/proc")))
107         goto out;
108
109     while ((entry = readdir(dir)) != NULL) {
110         pid_t pid = (pid_t)atoi(entry->d_name);
111         if (pid == 0 || pid == mypid)
112             continue;
113
114         char *name = NULL;
115         pid_t ppid;
116         int loopcnt = 0;
117         /* Now we need to find out the name of the process.
118          * To avoid the possible race condition of the process existing already
119          * but not executing the destination (shell after fork() and before
120          * exec()), we check if the name equals its parent.
121          *
122          * We try this for up to 0.5 seconds, then we give up.
123          */
124         do {
125             /* give the scheduler a chance between each iteration, don’t hog
126              * the CPU too much */
127             if (name) {
128                 usleep(50);
129                 free(name);
130             }
131
132             if (!parse_proc_stat(pid, &name, &ppid))
133                 break;
134             if (ppid != myppid)
135                 break;
136         } while (strcmp(parentname, name) == 0 && loopcnt++ < 10000);
137
138         if (!name)
139             continue;
140
141         /* Check for known destination programs and set format */
142         char *newfmt = format_for_process(name);
143         free(name);
144
145         if (newfmt && format && strcmp(newfmt, format) != 0) {
146             fprintf(stderr, "i3status: cannot auto-configure, situation ambiguous (format \"%s\" *and* \"%s\" detected)\n", newfmt, format);
147             format = NULL;
148             break;
149         } else {
150             format = newfmt;
151         }
152     }
153
154     closedir(dir);
155
156 out:
157     if (parentname)
158         free(parentname);
159
160     return format;
161 }