Don't wait until lazy init to block SIGPIPE 43/227643/2
authorMichal Bloch <m.bloch@samsung.com>
Fri, 13 Mar 2020 11:38:44 +0000 (12:38 +0100)
committerMichal Bloch <m.bloch@partner.samsung.com>
Fri, 13 Mar 2020 13:39:54 +0000 (13:39 +0000)
Change-Id: I116d1e3c23f00e2ac1523a7ffcb469bcf50d40d5
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/libdlog/log.c
src/libdlog/log_pipe.c

index 08d1e6a..a677641 100644 (file)
@@ -186,6 +186,26 @@ bool __configure(void)
        return true;
 }
 
+static void __attribute__((constructor(101))) __install_pipe_handler(void)
+{
+       /* We mask SIGPIPE signal because most applications do not install their
+        * own SIGPIPE handler. Default behaviour in SIGPIPE case is to abort the
+        * process. SIGPIPE occurs when e.g. dlog daemon closes read pipe endpoint.
+        *
+        * We do this in the library constructor (at maximum priority) and not
+        * during regular (lazy) initialisation so as to prevent overwriting the
+        * program's actual signal handler, if it has one.
+        *
+        * In theory this is not required for the Android logger backend; however,
+        * this early we don't yet know the backend and also it is good to behave
+        * consistently in this regard anyway.
+        *
+        * We don't revert this in a destructor because Unix signals are bonkers
+        * and we have no way to do this cleanly. Most libdlog users don't use
+        * runtime linking so this would mostly done at program exit either way. */
+       signal(SIGPIPE, SIG_IGN);
+}
+
 static bool first = true;
 static bool initialize()
 {
index b0e89b6..8459d9a 100644 (file)
@@ -194,7 +194,6 @@ static void __destroy_pipe()
                        close(pipe_fd[i]);
 
        wait_pipe_ms = DEFAULT_WAIT_PIPE_MS;
-       signal(SIGPIPE, SIG_DFL);
 }
 
 /**
@@ -203,16 +202,7 @@ static void __destroy_pipe()
  */
 void __dlog_init_pipe(const struct log_config *conf)
 {
-       int i;
-
-       /*
-        * We mask SIGPIPE signal because most applications do not install their
-        * own SIGPIPE handler. Default behaviour in SIGPIPE case is to abort the
-        * process. SIGPIPE occurs when e.g. dlog daemon closes read pipe endpoint
-        */
-       signal(SIGPIPE, SIG_IGN);
-
-       for (i = 0; i < LOG_ID_MAX; ++i) {
+       for (int i = 0; i < LOG_ID_MAX; ++i) {
                char conf_key[MAX_CONF_KEY_LEN];
                snprintf(conf_key, sizeof(conf_key), "%s_write_sock", log_name_by_id(i));
                const char * conf_val = log_config_get(conf, conf_key);