xref: /titanic_51/usr/src/cmd/modload/modunload.c (revision 791a814c934fcd4deb13b26c1f116ff283272a0d)
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 2003 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 <strings.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <sys/modctl.h>
39 
40 void	usage();
41 void	exec_userfile(char *execfile, int id, char **envp);
42 
43 extern void fatal(char *fmt, ...);
44 extern void error(char *fmt, ...);
45 
46 /*
47  * Unload a loaded module.
48  */
49 int
50 main(int argc, char *argv[], char *envp[])
51 {
52 	int child;
53 	int status;
54 	int id;
55 	char *execfile = NULL;
56 	int opt;
57 	extern char *optarg;
58 
59 	if (argc < 3)
60 		usage();
61 
62 	while ((opt = getopt(argc, argv, "i:e:")) != -1) {
63 		switch (opt) {
64 		case 'i':
65 			if (sscanf(optarg, "%d", &id) != 1)
66 				fatal("Invalid id %s\n", optarg);
67 			break;
68 		case 'e':
69 			execfile = optarg;
70 		}
71 	}
72 
73 	if (execfile) {
74 		child = fork();
75 		if (child == -1)
76 			error("can't fork %s", execfile);
77 		else if (child == 0)
78 			exec_userfile(execfile, id, envp);
79 		else {
80 			(void) wait(&status);
81 			if (status != 0) {
82 				(void) printf("%s returned error %d.\n",
83 				    execfile, status);
84 				(void) exit(status >> 8);
85 			}
86 		}
87 	}
88 
89 	/*
90 	 * Unload the module.
91 	 */
92 	if (modctl(MODUNLOAD, id) < 0) {
93 	    if (errno == EPERM)
94 		fatal("Insufficient privileges to unload a module\n");
95 	    else if (id != 0)
96 		error("can't unload the module");
97 	}
98 
99 	return (0);			/* success */
100 }
101 
102 /*
103  * exec the user file.
104  */
105 void
106 exec_userfile(char *execfile, int id, char **envp)
107 {
108 	struct modinfo modinfo;
109 
110 	char modid[8];
111 	char mod0[8];
112 
113 	modinfo.mi_id = modinfo.mi_nextid = id;
114 	modinfo.mi_info = MI_INFO_ONE;
115 	if (modctl(MODINFO, id, &modinfo) < 0)
116 		error("can't get module information");
117 
118 	(void) sprintf(modid, "%d", id);
119 	(void) sprintf(mod0, "%d", modinfo.mi_msinfo[0].msi_p0);
120 
121 	(void) execle(execfile, execfile, modid, mod0, NULL, envp);
122 
123 	error("couldn't exec %s\n", execfile);
124 }
125 
126 
127 void
128 usage()
129 {
130 	fatal("usage:  modunload -i <module_id> [-e <exec_file>]\n");
131 }
132