]>
Commit | Line | Data |
---|---|---|
1 | /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */ | |
2 | //TODO: grep is missing `-e' and `-f' options | |
3 | ||
4 | #define _XOPEN_SOURCE 500 | |
5 | #define _DEFAULT_SOURCE | |
6 | #include <errno.h> | |
7 | #include <fcntl.h> | |
8 | #include <libgen.h> | |
9 | #include <signal.h> | |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <sysexits.h> | |
14 | #include <sys/stat.h> | |
15 | #include <sys/time.h> | |
16 | #include <sys/wait.h> | |
17 | #include <unistd.h> | |
18 | #include "fag.h" | |
19 | ||
20 | pid_t cpid = 0; | |
21 | ||
22 | void term_child(int s) { | |
23 | if (cpid != 0) kill (cpid, s); | |
24 | exit(1); | |
25 | } | |
26 | ||
27 | int main (int argc, char** argv) { | |
28 | struct opt opts = {0, 0, 0, NULL, NULL, "/dev/null", "/dev/null", STDOUT_FILENO, "-q"}; | |
29 | /* `-q': don't print anything; exit with 0 on match; with 1 on error. used to interface with `grep' */ | |
30 | int opt; | |
31 | opterr = 0; | |
32 | ||
33 | /* generate grep options string */ | |
34 | char* p = opts.grepopt+2; /* move cursor behind `q' */ | |
35 | ||
36 | signal (SIGPIPE, SIG_IGN); /* ignore broken pipe between fag and grep */ | |
37 | struct sigaction sa = {0}; /* terminate child if fag gets interrupted/terminated */ | |
38 | sa.sa_handler = &term_child; /* NOTE: will also be inherited by keep-pipe-alive-child */ | |
39 | sigaction (SIGINT, &sa, NULL); | |
40 | sigaction (SIGTERM, &sa, NULL); | |
41 | ||
42 | ||
43 | /* the `+' forces getopt to stop at the first non-option */ | |
44 | while ((opt = getopt (argc, argv, "+t:k::rl:L:VhvEFGPiwxyUZJe:f:")) != -1) { | |
45 | switch (opt) { | |
46 | case 't': | |
47 | opts.timeout = atoi (optarg); | |
48 | break; | |
49 | case 'k': | |
50 | opts.kill_sig = optarg ? atoi (optarg) : SIGTERM; | |
51 | break; | |
52 | case 'r': | |
53 | opts.stream = STDERR_FILENO; | |
54 | break; | |
55 | case 'l': | |
56 | opts.logout = optarg; | |
57 | break; | |
58 | case 'L': | |
59 | opts.logerr = optarg; | |
60 | break; | |
61 | case 'V': | |
62 | opts.verbose = 1; | |
63 | break; | |
64 | case 'h': | |
65 | fprintf (stderr, VERTEXT USAGE | |
66 | "Options:\n" | |
67 | "\t-t N\ttimeout after N seconds\n" | |
68 | "\t-k[N]\tsend signal N to child after timeout (default: 15/SIGTERM)\n" | |
69 | "\t-r\tgrep on stderr instead of stdout\n" | |
70 | "\t-l FILE\tlog PROGRAM's stdout to FILE\n" | |
71 | "\t-L FILE\tlog PROGRAM's stderr to FILE\n" | |
72 | "\t-V\tbe verbose; print monitored stream to stderr\n" | |
73 | "\t-[EFGP]\tgrep: matcher selection\n" | |
74 | "\t-[iwxU]\tgrep: matching control\n" | |
75 | "\t-[ZJ]\tgrep: decompression control\n", argv[0]); | |
76 | return EX_OK; | |
77 | case 'v': | |
78 | fprintf (stderr, VERTEXT); | |
79 | return EX_OK; | |
80 | case 'e': case 'f': | |
81 | fprintf (stderr, "`grep -e' and `-f' are not implemented yet!\n"); | |
82 | return EX_SOFTWARE; | |
83 | break; | |
84 | case 'E': case 'F': case 'G': case 'P': | |
85 | case 'i': case 'y': case 'w': case 'x': | |
86 | case 'U': case 'Z': case 'J': | |
87 | *(p++)=opt; | |
88 | break; | |
89 | ||
90 | default: | |
91 | fprintf (stderr, "Unrecognized option: %c\n" USAGE, optopt, argv[0]); | |
92 | return EX_USAGE; | |
93 | } | |
94 | } | |
95 | *p = '\0'; /* terminate grep_options string */ | |
96 | ||
97 | /* the first non-option argument is the search string */ | |
98 | if (optind < argc) { | |
99 | opts.pattern = argv[optind++]; | |
100 | } else { | |
101 | fprintf (stderr, USAGE "(Missing PATTERN)\n", argv[0]); | |
102 | return EX_USAGE; | |
103 | } | |
104 | ||
105 | /* the remaining are the program to be run */ | |
106 | if (optind < argc) { | |
107 | opts.argv = &(argv[optind]); | |
108 | } else { | |
109 | fprintf (stderr, USAGE "(Missing PROGRAM)\n", argv[0]); | |
110 | return EX_USAGE; | |
111 | } | |
112 | ||
113 | int retval = fork_after_grep (opts); | |
114 | ||
115 | return retval; | |
116 | } | |
117 | ||
118 | int fork_after_grep (struct opt opts) { | |
119 | int pipefd[2]; | |
120 | int pipefd_cpid[2]; /* IPC to extract PID from daemonized userprog */ | |
121 | pid_t tmp_cpid; | |
122 | int status; | |
123 | ||
124 | int primary_logfile; | |
125 | int secondary_logfile; | |
126 | ||
127 | char buf[BUF_SIZE]; | |
128 | int nbytes; | |
129 | ||
130 | struct timeval begin, now, diff; | |
131 | ||
132 | if (pipe(pipefd) == -1) { | |
133 | fprintf (stderr, "pipe error (userprog)\n"); | |
134 | return EX_OSERR; | |
135 | } | |
136 | ||
137 | if(pipe(pipefd_cpid) == -1) { | |
138 | fprintf (stderr, "pipe error (cpid-ipc)\n"); | |
139 | close (pipefd[0]); | |
140 | close (pipefd[1]); | |
141 | return EX_OSERR; | |
142 | } | |
143 | ||
144 | /* Writing the -l and -L log files is a bit messy. We can easily just pipe the stream we aren't monitoring | |
145 | into the secondary_logfile, but we can't do that for the primary_logfile. This one we write to from the piping | |
146 | process, where -V is also handled at and in the keep-pipe-alive-daemon. */ | |
147 | primary_logfile = open((opts.stream==STDOUT_FILENO?opts.logout:opts.logerr), O_WRONLY|O_APPEND|O_CREAT, 0600); | |
148 | if (primary_logfile == -1) { | |
149 | fprintf (stderr, "error opening logfile (stdout): %s\n", strerror(errno)); | |
150 | close (pipefd[0]); | |
151 | close (pipefd[1]); | |
152 | close (pipefd_cpid[0]); | |
153 | close (pipefd_cpid[1]); | |
154 | return EX_OSFILE; | |
155 | } | |
156 | secondary_logfile = open((opts.stream==STDOUT_FILENO?opts.logerr:opts.logout), O_WRONLY|O_APPEND|O_CREAT, 0600); | |
157 | if (secondary_logfile == -1) { | |
158 | fprintf (stderr, "error opening logfile (stderr): %s\n", strerror(errno)); | |
159 | close (pipefd[0]); | |
160 | close (pipefd[1]); | |
161 | close (pipefd_cpid[0]); | |
162 | close (pipefd_cpid[1]); | |
163 | close (primary_logfile); | |
164 | return EX_OSFILE; | |
165 | } | |
166 | ||
167 | if ((tmp_cpid = fork()) == -1) { | |
168 | fprintf (stderr, "fork error (daemonizer): %s", strerror (errno)); | |
169 | close (pipefd[0]); | |
170 | close (pipefd[1]); | |
171 | close (pipefd_cpid[0]); | |
172 | close (pipefd_cpid[1]); | |
173 | close (primary_logfile); | |
174 | close (secondary_logfile); | |
175 | return EX_OSERR; | |
176 | } | |
177 | ||
178 | if (tmp_cpid == 0) { | |
179 | pid_t cpid_userprog; | |
180 | ||
181 | close (pipefd[0]); | |
182 | dup2 (pipefd[1], opts.stream); | |
183 | close (pipefd[1]); | |
184 | dup2 (secondary_logfile, opts.stream==STDOUT_FILENO?STDERR_FILENO:STDOUT_FILENO); | |
185 | close (primary_logfile); | |
186 | close (secondary_logfile); | |
187 | ||
188 | if (setsid () == -1) { | |
189 | fprintf (stderr, "setsid error (daemonizer): %s", strerror (errno)); | |
190 | _exit (EX_OSERR); | |
191 | } | |
192 | ||
193 | if ((cpid_userprog = fork()) == -1) { | |
194 | fprintf (stderr, "fork error (userprog): %s", strerror (errno)); | |
195 | _exit (EX_OSERR); | |
196 | } | |
197 | if (cpid_userprog == 0) { | |
198 | close(pipefd_cpid[0]); | |
199 | close(pipefd_cpid[1]); | |
200 | ||
201 | execvp (opts.argv[0], opts.argv); | |
202 | fprintf (stderr, "exec error (userprog): %s", strerror (errno)); | |
203 | } else { | |
204 | /* only way to get final child's pid to main is through IPC */ | |
205 | write(pipefd_cpid[1], &cpid_userprog, sizeof(pid_t)); | |
206 | close(pipefd_cpid[0]); | |
207 | close(pipefd_cpid[1]); | |
208 | } | |
209 | _exit (EX_UNAVAILABLE); | |
210 | } else { | |
211 | pid_t grep_cpid; | |
212 | int grep_pipefd[2]; | |
213 | int grep_status; | |
214 | ||
215 | /* read userprog's PID from IPC: */ | |
216 | read(pipefd_cpid[0], &cpid, sizeof(pid_t)); | |
217 | close(pipefd_cpid[0]); | |
218 | close(pipefd_cpid[1]); | |
219 | ||
220 | close (pipefd[1]); | |
221 | close (secondary_logfile); | |
222 | fcntl (pipefd[0], F_SETFL, fcntl (pipefd[0], F_GETFL, 0) | O_NONBLOCK); | |
223 | ||
224 | gettimeofday (&begin, NULL); /* for timeout */ | |
225 | ||
226 | if (pipe(grep_pipefd) == -1) { | |
227 | fprintf (stderr, "pipe error (grep)\n"); | |
228 | close (pipefd[0]); | |
229 | return EX_OSERR; | |
230 | } | |
231 | ||
232 | if ((grep_cpid = fork()) == -1) { | |
233 | fprintf (stderr, "fork error (grep): %s", strerror (errno)); | |
234 | close (pipefd[0]); | |
235 | close (grep_pipefd[0]); | |
236 | close (grep_pipefd[1]); | |
237 | return EX_OSERR; | |
238 | } | |
239 | ||
240 | if (grep_cpid == 0) { | |
241 | close (grep_pipefd[1]); | |
242 | dup2 (grep_pipefd[0], STDIN_FILENO); | |
243 | close (grep_pipefd[0]); | |
244 | ||
245 | close (STDOUT_FILENO); | |
246 | ||
247 | char* grep_override = getenv("GREP_OVERRIDE"); | |
248 | if (grep_override != NULL) { | |
249 | execlp (grep_override, basename(grep_override), opts.grepopt, "--", opts.pattern, NULL); | |
250 | } else { | |
251 | execlp ("grep", "grep", opts.grepopt, "--", opts.pattern, NULL); | |
252 | } | |
253 | fprintf (stderr, "exec error (grep): %s", strerror (errno)); | |
254 | _exit (EX_SOFTWARE); | |
255 | } else { | |
256 | close (grep_pipefd[0]); | |
257 | for (;;) { | |
258 | usleep (20000); | |
259 | nbytes = read (pipefd[0], buf, BUF_SIZE); | |
260 | if (nbytes == -1) { | |
261 | switch (errno) { | |
262 | case EAGAIN: | |
263 | break; | |
264 | default: | |
265 | fprintf (stderr, "read error (userprog): %s", strerror (errno)); | |
266 | close (pipefd[0]); | |
267 | close (grep_pipefd[1]); | |
268 | kill (grep_cpid, SIGTERM); | |
269 | return EX_IOERR; | |
270 | } | |
271 | } else if (nbytes == 0) { | |
272 | fprintf (stderr, "Child program exited prematurely (userprog).\n"); | |
273 | close (pipefd[0]); | |
274 | close (grep_pipefd[1]); | |
275 | kill (grep_cpid, SIGTERM); | |
276 | if (waitpid (cpid, &status, WNOHANG) > 0 && WIFEXITED (status)) { | |
277 | return WEXITSTATUS (status); | |
278 | } | |
279 | return EX_UNAVAILABLE; | |
280 | } else { | |
281 | /* have new userprog-data, send it to grep */ | |
282 | if (opts.verbose) { | |
283 | write(STDERR_FILENO, buf, nbytes); | |
284 | } | |
285 | ||
286 | write(primary_logfile, buf, nbytes); | |
287 | ||
288 | write(grep_pipefd[1], buf, nbytes); /* can cause SIGPIPE if grep exited, therefore signal will be ignored */ | |
289 | } | |
290 | ||
291 | if (waitpid (grep_cpid, &grep_status, WNOHANG) > 0 && WIFEXITED (grep_status)) { | |
292 | close (grep_pipefd[1]); | |
293 | ||
294 | if (WEXITSTATUS(grep_status) == 0) { | |
295 | /* grep exited with match found */ | |
296 | printf ("%d\n", cpid); | |
297 | fflush (stdout); | |
298 | ||
299 | /* create a new child to keep pipe alive, empty it and write log files (will exit with exec'd program) */ | |
300 | if (!fork()){setsid();if(!fork()) { | |
301 | close(0); close(1); close(2); umask(0); chdir("/"); | |
302 | for (;;) { | |
303 | usleep (20000); | |
304 | nbytes = read (pipefd[0], buf, BUF_SIZE); | |
305 | ||
306 | if ((nbytes == -1 && errno != EAGAIN) || nbytes == 0) break; | |
307 | ||
308 | write(primary_logfile, buf, nbytes); | |
309 | } | |
310 | close (pipefd[0]); | |
311 | close (primary_logfile); | |
312 | _exit(0); | |
313 | }} | |
314 | close (pipefd[0]); | |
315 | close (primary_logfile); | |
316 | return EX_OK; | |
317 | } else { | |
318 | /* grep exited due to an error */ | |
319 | fprintf (stderr, "grep exited due to an error.\n"); | |
320 | close (pipefd[0]); | |
321 | close (grep_pipefd[1]); | |
322 | close (primary_logfile); | |
323 | return EX_IOERR; | |
324 | } | |
325 | } | |
326 | ||
327 | if (opts.timeout > 0) { | |
328 | gettimeofday (&now, NULL); | |
329 | timersub (&now, &begin, &diff); | |
330 | if (diff.tv_sec >= opts.timeout) { | |
331 | fprintf (stderr, "Timeout reached. \n"); | |
332 | printf ("%d\n", cpid); | |
333 | fflush (stdout); | |
334 | ||
335 | if (opts.kill_sig > 0) kill (cpid, opts.kill_sig); | |
336 | ||
337 | close (pipefd[0]); | |
338 | close (grep_pipefd[1]); | |
339 | close (primary_logfile); | |
340 | kill (grep_cpid, SIGTERM); | |
341 | return EX_UNAVAILABLE; | |
342 | } | |
343 | } | |
344 | } | |
345 | } | |
346 | } | |
347 | } |