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;
wenzelm@47764
     1
/*  Author:     Makarius
wenzelm@47764
     2
wenzelm@47797
     3
Bash process group invocation.
wenzelm@47764
     4
*/
wenzelm@47764
     5
wenzelm@47764
     6
#include <stdlib.h>
wenzelm@47764
     7
#include <stdio.h>
wenzelm@47764
     8
#include <sys/types.h>
wenzelm@47764
     9
#include <unistd.h>
wenzelm@47764
    10
wenzelm@47764
    11
wenzelm@47764
    12
static void fail(const char *msg)
wenzelm@47764
    13
{
wenzelm@47798
    14
  fprintf(stderr, "%s\n", msg);
wenzelm@47764
    15
  exit(2);
wenzelm@47764
    16
}
wenzelm@47764
    17
wenzelm@47764
    18
wenzelm@47764
    19
int main(int argc, char *argv[])
wenzelm@47764
    20
{
wenzelm@47764
    21
  /* args */
wenzelm@47764
    22
wenzelm@47764
    23
  if (argc != 3) {
wenzelm@47798
    24
    fprintf(stderr, "Bad arguments\n");
wenzelm@47764
    25
    exit(1);
wenzelm@47764
    26
  }
wenzelm@47764
    27
  char *pid_name = argv[1];
wenzelm@47764
    28
  char *script = argv[2];
wenzelm@47764
    29
wenzelm@47764
    30
wenzelm@47764
    31
  /* report pid */
wenzelm@47764
    32
wenzelm@47764
    33
  FILE *pid_file;
wenzelm@47764
    34
  pid_file = fopen(pid_name, "w");
wenzelm@47764
    35
  if (pid_file == NULL) fail("Cannot open pid file");
wenzelm@47764
    36
  fprintf(pid_file, "%d", getpid());
wenzelm@47764
    37
  fclose(pid_file);
wenzelm@47764
    38
wenzelm@47764
    39
wenzelm@47764
    40
  /* setsid */
wenzelm@47764
    41
wenzelm@47764
    42
  if (getgid() == getpid()) fail("Cannot set session id");
wenzelm@47764
    43
  setsid();
wenzelm@47764
    44
wenzelm@47764
    45
wenzelm@47764
    46
  /* exec */
wenzelm@47764
    47
wenzelm@47764
    48
  char *cmd_line[4];
wenzelm@49551
    49
  cmd_line[0] = "bash";
wenzelm@47764
    50
  cmd_line[1] = "-c";
wenzelm@47764
    51
  cmd_line[2] = script;
wenzelm@47764
    52
  cmd_line[3] = NULL;
wenzelm@47764
    53
wenzelm@49551
    54
  execvp(cmd_line[0], cmd_line);
wenzelm@47796
    55
  fail("Cannot exec process");
wenzelm@47764
    56
}
wenzelm@47764
    57