xref: /onnv-gate/usr/src/cmd/modload/modunload.c (revision 10585:e59699fcc4b6)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*10585SDhanaraj.M@Sun.COM  * Common Development and Distribution License (the "License").
6*10585SDhanaraj.M@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*10585SDhanaraj.M@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/mman.h>
280Sstevel@tonic-gate #include <sys/stat.h>
290Sstevel@tonic-gate #include <sys/wait.h>
300Sstevel@tonic-gate #include <strings.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <sys/modctl.h>
36*10585SDhanaraj.M@Sun.COM #include <zone.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate void	usage();
390Sstevel@tonic-gate void	exec_userfile(char *execfile, int id, char **envp);
400Sstevel@tonic-gate 
410Sstevel@tonic-gate extern void fatal(char *fmt, ...);
420Sstevel@tonic-gate extern void error(char *fmt, ...);
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Unload a loaded module.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate int
main(int argc,char * argv[],char * envp[])480Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	int child;
510Sstevel@tonic-gate 	int status;
520Sstevel@tonic-gate 	int id;
530Sstevel@tonic-gate 	char *execfile = NULL;
540Sstevel@tonic-gate 	int opt;
550Sstevel@tonic-gate 	extern char *optarg;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	if (argc < 3)
580Sstevel@tonic-gate 		usage();
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "i:e:")) != -1) {
610Sstevel@tonic-gate 		switch (opt) {
620Sstevel@tonic-gate 		case 'i':
630Sstevel@tonic-gate 			if (sscanf(optarg, "%d", &id) != 1)
640Sstevel@tonic-gate 				fatal("Invalid id %s\n", optarg);
650Sstevel@tonic-gate 			break;
660Sstevel@tonic-gate 		case 'e':
670Sstevel@tonic-gate 			execfile = optarg;
680Sstevel@tonic-gate 		}
690Sstevel@tonic-gate 	}
700Sstevel@tonic-gate 
71*10585SDhanaraj.M@Sun.COM 	if (getzoneid() != GLOBAL_ZONEID) {
72*10585SDhanaraj.M@Sun.COM 		fatal("modunload can only be run from the global zone\n");
73*10585SDhanaraj.M@Sun.COM 	}
74*10585SDhanaraj.M@Sun.COM 
750Sstevel@tonic-gate 	if (execfile) {
760Sstevel@tonic-gate 		child = fork();
770Sstevel@tonic-gate 		if (child == -1)
780Sstevel@tonic-gate 			error("can't fork %s", execfile);
790Sstevel@tonic-gate 		else if (child == 0)
800Sstevel@tonic-gate 			exec_userfile(execfile, id, envp);
810Sstevel@tonic-gate 		else {
820Sstevel@tonic-gate 			(void) wait(&status);
830Sstevel@tonic-gate 			if (status != 0) {
840Sstevel@tonic-gate 				(void) printf("%s returned error %d.\n",
850Sstevel@tonic-gate 				    execfile, status);
860Sstevel@tonic-gate 				(void) exit(status >> 8);
870Sstevel@tonic-gate 			}
880Sstevel@tonic-gate 		}
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	/*
920Sstevel@tonic-gate 	 * Unload the module.
930Sstevel@tonic-gate 	 */
940Sstevel@tonic-gate 	if (modctl(MODUNLOAD, id) < 0) {
95*10585SDhanaraj.M@Sun.COM 		if (errno == EPERM)
96*10585SDhanaraj.M@Sun.COM 			fatal("Insufficient privileges to unload a module\n");
97*10585SDhanaraj.M@Sun.COM 		else if (id != 0)
98*10585SDhanaraj.M@Sun.COM 			error("can't unload the module");
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	return (0);			/* success */
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * exec the user file.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate void
exec_userfile(char * execfile,int id,char ** envp)1080Sstevel@tonic-gate exec_userfile(char *execfile, int id, char **envp)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	struct modinfo modinfo;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	char modid[8];
1130Sstevel@tonic-gate 	char mod0[8];
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	modinfo.mi_id = modinfo.mi_nextid = id;
1160Sstevel@tonic-gate 	modinfo.mi_info = MI_INFO_ONE;
1170Sstevel@tonic-gate 	if (modctl(MODINFO, id, &modinfo) < 0)
1180Sstevel@tonic-gate 		error("can't get module information");
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	(void) sprintf(modid, "%d", id);
1210Sstevel@tonic-gate 	(void) sprintf(mod0, "%d", modinfo.mi_msinfo[0].msi_p0);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	(void) execle(execfile, execfile, modid, mod0, NULL, envp);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	error("couldn't exec %s\n", execfile);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate void
usage()1300Sstevel@tonic-gate usage()
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	fatal("usage:  modunload -i <module_id> [-e <exec_file>]\n");
1330Sstevel@tonic-gate }
134