/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 1997 Doug Rabson4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/linker.h>3031#include <err.h>32#include <stdio.h>33#include <stdlib.h>34#include <sysexits.h>35#include <unistd.h>3637static void38usage(void)39{40fprintf(stderr, "usage: kldunload [-fv] -i id ...\n");41fprintf(stderr, " kldunload [-fv] [-n] name ...\n");42exit(EX_USAGE);43}4445#define OPT_NULL 0x0046#define OPT_ID 0x0147#define OPT_VERBOSE 0x0248#define OPT_FORCE 0x044950int51main(int argc, char** argv)52{53struct kld_file_stat stat;54int c, fileid, force, opt;55char *filename;5657filename = NULL;58opt = OPT_NULL;5960while ((c = getopt(argc, argv, "finv")) != -1) {61switch (c) {62case 'f':63opt |= OPT_FORCE;64break;65case 'i':66opt |= OPT_ID;67break;68case 'n':69/*70* XXX: For backward compatibility. Currently does71* nothing72*/73break;74case 'v':75opt |= OPT_VERBOSE;76break;77default:78usage();79}80}8182argc -= optind;83argv += optind;8485if (argc == 0)86usage();8788while ((filename = *argv++) != NULL) {89if (opt & OPT_ID) {90fileid = atoi(filename);91if (fileid < 0)92errx(EXIT_FAILURE, "Invalid ID %s", optarg);93} else {94if ((fileid = kldfind(filename)) < 0)95errx(EXIT_FAILURE, "can't find file %s",96filename);97}98if (opt & OPT_VERBOSE) {99stat.version = sizeof(stat);100if (kldstat(fileid, &stat) < 0)101err(EXIT_FAILURE, "can't stat file");102(void) printf("Unloading %s, id=%d\n", stat.name,103fileid);104}105if (opt & OPT_FORCE)106force = LINKER_UNLOAD_FORCE;107else108force = LINKER_UNLOAD_NORMAL;109110if (kldunloadf(fileid, force) < 0)111err(EXIT_FAILURE, "can't unload file");112}113114return (EXIT_SUCCESS);115}116117118