aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/common.c')
-rw-r--r--src/lib/common.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/lib/common.c b/src/lib/common.c
index 8ea926df1..885f31881 100644
--- a/src/lib/common.c
+++ b/src/lib/common.c
@@ -199,3 +199,62 @@ char *pid_proc_cmdline(const pid_t pid) {
199 } 199 }
200 return rv; 200 return rv;
201} 201}
202
203// return 1 if firejail --x11 on command line
204int pid_proc_cmdline_x11(const pid_t pid) {
205 // if comm is not firejail return 0
206 char *comm = pid_proc_comm(pid);
207 if (strcmp(comm, "firejail") != 0) {
208 free(comm);
209 return 0;
210 }
211 free(comm);
212
213 // open /proc/pid/cmdline file
214 char *fname;
215 int fd;
216 if (asprintf(&fname, "/proc/%d/cmdline", pid) == -1)
217 return 0;
218 if ((fd = open(fname, O_RDONLY)) < 0) {
219 free(fname);
220 return 0;
221 }
222 free(fname);
223
224 // read file
225 unsigned char buffer[BUFLEN];
226 ssize_t len;
227 if ((len = read(fd, buffer, sizeof(buffer) - 1)) <= 0) {
228 close(fd);
229 return 0;
230 }
231 buffer[len] = '\0';
232 close(fd);
233
234 // skip the first argument
235 int i;
236 for (i = 0; buffer[i] != '\0'; i++);
237
238 // parse remaining command line options
239 while (1) {
240 // extract argument
241 i++;
242 if (i >= len)
243 break;
244 char *arg = buffer + i;
245
246 // detect the last command line option
247 if (strcmp(arg, "--") == 0)
248 break;
249 if (strncmp(arg, "--", 2) != 0)
250 break;
251
252 // check x11
253 if (strcmp(arg, "--x11") == 0 || strncmp(arg, "--x11=", 6) == 0)
254 return 1;
255 }
256 return 0;
257}
258
259
260