diff options
author | Avidan Borisov <avidanborisov@gmail.com> | 2023-06-26 12:16:33 +0300 |
---|---|---|
committer | Steven Rostedt (Google) <rostedt@goodmis.org> | 2023-10-17 18:33:27 -0400 |
commit | 6ece22f32070a4af2c0165875e41864a7c1ef40f (patch) | |
tree | 140fcc4f3736c8d019a9f799eca11c28579de4df | |
parent | a3929b09b4f650e27d62c9602093936db5fca48a (diff) | |
download | trace-cmd-6ece22f32070a4af2c0165875e41864a7c1ef40f.tar.gz |
trace-cmd: export pidfile functions from trace-listen.c
trace-listen.c has some utility functions for creating and removing
pidfiles, to avoid code duplication we make those functions generic
and export them to the rest of the codebase.
Link: https://lore.kernel.org/linux-trace-devel/20230626091635.3002827-3-avidanborisov@gmail.com
Signed-off-by: Avidan Borisov <avidanborisov@gmail.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
-rw-r--r-- | tracecmd/include/trace-local.h | 4 | ||||
-rw-r--r-- | tracecmd/trace-listen.c | 32 |
2 files changed, 18 insertions, 18 deletions
diff --git a/tracecmd/include/trace-local.h b/tracecmd/include/trace-local.h index 8f18f6d5..33397b1e 100644 --- a/tracecmd/include/trace-local.h +++ b/tracecmd/include/trace-local.h @@ -461,4 +461,8 @@ static inline bool is_digits(const char *s) bool trace_tsc2nsec_is_supported(void); +void make_pid_name(char *buf, const char *pidfile_basename); +void remove_pid_file(const char *pidfile_basename); +void make_pid_file(const char *pidfile_basename); + #endif /* __TRACE_LOCAL_H */ diff --git a/tracecmd/trace-listen.c b/tracecmd/trace-listen.c index e95c5711..5894a92d 100644 --- a/tracecmd/trace-listen.c +++ b/tracecmd/trace-listen.c @@ -34,6 +34,8 @@ #define VAR_RUN_DIR VAR_DIR_Q(VAR_DIR) "/run" +#define LISTEN_PIDFILE "trace-cmd-net.pid" + static char *default_output_dir = "."; static char *output_dir; static char *default_output_file = "trace"; @@ -52,7 +54,8 @@ static bool done; #define pdie(fmt, ...) \ do { \ tracecmd_plog_error(fmt, ##__VA_ARGS__);\ - remove_pid_file(); \ + if (do_daemon) \ + remove_pid_file(LISTEN_PIDFILE);\ exit(-1); \ } while (0) @@ -126,21 +129,16 @@ static void finish(int sig) done = true; } -static void make_pid_name(int mode, char *buf) +void make_pid_name(char *buf, const char *pidfile_basename) { - snprintf(buf, PATH_MAX, VAR_RUN_DIR "/trace-cmd-net.pid"); + snprintf(buf, PATH_MAX, VAR_RUN_DIR "/%s", pidfile_basename); } -static void remove_pid_file(void) +void remove_pid_file(const char *pidfile_basename) { char buf[PATH_MAX]; - int mode = do_daemon; - - if (!do_daemon) - return; - - make_pid_name(mode, buf); + make_pid_name(buf, pidfile_basename); unlink(buf); } @@ -991,16 +989,12 @@ static void do_accept_loop(int sfd) clean_up(); } -static void make_pid_file(void) +void make_pid_file(const char *pidfile_basename) { char buf[PATH_MAX]; - int mode = do_daemon; int fd; - if (!do_daemon) - return; - - make_pid_name(mode, buf); + make_pid_name(buf, pidfile_basename); fd = open(buf, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) { @@ -1075,7 +1069,8 @@ static void do_listen(char *port) if (!tracecmd_get_debug()) signal_setup(SIGCHLD, sigstub); - make_pid_file(); + if (do_daemon) + make_pid_file(LISTEN_PIDFILE); if (use_vsock) sfd = get_vsock(port); @@ -1090,7 +1085,8 @@ static void do_listen(char *port) kill_clients(); - remove_pid_file(); + if (do_daemon) + remove_pid_file(LISTEN_PIDFILE); } static void start_daemon(void) |