]>
git.gir.st - forkaftergrep.git/blob - fag.c
1 /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */
2 //TODO: close all descriptors (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
};//"-E", //TODO: supply params (-E, -P, etc.) to grep from command line
120 fcntl (pipefd
[0], F_SETFL
, fcntl (pipefd
[0], F_GETFL
, 0) | O_NONBLOCK
);
122 gettimeofday (&begin
, NULL
);
124 if (pipe(pipefd_togrep
) == -1) {
125 fprintf (stderr
, "pipe error (grep)\n");
129 if ((grep_cpid
= fork()) == -1) {
130 fprintf (stderr
, "fork error (grep): %s", strerror (errno
));
136 if (grep_cpid
== 0) {
137 close (pipefd_togrep
[1]);
138 dup2 (pipefd_togrep
[0], STDIN_FILENO
);
139 close (pipefd_togrep
[0]);
141 close (STDERR_FILENO
);
142 close (STDOUT_FILENO
);
144 execvp (grepargv
[0], grepargv
);
145 fprintf (stderr
, "exec error (grep): %s", strerror (errno
));
146 _exit (EX_UNAVAILABLE
);
150 memset (buf
, 0, BUF_SIZE
);
151 nbytes
= read (pipefd
[0], buf
, BUF_SIZE
);
157 fprintf (stderr
, "read error (userprog): %s", strerror (errno
));
162 } else if (nbytes
== 0) {
163 fprintf (stderr
, "Child program exited prematurely (userprog).\n");
166 if (waitpid (cpid
, &status
, WNOHANG
) > 0 && WIFEXITED (status
)) {
167 return WEXITSTATUS (status
);
169 return EX_UNAVAILABLE
;
171 /* have new userprog-data, send it to grep */
176 write(pipefd_togrep
[1], buf
, strlen(buf
));
179 if (waitpid (grep_cpid
, &grep_status
, WNOHANG
) > 0 && WIFEXITED (grep_status
)) {
180 if (WEXITSTATUS(grep_status
) == 0) {
181 /* grep exited with match found */
182 printf ("%d\n", cpid
);
183 /* create a new child to keep pipe alive (will exit with exec'd program) */
185 while (kill(cpid
, 0) != -1 && errno
!= ESRCH
) sleep (1);
194 /* grep exited due to an error */
195 fprintf (stderr
, "grep exited due to an error.");
198 close (pipefd_togrep
[1]);
203 if (opts
.timeout
> 0) {
204 gettimeofday (&now
, NULL
);
205 timersub (&now
, &begin
, &diff
);
206 if (diff
.tv_sec
>= opts
.timeout
) {
207 fprintf (stderr
, "Timeout reached. \n");
208 if (opts
.kill_sig
> 0) kill (cpid
, opts
.kill_sig
);
211 return EX_UNAVAILABLE
;