From 7966a08a037b730a740305b15b615b25376b93d6 Mon Sep 17 00:00:00 2001 From: girst Date: Fri, 13 Oct 2017 16:58:35 +0200 Subject: [PATCH] terminate PROGRAM when fag gets interrupted/terminated useful, when one runs fag interactively and notices a mistake in the command line. you can now just ^C is such a case. --- fag.1 | 13 ++++++++++--- fag.c | 17 +++++++++++++++-- fag.h | 2 +- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/fag.1 b/fag.1 index 0c67e21..ca3da64 100644 --- a/fag.1 +++ b/fag.1 @@ -1,7 +1,7 @@ -.TH fag 1 "12 August 2017" "1.0" "User Commands" +.TH fag 1 "13 October 2017" "1.1" "User Commands" .hy 0 .SH NAME -fag \- daemonize program after a string was found (ForkAfterGrep) +fag \- daemonize program after a regular expression pattern was matched (ForkAfterGrep) . .SH SYNOPSIS .B fag @@ -74,7 +74,14 @@ Please report bugs and patches to the issue tracker at https://github.com/girst/ .SH NOTES .IR SIGNAL needs to be given as an integer. implementing mnemonics is dirty and not very portable; maybe support in the future. +.PP +Since 1.1, if +.B fag +gets interrupted or terminated, +.BR SIGTERM +is sent to +.IR PROGRAM. .SH COPYRIGHT Copyright 2017 Tobias Girstmair. This is free software; see https://www.gnu.org/licenses/gpl-3.0.html for conditions. .SH AUTHOR -Tobias Girstmair (http://isticktoit.net) +Tobias Girstmair (https://gir.st) diff --git a/fag.c b/fag.c index cbf886d..adcb7b7 100644 --- a/fag.c +++ b/fag.c @@ -1,5 +1,6 @@ /* forkaftergrep (C) 2017 Tobias Girstmair, GPLv3 */ //TODO: if grep exits with an error, fag thinks a match was found +//TODO: allow redirect of both streams to files #define _XOPEN_SOURCE 500 #define _DEFAULT_SOURCE @@ -15,6 +16,14 @@ #include #include "fag.h" +pid_t cpid = 0; + +void term_child(int s) { + (void)s; /* squash -Werror=unused-parameter */ + if (cpid != 0) kill (cpid, SIGTERM); + exit(1); +} + int main (int argc, char** argv) { struct opt opts = {0, 0, 0, NULL, NULL, STDOUT_FILENO, "-q"}; /* `-q': don't print anything; exit with 0 on match; with 1 on error. used to interface with `grep' */ @@ -25,10 +34,14 @@ int main (int argc, char** argv) { char* p = opts.grepopt+2; /* move cursor behind `q' */ signal (SIGPIPE, SIG_IGN); /* ignore broken pipe between fag and grep */ + struct sigaction sa = {0}; /* terminate child if fag gets interrupted/terminated */ + sa.sa_handler = &term_child; /* NOTE: will also be inherited by keep-pipe-alive-child */ + sigaction (SIGINT, &sa, NULL); + sigaction (SIGTERM, &sa, NULL); /* the `+' forces getopt to stop at the first non-option */ - while ((opt = getopt (argc, argv, "+t:k::eVhvEFGPiwxyU")) != -1) { + while ((opt = getopt (argc, argv, "+t:k::er:VhvEFGPiwxyU")) != -1) { switch (opt) { case 't': opts.timeout = atoi (optarg); @@ -90,7 +103,7 @@ int main (int argc, char** argv) { int fork_after_grep (struct opt opts) { int pipefd[2]; - pid_t cpid; + //pid_t cpid; int status; char buf[BUF_SIZE]; diff --git a/fag.h b/fag.h index 65148c0..5d765bc 100644 --- a/fag.h +++ b/fag.h @@ -2,7 +2,7 @@ #define __FAG_H__ #define PROGRNAME "fag" -#define VERSION "1.0" +#define VERSION "1.1" #define AUTHOR "Tobias Girstmair" #define YEAR "2017" #define LICENSE "GNU GPLv3" -- 2.39.3