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*f4da9be0Scth * Common Development and Distribution License (the "License"). 6*f4da9be0Scth * 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*f4da9be0Scth * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <stdio.h> 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <unistd.h> 317c478bd9Sstevel@tonic-gate #include <errno.h> 327c478bd9Sstevel@tonic-gate #include <libintl.h> 337c478bd9Sstevel@tonic-gate #include <string.h> 347c478bd9Sstevel@tonic-gate #include <fcntl.h> 357c478bd9Sstevel@tonic-gate #include <sys/buf.h> 367c478bd9Sstevel@tonic-gate #include <sys/stat.h> 377c478bd9Sstevel@tonic-gate #include <sys/wait.h> 387c478bd9Sstevel@tonic-gate #include <limits.h> 397c478bd9Sstevel@tonic-gate #include <malloc.h> 407c478bd9Sstevel@tonic-gate #include <locale.h> 417c478bd9Sstevel@tonic-gate #include <ftw.h> 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 447c478bd9Sstevel@tonic-gate #include "addrem.h" 457c478bd9Sstevel@tonic-gate #include "errmsg.h" 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate #define FT_DEPTH 15 /* device tree depth for nftw() */ 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate static void usage(void); 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate int 527c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 537c478bd9Sstevel@tonic-gate { 547c478bd9Sstevel@tonic-gate int opt; 557c478bd9Sstevel@tonic-gate char *basedir = NULL, *driver_name = NULL; 567c478bd9Sstevel@tonic-gate int server = 0, mod_unloaded = 0; 577c478bd9Sstevel@tonic-gate int modid, found; 587c478bd9Sstevel@tonic-gate char maj_num[MAX_STR_MAJOR + 1]; 597c478bd9Sstevel@tonic-gate int err; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 627c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 637c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 647c478bd9Sstevel@tonic-gate #endif 657c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate /* must be run by root */ 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate if (getuid() != 0) { 707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NOT_ROOT)); 717c478bd9Sstevel@tonic-gate exit(1); 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "b:")) != -1) { 757c478bd9Sstevel@tonic-gate switch (opt) { 767c478bd9Sstevel@tonic-gate case 'b' : 777c478bd9Sstevel@tonic-gate server = 1; 787c478bd9Sstevel@tonic-gate basedir = calloc(strlen(optarg) + 1, 1); 797c478bd9Sstevel@tonic-gate if (basedir == NULL) { 807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 817c478bd9Sstevel@tonic-gate exit(1); 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate (void) strcat(basedir, optarg); 847c478bd9Sstevel@tonic-gate break; 857c478bd9Sstevel@tonic-gate case '?' : 867c478bd9Sstevel@tonic-gate usage(); 877c478bd9Sstevel@tonic-gate exit(1); 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate if (argv[optind] != NULL) { 927c478bd9Sstevel@tonic-gate driver_name = calloc(strlen(argv[optind]) + 1, 1); 937c478bd9Sstevel@tonic-gate if (driver_name == NULL) { 947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 957c478bd9Sstevel@tonic-gate exit(1); 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate (void) strcat(driver_name, argv[optind]); 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * check for extra args 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate if ((optind + 1) != argc) { 1037c478bd9Sstevel@tonic-gate usage(); 1047c478bd9Sstevel@tonic-gate exit(1); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate } else { 1087c478bd9Sstevel@tonic-gate usage(); 1097c478bd9Sstevel@tonic-gate exit(1); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* set up add_drv filenames */ 1137c478bd9Sstevel@tonic-gate if ((build_filenames(basedir)) == ERROR) { 1147c478bd9Sstevel@tonic-gate exit(1); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* must be only running version of add_drv/mod_drv/rem_drv */ 1187c478bd9Sstevel@tonic-gate enter_lock(); 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate if ((check_perms_aliases(1, 1)) == ERROR) 1217c478bd9Sstevel@tonic-gate err_exit(); 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate if ((check_name_to_major(R_OK | W_OK)) == ERROR) 1247c478bd9Sstevel@tonic-gate err_exit(); 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate /* look up the major number of the driver being removed. */ 1277c478bd9Sstevel@tonic-gate if ((found = get_major_no(driver_name, name_to_major)) == ERROR) { 1287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MAX_MAJOR), name_to_major); 1297c478bd9Sstevel@tonic-gate err_exit(); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate if (found == UNIQUE) { 1327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NOT_INSTALLED), 1337c478bd9Sstevel@tonic-gate driver_name); 1347c478bd9Sstevel@tonic-gate err_exit(); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate if (!server) { 1387c478bd9Sstevel@tonic-gate mod_unloaded = 1; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* get the module id for this driver */ 1417c478bd9Sstevel@tonic-gate get_modid(driver_name, &modid); 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate /* module is installed */ 1447c478bd9Sstevel@tonic-gate if (modid != -1) { 1457c478bd9Sstevel@tonic-gate if (modctl(MODUNLOAD, modid) < 0) { 1467c478bd9Sstevel@tonic-gate perror(NULL); 1477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MODUN), 1487c478bd9Sstevel@tonic-gate driver_name); 1497c478bd9Sstevel@tonic-gate mod_unloaded = 0; 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate /* unload driver.conf file */ 1537c478bd9Sstevel@tonic-gate if (modctl(MODUNLOADDRVCONF, (major_t)found) < 0) { 1547c478bd9Sstevel@tonic-gate perror(NULL); 1557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1567c478bd9Sstevel@tonic-gate gettext("cannot unload %s.conf\n"), driver_name); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate if (mod_unloaded && (modctl(MODREMMAJBIND, (major_t)found) < 0)) { 1617c478bd9Sstevel@tonic-gate perror(NULL); 1627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MODREMMAJ), found); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * add driver to rem_name_to_major; if this fails, don`t 1667c478bd9Sstevel@tonic-gate * delete from name_to_major 1677c478bd9Sstevel@tonic-gate */ 1687c478bd9Sstevel@tonic-gate (void) sprintf(maj_num, "%d", found); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate if (append_to_file(driver_name, maj_num, 171*f4da9be0Scth rem_name_to_major, ' ', " ", 0) == ERROR) { 1727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_UPDATE), 1737c478bd9Sstevel@tonic-gate rem_name_to_major); 1747c478bd9Sstevel@tonic-gate err_exit(); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate /* 1787c478bd9Sstevel@tonic-gate * If removing the driver from the running system, notify 1797c478bd9Sstevel@tonic-gate * kernel dynamically to remove minor perm entries. 1807c478bd9Sstevel@tonic-gate */ 1817c478bd9Sstevel@tonic-gate if (basedir == NULL || (strcmp(basedir, "/") == 0)) { 1827c478bd9Sstevel@tonic-gate err = devfs_rm_minor_perm(driver_name, log_minorperm_error); 1837c478bd9Sstevel@tonic-gate if (err != 0) { 1847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE_PERM), 1857c478bd9Sstevel@tonic-gate driver_name, err); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /* 1907c478bd9Sstevel@tonic-gate * delete references to driver in add_drv/rem_drv database 1917c478bd9Sstevel@tonic-gate */ 1927c478bd9Sstevel@tonic-gate remove_entry(CLEAN_ALL, driver_name); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate /* 1957c478bd9Sstevel@tonic-gate * Clean up any dangling devfs shadow nodes for this 1967c478bd9Sstevel@tonic-gate * driver so that, in the event the driver is re-added 1977c478bd9Sstevel@tonic-gate * to the system, newly created nodes won't incorrectly 1987c478bd9Sstevel@tonic-gate * pick up these stale shadow node permissions. 1997c478bd9Sstevel@tonic-gate */ 2007c478bd9Sstevel@tonic-gate if (basedir == NULL || (strcmp(basedir, "/") == 0)) { 2017c478bd9Sstevel@tonic-gate err = modctl(MODREMDRVCLEANUP, driver_name, 0, NULL); 2027c478bd9Sstevel@tonic-gate if (err != 0) { 2037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_REMDRV_CLEANUP), 2047c478bd9Sstevel@tonic-gate driver_name, err); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate exit_unlock(); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate return (NOERR); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate static void 2147c478bd9Sstevel@tonic-gate usage() 2157c478bd9Sstevel@tonic-gate { 2167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(REM_USAGE1)); 2177c478bd9Sstevel@tonic-gate } 218