From 5e62eafcc290bbbbff213c043894767277237dda Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Thu, 25 Feb 2021 19:11:45 -0500 Subject: [PATCH] faketime: add -p option to wrapper for setting PID I had to decide what to do if FAKE_PID wasn't defined during the build. I decided that since the wrapper can't be sure it is preloading the same library that it was built with (someone could somehow mix and match the library and the wrapper tool), it should just warn and pass along the value anyway. This reserves the option space, but shouldn't annoy people too much if they are running it on a system that doesn't have FAKE_PID enabled. I note that this happens regardless of whether it is a "direct" invocation or not. I don't fully understand all the tradeoffs here, so I would appreciate another set of eyes reviewing this choice. Closes: #308 --- man/faketime.1 | 3 +++ src/faketime.c | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/man/faketime.1 b/man/faketime.1 index 5a22732..2b329ea 100644 --- a/man/faketime.1 +++ b/man/faketime.1 @@ -23,6 +23,9 @@ show version information and quit. \fB\-m\fR use the multi-threading variant of libfaketime. .TP +\fB\-p \fR +pretend that the program's process ID is PID. (only available if built with FAKE_PID) +.TP \fB\-f\fR use the advanced timestamp specification format. .TP diff --git a/src/faketime.c b/src/faketime.c index 23d6abf..af618f2 100644 --- a/src/faketime.c +++ b/src/faketime.c @@ -75,6 +75,9 @@ void usage(const char *name) " -m : Use the multi-threaded version of libfaketime\n" " -f : Use the advanced timestamp specification format (see manpage)\n" " --exclude-monotonic : Prevent monotonic clock from drifting (not the raw monotonic one)\n" +#ifdef FAKE_PID + " -p PID : Pretend that the program's process ID is PID\n" +#endif "\n" "Examples:\n" "%s 'last friday 5 pm' /bin/date\n" @@ -107,6 +110,8 @@ int main (int argc, char **argv) int curr_opt = 1; bool use_mt = false, use_direct = false; long offset; + bool fake_pid = false; + const char *pid_val; while(curr_opt < argc) { @@ -116,6 +121,16 @@ int main (int argc, char **argv) curr_opt++; continue; } + if (0 == strcmp(argv[curr_opt], "-p")) + { + fake_pid = true; + pid_val = argv[curr_opt + 1]; + curr_opt += 2; +#ifndef FAKE_PID + fprintf(stderr, "faketime: -p argument probably won't work (try rebuilding with -DFAKE_PID)\n"); +#endif + continue; + } else if (0 == strcmp(argv[curr_opt], "-f")) { use_direct = true; @@ -198,6 +213,8 @@ int main (int argc, char **argv) /* simply pass format string along */ setenv("FAKETIME", argv[curr_opt], true); } + if (fake_pid) + setenv("FAKETIME_FAKEPID", pid_val, true); int keepalive_fds[2]; (void) (pipe(keepalive_fds) + 1);