Skip to contentSkip to conversation

libselinux/utils: print errno on failure

PATCH In review Christian Göttsche 2 participants 5 messages May 5, 2022 – May 16, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux/utils: print errno on failure

Print error description on failure after functions known to set errno.

Also mention the library function name in getenforce, policyvers and setenforce instead of the program name twice.

---
 libselinux/utils/compute_av.c                              | 3 ++-
 libselinux/utils/compute_create.c                          | 5 +++--
 libselinux/utils/compute_member.c                          | 5 +++--
 libselinux/utils/compute_relabel.c                         | 5 +++--
 libselinux/utils/getconlist.c                              | 2 +-
 libselinux/utils/getdefaultcon.c                           | 2 +-
 libselinux/utils/getenforce.c                              | 4 +++-
 libselinux/utils/getfilecon.c                              | 6 ++++--
 libselinux/utils/getpidcon.c                               | 4 +++-
 libselinux/utils/policyvers.c                              | 4 +++-
 libselinux/utils/selabel_digest.c                          | 2 +-
 libselinux/utils/selabel_get_digests_all_partial_matches.c | 3 ++-
 libselinux/utils/selabel_lookup.c                          | 3 ++-
 libselinux/utils/selabel_lookup_best_match.c               | 3 ++-
 libselinux/utils/selabel_partial_match.c                   | 3 ++-
 libselinux/utils/setenforce.c                              | 3 ++-
 libselinux/utils/setfilecon.c                              | 6 ++++--
 17 files changed, 41 insertions(+), 22 deletions(-)
libselinux/utils/compute_av.c +2 -1
index ef08338f..cca407d6 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,7 +36,7 @@ int main(int argc, char **argv)
 
 	ret = security_compute_av(argv[1], argv[2], tclass, 1, &avd);
 	if (ret < 0) {
-		fprintf(stderr, "%s:  security_compute_av failed\n", argv[0]);
+		fprintf(stderr, "%s:  security_compute_av failed:  %s\n", argv[0], strerror(errno));
 		exit(3);
 	}
 
libselinux/utils/compute_create.c +3 -2
index 63029c19..c6481f4b 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
 	ret = security_compute_create(argv[1], argv[2], tclass, &buf);
 	if (ret < 0) {
-		fprintf(stderr, "%s:  security_compute_create failed\n",
-			argv[0]);
+		fprintf(stderr, "%s:  security_compute_create failed:  %s\n",
+			argv[0], strerror(errno));
 		exit(3);
 	}
 
libselinux/utils/compute_member.c +3 -2
index 1ef47c25..9fe790ee 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
 	ret = security_compute_member(argv[1], argv[2], tclass, &buf);
 	if (ret < 0) {
-		fprintf(stderr, "%s:  security_compute_member failed\n",
-			argv[0]);
+		fprintf(stderr, "%s:  security_compute_member failed:  %s\n",
+			argv[0], strerror(errno));
 		exit(3);
 	}
 
libselinux/utils/compute_relabel.c +3 -2
index f6a957da..bdd39d0f 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
 	ret = security_compute_relabel(argv[1], argv[2], tclass, &buf);
 	if (ret < 0) {
-		fprintf(stderr, "%s:  security_compute_relabel failed\n",
-			argv[0]);
+		fprintf(stderr, "%s:  security_compute_relabel failed:  %s\n",
+			argv[0], strerror(errno));
 		exit(3);
 	}
 
libselinux/utils/getconlist.c +1 -1
index 0bb28469..92f6a793 100644
@@ -55,7 +55,7 @@ int main(int argc, char **argv)
 	/* If a context wasn't passed, use the current context. */
 	if (((argc - optind) < 2)) {
 		if (getcon(&cur_context) < 0) {
-			fprintf(stderr, "Couldn't get current context.\n");
+			fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
 			free(level);
 			return 2;
 		}
libselinux/utils/getdefaultcon.c +1 -1
index 590e98d9..93102e5e 100644
@@ -62,7 +62,7 @@ int main(int argc, char **argv)
 	/* If a context wasn't passed, use the current context. */
 	if (((argc - optind) < 2)) {
 		if (getcon(&cur_context) < 0) {
-			fprintf(stderr, "Couldn't get current context.\n");
+			fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
 			return 2;
 		}
 	} else
libselinux/utils/getenforce.c +3 -1
index e5d19c54..aeeb79a8 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc __attribute__ ((unused)),
@@ -16,7 +18,7 @@ int main(int argc __attribute__ ((unused)),
 	if (rc == 1) {
 		rc = security_getenforce();
 		if (rc < 0) {
-			fputs("getenforce:  getenforce() failed", stderr);
+			fprintf(stderr, "getenforce:  security_getenforce() failed:  %s\n", strerror(errno));
 			return 2;
 		}
 
libselinux/utils/getfilecon.c +4 -2
index 6266ae16..b823a1a1 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -16,8 +18,8 @@ int main(int argc, char **argv)
 	for (i = 1; i < argc; i++) {
 		rc = getfilecon(argv[i], &buf);
 		if (rc < 0) {
-			fprintf(stderr, "%s:  getfilecon(%s) failed\n", argv[0],
-				argv[i]);
+			fprintf(stderr, "%s:  getfilecon(%s) failed:  %s\n", argv[0],
+				argv[i], strerror(errno));
 			exit(2);
 		}
 		printf("%s\t%s\n", argv[i], buf);
libselinux/utils/getpidcon.c +3 -1
index ea6c274e..1a88fa6d 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -21,7 +23,7 @@ int main(int argc, char **argv)
 
 	rc = getpidcon(pid, &buf);
 	if (rc < 0) {
-		fprintf(stderr, "%s:  getpidcon() failed\n", argv[0]);
+		fprintf(stderr, "%s:  getpidcon() failed:  %s\n", argv[0], strerror(errno));
 		exit(3);
 	}
 
libselinux/utils/policyvers.c +3 -1
index dd56f2c7..5230bcaf 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc __attribute__ ((unused)), char **argv)
@@ -9,7 +11,7 @@ int main(int argc __attribute__ ((unused)), char **argv)
 
 	rc = security_policyvers();
 	if (rc < 0) {
-		fprintf(stderr, "%s:  policyvers() failed\n", argv[0]);
+		fprintf(stderr, "%s:  security_policyvers() failed:  %s\n", argv[0], strerror(errno));
 		exit(2);
 	}
 
libselinux/utils/selabel_digest.c +1 -1
index 49408a0b..6a8313a2 100644
@@ -34,7 +34,7 @@ static int run_check_digest(char *cmd, char *selabel_digest)
 
 	fp = popen(cmd, "r");
 	if (!fp) {
-		printf("Failed to run command line\n");
+		fprintf(stderr, "Failed to run command '%s':  %s\n", cmd, strerror(errno));
 		return -1;
 	}
 
libselinux/utils/selabel_get_digests_all_partial_matches.c +2 -1
index e28833d2..c4e0f836 100644
@@ -77,7 +77,8 @@ int main(int argc, char **argv)
 	hnd = selabel_open(SELABEL_CTX_FILE, selabel_option, 2);
 	if (!hnd) {
 		fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-							     "handle.\n");
+							     "handle:  %s\n",
+							     strerror(errno));
 		return -1;
 	}
 
libselinux/utils/selabel_lookup.c +2 -1
index 1aef64de..112ffda1 100644
@@ -91,7 +91,8 @@ int main(int argc, char **argv)
 	hnd = selabel_open(backend, selabel_option, 2);
 	if (!hnd) {
 		fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-							     "handle.\n");
+							     "handle:  %s\n",
+							     strerror(errno));
 		return -1;
 	}
 
libselinux/utils/selabel_lookup_best_match.c +2 -1
index 2cddc6cd..a4af0679 100644
@@ -117,7 +117,8 @@ int main(int argc, char **argv)
 	hnd = selabel_open(SELABEL_CTX_FILE, options, 2);
 	if (!hnd) {
 		fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-							     "handle.\n");
+							     "handle:  %s\n",
+							     strerror(errno));
 		rc = -1;
 		goto out;
 	}
libselinux/utils/selabel_partial_match.c +2 -1
index c5932cb1..7bbd5777 100644
@@ -61,7 +61,8 @@ int main(int argc, char **argv)
 	hnd = selabel_open(SELABEL_CTX_FILE, selabel_option, 2);
 	if (!hnd) {
 		fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-							     "handle.\n");
+							     "handle:  %s\n",
+							     strerror(errno));
 		return -1;
 	}
 
libselinux/utils/setenforce.c +2 -1
index 60a20a4b..67c13dca 100644
@@ -4,6 +4,7 @@
 #include <ctype.h>
 #include <string.h>
 #include <strings.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 static __attribute__ ((__noreturn__)) void usage(const char *progname)
@@ -35,7 +36,7 @@ int main(int argc, char **argv)
 			usage(argv[0]);
 	}
 	if (rc < 0) {
-		fprintf(stderr, "%s:  setenforce() failed\n", argv[0]);
+		fprintf(stderr, "%s:  security_setenforce() failed:  %s\n", argv[0], strerror(errno));
 		return 2;
 	}
 	return 0;
libselinux/utils/setfilecon.c +4 -2
index 79af55de..a3fbc3ed 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -15,8 +17,8 @@ int main(int argc, char **argv)
 	for (i = 2; i < argc; i++) {
 		rc = setfilecon(argv[i], argv[1]);
 		if (rc < 0) {
-			fprintf(stderr, "%s:  setfilecon(%s,%s) failed\n",
-				argv[0], argv[i], argv[1]);
+			fprintf(stderr, "%s:  setfilecon(%s,%s) failed:  %s\n",
+				argv[0], argv[i], argv[1], strerror(errno));
 			exit(2);
 		}
 	}
Signature · 1 line
2.36.0
#1 lore raw
Quoted 40 lines · Christian Göttsche

Print error description on failure after functions known to set errno.

Also mention the library function name in getenforce, policyvers and setenforce instead of the program name twice.

---
 libselinux/utils/compute_av.c                              | 3 ++-
 libselinux/utils/compute_create.c                          | 5 +++--
 libselinux/utils/compute_member.c                          | 5 +++--
 libselinux/utils/compute_relabel.c                         | 5 +++--
 libselinux/utils/getconlist.c                              | 2 +-
 libselinux/utils/getdefaultcon.c                           | 2 +-
 libselinux/utils/getenforce.c                              | 4 +++-
 libselinux/utils/getfilecon.c                              | 6 ++++--
 libselinux/utils/getpidcon.c                               | 4 +++-
 libselinux/utils/policyvers.c                              | 4 +++-
 libselinux/utils/selabel_digest.c                          | 2 +-
 libselinux/utils/selabel_get_digests_all_partial_matches.c | 3 ++-
 libselinux/utils/selabel_lookup.c                          | 3 ++-
 libselinux/utils/selabel_lookup_best_match.c               | 3 ++-
 libselinux/utils/selabel_partial_match.c                   | 3 ++-
 libselinux/utils/setenforce.c                              | 3 ++-
 libselinux/utils/setfilecon.c                              | 6 ++++--
 17 files changed, 41 insertions(+), 22 deletions(-)
libselinux/utils/compute_av.c +1 -0
index ef08338f..cca407d6 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 

libselinux/utils/compute_av.c int main(int argc, char **argv) · line 42

Quoted 8 lines
libselinux/utils/compute_av.c +1 -1
@@ -35,7 +36,7 @@ int main(int argc, char **argv)
 
        ret = security_compute_av(argv[1], argv[2], tclass, 1, &avd);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_av failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_compute_av failed:  %s\n", argv[0], strerror(errno));
                exit(3);
        }
 

Back in August you converted a bunch of strerror() uses in libselinux to instead use "%m" for thread safety. Since all of these are for utility programs and not the main library, thread safety is not a concern, but is there any reason not to just use "%m"?

Jim

Quoted 302 lines
libselinux/utils/compute_create.c +3 -2
index 63029c19..c6481f4b 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_create(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_create failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_create failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/compute_member.c +3 -2
index 1ef47c25..9fe790ee 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_member(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_member failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_member failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/compute_relabel.c +3 -2
index f6a957da..bdd39d0f 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_relabel(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_relabel failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_relabel failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/getconlist.c +1 -1
index 0bb28469..92f6a793 100644
@@ -55,7 +55,7 @@ int main(int argc, char **argv)
        /* If a context wasn't passed, use the current context. */
        if (((argc - optind) < 2)) {
                if (getcon(&cur_context) < 0) {
-                       fprintf(stderr, "Couldn't get current context.\n");
+                       fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
                        free(level);
                        return 2;
                }
libselinux/utils/getdefaultcon.c +1 -1
index 590e98d9..93102e5e 100644
@@ -62,7 +62,7 @@ int main(int argc, char **argv)
        /* If a context wasn't passed, use the current context. */
        if (((argc - optind) < 2)) {
                if (getcon(&cur_context) < 0) {
-                       fprintf(stderr, "Couldn't get current context.\n");
+                       fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
                        return 2;
                }
        } else
libselinux/utils/getenforce.c +3 -1
index e5d19c54..aeeb79a8 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc __attribute__ ((unused)),
@@ -16,7 +18,7 @@ int main(int argc __attribute__ ((unused)),
        if (rc == 1) {
                rc = security_getenforce();
                if (rc < 0) {
-                       fputs("getenforce:  getenforce() failed", stderr);
+                       fprintf(stderr, "getenforce:  security_getenforce() failed:  %s\n", strerror(errno));
                        return 2;
                }
 
libselinux/utils/getfilecon.c +4 -2
index 6266ae16..b823a1a1 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -16,8 +18,8 @@ int main(int argc, char **argv)
        for (i = 1; i < argc; i++) {
                rc = getfilecon(argv[i], &buf);
                if (rc < 0) {
-                       fprintf(stderr, "%s:  getfilecon(%s) failed\n", argv[0],
-                               argv[i]);
+                       fprintf(stderr, "%s:  getfilecon(%s) failed:  %s\n", argv[0],
+                               argv[i], strerror(errno));
                        exit(2);
                }
                printf("%s\t%s\n", argv[i], buf);
libselinux/utils/getpidcon.c +3 -1
index ea6c274e..1a88fa6d 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -21,7 +23,7 @@ int main(int argc, char **argv)
 
        rc = getpidcon(pid, &buf);
        if (rc < 0) {
-               fprintf(stderr, "%s:  getpidcon() failed\n", argv[0]);
+               fprintf(stderr, "%s:  getpidcon() failed:  %s\n", argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/policyvers.c +3 -1
index dd56f2c7..5230bcaf 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc __attribute__ ((unused)), char **argv)
@@ -9,7 +11,7 @@ int main(int argc __attribute__ ((unused)), char **argv)
 
        rc = security_policyvers();
        if (rc < 0) {
-               fprintf(stderr, "%s:  policyvers() failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_policyvers() failed:  %s\n", argv[0], strerror(errno));
                exit(2);
        }
 
libselinux/utils/selabel_digest.c +1 -1
index 49408a0b..6a8313a2 100644
@@ -34,7 +34,7 @@ static int run_check_digest(char *cmd, char *selabel_digest)
 
        fp = popen(cmd, "r");
        if (!fp) {
-               printf("Failed to run command line\n");
+               fprintf(stderr, "Failed to run command '%s':  %s\n", cmd, strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_get_digests_all_partial_matches.c +2 -1
index e28833d2..c4e0f836 100644
@@ -77,7 +77,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_lookup.c +2 -1
index 1aef64de..112ffda1 100644
@@ -91,7 +91,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(backend, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_lookup_best_match.c +2 -1
index 2cddc6cd..a4af0679 100644
@@ -117,7 +117,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, options, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                rc = -1;
                goto out;
        }
libselinux/utils/selabel_partial_match.c +2 -1
index c5932cb1..7bbd5777 100644
@@ -61,7 +61,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/setenforce.c +2 -1
index 60a20a4b..67c13dca 100644
@@ -4,6 +4,7 @@
 #include <ctype.h>
 #include <string.h>
 #include <strings.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 static __attribute__ ((__noreturn__)) void usage(const char *progname)
@@ -35,7 +36,7 @@ int main(int argc, char **argv)
                        usage(argv[0]);
        }
        if (rc < 0) {
-               fprintf(stderr, "%s:  setenforce() failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_setenforce() failed:  %s\n", argv[0], strerror(errno));
                return 2;
        }
        return 0;
libselinux/utils/setfilecon.c +4 -3
index 79af55de..a3fbc3ed 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -15,8 +17,8 @@ int main(int argc, char **argv)
        for (i = 2; i < argc; i++) {
                rc = setfilecon(argv[i], argv[1]);
                if (rc < 0) {
-                       fprintf(stderr, "%s:  setfilecon(%s,%s) failed\n",
-                               argv[0], argv[i], argv[1]);
+                       fprintf(stderr, "%s:  setfilecon(%s,%s) failed:  %s\n",
+                               argv[0], argv[i], argv[1], strerror(errno));
                        exit(2);
                }
        }
--

2.36.0

#2 lore raw
Quoted 44 lines · James Carter
Quoted 41 lines · Christian Göttsche

Print error description on failure after functions known to set errno.

Also mention the library function name in getenforce, policyvers and setenforce instead of the program name twice.

---
 libselinux/utils/compute_av.c                              | 3 ++-
 libselinux/utils/compute_create.c                          | 5 +++--
 libselinux/utils/compute_member.c                          | 5 +++--
 libselinux/utils/compute_relabel.c                         | 5 +++--
 libselinux/utils/getconlist.c                              | 2 +-
 libselinux/utils/getdefaultcon.c                           | 2 +-
 libselinux/utils/getenforce.c                              | 4 +++-
 libselinux/utils/getfilecon.c                              | 6 ++++--
 libselinux/utils/getpidcon.c                               | 4 +++-
 libselinux/utils/policyvers.c                              | 4 +++-
 libselinux/utils/selabel_digest.c                          | 2 +-
 libselinux/utils/selabel_get_digests_all_partial_matches.c | 3 ++-
 libselinux/utils/selabel_lookup.c                          | 3 ++-
 libselinux/utils/selabel_lookup_best_match.c               | 3 ++-
 libselinux/utils/selabel_partial_match.c                   | 3 ++-
 libselinux/utils/setenforce.c                              | 3 ++-
 libselinux/utils/setfilecon.c                              | 6 ++++--
 17 files changed, 41 insertions(+), 22 deletions(-)
libselinux/utils/compute_av.c +1 -0
index ef08338f..cca407d6 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,7 +36,7 @@ int main(int argc, char **argv)
 
Quoted 12 lines

libselinux/utils/compute_av.c int main(int argc, char **argv) · line 42

Quoted 7 lines
libselinux/utils/compute_av.c +1 -1
        ret = security_compute_av(argv[1], argv[2], tclass, 1, &avd);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_av failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_compute_av failed:  %s\n", argv[0], strerror(errno));
                exit(3);
        }
 

Back in August you converted a bunch of strerror() uses in libselinux to instead use "%m" for thread safety. Since all of these are for utility programs and not the main library, thread safety is not a concern, but is there any reason not to just use "%m"?

None to my knowledge, other than matching the surrounding code.

Quoted 305 lines

Jim

Quoted 302 lines
libselinux/utils/compute_create.c +3 -2
index 63029c19..c6481f4b 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_create(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_create failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_create failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/compute_member.c +3 -2
index 1ef47c25..9fe790ee 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_member(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_member failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_member failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/compute_relabel.c +3 -2
index f6a957da..bdd39d0f 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_relabel(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_relabel failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_relabel failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/getconlist.c +1 -1
index 0bb28469..92f6a793 100644
@@ -55,7 +55,7 @@ int main(int argc, char **argv)
        /* If a context wasn't passed, use the current context. */
        if (((argc - optind) < 2)) {
                if (getcon(&cur_context) < 0) {
-                       fprintf(stderr, "Couldn't get current context.\n");
+                       fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
                        free(level);
                        return 2;
                }
libselinux/utils/getdefaultcon.c +1 -1
index 590e98d9..93102e5e 100644
@@ -62,7 +62,7 @@ int main(int argc, char **argv)
        /* If a context wasn't passed, use the current context. */
        if (((argc - optind) < 2)) {
                if (getcon(&cur_context) < 0) {
-                       fprintf(stderr, "Couldn't get current context.\n");
+                       fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
                        return 2;
                }
        } else
libselinux/utils/getenforce.c +3 -1
index e5d19c54..aeeb79a8 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc __attribute__ ((unused)),
@@ -16,7 +18,7 @@ int main(int argc __attribute__ ((unused)),
        if (rc == 1) {
                rc = security_getenforce();
                if (rc < 0) {
-                       fputs("getenforce:  getenforce() failed", stderr);
+                       fprintf(stderr, "getenforce:  security_getenforce() failed:  %s\n", strerror(errno));
                        return 2;
                }
 
libselinux/utils/getfilecon.c +4 -2
index 6266ae16..b823a1a1 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -16,8 +18,8 @@ int main(int argc, char **argv)
        for (i = 1; i < argc; i++) {
                rc = getfilecon(argv[i], &buf);
                if (rc < 0) {
-                       fprintf(stderr, "%s:  getfilecon(%s) failed\n", argv[0],
-                               argv[i]);
+                       fprintf(stderr, "%s:  getfilecon(%s) failed:  %s\n", argv[0],
+                               argv[i], strerror(errno));
                        exit(2);
                }
                printf("%s\t%s\n", argv[i], buf);
libselinux/utils/getpidcon.c +3 -1
index ea6c274e..1a88fa6d 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -21,7 +23,7 @@ int main(int argc, char **argv)
 
        rc = getpidcon(pid, &buf);
        if (rc < 0) {
-               fprintf(stderr, "%s:  getpidcon() failed\n", argv[0]);
+               fprintf(stderr, "%s:  getpidcon() failed:  %s\n", argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/policyvers.c +3 -1
index dd56f2c7..5230bcaf 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc __attribute__ ((unused)), char **argv)
@@ -9,7 +11,7 @@ int main(int argc __attribute__ ((unused)), char **argv)
 
        rc = security_policyvers();
        if (rc < 0) {
-               fprintf(stderr, "%s:  policyvers() failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_policyvers() failed:  %s\n", argv[0], strerror(errno));
                exit(2);
        }
 
libselinux/utils/selabel_digest.c +1 -1
index 49408a0b..6a8313a2 100644
@@ -34,7 +34,7 @@ static int run_check_digest(char *cmd, char *selabel_digest)
 
        fp = popen(cmd, "r");
        if (!fp) {
-               printf("Failed to run command line\n");
+               fprintf(stderr, "Failed to run command '%s':  %s\n", cmd, strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_get_digests_all_partial_matches.c +2 -1
index e28833d2..c4e0f836 100644
@@ -77,7 +77,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_lookup.c +2 -1
index 1aef64de..112ffda1 100644
@@ -91,7 +91,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(backend, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_lookup_best_match.c +2 -1
index 2cddc6cd..a4af0679 100644
@@ -117,7 +117,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, options, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                rc = -1;
                goto out;
        }
libselinux/utils/selabel_partial_match.c +2 -1
index c5932cb1..7bbd5777 100644
@@ -61,7 +61,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/setenforce.c +2 -1
index 60a20a4b..67c13dca 100644
@@ -4,6 +4,7 @@
 #include <ctype.h>
 #include <string.h>
 #include <strings.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 static __attribute__ ((__noreturn__)) void usage(const char *progname)
@@ -35,7 +36,7 @@ int main(int argc, char **argv)
                        usage(argv[0]);
        }
        if (rc < 0) {
-               fprintf(stderr, "%s:  setenforce() failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_setenforce() failed:  %s\n", argv[0], strerror(errno));
                return 2;
        }
        return 0;
libselinux/utils/setfilecon.c +4 -3
index 79af55de..a3fbc3ed 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -15,8 +17,8 @@ int main(int argc, char **argv)
        for (i = 2; i < argc; i++) {
                rc = setfilecon(argv[i], argv[1]);
                if (rc < 0) {
-                       fprintf(stderr, "%s:  setfilecon(%s,%s) failed\n",
-                               argv[0], argv[i], argv[1]);
+                       fprintf(stderr, "%s:  setfilecon(%s,%s) failed:  %s\n",
+                               argv[0], argv[i], argv[1], strerror(errno));
                        exit(2);
                }
        }
--

2.36.0

Acked-by on v1 1/1
#3 lore raw
In reply to Christian Göttsche
Quoted 7 lines · Christian Göttsche

Print error description on failure after functions known to set errno.

Also mention the library function name in getenforce, policyvers and setenforce instead of the program name twice.

Quoted 343 lines
---
 libselinux/utils/compute_av.c                              | 3 ++-
 libselinux/utils/compute_create.c                          | 5 +++--
 libselinux/utils/compute_member.c                          | 5 +++--
 libselinux/utils/compute_relabel.c                         | 5 +++--
 libselinux/utils/getconlist.c                              | 2 +-
 libselinux/utils/getdefaultcon.c                           | 2 +-
 libselinux/utils/getenforce.c                              | 4 +++-
 libselinux/utils/getfilecon.c                              | 6 ++++--
 libselinux/utils/getpidcon.c                               | 4 +++-
 libselinux/utils/policyvers.c                              | 4 +++-
 libselinux/utils/selabel_digest.c                          | 2 +-
 libselinux/utils/selabel_get_digests_all_partial_matches.c | 3 ++-
 libselinux/utils/selabel_lookup.c                          | 3 ++-
 libselinux/utils/selabel_lookup_best_match.c               | 3 ++-
 libselinux/utils/selabel_partial_match.c                   | 3 ++-
 libselinux/utils/setenforce.c                              | 3 ++-
 libselinux/utils/setfilecon.c                              | 6 ++++--
 17 files changed, 41 insertions(+), 22 deletions(-)
libselinux/utils/compute_av.c +2 -1
index ef08338f..cca407d6 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,7 +36,7 @@ int main(int argc, char **argv)
 
        ret = security_compute_av(argv[1], argv[2], tclass, 1, &avd);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_av failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_compute_av failed:  %s\n", argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/compute_create.c +3 -2
index 63029c19..c6481f4b 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_create(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_create failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_create failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/compute_member.c +3 -2
index 1ef47c25..9fe790ee 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_member(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_member failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_member failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/compute_relabel.c +3 -2
index f6a957da..bdd39d0f 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_relabel(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_relabel failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_relabel failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/getconlist.c +1 -1
index 0bb28469..92f6a793 100644
@@ -55,7 +55,7 @@ int main(int argc, char **argv)
        /* If a context wasn't passed, use the current context. */
        if (((argc - optind) < 2)) {
                if (getcon(&cur_context) < 0) {
-                       fprintf(stderr, "Couldn't get current context.\n");
+                       fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
                        free(level);
                        return 2;
                }
libselinux/utils/getdefaultcon.c +1 -1
index 590e98d9..93102e5e 100644
@@ -62,7 +62,7 @@ int main(int argc, char **argv)
        /* If a context wasn't passed, use the current context. */
        if (((argc - optind) < 2)) {
                if (getcon(&cur_context) < 0) {
-                       fprintf(stderr, "Couldn't get current context.\n");
+                       fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
                        return 2;
                }
        } else
libselinux/utils/getenforce.c +3 -1
index e5d19c54..aeeb79a8 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc __attribute__ ((unused)),
@@ -16,7 +18,7 @@ int main(int argc __attribute__ ((unused)),
        if (rc == 1) {
                rc = security_getenforce();
                if (rc < 0) {
-                       fputs("getenforce:  getenforce() failed", stderr);
+                       fprintf(stderr, "getenforce:  security_getenforce() failed:  %s\n", strerror(errno));
                        return 2;
                }
 
libselinux/utils/getfilecon.c +4 -2
index 6266ae16..b823a1a1 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -16,8 +18,8 @@ int main(int argc, char **argv)
        for (i = 1; i < argc; i++) {
                rc = getfilecon(argv[i], &buf);
                if (rc < 0) {
-                       fprintf(stderr, "%s:  getfilecon(%s) failed\n", argv[0],
-                               argv[i]);
+                       fprintf(stderr, "%s:  getfilecon(%s) failed:  %s\n", argv[0],
+                               argv[i], strerror(errno));
                        exit(2);
                }
                printf("%s\t%s\n", argv[i], buf);
libselinux/utils/getpidcon.c +3 -1
index ea6c274e..1a88fa6d 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -21,7 +23,7 @@ int main(int argc, char **argv)
 
        rc = getpidcon(pid, &buf);
        if (rc < 0) {
-               fprintf(stderr, "%s:  getpidcon() failed\n", argv[0]);
+               fprintf(stderr, "%s:  getpidcon() failed:  %s\n", argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/policyvers.c +3 -1
index dd56f2c7..5230bcaf 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc __attribute__ ((unused)), char **argv)
@@ -9,7 +11,7 @@ int main(int argc __attribute__ ((unused)), char **argv)
 
        rc = security_policyvers();
        if (rc < 0) {
-               fprintf(stderr, "%s:  policyvers() failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_policyvers() failed:  %s\n", argv[0], strerror(errno));
                exit(2);
        }
 
libselinux/utils/selabel_digest.c +1 -1
index 49408a0b..6a8313a2 100644
@@ -34,7 +34,7 @@ static int run_check_digest(char *cmd, char *selabel_digest)
 
        fp = popen(cmd, "r");
        if (!fp) {
-               printf("Failed to run command line\n");
+               fprintf(stderr, "Failed to run command '%s':  %s\n", cmd, strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_get_digests_all_partial_matches.c +2 -1
index e28833d2..c4e0f836 100644
@@ -77,7 +77,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_lookup.c +2 -1
index 1aef64de..112ffda1 100644
@@ -91,7 +91,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(backend, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_lookup_best_match.c +2 -1
index 2cddc6cd..a4af0679 100644
@@ -117,7 +117,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, options, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                rc = -1;
                goto out;
        }
libselinux/utils/selabel_partial_match.c +2 -1
index c5932cb1..7bbd5777 100644
@@ -61,7 +61,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/setenforce.c +2 -1
index 60a20a4b..67c13dca 100644
@@ -4,6 +4,7 @@
 #include <ctype.h>
 #include <string.h>
 #include <strings.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 static __attribute__ ((__noreturn__)) void usage(const char *progname)
@@ -35,7 +36,7 @@ int main(int argc, char **argv)
                        usage(argv[0]);
        }
        if (rc < 0) {
-               fprintf(stderr, "%s:  setenforce() failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_setenforce() failed:  %s\n", argv[0], strerror(errno));
                return 2;
        }
        return 0;
libselinux/utils/setfilecon.c +4 -3
index 79af55de..a3fbc3ed 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -15,8 +17,8 @@ int main(int argc, char **argv)
        for (i = 2; i < argc; i++) {
                rc = setfilecon(argv[i], argv[1]);
                if (rc < 0) {
-                       fprintf(stderr, "%s:  setfilecon(%s,%s) failed\n",
-                               argv[0], argv[i], argv[1]);
+                       fprintf(stderr, "%s:  setfilecon(%s,%s) failed:  %s\n",
+                               argv[0], argv[i], argv[1], strerror(errno));
                        exit(2);
                }
        }
--

2.36.0

#4 lore raw
Quoted 4 lines · James Carter
Quoted 1 line · Christian Göttsche
Quoted 9 lines
Quoted 6 lines

Print error description on failure after functions known to set errno.

Also mention the library function name in getenforce, policyvers and setenforce instead of the program name twice.

Merged.
Thanks,
Jim

Quoted 343 lines
Quoted 343 lines
---
 libselinux/utils/compute_av.c                              | 3 ++-
 libselinux/utils/compute_create.c                          | 5 +++--
 libselinux/utils/compute_member.c                          | 5 +++--
 libselinux/utils/compute_relabel.c                         | 5 +++--
 libselinux/utils/getconlist.c                              | 2 +-
 libselinux/utils/getdefaultcon.c                           | 2 +-
 libselinux/utils/getenforce.c                              | 4 +++-
 libselinux/utils/getfilecon.c                              | 6 ++++--
 libselinux/utils/getpidcon.c                               | 4 +++-
 libselinux/utils/policyvers.c                              | 4 +++-
 libselinux/utils/selabel_digest.c                          | 2 +-
 libselinux/utils/selabel_get_digests_all_partial_matches.c | 3 ++-
 libselinux/utils/selabel_lookup.c                          | 3 ++-
 libselinux/utils/selabel_lookup_best_match.c               | 3 ++-
 libselinux/utils/selabel_partial_match.c                   | 3 ++-
 libselinux/utils/setenforce.c                              | 3 ++-
 libselinux/utils/setfilecon.c                              | 6 ++++--
 17 files changed, 41 insertions(+), 22 deletions(-)
libselinux/utils/compute_av.c +2 -1
index ef08338f..cca407d6 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,7 +36,7 @@ int main(int argc, char **argv)
 
        ret = security_compute_av(argv[1], argv[2], tclass, 1, &avd);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_av failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_compute_av failed:  %s\n", argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/compute_create.c +3 -2
index 63029c19..c6481f4b 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_create(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_create failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_create failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/compute_member.c +3 -2
index 1ef47c25..9fe790ee 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_member(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_member failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_member failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/compute_relabel.c +3 -2
index f6a957da..bdd39d0f 100644
@@ -2,6 +2,7 @@
 #include <sys/types.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 #include <selinux/selinux.h>
 
@@ -35,8 +36,8 @@ int main(int argc, char **argv)
 
        ret = security_compute_relabel(argv[1], argv[2], tclass, &buf);
        if (ret < 0) {
-               fprintf(stderr, "%s:  security_compute_relabel failed\n",
-                       argv[0]);
+               fprintf(stderr, "%s:  security_compute_relabel failed:  %s\n",
+                       argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/getconlist.c +1 -1
index 0bb28469..92f6a793 100644
@@ -55,7 +55,7 @@ int main(int argc, char **argv)
        /* If a context wasn't passed, use the current context. */
        if (((argc - optind) < 2)) {
                if (getcon(&cur_context) < 0) {
-                       fprintf(stderr, "Couldn't get current context.\n");
+                       fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
                        free(level);
                        return 2;
                }
libselinux/utils/getdefaultcon.c +1 -1
index 590e98d9..93102e5e 100644
@@ -62,7 +62,7 @@ int main(int argc, char **argv)
        /* If a context wasn't passed, use the current context. */
        if (((argc - optind) < 2)) {
                if (getcon(&cur_context) < 0) {
-                       fprintf(stderr, "Couldn't get current context.\n");
+                       fprintf(stderr, "Couldn't get current context:  %s\n", strerror(errno));
                        return 2;
                }
        } else
libselinux/utils/getenforce.c +3 -1
index e5d19c54..aeeb79a8 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc __attribute__ ((unused)),
@@ -16,7 +18,7 @@ int main(int argc __attribute__ ((unused)),
        if (rc == 1) {
                rc = security_getenforce();
                if (rc < 0) {
-                       fputs("getenforce:  getenforce() failed", stderr);
+                       fprintf(stderr, "getenforce:  security_getenforce() failed:  %s\n", strerror(errno));
                        return 2;
                }
 
libselinux/utils/getfilecon.c +4 -2
index 6266ae16..b823a1a1 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -16,8 +18,8 @@ int main(int argc, char **argv)
        for (i = 1; i < argc; i++) {
                rc = getfilecon(argv[i], &buf);
                if (rc < 0) {
-                       fprintf(stderr, "%s:  getfilecon(%s) failed\n", argv[0],
-                               argv[i]);
+                       fprintf(stderr, "%s:  getfilecon(%s) failed:  %s\n", argv[0],
+                               argv[i], strerror(errno));
                        exit(2);
                }
                printf("%s\t%s\n", argv[i], buf);
libselinux/utils/getpidcon.c +3 -1
index ea6c274e..1a88fa6d 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -21,7 +23,7 @@ int main(int argc, char **argv)
 
        rc = getpidcon(pid, &buf);
        if (rc < 0) {
-               fprintf(stderr, "%s:  getpidcon() failed\n", argv[0]);
+               fprintf(stderr, "%s:  getpidcon() failed:  %s\n", argv[0], strerror(errno));
                exit(3);
        }
 
libselinux/utils/policyvers.c +3 -1
index dd56f2c7..5230bcaf 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc __attribute__ ((unused)), char **argv)
@@ -9,7 +11,7 @@ int main(int argc __attribute__ ((unused)), char **argv)
 
        rc = security_policyvers();
        if (rc < 0) {
-               fprintf(stderr, "%s:  policyvers() failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_policyvers() failed:  %s\n", argv[0], strerror(errno));
                exit(2);
        }
 
libselinux/utils/selabel_digest.c +1 -1
index 49408a0b..6a8313a2 100644
@@ -34,7 +34,7 @@ static int run_check_digest(char *cmd, char *selabel_digest)
 
        fp = popen(cmd, "r");
        if (!fp) {
-               printf("Failed to run command line\n");
+               fprintf(stderr, "Failed to run command '%s':  %s\n", cmd, strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_get_digests_all_partial_matches.c +2 -1
index e28833d2..c4e0f836 100644
@@ -77,7 +77,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_lookup.c +2 -1
index 1aef64de..112ffda1 100644
@@ -91,7 +91,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(backend, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/selabel_lookup_best_match.c +2 -1
index 2cddc6cd..a4af0679 100644
@@ -117,7 +117,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, options, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                rc = -1;
                goto out;
        }
libselinux/utils/selabel_partial_match.c +2 -1
index c5932cb1..7bbd5777 100644
@@ -61,7 +61,8 @@ int main(int argc, char **argv)
        hnd = selabel_open(SELABEL_CTX_FILE, selabel_option, 2);
        if (!hnd) {
                fprintf(stderr, "ERROR: selabel_open - Could not obtain "
-                                                            "handle.\n");
+                                                            "handle:  %s\n",
+                                                            strerror(errno));
                return -1;
        }
 
libselinux/utils/setenforce.c +2 -1
index 60a20a4b..67c13dca 100644
@@ -4,6 +4,7 @@
 #include <ctype.h>
 #include <string.h>
 #include <strings.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 static __attribute__ ((__noreturn__)) void usage(const char *progname)
@@ -35,7 +36,7 @@ int main(int argc, char **argv)
                        usage(argv[0]);
        }
        if (rc < 0) {
-               fprintf(stderr, "%s:  setenforce() failed\n", argv[0]);
+               fprintf(stderr, "%s:  security_setenforce() failed:  %s\n", argv[0], strerror(errno));
                return 2;
        }
        return 0;
libselinux/utils/setfilecon.c +4 -3
index 79af55de..a3fbc3ed 100644
@@ -1,6 +1,8 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <errno.h>
 #include <selinux/selinux.h>
 
 int main(int argc, char **argv)
@@ -15,8 +17,8 @@ int main(int argc, char **argv)
        for (i = 2; i < argc; i++) {
                rc = setfilecon(argv[i], argv[1]);
                if (rc < 0) {
-                       fprintf(stderr, "%s:  setfilecon(%s,%s) failed\n",
-                               argv[0], argv[i], argv[1]);
+                       fprintf(stderr, "%s:  setfilecon(%s,%s) failed:  %s\n",
+                               argv[0], argv[i], argv[1], strerror(errno));
                        exit(2);
                }
        }
--

2.36.0