mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: mwrap-perl@80x24.org
Subject: [PATCH 3/4] %p => PID expansion for dump_path + dump_csv
Date: Wed, 11 Jan 2023 01:12:48 +0000	[thread overview]
Message-ID: <20230111011249.2713039-4-e@80x24.org> (raw)
In-Reply-To: <20230111011249.2713039-1-e@80x24.org>

This makes it possible to dump per-PID files for processes which
fork.  `%p' matches what the Linux sys.kernel.core_pattern
sysctl understands.
---
 mwrap_core.h      | 15 +++++++++++++++
 script/mwrap-perl |  6 ++++++
 t/mwrap.t         | 15 +++++++++++++++
 3 files changed, 36 insertions(+)

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) {
diff --git a/script/mwrap-perl b/script/mwrap-perl
index eb29176..371aee6 100644
--- a/script/mwrap-perl
+++ b/script/mwrap-perl
@@ -76,6 +76,10 @@ Dumps the output at exit to a given filename:
 
 	total_bytes	call_count	location
 
+C<$FILENAME> may contain C<%p> where C<%p> is a placeholder for
+the PID being dumped.  No other use of C<%> is accepted, and
+multiple C<%> means all C<%> (including C<%p>) are handled as-is.
+
 =item dump_fd:$DESCRIPTOR
 
 As with dump_path, but dumps the output to a given file descriptor.
@@ -90,6 +94,8 @@ but is subject to change in future releases.
 C<dump_csv> without the C<:> may also be used in conjunction with
 C<dump_fd>, such as C<MWRAP=dump_fd:2,dump_csv>.
 
+Expands C<%p> to the PID in C<$FILENAME> as described for C<dump_path:>
+
 =back
 
 =head1 HTTP POST API
diff --git a/t/mwrap.t b/t/mwrap.t
index ccd739b..783f6e7 100644
--- a/t/mwrap.t
+++ b/t/mwrap.t
@@ -42,6 +42,21 @@ my $dump = "$mwrap_tmp/dump";
 	$nr_comma = ($s =~ tr/,/,/);
 	$nr_cr = ($s =~ tr/\n/\n/);
 	ok($nr_comma > ($nr_cr * 4), 'CSV has more commas than CR');
+
+	$env->{MWRAP} = "dump_path:$dump.%p";
+	mwrap_run('dump_path PID expansion', $env, '-e', $script);
+	my @d = grep(/\.\d+\z/, glob("$dump.*"));
+	is(scalar(@d), 1, 'got PID file') or diag explain([glob("$dump*")]);
+	unlink(@d) or BAIL_OUT "unlink: $!";
+
+	# don't allow injecting random formats
+	for my $fmt ('%p.%m', '%m.%p') {
+		my $fn = $dump.$fmt;
+		$env->{MWRAP} = "dump_path:$fn";
+		mwrap_run("PID expansion fails on $fmt", $env, '-e', $script);
+		ok($fn, "$fmt used as-is");
+		unlink($fn) or BAIL_OUT "unlink: $!";
+	}
 }
 
 SKIP: { # C++ program which uses malloc via "new"

  parent reply	other threads:[~2023-01-11  1:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-11  1:12 [PATCH 0/4] CSV-related improvements Eric Wong
2023-01-11  1:12 ` [PATCH 1/4] support MWRAP=dump_csv:$FILENAME parameter Eric Wong
2023-01-11  1:12 ` [PATCH 2/4] add mwrap-decode-csv tool Eric Wong
2023-01-11  1:12 ` Eric Wong [this message]
2023-01-11  1:12 ` [PATCH 4/4] rewrite README and update manpage to favor CSV Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230111011249.2713039-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=mwrap-perl@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mwrap-perl.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).