]>
git.gir.st - forkaftergrep.git/blob - fag.c
1 /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */
2 //TODO: supply user arguments to grep
3 //demo: ./fag -e -V "MPD server running at" mopidy
5 #define _XOPEN_SOURCE 500
6 #define _DEFAULT_SOURCE
18 struct opt opts
= {0, 0, 0, NULL
, NULL
, STDOUT_FILENO
};
20 int main (int argc
, char** argv
) {
24 /* the `+' forces getopt to stop at the first non-option */
25 while ((opt
= getopt (argc
, argv
, "+t:k::eVhv")) != -1) {
28 opts
.timeout
= atoi (optarg
);
31 opts
.kill_sig
= optarg
? atoi (optarg
) : SIGTERM
;
34 opts
.stream
= STDERR_FILENO
;
40 fprintf (stderr
, VERTEXT USAGE
42 "\t-t N\ttimeout after N seconds\n"
43 "\t-k [M]\tsend signal M to child after timeout (default: 15/SIGTERM)\n"
44 "\t-e\tgrep on stderr instead of stdout\n", argv
[0]);
47 fprintf (stderr
, VERTEXT
);
50 fprintf (stderr
, "Unrecognized option: %c\n" USAGE
, optopt
, argv
[0]);
55 /* the first non-option argument is the search string */
57 opts
.pattern
= argv
[optind
++];
59 fprintf (stderr
, USAGE
"(Missing PATTERN)\n", argv
[0]);
63 /* the remaining are the program to be run */
65 opts
.argv
= &(argv
[optind
]);
67 fprintf (stderr
, USAGE
"(Missing PROGRAM)\n", argv
[0]);
71 int retval
= fork_after_grep (opts
);
76 int fork_after_grep (struct opt opts
) {
84 struct timeval begin
, now
, diff
;
86 if (pipe(pipefd
) == -1) {
87 fprintf (stderr
, "pipe error (userprog)\n");
91 if ((cpid
= fork()) == -1) {
92 fprintf (stderr
, "fork error (userprog): %s", strerror (errno
));
100 dup2 (pipefd
[1], opts
.stream
);
102 close (opts
.stream
==STDOUT_FILENO
?STDERR_FILENO
:STDOUT_FILENO
);
104 if (setsid () == -1) {
105 fprintf (stderr
, "setsid error (userprog): %s", strerror (errno
));
109 execvp (opts
.argv
[0], opts
.argv
);
110 fprintf (stderr
, "exec error (userprog): %s", strerror (errno
));
111 _exit (EX_UNAVAILABLE
);
114 int pipefd_togrep
[2];
116 /* `-q': don't print anything; exit with 0 on match; with 1 on error */
117 char* grepargv
[] = {"grep", "-q", opts
.pattern
, NULL
};
120 fcntl (pipefd
[0], F_SETFL
, fcntl (pipefd
[0], F_GETFL
, 0) | O_NONBLOCK
);
122 gettimeofday (&begin
, NULL
); /* for timeout */
124 if (pipe(pipefd_togrep
) == -1) {
125 fprintf (stderr
, "pipe error (grep)\n");
131 if ((grep_cpid
= fork()) == -1) {
132 fprintf (stderr
, "fork error (grep): %s", strerror (errno
));
135 close (pipefd_togrep
[0]);
136 close (pipefd_togrep
[1]);
140 if (grep_cpid
== 0) {
141 close (pipefd_togrep
[1]);
142 dup2 (pipefd_togrep
[0], STDIN_FILENO
);
143 close (pipefd_togrep
[0]);
145 close (STDERR_FILENO
);
146 close (STDOUT_FILENO
);
148 execvp (grepargv
[0], grepargv
);
149 fprintf (stderr
, "exec error (grep): %s", strerror (errno
));
150 _exit (EX_UNAVAILABLE
);
152 close (pipefd_togrep
[0]);
155 memset (buf
, 0, BUF_SIZE
); /* necessary for opts.verbose */
156 nbytes
= read (pipefd
[0], buf
, BUF_SIZE
);
162 fprintf (stderr
, "read error (userprog): %s", strerror (errno
));
165 close (pipefd_togrep
[1]);
169 } else if (nbytes
== 0) {
170 fprintf (stderr
, "Child program exited prematurely (userprog).\n");
173 close (pipefd_togrep
[1]);
175 if (waitpid (cpid
, &status
, WNOHANG
) > 0 && WIFEXITED (status
)) {
176 return WEXITSTATUS (status
);
178 return EX_UNAVAILABLE
;
180 /* have new userprog-data, send it to grep */
185 write(pipefd_togrep
[1], buf
, strlen(buf
));
188 if (waitpid (grep_cpid
, &grep_status
, WNOHANG
) > 0 && WIFEXITED (grep_status
)) {
189 close (pipefd_togrep
[1]);
191 if (WEXITSTATUS(grep_status
) == 0) {
192 /* grep exited with match found */
193 printf ("%d\n", cpid
);
195 /* create a new child to keep pipe alive (will exit with exec'd program) */
197 while (kill(cpid
, 0) != -1 && errno
!= ESRCH
) sleep (1);
206 /* grep exited due to an error */
207 fprintf (stderr
, "grep exited due to an error.");
210 close (pipefd_togrep
[1]);
215 if (opts
.timeout
> 0) {
216 gettimeofday (&now
, NULL
);
217 timersub (&now
, &begin
, &diff
);
218 if (diff
.tv_sec
>= opts
.timeout
) {
219 fprintf (stderr
, "Timeout reached. \n");
220 if (opts
.kill_sig
> 0) kill (cpid
, opts
.kill_sig
);
223 close (pipefd_togrep
[1]);
225 return EX_UNAVAILABLE
;