#include <sys/mount.h>
#include <sys/prctl.h>
+#if HAVE_LIBDLOG_REDIRECT
+#include <dlog/dlog-redirect-stdout.h>
+#endif
+
#if HAVE_PAM
#include <security/pam_appl.h>
#include <security/pam_misc.h>
return !!c->tty_path;
}
-static int open_null_as(int flags, int nfd) {
+static int open_device_as(const char* devname, int flags, int nfd) {
int fd;
assert(nfd >= 0);
- fd = open("/dev/null", flags|O_NOCTTY);
+ fd = open(devname, flags|O_NOCTTY);
if (fd < 0)
return -errno;
return move_fd(fd, nfd, false);
}
+static int open_null_as(int flags, int nfd) {
+ return open_device_as("/dev/null", flags, nfd);
+}
+
+static int open_kmsg_as(int flags, int nfd) {
+ return open_device_as("/dev/kmsg", flags, nfd);
+}
+
static int connect_journal_socket(
int fd,
const char *log_namespace,
return true;
}
+#if HAVE_LIBDLOG_REDIRECT
+static int wire_up_dlog(int fileno, const char *ident)
+{
+ /* NB: dlogutil has FOO* tag filtering wildcards but not *FOO
+ * therefore the STDFOO part of the final tag goes in front.
+ *
+ * The tag can nominally be longer than 128 but dlogutil will
+ * not print a longer one anyway and it's good to limit overhead */
+ char tag[128];
+ int priority;
+
+ if (fileno == STDERR_FILENO) {
+ priority = 6; // ERROR
+ snprintf(tag, sizeof tag, "STDERR_%s", ident);
+ } else {
+ priority = 4; // INFO
+ snprintf(tag, sizeof tag, "STDOUT_%s", ident);
+ }
+
+ return connect_dlog(2, // SYSTEM dlog buffer (consider also 0 for generic MAIN buffer)
+ fileno, tag, priority);
+}
+#endif
+
static int setup_output(
const ExecContext *context,
const ExecParameters *params,
}
return r;
+#if HAVE_LIBDLOG_REDIRECT
+ case EXEC_OUTPUT_DLOG:
+ r = wire_up_dlog(fileno, ident);
+ return r < 0 ? r : fileno;
+
+ case EXEC_OUTPUT_DLOG_OR_NULL:
+ r = wire_up_dlog(fileno, ident);
+ if (r >= 0)
+ return fileno;
+ else
+ return open_null_as(O_WRONLY, fileno);
+
+ case EXEC_OUTPUT_DLOG_OR_KMSG:
+ r = wire_up_dlog(fileno, ident);
+ if (r >= 0)
+ return fileno;
+ else
+ return open_kmsg_as(O_WRONLY, fileno);
+#endif
+
case EXEC_OUTPUT_SOCKET:
assert(socket_fd >= 0);
[EXEC_OUTPUT_FILE] = "file",
[EXEC_OUTPUT_FILE_APPEND] = "append",
[EXEC_OUTPUT_FILE_TRUNCATE] = "truncate",
+#if HAVE_LIBDLOG_REDIRECT
+ [EXEC_OUTPUT_DLOG] = "dlog",
+ [EXEC_OUTPUT_DLOG_OR_NULL] = "dlog|null",
+ [EXEC_OUTPUT_DLOG_OR_KMSG] = "dlog|kmsg",
+#endif
};
DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);