kldunload.c revision 30573
1230592Sken/*-
2279253Sslm * Copyright (c) 1997 Doug Rabson
3279253Sslm * All rights reserved.
4230592Sken *
5230592Sken * Redistribution and use in source and binary forms, with or without
6230592Sken * modification, are permitted provided that the following conditions
7230592Sken * are met:
8230592Sken * 1. Redistributions of source code must retain the above copyright
9230592Sken *    notice, this list of conditions and the following disclaimer.
10230592Sken * 2. Redistributions in binary form must reproduce the above copyright
11230592Sken *    notice, this list of conditions and the following disclaimer in the
12230592Sken *    documentation and/or other materials provided with the distribution.
13230592Sken *
14230592Sken * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15230592Sken * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16230592Sken * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17230592Sken * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18230592Sken * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19230592Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20230592Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21230592Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22230592Sken * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23230592Sken * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24230592Sken * SUCH DAMAGE.
25230592Sken *
26230592Sken *	$Id: kldunload.c,v 1.1 1997/05/07 18:19:54 dfr Exp $
27279253Sslm */
28230592Sken
29230592Sken#include <err.h>
30230592Sken#include <stdio.h>
31230592Sken#include <stdlib.h>
32212420Sken#include <unistd.h>
33279253Sslm#include <sys/param.h>
34279253Sslm#include <sys/linker.h>
35212420Sken
36212420Skenextern char *optarg;
37212420Skenextern int optind;
38212420Sken
39212420Skenstatic char* progname;
40212420Sken
41230592Skenstatic void usage()
42212420Sken{
43212420Sken    fprintf(stderr, "usage: modunload [-i id] [-n filename]\n");
44212420Sken    exit(1);
45212420Sken}
46212420Sken
47212420Skenint main(int argc, char** argv)
48212420Sken{
49212420Sken    int c;
50212420Sken    int verbose = 0;
51212420Sken    int fileid = 0;
52212420Sken    char* filename = 0;
53212420Sken
54212420Sken    progname = argv[0];
55212420Sken    while ((c = getopt(argc, argv, "vi:n:")) != -1)
56212420Sken	switch (c) {
57212420Sken	case 'v':
58212420Sken	    verbose = 1;
59212420Sken	    break;
60212420Sken	case 'i':
61212420Sken	    fileid = atoi(optarg);
62212420Sken	    break;
63212420Sken	case 'n':
64212420Sken	    filename = optarg;
65212420Sken	    break;
66212420Sken	default:
67212420Sken	    usage();
68212420Sken	}
69212420Sken    argc -= optind;
70212420Sken    argv += optind;
71212420Sken
72212420Sken    if (argc != 0)
73212420Sken	usage();
74212420Sken
75212420Sken    if (fileid == 0 && filename == 0)
76212420Sken	usage();
77212420Sken
78212420Sken    if (filename) {
79212420Sken	fileid = kldfind(filename);
80212420Sken	if (fileid < 0)
81212420Sken	    err(1, "Can't find file %s", filename);
82212420Sken    }
83212420Sken
84212420Sken    if (verbose) {
85212420Sken	struct kld_file_stat stat;
86212420Sken	stat.version = sizeof stat;
87212420Sken	if (kldstat(fileid, &stat) < 0)
88212420Sken	    err(1, "Can't stat file");
89212420Sken	printf("Unloading %s, id=%d\n", stat.name, fileid);
90212420Sken    }
91212420Sken
92212420Sken    if (kldunload(fileid) < 0)
93212420Sken	err(1, "Can't unload file");
94212420Sken
95212420Sken    return 0;
96212420Sken}
97212420Sken
98212420Sken