Module Name: src
Committed By: martin
Date: Wed Sep 11 16:09:32 UTC 2024
Modified Files:
src/lib/librumpuser [netbsd-10]: rumpuser_daemonize.c
Log Message:
Pull up following revision(s) (requested by rin in ticket #824):
lib/librumpuser/rumpuser_daemonize.c: revision 1.9
lib/librumpuser/rumpuser_daemonize.c: revision 1.10
rumpuser(3): New RUMP_STDOUT, RUMP_STDERR environment variables.
If set, then when rump daemonizes, it opens the path in RUMP_STDOUT
and redirects fd 1 to that (which mostly gets the kernel console
output), and opens the path in RUMP_STDERR and redirects fd 2 to that
(no idea what this gets but it's probably good to record if it ever
gets anything).
This will allow tests that rely on rump_server daemons to stash the
output for diagnostics in case, e.g., the rump kernel crashes.
PR bin/58112
To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.7.26.1 src/lib/librumpuser/rumpuser_daemonize.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/librumpuser/rumpuser_daemonize.c
diff -u src/lib/librumpuser/rumpuser_daemonize.c:1.7 src/lib/librumpuser/rumpuser_daemonize.c:1.7.26.1
--- src/lib/librumpuser/rumpuser_daemonize.c:1.7 Tue Nov 4 19:05:17 2014
+++ src/lib/librumpuser/rumpuser_daemonize.c Wed Sep 11 16:09:32 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: rumpuser_daemonize.c,v 1.7 2014/11/04 19:05:17 pooka Exp $ */
+/* $NetBSD: rumpuser_daemonize.c,v 1.7.26.1 2024/09/11 16:09:32 martin Exp $ */
/*
* Copyright (c) 2010 Antti Kantee. All Rights Reserved.
@@ -28,7 +28,7 @@
#include "rumpuser_port.h"
#if !defined(lint)
-__RCSID("$NetBSD: rumpuser_daemonize.c,v 1.7 2014/11/04 19:05:17 pooka Exp $");
+__RCSID("$NetBSD: rumpuser_daemonize.c,v 1.7.26.1 2024/09/11 16:09:32 martin Exp $");
#endif /* !lint */
#include <sys/types.h>
@@ -53,6 +53,27 @@ static int daemonpipe[2];
#include <rump/rumpuser.h>
+static int
+openstdoutstderr(void)
+{
+ char path[PATH_MAX];
+ int fd;
+
+ if (getenv_r("RUMP_STDOUT", path, sizeof(path)) == 0) {
+ if ((fd = open(path, O_WRONLY|O_CREAT)) == -1)
+ return -1;
+ dup2(fd, STDOUT_FILENO);
+ (void)close(fd);
+ }
+ if (getenv_r("RUMP_STDERR", path, sizeof(path)) == 0) {
+ if ((fd = open(path, O_WRONLY|O_CREAT)) == -1)
+ return -1;
+ dup2(fd, STDERR_FILENO);
+ (void)close(fd);
+ }
+ return 0;
+}
+
int
rumpuser_daemonize_begin(void)
{
@@ -82,6 +103,13 @@ rumpuser_daemonize_begin(void)
goto out;
}
+ if (openstdoutstderr() == -1) {
+ rv = errno;
+ (void)close(daemonpipe[0]);
+ (void)close(daemonpipe[1]);
+ goto out;
+ }
+
switch (fork()) {
case 0:
if (setsid() == -1) {
@@ -125,12 +153,17 @@ rumpuser_daemonize_done(int error)
goto out;
}
dup2(fd, STDIN_FILENO);
- dup2(fd, STDOUT_FILENO);
- dup2(fd, STDERR_FILENO);
+ if (getenv("RUMP_STDOUT") == NULL)
+ dup2(fd, STDOUT_FILENO);
+ if (getenv("RUMP_STDERR") == NULL)
+ dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO)
close(fd);
}
+ fflush(stdout);
+ fflush(stderr);
+
out:
n = send(daemonpipe[1], &error, sizeof(error), MSG_NOSIGNAL);
if (n != sizeof(error)) {