1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/mman.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/wait.h> 33*7c478bd9Sstevel@tonic-gate #include <strings.h> 34*7c478bd9Sstevel@tonic-gate #include <unistd.h> 35*7c478bd9Sstevel@tonic-gate #include <errno.h> 36*7c478bd9Sstevel@tonic-gate #include <stdio.h> 37*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate void usage(); 41*7c478bd9Sstevel@tonic-gate void exec_userfile(char *execfile, int id, char **envp); 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate extern void fatal(char *fmt, ...); 44*7c478bd9Sstevel@tonic-gate extern void error(char *fmt, ...); 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate /* 47*7c478bd9Sstevel@tonic-gate * Unload a loaded module. 48*7c478bd9Sstevel@tonic-gate */ 49*7c478bd9Sstevel@tonic-gate int 50*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[]) 51*7c478bd9Sstevel@tonic-gate { 52*7c478bd9Sstevel@tonic-gate int child; 53*7c478bd9Sstevel@tonic-gate int status; 54*7c478bd9Sstevel@tonic-gate int id; 55*7c478bd9Sstevel@tonic-gate char *execfile = NULL; 56*7c478bd9Sstevel@tonic-gate int opt; 57*7c478bd9Sstevel@tonic-gate extern char *optarg; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate if (argc < 3) 60*7c478bd9Sstevel@tonic-gate usage(); 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "i:e:")) != -1) { 63*7c478bd9Sstevel@tonic-gate switch (opt) { 64*7c478bd9Sstevel@tonic-gate case 'i': 65*7c478bd9Sstevel@tonic-gate if (sscanf(optarg, "%d", &id) != 1) 66*7c478bd9Sstevel@tonic-gate fatal("Invalid id %s\n", optarg); 67*7c478bd9Sstevel@tonic-gate break; 68*7c478bd9Sstevel@tonic-gate case 'e': 69*7c478bd9Sstevel@tonic-gate execfile = optarg; 70*7c478bd9Sstevel@tonic-gate } 71*7c478bd9Sstevel@tonic-gate } 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate if (execfile) { 74*7c478bd9Sstevel@tonic-gate child = fork(); 75*7c478bd9Sstevel@tonic-gate if (child == -1) 76*7c478bd9Sstevel@tonic-gate error("can't fork %s", execfile); 77*7c478bd9Sstevel@tonic-gate else if (child == 0) 78*7c478bd9Sstevel@tonic-gate exec_userfile(execfile, id, envp); 79*7c478bd9Sstevel@tonic-gate else { 80*7c478bd9Sstevel@tonic-gate (void) wait(&status); 81*7c478bd9Sstevel@tonic-gate if (status != 0) { 82*7c478bd9Sstevel@tonic-gate (void) printf("%s returned error %d.\n", 83*7c478bd9Sstevel@tonic-gate execfile, status); 84*7c478bd9Sstevel@tonic-gate (void) exit(status >> 8); 85*7c478bd9Sstevel@tonic-gate } 86*7c478bd9Sstevel@tonic-gate } 87*7c478bd9Sstevel@tonic-gate } 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate /* 90*7c478bd9Sstevel@tonic-gate * Unload the module. 91*7c478bd9Sstevel@tonic-gate */ 92*7c478bd9Sstevel@tonic-gate if (modctl(MODUNLOAD, id) < 0) { 93*7c478bd9Sstevel@tonic-gate if (errno == EPERM) 94*7c478bd9Sstevel@tonic-gate fatal("Insufficient privileges to unload a module\n"); 95*7c478bd9Sstevel@tonic-gate else if (id != 0) 96*7c478bd9Sstevel@tonic-gate error("can't unload the module"); 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate return (0); /* success */ 100*7c478bd9Sstevel@tonic-gate } 101*7c478bd9Sstevel@tonic-gate 102*7c478bd9Sstevel@tonic-gate /* 103*7c478bd9Sstevel@tonic-gate * exec the user file. 104*7c478bd9Sstevel@tonic-gate */ 105*7c478bd9Sstevel@tonic-gate void 106*7c478bd9Sstevel@tonic-gate exec_userfile(char *execfile, int id, char **envp) 107*7c478bd9Sstevel@tonic-gate { 108*7c478bd9Sstevel@tonic-gate struct modinfo modinfo; 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate char modid[8]; 111*7c478bd9Sstevel@tonic-gate char mod0[8]; 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate modinfo.mi_id = modinfo.mi_nextid = id; 114*7c478bd9Sstevel@tonic-gate modinfo.mi_info = MI_INFO_ONE; 115*7c478bd9Sstevel@tonic-gate if (modctl(MODINFO, id, &modinfo) < 0) 116*7c478bd9Sstevel@tonic-gate error("can't get module information"); 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate (void) sprintf(modid, "%d", id); 119*7c478bd9Sstevel@tonic-gate (void) sprintf(mod0, "%d", modinfo.mi_msinfo[0].msi_p0); 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate (void) execle(execfile, execfile, modid, mod0, NULL, envp); 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate error("couldn't exec %s\n", execfile); 124*7c478bd9Sstevel@tonic-gate } 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate void 128*7c478bd9Sstevel@tonic-gate usage() 129*7c478bd9Sstevel@tonic-gate { 130*7c478bd9Sstevel@tonic-gate fatal("usage: modunload -i <module_id> [-e <exec_file>]\n"); 131*7c478bd9Sstevel@tonic-gate } 132