4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
35 #include <sys/modctl.h>
39 void exec_userfile(char *execfile
, int id
, char **envp
);
41 extern void fatal(char *fmt
, ...);
42 extern void error(char *fmt
, ...);
45 * Unload a loaded module.
48 main(int argc
, char *argv
[], char *envp
[])
53 char *execfile
= NULL
;
60 while ((opt
= getopt(argc
, argv
, "i:e:")) != -1) {
63 if (sscanf(optarg
, "%d", &id
) != 1)
64 fatal("Invalid id %s\n", optarg
);
71 if (getzoneid() != GLOBAL_ZONEID
) {
72 fatal("modunload can only be run from the global zone\n");
78 error("can't fork %s", execfile
);
80 exec_userfile(execfile
, id
, envp
);
84 (void) printf("%s returned error %d.\n",
86 (void) exit(status
>> 8);
94 if (modctl(MODUNLOAD
, id
) < 0) {
96 fatal("Insufficient privileges to unload a module\n");
98 error("can't unload the module");
101 return (0); /* success */
105 * exec the user file.
108 exec_userfile(char *execfile
, int id
, char **envp
)
110 struct modinfo modinfo
;
115 modinfo
.mi_id
= modinfo
.mi_nextid
= id
;
116 modinfo
.mi_info
= MI_INFO_ONE
;
117 if (modctl(MODINFO
, id
, &modinfo
) < 0)
118 error("can't get module information");
120 (void) sprintf(modid
, "%d", id
);
121 (void) sprintf(mod0
, "%d", modinfo
.mi_msinfo
[0].msi_p0
);
123 (void) execle(execfile
, execfile
, modid
, mod0
, NULL
, envp
);
125 error("couldn't exec %s\n", execfile
);
132 fatal("usage: modunload -i <module_id> [-e <exec_file>]\n");