~kris/suckless

dwm

ref: ecba1fc15a4e33ba4b00e04f8f047170019d7f2f dwm/patch/cool_autostart.c -rw-r--r-- 1.3 KiB
ecba1fc1 — Kris Yotam enable FLEXTILE_DELUXE_LAYOUT for RRR reading mode (3 vertical panes) 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* dwm will keep pid's of processes from autostart array and kill them at quit */
static pid_t *autostart_pids;
static size_t autostart_len;

/* execute command from autostart array */
static void
autostart_exec()
{
	const char *const *p;
	struct sigaction sa;
	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++) {
		/* kill any existing instance before starting a new one (seamless restart) */
		FILE *cmd;
		char pidline[256];
		pid_t oldpid;
		int len = snprintf(pidline, sizeof(pidline), "pidof -s %s", *p);
		if (len > 0 && (size_t)len < sizeof(pidline) && (cmd = popen(pidline, "r"))) {
			if (fgets(pidline, sizeof(pidline), cmd))  {
				oldpid = (pid_t)atol(pidline);
				if (oldpid > 0) {
					kill(oldpid, SIGTERM);
					waitpid(oldpid, NULL, WNOHANG);
				}
			}
			pclose(cmd);
		}

		if ((autostart_pids[i] = fork()) == 0) {
			setsid();

			/* Restore SIGCHLD sighandler to default before spawning a program */
			sigemptyset(&sa.sa_mask);
			sa.sa_flags = 0;
			sa.sa_handler = SIG_DFL;
			sigaction(SIGCHLD, &sa, NULL);

			execvp(*p, (char *const *)p);
			fprintf(stderr, "dwm: execvp %s\n", *p);
			perror(" failed");
			_exit(EXIT_FAILURE);
		}
		/* skip arguments */
		while (*++p);
	}
}