$(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
#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))
}
}
-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)
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;
}