Admin/exec_process/exec_process.c
author wenzelm
Fri Sep 28 22:53:18 2012 +0200 (2012-09-28 ago)
changeset 49650 9fad6480300d
parent 49551 9b12fcd0a889
child 50290 735bea8d89c9
permissions -rw-r--r--
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
     1 /*  Author:     Makarius
     2 
     3 Bash process group invocation.
     4 */
     5 
     6 #include <stdlib.h>
     7 #include <stdio.h>
     8 #include <sys/types.h>
     9 #include <unistd.h>
    10 
    11 
    12 static void fail(const char *msg)
    13 {
    14   fprintf(stderr, "%s\n", msg);
    15   exit(2);
    16 }
    17 
    18 
    19 int main(int argc, char *argv[])
    20 {
    21   /* args */
    22 
    23   if (argc != 3) {
    24     fprintf(stderr, "Bad arguments\n");
    25     exit(1);
    26   }
    27   char *pid_name = argv[1];
    28   char *script = argv[2];
    29 
    30 
    31   /* report pid */
    32 
    33   FILE *pid_file;
    34   pid_file = fopen(pid_name, "w");
    35   if (pid_file == NULL) fail("Cannot open pid file");
    36   fprintf(pid_file, "%d", getpid());
    37   fclose(pid_file);
    38 
    39 
    40   /* setsid */
    41 
    42   if (getgid() == getpid()) fail("Cannot set session id");
    43   setsid();
    44 
    45 
    46   /* exec */
    47 
    48   char *cmd_line[4];
    49   cmd_line[0] = "bash";
    50   cmd_line[1] = "-c";
    51   cmd_line[2] = script;
    52   cmd_line[3] = NULL;
    53 
    54   execvp(cmd_line[0], cmd_line);
    55   fail("Cannot exec process");
    56 }
    57