From 04fb15deb77fc91ca637ee9d5cf4542610149222 Mon Sep 17 00:00:00 2001 From: Nicolas Iooss Date: Sun, 3 Sep 2017 14:19:27 +0200 Subject: [PATCH] restorecond: check write() and daemon() results MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- restorecond/restorecond.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/restorecond/restorecond.c b/restorecond/restorecond.c index f379db1e..6fbbd35d 100644 --- a/restorecond/restorecond.c +++ b/restorecond/restorecond.c @@ -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();