]>
git.gir.st - forkaftergrep.git/blob - fag.c
bf497b3de83b60313126b60ef60a279209dbe0ac
1 /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */
2 //TODO: if grep exits with an error, fag thinks a match was found
4 #define _XOPEN_SOURCE 500
5 #define _DEFAULT_SOURCE
17 int main (int argc
, char** argv
) {
18 struct opt opts
= {0, 0, 0, NULL
, NULL
, STDOUT_FILENO
};
19 struct grepopt optg
= {basic_regexp
, 0, 0, 0, 0};
23 /* the `+' forces getopt to stop at the first non-option */
24 while ((opt
= getopt (argc
, argv
, "+t:k::eVhvEFGPiwxyU")) != -1) {
27 opts
.timeout
= atoi (optarg
);
30 opts
.kill_sig
= optarg
? atoi (optarg
) : SIGTERM
;
33 opts
.stream
= STDERR_FILENO
;
39 fprintf (stderr
, VERTEXT USAGE
41 "\t-t N\ttimeout after N seconds\n"
42 "\t-k [M]\tsend signal M to child after timeout (default: 15/SIGTERM)\n"
43 "\t-e\tgrep on stderr instead of stdout\n"
44 "\t-V\tbe verbose; print PROGRAM's stdout/stderr to stderr\n"
45 "\t-[EFGPiwxyU]\t grep options\n", argv
[0]);
48 fprintf (stderr
, VERTEXT
);
50 /* `grep' options (Note: missing `-e:', `-f:') */
51 case 'E': optg
.regex
= extended_regexp
; break;
52 case 'F': optg
.regex
= fixed_strings
; break;
53 case 'G': optg
.regex
= basic_regexp
; break; /* grep default */
54 case 'P': optg
.regex
= perl_regexp
; break;
55 case 'i': /* fall thru */
56 case 'y': optg
.ignore_case
= 1; break;
57 case 'w': optg
.word_regexp
= 1; break;
58 case 'x': optg
.line_regexp
= 1; break;
59 case 'U': optg
.binary
= 1; break;
62 fprintf (stderr
, "Unrecognized option: %c\n" USAGE
, optopt
, argv
[0]);
67 /* the first non-option argument is the search string */
69 opts
.pattern
= argv
[optind
++];
71 fprintf (stderr
, USAGE
"(Missing PATTERN)\n", argv
[0]);
75 /* the remaining are the program to be run */
77 opts
.argv
= &(argv
[optind
]);
79 fprintf (stderr
, USAGE
"(Missing PROGRAM)\n", argv
[0]);
83 int retval
= fork_after_grep (opts
, optg
);
88 int fork_after_grep (struct opt opts
, struct grepopt optg
) {
96 struct timeval begin
, now
, diff
;
98 if (pipe(pipefd
) == -1) {
99 fprintf (stderr
, "pipe error (userprog)\n");
103 if ((cpid
= fork()) == -1) {
104 fprintf (stderr
, "fork error (userprog): %s", strerror (errno
));
112 dup2 (pipefd
[1], opts
.stream
);
114 close (opts
.stream
==STDOUT_FILENO
?STDERR_FILENO
:STDOUT_FILENO
);
116 if (setsid () == -1) {
117 fprintf (stderr
, "setsid error (userprog): %s", strerror (errno
));
121 execvp (opts
.argv
[0], opts
.argv
);
122 fprintf (stderr
, "exec error (userprog): %s", strerror (errno
));
123 _exit (EX_UNAVAILABLE
);
130 fcntl (pipefd
[0], F_SETFL
, fcntl (pipefd
[0], F_GETFL
, 0) | O_NONBLOCK
);
132 gettimeofday (&begin
, NULL
); /* for timeout */
134 if (pipe(grep_pipefd
) == -1) {
135 fprintf (stderr
, "pipe error (grep)\n");
141 if ((grep_cpid
= fork()) == -1) {
142 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 /* generate argument list on the fly (TODO: ugly) */
159 /* `-q': don't print anything; exit with 0 on match; with 1 on error */
160 char grep_options
[16] = "-q";
161 char* p
= grep_options
+2;
162 if (optg
.regex
== extended_regexp
) *(p
++)='E';
163 if (optg
.regex
== fixed_strings
) *(p
++)='F';
164 if (optg
.regex
== basic_regexp
) *(p
++)='G';
165 if (optg
.regex
== perl_regexp
) *(p
++)='P';
166 if (optg
.ignore_case
) *(p
++)='i';
167 if (optg
.word_regexp
) *(p
++)='w';
168 if (optg
.line_regexp
) *(p
++)='x';
169 if (optg
.binary
) *(p
++)='U';
172 execlp ("grep", "grep", grep_options
, opts
.pattern
, NULL
);
173 fprintf (stderr
, "exec error (grep): %s", strerror (errno
));
176 close (grep_pipefd
[0]);
179 nbytes
= read (pipefd
[0], buf
, BUF_SIZE
);
185 fprintf (stderr
, "read error (userprog): %s", strerror (errno
));
188 close (grep_pipefd
[1]);
192 } else if (nbytes
== 0) {
193 fprintf (stderr
, "Child program exited prematurely (userprog).\n");
196 close (grep_pipefd
[1]);
198 if (waitpid (cpid
, &status
, WNOHANG
) > 0 && WIFEXITED (status
)) {
199 return WEXITSTATUS (status
);
201 return EX_UNAVAILABLE
;
203 /* have new userprog-data, send it to grep */
206 write(STDERR_FILENO
, buf
, nbytes
);
209 write(grep_pipefd
[1], buf
, nbytes
);
212 // TODO: exits with `0' even if `grep' exits with code > 0 !
213 if (waitpid (grep_cpid
, &grep_status
, WNOHANG
) > 0 && WIFEXITED (grep_status
)) {
214 close (grep_pipefd
[1]);
216 if (WEXITSTATUS(grep_status
) == 0) {
217 /* grep exited with match found */
218 printf ("%d\n", cpid
);
220 /* create a new child to keep pipe alive (will exit with exec'd program) */
222 while (kill(cpid
, 0) != -1 && errno
!= ESRCH
) sleep (1);
231 /* grep exited due to an error */
232 fprintf (stderr
, "grep exited due to an error.");
235 close (grep_pipefd
[1]);
240 if (opts
.timeout
> 0) {
241 gettimeofday (&now
, NULL
);
242 timersub (&now
, &begin
, &diff
);
243 if (diff
.tv_sec
>= opts
.timeout
) {
244 fprintf (stderr
, "Timeout reached. \n");
245 if (opts
.kill_sig
> 0) kill (cpid
, opts
.kill_sig
);
248 close (grep_pipefd
[1]);
250 return EX_UNAVAILABLE
;