Skip to contentSkip to conversation

mountpoint: refactor exit path

PATCH Applied Dave Reisner 3 participants 9 messages Oct 10, 2011 – Oct 11, 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.

---
v2: use warn{,x} instead of err{,x} to avoid leaks.

 sys-utils/mountpoint.c |   19 +++++++++++--------
 1 files changed, 11 insertions(+), 8 deletions(-)
sys-utils/mountpoint.c +11 -8
index 1297d82..e075b83 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;
 
@@ -151,8 +151,8 @@ int main(int argc, char **argv)
 
 	if (stat(spec, &st)) {
 		if (!quiet)
-			err(EXIT_FAILURE, "%s", spec);
-		return EXIT_FAILURE;
+			warn("%s", spec);
+		goto finish;
 	}
 	if (dev_devno)
 		rc = print_devno(spec, &st);
@@ -161,20 +161,23 @@ 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;
+				warnx(_("%s: not a directory"), spec);
+			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.

---
 sys-utils/Makefile.am  |    1 +
 sys-utils/mountpoint.c |    5 ++++-
 2 files changed, 5 insertions(+), 1 deletions(-)
sys-utils/Makefile.am +1 -0
index cfaf053..49141fb 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 e075b83..2565107 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
#3 lore raw
In reply to Dave Reisner

sys-utils/mountpoint.c int main(int argc, char **argv) · line 178

Quoted 4 lines · Dave Reisner
sys-utils/mountpoint.c +3 -0
+		else if (!quiet) {
 			printf(_("%s is a mountpoint\n"), spec);
+			rc = EXIT_SUCCESS;
+		}

So mountpoint will never return SUCCESS if -q is used?

sys-utils/mountpoint.c int main(int argc, char **argv) · line 182

Quoted 3 lines
sys-utils/mountpoint.c +2 -1
-	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
+finish:
+	return rc;

IMHO 'goto' is ugly and should only be used in more difficult cases. In this case - if you really liked to avoid several free(spec) statements - it could be easily done with a few else statements.

Berny

maintainer
#4 lore raw
Quoted 7 lines · Bernhard

sys-utils/mountpoint.c int main(int argc, char **argv) · line 182

Quoted 2 lines
sys-utils/mountpoint.c +2 -0
+finish:
+	return rc;

IMHO 'goto' is ugly and should only be used in more difficult cases. In this case - if you really liked to avoid several free(spec) statements - it could be easily done with a few else statements.

kernel Documentation/CodingStyle:

Albeit deprecated by some people, the equivalent of the goto statement is used frequently by compilers in form of the unconditional jump instruction.

The goto statement comes in handy when a function exits from multiple locations and some common work such as cleanup has to be done.

The rationale is:

- unconditional statements are easier to understand and follow
- nesting is reduced
- errors by not updating individual exit points when making modifications are prevented
- saves the compiler work to optimize redundant code away ;)

BTW, now you know why I don't like free-before-exit in simple utils like mountpoint(1). It makes code less readable and needlessly complicated.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#5 lore raw
Quoted 10 lines · Karel Zak
Quoted 7 lines · Bernhard

sys-utils/mountpoint.c int main(int argc, char **argv) · line 182

Quoted 2 lines
sys-utils/mountpoint.c +2 -0
+finish:
+	return rc;

IMHO 'goto' is ugly and should only be used in more difficult cases. In this case - if you really liked to avoid several free(spec) statements - it could be easily done with a few else statements.

Quoted 3 lines

BTW, now you know why I don't like free-before-exit in simple utils like mountpoint(1). It makes code less readable and needlessly complicated.

no worries. I'm okay with it if you are. ;-)

Re free-before-exit: the problem is in tools like valgrind. I'm not much into it, but don't they know about 'noreturn'?

Have a nice day,
Berny

maintainer
#6 lore raw
Quoted 12 lines · Bernhard
Quoted 8 lines · Karel Zak
Quoted 7 lines · Bernhard

sys-utils/mountpoint.c int main(int argc, char **argv) · line 182

Quoted 2 lines
sys-utils/mountpoint.c +2 -0
+finish:
+	return rc;

IMHO 'goto' is ugly and should only be used in more difficult cases. In this case - if you really liked to avoid several free(spec) statements - it could be easily done with a few else statements.

Quoted 7 lines
Quoted 3 lines

BTW, now you know why I don't like free-before-exit in simple utils like mountpoint(1). It makes code less readable and needlessly complicated.

no worries. I'm okay with it if you are. ;-)

Re free-before-exit: the problem is in tools like valgrind.

Yes if we want to use mountpoint(1) as a test (referential) application for libmount, otherwise it's better to keep so small utils simple and stupid.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer Applied
#7 lore raw
In reply to Dave Reisner

sys-utils/mountpoint.c int main(int argc, char **argv) · line 158

Quoted 2 lines · Dave Reisner
sys-utils/mountpoint.c +0 -0
 	if (dev_devno)
 		rc = print_devno(spec, &st);

print_devno() returns -1 on error. Fixed.

Quoted 12 lines
sys-utils/mountpoint.c +2 -2
@@ -161,20 +161,23 @@ 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;
+				warnx(_("%s: not a directory"), spec);
+			goto finish;
 		}
 		src = dir_to_device(spec);
 		if (src == (dev_t)-1) {
 			if (!quiet)

sys-utils/mountpoint.c int main(int argc, char **argv) · line 177

Quoted 10 lines
sys-utils/mountpoint.c +3 -2
 				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;

Fixed. (see Berny's email)

Applied, thanks.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#8 lore raw
Quoted 1 line · Karel Zak

Applied, thanks.

I completely forgot that we can use libmount for canonicalization. It means that the exit-path refactoring is unnecessary. Sorry.

The problem should be resolved in our git tree. Thanks!

Karel

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

Re: [PATCH 2/2] mountpoint: support symbolic and relative paths

In reply to Dave Reisner
Quoted 2 lines · Dave Reisner

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

Good idea. Thanks.

I have applied a more robust solution based on libmount.

Karel

==27191== Command:
/home/projects/util-linux/util-linux/sys-utils/.libs/mountpoint home ==27191==
home is a mountpoint
==27191==
==27191== HEAP SUMMARY:
==27191== in use at exit: 0 bytes in 0 blocks
==27191== total heap usage: 535 allocs, 535 frees, 44,094 bytes
allocated
==27191==
==27191== All heap blocks were freed -- no leaks are possible ==27191==
==27191== For counts of detected and suppressed errors, rerun with: -v ==27191== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)

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