Skip to content

Commit 7ad4d5a

Browse files
committed
linux-user: fix /proc/self/stat handling
In the original bug report long files names in Guix caused /proc/self/stat be truncated without the trailing ") " as specified in 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. In the kernel this is currently done by do_task_stat calling proc_task_name() which uses a structure limited by TASK_COMM_LEN (16). Additionally it should only be reporting the executable name rather than the full path. Fix both these failings while cleaning up the code to use GString to build up the reported values. As the whole function is cleaned up also adjust the white space to the current coding style. Message-ID: <[email protected]> Reported-by: Brice Goglin <[email protected]> Signed-off-by: Alex Bennée <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Reviewed-by: Richard Henderson <[email protected]> Message-Id: <[email protected]>
1 parent 469a788 commit 7ad4d5a

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

linux-user/syscall.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7295,34 +7295,29 @@ static int open_self_stat(void *cpu_env, int fd)
72957295
{
72967296
CPUState *cpu = env_cpu((CPUArchState *)cpu_env);
72977297
TaskState *ts = cpu->opaque;
7298-
abi_ulong start_stack = ts->info->start_stack;
7298+
g_autoptr(GString) buf = g_string_new(NULL);
72997299
int i;
73007300

73017301
for (i = 0; i < 44; i++) {
7302-
char buf[128];
7303-
int len;
7304-
uint64_t val = 0;
7305-
7306-
if (i == 0) {
7307-
/* pid */
7308-
val = getpid();
7309-
snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
7310-
} else if (i == 1) {
7311-
/* app name */
7312-
snprintf(buf, sizeof(buf), "(%s) ", ts->bprm->argv[0]);
7313-
} else if (i == 27) {
7314-
/* stack bottom */
7315-
val = start_stack;
7316-
snprintf(buf, sizeof(buf), "%"PRId64 " ", val);
7317-
} else {
7318-
/* for the rest, there is MasterCard */
7319-
snprintf(buf, sizeof(buf), "0%c", i == 43 ? '\n' : ' ');
7320-
}
7302+
if (i == 0) {
7303+
/* pid */
7304+
g_string_printf(buf, FMT_pid " ", getpid());
7305+
} else if (i == 1) {
7306+
/* app name */
7307+
gchar *bin = g_strrstr(ts->bprm->argv[0], "/");
7308+
bin = bin ? bin + 1 : ts->bprm->argv[0];
7309+
g_string_printf(buf, "(%.15s) ", bin);
7310+
} else if (i == 27) {
7311+
/* stack bottom */
7312+
g_string_printf(buf, TARGET_ABI_FMT_ld " ", ts->info->start_stack);
7313+
} else {
7314+
/* for the rest, there is MasterCard */
7315+
g_string_printf(buf, "0%c", i == 43 ? '\n' : ' ');
7316+
}
73217317

7322-
len = strlen(buf);
7323-
if (write(fd, buf, len) != len) {
7324-
return -1;
7325-
}
7318+
if (write(fd, buf->str, buf->len) != buf->len) {
7319+
return -1;
7320+
}
73267321
}
73277322

73287323
return 0;

0 commit comments

Comments
 (0)