summaryrefslogtreecommitdiff
path: root/sbin/modload/main.c
blob: aec7a7ba71c2905368997a0c9538a718420e921b (plain)
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
293
/*	$NetBSD: main.c,v 1.18 2020/06/07 05:49:05 thorpej Exp $	*/

/*-
 * Copyright (c) 2008 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: main.c,v 1.18 2020/06/07 05:49:05 thorpej Exp $");
#endif /* !lint */

#include <sys/module.h>
#include <sys/queue.h>

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>

#include <prop/proplib.h>

#include "prog_ops.h"

static void	parse_bool_param(prop_dictionary_t, const char *,
				 const char *);
static void	parse_int_param(prop_dictionary_t, const char *,
				const char *);
static void	parse_param(prop_dictionary_t, const char *,
			    void (*)(prop_dictionary_t, const char *,
			    const char *));
static void	parse_string_param(prop_dictionary_t, const char *,
				   const char *);
static void	usage(void) __dead;
static void	merge_dicts(prop_dictionary_t, const prop_dictionary_t);

int
main(int argc, char **argv)
{
	SIMPLEQ_HEAD(del_head, del_item) del_head;
	modctl_load_t cmdargs;
	prop_dictionary_t ext_props, props;
	bool del_props, merge_props, output_props;
	const char *ext_file;
	char *propsstr;
	int ch;
	int flags;

	struct del_item {
		SIMPLEQ_ENTRY(del_item) del_items;
		const char *del_key;
	} *delp;

	SIMPLEQ_INIT(&del_head);
	ext_file = NULL;
	ext_props = NULL;
	props = prop_dictionary_create();
	del_props = merge_props = output_props = false;
	flags = 0;

	while ((ch = getopt(argc, argv, "Pb:d:fi:m:ps:")) != -1) {
		switch (ch) {
		case 'P':
			flags |= MODCTL_NO_PROP;
			break;
		case 'b':
			parse_param(props, optarg, parse_bool_param);
			break;

		case 'd':
			del_props = true;
			delp = malloc(sizeof(struct del_item));
			if (delp == NULL)
				errx(EXIT_FAILURE, "Out of memory");
			delp->del_key = optarg;
			SIMPLEQ_INSERT_TAIL(&del_head, delp, del_items);
			break;

		case 'f':
			flags |= MODCTL_LOAD_FORCE;
			break;

		case 'i':
			parse_param(props, optarg, parse_int_param);
			break;

		case 'm':
			merge_props = true;
			ext_file = optarg;
			break;

		case 'p':
			output_props = true;
			break;

		case 's':
			parse_param(props, optarg, parse_string_param);
			break;

		default:
			usage();
			/* NOTREACHED */
		}
	}

	argc -= optind;
	argv += optind;

	propsstr = prop_dictionary_externalize(props);
	if (propsstr == NULL)
		errx(EXIT_FAILURE, "Failed to process properties");

	if (output_props) {
		if (merge_props) {
			ext_props =
			    prop_dictionary_internalize_from_file(ext_file);
			if (ext_props == NULL) {
				errx(EXIT_FAILURE, "Failed to read existing "
				    "property list");
			}

			free(propsstr);
			merge_dicts(ext_props, props);

			if (del_props)
				SIMPLEQ_FOREACH(delp, &del_head, del_items)
					prop_dictionary_remove(ext_props,
					    delp->del_key);

			propsstr = prop_dictionary_externalize(ext_props);
			if (propsstr == NULL)
				errx(EXIT_FAILURE, "Failed to process "
				    "properties");
		}
				
		fputs(propsstr, stdout);
	} else {
		if (argc != 1)
			usage();
		if (prog_init && prog_init() == -1)
			err(1, "prog init failed");
		cmdargs.ml_filename = argv[0];
		cmdargs.ml_flags = flags;
		cmdargs.ml_props = propsstr;
		cmdargs.ml_propslen = strlen(propsstr);

		if (prog_modctl(MODCTL_LOAD, &cmdargs)) {
			err(EXIT_FAILURE, "%s", cmdargs.ml_filename);
		}
	}

	free(propsstr);
	prop_object_release(props);

	exit(EXIT_SUCCESS);
}

static void
parse_bool_param(prop_dictionary_t props, const char *name,
		 const char *value)
{
	bool boolvalue;

	assert(name != NULL);
	assert(value != NULL);

	if (strcasecmp(value, "1") == 0 ||
	    strcasecmp(value, "true") == 0 ||
	    strcasecmp(value, "yes") == 0)
		boolvalue = true;
	else if (strcasecmp(value, "0") == 0 ||
	    strcasecmp(value, "false") == 0 ||
	    strcasecmp(value, "no") == 0)
		boolvalue = false;
	else
		errx(EXIT_FAILURE, "Invalid boolean value `%s'", value);

	if (!prop_dictionary_set_bool(props, name, boolvalue))
		err(EXIT_FAILURE, "prop_dictionary_set_bool");
}

static void
parse_int_param(prop_dictionary_t props, const char *name,
		const char *value)
{
	int64_t intvalue;

	assert(name != NULL);
	assert(value != NULL);

	if (dehumanize_number(value, &intvalue) != 0)
		err(EXIT_FAILURE, "Invalid integer value `%s'", value);

	if (!prop_dictionary_set_int64(props, name, intvalue))
		err(EXIT_FAILURE, "prop_dictionary_set_int64");
}

static void
parse_param(prop_dictionary_t props, const char *origstr,
	    void (*fmt_handler)(prop_dictionary_t, const char *, const char *))
{
	char *name, *value;

	name = strdup(origstr);

	value = strchr(name, '=');
	if (value == NULL) {
		free(name);
		errx(EXIT_FAILURE, "Invalid parameter `%s'", origstr);
	}
	*value++ = '\0';

	fmt_handler(props, name, value);

	free(name);
}

static void
parse_string_param(prop_dictionary_t props, const char *name,
		   const char *value)
{

	assert(name != NULL);
	assert(value != NULL);

	if (!prop_dictionary_set_string(props, name, value))
		err(EXIT_FAILURE, "prop_dictionary_set_string");
}

static void
usage(void)
{

	(void)fprintf(stderr,
	    "Usage: %s [-fP] [-b var=boolean] [-i var=integer] "
	    "[-s var=string] module\n"
	    "       %s -p [-b var=boolean] [-d var] [-i var=integer] "
	    "[-m plist]\n               [-s var=string]\n",
	    getprogname(), getprogname());
	exit(EXIT_FAILURE);
}

static void
merge_dicts(prop_dictionary_t existing_dict, const prop_dictionary_t new_dict)
{
	prop_dictionary_keysym_t props_keysym;
	prop_object_iterator_t props_iter;
	prop_object_t props_obj;
	const char *props_key;

	props_iter = prop_dictionary_iterator(new_dict);
	if (props_iter == NULL) {
		errx(EXIT_FAILURE, "Failed to iterate new property list");
	}

	while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) {
		props_keysym = (prop_dictionary_keysym_t)props_obj;
		props_key = prop_dictionary_keysym_value(props_keysym);
		props_obj = prop_dictionary_get_keysym(new_dict, props_keysym);
		if ((props_obj == NULL) || !prop_dictionary_set(existing_dict,
		    props_key, props_obj)) {
			errx(EXIT_FAILURE, "Failed to copy "
			    "existing property list");
		}
	}
	prop_object_iterator_release(props_iter);

	return;
}