Skip to content

Commit

Permalink
Merge pull request #2572 from brauner/2018-08-24/musl_fixes
Browse files Browse the repository at this point in the history
build: fix musl + add compiler.h
  • Loading branch information
Blub committed Aug 29, 2018
2 parents de2c631 + 62fc840 commit f288e10
Show file tree
Hide file tree
Showing 24 changed files with 203 additions and 147 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -611,7 +611,7 @@ AC_CHECK_DECLS([PR_SET_NO_NEW_PRIVS], [], [], [#include <sys/prctl.h>])
AC_CHECK_DECLS([PR_GET_NO_NEW_PRIVS], [], [], [#include <sys/prctl.h>])

# Check for some headers
AC_CHECK_HEADERS([sys/signalfd.h pty.h sys/memfd.h sys/personality.h utmpx.h sys/timerfd.h sys/resource.h])
AC_CHECK_HEADERS([pty.h sys/memfd.h sys/personality.h sys/resource.h sys/signalfd.h sys/timerfd.h utmpx.h])

AC_CHECK_HEADER([ifaddrs.h],
AM_CONDITIONAL(HAVE_IFADDRS_H, true)
Expand Down
10 changes: 8 additions & 2 deletions src/lxc/Makefile.am
Expand Up @@ -6,6 +6,7 @@ noinst_HEADERS = attach.h \
caps.h \
cgroups/cgroup.h \
cgroups/cgroup_utils.h \
compiler.h \
conf.h \
confile.h \
confile_utils.h \
Expand Down Expand Up @@ -39,9 +40,12 @@ noinst_HEADERS = attach.h \
tools/arguments.h \
utils.h

if !HAVE_IFADDRS_H
noinst_HEADERS += ../include/ifaddrs.h
endif

if IS_BIONIC
noinst_HEADERS += ../include/ifaddrs.h \
../include/lxcmntent.h \
noinst_HEADERS += ../include/lxcmntent.h \
../include/openpty.h
endif

Expand Down Expand Up @@ -86,6 +90,7 @@ liblxc_la_SOURCES = af_unix.c af_unix.h \
cgroups/cgfsng.c \
cgroups/cgroup.c cgroups/cgroup.h \
cgroups/cgroup_utils.c cgroups/cgroup_utils.h \
compiler.h \
commands.c commands.h \
commands_utils.c commands_utils.h \
conf.c conf.h \
Expand Down Expand Up @@ -300,6 +305,7 @@ lxc_freeze_SOURCES = tools/lxc_freeze.c \
lxc_info_SOURCES = tools/lxc_info.c \
tools/arguments.c tools/arguments.h
lxc_monitor_SOURCES = tools/lxc_monitor.c \
macro.h \
tools/arguments.c tools/arguments.h
lxc_ls_SOURCES = tools/lxc_ls.c \
tools/arguments.c tools/arguments.h
Expand Down
29 changes: 2 additions & 27 deletions src/lxc/caps.c
Expand Up @@ -39,31 +39,6 @@ lxc_log_define(caps, lxc);

#if HAVE_LIBCAP

#ifndef PR_CAPBSET_READ
#define PR_CAPBSET_READ 23
#endif

/* Control the ambient capability set */
#ifndef PR_CAP_AMBIENT
#define PR_CAP_AMBIENT 47
#endif

#ifndef PR_CAP_AMBIENT_IS_SET
#define PR_CAP_AMBIENT_IS_SET 1
#endif

#ifndef PR_CAP_AMBIENT_RAISE
#define PR_CAP_AMBIENT_RAISE 2
#endif

#ifndef PR_CAP_AMBIENT_LOWER
#define PR_CAP_AMBIENT_LOWER 3
#endif

#ifndef PR_CAP_AMBIENT_CLEAR_ALL
#define PR_CAP_AMBIENT_CLEAR_ALL 4
#endif

int lxc_caps_down(void)
{
cap_t caps;
Expand Down Expand Up @@ -321,10 +296,10 @@ static long int _real_caps_last_cap(void)
if (fd >= 0) {
ssize_t n;
char *ptr;
char buf[LXC_NUMSTRLEN64 + 1];
char buf[INTTYPE_TO_STRLEN(int)];

again:
n = read(fd, buf, LXC_NUMSTRLEN64);
n = read(fd, buf, INTTYPE_TO_STRLEN(int));
if (n < 0 && errno == EINTR) {
goto again;
} else if (n >= 0) {
Expand Down
7 changes: 4 additions & 3 deletions src/lxc/cgroups/cgfsng.c
Expand Up @@ -55,6 +55,7 @@
#include "commands.h"
#include "conf.h"
#include "log.h"
#include "macro.h"
#include "storage/storage.h"
#include "utils.h"

Expand Down Expand Up @@ -314,14 +315,14 @@ static char *lxc_cpumask_to_cpulist(uint32_t *bitarr, size_t nbits)
int ret;
size_t i;
char **cpulist = NULL;
char numstr[LXC_NUMSTRLEN64] = {0};
char numstr[INTTYPE_TO_STRLEN(size_t)] = {0};

for (i = 0; i <= nbits; i++) {
if (!is_set(i, bitarr))
continue;

ret = snprintf(numstr, LXC_NUMSTRLEN64, "%zu", i);
if (ret < 0 || (size_t)ret >= LXC_NUMSTRLEN64) {
ret = snprintf(numstr, INTTYPE_TO_STRLEN(size_t), "%zu", i);
if (ret < 0 || (size_t)ret >= INTTYPE_TO_STRLEN(size_t)) {
lxc_free_array((void **)cpulist, free);
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/commands.c
Expand Up @@ -1239,7 +1239,7 @@ static int lxc_cmd_accept(int fd, uint32_t events, void *data,
int lxc_cmd_init(const char *name, const char *lxcpath, const char *suffix)
{
int fd, len, ret;
char path[sizeof(((struct sockaddr_un *)0)->sun_path)] = {0};
char path[LXC_AUDS_ADDR_LEN] = {0};
char *offset = &path[1];

/* -2 here because this is an abstract unix socket so it needs a
Expand Down
11 changes: 3 additions & 8 deletions src/lxc/commands.h
Expand Up @@ -25,17 +25,12 @@
#define __LXC_COMMANDS_H

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>

#include "state.h"
#include "lxccontainer.h"

#define LXC_CMD_DATA_MAX (MAXPATHLEN * 2)

/* https://developer.gnome.org/glib/2.28/glib-Type-Conversion-Macros.html */
#define INT_TO_PTR(n) ((void *)(long)(n))
#define PTR_TO_INT(p) ((int)(long)(p))
#include "macro.h"
#include "state.h"

typedef enum {
LXC_CMD_CONSOLE,
Expand Down
2 changes: 1 addition & 1 deletion src/lxc/commands_utils.c
Expand Up @@ -162,7 +162,7 @@ int lxc_cmd_connect(const char *name, const char *lxcpath,
const char *hashed_sock_name, const char *suffix)
{
int ret, client_fd;
char path[sizeof(((struct sockaddr_un *)0)->sun_path)] = {0};
char path[LXC_AUDS_ADDR_LEN] = {0};
char *offset = &path[1];

/* -2 here because this is an abstract unix socket so it needs a
Expand Down
35 changes: 35 additions & 0 deletions src/lxc/compiler.h
@@ -0,0 +1,35 @@
/* liblxcapi
*
* Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>.
* Copyright © 2018 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef __LXC_COMPILER_H
#define __LXC_COMPILER_H

#include "config.h"

#ifndef thread_local
#if __STDC_VERSION__ >= 201112L && \
!(defined(__STDC_NO_THREADS__) || \
(defined(__GNU_LIBRARY__) && __GLIBC__ == 2 && __GLIBC_MINOR__ < 16))
#define thread_local _Thread_local
#else
#define thread_local __thread
#endif
#endif

#endif /* __LXC_COMPILER_H */
6 changes: 3 additions & 3 deletions src/lxc/conf.c
Expand Up @@ -125,7 +125,7 @@ lxc_log_define(conf, lxc);
* This is used in the error calls.
*/
#ifdef HAVE_TLS
__thread struct lxc_conf *current_config;
thread_local struct lxc_conf *current_config;
#else
struct lxc_conf *current_config;
#endif
Expand Down Expand Up @@ -2952,15 +2952,15 @@ int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
* +
* strlen(" ") = 1
* +
* LXC_NUMSTRLEN64
* INTTYPE_TO_STRLEN(uint32_t)
* +
* strlen(" ") = 1
*
* We add some additional space to make sure that we really have
* LXC_IDMAPLEN bytes available for our the {g,u]id mapping.
*/
int ret = 0, gidmap = 0, uidmap = 0;
char mapbuf[9 + 1 + LXC_NUMSTRLEN64 + 1 + LXC_IDMAPLEN] = {0};
char mapbuf[9 + 1 + INTTYPE_TO_STRLEN(uint32_t) + 1 + LXC_IDMAPLEN] = {0};
bool had_entry = false, use_shadow = false;
int hostuid, hostgid;

Expand Down
3 changes: 2 additions & 1 deletion src/lxc/conf.h
Expand Up @@ -38,6 +38,7 @@
#include <sys/resource.h>
#endif

#include "compiler.h"
#include "list.h"
#include "ringbuf.h"
#include "start.h" /* for lxc_handler */
Expand Down Expand Up @@ -395,7 +396,7 @@ extern int write_id_mapping(enum idtype idtype, pid_t pid, const char *buf,
size_t buf_size);

#ifdef HAVE_TLS
extern __thread struct lxc_conf *current_config;
extern thread_local struct lxc_conf *current_config;
#else
extern struct lxc_conf *current_config;
#endif
Expand Down
16 changes: 7 additions & 9 deletions src/lxc/confile.c
Expand Up @@ -3236,19 +3236,19 @@ static int get_config_idmaps(const char *key, char *retv, int inlen,
* +
* sizeof(" ")
* +
* sizeof(uint64_t)
* sizeof(uint32_t)
* +
* sizeof(" ")
* +
* sizeof(uint64_t)
* sizeof(uint32_t)
* +
* sizeof(" ")
* +
* sizeof(uint64_t)
* sizeof(uint32_t)
* +
* \0
*/
#define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
#define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
char buf[__LXC_IDMAP_STR_BUF];

if (!retv)
Expand All @@ -3257,8 +3257,7 @@ static int get_config_idmaps(const char *key, char *retv, int inlen,
memset(retv, 0, inlen);

listlen = lxc_list_len(&c->id_map);
lxc_list_for_each(it, &c->id_map)
{
lxc_list_for_each(it, &c->id_map) {
struct id_map *map = it->elem;
ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu",
(map->idtype == ID_TYPE_UID) ? 'u' : 'g',
Expand Down Expand Up @@ -3706,9 +3705,8 @@ static int get_config_prlimit(const char *key, char *retv, int inlen,
return -1;

lxc_list_for_each(it, &c->limits) {
char buf[LXC_NUMSTRLEN64 * 2 + 2]; /* 2 colon separated 64 bit
integers or the word
'unlimited' */
/* 2 colon separated 64 bit integers or the word 'unlimited' */
char buf[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
int partlen;
struct lxc_limit *lim = it->elem;

Expand Down
5 changes: 3 additions & 2 deletions src/lxc/initutils.c
Expand Up @@ -28,6 +28,7 @@
#include "initutils.h"
#include "log.h"
#include "macro.h"
#include "compiler.h"

#ifndef HAVE_STRLCPY
#include "include/strlcpy.h"
Expand Down Expand Up @@ -72,9 +73,9 @@ const char *lxc_global_config_value(const char *option_name)

/* placed in the thread local storage pool for non-bionic targets */
#ifdef HAVE_TLS
static __thread const char *values[sizeof(options) / sizeof(options[0])] = { 0 };
static thread_local const char *values[sizeof(options) / sizeof(options[0])] = {0};
#else
static const char *values[sizeof(options) / sizeof(options[0])] = { 0 };
static const char *values[sizeof(options) / sizeof(options[0])] = {0};
#endif

/* user_config_path is freed as soon as it is used */
Expand Down
8 changes: 4 additions & 4 deletions src/lxc/log.c
Expand Up @@ -53,7 +53,7 @@
* datatype is currently at maximum a 64bit integer, we have a date string that
* is of maximum length (2^64 - 1) * 2 = (21 + 21) = 42.
*/
#define LXC_LOG_TIME_SIZE ((LXC_NUMSTRLEN64)*2)
#define LXC_LOG_TIME_SIZE ((INTTYPE_TO_STRLEN(uint64_t)) * 2)

int lxc_log_fd = -1;
static int syslog_enable = 0;
Expand Down Expand Up @@ -170,7 +170,7 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
{
int64_t epoch_to_days, z, era, doe, yoe, year, doy, mp, day, month,
d_in_s, hours, h_in_s, minutes, seconds;
char nanosec[LXC_NUMSTRLEN64];
char nanosec[INTTYPE_TO_STRLEN(int64_t)];
int ret;

/* See https://howardhinnant.github.io/date_algorithms.html for an
Expand Down Expand Up @@ -247,8 +247,8 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
seconds = (time->tv_sec - d_in_s - h_in_s - (minutes * 60));

/* Make string from nanoseconds. */
ret = snprintf(nanosec, LXC_NUMSTRLEN64, "%"PRId64, (int64_t)time->tv_nsec);
if (ret < 0 || ret >= LXC_NUMSTRLEN64)
ret = snprintf(nanosec, INTTYPE_TO_STRLEN(int64_t), "%"PRId64, (int64_t)time->tv_nsec);
if (ret < 0 || ret >= INTTYPE_TO_STRLEN(int64_t))
return -1;

/* Create final timestamp for the log and shorten nanoseconds to 3
Expand Down
3 changes: 1 addition & 2 deletions src/lxc/lsm/lsm.h
Expand Up @@ -28,10 +28,9 @@ struct lxc_conf;

#include <sys/types.h>

#include "macro.h"
#include "utils.h"

#define LXC_LSMATTRLEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)

struct lsm_drv {
const char *name;

Expand Down
6 changes: 3 additions & 3 deletions src/lxc/lxccontainer.c
Expand Up @@ -1037,10 +1037,10 @@ static bool do_lxcapi_start(struct lxc_container *c, int useinit, char * const a
*/
if (c->pidfile) {
int ret, w;
char pidstr[LXC_NUMSTRLEN64];
char pidstr[INTTYPE_TO_STRLEN(int)];

w = snprintf(pidstr, LXC_NUMSTRLEN64, "%d", (int)lxc_raw_getpid());
if (w < 0 || (size_t)w >= LXC_NUMSTRLEN64) {
w = snprintf(pidstr, INTTYPE_TO_STRLEN(int), "%d", (int)lxc_raw_getpid());
if (w < 0 || (size_t)w >= INTTYPE_TO_STRLEN(int)) {
free_init_cmd(init_cmd);
lxc_free_handler(handler);

Expand Down

0 comments on commit f288e10

Please sign in to comment.