dwm-cool-autostart-20240312-9f88553.diff (3329B)
1 From feeaa839d2c6c1d0e66649cb64c4added563d4e4 Mon Sep 17 00:00:00 2001 2 From: Son Phan Trung <phantrungson17@gmail.com> 3 Date: Tue, 12 Mar 2024 18:37:32 +0700 4 Subject: [PATCH] patches/cool-autostart: Update patch 5 6 Updated for the removal of the sigchld() function. 7 --- 8 config.def.h | 5 +++++ 9 dwm.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++- 10 2 files changed, 61 insertions(+), 1 deletion(-) 11 12 diff --git a/config.def.h b/config.def.h 13 index 9efa774..aba210d 100644 14 --- a/config.def.h 15 +++ b/config.def.h 16 @@ -18,6 +18,11 @@ static const char *colors[][3] = { 17 [SchemeSel] = { col_gray4, col_cyan, col_cyan }, 18 }; 19 20 +static const char *const autostart[] = { 21 + "st", NULL, 22 + NULL /* terminate */ 23 +}; 24 + 25 /* tagging */ 26 static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; 27 28 diff --git a/dwm.c b/dwm.c 29 index f1d86b2..c2eb07d 100644 30 --- a/dwm.c 31 +++ b/dwm.c 32 @@ -233,6 +233,7 @@ static int xerror(Display *dpy, XErrorEvent *ee); 33 static int xerrordummy(Display *dpy, XErrorEvent *ee); 34 static int xerrorstart(Display *dpy, XErrorEvent *ee); 35 static void zoom(const Arg *arg); 36 +static void autostart_exec(void); 37 38 /* variables */ 39 static const char broken[] = "broken"; 40 @@ -274,6 +275,34 @@ static Window root, wmcheckwin; 41 /* compile-time check if all tags fit into an unsigned int bit array. */ 42 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; 43 44 +/* dwm will keep pid's of processes from autostart array and kill them at quit */ 45 +static pid_t *autostart_pids; 46 +static size_t autostart_len; 47 + 48 +/* execute command from autostart array */ 49 +static void 50 +autostart_exec() { 51 + const char *const *p; 52 + size_t i = 0; 53 + 54 + /* count entries */ 55 + for (p = autostart; *p; autostart_len++, p++) 56 + while (*++p); 57 + 58 + autostart_pids = malloc(autostart_len * sizeof(pid_t)); 59 + for (p = autostart; *p; i++, p++) { 60 + if ((autostart_pids[i] = fork()) == 0) { 61 + setsid(); 62 + execvp(*p, (char *const *)p); 63 + fprintf(stderr, "dwm: execvp %s\n", *p); 64 + perror(" failed"); 65 + _exit(EXIT_FAILURE); 66 + } 67 + /* skip arguments */ 68 + while (*++p); 69 + } 70 +} 71 + 72 /* function implementations */ 73 void 74 applyrules(Client *c) 75 @@ -1258,6 +1287,16 @@ propertynotify(XEvent *e) 76 void 77 quit(const Arg *arg) 78 { 79 + size_t i; 80 + 81 + /* kill child processes */ 82 + for (i = 0; i < autostart_len; i++) { 83 + if (0 < autostart_pids[i]) { 84 + kill(autostart_pids[i], SIGTERM); 85 + waitpid(autostart_pids[i], NULL, 0); 86 + } 87 + } 88 + 89 running = 0; 90 } 91 92 @@ -1540,6 +1579,7 @@ void 93 setup(void) 94 { 95 int i; 96 + pid_t pid; 97 XSetWindowAttributes wa; 98 Atom utf8string; 99 struct sigaction sa; 100 @@ -1551,7 +1591,21 @@ setup(void) 101 sigaction(SIGCHLD, &sa, NULL); 102 103 /* clean up any zombies (inherited from .xinitrc etc) immediately */ 104 - while (waitpid(-1, NULL, WNOHANG) > 0); 105 + while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) { 106 + pid_t *p, *lim; 107 + 108 + if (!(p = autostart_pids)) 109 + continue; 110 + lim = &p[autostart_len]; 111 + 112 + for (; p < lim; p++) { 113 + if (*p == pid) { 114 + *p = -1; 115 + break; 116 + } 117 + } 118 + 119 + } 120 121 /* init screen */ 122 screen = DefaultScreen(dpy); 123 @@ -2152,6 +2206,7 @@ main(int argc, char *argv[]) 124 if (!(dpy = XOpenDisplay(NULL))) 125 die("dwm: cannot open display"); 126 checkotherwm(); 127 + autostart_exec(); 128 setup(); 129 #ifdef __OpenBSD__ 130 if (pledge("stdio rpath proc exec", NULL) == -1) 131 -- 132 2.44.0 133