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/wait.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/stat.h>
300Sstevel@tonic-gate #include <sys/modctl.h>
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <fcntl.h>
360Sstevel@tonic-gate #include <errno.h>
37*10585SDhanaraj.M@Sun.COM #include <zone.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate void l_exec_userfile(char *execfile, int id, char **envp);
400Sstevel@tonic-gate void l_usage();
410Sstevel@tonic-gate
420Sstevel@tonic-gate extern void fatal(char *fmt, ...);
430Sstevel@tonic-gate extern void error(char *fmt, ...);
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * Load a module.
470Sstevel@tonic-gate */
480Sstevel@tonic-gate int
main(int argc,char * argv[],char * envp[])490Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
500Sstevel@tonic-gate {
510Sstevel@tonic-gate char *execfile = NULL; /* name of file to exec after loading */
520Sstevel@tonic-gate char *modpath = NULL;
530Sstevel@tonic-gate int id;
540Sstevel@tonic-gate extern int optind;
550Sstevel@tonic-gate extern char *optarg;
560Sstevel@tonic-gate int opt;
570Sstevel@tonic-gate int use_path = 0;
580Sstevel@tonic-gate char path[1024];
590Sstevel@tonic-gate
600Sstevel@tonic-gate if (argc < 2 || argc > 5) {
610Sstevel@tonic-gate l_usage();
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate while ((opt = getopt(argc, argv, "e:p")) != -1) {
650Sstevel@tonic-gate switch (opt) {
660Sstevel@tonic-gate case 'e':
670Sstevel@tonic-gate execfile = optarg;
680Sstevel@tonic-gate break;
690Sstevel@tonic-gate case 'p':
700Sstevel@tonic-gate use_path++;
710Sstevel@tonic-gate break;
720Sstevel@tonic-gate case '?':
730Sstevel@tonic-gate l_usage();
740Sstevel@tonic-gate }
750Sstevel@tonic-gate }
76*10585SDhanaraj.M@Sun.COM
77*10585SDhanaraj.M@Sun.COM if (getzoneid() != GLOBAL_ZONEID) {
78*10585SDhanaraj.M@Sun.COM fatal("modload can only be run from the global zone\n");
79*10585SDhanaraj.M@Sun.COM }
80*10585SDhanaraj.M@Sun.COM
810Sstevel@tonic-gate modpath = argv[optind];
820Sstevel@tonic-gate
830Sstevel@tonic-gate if (modpath == NULL) {
840Sstevel@tonic-gate (void) printf("modpath is null\n");
850Sstevel@tonic-gate l_usage();
860Sstevel@tonic-gate }
870Sstevel@tonic-gate if (!use_path && modpath[0] != '/') {
880Sstevel@tonic-gate if (getcwd(path, 1023 - strlen(modpath)) == NULL)
890Sstevel@tonic-gate fatal("Can't get current directory\n");
900Sstevel@tonic-gate (void) strcat(path, "/");
910Sstevel@tonic-gate (void) strcat(path, modpath);
920Sstevel@tonic-gate } else
930Sstevel@tonic-gate (void) strcpy(path, modpath);
940Sstevel@tonic-gate
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * Load the module.
970Sstevel@tonic-gate */
980Sstevel@tonic-gate if (modctl(MODLOAD, use_path, path, &id) != 0) {
990Sstevel@tonic-gate if (errno == EPERM)
1000Sstevel@tonic-gate fatal("Insufficient privileges to load a module\n");
1010Sstevel@tonic-gate else
1020Sstevel@tonic-gate error("can't load module");
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate * Exec the user's file (if any)
1070Sstevel@tonic-gate */
1080Sstevel@tonic-gate if (execfile)
1090Sstevel@tonic-gate l_exec_userfile(execfile, id, envp);
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate return (0); /* success */
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate * Exec the user's file
1160Sstevel@tonic-gate */
1170Sstevel@tonic-gate void
l_exec_userfile(char * execfile,int id,char ** envp)1180Sstevel@tonic-gate l_exec_userfile(char *execfile, int id, char **envp)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate struct modinfo modinfo;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate int child;
1230Sstevel@tonic-gate int status;
1240Sstevel@tonic-gate int waitret;
1250Sstevel@tonic-gate char module_id[8];
1260Sstevel@tonic-gate char mod0[8];
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate if ((child = fork()) == -1)
1290Sstevel@tonic-gate error("can't fork %s", execfile);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate * exec the user program.
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate if (child == 0) {
1350Sstevel@tonic-gate modinfo.mi_id = id;
1360Sstevel@tonic-gate modinfo.mi_nextid = id;
1370Sstevel@tonic-gate modinfo.mi_info = MI_INFO_ONE;
1380Sstevel@tonic-gate if (modctl(MODINFO, id, &modinfo) < 0)
1390Sstevel@tonic-gate error("can't get module status");
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate (void) sprintf(module_id, "%d", modinfo.mi_id);
1420Sstevel@tonic-gate (void) sprintf(mod0, "%d", modinfo.mi_msinfo[0].msi_p0);
1430Sstevel@tonic-gate (void) execle(execfile, execfile, module_id, mod0, NULL, envp);
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /* Shouldn't get here if execle was successful */
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate error("couldn't exec %s", execfile);
1480Sstevel@tonic-gate } else {
1490Sstevel@tonic-gate do {
1500Sstevel@tonic-gate /* wait for exec'd program to finish */
1510Sstevel@tonic-gate waitret = wait(&status);
1520Sstevel@tonic-gate } while ((waitret != child) && (waitret != -1));
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate waitret = (waitret == -1) ? waitret : status;
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate if ((waitret & 0377) != 0) {
1570Sstevel@tonic-gate /* exited because of a signal */
1580Sstevel@tonic-gate (void) printf("'%s' terminated because of signal %d",
1590Sstevel@tonic-gate execfile, (waitret & 0177));
1600Sstevel@tonic-gate if (waitret & 0200)
1610Sstevel@tonic-gate (void) printf(" and produced a core file\n");
1620Sstevel@tonic-gate (void) printf(".\n");
1630Sstevel@tonic-gate exit(waitret >> 8);
1640Sstevel@tonic-gate } else {
1650Sstevel@tonic-gate /* simple termination */
1660Sstevel@tonic-gate if (((waitret >> 8) & 0377) != 0) {
1670Sstevel@tonic-gate (void) printf("'%s' returned error %d.\n",
1680Sstevel@tonic-gate execfile, (waitret >> 8) & 0377);
1690Sstevel@tonic-gate exit(waitret >> 8);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate void
l_usage()1760Sstevel@tonic-gate l_usage()
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate fatal("usage: modload [-p] [-e <exec_file>] <filename>\n");
1790Sstevel@tonic-gate }
180