Admin/bash_process/bash_process.c
author wenzelm
Sat, 13 Feb 2016 17:27:23 +0100
changeset 62293 72f6d6fd5853
parent 62292 Admin/exec_process/exec_process.c@486236dcd4d7
child 62300 f41884b9c4f1
permissions -rw-r--r--
clarified name;
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
62292
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
     3
Bash process with separate process group id.
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>
62292
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
     8
#include <string.h>
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
     9
#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
    10
#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
    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
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
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
    14
{
47798
03ab3bd78282 print errors on stderr;
wenzelm
parents: 47797
diff changeset
    15
  fprintf(stderr, "%s\n", msg);
62292
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    16
  fflush(stderr);
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
    17
  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
    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
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
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
    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
  /* 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
    24
62292
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    25
  if (argc < 2) {
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    26
    fprintf(stderr, "Bad arguments: missing pid file\n");
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    27
    fflush(stderr);
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
    28
    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
    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
  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
    31
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
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    33
  /* 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
    34
50290
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    35
  if (setsid() == -1) {
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    36
    pid_t pid = fork();
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    37
    if (pid == -1) fail("Cannot set session id (failed to fork)");
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    38
    else if (pid != 0) exit(0);
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    39
    else if (setsid() == -1) fail("Cannot set session id (after fork)");
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    40
  }
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
    41
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    42
50292
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    43
  /* report pid */
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    44
62292
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    45
  if (strcmp(pid_name, "-") == 0) {
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    46
    fprintf(stdout, "%d\n", getpid());
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    47
    fflush(stdout);
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    48
  }
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    49
  else if (strlen(pid_name) > 0) {
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    50
    FILE *pid_file;
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    51
    pid_file = fopen(pid_name, "w");
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    52
    if (pid_file == NULL) fail("Cannot open pid file");
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    53
    fprintf(pid_file, "%d", getpid());
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    54
    fclose(pid_file);
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    55
  }
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    56
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    57
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    58
  /* shift command line */
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    59
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    60
  int i;
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    61
  for (i = 2; i < argc; i++) {
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    62
    argv[i - 2] = argv[i];
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    63
  }
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    64
  argv[argc - 2] = NULL;
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    65
  argv[argc - 1] = NULL;
50292
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    66
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    67
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
    68
  /* 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
    69
62292
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    70
  execvp("bash", argv);
47796
c37411691ee7 more direct exec with synchronous exit code;
wenzelm
parents: 47764
diff changeset
    71
  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
    72
}