]>
git.gir.st - forkaftergrep.git/blob - fag.c
1 /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */
2 //TODO: `tee': bad file descriptor
4 #define _XOPEN_SOURCE 500
5 #define _DEFAULT_SOURCE
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' */
24 /* generate grep options string */
25 char* p
= opts
.grepopt
+2; /* move cursor behind `q' */
27 signal (SIGPIPE
, SIG_IGN
); /* ignore broken pipe between fag and grep */
30 /* the `+' forces getopt to stop at the first non-option */
31 while ((opt
= getopt (argc
, argv
, "+t:k::eVhvEFGPiwxyU")) != -1) {
34 opts
.timeout
= atoi (optarg
);
37 opts
.kill_sig
= optarg
? atoi (optarg
) : SIGTERM
;
40 opts
.stream
= STDERR_FILENO
;
46 fprintf (stderr
, VERTEXT USAGE
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]);
56 fprintf (stderr
, VERTEXT
);
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;
64 fprintf (stderr
, "Unrecognized option: %c\n" USAGE
, optopt
, argv
[0]);
68 *p
= '\0'; /* terminate grep_options string */
70 /* the first non-option argument is the search string */
72 opts
.pattern
= argv
[optind
++];
74 fprintf (stderr
, USAGE
"(Missing PATTERN)\n", argv
[0]);
78 /* the remaining are the program to be run */
80 opts
.argv
= &(argv
[optind
]);
82 fprintf (stderr
, USAGE
"(Missing PROGRAM)\n", argv
[0]);
86 int retval
= fork_after_grep (opts
);
91 int fork_after_grep (struct opt opts
) {
99 struct timeval begin
, now
, diff
;
101 if (pipe(pipefd
) == -1) {
102 fprintf (stderr
, "pipe error (userprog)\n");
106 if ((cpid
= fork()) == -1) {
107 fprintf (stderr
, "fork error (userprog): %s", strerror (errno
));
115 dup2 (pipefd
[1], opts
.stream
);
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
);
120 if (setsid () == -1) {
121 fprintf (stderr
, "setsid error (userprog): %s", strerror (errno
));
125 execvp (opts
.argv
[0], opts
.argv
);
126 fprintf (stderr
, "exec error (userprog): %s", strerror (errno
));
127 _exit (EX_UNAVAILABLE
);
134 fcntl (pipefd
[0], F_SETFL
, fcntl (pipefd
[0], F_GETFL
, 0) | O_NONBLOCK
);
136 gettimeofday (&begin
, NULL
); /* for timeout */
138 if (pipe(grep_pipefd
) == -1) {
139 fprintf (stderr
, "pipe error (grep)\n");
144 if ((grep_cpid
= fork()) == -1) {
145 fprintf (stderr
, "fork error (grep): %s", strerror (errno
));
147 close (grep_pipefd
[0]);
148 close (grep_pipefd
[1]);
152 if (grep_cpid
== 0) {
153 close (grep_pipefd
[1]);
154 dup2 (grep_pipefd
[0], STDIN_FILENO
);
155 close (grep_pipefd
[0]);
157 close (STDERR_FILENO
);
158 close (STDOUT_FILENO
);
160 execlp ("grep", "grep", opts
.grepopt
, opts
.pattern
, NULL
);
161 fprintf (stderr
, "exec error (grep): %s", strerror (errno
));
164 close (grep_pipefd
[0]);
167 nbytes
= read (pipefd
[0], buf
, BUF_SIZE
);
173 fprintf (stderr
, "read error (userprog): %s", strerror (errno
));
175 close (grep_pipefd
[1]);
176 kill (grep_cpid
, SIGTERM
);
179 } else if (nbytes
== 0) {
180 fprintf (stderr
, "Child program exited prematurely (userprog).\n");
182 close (grep_pipefd
[1]);
183 kill (grep_cpid
, SIGTERM
);
184 if (waitpid (cpid
, &status
, WNOHANG
) > 0 && WIFEXITED (status
)) {
185 return WEXITSTATUS (status
);
187 return EX_UNAVAILABLE
;
189 /* have new userprog-data, send it to grep */
191 write(STDERR_FILENO
, buf
, nbytes
);
194 write(grep_pipefd
[1], buf
, nbytes
); /* can cause SIGPIPE if grep exited, therefore signal will be ignored */
197 if (waitpid (grep_cpid
, &grep_status
, WNOHANG
) > 0 && WIFEXITED (grep_status
)) {
198 close (grep_pipefd
[1]);
200 if (WEXITSTATUS(grep_status
) == 0) {
201 /* grep exited with match found */
202 printf ("%d\n", cpid
);
204 /* create a new child to keep pipe alive (will exit with exec'd program) */
206 int devnull
= open("/dev/null", O_WRONLY
);
207 dup2 (devnull
, pipefd
[0]);
209 while (kill(cpid
, 0) != -1 && errno
!= ESRCH
) sleep (1);
216 /* grep exited due to an error */
217 fprintf (stderr
, "grep exited due to an error.\n");
219 close (grep_pipefd
[1]);
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
);
231 close (grep_pipefd
[1]);
232 kill (grep_cpid
, SIGTERM
);
233 return EX_UNAVAILABLE
;