X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=os-posix.c;h=d39261d84949243cfc407d93603eb4d66a0d0586;hb=60aad298cb6de52f2716b2e82e1353ea9de95fd6;hp=612b6417371a60b4cd785fb7a7f5f3661141ef8c;hpb=949d31e665e9847b2a8f24a916abd96e79353535;p=qemu.git diff --git a/os-posix.c b/os-posix.c index 612b64173..d39261d84 100644 --- a/os-posix.c +++ b/os-posix.c @@ -31,11 +31,12 @@ /*needed for MAP_POPULATE before including qemu-options.h */ #include #include +#include #include /* Needed early for CONFIG_BSD etc. */ #include "config-host.h" -#include "sysemu.h" +#include "sysemu/sysemu.h" #include "net/slirp.h" #include "qemu-options.h" @@ -43,8 +44,8 @@ #include #endif -#ifdef CONFIG_EVENTFD -#include +#ifdef __FreeBSD__ +#include #endif static struct passwd *user_pwd; @@ -61,14 +62,9 @@ void os_setup_early_signal_handling(void) sigaction(SIGPIPE, &act, NULL); } -static void termsig_handler(int signal) +static void termsig_handler(int signal, siginfo_t *info, void *c) { - qemu_system_shutdown_request(); -} - -static void sigchld_handler(int signal) -{ - waitpid(-1, NULL, WNOHANG); + qemu_system_killed(info->si_signo, info->si_pid); } void os_setup_signal_handling(void) @@ -76,14 +72,11 @@ void os_setup_signal_handling(void) struct sigaction act; memset(&act, 0, sizeof(act)); - act.sa_handler = termsig_handler; + act.sa_sigaction = termsig_handler; + act.sa_flags = SA_SIGINFO; sigaction(SIGINT, &act, NULL); sigaction(SIGHUP, &act, NULL); sigaction(SIGTERM, &act, NULL); - - act.sa_handler = sigchld_handler; - act.sa_flags = SA_NOCLDSTOP; - sigaction(SIGCHLD, &act, NULL); } /* Find a likely location for support files using the location of the binary. @@ -134,12 +127,12 @@ char *os_find_datadir(const char *argv0) max_len = strlen(dir) + MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; - res = qemu_mallocz(max_len); + res = g_malloc0(max_len); snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX); if (access(res, R_OK)) { snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX); if (access(res, R_OK)) { - qemu_free(res); + g_free(res); res = NULL; } } @@ -155,8 +148,7 @@ void os_set_proc_name(const char *s) char name[16]; if (!s) return; - name[sizeof(name) - 1] = 0; - strncpy(name, s, sizeof(name)); + pstrcpy(name, sizeof(name), s); /* Could rewrite argv[0] too, but that's a bit more complicated. This simple way is enough for `top'. */ if (prctl(PR_SET_NAME, name)) { @@ -195,8 +187,12 @@ void os_parse_cmd_args(int index, const char *optarg) case QEMU_OPTION_daemonize: daemonize = 1; break; +#if defined(CONFIG_LINUX) + case QEMU_OPTION_enablefips: + fips_set_state(true); + break; +#endif } - return; } static void change_process_uid(void) @@ -206,6 +202,11 @@ static void change_process_uid(void) fprintf(stderr, "Failed to setgid(%d)\n", user_pwd->pw_gid); exit(1); } + if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) { + fprintf(stderr, "Failed to initgroups(\"%s\", %d)\n", + user_pwd->pw_name, user_pwd->pw_gid); + exit(1); + } if (setuid(user_pwd->pw_uid) < 0) { fprintf(stderr, "Failed to setuid(%d)\n", user_pwd->pw_uid); exit(1); @@ -334,30 +335,43 @@ void os_set_line_buffering(void) setvbuf(stdout, NULL, _IOLBF, 0); } -/* - * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set. - */ -int qemu_eventfd(int fds[2]) +int qemu_create_pidfile(const char *filename) { -#ifdef CONFIG_EVENTFD - int ret; - - ret = eventfd(0, 0); - if (ret >= 0) { - fds[0] = ret; - qemu_set_cloexec(ret); - if ((fds[1] = dup(ret)) == -1) { - close(ret); - return -1; - } - qemu_set_cloexec(fds[1]); - return 0; - } + char buffer[128]; + int len; + int fd; - if (errno != ENOSYS) { + fd = qemu_open(filename, O_RDWR | O_CREAT, 0600); + if (fd == -1) { return -1; } -#endif + if (lockf(fd, F_TLOCK, 0) == -1) { + close(fd); + return -1; + } + len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid()); + if (write(fd, buffer, len) != len) { + close(fd); + return -1; + } + + /* keep pidfile open & locked forever */ + return 0; +} + +bool is_daemonized(void) +{ + return daemonize; +} + +int os_mlock(void) +{ + int ret = 0; + + ret = mlockall(MCL_CURRENT | MCL_FUTURE); + if (ret < 0) { + perror("mlockall"); + } - return qemu_pipe(fds); + return ret; }