summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2013-05-19 19:58:51 +0200
committerMichael Stapelberg <michael@stapelberg.de>2013-05-19 19:58:51 +0200
commit3beb6b44062811ce7c87c13331899b9f3be9e348 (patch)
tree54d4726694da1e1cb5207f905f888e3acdeb3337
parent7a372b0f4627b9482d1276238348a1432c13fbe3 (diff)
signal handler: set flag because directly calling fprintf() is unsafe
-rw-r--r--i3status.c23
1 files changed, 16 insertions, 7 deletions
diff --git a/i3status.c b/i3status.c
index 5311ca9..c6973f7 100644
--- a/i3status.c
+++ b/i3status.c
@@ -45,19 +45,19 @@
/* socket file descriptor for general purposes */
int general_socket;
+static bool exit_upon_signal = false;
+
cfg_t *cfg, *cfg_general, *cfg_section;
/*
- * Exit upon SIGPIPE because when we have nowhere to write to, gathering system
- * information is pointless. Also exit explicitly on SIGTERM and SIGINT because
- * only this will trigger a reset of the cursor in the terminal output-format.
+ * Set the exit_upon_signal flag, because one cannot do anything in a safe
+ * manner in a signal handler (e.g. fprintf, which we really want to do for
+ * debugging purposes), see
+ * https://www.securecoding.cert.org/confluence/display/seccode/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers
*
*/
void fatalsig(int signum) {
- fprintf(stderr, "Received SIG%s, exiting\n", signum == SIGPIPE ? "PIPE" :
- signum == SIGTERM ? "TERM" :
- "INT");
- exit(1);
+ exit_upon_signal = true;
}
/*
@@ -325,6 +325,11 @@ int main(int argc, char *argv[]) {
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = fatalsig;
+
+ /* Exit upon SIGPIPE because when we have nowhere to write to, gathering system
+ * information is pointless. Also exit explicitly on SIGTERM and SIGINT because
+ * only this will trigger a reset of the cursor in the terminal output-format.
+ */
sigaction(SIGPIPE, &action, NULL);
sigaction(SIGTERM, &action, NULL);
sigaction(SIGINT, &action, NULL);
@@ -428,6 +433,10 @@ int main(int argc, char *argv[]) {
char buffer[4096];
while (1) {
+ if (exit_upon_signal) {
+ fprintf(stderr, "Exiting due to signal.\n");
+ exit(1);
+ }
struct timeval tv;
gettimeofday(&tv, NULL);
if (output_format == O_I3BAR)