Debian Codegraph @

src/utils/process.c

raw symbols SCIP
1 #include <fcntl.h>
2 #include <xcb/randr.h>
3 #include <xcb/render.h>
4 #include <xcb/xcb.h>
5
6 #include "process.h"
7 #include "x.h"
8
9 extern struct session *ps_g;
10
11 int spawn_picomling(struct x_connection *c) {
12 int dev_null = open("/dev/null", O_RDWR);
13 if (dev_null < 0) {
14 log_error("Failed to open /dev/null");
15 return -1;
16 }
17 int screen = 0;
18 auto new_c = xcb_connect(NULL, &screen);
19 if (xcb_connection_has_error(new_c)) {
20 log_error("Failed to open new connection");
21 close(dev_null);
22 xcb_disconnect(new_c);
23 return -1;
24 }
25
26 pid_t pid = fork();
27 if (pid == -1) {
28 log_error("Failed to fork");
29 return -1;
30 }
31
32 if (pid != 0) {
33 close(dev_null);
34 // Close the connection on the parent's side so `xcb_disconnect` won't
35 // shut it down.
36 close(xcb_get_file_descriptor(new_c));
37 xcb_disconnect(new_c);
38 return 1;
39 }
40
41 // Prevent child from using parent's X connection
42 ps_g = NULL;
43
44 close(STDIN_FILENO);
45 close(STDOUT_FILENO);
46 close(STDERR_FILENO);
47 dup2(dev_null, STDIN_FILENO);
48 dup2(dev_null, STDOUT_FILENO);
49 dup2(dev_null, STDERR_FILENO);
50
51 setsid();
52
53 x_connection_init_xcb(c, new_c, screen);
54 if (!x_extensions_init(c)) {
55 return -1;
56 }
57
58 if (!c->e.has_randr) {
59 log_error("The X server doesn't have the X RandR extension.");
60
61 return -1;
62 }
63
64 return 0;
65 }