author | wenzelm |
Sat, 13 Feb 2016 22:52:41 +0100 | |
changeset 62300 | f41884b9c4f1 |
parent 62293 | 72f6d6fd5853 |
child 62567 | cb4e6ca06505 |
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> |
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 | 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(); |
|
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 | 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 | 55 |
else if (setsid() == -1) fail("Cannot set session id (after fork)"); |
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 | 59 |
/* report pid */ |
60 |
||
62292 | 61 |
if (strcmp(pid_name, "-") == 0) { |
62 |
fprintf(stdout, "%d\n", getpid()); |
|
63 |
fflush(stdout); |
|
64 |
} |
|
65 |
else if (strlen(pid_name) > 0) { |
|
66 |
FILE *pid_file; |
|
67 |
pid_file = fopen(pid_name, "w"); |
|
68 |
if (pid_file == NULL) fail("Cannot open pid file"); |
|
69 |
fprintf(pid_file, "%d", getpid()); |
|
70 |
fclose(pid_file); |
|
71 |
} |
|
72 |
||
73 |
||
74 |
/* shift command line */ |
|
75 |
||
76 |
int i; |
|
77 |
for (i = 2; i < argc; i++) { |
|
78 |
argv[i - 2] = argv[i]; |
|
79 |
} |
|
80 |
argv[argc - 2] = NULL; |
|
81 |
argv[argc - 1] = NULL; |
|
50292 | 82 |
|
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 | 86 |
execvp("bash", argv); |
47796 | 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 |
} |