dlog_redirect_stdout: Use internal library 38/256438/4
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 2 Apr 2021 10:39:20 +0000 (12:39 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 2 Apr 2021 11:04:42 +0000 (13:04 +0200)
Change-Id: Id54c7df7a5ccbfbe6b2cd8167943b08ea3347080

Makefile.am
src/log-redirect-stdout/main.c

index a6c6451..43e94bc 100644 (file)
@@ -247,14 +247,12 @@ dlog_redirect_stdout_LDFLAGS = \
        $(AM_LDFLAGS) \
        -pie
 
+dlog_redirect_stdout_LDADD = \
+       libdlog_redirect_stdout.la
+
 dlog_redirect_stdout_SOURCES = \
-       src/log-redirect-stdout/main.c \
-       src/log-redirect-stdout/internal.c \
-        src/shared/backend_androidlogger.c \
-       src/shared/connect_pipe.c \
        src/shared/logcommon.c \
-       src/shared/logconfig.c \
-       src/shared/parsers.c
+       src/log-redirect-stdout/main.c
 
 usrlibexeclibdlogdir = /usr/libexec/libdlog
 
index f940e26..cedc6bd 100644 (file)
@@ -24,8 +24,7 @@
 #include <stdio.h>
 
 #include <logcommon.h>
-
-#include "internal.h"
+#include <dlog-redirect-stdout.h>
 
 /* Tag length is limited by maximum request size. */
 #define MAX_TAG_LENGTH (MAX_LOGGER_REQUEST_LEN - sizeof(struct dlog_control_msg) - sizeof(struct dlog_control_msg_stdout))
@@ -180,12 +179,12 @@ struct parse_result parse_command_line(int argc, char **argv)
        }
 }
 
-int setup_single(const struct parse_redirect_info *info, int *fd)
+int setup_single(const struct parse_redirect_info *info, int fd)
 {
        if (!info->enabled)
                return 0;
 
-       return setup_single_unstructed(info->buffer, info->tag, info->prio, fd);
+       return connect_dlog(info->buffer, fd, info->tag, info->prio);
 }
 
 int try_redirect(const struct parse_info *info)
@@ -193,37 +192,25 @@ int try_redirect(const struct parse_info *info)
        if (!info->out.enabled && !info->err.enabled)
                return 0;
 
-       __attribute__((cleanup(close_fd))) int outfd = -1;
-       __attribute__((cleanup(close_fd))) int errfd = -1;
+       int origout = dup(STDOUT_FILENO);
+       if (origout == -1)
+               return -errno;
 
-       int r = setup_single(&info->out, &outfd);
-       if (r < 0)
+       int r = setup_single(&info->out, STDOUT_FILENO);
+       if (r < 0) {
+               close(origout);
                return r;
+       }
 
-       r = setup_single(&info->err, &errfd);
-       if (r < 0)
+       r = setup_single(&info->err, STDERR_FILENO);
+       if (r < 0) {
+               dup2(origout, STDOUT_FILENO);
+               if (origout != STDOUT_FILENO)
+                       close(origout);
                return r;
-
-       /* We wait until both FDs are open, even though we could dup2
-        * stdout right when opened (so that redirection would work
-        * for stdout even if the stderr sink fails to open), because
-        * failure shouldn't really happen and it's better not to let
-        * errors silently fester.
-        *
-        * If somebody happens to use this on a mismaintained system
-        * on which it is not unusual for opening devices to fail,
-        * it might be viable to move the dup2 here so that at least
-        * something gets redirected. */
-
-       if (info->out.enabled) {
-               r = TEMP_FAILURE_RETRY(dup2(outfd, STDOUT_FILENO));
-               assert(r == STDOUT_FILENO);
        }
 
-       if (info->err.enabled) {
-               r = TEMP_FAILURE_RETRY(dup2(errfd, STDERR_FILENO));
-               assert(r == STDERR_FILENO);
-       }
+       close(origout);
 
        return 0;
 }