This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
pespin gerrit-no-reply at lists.osmocom.orgpespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-ggsn/+/17254 ) Change subject: netns: Improve error checking ...................................................................... netns: Improve error checking Change-Id: I9b9c8fd6eeaaa7d190b8e2a34ca82088904c7708 --- M lib/netns.c M lib/netns.h M sgsnemu/sgsnemu.c 3 files changed, 52 insertions(+), 20 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-ggsn refs/changes/54/17254/1 diff --git a/lib/netns.c b/lib/netns.c index 6734b5d..442c23c 100644 --- a/lib/netns.c +++ b/lib/netns.c @@ -56,11 +56,13 @@ return setns(nsfd, CLONE_NEWNET); } -void restore_ns(sigset_t *oldmask) +int restore_ns(sigset_t *oldmask) { - setns(default_nsfd, CLONE_NEWNET); + int rc; + if ((rc = setns(default_nsfd, CLONE_NEWNET)) < 0) + return rc; - sigprocmask(SIG_SETMASK, oldmask, NULL); + return sigprocmask(SIG_SETMASK, oldmask, NULL); } int open_ns(int nsfd, const char *pathname, int flags) @@ -103,12 +105,11 @@ return sk; } -void init_netns() +int init_netns() { - if ((default_nsfd = open("/proc/self/ns/net", O_RDONLY)) < 0) { - perror("init_netns"); - exit(EXIT_FAILURE); - } + if ((default_nsfd = open("/proc/self/ns/net", O_RDONLY)) < 0) + return default_nsfd; + return 0; } int get_nsfd(const char *name) @@ -137,7 +138,8 @@ unshare(CLONE_NEWNET); mount("/proc/self/ns/net", path, "none", MS_BIND, NULL); - setns(default_nsfd, CLONE_NEWNET); + if ((r = setns(default_nsfd, CLONE_NEWNET)) < 0) + return r; sigprocmask(SIG_SETMASK, &oldmask, NULL); diff --git a/lib/netns.h b/lib/netns.h index 168e44f..3b91ba3 100644 --- a/lib/netns.h +++ b/lib/netns.h @@ -21,10 +21,10 @@ #if defined(__linux__) -void init_netns(void); +int init_netns(void); int switch_ns(int nsfd, sigset_t *oldmask); -void restore_ns(sigset_t *oldmask); +int restore_ns(sigset_t *oldmask); int open_ns(int nsfd, const char *pathname, int flags); int socket_ns(int nsfd, int domain, int type, int protocol); diff --git a/sgsnemu/sgsnemu.c b/sgsnemu/sgsnemu.c index 2c0ce1b..f105d0a 100644 --- a/sgsnemu/sgsnemu.c +++ b/sgsnemu/sgsnemu.c @@ -1328,14 +1328,22 @@ sigset_t oldmask; if ((options.netns)) { - switch_ns(netns, &oldmask); + if (switch_ns(netns, &oldmask) < 0) { + SYS_ERR(DSGSN, LOGL_ERROR, 0, + "Failed to switch to netns %s: %s\n", + options.netns, strerror(errno)); + } } #endif tun_runscript(tun, options.ipdown); #if defined(__linux__) if ((options.netns)) { - restore_ns(&oldmask); + if (restore_ns(&oldmask) < 0) { + SYS_ERR(DSGSN, LOGL_ERROR, 0, + "Failed to switch to original netns: %s\n", + strerror(errno)); + } } #endif } @@ -1458,7 +1466,11 @@ #if defined(__linux__) if ((options.createif) && (options.netns)) { - switch_ns(netns, &oldmask); + if (switch_ns(netns, &oldmask) < 0) { + SYS_ERR(DSGSN, LOGL_ERROR, 0, + "Failed to switch to netns %s: %s\n", + options.netns, strerror(errno)); + } } #endif @@ -1504,7 +1516,10 @@ #if defined(__linux__) if ((options.createif) && (options.netns)) { - restore_ns(&oldmask); + if (restore_ns(&oldmask) < 0) { + SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to switch to original netns: %s\n", + strerror(errno)); + } } #endif @@ -1572,7 +1587,7 @@ fd_set fds; /* For select() */ struct timeval idleTime; /* How long to select() */ struct pdp_t *pdp; - int n; + int n, rc; int starttime = time(NULL); /* Time program was started */ int stoptime = 0; /* Time to exit */ int pingtimeout = 0; /* Time to print ping statistics */ @@ -1594,7 +1609,10 @@ osmo_init_logging2(tall_sgsnemu_ctx, &log_info); #if defined(__linux__) - init_netns(); + if ((rc = init_netns()) < 0) { + SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to initialize netns: %s", strerror(errno)); + exit(1); + } #endif /* Process options given in configuration file and command line */ @@ -1622,8 +1640,16 @@ #if defined(__linux__) if ((options.createif) && (options.netns)) { - netns = get_nsfd(options.netns); - switch_ns(netns, &oldmask); + if ((netns = get_nsfd(options.netns)) < 0) { + SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to obtain fd for netns %s: %s\n", + options.netns, strerror(errno)); + exit(1); + } + if (switch_ns(netns, &oldmask) < 0) { + SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to switch to netns %s: %s\n", + options.netns, strerror(errno)); + exit(1); + } } #endif @@ -1654,7 +1680,11 @@ #if defined(__linux__) if ((options.createif) && (options.netns)) { - restore_ns(&oldmask); + if ((rc = restore_ns(&oldmask)) < 0) { + SYS_ERR(DSGSN, LOGL_ERROR, 0, "Failed to switch to original netns: %s\n", + strerror(errno)); + exit(1); + } } #endif -- To view, visit https://gerrit.osmocom.org/c/osmo-ggsn/+/17254 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: osmo-ggsn Gerrit-Branch: master Gerrit-Change-Id: I9b9c8fd6eeaaa7d190b8e2a34ca82088904c7708 Gerrit-Change-Number: 17254 Gerrit-PatchSet: 1 Gerrit-Owner: pespin <pespin at sysmocom.de> Gerrit-MessageType: newchange -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200225/3aff348a/attachment.htm>