[lxc-devel] [PATCH 04/24] Support both getline and fgetln
Serge Hallyn
serge.hallyn at canonical.com
Tue Jan 8 18:03:10 UTC 2013
Quoting Stéphane Graber (stgraber at ubuntu.com):
> Some libc implementations don't have the getline function but instead
> have an equivalent fgetln function.
>
> Add code to detect both and use whatever is available.
>
> Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
My only concern here is that you switch on IS_BIONIC in the makefile,
but on HAVE_GETLINE in the source.
If/when someone uses a libc without HAVE_GETLINE on non-bionic, this
won't work quite right. It's something we can straighten out with a
later patch, though.
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>
> ---
> configure.ac | 3 +++
> src/include/getline.c | 31 +++++++++++++++++++++++++++++++
> src/include/getline.h | 6 ++++++
> src/lxc/Makefile.am | 10 ++++++++++
> src/lxc/attach.c | 8 ++++++++
> src/lxc/parse.c | 8 ++++++++
> 6 files changed, 66 insertions(+)
> create mode 100644 src/include/getline.c
> create mode 100644 src/include/getline.h
>
> diff --git a/configure.ac b/configure.ac
> index 564df0e..50e64ff 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -216,6 +216,9 @@ AC_CHECK_DECLS([PR_CAPBSET_DROP], [], [], [#include <sys/prctl.h>])
> # Check for optional headers
> AC_CHECK_HEADERS([sys/signalfd.h])
>
> +# Check for some functions
> +AC_CHECK_FUNCS([getline fgetln])
> +
> # Check for some standard binaries
> AC_PROG_GCC_TRADITIONAL
> AC_PROG_SED
> diff --git a/src/include/getline.c b/src/include/getline.c
> new file mode 100644
> index 0000000..d4117cb
> --- /dev/null
> +++ b/src/include/getline.c
> @@ -0,0 +1,31 @@
> +#include <sys/types.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +
> +/*
> + * Emulate glibc getline() via BSD fgetln().
> + * Note that outsize is not changed unless memory is allocated.
> + */
> +ssize_t
> +getline(char **outbuf, size_t *outsize, FILE *fp)
> +{
> + size_t len;
> + char *buf;
> + buf = fgetln(fp, &len);
> +
> + if (buf == NULL)
> + return (-1);
> +
> + /* Assumes realloc() accepts NULL for ptr (C99) */
> + if (*outbuf == NULL || *outsize < len + 1) {
> + void *tmp = realloc(*outbuf, len + 1);
> + if (tmp == NULL)
> + return (-1);
> + *outbuf = tmp;
> + *outsize = len + 1;
> + }
> + memcpy(*outbuf, buf, len);
> + (*outbuf)[len] = '\0';
> + return (len);
> +}
> diff --git a/src/include/getline.h b/src/include/getline.h
> new file mode 100644
> index 0000000..b030d7a
> --- /dev/null
> +++ b/src/include/getline.h
> @@ -0,0 +1,6 @@
> +#ifndef _getline_h
> +#define _getline_h
> +
> +extern ssize_t getline(char **outbuf, size_t *outsize, FILE *fp);
> +
> +#endif
> diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
> index bf675f9..23b6772 100644
> --- a/src/lxc/Makefile.am
> +++ b/src/lxc/Makefile.am
> @@ -17,6 +17,11 @@ pkginclude_HEADERS = \
> lxccontainer.h \
> lxclock.h
>
> +if IS_BIONIC
> +pkginclude_HEADERS += \
> + ../include/getline.h
> +endif
> +
> sodir=$(libdir)
> # use PROGRAMS to avoid complains from automake
> so_PROGRAMS = liblxc.so
> @@ -61,6 +66,11 @@ liblxc_so_SOURCES = \
> lxclock.h lxclock.c \
> lxccontainer.c lxccontainer.h
>
> +if IS_BIONIC
> +liblxc_so_SOURCES += \
> + ../include/getline.c ../include/getline.h
> +endif
> +
> AM_CFLAGS=-I$(top_srcdir)/src \
> -DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \
> -DLXCPATH=\"$(LXCPATH)\" \
> diff --git a/src/lxc/attach.c b/src/lxc/attach.c
> index ec0e083..9b7efbc 100644
> --- a/src/lxc/attach.c
> +++ b/src/lxc/attach.c
> @@ -31,6 +31,7 @@
> #include <sys/param.h>
> #include <sys/prctl.h>
> #include <sys/mount.h>
> +#include <sys/syscall.h>
> #include <linux/unistd.h>
>
> #if !HAVE_DECL_PR_CAPBSET_DROP
> @@ -56,6 +57,13 @@ int setns(int fd, int nstype)
> #endif
> }
>
> +/* Define getline() if missing from the C library */
> +#ifndef HAVE_GETLINE
> +#ifdef HAVE_FGETLN
> +#include <../include/getline.h>
> +#endif
> +#endif
> +
> struct lxc_proc_context_info *lxc_proc_get_context_info(pid_t pid)
> {
> struct lxc_proc_context_info *info = calloc(1, sizeof(*info));
> diff --git a/src/lxc/parse.c b/src/lxc/parse.c
> index 10510c9..b074b04 100644
> --- a/src/lxc/parse.c
> +++ b/src/lxc/parse.c
> @@ -29,8 +29,16 @@
> #include <dirent.h>
>
> #include "parse.h"
> +#include "config.h"
> #include <lxc/log.h>
>
> +/* Define getline() if missing from the C library */
> +#ifndef HAVE_GETLINE
> +#ifdef HAVE_FGETLN
> +#include <../include/getline.h>
> +#endif
> +#endif
> +
> lxc_log_define(lxc_parse, lxc);
>
> static int dir_filter(const struct dirent *dirent)
> --
> 1.8.0
>
>
> ------------------------------------------------------------------------------
> Master SQL Server Development, Administration, T-SQL, SSAS, SSIS, SSRS
> and more. Get SQL Server skills now (including 2012) with LearnDevNow -
> 200+ hours of step-by-step video tutorials by Microsoft MVPs and experts.
> SALE $99.99 this month only - learn more at:
> http://p.sf.net/sfu/learnmore_122512
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel
More information about the lxc-devel
mailing list