qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: linux-user: keep the name-ending parenthesis in /proc/self/stat


From: Brice Goglin
Subject: Re: linux-user: keep the name-ending parenthesis in /proc/self/stat
Date: Tue, 31 Mar 2020 00:29:03 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0

Le 31/03/2020 à 00:05, Philippe Mathieu-Daudé a écrit :
> On 3/30/20 9:07 PM, Brice Goglin wrote:
>> When the program name is very long, qemu-user may truncate it in
>> /proc/self/stat. However the truncation must keep the ending ") "
>> to conform to the proc manpage which says:
>>      (2) comm  %s
>>             The  filename of the executable, in parentheses.  This
>>             is visible whether or not the  executable  is  swapped
>>             out.
>>
>> To reproduce:
>> $ ln -s /bin/cat <filenamewithmorethan128chars>
>> $ qemu-x86_64 ./<filenamewithmorethan128chars> /proc/self/stat
>>
>> Before the patch, you get:
>> 1134631 (<filenametruncated>0 0 0 0 0 0 0 0 ...
>> After the patch:
>> 1134631 (<filenametruncat>) 0 0 0 0 0 0 0 0 ...
>>
>> This fixes an issue with hwloc failing to parse /proc/self/stat
>> when Ludovic Courtes was testing it in guix over qemu-aarch64.
>>
>> Signed-off-by: Brice Goglin<address@hidden>
>>
>> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
>> index 5af55fca78..a1126dcf5b 100644
>> --- a/linux-user/syscall.c
>> +++ b/linux-user/syscall.c
>> @@ -7305,7 +7305,10 @@ static int open_self_stat(void *cpu_env, int fd)
>>           snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
>>         } else if (i == 1) {
>>           /* app name */
>> -        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
>> +        len = snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
>> +        if (len >= sizeof(buf))
>> +          /* bring back the ending ") " that was truncated */
>> +          strcpy(buf+sizeof(buf)-3, ") ");
>
> Maybe we can avoid the sprintf() call:
>
> -- >8 --
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -7305,7 +7305,11 @@ static int open_self_stat(void *cpu_env, int fd)
>          snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
>        } else if (i == 1) {
>          /* app name */
> -        snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
> +        char *ptr = buf;
> +
> +        *ptr++ = '(';
> +        ptr = stpncpy(ptr, ts->bprm->argv[0], sizeof(buf) - 3);
> +        strcpy(ptr, ") ");
>        } else if (i == 27) {
>          /* stack bottom */
>          val = start_stack;
>

This works too.

Brice





reply via email to

[Prev in Thread] Current Thread [Next in Thread]