From patchwork Sat Jul 16 00:29:41 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Eric W. Biederman" X-Patchwork-Id: 104924 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id EC315B6F68 for ; Sat, 16 Jul 2011 10:29:50 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751772Ab1GPA3q (ORCPT ); Fri, 15 Jul 2011 20:29:46 -0400 Received: from out02.mta.xmission.com ([166.70.13.232]:56751 "EHLO out02.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751578Ab1GPA3p (ORCPT ); Fri, 15 Jul 2011 20:29:45 -0400 Received: from in02.mta.xmission.com ([166.70.13.52]) by out02.mta.xmission.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1QhslN-00082w-7D; Fri, 15 Jul 2011 18:29:45 -0600 Received: from c-98-207-153-68.hsd1.ca.comcast.net ([98.207.153.68] helo=fess.ebiederm.org) by in02.mta.xmission.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1QhslM-0006PA-LK; Fri, 15 Jul 2011 18:29:45 -0600 Received: from fess.ebiederm.org (localhost [127.0.0.1]) by fess.ebiederm.org (8.14.3/8.14.3/Debian-9.1ubuntu1) with ESMTP id p6G0TgiG025560; Fri, 15 Jul 2011 17:29:42 -0700 Received: (from eric@localhost) by fess.ebiederm.org (8.14.3/8.14.3/Submit) id p6G0TgTT025558; Fri, 15 Jul 2011 17:29:42 -0700 From: ebiederm@xmission.com (Eric W. Biederman) To: Stephen Hemminger Cc: Date: Fri, 15 Jul 2011 17:29:41 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-XM-SPF: eid=; ; ; mid=; ; ; hst=in02.mta.xmission.com; ; ; ip=98.207.153.68; ; ; frm=ebiederm@xmission.com; ; ; spf=neutral X-XM-AID: U2FsdGVkX18ezzMlyAVz0CuhFtTn2BgNRcKe7OCDVfo= X-SA-Exim-Connect-IP: 98.207.153.68 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on sa05.xmission.com X-Spam-Level: X-Spam-Status: No, score=-3.6 required=8.0 tests=ALL_TRUSTED,BAYES_00, DCC_CHECK_NEGATIVE,UNTRUSTED_Relay autolearn=disabled version=3.3.1 X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * -3.0 BAYES_00 BODY: Bayes spam probability is 0 to 1% * [score: 0.0000] * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa05 1397; Body=1 Fuz1=1 Fuz2=1] * 0.4 UNTRUSTED_Relay Comes from a non-trusted relay X-Spam-DCC: XMission; sa05 1397; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: ;Stephen Hemminger X-Spam-Relay-Country: Subject: [PATCH] iproute2: Fail "ip netns add" on existing network namespaces. X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Fri, 06 Aug 2010 16:31:04 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Use O_EXCL so that we only create and mount a new network namespace if there is no chance an existing network namespace is present. Signed-off-by: Eric W. Biederman --- ip/ipnetns.c | 17 +++++------------ 1 files changed, 5 insertions(+), 12 deletions(-) diff --git a/ip/ipnetns.c b/ip/ipnetns.c index dff3497..e41a598 100644 --- a/ip/ipnetns.c +++ b/ip/ipnetns.c @@ -41,16 +41,6 @@ static int setns(int fd, int nstype) #endif /* HAVE_SETNS */ -static int touch(const char *path, mode_t mode) -{ - int fd; - fd = open(path, O_RDONLY|O_CREAT, mode); - if (fd < 0) - return -1; - close(fd); - return 0; -} - static void usage(void) __attribute__((noreturn)); static void usage(void) @@ -214,6 +204,7 @@ static int netns_add(int argc, char **argv) */ char netns_path[MAXPATHLEN]; const char *name; + int fd; if (argc < 1) { fprintf(stderr, "No netns name specified\n"); @@ -227,11 +218,13 @@ static int netns_add(int argc, char **argv) mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); /* Create the filesystem state */ - if (touch(netns_path, 0) < 0) { + fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0); + if (fd < 0) { fprintf(stderr, "Could not create %s: %s\n", netns_path, strerror(errno)); - goto out_delete; + return -1; } + close(fd); if (unshare(CLONE_NEWNET) < 0) { fprintf(stderr, "Failed to create a new network namespace: %s\n", strerror(errno));