[minicoredumper] [PATCH 02/14] minicoredumper: Add command line options to specify the dst_dir and coredump file name

Mateusz Moscicki m.moscicki2 at partner.samsung.com
Tue May 21 14:52:38 CEST 2019


Specyfying these values may be needed when the minicoredumper is run by
another script or program.

This change doesn't break compatibility.

Change-Id: If4922bf6ca620607c4f5b8a1f79b7e7022d487eb
---
 src/minicoredumper/corestripper.c   | 69 +++++++++++++++++++++++++++----------
 src/minicoredumper/corestripper.h   |  2 ++
 src/minicoredumper/minicoredumper.1 | 12 +++++++
 3 files changed, 65 insertions(+), 18 deletions(-)

diff --git a/src/minicoredumper/corestripper.c b/src/minicoredumper/corestripper.c
index 48fa4e5..350927f 100644
--- a/src/minicoredumper/corestripper.c
+++ b/src/minicoredumper/corestripper.c
@@ -52,6 +52,10 @@
 #define PTRACE_INTERRUPT 0x4207
 #endif
 
+#define DEFAULT_BASE_NAME "core"
+
+char cmd_options[] = "o:d:";
+
 static struct dump_info *global_di;
 static long PAGESZ;
 
@@ -632,7 +636,7 @@ static int init_di(struct dump_info *di)
 		return 1;
 
 	if (di->cmd_params->signum != 0) {
-		if (asprintf(&tmp_path, "/core-%s-%d", comm_base,
+		if (asprintf(&tmp_path, "/%s-%s-%d", di->cmd_params->base_name, comm_base,
 			     di->cmd_params->pid) == -1) {
 			return 1;
 		}
@@ -648,7 +652,7 @@ static int init_di(struct dump_info *di)
 		shm_unlink(tmp_path);
 		free(tmp_path);
 
-		if (asprintf(&tmp_path, "%s/core", di->dst_dir) == -1)
+		if (asprintf(&tmp_path, "%s/%s", di->dst_dir, di->cmd_params->base_name) == -1)
 			return 1;
 		di->core_path = tmp_path;
 
@@ -1111,7 +1115,7 @@ static int open_compressor(struct dump_info *di, const char *core_suffix,
 
 	*path = NULL;
 
-	if (asprintf(&tmp_path, "%s/core%s.%s", di->dst_dir, core_suffix,
+	if (asprintf(&tmp_path, "%s/%s%s.%s", di->dst_dir, di->cmd_params->base_name, core_suffix,
 		     ext ? ext : "compressed") == -1) {
 		return -1;
 	}
@@ -3629,8 +3633,13 @@ static int do_all_dumps(struct dump_info *di)
 		comm_base = p + 1;
 	}
 
-	di->dst_dir = alloc_dst_dir(di->cmd_params->timestamp, cfg->base_dir,
-				    comm_base, di->cmd_params->pid);
+	if (!di->cmd_params->dst_dir) {
+		di->dst_dir = alloc_dst_dir(di->cmd_params->timestamp, cfg->base_dir,
+						comm_base, di->cmd_params->pid);
+	} else {
+		di->dst_dir = strdup(di->cmd_params->dst_dir);
+	}
+
 	if (!di->dst_dir)
 		return 1;
 
@@ -3724,36 +3733,60 @@ typedef enum {
 
 struct cmd_parameters* parse_args(char *argv[], int argc)
 {
-	if (argc < (ARG_EXE_NAME + 1) || argc > (ARG_CONF_PATH + 1))
-		fatal("wrong amount of command line parameters");
-
 	struct cmd_parameters *cmd_params = (struct cmd_parameters*)malloc(sizeof(struct cmd_parameters));
 	if (cmd_params == NULL)
 		fatal("malloc for cmd_parameters error: %m");
 
 	char *p;
+	int opt;
+
+	cmd_params->base_name = NULL;
+	cmd_params->dst_dir = NULL;
 
-	cmd_params->pid = strtol(argv[ARG_PID], &p, 10);
+	while ((opt = getopt(argc, argv, cmd_options)) != -1) {
+		switch (opt) {
+		case 'o':
+			cmd_params->base_name = optarg;
+			break;
+		case 'd':
+			cmd_params->dst_dir = optarg;
+			break;
+		}
+	}
+
+	int opts_count = optind - 1;
+
+	if (argc  < (ARG_EXE_NAME + opts_count + 1) || argc > (ARG_CONF_PATH + opts_count + 1)) {
+		fatal("wrong amount of command line parameters: %d", opts_count);
+	}
+
+	if (cmd_params->base_name == NULL)
+		cmd_params->base_name = DEFAULT_BASE_NAME;
+
+	cmd_params->pid = strtol(argv[ARG_PID + opts_count], &p, 10);
 	if (*p != 0)
 		fatal("invalid pid");
 
-	cmd_params->uid = strtol(argv[ARG_UID], &p, 10);
+	info("pid: %d", cmd_params->pid);
+	cmd_params->uid = strtol(argv[ARG_UID + opts_count], &p, 10);
 	if (*p != 0)
 		fatal("invalid uid");
 
-	cmd_params->gid = strtol(argv[ARG_GID], &p, 10);
+	cmd_params->gid = strtol(argv[ARG_GID + opts_count], &p, 10);
+
 	if (*p != 0)
 		fatal("invalid gid");
+	info("gid: %d", cmd_params->gid);
 
-	cmd_params->signum = strtol(argv[ARG_SIGNUM], &p, 10);
+	cmd_params->signum = strtol(argv[ARG_SIGNUM + opts_count], &p, 10);
 	if (*p != 0)
 		fatal("invalid signum");
 
-	cmd_params->timestamp = strtol(argv[ARG_TIMESTAMP], &p, 10);
+	cmd_params->timestamp = strtol(argv[ARG_TIMESTAMP + opts_count], &p, 10);
 	if (*p != 0)
 		fatal("invalid timestamp");
 
-	cmd_params->hostname = argv[ARG_HOSTNAME];
+	cmd_params->hostname = argv[ARG_HOSTNAME + opts_count];
 	if (!cmd_params->hostname)
 		fatal("invalid hostname");
 
@@ -3761,11 +3794,11 @@ struct cmd_parameters* parse_args(char *argv[], int argc)
 	if (!cmd_params->exe_name)
 		fatal("invalid exe filename");
 
-	if (argc == ARG_CONF_PATH) {
+	if (argc == ARG_CONF_PATH + opts_count) {
 		cmd_params->cfg_path = MCD_CONF_PATH "/minicoredumper.cfg.json";
-	} else if (argc == ARG_CONF_PATH + 1) {
-		info("using custom minicoredumper cfg: %s", argv[ARG_CONF_PATH]);
-		cmd_params->cfg_path = argv[ARG_CONF_PATH];
+	} else if (argc == ARG_CONF_PATH + opts_count + 1) {
+		info("using custom minicoredumper cfg: %s", argv[ARG_CONF_PATH + opts_count]);
+		cmd_params->cfg_path = argv[ARG_CONF_PATH + opts_count];
 	} else {
 		fatal("wrong arg count, check /proc/sys/kernel/core_pattern");
 	}
diff --git a/src/minicoredumper/corestripper.h b/src/minicoredumper/corestripper.h
index e1511f8..22da645 100644
--- a/src/minicoredumper/corestripper.h
+++ b/src/minicoredumper/corestripper.h
@@ -51,6 +51,8 @@ struct cmd_parameters {
 	char *hostname;
 	char *cfg_path;
 	char *exe_name;
+	char *dst_dir;
+	char *base_name;
 };
 
 struct dump_info {
diff --git a/src/minicoredumper/minicoredumper.1 b/src/minicoredumper/minicoredumper.1
index e2212dd..91c94e2 100644
--- a/src/minicoredumper/minicoredumper.1
+++ b/src/minicoredumper/minicoredumper.1
@@ -35,6 +35,18 @@ dump facility. By default the main configuration file is:
 .PP
 but can be overridden if the optional 8th argument is specified.
 .PP
+Additionally these options can be specified:
+.PP
+.TP
+.BI \-o \ core_filename
+Specify coredump base file name. \fBtar\fR
+or
+.BR gz
+extension will be added if needed.
+.TP
+.BI \-d \ dest_dir
+Specify the output directory.
+.PP
 .BR minicoredumper
 uses
 .BR syslog (3)
-- 
2.7.4




More information about the minicoredumper mailing list