xref: /titanic_52/usr/src/cmd/modload/modload.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2004 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/wait.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
36*7c478bd9Sstevel@tonic-gate #include <unistd.h>
37*7c478bd9Sstevel@tonic-gate #include <string.h>
38*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate void	l_exec_userfile(char *execfile, int id, char **envp);
42*7c478bd9Sstevel@tonic-gate void	l_usage();
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate extern void fatal(char *fmt, ...);
45*7c478bd9Sstevel@tonic-gate extern void error(char *fmt, ...);
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate /*
48*7c478bd9Sstevel@tonic-gate  * Load a module.
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate int
51*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	char *execfile = NULL;		/* name of file to exec after loading */
54*7c478bd9Sstevel@tonic-gate 	char *modpath = NULL;
55*7c478bd9Sstevel@tonic-gate 	int id;
56*7c478bd9Sstevel@tonic-gate 	extern int optind;
57*7c478bd9Sstevel@tonic-gate 	extern char *optarg;
58*7c478bd9Sstevel@tonic-gate 	int opt;
59*7c478bd9Sstevel@tonic-gate 	int use_path = 0;
60*7c478bd9Sstevel@tonic-gate 	char path[1024];
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	if (argc < 2 || argc > 5) {
63*7c478bd9Sstevel@tonic-gate 		l_usage();
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "e:p")) != -1) {
67*7c478bd9Sstevel@tonic-gate 		switch (opt) {
68*7c478bd9Sstevel@tonic-gate 		case 'e':
69*7c478bd9Sstevel@tonic-gate 			execfile = optarg;
70*7c478bd9Sstevel@tonic-gate 			break;
71*7c478bd9Sstevel@tonic-gate 		case 'p':
72*7c478bd9Sstevel@tonic-gate 			use_path++;
73*7c478bd9Sstevel@tonic-gate 			break;
74*7c478bd9Sstevel@tonic-gate 		case '?':
75*7c478bd9Sstevel@tonic-gate 			l_usage();
76*7c478bd9Sstevel@tonic-gate 		}
77*7c478bd9Sstevel@tonic-gate 	}
78*7c478bd9Sstevel@tonic-gate 	modpath = argv[optind];
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if (modpath == NULL) {
81*7c478bd9Sstevel@tonic-gate 		(void) printf("modpath is null\n");
82*7c478bd9Sstevel@tonic-gate 		l_usage();
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 	if (!use_path && modpath[0] != '/') {
85*7c478bd9Sstevel@tonic-gate 		if (getcwd(path, 1023 - strlen(modpath)) == NULL)
86*7c478bd9Sstevel@tonic-gate 			fatal("Can't get current directory\n");
87*7c478bd9Sstevel@tonic-gate 		(void) strcat(path, "/");
88*7c478bd9Sstevel@tonic-gate 		(void) strcat(path, modpath);
89*7c478bd9Sstevel@tonic-gate 	} else
90*7c478bd9Sstevel@tonic-gate 		(void) strcpy(path, modpath);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	/*
93*7c478bd9Sstevel@tonic-gate 	 * Load the module.
94*7c478bd9Sstevel@tonic-gate 	 */
95*7c478bd9Sstevel@tonic-gate 	if (modctl(MODLOAD, use_path, path, &id) != 0) {
96*7c478bd9Sstevel@tonic-gate 		if (errno == EPERM)
97*7c478bd9Sstevel@tonic-gate 			fatal("Insufficient privileges to load a module\n");
98*7c478bd9Sstevel@tonic-gate 		else
99*7c478bd9Sstevel@tonic-gate 			error("can't load module");
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	/*
103*7c478bd9Sstevel@tonic-gate 	 * Exec the user's file (if any)
104*7c478bd9Sstevel@tonic-gate 	 */
105*7c478bd9Sstevel@tonic-gate 	if (execfile)
106*7c478bd9Sstevel@tonic-gate 		l_exec_userfile(execfile, id, envp);
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	return (0);		/* success */
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate  * Exec the user's file
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate void
115*7c478bd9Sstevel@tonic-gate l_exec_userfile(char *execfile, int id, char **envp)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	struct modinfo modinfo;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	int child;
120*7c478bd9Sstevel@tonic-gate 	int status;
121*7c478bd9Sstevel@tonic-gate 	int waitret;
122*7c478bd9Sstevel@tonic-gate 	char module_id[8];
123*7c478bd9Sstevel@tonic-gate 	char mod0[8];
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	if ((child = fork()) == -1)
126*7c478bd9Sstevel@tonic-gate 		error("can't fork %s", execfile);
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	/*
129*7c478bd9Sstevel@tonic-gate 	 * exec the user program.
130*7c478bd9Sstevel@tonic-gate 	 */
131*7c478bd9Sstevel@tonic-gate 	if (child == 0) {
132*7c478bd9Sstevel@tonic-gate 		modinfo.mi_id = id;
133*7c478bd9Sstevel@tonic-gate 		modinfo.mi_nextid = id;
134*7c478bd9Sstevel@tonic-gate 		modinfo.mi_info = MI_INFO_ONE;
135*7c478bd9Sstevel@tonic-gate 		if (modctl(MODINFO, id, &modinfo) < 0)
136*7c478bd9Sstevel@tonic-gate 			error("can't get module status");
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 		(void) sprintf(module_id, "%d", modinfo.mi_id);
139*7c478bd9Sstevel@tonic-gate 		(void) sprintf(mod0, "%d", modinfo.mi_msinfo[0].msi_p0);
140*7c478bd9Sstevel@tonic-gate 		(void) execle(execfile, execfile, module_id, mod0, NULL, envp);
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 		/* Shouldn't get here if execle was successful */
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 		error("couldn't exec %s", execfile);
145*7c478bd9Sstevel@tonic-gate 	} else {
146*7c478bd9Sstevel@tonic-gate 		do {
147*7c478bd9Sstevel@tonic-gate 			/* wait for exec'd program to finish */
148*7c478bd9Sstevel@tonic-gate 			waitret = wait(&status);
149*7c478bd9Sstevel@tonic-gate 		} while ((waitret != child) && (waitret != -1));
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 		waitret = (waitret == -1) ? waitret : status;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 		if ((waitret & 0377) != 0) {
154*7c478bd9Sstevel@tonic-gate 			/* exited because of a signal */
155*7c478bd9Sstevel@tonic-gate 			(void) printf("'%s' terminated because of signal %d",
156*7c478bd9Sstevel@tonic-gate 			    execfile, (waitret & 0177));
157*7c478bd9Sstevel@tonic-gate 			if (waitret & 0200)
158*7c478bd9Sstevel@tonic-gate 				(void) printf(" and produced a core file\n");
159*7c478bd9Sstevel@tonic-gate 			(void) printf(".\n");
160*7c478bd9Sstevel@tonic-gate 			exit(waitret >> 8);
161*7c478bd9Sstevel@tonic-gate 		} else {
162*7c478bd9Sstevel@tonic-gate 			/* simple termination */
163*7c478bd9Sstevel@tonic-gate 			if (((waitret >> 8) & 0377) != 0) {
164*7c478bd9Sstevel@tonic-gate 				(void) printf("'%s' returned error %d.\n",
165*7c478bd9Sstevel@tonic-gate 				    execfile, (waitret >> 8) & 0377);
166*7c478bd9Sstevel@tonic-gate 				exit(waitret >> 8);
167*7c478bd9Sstevel@tonic-gate 			}
168*7c478bd9Sstevel@tonic-gate 		}
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate void
173*7c478bd9Sstevel@tonic-gate l_usage()
174*7c478bd9Sstevel@tonic-gate {
175*7c478bd9Sstevel@tonic-gate 	fatal("usage:  modload [-p] [-e <exec_file>] <filename>\n");
176*7c478bd9Sstevel@tonic-gate }
177