Created
September 25, 2017 00:00
-
-
Save japm48/7d455b195bb28f85577a5b692c8ecda1 to your computer and use it in GitHub Desktop.
PSPDET - Simple Proper Detacher
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
//pspdet: PSPDET Simple Proper Detacher | |
//References: | |
// - http://stackoverflow.com/a/8888612 | |
// - Unix Network programming 3rd Ed., Vol. 1 | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <signal.h> | |
#define unlikely(x) __builtin_expect(!!(x), 0) | |
#define MACRO_WRAP(x) do { x } while(0) | |
#define TRY(expr, tst) MACRO_WRAP( \ | |
if( unlikely( !((expr) tst) ) ) { \ | |
if(errno != 0) { \ | |
perror("" # expr); \ | |
exit(errno); \ | |
} else { \ | |
puts("Unknown error" # expr); \ | |
exit(-1); \ | |
} \ | |
} ) | |
int main(int argc, char** argv) { | |
if(argc < 2) | |
return 1; | |
TRY(daemon(1, 0), != -1); | |
TRY(signal(SIGHUP, SIG_IGN), != SIG_IGN); | |
pid_t p; | |
TRY(p = fork(), != -1); | |
if(p == 0) | |
TRY(execvp(argv[1], argv+1), == -1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment