From patchwork Sun May 19 20:36:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 164494 Delivered-To: patch@linaro.org Received: by 2002:a92:9e1a:0:0:0:0:0 with SMTP id q26csp4779996ili; Sun, 19 May 2019 13:41:13 -0700 (PDT) X-Google-Smtp-Source: APXvYqw2kFgIZ2bVeBSrDfQ41gvtf7GsapEravmfqW9ueFkhBOvXlZVMOxiKLXTVWSsLyWSZ+hkd X-Received: by 2002:a17:906:1e0a:: with SMTP id g10mr47891804ejj.127.1558298473905; Sun, 19 May 2019 13:41:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558298473; cv=none; d=google.com; s=arc-20160816; b=KP5ITktUuHDHZk8rV2Al4qcRxNkOTq4F0mQKPxZAI/K/tIb7nLOyXyIa+8EgWean6B sv/WAAgajIIpGyiR5n9mdpY6YGx0b7LaofXnPugvGKw+D8FRX5Uv2bR2WdyPcaTKwm+F /OsGmbS4C1Hqrc0nlx6TJx31ClLGtX182xK+S2Hsx8O7z9MYN0zzegEhSvHfVOFEPg6o /bLtGlCiyANMYjH+Nb3t74bvYEyjYPXJOBcN5IHLljptqGaeqHv/UrSXbgPx9I1fQIVj ishfK2Zh/jtXwujAuW6RMCXD0KkVkpChEqLYKGWUttT5piQHI46kqHCOZd4q3GwqmNW+ mDJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=eSYdriPx4kdDTBySMjSQ3hbHBLevuc9v6XaVM577ESk=; b=JfEh/B5AgvdZzn10Or/g3X/bAvTdoSNHr+wkofxvSRtuoC+D6bpjg9X32soXj/6Ghi KmLBVrGBWL+KL4Ttx70e3L3VL8RelvywFF9Cqpv01H5l/EIz2mBPdwzDULJIbfeDDTF2 sRSScjie9svh7HPixHFGgr4vLa5iyU/iK5GmvftwMKty4q/KaibX118Olp3vxICTsOpz gxkQOi8m1aK21e+tXFCto0DCiAUDZ25T+gewrzoq7bHuuOUnz7mpZdSsbf1+t60BUH9C CQO2b3Slz5KrVO2VB9CLPO5Oc4b7Yq03zjzzrT5fonl5YzdsUQkk35sM48NTf1geT9eI iLOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="Sg/4sKNr"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id l24si2514345edq.115.2019.05.19.13.41.13 for (version=TLS1 cipher=AES128-SHA bits=128/128); Sun, 19 May 2019 13:41:13 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="Sg/4sKNr"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:53159 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hSScK-0005Wg-MR for patch@linaro.org; Sun, 19 May 2019 16:41:12 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50070) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hSSYo-0002jK-I1 for qemu-devel@nongnu.org; Sun, 19 May 2019 16:37:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hSSYn-0003Nn-Cg for qemu-devel@nongnu.org; Sun, 19 May 2019 16:37:34 -0400 Received: from mail-pl1-x641.google.com ([2607:f8b0:4864:20::641]:39842) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hSSYn-0003Mi-5l for qemu-devel@nongnu.org; Sun, 19 May 2019 16:37:33 -0400 Received: by mail-pl1-x641.google.com with SMTP id g9so5708797plm.6 for ; Sun, 19 May 2019 13:37:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=eSYdriPx4kdDTBySMjSQ3hbHBLevuc9v6XaVM577ESk=; b=Sg/4sKNreLI47I2kSXyvKrfz/F9UXr8TRh0XKbe/P//pAE2vgTHjX0USqkAoNdpenR 1AHiGGzNl8e+QgGrMqaEYMUrYdGlXuBYXWGsJ4XY4dz2IBBhEqpD/iOdJYDs/i1AS3NI s0ZRWShf11oA39tEj2NZUPCj96dwvKsz2sD//woQ5rVHMgzvXDJX6dAEtnWLhD+wo9KU oxiT4rBTbQJ1ckGTbgCqH1VSAJ/vaBV44QHHLMJZMA6+UnT5J+EwV4ax2Gv90NLjFnfI 9W9GwBGMCElBsP9R89Ujbsu1MgNks/D4GHqdQZhHmOeo4IrIr+bPeI55Kd/BgI/Mr7dy HfwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=eSYdriPx4kdDTBySMjSQ3hbHBLevuc9v6XaVM577ESk=; b=bS5B/KC70G0uN+dWCEmqizNw0DXlLQqfJwkri9/B+mTRu8hLQSEv5XYccOjDNfmx2c TtHKAh597oU/lLuwydPlRlHB5YIqoBGvswOIkyXZYPC1QgsO+AKWnjc1Pbs+NNJqIM/9 kdrsN2lbmZWUd5CJWkaMOZ8dEF7qDyR75PliWUoXxUqnCduXxkUcN7NGxd5swwVect99 97NmWFFFg9HkbVBGiVfiIP/TuKJk/pFxDMgWmJG2s0f7ERIif3kFoYYgtg8E7CyowNz4 4hfxJVWQcvjZ4ph9C6A6Qd6PyP/qF/kKmVeI9hJeGAP5t5cfi4BCNvQIE3hKRbfl764h yFIg== X-Gm-Message-State: APjAAAUiNWv6d/MG80Jwwe25tf7Ha1ZpRy/XldSguT77PuatZZUEBhK4 Z8hIZ4P6MlzrLtAwRHneG1MYJKHqCGI= X-Received: by 2002:a17:902:bd46:: with SMTP id b6mr10556307plx.173.1558298251963; Sun, 19 May 2019 13:37:31 -0700 (PDT) Received: from localhost.localdomain (97-113-13-231.tukw.qwest.net. [97.113.13.231]) by smtp.gmail.com with ESMTPSA id i7sm11397448pfo.19.2019.05.19.13.37.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 19 May 2019 13:37:31 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Sun, 19 May 2019 13:36:15 -0700 Message-Id: <20190519203726.20729-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190519203726.20729-1-richard.henderson@linaro.org> References: <20190519203726.20729-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::641 Subject: [Qemu-devel] [PATCH v7 03/74] linux-user: Share more code for open and openat X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" The do_openat helper can have all of the code that is not directly related to the argument ordering of these two syscalls. Signed-off-by: Richard Henderson --- linux-user/syscall-file.inc.c | 69 ++++++++++++++++------------------- 1 file changed, 31 insertions(+), 38 deletions(-) -- 2.17.1 diff --git a/linux-user/syscall-file.inc.c b/linux-user/syscall-file.inc.c index 7974148ccb..961eed13ae 100644 --- a/linux-user/syscall-file.inc.c +++ b/linux-user/syscall-file.inc.c @@ -227,8 +227,8 @@ static int open_net_route(void *cpu_env, int fd) } #endif -static int do_openat(void *cpu_env, int dirfd, const char *pathname, - int flags, mode_t mode) +static abi_long do_openat(void *cpu_env, int dirfd, abi_ulong target_path, + int target_flags, mode_t mode) { struct fake_open { const char *filename; @@ -247,9 +247,20 @@ static int do_openat(void *cpu_env, int dirfd, const char *pathname, { NULL, NULL, NULL } }; + char *pathname = lock_user_string(target_path); + int flags = target_to_host_bitmask(target_flags, fcntl_flags_tbl); + abi_long ret; + + if (!pathname) { + return -TARGET_EFAULT; + } + if (is_proc_myself(pathname, "exe")) { - int execfd = qemu_getauxval(AT_EXECFD); - return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode); + ret = qemu_getauxval(AT_EXECFD); + if (ret == 0) { + ret = get_errno(safe_openat(dirfd, exec_path, flags, mode)); + } + goto done; } for (fake_open = fakes; fake_open->filename; fake_open++) { @@ -261,7 +272,7 @@ static int do_openat(void *cpu_env, int dirfd, const char *pathname, if (fake_open->filename) { const char *tmpdir; char filename[PATH_MAX]; - int fd, r; + int fd; /* create temporary file to map stat to */ tmpdir = getenv("TMPDIR"); @@ -271,55 +282,37 @@ static int do_openat(void *cpu_env, int dirfd, const char *pathname, snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir); fd = mkstemp(filename); if (fd < 0) { - return fd; + ret = -TARGET_ENOENT; + goto done; } unlink(filename); - r = fake_open->fill(cpu_env, fd); - if (r) { - int e = errno; + ret = fake_open->fill(cpu_env, fd); + if (ret) { + ret = get_errno(ret); close(fd); - errno = e; - return r; + goto done; } lseek(fd, 0, SEEK_SET); - - return fd; + ret = fd; + goto done; } - return safe_openat(dirfd, path(pathname), flags, mode); + ret = get_errno(safe_openat(dirfd, path(pathname), flags, mode)); + done: + fd_trans_unregister(ret); + unlock_user(pathname, target_path, 0); + return ret; } #ifdef TARGET_NR_open SYSCALL_IMPL(open) { - char *p = lock_user_string(arg1); - abi_long ret; - - if (!p) { - return -TARGET_EFAULT; - } - ret = get_errno(do_openat(cpu_env, AT_FDCWD, p, - target_to_host_bitmask(arg2, fcntl_flags_tbl), - arg3)); - fd_trans_unregister(ret); - unlock_user(p, arg1, 0); - return ret; + return do_openat(cpu_env, AT_FDCWD, arg1, arg2, arg3); } #endif SYSCALL_IMPL(openat) { - char *p = lock_user_string(arg2); - abi_long ret; - - if (!p) { - return -TARGET_EFAULT; - } - ret = get_errno(do_openat(cpu_env, arg1, p, - target_to_host_bitmask(arg3, fcntl_flags_tbl), - arg4)); - fd_trans_unregister(ret); - unlock_user(p, arg2, 0); - return ret; + return do_openat(cpu_env, arg1, arg2, arg3, arg4); }