From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:58019) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USqGI-00013f-NI for qemu-devel@nongnu.org; Thu, 18 Apr 2013 10:56:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1USqGH-0001fC-0B for qemu-devel@nongnu.org; Thu, 18 Apr 2013 10:56:34 -0400 Received: from mail-la0-x236.google.com ([2a00:1450:4010:c03::236]:58349) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USqGG-0001ew-Om for qemu-devel@nongnu.org; Thu, 18 Apr 2013 10:56:32 -0400 Received: by mail-la0-f54.google.com with SMTP id ec20so2645462lab.27 for ; Thu, 18 Apr 2013 07:56:30 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Peter Maydell Date: Thu, 18 Apr 2013 15:56:10 +0100 Message-ID: Content-Type: text/plain; charset=UTF-8 Subject: Re: [Qemu-devel] [PATCH] linux-user: identify running binary in /proc/$$/exe List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Andreas Schwab Cc: qemu-devel@nongnu.org On 11 March 2013 17:19, Andreas Schwab wrote: > Some applications like to test /proc/$$/exe (where $$ is the own pid) to > find out who they are. Handle it like /proc/self/exe. > > Also, do the same handling in readlinkat. Sorry I didn't get round to reviewing this earlier; I think it slipped through the cracks :-( > diff --git a/linux-user/syscall.c b/linux-user/syscall.c > index 19630ea..3e5a6ae 100644 > --- a/linux-user/syscall.c > +++ b/linux-user/syscall.c > @@ -6413,7 +6413,10 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, > if (!p || !p2) > ret = -TARGET_EFAULT; > else { > - if (strncmp((const char *)p, "/proc/self/exe", 14) == 0) { > + char myself[PATH_MAX]; > + snprintf(myself, sizeof(myself), "/proc/%d/exe", getpid()); > + if (strncmp((const char *)p, "/proc/self/exe", 14) == 0 || > + strcmp((const char *)p, myself) == 0) { > char real[PATH_MAX]; > temp = realpath(exec_path,real); > ret = (temp==NULL) ? get_errno(-1) : strlen(real) ; > @@ -6429,13 +6432,24 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1, > #if defined(TARGET_NR_readlinkat) && defined(__NR_readlinkat) > case TARGET_NR_readlinkat: > { > - void *p2; > + void *p2, *temp; > p = lock_user_string(arg2); > p2 = lock_user(VERIFY_WRITE, arg3, arg4, 0); > if (!p || !p2) > ret = -TARGET_EFAULT; > - else > - ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4)); You could add braces on the 'then' part of this if() and get rid of the stray hardcoded tab in the existing code. (not a requirement but since we're in the area anyway...) > + else { > + char myself[PATH_MAX]; > + snprintf(myself, sizeof(myself), "/proc/%d/exe", getpid()); > + if (strncmp((const char *)p, "/proc/self/exe", 14) == 0 || > + strcmp((const char *)p, myself) == 0) { > + char real[PATH_MAX]; > + temp = realpath(exec_path,real); > + ret = (temp==NULL) ? get_errno(-1) : strlen(real) ; > + snprintf((char *)p2, arg3, "%s", real); This seems to be a fair chunk of code in common with the readlink case above -- can we abstract it out? (ideally to a function somewhere in the same area of this file as the code that handles intercepting /proc/ calls for open().) It would be consistent to support /proc/$$/ for all the open() intercepts too. > + } > + else > + ret = get_errno(sys_readlinkat(arg1, path(p), p2, arg4)); > + } > unlock_user(p2, arg3, ret); > unlock_user(p, arg2, 0); > } > -- > 1.8.1.5 thanks -- PMM