sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

commit 1199036787b4e8fae5559426f65fb3667fe7bf77
parent 3e1fc37e9e5a445db0a8fec122863dab8d941132
Author: zsugabubus <zsugabubus@national.shitposting.agency>
Date:   Sun, 31 May 2020 12:58:15 +0200

fix and simplify cool_autostart patch again

Previous implementations all sucked because:

- If fork() fails, dwm will happily kill all your processes at quit() :).

- If fork() not failed, dwm will kill a random process instead. Will be
  also happy.

- If execvp() fails you now have two dwms, so they will be even more
  happier together, because they both can spawn remaining entries twice
  (pray for no more failing entries); than just God knows what will
  happen.

- (If `malloc()` fails dwm crashes... it is not a critical component)

- ...that’s all. The autostart does nothing else...

Bonus: Make it even simpler for the user to add entries. We have strong
enough hardware to compute an array length... once.

Diffstat:
Mdwm.suckless.org/patches/cool_autostart/dwm-cool-autostart-6.2.diff | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
Mdwm.suckless.org/patches/cool_autostart/index.md | 16+++++++++-------
2 files changed, 73 insertions(+), 26 deletions(-)

diff --git a/dwm.suckless.org/patches/cool_autostart/dwm-cool-autostart-6.2.diff b/dwm.suckless.org/patches/cool_autostart/dwm-cool-autostart-6.2.diff @@ -1,67 +1,111 @@ diff --git a/config.def.h b/config.def.h -index 1c0b587..ca33338 100644 +index 1c0b587..ed056a4 100644 --- a/config.def.h +++ b/config.def.h -@@ -18,6 +18,10 @@ static const char *colors[][3] = { +@@ -18,6 +18,11 @@ static const char *colors[][3] = { [SchemeSel] = { col_gray4, col_cyan, col_cyan }, }; -+static void const *autostart[] = { /* please replace 2 with maximum number of arguments from autostart array */ -+ &(const char *[]){ "st", NULL }, ++static const char *const autostart[] = { ++ "st", NULL, ++ NULL /* terminate */ +}; + /* tagging */ static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }; diff --git a/dwm.c b/dwm.c -index 4465af1..5d54149 100644 +index 9fd0286..1facd56 100644 --- a/dwm.c +++ b/dwm.c -@@ -233,6 +233,7 @@ static int xerror(Display *dpy, XErrorEvent *ee); +@@ -234,6 +234,7 @@ static int xerror(Display *dpy, XErrorEvent *ee); static int xerrordummy(Display *dpy, XErrorEvent *ee); static int xerrorstart(Display *dpy, XErrorEvent *ee); static void zoom(const Arg *arg); -+static void autostart_exec(); ++static void autostart_exec(void); /* variables */ static const char broken[] = "broken"; -@@ -274,6 +275,23 @@ static Window root, wmcheckwin; +@@ -275,6 +276,34 @@ static Window root, wmcheckwin; /* compile-time check if all tags fit into an unsigned int bit array. */ struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; +/* dwm will keep pid's of processes from autostart array and kill them at quit */ +static pid_t *autostart_pids; -+static int autostart_len = LENGTH(autostart); ++static size_t autostart_len; + +/* execute command from autostart array */ +static void +autostart_exec() { -+ autostart_pids = malloc((LENGTH(autostart) + 1) * sizeof(pid_t)); -+ for (int i = 0;i < LENGTH(autostart);i++) { -+ autostart_pids[i] = fork(); -+ if (autostart_pids[i] == 0) { ++ const char *const *p; ++ size_t i = 0; ++ ++ /* count entries */ ++ for (p = autostart; *p; autostart_len++, p++) ++ while (*++p); ++ ++ autostart_pids = malloc(autostart_len * sizeof(pid_t)); ++ for (p = autostart; *p; i++, p++) { ++ if ((autostart_pids[i] = fork()) == 0) { + setsid(); -+ execvp(((char **)(autostart[i]))[0], autostart[i]); ++ execvp(*p, (char *const *)p); ++ fprintf(stderr, "dwm: execvp %s\n", *p); ++ perror(" failed"); ++ _exit(EXIT_FAILURE); + } ++ /* skip arguments */ ++ while (*++p); + } +} + /* function implementations */ void applyrules(Client *c) -@@ -1248,6 +1266,11 @@ propertynotify(XEvent *e) +@@ -1249,6 +1278,16 @@ propertynotify(XEvent *e) void quit(const Arg *arg) { ++ size_t i; ++ + /* kill child processes */ -+ for (int i = 0;i < autostart_len;i++) { -+ kill(autostart_pids[i], SIGTERM); -+ waitpid(autostart_pids[i], NULL, 0); ++ for (i = 0; i < autostart_len; i++) { ++ if (0 < autostart_pids[i]) { ++ kill(autostart_pids[i], SIGTERM); ++ waitpid(autostart_pids[i], NULL, 0); ++ } + } ++ running = 0; } -@@ -2136,6 +2159,7 @@ main(int argc, char *argv[]) +@@ -1632,9 +1671,25 @@ showhide(Client *c) + void + sigchld(int unused) + { ++ pid_t pid; ++ + if (signal(SIGCHLD, sigchld) == SIG_ERR) + die("can't install SIGCHLD handler:"); +- while (0 < waitpid(-1, NULL, WNOHANG)); ++ while (0 < (pid = waitpid(-1, NULL, WNOHANG))) { ++ pid_t *p, *lim; ++ ++ if (!(p = autostart_pids)) ++ continue; ++ lim = &p[autostart_len]; ++ ++ for (; p < lim; p++) { ++ if (*p == pid) { ++ *p = -1; ++ break; ++ } ++ } ++ ++ } + } + + void +@@ -2139,6 +2194,7 @@ main(int argc, char *argv[]) if (!(dpy = XOpenDisplay(NULL))) die("dwm: cannot open display"); checkotherwm(); @@ -69,3 +113,4 @@ index 4465af1..5d54149 100644 setup(); #ifdef __OpenBSD__ if (pledge("stdio rpath proc exec", NULL) == -1) + diff --git a/dwm.suckless.org/patches/cool_autostart/index.md b/dwm.suckless.org/patches/cool_autostart/index.md @@ -9,13 +9,14 @@ And when you exit dwm all processes from `autostart` array will be killed. Example ------- - static char* const autostart[] = { - &(const char *[]){ "mpd-notification", NULL }, - &(const char *[]){ "hsetroot", "-center", "/usr/home/bit6tream/pic/wallapper.png", NULL }, - &(const char *[]){ "xrdb", "/usr/home/bit6tream/.config/X/Xresources", NULL }, - &(const char *[]){ "sh", "-c", "while :; do dwmstatus.sh -; sleep 60; done", NULL }, - &(const char *[]){ "dunst", NULL }, - &(const char *[]){ "picom", NULL } + static const char *const autostart[] = { + "mpd-notification", NULL, + "hsetroot", "-center", "/usr/home/bit6tream/pic/wallapper.png", NULL, + "xrdb", "/usr/home/bit6tream/.config/X/Xresources", NULL, + "sh", "-c", "while :; do dwmstatus.sh -; sleep 60; done", NULL, + "dunst", NULL, + "picom", NULL, + NULL }; Attention @@ -31,3 +32,4 @@ Download Authors ------- * bit6tream <bit6tream@cock.li> [bit6tream's gitlab](https://gitlab.com/bit9tream) +* zsugabubus <zsugabubus@national.shitposting.agency>