From patchwork Fri Jan 18 21:30:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 156050 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3729087jaa; Fri, 18 Jan 2019 13:34:12 -0800 (PST) X-Google-Smtp-Source: ALg8bN7AXfXtKXUOexzNlmfshbJTkB7xjmDU8tr5s3VJy37UMxNT32+d+ho4jGYKutcUaIis5kzf X-Received: by 2002:a5d:6988:: with SMTP id g8mr17471571wru.33.1547847252463; Fri, 18 Jan 2019 13:34:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547847252; cv=none; d=google.com; s=arc-20160816; b=EmHBU1I7Y58194HLMU+yIvhsefkkcecVAIk9QPFIzb/c55sJ1brogBqgxnEndD+pCv cTkfs9E3IbdmXleuQUeb1rpJMo7D7B01dsiXR0N09aVVk8c/r6peKWUMfYSrkiOEiEDD oRk+jW2PthPSn30VQWt7WbQkXBQwmPcc6i4A3ZxuAcfB5XV2GDAXYt7v1DbmenynTv/f cXDLaqjBuklcturSzopkRGHxxizApfrvsgMNBZ02a1GjpR2Avlpr/3itgmezzN8no68K /SZFiTB7+DM2x2wfSnuWys2b0lprLtX3yGAn74aBJltoXco0+TLp7ji0LZ0ScxmE2T0b SMLQ== 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=xn0wCKHONKALxJfo3bPxJs7CyFRY/xeQ+BVQGQWT6CM=; b=hNwYryZ26WfFSf9NJszayt2/PEQHdsMiBxDPl+9TqCnZdJ3AAzfnGcADo+7e/9BiTG 4/gBP8d0o7oLFiqlafBZTjUKWAtalYfeOi5VKa+f4TRUqOKPPhnMBdjvw52ZXqoRGdtp ShCtdED4uuNdCWU3IwFfaKn33uBrr+UlMAFYY3FxUorh319PwhWphdnnhvuPY3vYz+R/ zKhQI5SR2TrsNbnsjTynX/BmObZDYICOG0j1A+8eTUVM6PHtKSiwrkYjNPlg3D1108Sc xsnEDDdEplq5F0v+EwiFjuoaBXblhLPcwCke3E9ceSZWO0A/3SP36MrVE2JjMvsn0cto /HKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=BvhPAo3N; 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 h16si57515907wrq.337.2019.01.18.13.34.12 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 18 Jan 2019 13:34:12 -0800 (PST) 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=BvhPAo3N; 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]:47405 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gkbmF-0006Gr-AF for patch@linaro.org; Fri, 18 Jan 2019 16:34:11 -0500 Received: from eggs.gnu.org ([209.51.188.92]:55719) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gkbju-0004hk-V4 for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:31:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gkbjt-0003xf-J0 for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:31:46 -0500 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]:33615) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gkbjs-0003mi-Ip for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:31:45 -0500 Received: by mail-pf1-x441.google.com with SMTP id c123so7220157pfb.0 for ; Fri, 18 Jan 2019 13:31:38 -0800 (PST) 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=xn0wCKHONKALxJfo3bPxJs7CyFRY/xeQ+BVQGQWT6CM=; b=BvhPAo3NI3tJhJnsVF4oo8qoNe5fO0RrE8ym4yDgpF5y7DKpVLLCNwXg1GzA3NL8MK 2KR/U9a2pgiHxhi9CnhtkJQnABKUkM1/559ADp6eARvCbQ6IrteymWFg0pQhfkKAt7ot kx23KO8cJOKVupntcsqiGe1wiuJ0joVkpvDxQ= 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=xn0wCKHONKALxJfo3bPxJs7CyFRY/xeQ+BVQGQWT6CM=; b=rY6vS2dDm4zStoYoFdcT3wpE/RyLyhZCRCn6E+nz6vE2mD1FWkgawnVIagb6Mq2InX cSzoT7z5L6K98idgtQwHEfV3rg6JdEyWAgr80sR48phRqvX/LavFeJbTJlpzip4cfNyv ICC+Hq+XWEUt0cMfSIdCAw0Ltz5Af3q/DLOpQChW1ZizwpvjP4a4+vdw/8s8lixnE0Yt 96KnuqvKMg3PWs37xw4dkb88V/yQoPrxnaQO+IOkWi6g2924oezKiE+WMqHRc8ogGVxn ECVks26tUdSvZHwbs03gK6yR87Yw5kfkVTpZrA4jd5TBwyTWAHS8ScOq9m3HLdU+3zsq m/KA== X-Gm-Message-State: AJcUukegzZbQpNuEYn4K6DsbgX8Wc20CSrw0Mi/EjgefvjDt6l6MWgJ7 8VAPwJfFtEzv2hVi7Sv50oab3te/daw= X-Received: by 2002:a65:5bc4:: with SMTP id o4mr19550125pgr.426.1547847096927; Fri, 18 Jan 2019 13:31:36 -0800 (PST) Received: from cloudburst.twiddle.net ([2001:8000:10e0:a000:c673:a6b6:fdef:1933]) by smtp.gmail.com with ESMTPSA id v12sm5833667pgg.41.2019.01.18.13.31.34 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 18 Jan 2019 13:31:36 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Sat, 19 Jan 2019 08:30:37 +1100 Message-Id: <20190118213122.22865-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190118213122.22865-1-richard.henderson@linaro.org> References: <20190118213122.22865-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::441 Subject: [Qemu-devel] [PATCH v6 04/49] 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.2 diff --git a/linux-user/syscall-file.inc.c b/linux-user/syscall-file.inc.c index ca777da753..ffa70bbea8 100644 --- a/linux-user/syscall-file.inc.c +++ b/linux-user/syscall-file.inc.c @@ -221,8 +221,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; @@ -241,9 +241,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++) { @@ -255,7 +266,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"); @@ -265,55 +276,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); }