Merge branch 'rawoul/daemonize' into 'main'

compositor: add option to daemonize after socket creation

See merge request wayland/weston!1396
This commit is contained in:
Arnaud Vrac 2025-12-11 05:48:50 +00:00
commit aa0ef247da

View file

@ -37,6 +37,7 @@
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/wait.h>
@ -5307,6 +5308,7 @@ wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
char *server_socket = NULL;
char *require_outputs = NULL;
int32_t idle_time = -1;
int32_t daemonize = 0;
int32_t help = 0;
char *socket_name = NULL;
int32_t version = 0;
@ -5331,6 +5333,7 @@ wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
bool wait_for_debugger = false;
struct wl_protocol_logger *protologger = NULL;
int pipefd[2];
const struct weston_option core_options[] = {
{ WESTON_OPTION_STRING, "backend", 'B', &backends },
@ -5345,6 +5348,7 @@ wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
#endif
{ WESTON_OPTION_STRING, "modules", 0, &option_modules },
{ WESTON_OPTION_STRING, "log", 0, &log },
{ WESTON_OPTION_BOOLEAN, "daemonize", 'd', &daemonize },
{ WESTON_OPTION_BOOLEAN, "help", 'h', &help },
{ WESTON_OPTION_BOOLEAN, "version", 0, &version },
{ WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
@ -5419,6 +5423,30 @@ wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
weston_log_continue("\n");
if (daemonize) {
pid_t pid;
if (pipe2(pipefd, O_CLOEXEC) < 0) {
weston_log("fatal: pipe2 failed: %m\n");
return EXIT_FAILURE;
}
pid = fork();
if (pid < 0) {
weston_log("fatal: fork failed: %m\n");
return EXIT_FAILURE;
}
if (pid > 0) {
close(pipefd[1]);
if (read(pipefd[0], &ret, sizeof (ret)) <= 0)
ret = EXIT_FAILURE;
_exit(ret);
}
close(pipefd[0]);
}
display = wl_display_create();
if (display == NULL) {
weston_log("fatal: failed to create display\n");
@ -5655,6 +5683,29 @@ wet_main(int argc, char *argv[], const struct weston_testsuite_data *test_data)
goto out;
}
if (daemonize) {
int devnull;
ssize_t written __attribute__ ((unused));
if (setsid() < 0) {
weston_log("fatal: setsid failed: %m\n");
goto out;
}
if ((devnull = open("/dev/null", O_RDWR, 0)) != -1) {
dup2(devnull, 0);
dup2(devnull, 1);
dup2(devnull, 2);
if (devnull > 2)
close(devnull);
}
ret = EXIT_SUCCESS;
written = write(pipefd[1], &ret, sizeof (ret));
close(pipefd[1]);
}
weston_compositor_wake(wet.compositor);
if (argc > 1) {