restorecond: check write() and daemon() results

When compiling restorecond with -Wunused, gcc 4.8.4 (from Ubuntu 14.04)
reports the following warnings:

    restorecond.c: In function ‘main’:
    restorecond.c:208:9: error: ignoring return value of ‘daemon’,
    declared with attribute warn_unused_result [-Werror=unused-result]
       daemon(0, 0);
             ^

    restorecond.c: In function ‘write_pid_file’:
    restorecond.c:106:2: error: ignoring return value of ‘write’,
    declared with attribute warn_unused_result [-Werror=unused-result]
      (void)write(pidfd, val, (unsigned int)len);
      ^

If any of these calls returns an error, it is currently silently
discarded. Add a message in order to warn about such an error.

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
This commit is contained in:
Nicolas Iooss 2017-09-03 14:19:27 +02:00 committed by James Carter
parent 13e5fa3b6b
commit 04fb15deb7
1 changed files with 8 additions and 3 deletions

View File

@ -103,7 +103,10 @@ static int write_pid_file(void)
pidfile = 0;
return 1;
}
(void)write(pidfd, val, (unsigned int)len);
if (write(pidfd, val, (unsigned int)len) != len) {
syslog(LOG_ERR, "Unable to write to pidfile (%s)", strerror(errno));
return 1;
}
close(pidfd);
return 0;
}
@ -204,8 +207,10 @@ int main(int argc, char **argv)
watch_file = server_watch_file;
read_config(master_fd, watch_file);
if (!debug_mode)
daemon(0, 0);
if (!debug_mode) {
if (daemon(0, 0) < 0)
exitApp("daemon");
}
write_pid_file();