]> git.sur5r.net Git - i3/i3status/blob - src/auto_detect_format.c
magic: try to auto-detect output_format by default (dzen2/i3bar/xmobar)
[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  * This function tries to automatically find out where i3status is being piped
19  * to and choses the appropriate output format.
20  *
21  * It is a little hackish but should work for most setups :).
22  *
23  * By iterating through /proc/<number>/stat and finding out the parent process
24  * id (just like pstree(1) or ps(1) work), we can get all children of our
25  * parent. When the output of i3status is being piped somewhere, the shell
26  * (parent process) spawns i3status and the destination process, so we will
27  * find our own process and the pipe target.
28  *
29  * We then check whether the pipe target’s name is known and chose the format.
30  *
31  */
32 char *auto_detect_format() {
33     pid_t myppid = getppid();
34     pid_t mypid = getpid();
35
36     DIR *dir;
37     struct dirent *entry;
38     char path[255];
39     /* the relevant contents (for us) are:
40      * <pid> (<program name>) <status> <ppid>
41      * which should well fit into one page of 4096 bytes */
42     char buffer[4096];
43
44     char *format = NULL;
45
46     char *parentname = NULL;
47
48     if (!(dir = opendir("/proc")))
49         return NULL;
50
51     /* First pass: get the executable name of the parent.
52      * Upon error, we directly return NULL as we cannot continue without the
53      * name of our parent process. */
54     while ((entry = readdir(dir)) != NULL) {
55         pid_t pid = (pid_t)atoi(entry->d_name);
56         if (pid != myppid)
57             continue;
58
59         if (snprintf(path, sizeof(path), "/proc/%d/stat", pid) == -1 ||
60             !slurp(path, buffer, 4095))
61             goto out;
62
63         buffer[4095] = '\0';
64         char *leftbracket = strchr(buffer, '(');
65         char *rightbracket = strrchr(buffer, ')');
66         if (!leftbracket ||
67             !rightbracket ||
68             !(parentname = malloc((rightbracket - leftbracket))))
69             goto out;
70         *rightbracket = '\0';
71         strcpy(parentname, leftbracket + 1);
72     }
73
74     if (!parentname)
75         goto out;
76
77     rewinddir(dir);
78
79     while ((entry = readdir(dir)) != NULL) {
80         pid_t pid = (pid_t)atoi(entry->d_name);
81         if (pid == 0 || pid == mypid)
82             continue;
83
84         if (snprintf(path, sizeof(path), "/proc/%d/stat", pid) == -1)
85             continue;
86
87         char *name = NULL;
88         pid_t ppid;
89         int loopcnt = 0;
90         /* Now we need to find out the name of the process.
91          * To avoid the possible race condition of the process existing already
92          * but not executing the destination (shell after fork() and before
93          * exec()), we check if the name equals its parent.
94          *
95          * We try this for up to 0.5 seconds, then we give up.
96          */
97         do {
98             /* give the scheduler a chance between each iteration, don’t hog
99              * the CPU too much */
100             if (name)
101                 usleep(50);
102
103             if (!slurp(path, buffer, 4095))
104                 break;
105             buffer[4095] = '\0';
106             char *leftbracket = strchr(buffer, '(');
107             char *rightbracket = strrchr(buffer, ')');
108             if (!leftbracket ||
109                 !rightbracket ||
110                 sscanf(rightbracket + 2, "%*c %d", &ppid) != 1 ||
111                 ppid != myppid)
112                 break;
113             *rightbracket = '\0';
114             name = leftbracket + 1;
115         } while (strcmp(parentname, name) == 0 && loopcnt++ < 10000);
116
117         if (!name)
118             continue;
119
120         /* Check for known destination programs and set format */
121         char *newfmt = NULL;
122         if (strcasecmp(name, "i3bar") == 0)
123             newfmt = "none";
124         else if (strcasecmp(name, "dzen2") == 0)
125             newfmt = "dzen2";
126         else if (strcasecmp(name, "xmobar") == 0)
127             newfmt = "xmobar";
128
129         if (newfmt && format) {
130             fprintf(stderr, "i3status: cannot auto-configure, situation ambiguous (format \"%s\" *and* \"%s\" detected)\n", newfmt, format);
131             format = NULL;
132             break;
133         } else {
134             format = newfmt;
135         }
136     }
137
138 out:
139     closedir(dir);
140
141     return format;
142 }