about summary refs log tree commit homepage
path: root/mwrap_core.h
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2023-01-11 01:12:48 +0000
committerEric Wong <mwrap-perl@80x24.org>2023-01-11 04:23:31 +0000
commit64a55ae0ba1d09ccda458eb895d849e7d38cab81 (patch)
tree10f5f5d99c49de5ba17277540a786a477f9e9c5c /mwrap_core.h
parent86d350a3854af1a5a292972d4f70154e61ce5e80 (diff)
downloadmwrap-64a55ae0ba1d09ccda458eb895d849e7d38cab81.tar.gz
%p => PID expansion for dump_path + dump_csv
This makes it possible to dump per-PID files for processes which
fork.  `%p' matches what the Linux sys.kernel.core_pattern
sysctl understands.
Diffstat (limited to 'mwrap_core.h')
-rw-r--r--mwrap_core.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/mwrap_core.h b/mwrap_core.h
index fff0538..86e4498 100644
--- a/mwrap_core.h
+++ b/mwrap_core.h
@@ -895,12 +895,27 @@ __attribute__ ((destructor)) static void mwrap_dtor(void)
         if (dump_path) {
                 char *end = strchr(dump_path, ',');
                 char buf[PATH_MAX];
+                AUTO_FREE char *pid_path = NULL;
                 if (end) {
                         mwrap_assert((end - dump_path) < (intptr_t)sizeof(buf));
                         end = mempcpy(buf, dump_path, end - dump_path);
                         *end = 0;
                         dump_path = buf;
                 }
+
+                /* %p => PID expansion (Linux core_pattern uses %p, too) */
+                if ((s = strchr(dump_path, '%')) && s[1] == 'p' &&
+                                /* don't allow injecting extra formats: */
+                                !strchr(s + 2, '%')) {
+                        s[1] = 'd'; /* s/%p/%d/ to make asprintf happy */
+                        int n = asprintf(&pid_path, dump_path, (int)getpid());
+                        if (n < 0)
+                                fprintf(stderr,
+                                        "asprintf failed: %m, dumping to %s\n",
+                                        dump_path);
+                        else
+                                dump_path = pid_path;
+                }
                 dump_fd = open(dump_path, O_CLOEXEC|O_WRONLY|O_APPEND|O_CREAT,
                                 0666);
                 if (dump_fd < 0) {