From 2c4f0f03a9c21f934747013c06584902f22b2966 Mon Sep 17 00:00:00 2001 From: Tudor Golubenco Date: Thu, 14 Apr 2016 13:52:45 +0200 Subject: [PATCH 1/3] Close stdin / stdout / stderr in child process after fork This seems to be necessary to detach from the pseudo tty when running in background. I did the minimal change that seems to fix our issue, but other changes might be useful as well here (setsid, redirect stdout/stderr to /dev/null), etc. --- god.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/god.c b/god.c index 89ebc83..a183d5b 100644 --- a/god.c +++ b/god.c @@ -174,6 +174,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"); From 757c8c1cd8e634514bac9b5ff9876e85eab9c78d Mon Sep 17 00:00:00 2001 From: Tudor Golubenco Date: Thu, 14 Apr 2016 19:58:15 +0200 Subject: [PATCH 2/3] Stat the file to execute to report errors early Also check if it's executable. The check for executable succeeds if any of user, group or other has the x flag set. Note that this change means that the executable must be passed with the full path. --- god.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/god.c b/god.c index a183d5b..cbebbc2 100644 --- a/god.c +++ b/god.c @@ -19,6 +19,7 @@ #include #include #include +#include void usage() { printf( @@ -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); @@ -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, "file %s doesn't look executable\n", + argv[optind]); + return 1; + } + if (foreground) { daemon_main(optind, argv); } else { From 4890bd422eca772a77ebb587ec064f800854dc12 Mon Sep 17 00:00:00 2001 From: Tudor Golubenco Date: Thu, 14 Apr 2016 23:19:17 +0200 Subject: [PATCH 3/3] Changed error message in case x flag is missing --- god.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/god.c b/god.c index cbebbc2..7f63e08 100644 --- a/god.c +++ b/god.c @@ -171,7 +171,7 @@ int main(int argc, char **argv) { return 1; } if (!(exec_stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) { - fprintf(stderr, "file %s doesn't look executable\n", + fprintf(stderr, "permission denied: %s\n", argv[optind]); return 1; }