Created
December 12, 2018 17:37
-
-
Save yjfvictor/d8a0f50a8a352bb92511f1d97da775c9 to your computer and use it in GitHub Desktop.
A tool to start a process in daemon mode
This file contains hidden or 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
/** | |
* @file daemon_utility.c | |
* @brief A tool to start a process in daemon mode | |
* @author yjf_victor | |
* @version 1.0 | |
* @date 2018-12-13 | |
* @copyright CC0 1.0 Universal https://creativecommons.org/publicdomain/zero/1.0/ | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
int main(int argc, char *argv[]) | |
{ | |
if (argc <= 0) | |
{ | |
fputs("error: argc <= 0\n", stderr); | |
return EXIT_FAILURE; | |
} | |
if (argv[argc] != NULL) | |
{ | |
fputs("error: argv error\n", stderr); | |
return EXIT_FAILURE; | |
} | |
if (argc == 1) | |
{ | |
return EXIT_SUCCESS; | |
} | |
if (daemon(1, 0) < 0) | |
{ | |
perror(argv[0]); | |
return EXIT_FAILURE; | |
} | |
if (execvp(argv[1], argv + 1) < 0) | |
{ | |
perror(argv[1]); | |
return EXIT_FAILURE; | |
} | |
else | |
{ | |
fputs("Something wrong occurred\n", stderr); | |
return EXIT_FAILURE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment