From patchwork Thu Jun 3 16:48:09 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 54508 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 0C9ECB6ED0 for ; Fri, 4 Jun 2010 03:12:52 +1000 (EST) Received: from localhost ([127.0.0.1]:59187 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OKDyK-0002OH-Qm for incoming@patchwork.ozlabs.org; Thu, 03 Jun 2010 13:12:48 -0400 Received: from [140.186.70.92] (port=59662 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OKDb2-0002jU-15 for qemu-devel@nongnu.org; Thu, 03 Jun 2010 12:48:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OKDb0-00069b-Ao for qemu-devel@nongnu.org; Thu, 03 Jun 2010 12:48:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:24428) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OKDaz-00069R-SJ for qemu-devel@nongnu.org; Thu, 03 Jun 2010 12:48:42 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o53GmdaM016827 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 3 Jun 2010 12:48:39 -0400 Received: from localhost.localdomain (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o53GmE8o028747; Thu, 3 Jun 2010 12:48:38 -0400 From: Jes.Sorensen@redhat.com To: anthony@codemonkey.ws Date: Thu, 3 Jun 2010 18:48:09 +0200 Message-Id: <1275583692-11678-14-git-send-email-Jes.Sorensen@redhat.com> In-Reply-To: <1275583692-11678-1-git-send-email-Jes.Sorensen@redhat.com> References: <1275583692-11678-1-git-send-email-Jes.Sorensen@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: Jes Sorensen , qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH 13/16] Move daemonize handling to OS specific files X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Jes Sorensen Move daemonize handling from vl.c to OS specific files. Provide dummy stubs for Win32. Signed-off-by: Jes Sorensen --- os-posix.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ os-win32.c | 5 +++ qemu-os-posix.h | 2 + qemu-os-win32.h | 2 + sysemu.h | 1 + vl.c | 106 ++----------------------------------------------------- 6 files changed, 115 insertions(+), 103 deletions(-) diff --git a/os-posix.c b/os-posix.c index a91e1f6..8a9d102 100644 --- a/os-posix.c +++ b/os-posix.c @@ -38,6 +38,8 @@ static struct passwd *user_pwd; static const char *chroot_dir; +static int daemonize; +static int fds[2]; void os_setup_early_signal_handling(void) { @@ -173,6 +175,9 @@ int os_parse_cmd_args(const QEMUOption *popt, const char *optarg) case QEMU_OPTION_chroot: chroot_dir = optarg; break; + case QEMU_OPTION_daemonize: + daemonize = 1; + break; default: ret = -1; } @@ -211,3 +216,100 @@ void os_change_root(void) } } + +void os_daemonize(void) +{ + if (daemonize) { + pid_t pid; + + if (pipe(fds) == -1) + exit(1); + + pid = fork(); + if (pid > 0) { + uint8_t status; + ssize_t len; + + close(fds[1]); + + again: + len = read(fds[0], &status, 1); + if (len == -1 && (errno == EINTR)) + goto again; + + if (len != 1) + exit(1); + else if (status == 1) { + fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno)); + exit(1); + } else + exit(0); + } else if (pid < 0) + exit(1); + + close(fds[0]); + qemu_set_cloexec(fds[1]); + + setsid(); + + pid = fork(); + if (pid > 0) + exit(0); + else if (pid < 0) + exit(1); + + umask(027); + + signal(SIGTSTP, SIG_IGN); + signal(SIGTTOU, SIG_IGN); + signal(SIGTTIN, SIG_IGN); + } +} + +void os_setup_post(void) +{ + int fd = 0; + + if (daemonize) { + uint8_t status = 0; + ssize_t len; + + again1: + len = write(fds[1], &status, 1); + if (len == -1 && (errno == EINTR)) + goto again1; + + if (len != 1) + exit(1); + + if (chdir("/")) { + perror("not able to chdir to /"); + exit(1); + } + TFR(fd = qemu_open("/dev/null", O_RDWR)); + if (fd == -1) + exit(1); + } + + os_change_root(); + os_change_process_uid(); + + if (daemonize) { + dup2(fd, 0); + dup2(fd, 1); + dup2(fd, 2); + + close(fd); + } +} + +void os_pidfile_error(void) +{ + if (daemonize) { + uint8_t status = 1; + if (write(fds[1], &status, 1) != 1) { + perror("daemonize. Writing to pipe\n"); + } + } else + fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno)); +} diff --git a/os-win32.c b/os-win32.c index a311a90..86ff327 100644 --- a/os-win32.c +++ b/os-win32.c @@ -226,3 +226,8 @@ int os_parse_cmd_args(const QEMUOption *popt, const char *optarg) { return -1; } + +void os_pidfile_error(void) +{ + fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno)); +} diff --git a/qemu-os-posix.h b/qemu-os-posix.h index 91c7b68..9b07660 100644 --- a/qemu-os-posix.h +++ b/qemu-os-posix.h @@ -33,5 +33,7 @@ static inline void os_host_main_loop_wait(int *timeout) void os_setup_signal_handling(void); void os_change_process_uid(void); void os_change_root(void); +void os_daemonize(void); +void os_setup_post(void); #endif diff --git a/qemu-os-win32.h b/qemu-os-win32.h index 245b188..ccb9691 100644 --- a/qemu-os-win32.h +++ b/qemu-os-win32.h @@ -45,5 +45,7 @@ void os_host_main_loop_wait(int *timeout); static inline void os_setup_signal_handling(void) {}; static inline void os_change_process_uid(void) {}; static inline void os_change_root(void) {}; +static inline void os_daemonize(void) {}; +static inline void os_setup_post(void) {}; #endif diff --git a/sysemu.h b/sysemu.h index 08ec323..aa44a20 100644 --- a/sysemu.h +++ b/sysemu.h @@ -91,6 +91,7 @@ typedef struct QEMUOption { void os_setup_early_signal_handling(void); char *os_find_datadir(const char *argv0); int os_parse_cmd_args(const QEMUOption *popt, const char *optarg); +void os_pidfile_error(void); typedef enum DisplayType { diff --git a/vl.c b/vl.c index 7173684..bb8abbf 100644 --- a/vl.c +++ b/vl.c @@ -215,9 +215,6 @@ int no_shutdown = 0; int cursor_hide = 1; int graphic_rotate = 0; uint8_t irq0override = 1; -#ifndef _WIN32 -int daemonize = 0; -#endif const char *watchdog; const char *option_rom[MAX_OPTION_ROMS]; int nb_option_roms; @@ -2303,15 +2300,9 @@ int main(int argc, char **argv, char **envp) const char *loadvm = NULL; QEMUMachine *machine; const char *cpu_model; -#ifndef _WIN32 - int fds[2]; -#endif int tb_size; const char *pid_file = NULL; const char *incoming = NULL; -#ifndef _WIN32 - int fd = 0; -#endif int show_vnc_port = 0; int defconfig = 1; @@ -2977,11 +2968,6 @@ int main(int argc, char **argv, char **envp) exit(1); } break; -#ifndef _WIN32 - case QEMU_OPTION_daemonize: - daemonize = 1; - break; -#endif case QEMU_OPTION_option_rom: if (nb_option_roms >= MAX_OPTION_ROMS) { fprintf(stderr, "Too many option ROMs\n"); @@ -3195,64 +3181,10 @@ int main(int argc, char **argv, char **envp) } #endif -#ifndef _WIN32 - if (daemonize) { - pid_t pid; - - if (pipe(fds) == -1) - exit(1); - - pid = fork(); - if (pid > 0) { - uint8_t status; - ssize_t len; - - close(fds[1]); - - again: - len = read(fds[0], &status, 1); - if (len == -1 && (errno == EINTR)) - goto again; - - if (len != 1) - exit(1); - else if (status == 1) { - fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno)); - exit(1); - } else - exit(0); - } else if (pid < 0) - exit(1); - - close(fds[0]); - qemu_set_cloexec(fds[1]); - - setsid(); - - pid = fork(); - if (pid > 0) - exit(0); - else if (pid < 0) - exit(1); - - umask(027); - - signal(SIGTSTP, SIG_IGN); - signal(SIGTTOU, SIG_IGN); - signal(SIGTTIN, SIG_IGN); - } -#endif + os_daemonize(); if (pid_file && qemu_create_pidfile(pid_file) != 0) { -#ifndef _WIN32 - if (daemonize) { - uint8_t status = 1; - if (write(fds[1], &status, 1) != 1) { - perror("daemonize. Writing to pipe\n"); - } - } else -#endif - fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno)); + os_pidfile_error(); exit(1); } @@ -3520,39 +3452,7 @@ int main(int argc, char **argv, char **envp) vm_start(); } -#ifndef _WIN32 - if (daemonize) { - uint8_t status = 0; - ssize_t len; - - again1: - len = write(fds[1], &status, 1); - if (len == -1 && (errno == EINTR)) - goto again1; - - if (len != 1) - exit(1); - - if (chdir("/")) { - perror("not able to chdir to /"); - exit(1); - } - TFR(fd = qemu_open("/dev/null", O_RDWR)); - if (fd == -1) - exit(1); - } - - os_change_root(); - os_change_process_uid(); - - if (daemonize) { - dup2(fd, 0); - dup2(fd, 1); - dup2(fd, 2); - - close(fd); - } -#endif + os_setup_post(); main_loop(); quit_timers();