util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] switch_root: use typeof() instead of __SWORD_TYPE
@ 2014-10-29  9:09 Natanael Copa
  2014-10-29 19:37 ` Mike Frysinger
  0 siblings, 1 reply; 3+ messages in thread
From: Natanael Copa @ 2014-10-29  9:09 UTC (permalink / raw)
  To: util-linux; +Cc: Natanael Copa

Identifiers prefixed with __ are normally for internal use and should
normally not be used outside libc.

This fixes the following compile error with musl libc:
sys-utils/switch_root.c:184:25: error: '__SWORD_TYPE' undeclared (first use in this function)
        (stfs.f_type == (__SWORD_TYPE)STATFS_RAMFS_MAGIC ||
                         ^

Also, statfs(2) man page is also wrong on some systems, because f_type
is not __SWORD_TYPE on some architecures.

The following program:

int main(int argc, char**argv)
{
        struct statfs s;
        statfs(argv[1], &s);

	printf("sizeof(f_type) = %d\n", sizeof(s.f_type));
	printf("sizeof(__SWORD_TYPE) = %d\n", sizeof(__SWORD_TYPE));
	printf("sizeof(long) = %d\n", sizeof(long));
	printf("sizeof(int) = %d\n", sizeof(int));
	if (sizeof(s.f_type) == sizeof(int)) {
		printf("f_type = 0x%x\n", s.f_type);
	} else {
                printf("f_type = 0x%lx\n", s.f_type);
	}
        return 0;
}

executed on s390x gives for a btrfs:

sizeof(f_type) = 4
sizeof(__SWORD_TYPE) = 8
sizeof(long) = 8
sizeof(int) = 4
f_type = 0x9123683e

Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
---
This is similar to what they did in systemd:
http://cgit.freedesktop.org/systemd/systemd/commit/?id=bdd29249a882e599e5e365536372d08dee398cd4

 sys-utils/switch_root.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c
index 6822a5d..3fbecdd 100644
--- a/sys-utils/switch_root.c
+++ b/sys-utils/switch_root.c
@@ -181,8 +181,8 @@ static int switchroot(const char *newroot)
 		if (pid <= 0) {
 			struct statfs stfs;
 			if (fstatfs(cfd, &stfs) == 0 &&
-			    (stfs.f_type == (__SWORD_TYPE)STATFS_RAMFS_MAGIC ||
-			     stfs.f_type == (__SWORD_TYPE)STATFS_TMPFS_MAGIC))
+			    (stfs.f_type == (typeof(stfs.f_type))STATFS_RAMFS_MAGIC ||
+			     stfs.f_type == (typeof(stfs.f_type))STATFS_TMPFS_MAGIC))
 				recursiveRemove(cfd);
 			else
 				warn(_("old root filesystem is not an initramfs"));
-- 
2.1.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] switch_root: use typeof() instead of __SWORD_TYPE
  2014-10-29  9:09 [PATCH] switch_root: use typeof() instead of __SWORD_TYPE Natanael Copa
@ 2014-10-29 19:37 ` Mike Frysinger
  2014-11-06 12:01   ` Karel Zak
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Frysinger @ 2014-10-29 19:37 UTC (permalink / raw)
  To: Natanael Copa; +Cc: util-linux

[-- Attachment #1: Type: text/plain, Size: 323 bytes --]

On 29 Oct 2014 09:09, Natanael Copa wrote:
> +			    (stfs.f_type == (typeof(stfs.f_type))STATFS_RAMFS_MAGIC ||
> +			     stfs.f_type == (typeof(stfs.f_type))STATFS_TMPFS_MAGIC))

typeof is a gcc extension and really should be __typeof__.  ignoring irony of 
replacing on unportable behavior with another ;).
-mike

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] switch_root: use typeof() instead of __SWORD_TYPE
  2014-10-29 19:37 ` Mike Frysinger
@ 2014-11-06 12:01   ` Karel Zak
  0 siblings, 0 replies; 3+ messages in thread
From: Karel Zak @ 2014-11-06 12:01 UTC (permalink / raw)
  To: Natanael Copa, util-linux

On Wed, Oct 29, 2014 at 03:37:52PM -0400, Mike Frysinger wrote:
> On 29 Oct 2014 09:09, Natanael Copa wrote:
> > +			    (stfs.f_type == (typeof(stfs.f_type))STATFS_RAMFS_MAGIC ||
> > +			     stfs.f_type == (typeof(stfs.f_type))STATFS_TMPFS_MAGIC))
> 
> typeof is a gcc extension and really should be __typeof__.  ignoring irony of 
> replacing on unportable behavior with another ;).

:-)

I have applied the patch below, please review. It is not perfect, but I
hope good enough for usual cases (gcc, clang).

    Karel


>From 8f806bb1ea30f15db7ca36d1cfa79349f8115302 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 6 Nov 2014 12:50:27 +0100
Subject: [PATCH] switch_root: improve statfs->f_type portability

__SWORD_TYPE is not available everywhere, for example it's not defined
by musl libc. It also seems that __SWORD_TYPE is not used for f_type
on some architectures (s390x).

Reported-by: Natanael Copa <ncopa@alpinelinux.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
 include/statfs_magic.h  | 11 +++++++++++
 sys-utils/switch_root.c |  4 ++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/include/statfs_magic.h b/include/statfs_magic.h
index b5fde1a..d27be1c 100644
--- a/include/statfs_magic.h
+++ b/include/statfs_magic.h
@@ -1,6 +1,17 @@
 #ifndef UTIL_LINUX_STATFS_MAGIC_H
 #define UTIL_LINUX_STATFS_MAGIC_H
 
+#include <sys/statfs.h>
+
+/*
+ * If possible then don't depend on internal libc __SWORD_TYPE type.
+ */
+#ifdef __GNUC__
+typedef __typeof__( ((struct statfs *)0)->f_type )	ul_statfs_ftype_t;
+#else
+typedef __SWORD_TYPE	ul_statfs_ftype_t;
+#endif
+
 /*
  *  Unfortunately, Linux kernel hedeader file <linux/magic.h> is incomplete
  *  mess and kernel returns by statfs f_type many numbers that are nowhere
diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c
index 6822a5d..c6a2eff 100644
--- a/sys-utils/switch_root.c
+++ b/sys-utils/switch_root.c
@@ -181,8 +181,8 @@ static int switchroot(const char *newroot)
 		if (pid <= 0) {
 			struct statfs stfs;
 			if (fstatfs(cfd, &stfs) == 0 &&
-			    (stfs.f_type == (__SWORD_TYPE)STATFS_RAMFS_MAGIC ||
-			     stfs.f_type == (__SWORD_TYPE)STATFS_TMPFS_MAGIC))
+			    (stfs.f_type == (ul_statfs_ftype_t) STATFS_RAMFS_MAGIC ||
+			     stfs.f_type == (ul_statfs_ftype_t) STATFS_TMPFS_MAGIC))
 				recursiveRemove(cfd);
 			else
 				warn(_("old root filesystem is not an initramfs"));
-- 
1.9.3



-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-11-06 12:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-29  9:09 [PATCH] switch_root: use typeof() instead of __SWORD_TYPE Natanael Copa
2014-10-29 19:37 ` Mike Frysinger
2014-11-06 12:01   ` Karel Zak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).