]>
git.gir.st - forkaftergrep.git/blob - fag.c
7d713f45772f70cf01f378d8c6ff4b3c41552516
1 /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */
2 //TODO: grep is missing `-e' and `-f' options
4 #define _XOPEN_SOURCE 500
5 #define _DEFAULT_SOURCE
21 void term_child(int s
) {
22 if (cpid
!= 0) kill (cpid
, s
);
26 int main (int argc
, char** argv
) {
27 struct opt opts
= {0, 0, 0, NULL
, NULL
, "/dev/null", "/dev/null", STDOUT_FILENO
, "-q"};
28 /* `-q': don't print anything; exit with 0 on match; with 1 on error. used to interface with `grep' */
32 /* generate grep options string */
33 char* p
= opts
.grepopt
+2; /* move cursor behind `q' */
35 signal (SIGPIPE
, SIG_IGN
); /* ignore broken pipe between fag and grep */
36 struct sigaction sa
= {0}; /* terminate child if fag gets interrupted/terminated */
37 sa
.sa_handler
= &term_child
; /* NOTE: will also be inherited by keep-pipe-alive-child */
38 sigaction (SIGINT
, &sa
, NULL
);
39 sigaction (SIGTERM
, &sa
, NULL
);
42 /* the `+' forces getopt to stop at the first non-option */
43 while ((opt
= getopt (argc
, argv
, "+t:k::rl:L:VhvEFGPiwxyUZJe:f:")) != -1) {
46 opts
.timeout
= atoi (optarg
);
49 opts
.kill_sig
= optarg
? atoi (optarg
) : SIGTERM
;
52 opts
.stream
= STDERR_FILENO
;
64 fprintf (stderr
, VERTEXT USAGE
66 "\t-t N\ttimeout after N seconds\n"
67 "\t-k[N]\tsend signal N to child after timeout (default: 15/SIGTERM)\n"
68 "\t-r\tgrep on stderr instead of stdout\n"
69 "\t-l FILE\tlog PROGRAM's stdout to FILE\n"
70 "\t-L FILE\tlog PROGRAM's stderr to FILE\n"
71 "\t-V\tbe verbose; print monitored stream to stderr\n"
72 "\t-[EFGP]\tgrep: matcher selection\n"
73 "\t-[iwxU]\tgrep: matching control\n"
74 "\t-[ZJ]\tgrep: decompression control\n", argv
[0]);
77 fprintf (stderr
, VERTEXT
);
80 fprintf (stderr
, "`grep -e' and `-f' are not implemented yet!\n");
83 case 'E': case 'F': case 'G': case 'P':
84 case 'i': case 'y': case 'w': case 'x':
85 case 'U': case 'Z': case 'J':
90 fprintf (stderr
, "Unrecognized option: %c\n" USAGE
, optopt
, argv
[0]);
94 *p
= '\0'; /* terminate grep_options string */
96 /* the first non-option argument is the search string */
98 opts
.pattern
= argv
[optind
++];
100 fprintf (stderr
, USAGE
"(Missing PATTERN)\n", argv
[0]);
104 /* the remaining are the program to be run */
106 opts
.argv
= &(argv
[optind
]);
108 fprintf (stderr
, USAGE
"(Missing PROGRAM)\n", argv
[0]);
112 int retval
= fork_after_grep (opts
);
117 int fork_after_grep (struct opt opts
) {
119 int pipefd_cpid
[2]; /* IPC to extract PID from daemonized userprog */
124 int secondary_logfile
;
129 struct timeval begin
, now
, diff
;
131 if (pipe(pipefd
) == -1) {
132 fprintf (stderr
, "pipe error (userprog)\n");
136 if(pipe(pipefd_cpid
) == -1) {
137 fprintf (stderr
, "pipe error (cpid-ipc)\n");
143 /* Writing the -l and -L log files is a bit messy. We can easily just pipe the stream we aren't monitoring
144 into the secondary_logfile, but we can't do that for the primary_logfile. This one we write to from the piping
145 process, where -V is also handled at and in the keep-pipe-alive-daemon. */
146 primary_logfile
= open((opts
.stream
==STDOUT_FILENO
?opts
.logout
:opts
.logerr
), O_WRONLY
|O_APPEND
|O_CREAT
, 0600);
147 if (primary_logfile
== -1) {
148 fprintf (stderr
, "error opening logfile (stdout): %s\n", strerror(errno
));
151 close (pipefd_cpid
[0]);
152 close (pipefd_cpid
[1]);
155 secondary_logfile
= open((opts
.stream
==STDOUT_FILENO
?opts
.logerr
:opts
.logout
), O_WRONLY
|O_APPEND
|O_CREAT
, 0600);
156 if (secondary_logfile
== -1) {
157 fprintf (stderr
, "error opening logfile (stderr): %s\n", strerror(errno
));
160 close (pipefd_cpid
[0]);
161 close (pipefd_cpid
[1]);
162 close (primary_logfile
);
166 if ((tmp_cpid
= fork()) == -1) {
167 fprintf (stderr
, "fork error (daemonizer): %s", strerror (errno
));
170 close (pipefd_cpid
[0]);
171 close (pipefd_cpid
[1]);
172 close (primary_logfile
);
173 close (secondary_logfile
);
181 dup2 (pipefd
[1], opts
.stream
);
183 dup2 (secondary_logfile
, opts
.stream
==STDOUT_FILENO
?STDERR_FILENO
:STDOUT_FILENO
);
184 close (primary_logfile
);
185 close (secondary_logfile
);
187 if (setsid () == -1) {
188 fprintf (stderr
, "setsid error (daemonizer): %s", strerror (errno
));
192 if ((cpid_userprog
= fork()) == -1) {
193 fprintf (stderr
, "fork error (userprog): %s", strerror (errno
));
196 if (cpid_userprog
== 0) {
197 close(pipefd_cpid
[0]);
198 close(pipefd_cpid
[1]);
200 execvp (opts
.argv
[0], opts
.argv
);
201 fprintf (stderr
, "exec error (userprog): %s", strerror (errno
));
203 /* only way to get final child's pid to main is through IPC */
204 write(pipefd_cpid
[1], &cpid_userprog
, sizeof(pid_t
));
205 close(pipefd_cpid
[0]);
206 close(pipefd_cpid
[1]);
208 _exit (EX_UNAVAILABLE
);
214 /* read userprog's PID from IPC: */
215 read(pipefd_cpid
[0], &cpid
, sizeof(pid_t
));
216 close(pipefd_cpid
[0]);
217 close(pipefd_cpid
[1]);
220 close (secondary_logfile
);
221 fcntl (pipefd
[0], F_SETFL
, fcntl (pipefd
[0], F_GETFL
, 0) | O_NONBLOCK
);
223 gettimeofday (&begin
, NULL
); /* for timeout */
225 if (pipe(grep_pipefd
) == -1) {
226 fprintf (stderr
, "pipe error (grep)\n");
231 if ((grep_cpid
= fork()) == -1) {
232 fprintf (stderr
, "fork error (grep): %s", strerror (errno
));
234 close (grep_pipefd
[0]);
235 close (grep_pipefd
[1]);
239 if (grep_cpid
== 0) {
240 close (grep_pipefd
[1]);
241 dup2 (grep_pipefd
[0], STDIN_FILENO
);
242 close (grep_pipefd
[0]);
244 close (STDOUT_FILENO
);
246 execlp ("grep", "grep", opts
.grepopt
, "--", opts
.pattern
, NULL
);
247 fprintf (stderr
, "exec error (grep): %s", strerror (errno
));
250 close (grep_pipefd
[0]);
253 nbytes
= read (pipefd
[0], buf
, BUF_SIZE
);
259 fprintf (stderr
, "read error (userprog): %s", strerror (errno
));
261 close (grep_pipefd
[1]);
262 kill (grep_cpid
, SIGTERM
);
265 } else if (nbytes
== 0) {
266 fprintf (stderr
, "Child program exited prematurely (userprog).\n");
268 close (grep_pipefd
[1]);
269 kill (grep_cpid
, SIGTERM
);
270 if (waitpid (cpid
, &status
, WNOHANG
) > 0 && WIFEXITED (status
)) {
271 return WEXITSTATUS (status
);
273 return EX_UNAVAILABLE
;
275 /* have new userprog-data, send it to grep */
277 write(STDERR_FILENO
, buf
, nbytes
);
280 write(primary_logfile
, buf
, nbytes
);
282 write(grep_pipefd
[1], buf
, nbytes
); /* can cause SIGPIPE if grep exited, therefore signal will be ignored */
285 if (waitpid (grep_cpid
, &grep_status
, WNOHANG
) > 0 && WIFEXITED (grep_status
)) {
286 close (grep_pipefd
[1]);
288 if (WEXITSTATUS(grep_status
) == 0) {
289 /* grep exited with match found */
290 printf ("%d\n", cpid
);
293 /* create a new child to keep pipe alive, empty it and write log files (will exit with exec'd program) */
294 if (!fork()){setsid();if(!fork()) {
295 close(0); close(1); close(2); umask(0); chdir("/");
298 nbytes
= read (pipefd
[0], buf
, BUF_SIZE
);
300 if ((nbytes
== -1 && errno
!= EAGAIN
) || nbytes
== 0) break;
302 write(primary_logfile
, buf
, nbytes
);
305 close (primary_logfile
);
309 close (primary_logfile
);
312 /* grep exited due to an error */
313 fprintf (stderr
, "grep exited due to an error.\n");
315 close (grep_pipefd
[1]);
316 close (primary_logfile
);
321 if (opts
.timeout
> 0) {
322 gettimeofday (&now
, NULL
);
323 timersub (&now
, &begin
, &diff
);
324 if (diff
.tv_sec
>= opts
.timeout
) {
325 fprintf (stderr
, "Timeout reached. \n");
326 if (opts
.kill_sig
> 0) kill (cpid
, opts
.kill_sig
);
328 close (grep_pipefd
[1]);
329 close (primary_logfile
);
330 kill (grep_cpid
, SIGTERM
);
331 return EX_UNAVAILABLE
;