author | wenzelm |
Wed, 09 Mar 2016 14:24:16 +0100 | |
changeset 62568 | 3541bc1e97d2 |
parent 62567 | cb4e6ca06505 |
child 62575 | 590df5f4e531 |
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 |
|
62567 | 6 |
#include <stdio.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
|
7 |
#include <stdlib.h> |
62292 | 8 |
#include <string.h> |
62567 | 9 |
#include <sys/resource.h> |
10 |
#include <sys/time.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 <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
|
12 |
#include <sys/wait.h> |
62567 | 13 |
#include <time.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
|
14 |
#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
|
15 |
|
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 |
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
|
17 |
{ |
47798 | 18 |
fprintf(stderr, "%s\n", msg); |
62292 | 19 |
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
|
20 |
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
|
21 |
} |
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 |
|
62568
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
23 |
static time_t now() |
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
24 |
{ |
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
25 |
struct timeval tv; |
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
26 |
if (gettimeofday(&tv, NULL) == 0) { |
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
27 |
return tv.tv_sec * 1000 + tv.tv_usec / 1000; |
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
28 |
} |
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
29 |
else { |
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
30 |
return time(NULL) * 1000; |
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
31 |
} |
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
32 |
} |
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
33 |
|
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
|
34 |
|
d141f1193789
more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff
changeset
|
35 |
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
|
36 |
{ |
d141f1193789
more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff
changeset
|
37 |
/* 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
|
38 |
|
62567 | 39 |
if (argc < 3) { |
40 |
fprintf(stderr, "Bad arguments: PID_FILE and TIMING_FILE required\n"); |
|
62292 | 41 |
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
|
42 |
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
|
43 |
} |
d141f1193789
more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff
changeset
|
44 |
char *pid_name = argv[1]; |
62567 | 45 |
char *timing_name = argv[2]; |
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
|
46 |
|
d141f1193789
more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff
changeset
|
47 |
|
62567 | 48 |
/* potential fork */ |
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
|
49 |
|
62568
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
50 |
time_t time_start = now(); |
62567 | 51 |
|
52 |
if (strlen(timing_name) > 0 || setsid() == -1) { |
|
50290 | 53 |
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
|
54 |
|
50290 | 55 |
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
|
56 |
else if (pid != 0) { |
62567 | 57 |
int status; |
58 |
||
62300
f41884b9c4f1
actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents:
62293
diff
changeset
|
59 |
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
|
60 |
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
|
61 |
} |
f41884b9c4f1
actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents:
62293
diff
changeset
|
62 |
|
62567 | 63 |
/* report timing */ |
64 |
||
65 |
if (strlen(timing_name) > 0) { |
|
62568
3541bc1e97d2
elapsed time in milliseconds (cf. Time.now in Poly/ML);
wenzelm
parents:
62567
diff
changeset
|
66 |
long long timing_elapsed = now() - time_start; |
62567 | 67 |
|
68 |
struct rusage ru; |
|
69 |
getrusage(RUSAGE_CHILDREN, &ru); |
|
70 |
||
71 |
long long timing_cpu = |
|
72 |
ru.ru_utime.tv_sec * 1000 + ru.ru_utime.tv_usec / 1000 + |
|
73 |
ru.ru_stime.tv_sec * 1000 + ru.ru_stime.tv_usec / 1000; |
|
74 |
||
75 |
FILE *timing_file = fopen(timing_name, "w"); |
|
76 |
if (timing_file == NULL) fail("Cannot open timing file"); |
|
77 |
fprintf(timing_file, "%lld %lld", timing_elapsed, timing_cpu); |
|
78 |
fclose(timing_file); |
|
79 |
} |
|
80 |
||
62300
f41884b9c4f1
actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents:
62293
diff
changeset
|
81 |
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
|
82 |
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
|
83 |
} |
f41884b9c4f1
actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents:
62293
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
} |
f41884b9c4f1
actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents:
62293
diff
changeset
|
87 |
else { |
f41884b9c4f1
actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents:
62293
diff
changeset
|
88 |
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
|
89 |
} |
f41884b9c4f1
actually wait for forked process and return its status -- this is not meant to be a daemon;
wenzelm
parents:
62293
diff
changeset
|
90 |
} |
50290 | 91 |
else if (setsid() == -1) fail("Cannot set session id (after fork)"); |
92 |
} |
|
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
|
93 |
|
d141f1193789
more direct bash process group invocation on Cygwin, bypassing extra sh.exe and perl.exe which tend to crash;
wenzelm
parents:
diff
changeset
|
94 |
|
50292 | 95 |
/* report pid */ |
96 |
||
62292 | 97 |
if (strcmp(pid_name, "-") == 0) { |
98 |
fprintf(stdout, "%d\n", getpid()); |
|
99 |
fflush(stdout); |
|
100 |
} |
|
101 |
else if (strlen(pid_name) > 0) { |
|
102 |
FILE *pid_file; |
|
103 |
pid_file = fopen(pid_name, "w"); |
|
104 |
if (pid_file == NULL) fail("Cannot open pid file"); |
|
105 |
fprintf(pid_file, "%d", getpid()); |
|
106 |
fclose(pid_file); |
|
107 |
} |
|
108 |
||
109 |
||
110 |
/* shift command line */ |
|
111 |
||
112 |
int i; |
|
62567 | 113 |
for (i = 3; i < argc; i++) { |
114 |
argv[i - 3] = argv[i]; |
|
62292 | 115 |
} |
62567 | 116 |
argv[argc - 3] = NULL; |
62292 | 117 |
argv[argc - 2] = NULL; |
118 |
argv[argc - 1] = NULL; |
|
50292 | 119 |
|
120 |
||
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
|
121 |
/* 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
|
122 |
|
62292 | 123 |
execvp("bash", argv); |
47796 | 124 |
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
|
125 |
} |