]> git.gir.st - forkaftergrep.git/blob - fag.c
update readme from manpage
[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 (e.g. grep -BEP, killall grep)
3 //TODO: allow redirect of both streams to files
4 //TODO: grep is missing `-e' and `-f' options
5
6 #define _XOPEN_SOURCE 500
7 #define _DEFAULT_SOURCE
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sysexits.h>
15 #include <sys/time.h>
16 #include <sys/wait.h>
17 #include <unistd.h>
18 #include "fag.h"
19
20 pid_t cpid = 0;
21
22 void term_child(int s) {
23 (void)s; /* squash -Werror=unused-parameter */
24 if (cpid != 0) kill (cpid, SIGTERM);
25 exit(1);
26 }
27
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' */
31 int opt;
32 opterr = 0;
33
34 /* generate grep options string */
35 char* p = opts.grepopt+2; /* move cursor behind `q' */
36
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);
42
43
44 /* the `+' forces getopt to stop at the first non-option */
45 while ((opt = getopt (argc, argv, "+t:k::er:VhvEFGPiwxyU")) != -1) {
46 switch (opt) {
47 case 't':
48 opts.timeout = atoi (optarg);
49 break;
50 case 'k':
51 opts.kill_sig = optarg ? atoi (optarg) : SIGTERM;
52 break;
53 case 'e':
54 opts.stream = STDERR_FILENO;
55 break;
56 case 'V':
57 opts.verbose = 1;
58 break;
59 case 'h':
60 fprintf (stderr, VERTEXT USAGE
61 "Options:\n"
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]);
68 return EX_OK;
69 case 'v':
70 fprintf (stderr, VERTEXT);
71 return EX_OK;
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;
76
77 default:
78 fprintf (stderr, "Unrecognized option: %c\n" USAGE, optopt, argv[0]);
79 return EX_USAGE;
80 }
81 }
82 *p = '\0'; /* terminate grep_options string */
83
84 /* the first non-option argument is the search string */
85 if (optind < argc) {
86 opts.pattern = argv[optind++];
87 } else {
88 fprintf (stderr, USAGE "(Missing PATTERN)\n", argv[0]);
89 return EX_USAGE;
90 }
91
92 /* the remaining are the program to be run */
93 if (optind < argc) {
94 opts.argv = &(argv[optind]);
95 } else {
96 fprintf (stderr, USAGE "(Missing PROGRAM)\n", argv[0]);
97 return EX_USAGE;
98 }
99
100 int retval = fork_after_grep (opts);
101
102 return retval;
103 }
104
105 int fork_after_grep (struct opt opts) {
106 int pipefd[2];
107 //pid_t cpid;
108 int status;
109
110 char buf[BUF_SIZE];
111 int nbytes;
112
113 struct timeval begin, now, diff;
114
115 if (pipe(pipefd) == -1) {
116 fprintf (stderr, "pipe error (userprog)\n");
117 return EX_OSERR;
118 }
119
120 if ((cpid = fork()) == -1) {
121 fprintf (stderr, "fork error (userprog): %s", strerror (errno));
122 close (pipefd[0]);
123 close (pipefd[1]);
124 return EX_OSERR;
125 }
126
127 if (cpid == 0) {
128 close (pipefd[0]);
129 dup2 (pipefd[1], opts.stream);
130 close (pipefd[1]);
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);
133
134 if (setsid () == -1) {
135 fprintf (stderr, "setsid error (userprog): %s", strerror (errno));
136 _exit (EX_OSERR);
137 }
138
139 execvp (opts.argv[0], opts.argv);
140 fprintf (stderr, "exec error (userprog): %s", strerror (errno));
141 _exit (EX_UNAVAILABLE);
142 } else {
143 pid_t grep_cpid;
144 int grep_pipefd[2];
145 int grep_status;
146
147 close (pipefd[1]);
148 fcntl (pipefd[0], F_SETFL, fcntl (pipefd[0], F_GETFL, 0) | O_NONBLOCK);
149
150 gettimeofday (&begin, NULL); /* for timeout */
151
152 if (pipe(grep_pipefd) == -1) {
153 fprintf (stderr, "pipe error (grep)\n");
154 close (pipefd[0]);
155 return EX_OSERR;
156 }
157
158 if ((grep_cpid = fork()) == -1) {
159 fprintf (stderr, "fork error (grep): %s", strerror (errno));
160 close (pipefd[0]);
161 close (grep_pipefd[0]);
162 close (grep_pipefd[1]);
163 return EX_OSERR;
164 }
165
166 if (grep_cpid == 0) {
167 close (grep_pipefd[1]);
168 dup2 (grep_pipefd[0], STDIN_FILENO);
169 close (grep_pipefd[0]);
170
171 close (STDOUT_FILENO);
172
173 execlp ("grep", "grep", opts.grepopt, "--", opts.pattern, NULL);
174 fprintf (stderr, "exec error (grep): %s", strerror (errno));
175 _exit (EX_SOFTWARE);
176 } else {
177 close (grep_pipefd[0]);
178 for (;;) {
179 usleep (20000);
180 nbytes = read (pipefd[0], buf, BUF_SIZE);
181 if (nbytes == -1) {
182 switch (errno) {
183 case EAGAIN:
184 break;
185 default:
186 fprintf (stderr, "read error (userprog): %s", strerror (errno));
187 close (pipefd[0]);
188 close (grep_pipefd[1]);
189 kill (grep_cpid, SIGTERM);
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 (grep_pipefd[1]);
196 kill (grep_cpid, SIGTERM);
197 if (waitpid (cpid, &status, WNOHANG) > 0 && WIFEXITED (status)) {
198 return WEXITSTATUS (status);
199 }
200 return EX_UNAVAILABLE;
201 } else {
202 /* have new userprog-data, send it to grep */
203 if (opts.verbose) {
204 write(STDERR_FILENO, buf, nbytes);
205 }
206
207 write(grep_pipefd[1], buf, nbytes); /* can cause SIGPIPE if grep exited, therefore signal will be ignored */
208 }
209
210 if (waitpid (grep_cpid, &grep_status, WNOHANG) > 0 && WIFEXITED (grep_status)) {
211 close (grep_pipefd[1]);
212
213 if (WEXITSTATUS(grep_status) == 0) {
214 /* grep exited with match found */
215 printf ("%d\n", cpid);
216
217 /* create a new child to keep pipe alive (will exit with exec'd program) */
218 if (!fork ()) {
219 int devnull = open("/dev/null", O_WRONLY);
220 dup2 (devnull, pipefd[0]);
221 close (devnull);
222 while (kill(cpid, 0) != -1 && errno != ESRCH ) sleep (1);
223 close (pipefd[0]);
224 _exit(0);
225 }
226 close (pipefd[0]);
227 return EX_OK;
228 } else {
229 /* grep exited due to an error */
230 fprintf (stderr, "grep exited due to an error.\n");
231 close (pipefd[0]);
232 close (grep_pipefd[1]);
233 return EX_IOERR;
234 }
235 }
236
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);
243 close (pipefd[0]);
244 close (grep_pipefd[1]);
245 kill (grep_cpid, SIGTERM);
246 return EX_UNAVAILABLE;
247 }
248 }
249 }
250 }
251 }
252 }
Imprint / Impressum