]> git.sur5r.net Git - i3/i3status/blob - src/auto_detect_format.c
7c4d65d6adee54697a8a752a57fb7554fb104a50
[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     /* Some shells, for example zsh, open a pipe in a way which will make the
78      * pipe target the parent process of i3status. If we detect that, we set
79      * the format and we are done. */
80     if (strcasecmp(parentname, "i3bar") == 0)
81         format = "i3bar";
82     else if (strcasecmp(parentname, "dzen2") == 0)
83         format = "dzen2";
84     else if (strcasecmp(parentname, "xmobar") == 0)
85         format = "xmobar";
86
87     if (format)
88         goto out;
89
90     rewinddir(dir);
91
92     while ((entry = readdir(dir)) != NULL) {
93         pid_t pid = (pid_t)atoi(entry->d_name);
94         if (pid == 0 || pid == mypid)
95             continue;
96
97         if (snprintf(path, sizeof(path), "/proc/%d/stat", pid) == -1)
98             continue;
99
100         char *name = NULL;
101         pid_t ppid;
102         int loopcnt = 0;
103         /* Now we need to find out the name of the process.
104          * To avoid the possible race condition of the process existing already
105          * but not executing the destination (shell after fork() and before
106          * exec()), we check if the name equals its parent.
107          *
108          * We try this for up to 0.5 seconds, then we give up.
109          */
110         do {
111             /* give the scheduler a chance between each iteration, don’t hog
112              * the CPU too much */
113             if (name)
114                 usleep(50);
115
116             if (!slurp(path, buffer, 4095))
117                 break;
118             buffer[4095] = '\0';
119             char *leftbracket = strchr(buffer, '(');
120             char *rightbracket = strrchr(buffer, ')');
121             if (!leftbracket ||
122                 !rightbracket ||
123                 sscanf(rightbracket + 2, "%*c %d", &ppid) != 1 ||
124                 ppid != myppid)
125                 break;
126             *rightbracket = '\0';
127             name = leftbracket + 1;
128         } while (strcmp(parentname, name) == 0 && loopcnt++ < 10000);
129
130         if (!name)
131             continue;
132
133         /* Check for known destination programs and set format */
134         char *newfmt = NULL;
135         if (strcasecmp(name, "i3bar") == 0)
136             newfmt = "i3bar";
137         else if (strcasecmp(name, "dzen2") == 0)
138             newfmt = "dzen2";
139         else if (strcasecmp(name, "xmobar") == 0)
140             newfmt = "xmobar";
141
142         if (newfmt && format) {
143             fprintf(stderr, "i3status: cannot auto-configure, situation ambiguous (format \"%s\" *and* \"%s\" detected)\n", newfmt, format);
144             format = NULL;
145             break;
146         } else {
147             format = newfmt;
148         }
149     }
150
151 out:
152     if (parentname)
153         free(parentname);
154
155     closedir(dir);
156
157     return format;
158 }