xref: /dragonfly/sbin/kldload/kldload.c (revision 0ca59c34)
1 /*-
2  * Copyright (c) 1997 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sbin/kldload/kldload.c,v 1.6.2.2 2002/12/07 08:44:02 jmallett Exp $
27  * $DragonFly: src/sbin/kldload/kldload.c,v 1.3 2005/04/02 16:04:41 liamfoy Exp $
28  */
29 #include <sys/param.h>
30 #include <sys/linker.h>
31 
32 #include <err.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 static void
39 usage(void)
40 {
41     fprintf(stderr, "usage: kldload [-nv] file ...\n");
42     exit(1);
43 }
44 
45 int
46 main(int argc, char **argv)
47 {
48     int c;
49     int errors;
50     int fileid;
51     int verbose;
52     int check_loaded;
53 
54     errors = 0;
55     verbose = 0;
56     check_loaded = 0;
57 
58     while ((c = getopt(argc, argv, "nv")) != -1)
59 	switch (c) {
60 	case 'n':
61 	    check_loaded = 1;
62 	    break;
63 	case 'v':
64 	    verbose = 1;
65 	    break;
66 	default:
67 	    usage();
68 	}
69     argc -= optind;
70     argv += optind;
71 
72     if (argc == 0)
73 	usage();
74 
75     while (argc-- != 0) {
76 	fileid = kldload(argv[0]);
77 	if (fileid < 0) {
78 	    if (check_loaded != 0 && errno == EEXIST) {
79 		if (verbose)
80 		    printf("%s is already loaded\n", argv[0]);
81 	    } else {
82 		switch (errno) {
83 		case EEXIST:
84 		    warnx("can't load %s: module already loaded or "
85 			"in kernel", argv[0]);
86 		    break;
87 		case ENOEXEC:
88 		    warnx("an error occurred while loading the module. "
89 			"Please check dmesg(8) for more details.");
90 		    break;
91 		default:
92 		    warn("can't load %s", argv[0]);
93 		    break;
94 		}
95 		errors++;
96 	    }
97 	} else {
98 	    if (verbose)
99 		printf("Loaded %s, id=%d\n", argv[0], fileid);
100 	}
101 	argv++;
102     }
103 
104     return errors ? 1 : 0;
105 }
106