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/wait.h> 287c478bd9Sstevel@tonic-gate #include <sys/param.h> 297c478bd9Sstevel@tonic-gate #include <sys/stat.h> 307c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 317c478bd9Sstevel@tonic-gate #include <stdio.h> 327c478bd9Sstevel@tonic-gate #include <stdlib.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <string.h> 357c478bd9Sstevel@tonic-gate #include <fcntl.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 37*17e9b2b7SDhanaraj M #include <zone.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate void l_exec_userfile(char *execfile, int id, char **envp); 407c478bd9Sstevel@tonic-gate void l_usage(); 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate extern void fatal(char *fmt, ...); 437c478bd9Sstevel@tonic-gate extern void error(char *fmt, ...); 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate /* 467c478bd9Sstevel@tonic-gate * Load a module. 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate int 497c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[]) 507c478bd9Sstevel@tonic-gate { 517c478bd9Sstevel@tonic-gate char *execfile = NULL; /* name of file to exec after loading */ 527c478bd9Sstevel@tonic-gate char *modpath = NULL; 537c478bd9Sstevel@tonic-gate int id; 547c478bd9Sstevel@tonic-gate extern int optind; 557c478bd9Sstevel@tonic-gate extern char *optarg; 567c478bd9Sstevel@tonic-gate int opt; 577c478bd9Sstevel@tonic-gate int use_path = 0; 587c478bd9Sstevel@tonic-gate char path[1024]; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate if (argc < 2 || argc > 5) { 617c478bd9Sstevel@tonic-gate l_usage(); 627c478bd9Sstevel@tonic-gate } 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "e:p")) != -1) { 657c478bd9Sstevel@tonic-gate switch (opt) { 667c478bd9Sstevel@tonic-gate case 'e': 677c478bd9Sstevel@tonic-gate execfile = optarg; 687c478bd9Sstevel@tonic-gate break; 697c478bd9Sstevel@tonic-gate case 'p': 707c478bd9Sstevel@tonic-gate use_path++; 717c478bd9Sstevel@tonic-gate break; 727c478bd9Sstevel@tonic-gate case '?': 737c478bd9Sstevel@tonic-gate l_usage(); 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate } 76*17e9b2b7SDhanaraj M 77*17e9b2b7SDhanaraj M if (getzoneid() != GLOBAL_ZONEID) { 78*17e9b2b7SDhanaraj M fatal("modload can only be run from the global zone\n"); 79*17e9b2b7SDhanaraj M } 80*17e9b2b7SDhanaraj M 817c478bd9Sstevel@tonic-gate modpath = argv[optind]; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate if (modpath == NULL) { 847c478bd9Sstevel@tonic-gate (void) printf("modpath is null\n"); 857c478bd9Sstevel@tonic-gate l_usage(); 867c478bd9Sstevel@tonic-gate } 877c478bd9Sstevel@tonic-gate if (!use_path && modpath[0] != '/') { 887c478bd9Sstevel@tonic-gate if (getcwd(path, 1023 - strlen(modpath)) == NULL) 897c478bd9Sstevel@tonic-gate fatal("Can't get current directory\n"); 907c478bd9Sstevel@tonic-gate (void) strcat(path, "/"); 917c478bd9Sstevel@tonic-gate (void) strcat(path, modpath); 927c478bd9Sstevel@tonic-gate } else 937c478bd9Sstevel@tonic-gate (void) strcpy(path, modpath); 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * Load the module. 977c478bd9Sstevel@tonic-gate */ 987c478bd9Sstevel@tonic-gate if (modctl(MODLOAD, use_path, path, &id) != 0) { 997c478bd9Sstevel@tonic-gate if (errno == EPERM) 1007c478bd9Sstevel@tonic-gate fatal("Insufficient privileges to load a module\n"); 1017c478bd9Sstevel@tonic-gate else 1027c478bd9Sstevel@tonic-gate error("can't load module"); 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate /* 1067c478bd9Sstevel@tonic-gate * Exec the user's file (if any) 1077c478bd9Sstevel@tonic-gate */ 1087c478bd9Sstevel@tonic-gate if (execfile) 1097c478bd9Sstevel@tonic-gate l_exec_userfile(execfile, id, envp); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate return (0); /* success */ 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate /* 1157c478bd9Sstevel@tonic-gate * Exec the user's file 1167c478bd9Sstevel@tonic-gate */ 1177c478bd9Sstevel@tonic-gate void 1187c478bd9Sstevel@tonic-gate l_exec_userfile(char *execfile, int id, char **envp) 1197c478bd9Sstevel@tonic-gate { 1207c478bd9Sstevel@tonic-gate struct modinfo modinfo; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate int child; 1237c478bd9Sstevel@tonic-gate int status; 1247c478bd9Sstevel@tonic-gate int waitret; 1257c478bd9Sstevel@tonic-gate char module_id[8]; 1267c478bd9Sstevel@tonic-gate char mod0[8]; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate if ((child = fork()) == -1) 1297c478bd9Sstevel@tonic-gate error("can't fork %s", execfile); 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate /* 1327c478bd9Sstevel@tonic-gate * exec the user program. 1337c478bd9Sstevel@tonic-gate */ 1347c478bd9Sstevel@tonic-gate if (child == 0) { 1357c478bd9Sstevel@tonic-gate modinfo.mi_id = id; 1367c478bd9Sstevel@tonic-gate modinfo.mi_nextid = id; 1377c478bd9Sstevel@tonic-gate modinfo.mi_info = MI_INFO_ONE; 1387c478bd9Sstevel@tonic-gate if (modctl(MODINFO, id, &modinfo) < 0) 1397c478bd9Sstevel@tonic-gate error("can't get module status"); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate (void) sprintf(module_id, "%d", modinfo.mi_id); 1427c478bd9Sstevel@tonic-gate (void) sprintf(mod0, "%d", modinfo.mi_msinfo[0].msi_p0); 1437c478bd9Sstevel@tonic-gate (void) execle(execfile, execfile, module_id, mod0, NULL, envp); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* Shouldn't get here if execle was successful */ 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate error("couldn't exec %s", execfile); 1487c478bd9Sstevel@tonic-gate } else { 1497c478bd9Sstevel@tonic-gate do { 1507c478bd9Sstevel@tonic-gate /* wait for exec'd program to finish */ 1517c478bd9Sstevel@tonic-gate waitret = wait(&status); 1527c478bd9Sstevel@tonic-gate } while ((waitret != child) && (waitret != -1)); 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate waitret = (waitret == -1) ? waitret : status; 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate if ((waitret & 0377) != 0) { 1577c478bd9Sstevel@tonic-gate /* exited because of a signal */ 1587c478bd9Sstevel@tonic-gate (void) printf("'%s' terminated because of signal %d", 1597c478bd9Sstevel@tonic-gate execfile, (waitret & 0177)); 1607c478bd9Sstevel@tonic-gate if (waitret & 0200) 1617c478bd9Sstevel@tonic-gate (void) printf(" and produced a core file\n"); 1627c478bd9Sstevel@tonic-gate (void) printf(".\n"); 1637c478bd9Sstevel@tonic-gate exit(waitret >> 8); 1647c478bd9Sstevel@tonic-gate } else { 1657c478bd9Sstevel@tonic-gate /* simple termination */ 1667c478bd9Sstevel@tonic-gate if (((waitret >> 8) & 0377) != 0) { 1677c478bd9Sstevel@tonic-gate (void) printf("'%s' returned error %d.\n", 1687c478bd9Sstevel@tonic-gate execfile, (waitret >> 8) & 0377); 1697c478bd9Sstevel@tonic-gate exit(waitret >> 8); 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate void 1767c478bd9Sstevel@tonic-gate l_usage() 1777c478bd9Sstevel@tonic-gate { 1787c478bd9Sstevel@tonic-gate fatal("usage: modload [-p] [-e <exec_file>] <filename>\n"); 1797c478bd9Sstevel@tonic-gate } 180