-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mc36
committed
Mar 28, 2022
1 parent
3fb9180
commit 556af7d
Showing
4 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
#include <fcntl.h> | ||
#include <signal.h> | ||
|
||
|
||
void err(char*buf) { | ||
printf("%s\n", buf); | ||
exit(1); | ||
} | ||
|
||
|
||
int main(int argc, char **argv) { | ||
|
||
if (argc < 2) err("using: daemon <bin> [args]"); | ||
|
||
if (close(STDIN_FILENO) != 0) err("error closing stdin"); | ||
if (close(STDOUT_FILENO) != 0) err("error closing stdout"); | ||
if (close(STDERR_FILENO) != 0) err("error closing stderr"); | ||
|
||
sigset_t mask; | ||
if (sigfillset(&mask) != 0) err("error setting mask"); | ||
if (sigprocmask(SIG_SETMASK, &mask, NULL) != 0) err("error masking signals"); | ||
|
||
int i = fork(); | ||
if (i == -1) err("failed to fork"); | ||
if (i != 0) err("normal termination"); | ||
|
||
i = fork(); | ||
if (i == -1) err("failed to fork"); | ||
if (i != 0) err("normal termination"); | ||
|
||
if (dup2(open("/dev/null", O_RDONLY), STDIN_FILENO) == -1) err("error redirecting stdin"); | ||
if (dup2(open("/dev/null", O_RDWR), STDOUT_FILENO) == -1) err("error redirecting stdout"); | ||
if (dup2(open("/dev/null", O_RDWR), STDERR_FILENO) == -1) err("error redirecting stderr"); | ||
|
||
if (chdir("/") == -1) err("error changing directory"); | ||
|
||
if (execvp(argv[1], &(argv[1])) == -1) err("error executing process"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters