aboutsummaryrefslogtreecommitdiffstats
path: root/src/reapd.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-09-23 01:56:07 +0200
committerMattias Andrée <maandree@operamail.com>2014-09-23 01:56:07 +0200
commitbf97a42ac16ac031cddc6d67eff2db9e9a6e221f (patch)
tree35fff8077201177190a1a5b7805b99ea83d96d17 /src/reapd.c
parentadd readme (diff)
downloadorphan-reaper-1665434291513a86f6911147dc2f120a0c3a87da.tar.gz
orphan-reaper-1665434291513a86f6911147dc2f120a0c3a87da.tar.bz2
orphan-reaper-1665434291513a86f6911147dc2f120a0c3a87da.tar.xz
m + rename the command orphan-reaper and exec to reapd1
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/reapd.c')
-rw-r--r--src/reapd.c89
1 files changed, 10 insertions, 79 deletions
diff --git a/src/reapd.c b/src/reapd.c
index 5ffecd5..be74690 100644
--- a/src/reapd.c
+++ b/src/reapd.c
@@ -15,94 +15,25 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
+#include <stddef.h>
#include <sys/types.h>
#include <sys/wait.h>
-#include <sys/prctl.h>
-
-
-
-static void usage(void)
-{
- printf("USAGE: reapd [--fatal] [--] <command...>\n");
- printf("\n");
- printf("Unless `--fatal` is used, the program will run <command>\n");
- printf("even if the process cound not be marked as a child subreaper.\n");
- printf("\n");
-}
+#include <errno.h>
+#include <stdio.h>
int main(int argc, char** argv)
{
- int i, j, r, fatal = 0;
- char** exec_argv;
pid_t pid;
+ (void) argc;
- if ((argc < 2) || !strcmp(argv[1], "--help"))
- return usage(), argc < 2 ? 1 : 0;
-
- for (i = 1; i < argc; i++)
- if (!strcmp(argv[i], "--fatal"))
- fatal = 1;
- else if (!strcmp(argv[i], "--"))
+ for (;;)
+ if (pid = wait(NULL), pid == -1)
{
- i++;
- break;
+ if (errno == ECHILD)
+ return 0;
+ else if (errno != EINTR)
+ return perror(*argv), 2;
}
- else if (strstr(argv[i], "-") == argv[i])
- return usage(), 1;
- else
- break;
-
- if (i == argc)
- return usage(), 1;
-
- exec_argv = malloc((argc - i + 1) * sizeof(char*));
- if (exec_argv == NULL)
- return perror(*argv), 2;
- exec_argv[argc - i] = NULL;
- for (j = 0; i < argc; j++, i++)
- exec_argv[j] = argv[i];
-
- r = prctl(PR_SET_CHILD_SUBREAPER, 1);
- if (r < 0)
- goto error;
-
- pid = fork();
- if (pid == -1)
- goto error;
-
- if (pid == 0)
- goto exec;
- else
- {
- free(exec_argv);
- for (;;)
- if (pid = wait(NULL), pid == -1)
- {
- if (errno == ECHILD)
- return 0;
- else if (errno != EINTR)
- return perror(*argv), 2;
- }
- }
-
- exec:
- (void) prctl(PR_SET_CHILD_SUBREAPER, 0);
- execvp(*exec_argv, exec_argv);
- perror(*argv);
- fail:
- free(exec_argv);
- return 2;
-
- error:
- perror(*argv);
- if (fatal)
- goto fail;
- goto exec;
}