mirror of
https://github.com/emersion/kanshi
synced 2024-11-26 05:55:27 +01:00
Clean up exec_command.
Rename pid_t variables "pid", "child" to "child" and "grandchild", since we were using "child" in comments to refer to the PID stored in "pid". Use _exit(2) whenever exiting from a forked child; calling exit(3) should't be done in such a context. Add missing new lines to error messages. Arguably using stdio in this context isn't safe either, but we aren't a threaded program and stderr is always flushed when this function is called, so it won't ouput previous buffer contents or deadlock. Fix one _exit(2) call to return 1 as status code, since it should be indicating a failure. Add error message in case waitpid(2) fails.
This commit is contained in:
parent
b6613c2eac
commit
dd3f91fbd3
26
main.c
26
main.c
@ -78,34 +78,36 @@ static struct kanshi_profile *match(struct kanshi_state *state,
|
|||||||
|
|
||||||
|
|
||||||
static void exec_command(char *cmd) {
|
static void exec_command(char *cmd) {
|
||||||
pid_t pid, child;
|
pid_t child, grandchild;
|
||||||
// Fork process
|
// Fork process
|
||||||
if ((pid = fork()) == 0) {
|
if ((child = fork()) == 0) {
|
||||||
// Fork child process again
|
// Fork child process again so we can unparent the process
|
||||||
setsid();
|
setsid();
|
||||||
sigset_t set;
|
sigset_t set;
|
||||||
sigemptyset(&set);
|
sigemptyset(&set);
|
||||||
sigprocmask(SIG_SETMASK, &set, NULL);
|
sigprocmask(SIG_SETMASK, &set, NULL);
|
||||||
if ((child = fork()) == 0) {
|
if ((grandchild = fork()) == 0) {
|
||||||
execl("/bin/sh", "/bin/sh", "-c", cmd, (void *)NULL);
|
execl("/bin/sh", "/bin/sh", "-c", cmd, (void *)NULL);
|
||||||
fprintf(stderr, "Executing command '%s' failed: %s", cmd, strerror(errno));
|
fprintf(stderr, "Executing command '%s' failed: %s\n", cmd, strerror(errno));
|
||||||
exit(-1);
|
_exit(-1);
|
||||||
}
|
}
|
||||||
if (child < 0) {
|
if (grandchild < 0) {
|
||||||
fprintf(stderr, "Impossible to fork a new process to execute"
|
fprintf(stderr, "Impossible to fork a new process to execute"
|
||||||
" command '%s': %s", cmd, strerror(errno));
|
" command '%s': %s\n", cmd, strerror(errno));
|
||||||
exit(0);
|
_exit(1);
|
||||||
}
|
}
|
||||||
exit(0); // Close child process
|
_exit(0); // Close child process
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pid < 0) {
|
if (child < 0) {
|
||||||
perror("Impossible to fork a new process");
|
perror("Impossible to fork a new process");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanup child process
|
// cleanup child process
|
||||||
waitpid(pid, NULL, 0);
|
if (waitpid(child, NULL, 0) < 0) {
|
||||||
|
perror("Impossible to clean up child process");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void execute_profile_commands(struct kanshi_profile *profile) {
|
static void execute_profile_commands(struct kanshi_profile *profile) {
|
||||||
|
Loading…
Reference in New Issue
Block a user