From 30c42b9b3270a8d8a259c90cf99836fa6d94b6be Mon Sep 17 00:00:00 2001 From: Dariusz Stojaczyk Date: Fri, 7 Jul 2017 12:56:04 +0200 Subject: [PATCH] vhost: added option to save pid to file Usage: ./vhost -f path/to/pidfile A text file containing single pid number will be created Change-Id: Ia7762ce3085400a47740c9a5f44b56d63c9fafc6 Signed-off-by: Dariusz Stojaczyk Reviewed-on: https://review.gerrithub.io/368518 Tested-by: SPDK Automated Test System Reviewed-by: Jim Harris Reviewed-by: Daniel Verkamp --- app/vhost/vhost.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/vhost/vhost.c b/app/vhost/vhost.c index 9c7f06c8a..4d90a8113 100644 --- a/app/vhost/vhost.c +++ b/app/vhost/vhost.c @@ -64,6 +64,7 @@ usage(char *executable_name) printf("options:\n"); printf(" -c config config file (default: %s)\n", defaults.config_file); printf(" -e mask tracepoint group mask for spdk trace buffers (default: 0x0)\n"); + printf(" -f pidfile save pid to file under given path\n"); printf(" -m mask reactor core mask (default: 0x1)\n"); printf(" -n channel number of memory channels used for DPDK\n"); printf(" -p core master (primary) core for DPDK\n"); @@ -75,6 +76,21 @@ usage(char *executable_name) printf(" -q disable notice level logging to stderr\n"); } +static void +save_pid(const char *pid_path) +{ + FILE *pid_file; + + pid_file = fopen(pid_path, "w"); + if (pid_file == NULL) { + fprintf(stderr, "Couldn't create pid file '%s': %s\n", pid_path, strerror(errno)); + exit(EXIT_FAILURE); + } + + fprintf(pid_file, "%d\n", getpid()); + fclose(pid_file); +} + int main(int argc, char *argv[]) { @@ -83,10 +99,11 @@ main(int argc, char *argv[]) int rc; const char *socket_path = NULL; enum spdk_log_level print_level = SPDK_LOG_NOTICE; + const char *pid_path = NULL; vhost_app_opts_init(&opts); - while ((ch = getopt(argc, argv, "c:de:m:p:qs:S:t:h")) != -1) { + while ((ch = getopt(argc, argv, "c:de:f:m:p:qs:S:t:h")) != -1) { switch (ch) { case 'c': opts.config_file = optarg; @@ -97,6 +114,9 @@ main(int argc, char *argv[]) case 'e': opts.tpoint_group_mask = optarg; break; + case 'f': + pid_path = optarg; + break; case 'h': usage(argv[0]); exit(EXIT_SUCCESS); @@ -137,6 +157,10 @@ main(int argc, char *argv[]) } } + if (pid_path) { + save_pid(pid_path); + } + if (print_level > SPDK_LOG_WARN && isatty(STDERR_FILENO) && !strncmp(ttyname(STDERR_FILENO), "/dev/tty", strlen("/dev/tty"))) {