diff mbox

[5/6] restorecond: check write() and daemon() results

Message ID 20170903121928.24024-5-nicolas.iooss@m4x.org (mailing list archive)
State Not Applicable
Headers show

Commit Message

Nicolas Iooss Sept. 3, 2017, 12:19 p.m. UTC
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>
---
 restorecond/restorecond.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/restorecond/restorecond.c b/restorecond/restorecond.c
index f379db1e7f8d..6fbbd35dc1b3 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();