[psplash,RFC,02/14] Fix 'unused-result' warnings

Message ID 20220425075954.10427-3-vasyl.vavrychuk@opensynergy.com
State New
Headers show
Series Implement DRM backend | expand

Commit Message

Vasyl Vavrychuk April 25, 2022, 7:59 a.m. UTC
This fixes warnings such as:

    ignoring return value of 'chdir', declared with attribute warn_unused_result [-Wunused-result]

Signed-off-by: Vasyl Vavrychuk <vasyl.vavrychuk@opensynergy.com>
---
 psplash-systemd.c | 36 +++++++++++++++++++++++++++++++++---
 psplash-write.c   | 23 ++++++++++++++++++-----
 psplash.c         |  5 ++++-
 3 files changed, 55 insertions(+), 9 deletions(-)

Patch

diff --git a/psplash-systemd.c b/psplash-systemd.c
index 840bd4e..dcf7e61 100644
--- a/psplash-systemd.c
+++ b/psplash-systemd.c
@@ -32,6 +32,7 @@  int get_progress(void)
 	int r;
 	char buffer[20];
 	int len;
+	ssize_t written;
 
         /* Connect to the system bus */
 	r = sd_bus_new(&bus);
@@ -71,11 +72,36 @@  int get_progress(void)
 		current_progress = progress;
 
 	len = snprintf(buffer, 20, "PROGRESS %d", (int)(current_progress * 100));
-	write(pipe_fd, buffer, len + 1);
+	written = write(pipe_fd, buffer, len + 1);
+	if (written == -1) {
+		/* EPIPE could mean that psplash detected boot complete sooner
+		then psplash-systemd and exited */
+		if (errno != EPIPE) {
+			perror("write");
+			r = -1;
+			goto finish;
+		}
+	} else if (written < len + 1) {
+		fprintf(stderr, "Wrote %zd bytes, less then expected %d bytes\n",
+			written, len + 1);
+		r = -1;
+		goto finish;
+	}
 
 	if (progress == 1.0) {
 		printf("Systemd reported progress of 1.0, quit psplash.\n");
-		write(pipe_fd, "QUIT", 5);
+		written = write(pipe_fd, "QUIT", 5);
+		if (written == -1) {
+			/* EPIPE could mean that psplash detected boot complete
+			sooner then psplash-systemd and exited */
+			if (errno != EPIPE) {
+				perror("write");
+				r = -1;
+				goto finish;
+			}
+		} else if (written < 5)
+			fprintf(stderr, "Wrote %zd bytes, less then expected 5 bytes\n",
+				written);
 		r = -1;
 	}
 
@@ -123,7 +149,11 @@  int main()
 	if (!rundir)
 		rundir = "/run";
 
-	chdir(rundir);
+	r = chdir(rundir);
+	if (r < 0) {
+		perror("chdir");
+		goto finish;
+	}
 
 	if ((pipe_fd = open (PSPLASH_FIFO,O_WRONLY|O_NONBLOCK)) == -1) {
 		fprintf(stderr, "Error unable to open fifo");
diff --git a/psplash-write.c b/psplash-write.c
index eee0ea3..16b87e1 100644
--- a/psplash-write.c
+++ b/psplash-write.c
@@ -21,8 +21,10 @@ 
 
 int main(int argc, char **argv)
 {
-  char *rundir;
-  int   pipe_fd;
+  char   *rundir;
+  int     pipe_fd;
+  size_t  count;
+  ssize_t written;
 
   rundir = getenv("PSPLASH_FIFO_DIR");
 
@@ -35,7 +37,10 @@  int main(int argc, char **argv)
       exit(-1);
     }
 
-  chdir(rundir);
+  if (chdir(rundir)) {
+    perror("chdir");
+    exit(-1);
+  }
 
   if ((pipe_fd = open (PSPLASH_FIFO,O_WRONLY|O_NONBLOCK)) == -1)
     {
@@ -45,8 +50,16 @@  int main(int argc, char **argv)
       exit (-1);
     }
 
-  write(pipe_fd, argv[1], strlen(argv[1])+1);
+  count = strlen(argv[1]) + 1;
+  written = write(pipe_fd, argv[1], count);
+  if (written == -1) {
+    perror("write");
+    exit(-1);
+  } else if ((size_t)written < count) {
+    fprintf(stderr, "Wrote %zd bytes, less then expected %zu bytes\n",
+      written, count);
+    exit(-1);
+  }
 
   return 0;
 }
-
diff --git a/psplash.c b/psplash.c
index 838ac13..62244ba 100644
--- a/psplash.c
+++ b/psplash.c
@@ -264,7 +264,10 @@  main (int argc, char** argv)
   if (!rundir)
     rundir = "/run";
 
-  chdir(rundir);
+  if (chdir(rundir)) {
+    perror("chdir");
+    exit(-1);
+  }
 
   if (mkfifo(PSPLASH_FIFO, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP))
     {