1272343Sngie/*	$NetBSD: k_helper3.c,v 1.3 2012/04/17 21:39:19 martin Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2012 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Jukka Ruohonen.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie#include <sys/cdefs.h>
32272343Sngie__RCSID("$NetBSD: k_helper3.c,v 1.3 2012/04/17 21:39:19 martin Exp $");
33272343Sngie
34272343Sngie#include <sys/module.h>
35272343Sngie
36272343Sngie#include <assert.h>
37272343Sngie#include <errno.h>
38272343Sngie#include <stdarg.h>
39272343Sngie#include <stdio.h>
40272343Sngie#include <stdlib.h>
41272343Sngie#include <string.h>
42272343Sngie
43272343Sngie#include <prop/proplib.h>
44272343Sngie
45272343Sngiestatic int load(const char *, ...);
46272343Sngie
47272343Sngie/*
48272343Sngie * A program that loads a module and returns the errno(2) from modctl(8).
49272343Sngie */
50272343Sngieint
51272343Sngiemain(int argc, char *argv[])
52272343Sngie{
53272343Sngie	assert(argc == 3);
54272343Sngie
55272343Sngie	return load(argv[1], argv[2]);
56272343Sngie}
57272343Sngie
58272343Sngiestatic __printflike(1, 2) int
59272343Sngieload(const char *fmt, ...)
60272343Sngie{
61272343Sngie	char filename[MAXPATHLEN], *propsstr, *shortname, *dot;
62272343Sngie	prop_dictionary_t props;
63272343Sngie	modctl_load_t ml;
64272343Sngie	int serrno, rv;
65272343Sngie	va_list ap;
66272343Sngie
67272343Sngie	props = prop_dictionary_create();
68272343Sngie	propsstr = prop_dictionary_externalize(props);
69272343Sngie	assert(propsstr != NULL);
70272343Sngie	prop_object_release(props);
71272343Sngie
72272343Sngie	va_start(ap, fmt);
73272343Sngie	(void)vsnprintf(filename, sizeof(filename), fmt, ap);
74272343Sngie	va_end(ap);
75272343Sngie
76272343Sngie	ml.ml_filename = filename;
77272343Sngie	ml.ml_flags = 0;
78272343Sngie	ml.ml_props = propsstr;
79272343Sngie	ml.ml_propslen = strlen(propsstr);
80272343Sngie
81272343Sngie	printf("Loading module %s\n", filename);
82272343Sngie	errno = serrno = 0;
83272343Sngie
84272343Sngie	rv = modctl(MODCTL_LOAD, &ml);
85272343Sngie
86272343Sngie	if (rv != 0)
87272343Sngie		serrno = errno;
88272343Sngie
89272343Sngie	shortname = strrchr(filename, '/');
90272343Sngie	if (shortname != NULL)
91272343Sngie		shortname++;
92272343Sngie	else
93272343Sngie		shortname = filename;
94272343Sngie	dot = strrchr(shortname, '.');
95272343Sngie	if (dot)
96272343Sngie		*dot = 0;
97272343Sngie	(void)modctl(MODCTL_UNLOAD, shortname);
98272343Sngie
99272343Sngie	free(propsstr);
100272343Sngie
101272343Sngie	return serrno;
102272343Sngie}
103