From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 105553 invoked by alias); 17 Apr 2015 17:09:37 -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 105525 invoked by uid 89); 17 Apr 2015 17:09:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS,T_RP_MATCHES_RCVD autolearn=ham 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 17:09:34 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t3HH9XuM002973 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 17 Apr 2015 13:09:33 -0400 Received: from [127.0.0.1] (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t3HH9VU3014645; Fri, 17 Apr 2015 13:09:32 -0400 Message-ID: <55313E4B.4010803@redhat.com> Date: Fri, 17 Apr 2015 17:09: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 CC: gdb-patches@sourceware.org Subject: Re: [PATCH 7/7] Implement vFile:setfs in gdbserver References: <1429186791-6867-1-git-send-email-gbenson@redhat.com> <1429186791-6867-8-git-send-email-gbenson@redhat.com> <553126FA.8030402@redhat.com> <20150417160524.GC14618@blade.nx> <20150417162944.GA16766@blade.nx> In-Reply-To: <20150417162944.GA16766@blade.nx> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-SW-Source: 2015-04/txt/msg00697.txt.bz2 On 04/17/2015 05:29 PM, Gary Benson wrote: > Gary Benson wrote: >> Pedro Alves wrote: >>> So back on linux_ns_enter's naming --- >>> >>> I find the abstraction here a bit odd. It seems to be me that >>> a function that does: >>> >>> #1 - select filesystem/namespace >>> #2 - call foo >>> #3 - restore filesystem/namespace >>> >>> should be a function in common code, and that the only >>> target-specific part is selecting a filesystem/namespace. >>> That is, simplifying, something like: >>> >>> /* Cause the filesystem to appear as it does to process PID and >>> call FUNC with argument ARG, restoring the filesystem to its >>> original state afterwards. Return nonzero if FUNC was called, >>> zero otherwise (and set ERRNO). */ >>> >>> int >>> call_with_fs_of (int pid, void (*func) (void *), void *arg) >>> { >>> int ret; >>> >>> target->select_fs (pid); >>> ret = func (); >>> target->select_fs (0); >>> return ret; >>> } >>> >>> Was there a reason this wasn't done that way? >>> >>> (maybe make target->select_fs() return the previous >>> namespace, instead of passing 0 on the second >>> call.) >> >> I'll have a go at doing it that way. > > It is kind of uglier doing it that way. It would need to look > something more like this: > > struct cleanup **old_chain; > > if (target->select_fs (pid, &old_chain) != 0) > return failure_return_value; > ret = func (); > do_cleanups (old_chain); > return ret; > > linux_ns_enter has to open file descriptors for its own namespace > and the one it wants to enter. Its own namespace file descriptor > is what it needs to return, and it has to be held open: you can't > flip namespace and then open your own descriptor to flip back. > So the thing target->select_fs would have to return would be an > open file descriptor (to then pass to target->restore_fs, which > would take a file descriptor argument rather than an PID) but > that would be putting a Linuxism into the target vector. Seems to me we can fix that by returning an opaque closure instead then. I don't think that's ugly at all. E.g.,: int call_with_fs_of (int pid, void (*func) (void *), void *arg) { int ret = failure_return_value; restore_token = target->select_fs (pid); if (restore_token == NULL) return failure_return_value; TRY { ret = func (); } CATCH (ex, RETURN_MASK_ALL) { target->restore_fs (restore_token); throw_exception (ex); } END_CATCH return ret; } The reason I think this target interface is better is that it also allows open coding the select_fs/restore_fs calls. Which in turn would allow getting rid of all those closures in the other patch. For example, like this: /* On exception, restore the filesystem, and rethrow. */ #define CATCH_RESTORE_FS(restore_token) \ CATCH (ex, RETURN_MASK_ALL) \ { \ linux_restore_namespace (restore_token); \ throw_exception (ex); \ } \ END_CATCH static int linux_nat_fileio_unlink (struct target_ops *ops, const char *filename, int *target_errno) { void *restore_token; int ret = -1; restore_token = linux_nat_enter_fs (); TRY { ret = super_fileio_unlink (ops, filename, target_errno); } CATCH_RESTORE_FS(restore_token) return ret; } static char * linux_nat_fileio_readlink (struct target_ops *ops, const char *filename, int *target_errno) { void *restore_token; char *ret = NULL; restore_token = linux_nat_enter_fs (); TRY { ret = super_fileio_readlink (ops, filename, target_errno); } CATCH_RESTORE_FS(restore_token) return ret; } etc., which I find _much_ more readable than: +/* Arguments and return value of to_fileio_unlink. */ + +struct unlink_closure +{ + struct target_ops *ops; + const char *filename; + int *target_errno; + int return_value; +}; + +/* Helper for linux_nat_fileio_unlink. */ + +static void +linux_nat_fileio_unlink_1 (void *arg) +{ + struct unlink_closure *clo = (struct unlink_closure *) arg; + + clo->return_value = super_fileio_unlink (clo->ops, clo->filename, + clo->target_errno); +} + +/* Implementation of to_fileio_unlink. */ + +static int +linux_nat_fileio_unlink (struct target_ops *ops, + const char *filename, int *target_errno) +{ + struct unlink_closure clo = + { + /* Arguments. */ + ops, filename, target_errno, + + /* Failure return value. */ + -1 + }; + + linux_nat_enter_fs (linux_nat_fileio_unlink_1, &clo, target_errno); + + return clo.return_value; +} + +/* Arguments and return value of to_fileio_readlink. */ + +struct readlink_closure +{ + struct target_ops *ops; + const char *filename; + int *target_errno; + char *return_value; +}; + +/* Helper for linux_nat_fileio_readlink. */ + +static void +linux_nat_fileio_readlink_1 (void *arg) +{ + struct readlink_closure *clo = (struct readlink_closure *) arg; + + clo->return_value = super_fileio_readlink (clo->ops, clo->filename, + clo->target_errno); +} + +/* Implementation of to_fileio_readlink. */ + +static char * +linux_nat_fileio_readlink (struct target_ops *ops, + const char *filename, int *target_errno) +{ + struct readlink_closure clo = + { + /* Arguments. */ + ops, filename, target_errno, + + /* Failure return value. */ + NULL + }; + + linux_nat_enter_fs (linux_nat_fileio_readlink_1, &clo, target_errno); + + return clo.return_value; +} Thanks, Pedro Alves