[alsa-devel] [RFCv2][PATCH 35/38] axfer: add a feature to generate a file for process id

Takashi Sakamoto o-takashi at sakamocchi.jp
Tue Sep 19 02:44:15 CEST 2017


In current implementation of aplay, during transferring data frames, the
program generates a file with PID in given path for an option
'--process-id-file'.

This commit adds support for this feature. However, usually, an idea of
process ID file is used for programs such that they run several
sub-processes and one of them have significant role to maintain the other
sub-processes. Content of the process ID file includes PID for the
significant sub-process to avoid sending Unix signals to the other
processes. Apparently, aplay and this program are not this kind of program.
This feature makes less sense because process ID for this program can be
found by usual tools such as ps(1) or pgrep(1) which seek nodes on procfs.

Signed-off-by: Takashi Sakamoto <o-takashi at sakamocchi.jp>
---
 axfer/options.c | 41 +++++++++++++++++++++++++++++++++++++++--
 axfer/options.h |  1 +
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/axfer/options.c b/axfer/options.c
index 335d8af3..9bde51bf 100644
--- a/axfer/options.c
+++ b/axfer/options.c
@@ -11,6 +11,8 @@
 
 #include <getopt.h>
 #include <math.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 enum no_short_opts {
 	/* 128 belongs to non us-ascii character set. */
@@ -24,6 +26,7 @@ enum no_short_opts {
 	OPT_DISABLE_FORMAT,
 	OPT_DISABLE_SOFTVOL,
 	OPT_USE_STRFTIME,
+	OPT_PROCESS_ID_FILE,
 	/* Obsoleted. */
 	OPT_MAX_FILE_TIME,
 };
@@ -203,7 +206,8 @@ static int apply_policies(struct context_options *opts,
 			  const char *node_literal,
 			  const char *sample_format_literal,
 			  const char *vu_mode_literal,
-			  const char *chmap_literal)
+			  const char *chmap_literal,
+			  const char *pidfile_name_literal)
 {
 	int err;
 
@@ -251,6 +255,30 @@ static int apply_policies(struct context_options *opts,
 			return -ENOMEM;
 	}
 
+	if (pidfile_name_literal) {
+		FILE *fstream;
+
+		opts->pidfile_name = strdup(pidfile_name_literal);
+		if (opts->pidfile_name == NULL) {
+			fprintf(stderr,
+				"Fail to allocate for a name of file with "
+				"process id: %s\n",
+				pidfile_name_literal);
+			return -ENOMEM;
+		}
+
+		unlink(pidfile_name_literal);
+		fstream = fopen(pidfile_name_literal, "w");
+		if (fstream == NULL) {
+			fprintf(stderr,
+				"Fail to generate a file with process id: %s\n",
+				pidfile_name_literal);
+			return -errno;
+		}
+		fprintf(fstream, "%d\n", getpid());
+		fclose(fstream);
+	}
+
 	if (opts->samples_per_frame > 0) {
 		if (opts->samples_per_frame < 1 ||
 		    opts->samples_per_frame > 256) {
@@ -429,6 +457,7 @@ int context_options_init(struct context_options *opts, int argc,
 		{"vumeter",		1, 0, 'V'},
 		{"interactive",		0, 0, 'i'},
 		{"chmap",		1, 0, 'm'},
+		{"process-id-file",	1, 0, OPT_PROCESS_ID_FILE},
 		/* Obsoleted. */
 		{"max-file-time",       1, 0, OPT_MAX_FILE_TIME},
 		{NULL,			0, 0, 0},
@@ -438,6 +467,7 @@ int context_options_init(struct context_options *opts, int argc,
 	const char *sample_format_literal = NULL;
 	const char *vu_mode_literal = NULL;
 	const char *chmap_literal = NULL;
+	const char *pidfile_name_literal = NULL;
 	int l_index = 0;
 	int c;
 	int err = 0;
@@ -510,6 +540,8 @@ int context_options_init(struct context_options *opts, int argc,
 			opts->interactive = true;
 		else if (c == 'm')
 			chmap_literal = optarg;
+		else if (c == OPT_PROCESS_ID_FILE)
+			pidfile_name_literal = optarg;
 		else if (c == OPT_MAX_FILE_TIME) {
 			fprintf(stderr,
 				"An option '--%s' is obsoleted and has no "
@@ -529,7 +561,8 @@ int context_options_init(struct context_options *opts, int argc,
 
 	return apply_policies(opts, direction, cntr_format_literal,
 			      node_literal, sample_format_literal,
-			      vu_mode_literal, chmap_literal);
+			      vu_mode_literal, chmap_literal,
+			      pidfile_name_literal);
 }
 
 /*
@@ -876,6 +909,10 @@ void context_options_destroy(struct context_options *opts)
 {
 	int i;
 
+	if (opts->pidfile_name)
+		unlink(opts->pidfile_name);
+	opts->pidfile_name = NULL;
+
 	if (opts->paths) {
 		for (i = 0; i < opts->path_count; ++i)
 			free(opts->paths[i]);
diff --git a/axfer/options.h b/axfer/options.h
index e90dc6b7..f1989ae6 100644
--- a/axfer/options.h
+++ b/axfer/options.h
@@ -62,6 +62,7 @@ struct context_options {
 	enum vumeter_mode vu_mode;
 	bool interactive;
 	char *chmap_literal;
+	char *pidfile_name;
 };
 
 int context_options_init(struct context_options *opts, int argc,
-- 
2.11.0



More information about the Alsa-devel mailing list