[libvirt] [PATCH v3 0/3] fix build failure in vircgroup code

Chagnes in v3: - removed VIR_CGROUP_SUPPORTED - include system headers only on linux Pavel Hrdina (3): vircgroup: remove VIR_CGROUP_SUPPORTED vircgroup: include system headers only on linux vircgroupv1: fix build on non-linux OSes src/util/vircgroup.c | 38 ++++++++++++++++---------------------- src/util/vircgroupv1.c | 20 +++++++++++++++----- 2 files changed, 31 insertions(+), 27 deletions(-) -- 2.17.1

tests/vircgrouptest.c uses #ifdef __linux__ for a long time and no failure was reported so far so it's safe to assume that __linux__ is good enough to guard cgroup code. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 23957c82c7..7700a9f7a7 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -66,11 +66,6 @@ VIR_LOG_INIT("util.cgroup"); #define CGROUP_NB_TOTAL_CPU_STAT_PARAM 3 #define CGROUP_NB_PER_CPU_STAT_PARAM 1 -#if defined(__linux__) && defined(HAVE_GETMNTENT_R) && \ - defined(_DIRENT_HAVE_D_TYPE) && defined(_SC_CLK_TCK) -# define VIR_CGROUP_SUPPORTED -#endif - VIR_ENUM_IMPL(virCgroupController, VIR_CGROUP_CONTROLLER_LAST, "cpu", "cpuacct", "cpuset", "memory", "devices", "freezer", "blkio", "net_cls", "perf_event", @@ -115,7 +110,7 @@ virCgroupGetDevicePermsString(int perms) } -#ifdef VIR_CGROUP_SUPPORTED +#ifdef __linux__ bool virCgroupAvailable(void) { @@ -2639,7 +2634,7 @@ virCgroupControllerAvailable(int controller) return ret; } -#else /* !VIR_CGROUP_SUPPORTED */ +#else /* !__linux__ */ bool virCgroupAvailable(void) @@ -3400,7 +3395,7 @@ virCgroupControllerAvailable(int controller ATTRIBUTE_UNUSED) { return false; } -#endif /* !VIR_CGROUP_SUPPORTED */ +#endif /* !__linux__ */ int -- 2.17.1

All the system headers are used only if we are compiling on linux and they all are present otherwise we would have seen build errors because in our tests/vircgrouptest.c we use only __linux__ to check whether to skip the cgroup tests or not. Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroup.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 7700a9f7a7..f90906e4ad 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -23,24 +23,23 @@ */ #include <config.h> -#if defined HAVE_MNTENT_H && defined HAVE_SYS_MOUNT_H \ - && defined HAVE_GETMNTENT_R +#ifdef __linux__ # include <mntent.h> # include <sys/mount.h> -#endif -#include <fcntl.h> -#include <sys/stat.h> +# include <fcntl.h> +# include <sys/stat.h> -#ifdef MAJOR_IN_MKDEV -# include <sys/mkdev.h> -#elif MAJOR_IN_SYSMACROS -# include <sys/sysmacros.h> -#endif +# ifdef MAJOR_IN_MKDEV +# include <sys/mkdev.h> +# elif MAJOR_IN_SYSMACROS +# include <sys/sysmacros.h> +# endif -#include <sys/types.h> -#include <signal.h> -#include <dirent.h> -#include <unistd.h> +# include <sys/types.h> +# include <signal.h> +# include <dirent.h> +# include <unistd.h> +#endif /* __linux__ */ #define __VIR_CGROUP_ALLOW_INCLUDE_PRIV_H__ #include "vircgrouppriv.h" -- 2.17.1

Cgroups are linux specific and we need to make sure that the code is compiled only on linux. On different OSes it fails the compilation: ../../src/util/vircgroupv1.c:65:19: error: variable has incomplete type 'struct mntent' struct mntent entry; ^ ../../src/util/vircgroupv1.c:65:12: note: forward declaration of 'struct mntent' struct mntent entry; ^ ../../src/util/vircgroupv1.c:74:12: error: implicit declaration of function 'getmntent_r' is invalid in C99 [-Werror,-Wimplicit-function-declaration] while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) { ^ ../../src/util/vircgroupv1.c:814:39: error: use of undeclared identifier 'MS_NOSUID' if (mount("tmpfs", root, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC, opts) < 0) { ^ ../../src/util/vircgroupv1.c:814:49: error: use of undeclared identifier 'MS_NODEV' if (mount("tmpfs", root, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC, opts) < 0) { ^ ../../src/util/vircgroupv1.c:814:58: error: use of undeclared identifier 'MS_NOEXEC' if (mount("tmpfs", root, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC, opts) < 0) { ^ ../../src/util/vircgroupv1.c:841:65: error: use of undeclared identifier 'MS_BIND' if (mount(src, group->legacy[i].mountPoint, "none", MS_BIND, ^ Signed-off-by: Pavel Hrdina <phrdina@redhat.com> --- src/util/vircgroupv1.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c index 62a6e5c448..28a74474ee 100644 --- a/src/util/vircgroupv1.c +++ b/src/util/vircgroupv1.c @@ -20,13 +20,11 @@ */ #include <config.h> -#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R +#ifdef __linux__ # include <mntent.h> -#endif -#include <sys/stat.h> -#if defined HAVE_SYS_MOUNT_H +# include <sys/stat.h> # include <sys/mount.h> -#endif +#endif /* __linux__ */ #include "internal.h" @@ -55,6 +53,8 @@ VIR_ENUM_IMPL(virCgroupV1Controller, VIR_CGROUP_CONTROLLER_LAST, "name=systemd"); +#ifdef __linux__ + /* We're looking for at least one 'cgroup' fs mount, * which is *not* a named mount. */ static bool @@ -2099,3 +2099,13 @@ virCgroupV1Register(void) { virCgroupBackendRegister(&virCgroupV1Backend); } + +#else /* !__linux__ */ + +void +virCgroupV1Register(void) +{ + VIR_INFO("Control groups not supported on this platform"); +} + +#endif /* !__linux__ */ -- 2.17.1

On Thu, Sep 27, 2018 at 12:31:08PM +0200, Pavel Hrdina wrote:
Chagnes in v3: - removed VIR_CGROUP_SUPPORTED - include system headers only on linux
Pavel Hrdina (3): vircgroup: remove VIR_CGROUP_SUPPORTED vircgroup: include system headers only on linux vircgroupv1: fix build on non-linux OSes
src/util/vircgroup.c | 38 ++++++++++++++++---------------------- src/util/vircgroupv1.c | 20 +++++++++++++++----- 2 files changed, 31 insertions(+), 27 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano
participants (2)
-
Ján Tomko
-
Pavel Hrdina