Admin/bash_process/bash_process.c
author wenzelm
Sat, 13 Feb 2016 22:52:41 +0100
changeset 62300 f41884b9c4f1
parent 62293 72f6d6fd5853
child 62567 cb4e6ca06505
permissions -rw-r--r--
actually wait for forked process and return its status -- this is not meant to be a daemon;
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>
62300
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    10
#include <sys/wait.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
    11
#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
    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();
62300
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    37
    int status;
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    38
50290
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    39
    if (pid == -1) fail("Cannot set session id (failed to fork)");
62300
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    40
    else if (pid != 0) {
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    41
      if (waitpid(pid, &status, 0) == -1) {
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    42
        fail("Cannot join forked process");
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    43
      }
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    44
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    45
      if (WIFEXITED(status)) {
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    46
        exit(WEXITSTATUS(status));
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    47
      }
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    48
      else if (WIFSIGNALED(status)) {
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    49
        exit(128 + WTERMSIG(status));
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    50
      }
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    51
      else {
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    52
        fail("Unknown status of forked process");
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    53
      }
f41884b9c4f1 actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents: 62293
diff changeset
    54
    }
50290
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    55
    else if (setsid() == -1) fail("Cannot set session id (after fork)");
735bea8d89c9 more defensive retry via fork;
wenzelm
parents: 49551
diff changeset
    56
  }
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
    57
d141f1193789 more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff changeset
    58
50292
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    59
  /* report pid */
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    60
62292
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    61
  if (strcmp(pid_name, "-") == 0) {
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    62
    fprintf(stdout, "%d\n", getpid());
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    63
    fflush(stdout);
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    64
  }
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    65
  else if (strlen(pid_name) > 0) {
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    66
    FILE *pid_file;
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    67
    pid_file = fopen(pid_name, "w");
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    68
    if (pid_file == NULL) fail("Cannot open pid file");
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    69
    fprintf(pid_file, "%d", getpid());
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    70
    fclose(pid_file);
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    71
  }
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    72
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    73
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    74
  /* shift command line */
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    75
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    76
  int i;
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    77
  for (i = 2; i < argc; i++) {
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    78
    argv[i - 2] = argv[i];
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    79
  }
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    80
  argv[argc - 2] = NULL;
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    81
  argv[argc - 1] = NULL;
50292
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    82
45fe12c9788e report proper pid *after* fork;
wenzelm
parents: 50290
diff changeset
    83
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
    84
  /* 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
    85
62292
486236dcd4d7 more flexible command-line;
wenzelm
parents: 50292
diff changeset
    86
  execvp("bash", argv);
47796
c37411691ee7 more direct exec with synchronous exit code;
wenzelm
parents: 47764
diff changeset
    87
  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
    88
}