]> git.gir.st - forkaftergrep.git/blob - fag.c
39b8a963aced0a839167bde5e55f1435457b3c40
[forkaftergrep.git] / fag.c
1 /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */
2 //TODO: `tee': bad file descriptor
3
4 #define _XOPEN_SOURCE 500
5 #define _DEFAULT_SOURCE
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sysexits.h>
13 #include <sys/time.h>
14 #include <sys/wait.h>
15 #include <unistd.h>
16 #include "fag.h"
17
18 int main (int argc, char** argv) {
19 struct opt opts = {0, 0, 0, NULL, NULL, STDOUT_FILENO, "-q"};
20 /* `-q': don't print anything; exit with 0 on match; with 1 on error. used to interface with `grep' */
21 int opt;
22 opterr = 0;
23
24 /* generate grep options string */
25 char* p = opts.grepopt+2; /* move cursor behind `q' */
26
27 signal (SIGPIPE, SIG_IGN); /* ignore broken pipe between fag and grep */
28
29
30 /* the `+' forces getopt to stop at the first non-option */
31 while ((opt = getopt (argc, argv, "+t:k::eVhvEFGPiwxyU")) != -1) {
32 switch (opt) {
33 case 't':
34 opts.timeout = atoi (optarg);
35 break;
36 case 'k':
37 opts.kill_sig = optarg ? atoi (optarg) : SIGTERM;
38 break;
39 case 'e':
40 opts.stream = STDERR_FILENO;
41 break;
42 case 'V':
43 opts.verbose = 1;
44 break;
45 case 'h':
46 fprintf (stderr, VERTEXT USAGE
47 "Options:\n"
48 "\t-t N\ttimeout after N seconds\n"
49 "\t-k[M]\tsend signal M to child after timeout (default: 15/SIGTERM)\n"
50 "\t-e\tgrep on stderr instead of stdout\n"
51 "\t-V\tbe verbose; print PROGRAM's stdout/stderr to stderr\n"
52 "\t-[EFP]\tgrep: matcher selection\n"
53 "\t-[iwxU]\tgrep: matching control\n", argv[0]);
54 return EX_OK;
55 case 'v':
56 fprintf (stderr, VERTEXT);
57 return EX_OK;
58 /* `grep' options (Note: missing `-e:', `-f:') */
59 case 'E': case 'F': case 'G': case 'P':
60 case 'i': case 'y': case 'w': case 'x':
61 case 'U': *(p++)=opt; break;
62
63 default:
64 fprintf (stderr, "Unrecognized option: %c\n" USAGE, optopt, argv[0]);
65 return EX_USAGE;
66 }
67 }
68 *p = '\0'; /* terminate grep_options string */
69
70 /* the first non-option argument is the search string */
71 if (optind < argc) {
72 opts.pattern = argv[optind++];
73 } else {
74 fprintf (stderr, USAGE "(Missing PATTERN)\n", argv[0]);
75 return EX_USAGE;
76 }
77
78 /* the remaining are the program to be run */
79 if (optind < argc) {
80 opts.argv = &(argv[optind]);
81 } else {
82 fprintf (stderr, USAGE "(Missing PROGRAM)\n", argv[0]);
83 return EX_USAGE;
84 }
85
86 int retval = fork_after_grep (opts);
87
88 return retval;
89 }
90
91 int fork_after_grep (struct opt opts) {
92 int pipefd[2];
93 pid_t cpid;
94 int status;
95
96 char buf[BUF_SIZE];
97 int nbytes;
98
99 struct timeval begin, now, diff;
100
101 if (pipe(pipefd) == -1) {
102 fprintf (stderr, "pipe error (userprog)\n");
103 return EX_OSERR;
104 }
105
106 if ((cpid = fork()) == -1) {
107 fprintf (stderr, "fork error (userprog): %s", strerror (errno));
108 close (pipefd[0]);
109 close (pipefd[1]);
110 return EX_OSERR;
111 }
112
113 if (cpid == 0) {
114 close (pipefd[0]);
115 dup2 (pipefd[1], opts.stream);
116 close (pipefd[1]);
117 //dup2 (open("/dev/null", O_WRONLY), opts.stream==STDOUT_FILENO?STDERR_FILENO:STDOUT_FILENO);
118 close (opts.stream==STDOUT_FILENO?STDERR_FILENO:STDOUT_FILENO);
119
120 if (setsid () == -1) {
121 fprintf (stderr, "setsid error (userprog): %s", strerror (errno));
122 _exit (EX_OSERR);
123 }
124
125 execvp (opts.argv[0], opts.argv);
126 fprintf (stderr, "exec error (userprog): %s", strerror (errno));
127 _exit (EX_UNAVAILABLE);
128 } else {
129 pid_t grep_cpid;
130 int grep_pipefd[2];
131 int grep_status;
132
133 close (pipefd[1]);
134 fcntl (pipefd[0], F_SETFL, fcntl (pipefd[0], F_GETFL, 0) | O_NONBLOCK);
135
136 gettimeofday (&begin, NULL); /* for timeout */
137
138 if (pipe(grep_pipefd) == -1) {
139 fprintf (stderr, "pipe error (grep)\n");
140 close (pipefd[0]);
141 return EX_OSERR;
142 }
143
144 if ((grep_cpid = fork()) == -1) {
145 fprintf (stderr, "fork error (grep): %s", strerror (errno));
146 close (pipefd[0]);
147 close (grep_pipefd[0]);
148 close (grep_pipefd[1]);
149 return EX_OSERR;
150 }
151
152 if (grep_cpid == 0) {
153 close (grep_pipefd[1]);
154 dup2 (grep_pipefd[0], STDIN_FILENO);
155 close (grep_pipefd[0]);
156
157 close (STDERR_FILENO);
158 close (STDOUT_FILENO);
159
160 execlp ("grep", "grep", opts.grepopt, opts.pattern, NULL);
161 fprintf (stderr, "exec error (grep): %s", strerror (errno));
162 _exit (EX_SOFTWARE);
163 } else {
164 close (grep_pipefd[0]);
165 for (;;) {
166 usleep (20000);
167 nbytes = read (pipefd[0], buf, BUF_SIZE);
168 if (nbytes == -1) {
169 switch (errno) {
170 case EAGAIN:
171 break;
172 default:
173 fprintf (stderr, "read error (userprog): %s", strerror (errno));
174 close (pipefd[0]);
175 close (grep_pipefd[1]);
176 kill (grep_cpid, SIGTERM);
177 return EX_IOERR;
178 }
179 } else if (nbytes == 0) {
180 fprintf (stderr, "Child program exited prematurely (userprog).\n");
181 close (pipefd[0]);
182 close (grep_pipefd[1]);
183 kill (grep_cpid, SIGTERM);
184 if (waitpid (cpid, &status, WNOHANG) > 0 && WIFEXITED (status)) {
185 return WEXITSTATUS (status);
186 }
187 return EX_UNAVAILABLE;
188 } else {
189 /* have new userprog-data, send it to grep */
190 if (opts.verbose) {
191 write(STDERR_FILENO, buf, nbytes);
192 }
193
194 write(grep_pipefd[1], buf, nbytes); /* can cause SIGPIPE if grep exited, therefore signal will be ignored */
195 }
196
197 if (waitpid (grep_cpid, &grep_status, WNOHANG) > 0 && WIFEXITED (grep_status)) {
198 close (grep_pipefd[1]);
199
200 if (WEXITSTATUS(grep_status) == 0) {
201 /* grep exited with match found */
202 printf ("%d\n", cpid);
203
204 /* create a new child to keep pipe alive (will exit with exec'd program) */
205 if (!fork ()) {
206 int devnull = open("/dev/null", O_WRONLY);
207 dup2 (devnull, pipefd[0]);
208 close (devnull);
209 while (kill(cpid, 0) != -1 && errno != ESRCH ) sleep (1);
210 close (pipefd[0]);
211 _exit(0);
212 }
213 close (pipefd[0]);
214 return EX_OK;
215 } else {
216 /* grep exited due to an error */
217 fprintf (stderr, "grep exited due to an error.\n");
218 close (pipefd[0]);
219 close (grep_pipefd[1]);
220 return EX_IOERR;
221 }
222 }
223
224 if (opts.timeout > 0) {
225 gettimeofday (&now, NULL);
226 timersub (&now, &begin, &diff);
227 if (diff.tv_sec >= opts.timeout) {
228 fprintf (stderr, "Timeout reached. \n");
229 if (opts.kill_sig > 0) kill (cpid, opts.kill_sig);
230 close (pipefd[0]);
231 close (grep_pipefd[1]);
232 kill (grep_cpid, SIGTERM);
233 return EX_UNAVAILABLE;
234 }
235 }
236 }
237 }
238 }
239 }
Imprint / Impressum