]>
git.gir.st - forkaftergrep.git/blob - fag.c
1 /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */
3 #define _XOPEN_SOURCE 500
4 #define _DEFAULT_SOURCE
17 int main (int argc
, char** argv
) {
18 struct opt opts
= {0, 0, 0, NULL
, NULL
, STDOUT_FILENO
, "-q"};
19 /* `-q': don't print anything; exit with 0 on match; with 1 on error. used to interface with `grep' */
23 /* generate grep options string */
24 char* p
= opts
.grepopt
+2; /* move cursor behind `q' */
26 signal (SIGPIPE
, SIG_IGN
); /* ignore broken pipe between fag and grep */
29 /* the `+' forces getopt to stop at the first non-option */
30 while ((opt
= getopt (argc
, argv
, "+t:k::eVhvEFGPiwxyU")) != -1) {
33 opts
.timeout
= atoi (optarg
);
36 opts
.kill_sig
= optarg
? atoi (optarg
) : SIGTERM
;
39 opts
.stream
= STDERR_FILENO
;
45 fprintf (stderr
, VERTEXT USAGE
47 "\t-t N\ttimeout after N seconds\n"
48 "\t-k[M]\tsend signal M to child after timeout (default: 15/SIGTERM)\n"
49 "\t-e\tgrep on stderr instead of stdout\n"
50 "\t-V\tbe verbose; print PROGRAM's stdout/stderr to stderr\n"
51 "\t-[EFP]\tgrep: matcher selection\n"
52 "\t-[iwxU]\tgrep: matching control\n", argv
[0]);
55 fprintf (stderr
, VERTEXT
);
57 /* `grep' options (Note: missing `-e:', `-f:') */
58 case 'E': case 'F': case 'G': case 'P':
59 case 'i': case 'y': case 'w': case 'x':
60 case 'U': *(p
++)=opt
; break;
63 fprintf (stderr
, "Unrecognized option: %c\n" USAGE
, optopt
, argv
[0]);
67 *p
= '\0'; /* terminate grep_options string */
69 /* the first non-option argument is the search string */
71 opts
.pattern
= argv
[optind
++];
73 fprintf (stderr
, USAGE
"(Missing PATTERN)\n", argv
[0]);
77 /* the remaining are the program to be run */
79 opts
.argv
= &(argv
[optind
]);
81 fprintf (stderr
, USAGE
"(Missing PROGRAM)\n", argv
[0]);
85 int retval
= fork_after_grep (opts
);
90 int fork_after_grep (struct opt opts
) {
98 struct timeval begin
, now
, diff
;
100 if (pipe(pipefd
) == -1) {
101 fprintf (stderr
, "pipe error (userprog)\n");
105 if ((cpid
= fork()) == -1) {
106 fprintf (stderr
, "fork error (userprog): %s", strerror (errno
));
114 dup2 (pipefd
[1], opts
.stream
);
116 close (opts
.stream
==STDOUT_FILENO
?STDERR_FILENO
:STDOUT_FILENO
);
118 if (setsid () == -1) {
119 fprintf (stderr
, "setsid error (userprog): %s", strerror (errno
));
123 execvp (opts
.argv
[0], opts
.argv
);
124 fprintf (stderr
, "exec error (userprog): %s", strerror (errno
));
125 _exit (EX_UNAVAILABLE
);
132 fcntl (pipefd
[0], F_SETFL
, fcntl (pipefd
[0], F_GETFL
, 0) | O_NONBLOCK
);
134 gettimeofday (&begin
, NULL
); /* for timeout */
136 if (pipe(grep_pipefd
) == -1) {
137 fprintf (stderr
, "pipe error (grep)\n");
142 if ((grep_cpid
= fork()) == -1) {
143 fprintf (stderr
, "fork error (grep): %s", strerror (errno
));
145 close (grep_pipefd
[0]);
146 close (grep_pipefd
[1]);
150 if (grep_cpid
== 0) {
151 close (grep_pipefd
[1]);
152 dup2 (grep_pipefd
[0], STDIN_FILENO
);
153 close (grep_pipefd
[0]);
155 close (STDERR_FILENO
);
156 close (STDOUT_FILENO
);
158 execlp ("grep", "grep", opts
.grepopt
, opts
.pattern
, NULL
);
159 fprintf (stderr
, "exec error (grep): %s", strerror (errno
));
162 close (grep_pipefd
[0]);
165 nbytes
= read (pipefd
[0], buf
, BUF_SIZE
);
171 fprintf (stderr
, "read error (userprog): %s", strerror (errno
));
173 close (grep_pipefd
[1]);
174 kill (grep_cpid
, SIGTERM
);
177 } else if (nbytes
== 0) {
178 fprintf (stderr
, "Child program exited prematurely (userprog).\n");
180 close (grep_pipefd
[1]);
181 kill (grep_cpid
, SIGTERM
);
182 if (waitpid (cpid
, &status
, WNOHANG
) > 0 && WIFEXITED (status
)) {
183 return WEXITSTATUS (status
);
185 return EX_UNAVAILABLE
;
187 /* have new userprog-data, send it to grep */
189 write(STDERR_FILENO
, buf
, nbytes
);
192 write(grep_pipefd
[1], buf
, nbytes
); /* can cause SIGPIPE if grep exited, therefore signal will be ignored */
195 if (waitpid (grep_cpid
, &grep_status
, WNOHANG
) > 0 && WIFEXITED (grep_status
)) {
196 close (grep_pipefd
[1]);
198 if (WEXITSTATUS(grep_status
) == 0) {
199 /* grep exited with match found */
200 printf ("%d\n", cpid
);
202 /* create a new child to keep pipe alive (will exit with exec'd program) */
204 while (kill(cpid
, 0) != -1 && errno
!= ESRCH
) sleep (1);
211 /* grep exited due to an error */
212 fprintf (stderr
, "grep exited due to an error.\n");
214 close (grep_pipefd
[1]);
219 if (opts
.timeout
> 0) {
220 gettimeofday (&now
, NULL
);
221 timersub (&now
, &begin
, &diff
);
222 if (diff
.tv_sec
>= opts
.timeout
) {
223 fprintf (stderr
, "Timeout reached. \n");
224 if (opts
.kill_sig
> 0) kill (cpid
, opts
.kill_sig
);
226 close (grep_pipefd
[1]);
227 kill (grep_cpid
, SIGTERM
);
228 return EX_UNAVAILABLE
;