From patchwork Tue Jul 24 17:26:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lubomir Rintel X-Patchwork-Id: 948673 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=v3.sk Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 41ZlmY6HsTz9s0R for ; Wed, 25 Jul 2018 03:32:29 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388495AbeGXSj7 (ORCPT ); Tue, 24 Jul 2018 14:39:59 -0400 Received: from shell.v3.sk ([90.176.6.54]:35370 "EHLO shell.v3.sk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388445AbeGXSj6 (ORCPT ); Tue, 24 Jul 2018 14:39:58 -0400 X-Greylist: delayed 335 seconds by postgrey-1.27 at vger.kernel.org; Tue, 24 Jul 2018 14:39:57 EDT Received: from localhost (localhost [127.0.0.1]) by zimbra.v3.sk (Postfix) with ESMTP id DDCADB0FF9; Tue, 24 Jul 2018 19:26:50 +0200 (CEST) Received: from shell.v3.sk ([127.0.0.1]) by localhost (zimbra.v3.sk [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id mQTRU5hhIQPK; Tue, 24 Jul 2018 19:26:48 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by zimbra.v3.sk (Postfix) with ESMTP id D6841B0FF8; Tue, 24 Jul 2018 19:26:47 +0200 (CEST) X-Virus-Scanned: amavisd-new at zimbra.v3.sk Received: from shell.v3.sk ([127.0.0.1]) by localhost (zimbra.v3.sk [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id s-4AXoeMzVxb; Tue, 24 Jul 2018 19:26:47 +0200 (CEST) Received: from belphegor.brq.redhat.com (nat-pool-brq-t.redhat.com [213.175.37.10]) by zimbra.v3.sk (Postfix) with ESMTPSA id 54324B0FF7; Tue, 24 Jul 2018 19:26:47 +0200 (CEST) From: Lubomir Rintel To: netdev@vger.kernel.org Cc: Stephen Hemminger , Phil Sutter , Lubomir Rintel Subject: [iproute PATCH] lib/namespace: avoid double-mounting a /sys Date: Tue, 24 Jul 2018 19:26:38 +0200 Message-Id: <20180724172638.760873-1-lkundrak@v3.sk> X-Mailer: git-send-email 2.17.1 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This partly reverts 8f0807023d067e2bb585a2ae8da93e59689d10f1, bringing back the umount(/sys) attempt. In a LXC container we're unable to umount the sysfs instance, nor mount a read-write one. We still are able to create a new read-only instance. Nevertheless, it still makes sense to attempt the umount() even though the sysfs is mounted read-only. Otherwise we may end up attempting to mount a sysfs with the same flags as is already mounted, resulting in an EBUSY error (meaning "Already mounted"). Perhaps this is not a very likely scenario in real world, but we hit it in NetworkManager test suite and makes netns_switch() somewhat more robust. It also fixes the case, when /sys wasn't mounted at all. Signed-off-by: Lubomir Rintel --- lib/namespace.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/namespace.c b/lib/namespace.c index 43e0fe34..06ae0a48 100644 --- a/lib/namespace.c +++ b/lib/namespace.c @@ -82,19 +82,13 @@ int netns_switch(char *name) /* Mount a version of /sys that describes the network namespace */ - if (statvfs("/sys", &fsstat) < 0) { - fprintf(stderr, "could not stat /sys (not mounted?): %s\n",strerror(errno)); - return -1; - } - if (fsstat.f_flag & ST_RDONLY) { - /* If /sys is not writable (e.g. in a container), we can't - * unmount the old /sys instance, but we can still mount a new - * read-only instance over it. */ - mountflags = MS_RDONLY; - } else { - if (umount2("/sys", MNT_DETACH) < 0) { - fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno)); - return -1; + if (umount2("/sys", MNT_DETACH) < 0) { + /* If this fails, perhaps there wasn't a sysfs instance mounted. Good. */ + if (statvfs("/sys", &fsstat) == 0) { + /* We couldn't umount the sysfs, we'll attempt to overlay it. + * A read-only instance can't be shadowed with a read-write one. */ + if (fsstat.f_flag & ST_RDONLY) + mountflags = MS_RDONLY; } } if (mount(name, "/sys", "sysfs", mountflags, NULL) < 0) {