xref: /illumos-gate/usr/src/cmd/modload/modunload.c (revision 17e9b2b7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*17e9b2b7SDhanaraj M  * Common Development and Distribution License (the "License").
6*17e9b2b7SDhanaraj M  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*17e9b2b7SDhanaraj M  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/mman.h>
287c478bd9Sstevel@tonic-gate #include <sys/stat.h>
297c478bd9Sstevel@tonic-gate #include <sys/wait.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
36*17e9b2b7SDhanaraj M #include <zone.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate void	usage();
397c478bd9Sstevel@tonic-gate void	exec_userfile(char *execfile, int id, char **envp);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate extern void fatal(char *fmt, ...);
427c478bd9Sstevel@tonic-gate extern void error(char *fmt, ...);
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Unload a loaded module.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[],char * envp[])487c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	int child;
517c478bd9Sstevel@tonic-gate 	int status;
527c478bd9Sstevel@tonic-gate 	int id;
537c478bd9Sstevel@tonic-gate 	char *execfile = NULL;
547c478bd9Sstevel@tonic-gate 	int opt;
557c478bd9Sstevel@tonic-gate 	extern char *optarg;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (argc < 3)
587c478bd9Sstevel@tonic-gate 		usage();
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "i:e:")) != -1) {
617c478bd9Sstevel@tonic-gate 		switch (opt) {
627c478bd9Sstevel@tonic-gate 		case 'i':
637c478bd9Sstevel@tonic-gate 			if (sscanf(optarg, "%d", &id) != 1)
647c478bd9Sstevel@tonic-gate 				fatal("Invalid id %s\n", optarg);
657c478bd9Sstevel@tonic-gate 			break;
667c478bd9Sstevel@tonic-gate 		case 'e':
677c478bd9Sstevel@tonic-gate 			execfile = optarg;
687c478bd9Sstevel@tonic-gate 		}
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 
71*17e9b2b7SDhanaraj M 	if (getzoneid() != GLOBAL_ZONEID) {
72*17e9b2b7SDhanaraj M 		fatal("modunload can only be run from the global zone\n");
73*17e9b2b7SDhanaraj M 	}
74*17e9b2b7SDhanaraj M 
757c478bd9Sstevel@tonic-gate 	if (execfile) {
767c478bd9Sstevel@tonic-gate 		child = fork();
777c478bd9Sstevel@tonic-gate 		if (child == -1)
787c478bd9Sstevel@tonic-gate 			error("can't fork %s", execfile);
797c478bd9Sstevel@tonic-gate 		else if (child == 0)
807c478bd9Sstevel@tonic-gate 			exec_userfile(execfile, id, envp);
817c478bd9Sstevel@tonic-gate 		else {
827c478bd9Sstevel@tonic-gate 			(void) wait(&status);
837c478bd9Sstevel@tonic-gate 			if (status != 0) {
847c478bd9Sstevel@tonic-gate 				(void) printf("%s returned error %d.\n",
857c478bd9Sstevel@tonic-gate 				    execfile, status);
867c478bd9Sstevel@tonic-gate 				(void) exit(status >> 8);
877c478bd9Sstevel@tonic-gate 			}
887c478bd9Sstevel@tonic-gate 		}
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * Unload the module.
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 	if (modctl(MODUNLOAD, id) < 0) {
95*17e9b2b7SDhanaraj M 		if (errno == EPERM)
96*17e9b2b7SDhanaraj M 			fatal("Insufficient privileges to unload a module\n");
97*17e9b2b7SDhanaraj M 		else if (id != 0)
98*17e9b2b7SDhanaraj M 			error("can't unload the module");
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	return (0);			/* success */
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * exec the user file.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate void
exec_userfile(char * execfile,int id,char ** envp)1087c478bd9Sstevel@tonic-gate exec_userfile(char *execfile, int id, char **envp)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	struct modinfo modinfo;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	char modid[8];
1137c478bd9Sstevel@tonic-gate 	char mod0[8];
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	modinfo.mi_id = modinfo.mi_nextid = id;
1167c478bd9Sstevel@tonic-gate 	modinfo.mi_info = MI_INFO_ONE;
1177c478bd9Sstevel@tonic-gate 	if (modctl(MODINFO, id, &modinfo) < 0)
1187c478bd9Sstevel@tonic-gate 		error("can't get module information");
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	(void) sprintf(modid, "%d", id);
1217c478bd9Sstevel@tonic-gate 	(void) sprintf(mod0, "%d", modinfo.mi_msinfo[0].msi_p0);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	(void) execle(execfile, execfile, modid, mod0, NULL, envp);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	error("couldn't exec %s\n", execfile);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate void
usage()1307c478bd9Sstevel@tonic-gate usage()
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	fatal("usage:  modunload -i <module_id> [-e <exec_file>]\n");
1337c478bd9Sstevel@tonic-gate }
134