]>
git.gir.st - forkaftergrep.git/blob - fag.c
1 /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */
2 //TODO: if grep exits with an error, fag thinks a match was found (e.g. grep -BEP, killall grep)
3 //TODO: allow redirect of both streams to files
4 //TODO: grep is missing `-e' and `-f' options
6 #define _XOPEN_SOURCE 500
7 #define _DEFAULT_SOURCE
22 void term_child(int s
) {
23 (void)s
; /* squash -Werror=unused-parameter */
24 if (cpid
!= 0) kill (cpid
, SIGTERM
);
28 int main (int argc
, char** argv
) {
29 struct opt opts
= {0, 0, 0, NULL
, NULL
, STDOUT_FILENO
, "-q"};
30 /* `-q': don't print anything; exit with 0 on match; with 1 on error. used to interface with `grep' */
34 /* generate grep options string */
35 char* p
= opts
.grepopt
+2; /* move cursor behind `q' */
37 signal (SIGPIPE
, SIG_IGN
); /* ignore broken pipe between fag and grep */
38 struct sigaction sa
= {0}; /* terminate child if fag gets interrupted/terminated */
39 sa
.sa_handler
= &term_child
; /* NOTE: will also be inherited by keep-pipe-alive-child */
40 sigaction (SIGINT
, &sa
, NULL
);
41 sigaction (SIGTERM
, &sa
, NULL
);
44 /* the `+' forces getopt to stop at the first non-option */
45 while ((opt
= getopt (argc
, argv
, "+t:k::er:VhvEFGPiwxyU")) != -1) {
48 opts
.timeout
= atoi (optarg
);
51 opts
.kill_sig
= optarg
? atoi (optarg
) : SIGTERM
;
54 opts
.stream
= STDERR_FILENO
;
60 fprintf (stderr
, VERTEXT USAGE
62 "\t-t N\ttimeout after N seconds\n"
63 "\t-k[M]\tsend signal M to child after timeout (default: 15/SIGTERM)\n"
64 "\t-e\tgrep on stderr instead of stdout\n"
65 "\t-V\tbe verbose; print PROGRAM's stdout/stderr to stderr\n"
66 "\t-[EFP]\tgrep: matcher selection\n"
67 "\t-[iwxU]\tgrep: matching control\n", argv
[0]);
70 fprintf (stderr
, VERTEXT
);
72 /* `grep' options (Note: missing `-e:', `-f:') */
73 case 'E': case 'F': case 'G': case 'P':
74 case 'i': case 'y': case 'w': case 'x':
75 case 'U': *(p
++)=opt
; break;
78 fprintf (stderr
, "Unrecognized option: %c\n" USAGE
, optopt
, argv
[0]);
82 *p
= '\0'; /* terminate grep_options string */
84 /* the first non-option argument is the search string */
86 opts
.pattern
= argv
[optind
++];
88 fprintf (stderr
, USAGE
"(Missing PATTERN)\n", argv
[0]);
92 /* the remaining are the program to be run */
94 opts
.argv
= &(argv
[optind
]);
96 fprintf (stderr
, USAGE
"(Missing PROGRAM)\n", argv
[0]);
100 int retval
= fork_after_grep (opts
);
105 int fork_after_grep (struct opt opts
) {
113 struct timeval begin
, now
, diff
;
115 if (pipe(pipefd
) == -1) {
116 fprintf (stderr
, "pipe error (userprog)\n");
120 if ((cpid
= fork()) == -1) {
121 fprintf (stderr
, "fork error (userprog): %s", strerror (errno
));
129 dup2 (pipefd
[1], opts
.stream
);
131 //dup2 (open("/dev/null", O_WRONLY), opts.stream==STDOUT_FILENO?STDERR_FILENO:STDOUT_FILENO);
132 close (opts
.stream
==STDOUT_FILENO
?STDERR_FILENO
:STDOUT_FILENO
);
134 if (setsid () == -1) {
135 fprintf (stderr
, "setsid error (userprog): %s", strerror (errno
));
139 execvp (opts
.argv
[0], opts
.argv
);
140 fprintf (stderr
, "exec error (userprog): %s", strerror (errno
));
141 _exit (EX_UNAVAILABLE
);
148 fcntl (pipefd
[0], F_SETFL
, fcntl (pipefd
[0], F_GETFL
, 0) | O_NONBLOCK
);
150 gettimeofday (&begin
, NULL
); /* for timeout */
152 if (pipe(grep_pipefd
) == -1) {
153 fprintf (stderr
, "pipe error (grep)\n");
158 if ((grep_cpid
= fork()) == -1) {
159 fprintf (stderr
, "fork error (grep): %s", strerror (errno
));
161 close (grep_pipefd
[0]);
162 close (grep_pipefd
[1]);
166 if (grep_cpid
== 0) {
167 close (grep_pipefd
[1]);
168 dup2 (grep_pipefd
[0], STDIN_FILENO
);
169 close (grep_pipefd
[0]);
171 close (STDOUT_FILENO
);
173 execlp ("grep", "grep", opts
.grepopt
, "--", opts
.pattern
, NULL
);
174 fprintf (stderr
, "exec error (grep): %s", strerror (errno
));
177 close (grep_pipefd
[0]);
180 nbytes
= read (pipefd
[0], buf
, BUF_SIZE
);
186 fprintf (stderr
, "read error (userprog): %s", strerror (errno
));
188 close (grep_pipefd
[1]);
189 kill (grep_cpid
, SIGTERM
);
192 } else if (nbytes
== 0) {
193 fprintf (stderr
, "Child program exited prematurely (userprog).\n");
195 close (grep_pipefd
[1]);
196 kill (grep_cpid
, SIGTERM
);
197 if (waitpid (cpid
, &status
, WNOHANG
) > 0 && WIFEXITED (status
)) {
198 return WEXITSTATUS (status
);
200 return EX_UNAVAILABLE
;
202 /* have new userprog-data, send it to grep */
204 write(STDERR_FILENO
, buf
, nbytes
);
207 write(grep_pipefd
[1], buf
, nbytes
); /* can cause SIGPIPE if grep exited, therefore signal will be ignored */
210 if (waitpid (grep_cpid
, &grep_status
, WNOHANG
) > 0 && WIFEXITED (grep_status
)) {
211 close (grep_pipefd
[1]);
213 if (WEXITSTATUS(grep_status
) == 0) {
214 /* grep exited with match found */
215 printf ("%d\n", cpid
);
217 /* create a new child to keep pipe alive (will exit with exec'd program) */
219 int devnull
= open("/dev/null", O_WRONLY
);
220 dup2 (devnull
, pipefd
[0]);
222 while (kill(cpid
, 0) != -1 && errno
!= ESRCH
) sleep (1);
229 /* grep exited due to an error */
230 fprintf (stderr
, "grep exited due to an error.\n");
232 close (grep_pipefd
[1]);
237 if (opts
.timeout
> 0) {
238 gettimeofday (&now
, NULL
);
239 timersub (&now
, &begin
, &diff
);
240 if (diff
.tv_sec
>= opts
.timeout
) {
241 fprintf (stderr
, "Timeout reached. \n");
242 if (opts
.kill_sig
> 0) kill (cpid
, opts
.kill_sig
);
244 close (grep_pipefd
[1]);
245 kill (grep_cpid
, SIGTERM
);
246 return EX_UNAVAILABLE
;