Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close stdin / stdout / stderr in child process after fork #7

Merged
merged 3 commits into from
Apr 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions god.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>

void usage() {
printf(
Expand Down Expand Up @@ -61,6 +62,7 @@ int main(int argc, char **argv) {
char user[64];
char group[64];
int foreground = 0;
struct stat exec_stat;

memset(logfile, 0, sizeof logfile);
memset(pidfile, 0, sizeof pidfile);
Expand Down Expand Up @@ -163,6 +165,17 @@ int main(int argc, char **argv) {
return 1;
}

if (stat(argv[optind], &exec_stat) < 0) {
fprintf(stderr, "failed to stat %s: %s\n",
argv[optind], strerror(errno));
return 1;
}
if (!(exec_stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
fprintf(stderr, "permission denied: %s\n",
argv[optind]);
return 1;
}

if (foreground) {
daemon_main(optind, argv);
} else {
Expand All @@ -174,6 +187,9 @@ int main(int argc, char **argv) {
if ((pid = fork())) {
exit(0);
} else if (!pid) {
close(0);
close(1);
close(2);
daemon_main(optind, argv);
} else {
perror("fork");
Expand Down