Skip to content

Commit fee78fd

Browse files
author
Michael Tokarev
committed
pidfile: stop making pidfile error a special case
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 <[email protected]>
1 parent ccea25f commit fee78fd

File tree

4 files changed

+9
-30
lines changed

4 files changed

+9
-30
lines changed

include/qemu-common.h

-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ char *qemu_find_file(int type, const char *name);
357357
void os_setup_early_signal_handling(void);
358358
char *os_find_datadir(void);
359359
void os_parse_cmd_args(int index, const char *optarg);
360-
void os_pidfile_error(void);
361360

362361
/* Convert a byte between binary and BCD. */
363362
static inline uint8_t to_bcd(uint8_t val)

os-posix.c

+8-23
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,14 @@ void os_daemonize(void)
221221
do {
222222
len = read(fds[0], &status, 1);
223223
} while (len < 0 && errno == EINTR);
224-
if (len != 1) {
225-
exit(1);
226-
}
227-
else if (status == 1) {
228-
fprintf(stderr, "Could not acquire pidfile\n");
229-
exit(1);
230-
} else {
231-
exit(0);
232-
}
233-
} else if (pid < 0) {
234-
exit(1);
235-
}
224+
225+
/* only exit successfully if our child actually wrote
226+
* a one-byte zero to our pipe, upon successful init */
227+
exit(len == 1 && status == 0 ? 0 : 1);
228+
229+
} else if (pid < 0) {
230+
exit(1);
231+
}
236232

237233
close(fds[0]);
238234
daemon_pipe = fds[1];
@@ -290,17 +286,6 @@ void os_setup_post(void)
290286
}
291287
}
292288

293-
void os_pidfile_error(void)
294-
{
295-
if (daemonize) {
296-
uint8_t status = 1;
297-
if (write(daemon_pipe, &status, 1) != 1) {
298-
perror("daemonize. Writing to pipe\n");
299-
}
300-
} else
301-
fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
302-
}
303-
304289
void os_set_line_buffering(void)
305290
{
306291
setvbuf(stdout, NULL, _IOLBF, 0);

os-win32.c

-5
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ void os_parse_cmd_args(int index, const char *optarg)
104104
return;
105105
}
106106

107-
void os_pidfile_error(void)
108-
{
109-
fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
110-
}
111-
112107
int qemu_create_pidfile(const char *filename)
113108
{
114109
char buffer[128];

vl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3999,7 +3999,7 @@ int main(int argc, char **argv, char **envp)
39993999
#endif
40004000

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

0 commit comments

Comments
 (0)