]>
Commit | Line | Data |
---|---|---|
1 | /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */ | |
2 | //TODO: if grep exits with an error, fag thinks a match was found | |
3 | //TODO: sometimes fag exits with code 141 (128+SIGPIPE) | |
4 | ||
5 | #define _XOPEN_SOURCE 500 | |
6 | #define _DEFAULT_SOURCE | |
7 | #include <errno.h> | |
8 | #include <fcntl.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 | int opt; | |
21 | opterr = 0; | |
22 | ||
23 | /* generate grep options string */ | |
24 | /* `-q': don't print anything; exit with 0 on match; with 1 on error */ | |
25 | char* p = opts.grepopt+2; /* move cursor behind `q' */ | |
26 | ||
27 | ||
28 | /* the `+' forces getopt to stop at the first non-option */ | |
29 | while ((opt = getopt (argc, argv, "+t:k::eVhvEFGPiwxyU")) != -1) { | |
30 | switch (opt) { | |
31 | case 't': | |
32 | opts.timeout = atoi (optarg); | |
33 | break; | |
34 | case 'k': | |
35 | opts.kill_sig = optarg ? atoi (optarg) : SIGTERM; | |
36 | break; | |
37 | case 'e': | |
38 | opts.stream = STDERR_FILENO; | |
39 | break; | |
40 | case 'V': | |
41 | opts.verbose = 1; | |
42 | break; | |
43 | case 'h': | |
44 | fprintf (stderr, VERTEXT USAGE | |
45 | "Options:\n" | |
46 | "\t-t N\ttimeout after N seconds\n" | |
47 | "\t-k [M]\tsend signal M to child after timeout (default: 15/SIGTERM)\n" | |
48 | "\t-e\tgrep on stderr instead of stdout\n" | |
49 | "\t-V\tbe verbose; print PROGRAM's stdout/stderr to stderr\n" | |
50 | "\t-[EFGPiwxyU]\t grep options\n", argv[0]); | |
51 | return EX_OK; | |
52 | case 'v': | |
53 | fprintf (stderr, VERTEXT); | |
54 | return EX_OK; | |
55 | /* `grep' options (Note: missing `-e:', `-f:') */ | |
56 | case 'E': case 'F': case 'G': case 'P': | |
57 | case 'i': case 'y': case 'w': case 'x': | |
58 | case 'U': *(p++)=opt; break; | |
59 | ||
60 | default: | |
61 | fprintf (stderr, "Unrecognized option: %c\n" USAGE, optopt, argv[0]); | |
62 | return EX_USAGE; | |
63 | } | |
64 | } | |
65 | *p = '\0'; /* finish grep_options string */ | |
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); | |
84 | ||
85 | return retval; | |
86 | } | |
87 | ||
88 | int fork_after_grep (struct opt opts) { | |
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 | return EX_OSERR; | |
138 | } | |
139 | ||
140 | if ((grep_cpid = fork()) == -1) { | |
141 | fprintf (stderr, "fork error (grep): %s", strerror (errno)); | |
142 | close (pipefd[0]); | |
143 | close (grep_pipefd[0]); | |
144 | close (grep_pipefd[1]); | |
145 | return EX_OSERR; | |
146 | } | |
147 | ||
148 | if (grep_cpid == 0) { | |
149 | close (grep_pipefd[1]); | |
150 | dup2 (grep_pipefd[0], STDIN_FILENO); | |
151 | close (grep_pipefd[0]); | |
152 | ||
153 | close (STDERR_FILENO); | |
154 | close (STDOUT_FILENO); | |
155 | ||
156 | execlp ("grep", "grep", opts.grepopt, opts.pattern, NULL); | |
157 | fprintf (stderr, "exec error (grep): %s", strerror (errno)); | |
158 | _exit (EX_SOFTWARE); | |
159 | } else { | |
160 | close (grep_pipefd[0]); | |
161 | for (;;) { | |
162 | usleep (20000); | |
163 | nbytes = read (pipefd[0], buf, BUF_SIZE); | |
164 | if (nbytes == -1) { | |
165 | switch (errno) { | |
166 | case EAGAIN: | |
167 | break; | |
168 | default: | |
169 | fprintf (stderr, "read error (userprog): %s", strerror (errno)); | |
170 | close (pipefd[0]); | |
171 | close (grep_pipefd[1]); | |
172 | //TODO: kill grep? | |
173 | return EX_IOERR; | |
174 | } | |
175 | } else if (nbytes == 0) { | |
176 | fprintf (stderr, "Child program exited prematurely (userprog).\n"); | |
177 | close (pipefd[0]); | |
178 | close (grep_pipefd[1]); | |
179 | //TODO: kill grep? | |
180 | if (waitpid (cpid, &status, WNOHANG) > 0 && WIFEXITED (status)) { | |
181 | return WEXITSTATUS (status); | |
182 | } | |
183 | return EX_UNAVAILABLE; | |
184 | } else { | |
185 | /* have new userprog-data, send it to grep */ | |
186 | if (opts.verbose) { | |
187 | write(STDERR_FILENO, buf, nbytes); | |
188 | } | |
189 | ||
190 | write(grep_pipefd[1], buf, nbytes); | |
191 | } | |
192 | ||
193 | // TODO: exits with `0' even if `grep' exits with code > 0 ! | |
194 | if (waitpid (grep_cpid, &grep_status, WNOHANG) > 0 && WIFEXITED (grep_status)) { | |
195 | close (grep_pipefd[1]); | |
196 | ||
197 | if (WEXITSTATUS(grep_status) == 0) { | |
198 | /* grep exited with match found */ | |
199 | printf ("%d\n", cpid); | |
200 | ||
201 | /* create a new child to keep pipe alive (will exit with exec'd program) */ | |
202 | if (!fork ()) { | |
203 | while (kill(cpid, 0) != -1 && errno != ESRCH ) sleep (1); | |
204 | close (pipefd[0]); | |
205 | _exit(0); | |
206 | } | |
207 | close (pipefd[0]); | |
208 | return EX_OK; | |
209 | } else { | |
210 | /* grep exited due to an error */ | |
211 | fprintf (stderr, "grep exited due to an error.\n"); | |
212 | close (pipefd[0]); | |
213 | close (grep_pipefd[1]); | |
214 | return EX_IOERR; | |
215 | } | |
216 | } | |
217 | ||
218 | if (opts.timeout > 0) { | |
219 | gettimeofday (&now, NULL); | |
220 | timersub (&now, &begin, &diff); | |
221 | if (diff.tv_sec >= opts.timeout) { | |
222 | fprintf (stderr, "Timeout reached. \n"); | |
223 | if (opts.kill_sig > 0) kill (cpid, opts.kill_sig); | |
224 | close (pipefd[0]); | |
225 | close (grep_pipefd[1]); | |
226 | //TODO: kill grep? | |
227 | return EX_UNAVAILABLE; | |
228 | } | |
229 | } | |
230 | } | |
231 | } | |
232 | } | |
233 | } |