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-- |
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 | 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 | 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 | 15 |
fprintf(stderr, "%s\n", msg); |
62292 | 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 | 25 |
if (argc < 2) { |
26 |
fprintf(stderr, "Bad arguments: missing pid file\n"); |
|
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 | 35 |
if (setsid() == -1) { |
36 |
pid_t pid = fork(); |
|
37 |
if (pid == -1) fail("Cannot set session id (failed to fork)"); |
|
38 |
else if (pid != 0) exit(0); |
|
39 |
else if (setsid() == -1) fail("Cannot set session id (after fork)"); |
|
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 | 43 |
/* report pid */ |
44 |
||
62292 | 45 |
if (strcmp(pid_name, "-") == 0) { |
46 |
fprintf(stdout, "%d\n", getpid()); |
|
47 |
fflush(stdout); |
|
48 |
} |
|
49 |
else if (strlen(pid_name) > 0) { |
|
50 |
FILE *pid_file; |
|
51 |
pid_file = fopen(pid_name, "w"); |
|
52 |
if (pid_file == NULL) fail("Cannot open pid file"); |
|
53 |
fprintf(pid_file, "%d", getpid()); |
|
54 |
fclose(pid_file); |
|
55 |
} |
|
56 |
||
57 |
||
58 |
/* shift command line */ |
|
59 |
||
60 |
int i; |
|
61 |
for (i = 2; i < argc; i++) { |
|
62 |
argv[i - 2] = argv[i]; |
|
63 |
} |
|
64 |
argv[argc - 2] = NULL; |
|
65 |
argv[argc - 1] = NULL; |
|
50292 | 66 |
|
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 | 70 |
execvp("bash", argv); |
47796 | 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 |
} |