Move daemonize handling to OS specific files

Move daemonize handling from vl.c to OS specific files. Provide dummy
stubs for Win32.

Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
Acked-by: Juan Quintela <quintela@redhat.com>
Acked-by: Richard Henderson <rth@redhat.com>
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
This commit is contained in:
Jes Sorensen 2010-06-10 11:42:28 +02:00 committed by Blue Swirl
parent 0766379d4c
commit eb505be11b
6 changed files with 115 additions and 103 deletions

View File

@ -39,6 +39,8 @@
static struct passwd *user_pwd; static struct passwd *user_pwd;
static const char *chroot_dir; static const char *chroot_dir;
static int daemonize;
static int fds[2];
void os_setup_early_signal_handling(void) void os_setup_early_signal_handling(void)
{ {
@ -160,6 +162,9 @@ void os_parse_cmd_args(int index, const char *optarg)
case QEMU_OPTION_chroot: case QEMU_OPTION_chroot:
chroot_dir = optarg; chroot_dir = optarg;
break; break;
case QEMU_OPTION_daemonize:
daemonize = 1;
break;
} }
return; return;
} }
@ -196,3 +201,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));
}

View File

@ -214,3 +214,8 @@ void os_parse_cmd_args(int index, const char *optarg)
{ {
return; return;
} }
void os_pidfile_error(void)
{
fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
}

View File

@ -33,5 +33,7 @@ static inline void os_host_main_loop_wait(int *timeout)
void os_setup_signal_handling(void); void os_setup_signal_handling(void);
void os_change_process_uid(void); void os_change_process_uid(void);
void os_change_root(void); void os_change_root(void);
void os_daemonize(void);
void os_setup_post(void);
#endif #endif

View File

@ -45,5 +45,7 @@ void os_host_main_loop_wait(int *timeout);
static inline void os_setup_signal_handling(void) {} static inline void os_setup_signal_handling(void) {}
static inline void os_change_process_uid(void) {} static inline void os_change_process_uid(void) {}
static inline void os_change_root(void) {} static inline void os_change_root(void) {}
static inline void os_daemonize(void) {}
static inline void os_setup_post(void) {}
#endif #endif

View File

@ -83,6 +83,7 @@ void do_info_slirp(Monitor *mon);
void os_setup_early_signal_handling(void); void os_setup_early_signal_handling(void);
char *os_find_datadir(const char *argv0); char *os_find_datadir(const char *argv0);
void os_parse_cmd_args(int index, const char *optarg); void os_parse_cmd_args(int index, const char *optarg);
void os_pidfile_error(void);
typedef enum DisplayType typedef enum DisplayType
{ {

106
vl.c
View File

@ -216,9 +216,6 @@ int no_shutdown = 0;
int cursor_hide = 1; int cursor_hide = 1;
int graphic_rotate = 0; int graphic_rotate = 0;
uint8_t irq0override = 1; uint8_t irq0override = 1;
#ifndef _WIN32
int daemonize = 0;
#endif
const char *watchdog; const char *watchdog;
const char *option_rom[MAX_OPTION_ROMS]; const char *option_rom[MAX_OPTION_ROMS];
int nb_option_roms; int nb_option_roms;
@ -2301,15 +2298,9 @@ int main(int argc, char **argv, char **envp)
const char *loadvm = NULL; const char *loadvm = NULL;
QEMUMachine *machine; QEMUMachine *machine;
const char *cpu_model; const char *cpu_model;
#ifndef _WIN32
int fds[2];
#endif
int tb_size; int tb_size;
const char *pid_file = NULL; const char *pid_file = NULL;
const char *incoming = NULL; const char *incoming = NULL;
#ifndef _WIN32
int fd = 0;
#endif
int show_vnc_port = 0; int show_vnc_port = 0;
int defconfig = 1; int defconfig = 1;
@ -2975,11 +2966,6 @@ int main(int argc, char **argv, char **envp)
exit(1); exit(1);
} }
break; break;
#ifndef _WIN32
case QEMU_OPTION_daemonize:
daemonize = 1;
break;
#endif
case QEMU_OPTION_option_rom: case QEMU_OPTION_option_rom:
if (nb_option_roms >= MAX_OPTION_ROMS) { if (nb_option_roms >= MAX_OPTION_ROMS) {
fprintf(stderr, "Too many option ROMs\n"); fprintf(stderr, "Too many option ROMs\n");
@ -3194,64 +3180,10 @@ int main(int argc, char **argv, char **envp)
} }
#endif #endif
#ifndef _WIN32 os_daemonize();
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
if (pid_file && qemu_create_pidfile(pid_file) != 0) { if (pid_file && qemu_create_pidfile(pid_file) != 0) {
#ifndef _WIN32 os_pidfile_error();
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));
exit(1); exit(1);
} }
@ -3520,39 +3452,7 @@ int main(int argc, char **argv, char **envp)
vm_start(); vm_start();
} }
#ifndef _WIN32 os_setup_post();
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
main_loop(); main_loop();
quit_timers(); quit_timers();