[lxc-devel] [PATCH 18/21] Add local implementation of mntent.h

Stéphane Graber stgraber at ubuntu.com
Thu Jan 3 17:24:19 UTC 2013


Bionic (at least) is missing some of the usual mntent functions.
This adds code defining those that we need when they're missing from the C
library.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 src/include/mntent.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/include/mntent.h | 18 ++++++++++++++++++
 src/lxc/Makefile.am  |  8 ++++++--
 src/lxc/cgroup.c     | 12 ++++++++++++
 src/lxc/conf.c       |  8 ++++++++
 5 files changed, 93 insertions(+), 2 deletions(-)
 create mode 100644 src/include/mntent.c
 create mode 100644 src/include/mntent.h

diff --git a/src/include/mntent.c b/src/include/mntent.c
new file mode 100644
index 0000000..cd22a89
--- /dev/null
+++ b/src/include/mntent.c
@@ -0,0 +1,49 @@
+#include <mntent.h>
+#include <stdio.h>
+#include <string.h>
+
+/* Prepare to begin reading and/or writing mount table entries from the
+ beginning of FILE. MODE is as for `fopen'. */
+FILE *setmntent (const char *file, const char *mode)
+{
+    /* Extend the mode parameter with "c" to disable cancellation in the
+    I/O functions and "e" to set FD_CLOEXEC. */
+    size_t modelen = strlen (mode);
+    char newmode[modelen + 3];
+    memcpy (newmode, mode, modelen);
+    memcpy (newmode + modelen, "ce", 3);
+    FILE *result = fopen (file, newmode);
+
+    return result;
+}
+
+
+/* Close a stream opened with `setmntent'. */
+int endmntent (FILE *stream)
+{
+    if (stream) /* SunOS 4.x allows for NULL stream */
+    fclose (stream);
+    return 1; /* SunOS 4.x says to always return 1 */
+}
+
+/* Search MNT->mnt_opts for an option matching OPT.
+ Returns the address of the substring, or null if none found. */
+char *hasmntopt (const struct mntent *mnt, const char *opt)
+{
+    const size_t optlen = strlen (opt);
+    char *rest = mnt->mnt_opts, *p;
+
+    while ((p = strstr (rest, opt)) != NULL)
+    {
+        if ((p == rest || p[-1] == ',')
+            && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
+            return p;
+
+        rest = strchr (p, ',');
+        if (rest == NULL)
+            break;
+        ++rest;
+    }
+
+    return NULL;
+}
diff --git a/src/include/mntent.h b/src/include/mntent.h
new file mode 100644
index 0000000..0a0398a
--- /dev/null
+++ b/src/include/mntent.h
@@ -0,0 +1,18 @@
+#ifndef _mntent_h
+#define _mntent_h
+
+#include <../config.h>
+
+#ifndef HAVE_SETMNTENT
+FILE *setmntent (const char *file, const char *mode);
+#endif
+
+#ifndef HAVE_ENDMNTENT
+int endmntent (FILE *stream);
+#endif
+
+#ifndef HAVE_HASMNTOPT
+extern char *hasmntopt (const struct mntent *mnt, const char *opt);
+#endif
+
+#endif
diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index c82ee76..a6bfa9b 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -18,7 +18,9 @@ pkginclude_HEADERS = \
 		lxclock.h
 
 if IS_BIONIC
-pkginclude_HEADERS += ../include/openpty.h
+pkginclude_HEADERS += \
+	../include/openpty.h \
+	../include/mntent.h
 endif
 
 sodir=$(libdir)
@@ -66,7 +68,9 @@ liblxc_so_SOURCES = \
 	lxccontainer.c lxccontainer.h
 
 if IS_BIONIC
-liblxc_so_SOURCES += ../include/openpty.c ../include/openpty.h
+liblxc_so_SOURCES += \
+	../include/openpty.c ../include/openpty.h \
+	../include/mntent.c ../include/mntent.h
 endif
 
 AM_CFLAGS=-I$(top_srcdir)/src \
diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c
index f6243b8..88d8ff8 100644
--- a/src/lxc/cgroup.c
+++ b/src/lxc/cgroup.c
@@ -44,6 +44,18 @@
 #include <lxc/cgroup.h>
 #include <lxc/start.h>
 
+#ifndef HAVE_HASMNTOPT
+#include <../include/mntent.h>
+#endif
+
+#ifndef HAVE_SETMNTENT
+#include <../include/mntent.h>
+#endif
+
+#ifndef HAVE_ENDMNTENT
+#include <../include/mntent.h>
+#endif
+
 lxc_log_define(lxc_cgroup, lxc);
 
 #define MTAB "/proc/mounts"
diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 7963b00..21a5cbb 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -77,6 +77,14 @@
 #include <sys/personality.h>
 #endif
 
+#ifndef HAVE_SETMNTENT
+#include <../include/mntent.h>
+#endif
+
+#ifndef HAVE_ENDMNTENT
+#include <../include/mntent.h>
+#endif
+
 #include "lxcseccomp.h"
 
 lxc_log_define(lxc_conf, lxc);
-- 
1.8.0





More information about the lxc-devel mailing list