Skip to content

Commit

Permalink
Merge pull request #7 from tsg/close_fds_after_fork
Browse files Browse the repository at this point in the history
Close stdin / stdout / stderr in child process after fork
  • Loading branch information
fiorix committed Apr 14, 2016
2 parents 8e41529 + 4890bd4 commit c6e0d1a
Showing 1 changed file with 16 additions and 0 deletions.
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

0 comments on commit c6e0d1a

Please sign in to comment.