From 7520e4ff572171220198300682f021b9657fa564 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 11 Nov 2018 14:38:09 +0100 Subject: [PATCH] MINOR: namespaces: don't build namespace.c if disabled When namespaces are disabled, support is still reported because the file is built with almost nothing in it but built anyway. Instead of extending the scope of the numerous ifdefs in this file, better avoid building it when namespaces are diabled. In this case we define my_socketat() as an inline function mapping directly to socket(). The struct netns_entry still needs to be defined because it's used by various other functions in the code. --- Makefile | 3 ++- include/common/namespace.h | 14 +++++++++++--- src/namespace.c | 9 +-------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 3b089d227c..965d89d66c 100644 --- a/Makefile +++ b/Makefile @@ -871,6 +871,7 @@ endif ifneq ($(USE_NS),) OPTIONS_CFLAGS += -DCONFIG_HAP_NS BUILD_OPTIONS += $(call ignore_implicit,USE_NS) +OPTIONS_OBJS += src/namespace.o endif #### Global link options @@ -916,7 +917,7 @@ OBJS = src/proto_http.o src/cfgparse.o src/server.o src/stream.o \ src/hpack-dec.o src/memory.o src/lb_fwrr.o src/lb_chash.o \ src/lb_fas.o src/hathreads.o src/chunk.o src/lb_map.o \ src/xxhash.o src/regex.o src/shctx.o src/buffer.o src/action.o \ - src/h1.o src/compression.o src/pipe.o src/namespace.o \ + src/h1.o src/compression.o src/pipe.o \ src/sha1.o src/hpack-tbl.o src/hpack-enc.o src/uri_auth.o \ src/time.o src/proto_udp.o src/arg.o src/signal.o \ src/protocol.o src/lru.o src/hdr_idx.o src/hpack-huff.o \ diff --git a/include/common/namespace.h b/include/common/namespace.h index fa4185db61..7aacff9681 100644 --- a/include/common/namespace.h +++ b/include/common/namespace.h @@ -4,9 +4,6 @@ #include #include -struct netns_entry; -int my_socketat(const struct netns_entry *ns, int domain, int type, int protocol); - #ifdef CONFIG_HAP_NS struct netns_entry @@ -16,10 +13,21 @@ struct netns_entry int fd; }; +int my_socketat(const struct netns_entry *ns, int domain, int type, int protocol); struct netns_entry* netns_store_insert(const char *ns_name); const struct netns_entry* netns_store_lookup(const char *ns_name, size_t ns_name_len); int netns_init(void); + +#else /* no namespace support */ + +struct netns_entry; + +static inline int my_socketat(const struct netns_entry *ns, int domain, int type, int protocol) +{ + return socket(domain, type, protocol); +} + #endif /* CONFIG_HAP_NS */ #endif /* _NAMESPACE_H */ diff --git a/src/namespace.c b/src/namespace.c index 93d92480dd..72f5079ebd 100644 --- a/src/namespace.c +++ b/src/namespace.c @@ -17,8 +17,6 @@ #include #include -#ifdef CONFIG_HAP_NS - /* Opens the namespace and returns the FD or -1 in case of error * (check errno). */ @@ -88,7 +86,6 @@ const struct netns_entry* netns_store_lookup(const char *ns_name, size_t ns_name else return NULL; } -#endif /* Opens a socket in the namespace described by with the parameters , * and and returns the FD or -1 in case of error (check errno). @@ -97,19 +94,15 @@ int my_socketat(const struct netns_entry *ns, int domain, int type, int protocol { int sock; -#ifdef CONFIG_HAP_NS if (default_namespace >= 0 && ns && setns(ns->fd, CLONE_NEWNET) == -1) return -1; -#endif + sock = socket(domain, type, protocol); -#ifdef CONFIG_HAP_NS if (default_namespace >= 0 && ns && setns(default_namespace, CLONE_NEWNET) == -1) { close(sock); return -1; } -#endif - return sock; }