Util-Linux package development
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: Sami Kerola <kerolasa@iki.fi>
Subject: [PATCH 06/12] write: run atexit() checks at the end of execution
Date: Mon,  9 May 2016 22:27:22 +0100	[thread overview]
Message-ID: <1462829248-6935-7-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1462829248-6935-1-git-send-email-kerolasa@iki.fi>

The use of _exit() at the end of earlier signal handler, that was called at
end of execution, caused atexit() standard file descriptor close checks not
to be done.  So make the signal handler to set flag to exit, and perform
expected exit workflow.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 term-utils/write.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/term-utils/write.c b/term-utils/write.c
index ed508d4..021e943 100644
--- a/term-utils/write.c
+++ b/term-utils/write.c
@@ -66,6 +66,8 @@
 #include "strutils.h"
 #include "xalloc.h"
 
+static sig_atomic_t signal_received = 0;
+
 static void __attribute__ ((__noreturn__)) usage(FILE * out)
 {
 	fputs(USAGE_HEADER, out);
@@ -205,13 +207,11 @@ static void search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
 }
 
 /*
- * done - cleanup and exit
+ * signal_handler - cause write loop to exit
  */
-static void __attribute__ ((__noreturn__))
-    done(int dummy __attribute__ ((__unused__)))
+static void signal_handler(int signo)
 {
-	printf("EOF\r\n");
-	_exit(EXIT_SUCCESS);
+	signal_received = signo;
 }
 
 /*
@@ -243,6 +243,7 @@ static void do_write(char *tty, char *mytty, uid_t myuid)
 	struct passwd *pwd;
 	time_t now;
 	char path[PATH_MAX], *host, line[512];
+	struct sigaction sigact;
 
 	/* Determine our login name(s) before the we reopen() stdout */
 	if ((pwd = getpwuid(myuid)) != NULL)
@@ -258,8 +259,11 @@ static void do_write(char *tty, char *mytty, uid_t myuid)
 	if ((freopen(path, "w", stdout)) == NULL)
 		err(EXIT_FAILURE, "%s", path);
 
-	signal(SIGINT, done);
-	signal(SIGHUP, done);
+	sigact.sa_handler = signal_handler;
+	sigemptyset(&sigact.sa_mask);
+	sigact.sa_flags = 0;
+	sigaction(SIGINT, &sigact, NULL);
+	sigaction(SIGHUP, &sigact, NULL);
 
 	/* print greeting */
 	host = xgethostname();
@@ -279,8 +283,12 @@ static void do_write(char *tty, char *mytty, uid_t myuid)
 	free(host);
 	printf("\r\n");
 
-	while (fgets(line, sizeof(line), stdin) != NULL)
+	while (fgets(line, sizeof(line), stdin) != NULL) {
+		if (signal_received)
+			break;
 		wr_fputs(line);
+	}
+	printf("EOF\r\n");
 }
 
 int main(int argc, char **argv)
@@ -368,8 +376,5 @@ int main(int argc, char **argv)
 	default:
 		usage(stderr);
 	}
-
-	done(0);
-	/* NOTREACHED */
-	return EXIT_FAILURE;
+	return EXIT_SUCCESS;
 }
-- 
2.8.2


  parent reply	other threads:[~2016-05-09 21:27 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-09 21:27 [PATCH 00/12] pull: write(1) improvements Sami Kerola
2016-05-09 21:27 ` [PATCH 01/12] write: remove unused variable Sami Kerola
2016-05-09 21:27 ` [PATCH 02/12] write: get rid of function prototypes Sami Kerola
2016-05-09 21:27 ` [PATCH 03/12] write: remove pointless fileno(3) calls Sami Kerola
2016-05-09 21:27 ` [PATCH 04/12] write: set atime value in term_chk() only if needed Sami Kerola
2016-05-09 21:27 ` [PATCH 05/12] write: use xstrncpy() from strutils.h Sami Kerola
2016-05-09 21:27 ` Sami Kerola [this message]
2016-05-09 21:27 ` [PATCH 07/12] write: add control structure to clarify what is going on Sami Kerola
2016-05-11  8:38   ` Karel Zak
2016-05-15 17:00     ` Sami Kerola
2016-05-09 21:27 ` [PATCH 08/12] write: improve variable names Sami Kerola
2016-05-09 21:27 ` [PATCH 09/12] write: remove unnecessary utmp variables Sami Kerola
2016-05-09 21:27 ` [PATCH 10/12] write: make timestamp to be obviously just a clock time Sami Kerola
2016-05-09 21:27 ` [PATCH 11/12] write: improve coding style Sami Kerola
2016-05-11  8:54   ` Karel Zak
2016-05-15 17:03     ` Sami Kerola
2016-05-09 21:27 ` [PATCH 12/12] write: tell when effective gid and tty path group mismatch Sami Kerola
2016-05-10 14:24   ` Benno Schulenberg
2016-05-10 19:37     ` Sami Kerola

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1462829248-6935-7-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox