From patchwork Mon May 1 20:31:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: avidanborisov@gmail.com X-Patchwork-Id: 13228278 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7696C77B73 for ; Mon, 1 May 2023 20:32:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229664AbjEAUcG (ORCPT ); Mon, 1 May 2023 16:32:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232579AbjEAUcE (ORCPT ); Mon, 1 May 2023 16:32:04 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7B541FFD for ; Mon, 1 May 2023 13:32:02 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-50bc37e1525so3293630a12.1 for ; Mon, 01 May 2023 13:32:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682973120; x=1685565120; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8H7OIAXjkJ3+gFesoeXBFEp2Gs0YKi4vuH/7xoKVHpA=; b=kCXRwi9kUXlddZBL5o6Sd7G9T50mcwd3yz7iMwXtKtOnpYCKUv2i3Av28n5G0A9AFQ hdDKhlu/B1fOdoniygIc7PCizNF9SGkcPq/f808mERYA89cLCAxKrfNot+Jlehf+ezoK Ig2tSF9gBn2mpYFjbDkhzRrSctqq/LVKosDq//G3RJKc7OH8CmSN9lZnoCe+VbnMFMQz BYV2EqasVgS+MIv2tdVBYOQelH9JW/dobCvFqGe6b6UNLzwz+8pQknv8aDXIAkD69NXZ r6rSkjhImUimS8otZYV0Iv1lialrIkaqcTeyCVSX2DnuG0y9LfFHxwin/Izeb40KS+e0 EVQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682973120; x=1685565120; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8H7OIAXjkJ3+gFesoeXBFEp2Gs0YKi4vuH/7xoKVHpA=; b=VHlBrhCM8RW7uQoE+1clXUzlifqzOZWxlThzfP0OmPEd5r4BQqODYdfEH+5BytD7au SBMpxBNc0BphK1Mf+KEybKlwjgSbNzdNYo7bRVbMYjb+MDqKSIqT3cynYLZ60m0zdNPS 3YMGH7SzakAMwI8l2ny/Lp8lxyT+5AhYo88Efok5GbP5zFaEZGp4Z5qRHYcfAlAjHmOQ fqvaTDum4/vUa4ZYszvkCW9KZlfi1QwgMyCjvQ/dGoHDwNXjKFCRlN8UjOWBLCVc0gXT B33HY6ysUt0K3XELfgfPnejc13H1+aGcZTuHz/zmq3tkOasMEoOz66cH8zXmG6vgpAob N0pg== X-Gm-Message-State: AC+VfDwnLsBi9/k0fOzZu2QRCJU/8B0LPCywHhzgTdSHv6XJ4izMsw7a KF/ujbNBR6hrOR1QFSzZvw1nD7jSzrY= X-Google-Smtp-Source: ACHHUZ7fzx9npWT0Gaifb+TxOnkdqGFXC0d8JneyTyQZnkQQPGDJrd3OiR7H7H42QXdXCq9NAxIO9A== X-Received: by 2002:a05:6402:1add:b0:500:50f6:dd34 with SMTP id ba29-20020a0564021add00b0050050f6dd34mr6298763edb.1.1682973120596; Mon, 01 May 2023 13:32:00 -0700 (PDT) Received: from dante731.cslcs.technion.ac.il ([132.68.206.91]) by smtp.gmail.com with ESMTPSA id i25-20020a170906851900b0094f1b8901e1sm15233933ejx.68.2023.05.01.13.32.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 May 2023 13:32:00 -0700 (PDT) From: avidanborisov@gmail.com To: linux-trace-devel@vger.kernel.org Cc: Avidan Borisov Subject: [PATCH 1/3] trace-cmd record: Add --daemonize Date: Mon, 1 May 2023 23:31:16 +0300 Message-Id: <20230501203118.3105605-2-avidanborisov@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230501203118.3105605-1-avidanborisov@gmail.com> References: <20230501203118.3105605-1-avidanborisov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Avidan Borisov Add a --daemonize flag to trace-cmd record which allows the user to record in the background, after tracing has completely initialized. Signed-off-by: Avidan Borisov --- .../trace-cmd/trace-cmd-record.1.txt | 3 + tracecmd/trace-record.c | 128 +++++++++++++++++- tracecmd/trace-usage.c | 1 + 3 files changed, 130 insertions(+), 2 deletions(-) diff --git a/Documentation/trace-cmd/trace-cmd-record.1.txt b/Documentation/trace-cmd/trace-cmd-record.1.txt index b709e48..366ab46 100644 --- a/Documentation/trace-cmd/trace-cmd-record.1.txt +++ b/Documentation/trace-cmd/trace-cmd-record.1.txt @@ -418,6 +418,9 @@ OPTIONS run on a privileged guest that the host is aware of (as denoted by the 'cid' in the *-P* option for the agent). +*--daemonize* + Run trace-cmd in the background as a daemon after recording has started. + EXAMPLES -------- diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 7f0cebe..0eee8dd 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -91,6 +91,8 @@ static bool quiet; static bool fork_process; +static bool do_daemonize; + /* Max size to let a per cpu file get */ static int max_kb; @@ -1636,6 +1638,109 @@ static inline void ptrace_attach(struct buffer_instance *instance, int pid) { } #endif /* NO_PTRACE */ +static bool child_detached; + +static void daemonize_set_child_detached(int s) +{ + child_detached = true; +} + +static void daemonize_start(void) +{ + int devnull; + int status; + int pid; + int rc; + + pid = fork(); + if (pid == -1) + die("daemonize: fork failed"); + + if (pid == 0) { /* child */ + /* + * We keep stdout and stderr open to allow the user to + * see output and errors after the daemonization (he can + * choose to supress it with >/dev/null if he wants). + * + * No reason to keep stdin open (it might interfere with the + * shell), we redirect it to /dev/null. + */ + devnull = open("/dev/null", O_RDONLY); + if (devnull == -1) + die("daemonize: open /dev/null failed"); + + if (devnull > 0) { + if (dup2(devnull, 0) == -1) + die("daemonize: dup2"); + close(0); + } + + return; + + /* + * The child returns to back to the caller, but the parent waits until + * SIGRTMIN is received from the child (by calling daemonize_finish()), + * or the child exits for some reason (usually an indication of + * an error), which ever comes first. + * + * Then the parent exits (with the status code of the child, + * if it finished early, or with 0 if SIGRTMIN was received), + * which causes the child (and its entire process tree) to be + * inherited by init. + * + * Note that until the child calls daemonize_finish(), it still has + * the same session id as the parent, so it can die together with + * the parent before daemonization finished (purposefully, since the + * user might send a quick Ctrl^C to cancel the command, and we don't + * want background processes staying alive in that case) + */ + } else { /* parent */ + struct sigaction sa = { + /* disable SA_RESTART, to allow waitpid() to be interrupted by SIGRTMIN */ + .sa_flags = 0, + .sa_handler = daemonize_set_child_detached + }; + + if (sigemptyset(&sa.sa_mask) == -1) + die("daemonize: sigemptyset failed"); + + if (sigaddset(&sa.sa_mask, SIGRTMIN) == -1) + die("daemonize: sigaddset failed"); + + if (sigprocmask(SIG_UNBLOCK, &sa.sa_mask, NULL) == -1) + die("daemonize: sigprocmask failed"); + + if (sigaction(SIGRTMIN, &sa, NULL) == -1) + die("daemonize: sigaction failed"); + + do { + rc = waitpid(pid, &status, 0); + } while (!child_detached && ((rc == -1) && (errno == EINTR))); + + if (child_detached) + exit(0); + else if (rc == pid) + exit(WIFEXITED(status)); + else + die("daemonize: waitpid failed"); + + __builtin_unreachable(); + } +} + +static void daemonize_finish(void) +{ + /* + * setsid() will also set the sid to be the pgid to all currently + * running threads in the process group (such as the tsync thread). + */ + if (setsid() == -1) + die("daemonize: setsid"); + + if (kill(getppid(), SIGRTMIN) == -1) + die("daemonize: kill"); +} + static void trace_or_sleep(enum trace_type type, bool pwait) { int i; @@ -1716,6 +1821,9 @@ static void run_cmd(enum trace_type type, const char *user, int argc, char **arg die("Failed to exec %s", argv[0]); } } + + if (do_daemonize) + daemonize_finish(); if (fork_process) exit(0); if (do_ptrace) { @@ -5757,6 +5865,7 @@ enum { OPT_proxy = 261, OPT_temp = 262, OPT_notimeout = 264, + OPT_daemonize = 265, }; void trace_stop(int argc, char **argv) @@ -6183,6 +6292,7 @@ static void parse_record_options(int argc, {"file-version", required_argument, NULL, OPT_file_ver}, {"proxy", required_argument, NULL, OPT_proxy}, {"temp", required_argument, NULL, OPT_temp}, + {"daemonize", no_argument, NULL, OPT_daemonize}, {NULL, 0, NULL, 0} }; @@ -6656,6 +6766,10 @@ static void parse_record_options(int argc, die("--fork option used for 'start' command only"); fork_process = true; break; + case OPT_daemonize: + if (!IS_RECORD(ctx)) + die("--daemonize option used for 'record' command only"); + do_daemonize = true; case OPT_tsc2nsec: ret = get_tsc_nsec(&ctx->tsc2nsec.shift, &ctx->tsc2nsec.mult); @@ -6899,6 +7013,9 @@ static void record_trace(int argc, char **argv, struct buffer_instance *instance; struct filter_pids *pid; + if (do_daemonize) + daemonize_start(); + /* * If top_instance doesn't have any plugins or events, then * remove it from being processed. @@ -7017,8 +7134,15 @@ static void record_trace(int argc, char **argv, } } } - /* sleep till we are woken with Ctrl^C */ - printf("Hit Ctrl^C to stop recording\n"); + + if (do_daemonize) { + daemonize_finish(); + printf("Send SIGINT to pid %d to stop recording\n", getpid()); + } else { + /* sleep till we are woken with Ctrl^C */ + printf("Hit Ctrl^C to stop recording\n"); + } + for_all_instances(instance) { /* If an instance is not tracing individual processes * or there is an error while waiting for a process to diff --git a/tracecmd/trace-usage.c b/tracecmd/trace-usage.c index 42a8e7d..0630e8e 100644 --- a/tracecmd/trace-usage.c +++ b/tracecmd/trace-usage.c @@ -81,6 +81,7 @@ static struct usage_help usage_help[] = { " available algorithms can be listed with trace-cmd list -c\n" " --proxy vsocket to reach the agent. Acts the same as -A (for an agent)\n" " but will send the proxy connection to the agent.\n" + " --daemonize run trace-cmd in the background as a daemon after recording has started\n" }, { "set", From patchwork Mon May 1 20:31:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: avidanborisov@gmail.com X-Patchwork-Id: 13228279 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1472FC77B73 for ; Mon, 1 May 2023 20:32:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232604AbjEAUcJ (ORCPT ); Mon, 1 May 2023 16:32:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232579AbjEAUcI (ORCPT ); Mon, 1 May 2023 16:32:08 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15E1A1FFD for ; Mon, 1 May 2023 13:32:06 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-94f6c285d22so616002566b.2 for ; Mon, 01 May 2023 13:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682973125; x=1685565125; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Uu4++7Xe7wTeAu/1Ko96Y99YyVYN3adXsS8XZc485Do=; b=lJXFlJTIMA3a8f6PKT/ogrn1EYxgrsCdSf7zqa0mq5aeySf2TFMwB/eF9YvbE48jT8 kJ2Rl1Fqie6VgT8hLBNImjQlnt7obwFo9aKELINwZ6WiyxoGkBGDGQ2aw5dsrX1+jKDt FD8fxDgOVSVOmOFpomuxkwJrUE7EtunLmyCCTNMjRCOdFAZte9WB5C0U5FCkQXZWKD0t GBYeJojX1EubfhA8cKyiwGAnwoIzTcmlZ7iF7Zz6kv/2gnsxHP2Je7WedWK1FXBsnCh3 gV+Y+Y2BjLSRnkKsTACJMk8f3FwoVc7+V9/LdlPaDpFxXYAcUChiWiXiI17ZCQ5eKDUZ zu5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682973125; x=1685565125; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Uu4++7Xe7wTeAu/1Ko96Y99YyVYN3adXsS8XZc485Do=; b=VgQgEJUfL5rjSt1lhIvWcCvDXgIuRGMhBLofflGJHLvTxux1UC0VGuQ0eb8YblOsmj C+6WOl60YKBy36JHfRNkbR+C06Nl44/7CNM5Ju1ij3kD8Jq2x3i3nJSEfRW6Nb50M5xr VPyjy1lCpbQORMUSztJ0jFOkB7TFLbakr3P+D9T40Xew2bERJxYTV57AJ3+0RuwQLEZd sodsbnJHh5PKGdAkg5bVgs3DwVT78gn9zBanNGHrZnfzdGj/8bTrBN3VIh+lLG2esXDT T0pyM5cp2iFTJvZdltUCHkruSvr8yCn53WWm3QBxVU3zRsU4aXO3/9asUFEQ8W09HCCW CXEQ== X-Gm-Message-State: AC+VfDyqM40GMfYtzMiWUu1+ZzCckViw0qzChsX4EC1oYGgBK/t8i0r0 66wkQz72eOeeuArRJzgTh56ZajBLtkU= X-Google-Smtp-Source: ACHHUZ4YtnFU4NqSy6oUbfxF8zSc9EBOtRSklpW9SyhwoVbkpHhkIUflBxLr8itMN2bEBEVROKNKHA== X-Received: by 2002:a17:907:3d94:b0:947:72bd:f72a with SMTP id he20-20020a1709073d9400b0094772bdf72amr16224971ejc.72.1682973125208; Mon, 01 May 2023 13:32:05 -0700 (PDT) Received: from dante731.cslcs.technion.ac.il ([132.68.206.91]) by smtp.gmail.com with ESMTPSA id i25-20020a170906851900b0094f1b8901e1sm15233933ejx.68.2023.05.01.13.32.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 May 2023 13:32:05 -0700 (PDT) From: avidanborisov@gmail.com To: linux-trace-devel@vger.kernel.org Cc: Avidan Borisov Subject: [PATCH 2/3] trace-cmd: export pidfile functions from trace-listen.c Date: Mon, 1 May 2023 23:31:17 +0300 Message-Id: <20230501203118.3105605-3-avidanborisov@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230501203118.3105605-1-avidanborisov@gmail.com> References: <20230501203118.3105605-1-avidanborisov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Avidan Borisov 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. Signed-off-by: Avidan Borisov --- tracecmd/include/trace-local.h | 4 ++++ 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 ae208fb..73e93dc 100644 --- a/tracecmd/include/trace-local.h +++ b/tracecmd/include/trace-local.h @@ -457,4 +457,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 e95c571..5894a92 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) From patchwork Mon May 1 20:31:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: avidanborisov@gmail.com X-Patchwork-Id: 13228280 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F5F7C77B73 for ; Mon, 1 May 2023 20:32:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232579AbjEAUcO (ORCPT ); Mon, 1 May 2023 16:32:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232627AbjEAUcN (ORCPT ); Mon, 1 May 2023 16:32:13 -0400 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2DE32693 for ; Mon, 1 May 2023 13:32:11 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-95369921f8eso480754366b.0 for ; Mon, 01 May 2023 13:32:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682973130; x=1685565130; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TbsPqu0NmMx050DBeWWzLXTJFATKv4SINn0Iil4PKTg=; b=OL18do9ILd+I7romC3Q9hUTN9OfDxb+5RgZRfG5CUJCg8N4523JlG5GTTE1Lr5xoVz WiKTA5aMeVwrQsEl4ToIMwpwDaxO52ops7GaOoM0RDgmluO41/dztetoRZEaEX9cjcBo 4s/rns3Uou82VB4qPnZCJ0cqhk1mSZiNm9qvMSo6lLUs/00SRoTjpaF1l2Jd4yIrGSNF GE8d1Xj623g10i1SFT7TD/HNapHuEf4O9i/t7Y2SjJn4mdaMEEVmQW5xquuLIhvoZSwl Tv8L38vqG16RTRyYzK28ruHvNkUz7mLXvGTI+ZW99oeg5ewLd3X1/+WkVnjmO1+DHNP2 9Xeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682973130; x=1685565130; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TbsPqu0NmMx050DBeWWzLXTJFATKv4SINn0Iil4PKTg=; b=Ma6I7DT8GB6tkGBUFuL2TI/L4ZERqfOQzckPx0Vz2FbnXmeFTv/FG8gRryl/QrZolL F0dFwylndmNkPP41OEo5EwDonBnk6vL0hIhYq+oPXxGh9/UTgmlCacATMUucrJDMNJKA 89qgDr1YJ8k+d2EY7tIxNAOWX4RrkWaFhrF5PfnNGK/DVnRR9qX/MhedLdpsk/pR+YJ2 92WsZGvtw6tx6R4hJYeRqJBNJwxYNitWMwQdHn83P+lJNHis5uoz7ev8Dnp3lFCiIfpN c/B3SofmYVLa7FV5f5SuJecb1qSAc7axQ5sx8nd+eEcA+uxivOJCNr0JQMB9kKiulrDF Jg1g== X-Gm-Message-State: AC+VfDyn64l+FqvDj7wNo/0+Hk6lQoznorDEBgQg8oYssbPHk7q8qgxU UK4bCE/9JxbE4KtPzLSqIZg08SxvV+U= X-Google-Smtp-Source: ACHHUZ68A2UqXDFA1ZWud2MRt3ue9zBQ0MQIaN8wL3zzdFt54VCjcG1VfWt135N+hl/BTbzfaM0xEA== X-Received: by 2002:a17:907:9688:b0:94f:61f5:9ef7 with SMTP id hd8-20020a170907968800b0094f61f59ef7mr14540187ejc.44.1682973129761; Mon, 01 May 2023 13:32:09 -0700 (PDT) Received: from dante731.cslcs.technion.ac.il ([132.68.206.91]) by smtp.gmail.com with ESMTPSA id i25-20020a170906851900b0094f1b8901e1sm15233933ejx.68.2023.05.01.13.32.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 May 2023 13:32:09 -0700 (PDT) From: avidanborisov@gmail.com To: linux-trace-devel@vger.kernel.org Cc: Avidan Borisov Subject: [PATCH 3/3] trace-cmd record: Create a pidfile when using --daemonize Date: Mon, 1 May 2023 23:31:18 +0300 Message-Id: <20230501203118.3105605-4-avidanborisov@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230501203118.3105605-1-avidanborisov@gmail.com> References: <20230501203118.3105605-1-avidanborisov@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-trace-devel@vger.kernel.org From: Avidan Borisov When used with --daemonize, create a pidfile at /var/run/trace-cmd-record.pid during the duration of the recording, and delete it after we're done. Signed-off-by: Avidan Borisov --- Documentation/trace-cmd/trace-cmd-record.1.txt | 1 + tracecmd/trace-record.c | 13 +++++++++++++ tracecmd/trace-usage.c | 4 +++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Documentation/trace-cmd/trace-cmd-record.1.txt b/Documentation/trace-cmd/trace-cmd-record.1.txt index 366ab46..0c9a914 100644 --- a/Documentation/trace-cmd/trace-cmd-record.1.txt +++ b/Documentation/trace-cmd/trace-cmd-record.1.txt @@ -420,6 +420,7 @@ OPTIONS *--daemonize* Run trace-cmd in the background as a daemon after recording has started. + Creates a pidfile at /var/run/trace-cmd-record.pid with the pid of trace-cmd during the recording. EXAMPLES -------- diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c index 0eee8dd..85ad808 100644 --- a/tracecmd/trace-record.c +++ b/tracecmd/trace-record.c @@ -46,6 +46,8 @@ #define _STR(x) #x #define STR(x) _STR(x) +#define RECORD_PIDFILE "trace-cmd-record.pid" + #define TRACE_CTRL "tracing_on" #define TRACE "trace" #define AVAILABLE "available_tracers" @@ -93,6 +95,8 @@ static bool fork_process; static bool do_daemonize; +static bool created_pidfile; + /* Max size to let a per cpu file get */ static int max_kb; @@ -648,6 +652,9 @@ void die(const char *fmt, ...) else ret = -1; + if (created_pidfile) + remove_pid_file(RECORD_PIDFILE); + kill_threads(); va_start(ap, fmt); fprintf(stderr, " "); @@ -1739,6 +1746,9 @@ static void daemonize_finish(void) if (kill(getppid(), SIGRTMIN) == -1) die("daemonize: kill"); + + make_pid_file(RECORD_PIDFILE); + created_pidfile = true; } static void trace_or_sleep(enum trace_type type, bool pwait) @@ -7190,6 +7200,9 @@ static void record_trace(int argc, char **argv, destroy_stats(); finalize_record_trace(ctx); + + if (created_pidfile) + remove_pid_file(RECORD_PIDFILE); } /* diff --git a/tracecmd/trace-usage.c b/tracecmd/trace-usage.c index 0630e8e..2e5d861 100644 --- a/tracecmd/trace-usage.c +++ b/tracecmd/trace-usage.c @@ -81,7 +81,9 @@ static struct usage_help usage_help[] = { " available algorithms can be listed with trace-cmd list -c\n" " --proxy vsocket to reach the agent. Acts the same as -A (for an agent)\n" " but will send the proxy connection to the agent.\n" - " --daemonize run trace-cmd in the background as a daemon after recording has started\n" + " --daemonize run trace-cmd in the background as a daemon after recording has started.\n" + " creates a pidfile at /var/run/trace-cmd-record.pid with the pid of trace-cmd\n" + " during the recording.\n" }, { "set",