Commit 1ba0013f by Stéphane Graber

Support both getline and fgetln

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: 's avatarStéphane Graber <stgraber@ubuntu.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent 7c11d57a
...@@ -216,6 +216,15 @@ AC_CHECK_DECLS([PR_CAPBSET_DROP], [], [], [#include <sys/prctl.h>]) ...@@ -216,6 +216,15 @@ AC_CHECK_DECLS([PR_CAPBSET_DROP], [], [], [#include <sys/prctl.h>])
# Check for optional headers # Check for optional headers
AC_CHECK_HEADERS([sys/signalfd.h]) AC_CHECK_HEADERS([sys/signalfd.h])
AC_CHECK_FUNCS([getline],
AM_CONDITIONAL(HAVE_GETLINE, true)
AC_DEFINE(HAVE_GETLINE,1,[Have getline]),
AM_CONDITIONAL(HAVE_GETLINE, false))
AC_CHECK_FUNCS([fgetln],
AM_CONDITIONAL(HAVE_FGETLN, true)
AC_DEFINE(HAVE_FGETLN,1,[Have fgetln]),
AM_CONDITIONAL(HAVE_FGETLN, false))
# Check for some standard binaries # Check for some standard binaries
AC_PROG_GCC_TRADITIONAL AC_PROG_GCC_TRADITIONAL
AC_PROG_SED AC_PROG_SED
......
#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);
}
#ifndef _getline_h
#define _getline_h
extern ssize_t getline(char **outbuf, size_t *outsize, FILE *fp);
#endif
...@@ -17,6 +17,12 @@ pkginclude_HEADERS = \ ...@@ -17,6 +17,12 @@ pkginclude_HEADERS = \
lxccontainer.h \ lxccontainer.h \
lxclock.h lxclock.h
if !HAVE_GETLINE
if HAVE_FGETLN
pkginclude_HEADERS += ../include/getline.h
endif
endif
sodir=$(libdir) sodir=$(libdir)
# use PROGRAMS to avoid complains from automake # use PROGRAMS to avoid complains from automake
so_PROGRAMS = liblxc.so so_PROGRAMS = liblxc.so
...@@ -61,6 +67,12 @@ liblxc_so_SOURCES = \ ...@@ -61,6 +67,12 @@ liblxc_so_SOURCES = \
lxclock.h lxclock.c \ lxclock.h lxclock.c \
lxccontainer.c lxccontainer.h lxccontainer.c lxccontainer.h
if !HAVE_GETLINE
if HAVE_FGETLN
liblxc_so_SOURCES += ../include/getline.c ../include/getline.h
endif
endif
AM_CFLAGS=-I$(top_srcdir)/src \ AM_CFLAGS=-I$(top_srcdir)/src \
-DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \ -DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \
-DLXCPATH=\"$(LXCPATH)\" \ -DLXCPATH=\"$(LXCPATH)\" \
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <sys/param.h> #include <sys/param.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include <sys/mount.h> #include <sys/mount.h>
#include <sys/syscall.h>
#include <linux/unistd.h> #include <linux/unistd.h>
#if !HAVE_DECL_PR_CAPBSET_DROP #if !HAVE_DECL_PR_CAPBSET_DROP
...@@ -56,6 +57,13 @@ int setns(int fd, int nstype) ...@@ -56,6 +57,13 @@ int setns(int fd, int nstype)
#endif #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 *lxc_proc_get_context_info(pid_t pid)
{ {
struct lxc_proc_context_info *info = calloc(1, sizeof(*info)); struct lxc_proc_context_info *info = calloc(1, sizeof(*info));
......
...@@ -29,8 +29,16 @@ ...@@ -29,8 +29,16 @@
#include <dirent.h> #include <dirent.h>
#include "parse.h" #include "parse.h"
#include "config.h"
#include <lxc/log.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); lxc_log_define(lxc_parse, lxc);
static int dir_filter(const struct dirent *dirent) static int dir_filter(const struct dirent *dirent)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment