]> git.gir.st - forkaftergrep.git/blob - fag.c
80119cff441bd654944c013f936f824b54698a6a
[forkaftergrep.git] / fag.c
1 /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */
2 //TODO: if grep exits with an error, fag thinks a match was found
3
4 #define _XOPEN_SOURCE 500
5 #define _DEFAULT_SOURCE
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sysexits.h>
12 #include <sys/time.h>
13 #include <sys/wait.h>
14 #include <unistd.h>
15 #include "fag.h"
16
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};
20 int opt;
21 opterr = 0;
22
23 /* the `+' forces getopt to stop at the first non-option */
24 while ((opt = getopt (argc, argv, "+t:k::eVhvEFGPiwxyU")) != -1) {
25 switch (opt) {
26 case 't':
27 opts.timeout = atoi (optarg);
28 break;
29 case 'k':
30 opts.kill_sig = optarg ? atoi (optarg) : SIGTERM;
31 break;
32 case 'e':
33 opts.stream = STDERR_FILENO;
34 break;
35 case 'V':
36 opts.verbose = 1;
37 break;
38 case 'h':
39 fprintf (stderr, VERTEXT USAGE
40 "Options:\n"
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]);
46 return EX_OK;
47 case 'v':
48 fprintf (stderr, VERTEXT);
49 return EX_OK;
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;
60
61 default:
62 fprintf (stderr, "Unrecognized option: %c\n" USAGE, optopt, argv[0]);
63 return EX_USAGE;
64 }
65 }
66
67 /* the first non-option argument is the search string */
68 if (optind < argc) {
69 opts.pattern = argv[optind++];
70 } else {
71 fprintf (stderr, USAGE "(Missing PATTERN)\n", argv[0]);
72 return EX_USAGE;
73 }
74
75 /* the remaining are the program to be run */
76 if (optind < argc) {
77 opts.argv = &(argv[optind]);
78 } else {
79 fprintf (stderr, USAGE "(Missing PROGRAM)\n", argv[0]);
80 return EX_USAGE;
81 }
82
83 int retval = fork_after_grep (opts, optg);
84
85 return retval;
86 }
87
88 int fork_after_grep (struct opt opts, struct grepopt optg) {
89 int pipefd[2];
90 pid_t cpid;
91 int status;
92
93 char buf[BUF_SIZE];
94 int nbytes;
95
96 struct timeval begin, now, diff;
97
98 if (pipe(pipefd) == -1) {
99 fprintf (stderr, "pipe error (userprog)\n");
100 return EX_OSERR;
101 }
102
103 if ((cpid = fork()) == -1) {
104 fprintf (stderr, "fork error (userprog): %s", strerror (errno));
105 close (pipefd[0]);
106 close (pipefd[1]);
107 return EX_OSERR;
108 }
109
110 if (cpid == 0) {
111 close (pipefd[0]);
112 dup2 (pipefd[1], opts.stream);
113 close (pipefd[1]);
114 close (opts.stream==STDOUT_FILENO?STDERR_FILENO:STDOUT_FILENO);
115
116 if (setsid () == -1) {
117 fprintf (stderr, "setsid error (userprog): %s", strerror (errno));
118 _exit (EX_OSERR);
119 }
120
121 execvp (opts.argv[0], opts.argv);
122 fprintf (stderr, "exec error (userprog): %s", strerror (errno));
123 _exit (EX_UNAVAILABLE);
124 } else {
125 pid_t grep_cpid;
126 int grep_pipefd[2];
127 int grep_status;
128
129 close (pipefd[1]);
130 fcntl (pipefd[0], F_SETFL, fcntl (pipefd[0], F_GETFL, 0) | O_NONBLOCK);
131
132 gettimeofday (&begin, NULL); /* for timeout */
133
134 if (pipe(grep_pipefd) == -1) {
135 fprintf (stderr, "pipe error (grep)\n");
136 close (pipefd[0]);
137 close (pipefd[1]);
138 return EX_OSERR;
139 }
140
141 if ((grep_cpid = fork()) == -1) {
142 fprintf (stderr, "fork error (grep): %s", strerror (errno));
143 close (pipefd[0]);
144 close (pipefd[1]);
145 close (grep_pipefd[0]);
146 close (grep_pipefd[1]);
147 return EX_OSERR;
148 }
149
150 if (grep_cpid == 0) {
151 close (grep_pipefd[1]);
152 dup2 (grep_pipefd[0], STDIN_FILENO);
153 close (grep_pipefd[0]);
154
155 close (STDERR_FILENO);
156 close (STDOUT_FILENO);
157
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';
170 *p = '\0';
171
172 execlp ("grep", "grep", grep_options, opts.pattern, NULL);
173 fprintf (stderr, "exec error (grep): %s", strerror (errno));
174 _exit (EX_SOFTWARE);
175 } else {
176 close (grep_pipefd[0]);
177 for (;;) {
178 usleep (20000);
179 nbytes = read (pipefd[0], buf, BUF_SIZE);
180 if (nbytes == -1) {
181 switch (errno) {
182 case EAGAIN:
183 break;
184 default:
185 fprintf (stderr, "read error (userprog): %s", strerror (errno));
186 close (pipefd[0]);
187 close (pipefd[1]);
188 close (grep_pipefd[1]);
189 //TODO: kill grep?
190 return EX_IOERR;
191 }
192 } else if (nbytes == 0) {
193 fprintf (stderr, "Child program exited prematurely (userprog).\n");
194 close (pipefd[0]);
195 close (pipefd[1]);
196 close (grep_pipefd[1]);
197 //TODO: kill grep?
198 if (waitpid (cpid, &status, WNOHANG) > 0 && WIFEXITED (status)) {
199 return WEXITSTATUS (status);
200 }
201 return EX_UNAVAILABLE;
202 } else {
203 /* have new userprog-data, send it to grep */
204 if (opts.verbose) {
205 write(STDERR_FILENO, buf, nbytes);
206 }
207
208 write(grep_pipefd[1], buf, nbytes);
209 }
210
211 // TODO: exits with `0' even if `grep' exits with code > 0 !
212 if (waitpid (grep_cpid, &grep_status, WNOHANG) > 0 && WIFEXITED (grep_status)) {
213 close (grep_pipefd[1]);
214
215 if (WEXITSTATUS(grep_status) == 0) {
216 /* grep exited with match found */
217 printf ("%d\n", cpid);
218
219 /* create a new child to keep pipe alive (will exit with exec'd program) */
220 if (!fork ()) {
221 while (kill(cpid, 0) != -1 && errno != ESRCH ) sleep (1);
222 close (pipefd[0]);
223 close (pipefd[1]);
224 _exit(0);
225 }
226 close (pipefd[0]);
227 close (pipefd[1]);
228 return EX_OK;
229 } else {
230 /* grep exited due to an error */
231 fprintf (stderr, "grep exited due to an error.");
232 close (pipefd[0]);
233 close (pipefd[1]);
234 close (grep_pipefd[1]);
235 return EX_IOERR;
236 }
237 }
238
239 if (opts.timeout > 0) {
240 gettimeofday (&now, NULL);
241 timersub (&now, &begin, &diff);
242 if (diff.tv_sec >= opts.timeout) {
243 fprintf (stderr, "Timeout reached. \n");
244 if (opts.kill_sig > 0) kill (cpid, opts.kill_sig);
245 close (pipefd[0]);
246 close (pipefd[1]);
247 close (grep_pipefd[1]);
248 //TODO: kill grep?
249 return EX_UNAVAILABLE;
250 }
251 }
252 }
253 }
254 }
255 }
Imprint / Impressum