[go: up one dir, main page]

File: opts.c

package info (click to toggle)
s390-tools 2.3.0-2~deb10u1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 6,188 kB
  • sloc: ansic: 87,755; sh: 8,398; cpp: 8,384; perl: 3,783; makefile: 1,476; asm: 654
file content (292 lines) | stat: -rw-r--r-- 6,990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * zgetdump - Tool for copying and converting System z dumps
 *
 * Option parsing
 *
 * Copyright IBM Corp. 2001, 2017
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "lib/zt_common.h"
#include "zgetdump.h"

/*
 * Text for --help option
 */
static char help_text[] =
"Usage: zgetdump    DUMP [-s SYS] [-f FMT] > DUMP_FILE\n"
"                -m DUMP [-s SYS] [-f FMT] DIR\n"
"                -i DUMP [-s SYS]\n"
"                -d DUMPDEV\n"
"                -u DIR\n"
"\n"
"The zgetdump tool can read different dump formats from a dump device or from\n"
"a dump file. You can use zgetdump to:\n"
"\n"
"  - Write the dump content to standard output or to a file\n"
"  - Mount the dump content to a Linux directory\n"
"  - Convert a dump to a different dump format\n"
"  - Check if a dump is valid\n"
"  - Check if a DASD contains a valid dump tool.\n"
"\n"
"In the syntax description, DUMP specifies a dump device or dump file to be\n"
"read. The following options are available:\n"
"\n"
"-m, --mount    Mount DUMP to mount point DIR\n"
"-u, --umount   Unmount dump from mount point DIR\n"
"-i, --info     Print DUMP information\n"
"-f, --fmt      Specify target dump format FMT (\"elf\" or \"s390\")\n"
"-s, --select   Select system data SYS (\"kdump\", \"prod\", or \"all\")\n"
"-d, --device   Print DUMPDEV (dump device) information\n"
"-v, --version  Print version information, then exit\n"
"-h, --help     Print this help, then exit\n";

static const char copyright_str[] = "Copyright IBM Corp. 2001, 2017";

/*
 * Select option strings
 */
const char *OPTS_SELECT_KDUMP	= "kdump";
const char *OPTS_SELECT_PROD	= "prod";
const char *OPTS_SELECT_ALL	= "all";

/*
 * Initialize default settings
 */
static void init_defaults(void)
{
	g.prog_name = "zgetdump";
	g.opts.action = ZG_ACTION_STDOUT;
#ifdef __s390x__
	g.opts.fmt = "elf";
#else
	g.opts.fmt = "s390";
#endif
	dfo_set(g.opts.fmt);
}

/*
 * Print "help" hint
 */
static void __noreturn print_usage_exit(void)
{
	STDERR("Try '%s --help' for more information.\n", g.prog_name);
	zg_exit(1);
}

/*
 * Print help text
 */
static void __noreturn print_help_exit(void)
{
	STDOUT("%s", help_text);
	zg_exit(0);
}

/*
 * Print version information
 */
static void __noreturn print_version_exit(void)
{
	STDOUT("%s: Tool for copying and converting dumps version %s\n",
	       g.prog_name, RELEASE_STRING);
	STDOUT("%s\n", copyright_str);
	zg_exit(0);
}

/*
 * Set "--fmt" option
 */
static void fmt_set(const char *fmt)
{
	if (dfo_set(fmt) != 0)
		ERR_EXIT("Invalid target format \"%s\" specified", fmt);
	g.opts.fmt_specified = 1;
	g.opts.fmt = fmt;
}

/*
 * Set "--select" option
 */
static void select_set(const char *select)
{
	if (strcmp(select, OPTS_SELECT_KDUMP) == 0)
		g.opts.select = OPTS_SELECT_KDUMP;
	else if (strcmp(select, OPTS_SELECT_PROD) == 0)
		g.opts.select = OPTS_SELECT_PROD;
	else if (strcmp(select, OPTS_SELECT_ALL) == 0)
		g.opts.select = OPTS_SELECT_ALL;
	else
		ERR_EXIT("Invalid select argument \"%s\" specified", select);
	g.opts.select_specified = 1;
}

/*
 * Set mount point
 */
static void mount_point_set(const char *mount_point)
{
	g.opts.mount_point = zg_strdup(mount_point);
}

/*
 * Set device
 */
static void device_set(const char *path)
{
	g.opts.device = zg_strdup(path);
}

/*
 * Set FUSE debug options
 */
static void argv_fuse_set(char **argv, int argc)
{
	int i;

	g.opts.argv_fuse = argv;
	g.opts.argc_fuse = argc;

	STDERR_PR("Fuse Options: ");
	for (i = 0; i < argc; i++)
		STDERR("%s ", g.opts.argv_fuse[i]);
	STDERR("\n");
}

/*
 * Set action
 */
static void action_set(enum zg_action action)
{
	if (g.opts.action_specified)
		ERR_EXIT("Please specifiy only one of the \"-i\", \"-d\", "
			 "\"-m\" or \"-u\" option");
	g.opts.action = action;
	g.opts.action_specified = 1;
}

/*
 * Verify option combinations
 */
static void verify_opts(void)
{
	if (g.opts.select_specified) {
		if (g.opts.action != ZG_ACTION_MOUNT &&
		    g.opts.action != ZG_ACTION_STDOUT &&
		    g.opts.action != ZG_ACTION_DUMP_INFO)
			ERR_EXIT("The \"--select\" option can only be "
				 "specifed for info, mount, or copy");
	}
	if (!g.opts.fmt_specified)
		return;

	if (g.opts.action == ZG_ACTION_DUMP_INFO)
		ERR_EXIT("The \"--fmt\" option cannot be specified "
			 "together with \"--info\"");
	if (g.opts.action == ZG_ACTION_DEVICE_INFO)
		ERR_EXIT("The \"--fmt\" option cannot be specified "
			 "together with \"--device\"");
	if (g.opts.action == ZG_ACTION_UMOUNT)
		ERR_EXIT("The \"--fmt\" option cannot be specified "
			 "together with \"--umount\"");
}

/*
 * Parse positional arguments
 */
static void parse_pos_args(char *argv[], int argc)
{
	int pos_args = argc - optind;

	switch (g.opts.action) {
	case ZG_ACTION_STDOUT:
	case ZG_ACTION_DUMP_INFO:
	case ZG_ACTION_DEVICE_INFO:
		if (pos_args == 0)
			ERR_EXIT("No device or dump specified");
		if (pos_args > 1 && !g.opts.debug_specified)
			ERR_EXIT("Too many positional parameters specified");
		device_set(argv[optind]);
		break;
	case ZG_ACTION_MOUNT:
		if (pos_args == 0)
			ERR_EXIT("No dump specified");
		if (pos_args == 1)
			ERR_EXIT("No mount point specified");
		if (pos_args > 2 && !g.opts.debug_specified)
			ERR_EXIT("Too many positional parameters specified");
		device_set(argv[optind]);
		mount_point_set(argv[optind + 1]);
		if (g.opts.debug_specified && pos_args > 2)
			argv_fuse_set(&argv[optind + 2], pos_args - 2);
		break;
	case ZG_ACTION_UMOUNT:
		if (pos_args == 0)
			ERR_EXIT("No mount point specified");
		mount_point_set(argv[optind]);
		break;
	}
}

/*
 * Main command line parsing function
 */
void opts_parse(int argc, char *argv[])
{
	int opt, idx;
	static struct option long_opts[] = {
		{"help",    no_argument,       NULL, 'h'},
		{"version", no_argument,       NULL, 'v'},
		{"info",    no_argument,       NULL, 'i'},
		{"device",  no_argument,       NULL, 'd'},
		{"mount",   no_argument,       NULL, 'm'},
		{"umount",  no_argument,       NULL, 'u'},
		{"fmt",     required_argument, NULL, 'f'},
		{"select",  required_argument, NULL, 's'},
		{"debug",   no_argument,       NULL, 'X'},
		{NULL,      0,                 NULL,  0 }
	};
	static const char optstr[] = "hvidmus:f:X";

	init_defaults();
	while ((opt = getopt_long(argc, argv, optstr, long_opts, &idx)) != -1) {
		switch (opt) {
		case 'h':
			print_help_exit();
		case 'v':
			print_version_exit();
		case 'i':
			action_set(ZG_ACTION_DUMP_INFO);
			break;
		case 'd':
			action_set(ZG_ACTION_DEVICE_INFO);
			break;
		case 'm':
			action_set(ZG_ACTION_MOUNT);
			break;
		case 'u':
			action_set(ZG_ACTION_UMOUNT);
			break;
		case 'f':
			fmt_set(optarg);
			break;
		case 's':
			select_set(optarg);
			break;
		case 'X':
			g.opts.debug_specified = 1;
			break;
		default:
			print_usage_exit();
		}
	}
	parse_pos_args(argv, argc);
	verify_opts();
}