From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17873 invoked by alias); 17 Apr 2015 14:52:49 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 17864 invoked by uid 89); 17 Apr 2015 14:52:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,SPF_HELO_PASS,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 17 Apr 2015 14:52:48 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t3HEqkHR006539 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 17 Apr 2015 10:52:47 -0400 Received: from [127.0.0.1] (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t3HEqjQP004848; Fri, 17 Apr 2015 10:52:46 -0400 Message-ID: <55311E3D.1090502@redhat.com> Date: Fri, 17 Apr 2015 14:52:00 -0000 From: Pedro Alves User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: Gary Benson , gdb-patches@sourceware.org Subject: Re: [PATCH 3/7] Introduce nat/linux-namespaces.[ch] References: <1429186791-6867-1-git-send-email-gbenson@redhat.com> <1429186791-6867-4-git-send-email-gbenson@redhat.com> In-Reply-To: <1429186791-6867-4-git-send-email-gbenson@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-SW-Source: 2015-04/txt/msg00679.txt.bz2 On 04/16/2015 01:19 PM, Gary Benson wrote: > + path = linux_ns_path_for (pid1, type); > + if (stat (path, &sb) != 0) > + { > + int saved_errno; > + > + if (pid1 == getpid ()) > + { > + /* Assume the kernel does not support TYPE namespaces. */ > + return 1; > + } > + > + saved_errno = errno; Don't assume any function preserves errno. Save this right after stat. > + path = linux_ns_path_for (getpid (), type); > + if (stat (path, &sb) != 0) > + { > + /* Assume the kernel does not support TYPE namespaces. */ > + return 1; > + } > + > + /* We can open our own TYPE namespace but not that for process > + PID. The process might have died, or we might not have the > + right permissions (though we should be attached by this time > + so this seems unlikely). In any event, we cannot make any > + decisions and must throw. */ > + errno = saved_errno; > + perror_with_name (linux_ns_path_for (pid1, type)); > + } > + pid1_id = sb.st_ino; > + > + /* The kernel definitely supports TYPE namespaces so we cannot > + make any decisions if this stat fails. */ > + path = linux_ns_path_for (pid2, type); > + if (stat (path, &sb) != 0) > + perror_with_name (path); > + > + return sb.st_ino == pid1_id; > +} > + > +/* Helper function which does the work for make_cleanup_setns. */ > + > +static void > +do_setns_cleanup (void *arg) > +{ > + int *fd = arg; > + > + if (setns (*fd, 0) != 0) > + internal_error (__FILE__, __LINE__, > + _("unable to restore namespace: %s"), > + safe_strerror (errno)); And here if (setns (*fd, 0) != 0) { int saved_errno = errno; internal_error (__FILE__, __LINE__, _("unable to restore namespace: %s"), safe_strerror (saved_errno)); } because _() is a function call, and you can't rely on whether safe_strerror is called before or after. > +/* Enter the TYPE namespace of process PID and call FUNC with the > + argument ARG, returning to the original TYPE namespace afterwards. > + If process PID has the same TYPE namespace as the current process, > + or if TYPE namespaces are not supported, just call FUNC with ARG. > + Return nonzero if FUNC was called, zero otherwise (and set ERRNO). */ > + > +extern int linux_ns_enter (int pid, const char *type, > + void (*func) (void *), void *arg); So the function: #1 - enters the namespace #2 - calls func #3 - exits the namespace. IMO, "linux_ns_ENTER" isn't a good name for that. I'd expect that a function called "enter" do just #1 above. Something like "linux_ns_do", "linux_do_in_ns", "linux_in_ns", etc., would be clearer, IMO. Thanks, Pedro Alves