Skip to contentSkip to conversation

mountpoint: refactor exit path

PATCH In review Dave Reisner 2 participants 4 messages Oct 9, 2011 – Oct 10, 2011 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 mountpoint: refactor exit path

There's only one condition for which we declare success, but many for failure. Initialize rc as failure and set to success on this single condition. In all cases, jump to a label to exit instead of exiting immediately. This will be used later on to ease cleanup of any heap allocations.

---
 sys-utils/mountpoint.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)
sys-utils/mountpoint.c +9 -6
index 1297d82..e021c70 100644
@@ -105,7 +105,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 
 int main(int argc, char **argv)
 {
-	int c, fs_devno = 0, dev_devno = 0, rc = 0;
+	int c, fs_devno = 0, dev_devno = 0, rc = EXIT_FAILURE;
 	char *spec;
 	struct stat st;
 
@@ -152,7 +152,7 @@ int main(int argc, char **argv)
 	if (stat(spec, &st)) {
 		if (!quiet)
 			err(EXIT_FAILURE, "%s", spec);
-		return EXIT_FAILURE;
+		goto finish;
 	}
 	if (dev_devno)
 		rc = print_devno(spec, &st);
@@ -162,19 +162,22 @@ int main(int argc, char **argv)
 		if (!S_ISDIR(st.st_mode)) {
 			if (!quiet)
 				errx(EXIT_FAILURE, _("%s: not a directory"), spec);
-			return EXIT_FAILURE;
+			goto finish;
 		}
 		src = dir_to_device(spec);
 		if (src == (dev_t)-1) {
 			if (!quiet)
 				printf(_("%s is not a mountpoint\n"), spec);
-			return EXIT_FAILURE;
+			goto finish;
 		}
 		if (fs_devno)
 			printf("%u:%u\n", major(src), minor(src));
-		else if (!quiet)
+		else if (!quiet) {
 			printf(_("%s is a mountpoint\n"), spec);
+			rc = EXIT_SUCCESS;
+		}
 	}
 
-	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
+finish:
+	return rc;
 }
Signature · 1 line
1.7.7
#1 lore raw

PATCH 2/2 mountpoint: support symbolic and relative paths

Use our own canonicalize library to resolve a provided path before checking if its a mountpoint.

---
Unless I've missed something, this should bring u-l's mountpoint to feature parity with sysvinit's.

 sys-utils/Makefile.am  |    1 +
 sys-utils/mountpoint.c |    5 ++++-
 2 files changed, 5 insertions(+), 1 deletions(-)
sys-utils/Makefile.am +1 -0
index 8059ee6..34108da 100644
@@ -44,6 +44,7 @@ flock_SOURCES = flock.c $(top_srcdir)/lib/strutils.c
 
 if BUILD_MOUNTPOINT
 bin_PROGRAMS += mountpoint
+mountpoint_SOURCES = mountpoint.c $(top_srcdir)/lib/canonicalize.c
 mountpoint_LDADD = $(ul_libmount_la)
 mountpoint_CFLAGS = $(AM_CFLAGS) -I$(ul_libmount_incdir)
 dist_man_MANS += mountpoint.1
sys-utils/mountpoint.c +4 -1
index e021c70..204f061 100644
@@ -37,6 +37,7 @@
 #include "nls.h"
 #include "xalloc.h"
 #include "c.h"
+#include "canonicalize.h"
 
 static int quiet;
 
@@ -147,7 +148,8 @@ int main(int argc, char **argv)
 	if (optind + 1 != argc)
 		usage(stderr);
 
-	spec = argv[optind++];
+	/* resolve relative paths to absolute */
+	spec = canonicalize_path(argv[optind++]);
 
 	if (stat(spec, &st)) {
 		if (!quiet)
@@ -179,5 +181,6 @@ int main(int argc, char **argv)
 	}
 
 finish:
+	free(spec);
 	return rc;
 }
Signature · 1 line
1.7.7
#2 lore raw
In reply to Dave Reisner
Quoted 8 lines · Dave Reisner

There's only one condition for which we declare success, but many for failure. Initialize rc as failure and set to success on this single condition. In all cases, jump to a label to exit instead of exiting immediately. This will be used later on to ease cleanup of any heap allocations.

---

Noticing that this could be done better -- the return/goto calls here don't do anything because err/errx are exiting after throwing the error message. They should be replaced with equivalent warn/warnx calls so we hit the goto on error and cleanup before exiting.

@Karel: I can resend this if you like.

Regards,
dave

Quoted 55 lines
 sys-utils/mountpoint.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)
sys-utils/mountpoint.c +9 -6
index 1297d82..e021c70 100644
@@ -105,7 +105,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
 
 int main(int argc, char **argv)
 {
-	int c, fs_devno = 0, dev_devno = 0, rc = 0;
+	int c, fs_devno = 0, dev_devno = 0, rc = EXIT_FAILURE;
 	char *spec;
 	struct stat st;
 
@@ -152,7 +152,7 @@ int main(int argc, char **argv)
 	if (stat(spec, &st)) {
 		if (!quiet)
 			err(EXIT_FAILURE, "%s", spec);
-		return EXIT_FAILURE;
+		goto finish;
 	}
 	if (dev_devno)
 		rc = print_devno(spec, &st);
@@ -162,19 +162,22 @@ int main(int argc, char **argv)
 		if (!S_ISDIR(st.st_mode)) {
 			if (!quiet)
 				errx(EXIT_FAILURE, _("%s: not a directory"), spec);
-			return EXIT_FAILURE;
+			goto finish;
 		}
 		src = dir_to_device(spec);
 		if (src == (dev_t)-1) {
 			if (!quiet)
 				printf(_("%s is not a mountpoint\n"), spec);
-			return EXIT_FAILURE;
+			goto finish;
 		}
 		if (fs_devno)
 			printf("%u:%u\n", major(src), minor(src));
-		else if (!quiet)
+		else if (!quiet) {
 			printf(_("%s is a mountpoint\n"), spec);
+			rc = EXIT_SUCCESS;
+		}
 	}
 
-	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
+finish:
+	return rc;
 }
Signature · 1 line
1.7.7
maintainer
#3 lore raw
Quoted 6 lines · Dave Reisner
Quoted 5 lines · Dave Reisner

There's only one condition for which we declare success, but many for failure. Initialize rc as failure and set to success on this single condition. In all cases, jump to a label to exit instead of exiting immediately. This will be used later on to ease cleanup of any heap allocations.

Well, free() before exit() is usually waste of time... but maybe we can think about mountpoint(1) as about libmount test application, then it would be nice to keep it without leaks (for example for valgrind).

Quoted 1 line

@Karel: I can resend this if you like.

go ahead, thanks

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com