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 5f4da9be0Scth * Common Development and Distribution License (the "License"). 6f4da9be0Scth * 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 /* 22f4da9be0Scth * 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> 44*1aef0e11Sjg #include <sys/modctl.h> 45*1aef0e11Sjg #include <sys/instance.h> 46*1aef0e11Sjg #include <libdevinfo.h> 47*1aef0e11Sjg 487c478bd9Sstevel@tonic-gate #include "addrem.h" 497c478bd9Sstevel@tonic-gate #include "errmsg.h" 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #define FT_DEPTH 15 /* device tree depth for nftw() */ 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate static void usage(void); 54*1aef0e11Sjg static void cleanup_devfs_attributes(char *, char *); 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate int 577c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 587c478bd9Sstevel@tonic-gate { 597c478bd9Sstevel@tonic-gate int opt; 607c478bd9Sstevel@tonic-gate char *basedir = NULL, *driver_name = NULL; 617c478bd9Sstevel@tonic-gate int server = 0, mod_unloaded = 0; 627c478bd9Sstevel@tonic-gate int modid, found; 637c478bd9Sstevel@tonic-gate char maj_num[MAX_STR_MAJOR + 1]; 64*1aef0e11Sjg int cleanup = 0; 657c478bd9Sstevel@tonic-gate int err; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 687c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 697c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 707c478bd9Sstevel@tonic-gate #endif 717c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* must be run by root */ 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate if (getuid() != 0) { 767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NOT_ROOT)); 777c478bd9Sstevel@tonic-gate exit(1); 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate 80*1aef0e11Sjg while ((opt = getopt(argc, argv, "b:C")) != -1) { 817c478bd9Sstevel@tonic-gate switch (opt) { 827c478bd9Sstevel@tonic-gate case 'b' : 837c478bd9Sstevel@tonic-gate server = 1; 847c478bd9Sstevel@tonic-gate basedir = calloc(strlen(optarg) + 1, 1); 857c478bd9Sstevel@tonic-gate if (basedir == NULL) { 867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 877c478bd9Sstevel@tonic-gate exit(1); 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate (void) strcat(basedir, optarg); 907c478bd9Sstevel@tonic-gate break; 91*1aef0e11Sjg case 'C': 92*1aef0e11Sjg cleanup = 1; 93*1aef0e11Sjg break; 947c478bd9Sstevel@tonic-gate case '?' : 957c478bd9Sstevel@tonic-gate usage(); 967c478bd9Sstevel@tonic-gate exit(1); 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate if (argv[optind] != NULL) { 1017c478bd9Sstevel@tonic-gate driver_name = calloc(strlen(argv[optind]) + 1, 1); 1027c478bd9Sstevel@tonic-gate if (driver_name == NULL) { 1037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_MEM)); 1047c478bd9Sstevel@tonic-gate exit(1); 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate (void) strcat(driver_name, argv[optind]); 1087c478bd9Sstevel@tonic-gate /* 1097c478bd9Sstevel@tonic-gate * check for extra args 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate if ((optind + 1) != argc) { 1127c478bd9Sstevel@tonic-gate usage(); 1137c478bd9Sstevel@tonic-gate exit(1); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate } else { 1177c478bd9Sstevel@tonic-gate usage(); 1187c478bd9Sstevel@tonic-gate exit(1); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate /* set up add_drv filenames */ 1227c478bd9Sstevel@tonic-gate if ((build_filenames(basedir)) == ERROR) { 1237c478bd9Sstevel@tonic-gate exit(1); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate /* must be only running version of add_drv/mod_drv/rem_drv */ 1277c478bd9Sstevel@tonic-gate enter_lock(); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate if ((check_perms_aliases(1, 1)) == ERROR) 1307c478bd9Sstevel@tonic-gate err_exit(); 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate if ((check_name_to_major(R_OK | W_OK)) == ERROR) 1337c478bd9Sstevel@tonic-gate err_exit(); 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate /* look up the major number of the driver being removed. */ 1367c478bd9Sstevel@tonic-gate if ((found = get_major_no(driver_name, name_to_major)) == ERROR) { 1377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MAX_MAJOR), name_to_major); 1387c478bd9Sstevel@tonic-gate err_exit(); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate if (found == UNIQUE) { 1417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NOT_INSTALLED), 1427c478bd9Sstevel@tonic-gate driver_name); 1437c478bd9Sstevel@tonic-gate err_exit(); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate if (!server) { 1477c478bd9Sstevel@tonic-gate mod_unloaded = 1; 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate /* get the module id for this driver */ 1507c478bd9Sstevel@tonic-gate get_modid(driver_name, &modid); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /* module is installed */ 1537c478bd9Sstevel@tonic-gate if (modid != -1) { 1547c478bd9Sstevel@tonic-gate if (modctl(MODUNLOAD, modid) < 0) { 1557c478bd9Sstevel@tonic-gate perror(NULL); 1567c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MODUN), 1577c478bd9Sstevel@tonic-gate driver_name); 1587c478bd9Sstevel@tonic-gate mod_unloaded = 0; 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate /* unload driver.conf file */ 1627c478bd9Sstevel@tonic-gate if (modctl(MODUNLOADDRVCONF, (major_t)found) < 0) { 1637c478bd9Sstevel@tonic-gate perror(NULL); 1647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1657c478bd9Sstevel@tonic-gate gettext("cannot unload %s.conf\n"), driver_name); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate if (mod_unloaded && (modctl(MODREMMAJBIND, (major_t)found) < 0)) { 1707c478bd9Sstevel@tonic-gate perror(NULL); 1717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_MODREMMAJ), found); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate /* 1747c478bd9Sstevel@tonic-gate * add driver to rem_name_to_major; if this fails, don`t 1757c478bd9Sstevel@tonic-gate * delete from name_to_major 1767c478bd9Sstevel@tonic-gate */ 1777c478bd9Sstevel@tonic-gate (void) sprintf(maj_num, "%d", found); 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate if (append_to_file(driver_name, maj_num, 180f4da9be0Scth rem_name_to_major, ' ', " ", 0) == ERROR) { 1817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_NO_UPDATE), 1827c478bd9Sstevel@tonic-gate rem_name_to_major); 1837c478bd9Sstevel@tonic-gate err_exit(); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate /* 1877c478bd9Sstevel@tonic-gate * If removing the driver from the running system, notify 1887c478bd9Sstevel@tonic-gate * kernel dynamically to remove minor perm entries. 1897c478bd9Sstevel@tonic-gate */ 1907c478bd9Sstevel@tonic-gate if (basedir == NULL || (strcmp(basedir, "/") == 0)) { 1917c478bd9Sstevel@tonic-gate err = devfs_rm_minor_perm(driver_name, log_minorperm_error); 1927c478bd9Sstevel@tonic-gate if (err != 0) { 1937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_UPDATE_PERM), 1947c478bd9Sstevel@tonic-gate driver_name, err); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /* 1997c478bd9Sstevel@tonic-gate * delete references to driver in add_drv/rem_drv database 2007c478bd9Sstevel@tonic-gate */ 2017c478bd9Sstevel@tonic-gate remove_entry(CLEAN_ALL, driver_name); 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate /* 204*1aef0e11Sjg * Optionally clean up any dangling devfs shadow nodes for 205*1aef0e11Sjg * this driver so that, in the event the driver is re-added 2067c478bd9Sstevel@tonic-gate * to the system, newly created nodes won't incorrectly 2077c478bd9Sstevel@tonic-gate * pick up these stale shadow node permissions. 2087c478bd9Sstevel@tonic-gate */ 209*1aef0e11Sjg if (cleanup) { 210*1aef0e11Sjg if ((basedir == NULL || (strcmp(basedir, "/") == 0))) { 2117c478bd9Sstevel@tonic-gate err = modctl(MODREMDRVCLEANUP, driver_name, 0, NULL); 2127c478bd9Sstevel@tonic-gate if (err != 0) { 213*1aef0e11Sjg (void) fprintf(stderr, 214*1aef0e11Sjg gettext(ERR_REMDRV_CLEANUP), 2157c478bd9Sstevel@tonic-gate driver_name, err); 2167c478bd9Sstevel@tonic-gate } 217*1aef0e11Sjg } else if (strcmp(basedir, "/") != 0) { 218*1aef0e11Sjg cleanup_devfs_attributes(basedir, driver_name); 219*1aef0e11Sjg } 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate exit_unlock(); 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate return (NOERR); 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate 227*1aef0e11Sjg /* 228*1aef0e11Sjg * Optionally remove attribute nodes for a driver when 229*1aef0e11Sjg * removing drivers on a mounted root image. Useful 230*1aef0e11Sjg * when reprovisioning a machine to return to default 231*1aef0e11Sjg * permission/ownership settings if the driver is 232*1aef0e11Sjg * re-installed. 233*1aef0e11Sjg */ 234*1aef0e11Sjg typedef struct cleanup_arg { 235*1aef0e11Sjg char *ca_basedir; 236*1aef0e11Sjg char *ca_drvname; 237*1aef0e11Sjg } cleanup_arg_t; 238*1aef0e11Sjg 239*1aef0e11Sjg 240*1aef0e11Sjg /* 241*1aef0e11Sjg * Callback to remove a minor node for a device 242*1aef0e11Sjg */ 243*1aef0e11Sjg /*ARGSUSED*/ 244*1aef0e11Sjg static int 245*1aef0e11Sjg cleanup_minor_walker(void *cb_arg, const char *minor_path) 246*1aef0e11Sjg { 247*1aef0e11Sjg if (unlink(minor_path) == -1) { 248*1aef0e11Sjg (void) fprintf(stderr, "rem_drv: error removing %s\n", 249*1aef0e11Sjg minor_path, strerror(errno)); 250*1aef0e11Sjg } 251*1aef0e11Sjg return (DI_WALK_CONTINUE); 252*1aef0e11Sjg } 253*1aef0e11Sjg 254*1aef0e11Sjg /* 255*1aef0e11Sjg * Callback for each device registered in the binding file (path_to_inst) 256*1aef0e11Sjg */ 257*1aef0e11Sjg static int 258*1aef0e11Sjg cleanup_device_walker(void *cb_arg, const char *inst_path, 259*1aef0e11Sjg int inst_number, const char *inst_driver) 260*1aef0e11Sjg { 261*1aef0e11Sjg char path[MAXPATHLEN]; 262*1aef0e11Sjg cleanup_arg_t *arg = (cleanup_arg_t *)cb_arg; 263*1aef0e11Sjg int rv = DI_WALK_CONTINUE; 264*1aef0e11Sjg 265*1aef0e11Sjg if (strcmp(inst_driver, arg->ca_drvname) == 0) { 266*1aef0e11Sjg if (snprintf(path, MAXPATHLEN, "%s/devices%s", 267*1aef0e11Sjg arg->ca_basedir, inst_path) < MAXPATHLEN) { 268*1aef0e11Sjg rv = devfs_walk_minor_nodes(path, 269*1aef0e11Sjg cleanup_minor_walker, NULL); 270*1aef0e11Sjg } 271*1aef0e11Sjg } 272*1aef0e11Sjg return (rv); 273*1aef0e11Sjg } 274*1aef0e11Sjg 275*1aef0e11Sjg static void 276*1aef0e11Sjg cleanup_devfs_attributes(char *basedir, char *driver_name) 277*1aef0e11Sjg { 278*1aef0e11Sjg int rv; 279*1aef0e11Sjg cleanup_arg_t arg; 280*1aef0e11Sjg char binding_path[MAXPATHLEN+1]; 281*1aef0e11Sjg 282*1aef0e11Sjg (void) snprintf(binding_path, MAXPATHLEN, 283*1aef0e11Sjg "%s%s", basedir, INSTANCE_FILE); 284*1aef0e11Sjg 285*1aef0e11Sjg arg.ca_basedir = basedir; 286*1aef0e11Sjg arg.ca_drvname = driver_name; 287*1aef0e11Sjg (void) devfs_parse_binding_file(binding_path, 288*1aef0e11Sjg cleanup_device_walker, (void *)&arg); 289*1aef0e11Sjg } 290*1aef0e11Sjg 2917c478bd9Sstevel@tonic-gate static void 2927c478bd9Sstevel@tonic-gate usage() 2937c478bd9Sstevel@tonic-gate { 2947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(REM_USAGE1)); 2957c478bd9Sstevel@tonic-gate } 296