Admin/exec_process/exec_process.c
author wenzelm
Wed, 19 Aug 2015 21:51:30 +0200
changeset 60979 fb3a641bc914
parent 50292 45fe12c9788e
child 62292 486236dcd4d7
permissions -rw-r--r--
clarified x86-windows setup;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47764
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
     1
/*  Author:     Makarius
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
     2
47797
63152f78e18b general exec_process -- nothing specific to Cygwin;
wenzelm
parents: 47796
diff changeset
     3
Bash process group invocation.
47764
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
     4
*/
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
     5
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
     6
#include <stdlib.h>
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
     7
#include <stdio.h>
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
     8
#include <sys/types.h>
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
     9
#include <unistd.h>
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    10
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    11
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    12
static void fail(const char *msg)
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    13
{
47798
03ab3bd78282 print errors on stderr;
wenzelm
parents: 47797
diff changeset
    14
  fprintf(stderr, "%s\n", msg);
47764
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    15
  exit(2);
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    16
}
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    17
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    18
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    19
int main(int argc, char *argv[])
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    20
{
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    21
  /* args */
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    22
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    23
  if (argc != 3) {
47798
03ab3bd78282 print errors on stderr;
wenzelm
parents: 47797
diff changeset
    24
    fprintf(stderr, "Bad arguments\n");
47764
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    25
    exit(1);
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    26
  }
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    27
  char *pid_name = argv[1];
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    28
  char *script = argv[2];
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    29
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    30
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    31
  /* setsid */
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    32
50290
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    33
  if (setsid() == -1) {
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    34
    pid_t pid = fork();
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    35
    if (pid == -1) fail("Cannot set session id (failed to fork)");
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    36
    else if (pid != 0) exit(0);
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    37
    else if (setsid() == -1) fail("Cannot set session id (after fork)");
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    38
  }
47764
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    39
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    40
50292
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    41
  /* report pid */
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    42
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    43
  FILE *pid_file;
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    44
  pid_file = fopen(pid_name, "w");
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    45
  if (pid_file == NULL) fail("Cannot open pid file");
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    46
  fprintf(pid_file, "%d", getpid());
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    47
  fclose(pid_file);
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    48
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    49
47764
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    50
  /* exec */
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    51
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    52
  char *cmd_line[4];
49551
9b12fcd0a889 search bash via PATH as usual (this is no longer restricted to Cygwin with its known file-system layout);
wenzelm
parents: 49447
diff changeset
    53
  cmd_line[0] = "bash";
47764
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    54
  cmd_line[1] = "-c";
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    55
  cmd_line[2] = script;
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    56
  cmd_line[3] = NULL;
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    57
49551
9b12fcd0a889 search bash via PATH as usual (this is no longer restricted to Cygwin with its known file-system layout);
wenzelm
parents: 49447
diff changeset
    58
  execvp(cmd_line[0], cmd_line);
47796
c37411691ee7 more direct exec with synchronous exit code;
wenzelm
parents: 47764
diff changeset
    59
  fail("Cannot exec process");
47764
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    60
}
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    61