From 4c048c208645d470a040ae5b7fcbfff2ac3f8e03 Mon Sep 17 00:00:00 2001 From: Andrew Vagin Date: Wed, 19 Nov 2014 17:49:37 +0400 Subject: [PATCH] libct: add ability to use one library with OpenVZ and upstream kernels Signed-off-by: Andrey Vagin --- Makefile | 7 ------- src/include/net.h | 3 +++ src/include/vz/vz_net.h | 1 + src/net.c | 13 +++++++------ src/session.c | 9 ++++----- src/vz/vz.c | 2 +- src/vz/vz_net.c | 5 +++++ 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index dfacb1d..1652f17 100644 --- a/Makefile +++ b/Makefile @@ -65,9 +65,6 @@ cflags-y += -iquote src/lsm cflags-y += -iquote src cflags-y += -fno-strict-aliasing cflags-y += -I/usr/include -ifeq ($(VZ),1) - cflags-y += -I/usr/src/kernels/$(shell uname -r)/include/ -endif export cflags-y VERSION_MAJOR := 0 @@ -109,10 +106,6 @@ endif CFLAGS += $(WARNINGS) $(DEFINES) -ifneq ("$(wildcard /proc/vz)","") - CFLAGS += -D VZ -endif - export E Q CC ECHO MAKE CFLAGS LIBS ARCH DEFINES MAKEFLAGS export SH RM OBJCOPY LDARCH LD CP MKDIR CD LN export ESED SED CAT diff --git a/src/include/net.h b/src/include/net.h index 20c61e4..0346e3e 100644 --- a/src/include/net.h +++ b/src/include/net.h @@ -8,6 +8,9 @@ struct _NetaddReq; struct container; +typedef const struct ct_net_ops *(*net_get_ops_cb)(enum ct_net_type ntype); +extern ct_net_t __local_net_add(ct_handler_t h, enum ct_net_type, void *arg, net_get_ops_cb cb); + extern ct_net_t local_net_add(ct_handler_t h, enum ct_net_type, void *arg); extern int local_net_del(ct_handler_t h, enum ct_net_type, void *arg); extern void net_release(struct container *ct); diff --git a/src/include/vz/vz_net.h b/src/include/vz/vz_net.h index c6a8d6c..ec9e4fc 100644 --- a/src/include/vz/vz_net.h +++ b/src/include/vz/vz_net.h @@ -4,5 +4,6 @@ #include "net.h" extern const struct ct_net_ops *vz_net_get_ops(enum ct_net_type); +extern ct_net_t vz_net_add(ct_handler_t h, enum ct_net_type ntype, void *arg); #endif /* __LIBCT_VZ_NET_H__ */ diff --git a/src/net.c b/src/net.c index 3f88bd1..f4e2258 100644 --- a/src/net.c +++ b/src/net.c @@ -97,7 +97,7 @@ static int local_net_link_apply(char *name, ct_net_t n, int pid) return ret; } -ct_net_t local_net_add(ct_handler_t h, enum ct_net_type ntype, void *arg) +ct_net_t __local_net_add(ct_handler_t h, enum ct_net_type ntype, void *arg, const struct ct_net_ops *(*get_ops_cb)(enum ct_net_type ntype)) { struct container *ct = cth2ct(h); const struct ct_net_ops *nops; @@ -113,11 +113,7 @@ ct_net_t local_net_add(ct_handler_t h, enum ct_net_type ntype, void *arg) if (ntype == CT_NET_NONE) return 0; -#ifndef VZ - nops = net_get_ops(ntype); -#else - nops = vz_net_get_ops(ntype); -#endif + nops = get_ops_cb(ntype); if (!nops) return ERR_PTR(-LCTERR_BADTYPE); @@ -129,6 +125,11 @@ ct_net_t local_net_add(ct_handler_t h, enum ct_net_type ntype, void *arg) return cn; } +ct_net_t local_net_add(ct_handler_t h, enum ct_net_type ntype, void *arg) +{ + return __local_net_add(h, ntype, arg, net_get_ops); +} + int local_net_del(ct_handler_t h, enum ct_net_type ntype, void *arg) { struct container *ct = cth2ct(h); diff --git a/src/session.c b/src/session.c index 26af8cc..5e256fc 100644 --- a/src/session.c +++ b/src/session.c @@ -109,11 +109,10 @@ libct_session_t libct_session_open_local(void) if (s) { INIT_LIST_HEAD(&s->s.s_cts); INIT_LIST_HEAD(&s->s.async_list); -#ifndef VZ - s->s.ops = &local_session_ops; -#else - s->s.ops = &vz_session_ops; -#endif + if (!access("/proc/vz", F_OK)) + s->s.ops = &vz_session_ops; + else + s->s.ops = &local_session_ops; return &s->s; } diff --git a/src/vz/vz.c b/src/vz/vz.c index 53c7b1d..8ab3772 100644 --- a/src/vz/vz.c +++ b/src/vz/vz.c @@ -1423,7 +1423,7 @@ static const struct container_ops vz_ct_ops = { .get_state = vz_get_state, .set_option = vz_set_option, .set_console_fd = vz_set_console_fd, - .net_add = local_net_add, + .net_add = vz_net_add, .net_del = local_net_del, .net_route_add = local_net_route_add, .uname = vz_uname, diff --git a/src/vz/vz_net.c b/src/vz/vz_net.c index b58a6fe..985a5e2 100644 --- a/src/vz/vz_net.c +++ b/src/vz/vz_net.c @@ -177,6 +177,11 @@ const struct ct_net_ops *vz_net_get_ops(enum ct_net_type ntype) } } +ct_net_t vz_net_add(ct_handler_t h, enum ct_net_type ntype, void *arg) +{ + return __local_net_add(h, ntype, arg, vz_net_get_ops); +} + struct ct_net_veth *cn2vn(struct ct_net *n) { return container_of(n, struct ct_net_veth, n);