1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #pragma ident "%Z%%M% %I% %E% SMI"
28
29 #include <sys/types.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <sys/wait.h>
33 #include <sys/errno.h>
34 #include <sys/modctl.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <svm.h>
39
40 /*
41 * FUNCTION: get modid
42 * Given a module name returns module id.
43 *
44 * INPUT: module name
45 *
46 * RETURN VALUES:
47 * > 0 SUCCESS
48 * -1 FAIL
49 */
50
51 static int
get_modid(char * modname)52 get_modid(char *modname)
53 {
54 struct modinfo modinfo;
55 int id;
56 int rval = RET_ERROR;
57
58 id = -1; /* look for all modules */
59
60 modinfo.mi_id = modinfo.mi_nextid = id;
61 modinfo.mi_info = MI_INFO_ALL | MI_INFO_NOBASE;
62
63 do {
64 if (modctl(MODINFO, id, &modinfo) < 0)
65 break;
66
67 modinfo.mi_name[MODMAXNAMELEN - 1] = '\0';
68 /* if we find a match break out */
69 if (strcmp(modinfo.mi_name, modname) == 0) {
70 rval = modinfo.mi_id;
71 break;
72 }
73 /* LINTED */
74 } while (1);
75
76 return (rval);
77 }
78
79 /*
80 * FUNCTION: mod_unload
81 * unload a module.
82 *
83 * INPUT: module name
84 *
85 * RETURN VALUES:
86 * 0 - SUCCESS
87 * !0 - FAIL
88 * > 0 errno
89 * -1
90 * NOTE: If we fail to get the module id because the module is not
91 * currently loaded we still want to try to force a reload of the
92 * .conf file when it does load.
93 */
94 int
mod_unload(char * modname)95 mod_unload(char *modname)
96 {
97 int id;
98 major_t major;
99 int rval = RET_SUCCESS;
100
101 id = get_modid(modname);
102
103 if (id != -1) {
104 if (modctl(MODUNLOAD, id) < 0) {
105 rval = errno;
106 }
107 }
108
109 if ((modctl(MODGETMAJBIND, modname, strlen(modname) + 1,
110 &major)) != 0) {
111 return (errno);
112 }
113
114 if ((modctl(MODUNLOADDRVCONF, major) != 0) ||
115 (modctl(MODLOADDRVCONF, major) != 0)) {
116 return (errno);
117 }
118
119 return (rval);
120 }
121