coredump: add %f for executable filename
authorLepton Wu <ytht.net@gmail.com>
Wed, 12 Aug 2020 01:36:20 +0000 (18:36 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 12 Aug 2020 17:58:01 +0000 (10:58 -0700)
The document reads "%e" should be "executable filename" while actually it
could be changed by things like pr_ctl PR_SET_NAME.  People who uses "%e"
in core_pattern get surprised when they find out they get thread name
instead of executable filename.

This is either a bug of document or a bug of code.  Since the behavior of
"%e" is there for long time, it could bring another surprise for users if
we "fix" the code.

So we just "fix" the document.  And more, for users who really need the
"executable filename" in core_pattern, we introduce a new "%f" for the
real executable filename.  We already have "%E" for executable path in
kernel, so just reuse most of its code for the new added "%f" format.

Signed-off-by: Lepton Wu <ytht.net@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Link: http://lkml.kernel.org/r/20200701031432.2978761-1-ytht.net@gmail.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Documentation/admin-guide/sysctl/kernel.rst
fs/coredump.c

index 2ae9669eb22c82fde4a1fe84727de289fea469ce..d4b32cc32bb79030d9f685a969c7934ad7312180 100644 (file)
@@ -164,7 +164,8 @@ core_pattern
        %s              signal number
        %t              UNIX time of dump
        %h              hostname
-       %e              executable filename (may be shortened)
+       %e              executable filename (may be shortened, could be changed by prctl etc)
+       %f              executable filename
        %E              executable path
        %c              maximum size of core file by resource limit RLIMIT_CORE
        %<OTHER>        both are dropped
index 7237f07ff6bed2b63a83491307f1d98f56548687..76e7c10edfc03011b9b35a694f437cf6c9e5fe6e 100644 (file)
@@ -153,10 +153,10 @@ int cn_esc_printf(struct core_name *cn, const char *fmt, ...)
        return ret;
 }
 
-static int cn_print_exe_file(struct core_name *cn)
+static int cn_print_exe_file(struct core_name *cn, bool name_only)
 {
        struct file *exe_file;
-       char *pathbuf, *path;
+       char *pathbuf, *path, *ptr;
        int ret;
 
        exe_file = get_mm_exe_file(current->mm);
@@ -175,6 +175,11 @@ static int cn_print_exe_file(struct core_name *cn)
                goto free_buf;
        }
 
+       if (name_only) {
+               ptr = strrchr(path, '/');
+               if (ptr)
+                       path = ptr + 1;
+       }
        ret = cn_esc_printf(cn, "%s", path);
 
 free_buf:
@@ -301,12 +306,16 @@ static int format_corename(struct core_name *cn, struct coredump_params *cprm,
                                              utsname()->nodename);
                                up_read(&uts_sem);
                                break;
-                       /* executable */
+                       /* executable, could be changed by prctl PR_SET_NAME etc */
                        case 'e':
                                err = cn_esc_printf(cn, "%s", current->comm);
                                break;
+                       /* file name of executable */
+                       case 'f':
+                               err = cn_print_exe_file(cn, true);
+                               break;
                        case 'E':
-                               err = cn_print_exe_file(cn);
+                               err = cn_print_exe_file(cn, false);
                                break;
                        /* core limit size */
                        case 'c':