Skip to content

Commit

Permalink
pidfile: stop making pidfile error a special case
Browse files Browse the repository at this point in the history
In case of -daemonize, we write non-zero to the daemon
pipe only if pidfile creation failed, so the parent will
report error about pidfile problem.  There's no need to
make special case for this, since all other errors are
reported by the child just fine.  Let the parent report
error and simplify logic in os_daemonize().

This way, we don't need os_pidfile_error() function, since
it only prints error now, so put the error reporting printf
into the only place where qemu_create_pidfile() is called,
in vl.c.

While at it, fix wrong indentation in os_daemonize().

Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
  • Loading branch information
Michael Tokarev committed Nov 2, 2014
1 parent ccea25f commit fee78fd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 30 deletions.
1 change: 0 additions & 1 deletion include/qemu-common.h
Expand Up @@ -357,7 +357,6 @@ char *qemu_find_file(int type, const char *name);
void os_setup_early_signal_handling(void);
char *os_find_datadir(void);
void os_parse_cmd_args(int index, const char *optarg);
void os_pidfile_error(void);

/* Convert a byte between binary and BCD. */
static inline uint8_t to_bcd(uint8_t val)
Expand Down
31 changes: 8 additions & 23 deletions os-posix.c
Expand Up @@ -221,18 +221,14 @@ void os_daemonize(void)
do {
len = read(fds[0], &status, 1);
} while (len < 0 && errno == EINTR);
if (len != 1) {
exit(1);
}
else if (status == 1) {
fprintf(stderr, "Could not acquire pidfile\n");
exit(1);
} else {
exit(0);
}
} else if (pid < 0) {
exit(1);
}

/* only exit successfully if our child actually wrote
* a one-byte zero to our pipe, upon successful init */
exit(len == 1 && status == 0 ? 0 : 1);

} else if (pid < 0) {
exit(1);
}

close(fds[0]);
daemon_pipe = fds[1];
Expand Down Expand Up @@ -290,17 +286,6 @@ void os_setup_post(void)
}
}

void os_pidfile_error(void)
{
if (daemonize) {
uint8_t status = 1;
if (write(daemon_pipe, &status, 1) != 1) {
perror("daemonize. Writing to pipe\n");
}
} else
fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
}

void os_set_line_buffering(void)
{
setvbuf(stdout, NULL, _IOLBF, 0);
Expand Down
5 changes: 0 additions & 5 deletions os-win32.c
Expand Up @@ -104,11 +104,6 @@ void os_parse_cmd_args(int index, const char *optarg)
return;
}

void os_pidfile_error(void)
{
fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
}

int qemu_create_pidfile(const char *filename)
{
char buffer[128];
Expand Down
2 changes: 1 addition & 1 deletion vl.c
Expand Up @@ -3999,7 +3999,7 @@ int main(int argc, char **argv, char **envp)
#endif

if (pid_file && qemu_create_pidfile(pid_file) != 0) {
os_pidfile_error();
fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
exit(1);
}

Expand Down

0 comments on commit fee78fd

Please sign in to comment.