xref: /freebsd/usr.sbin/pnfsdskill/pnfsdskill.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2017 Rick Macklem
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 #include <err.h>
31 #include <getopt.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <netdb.h>
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
38 #include <nfs/nfssvc.h>
39 
40 #include <fs/nfs/nfsproto.h>
41 #include <fs/nfs/nfskpiport.h>
42 #include <fs/nfs/nfs.h>
43 
44 static void usage(void) __dead2;
45 
46 static struct option longopts[] = {
47 	{ "force",	no_argument,	NULL,	'f'	},
48 	{ NULL,		0,		NULL,	0	}
49 };
50 
51 /*
52  * This program disables use of a DS mirror.  The "dspath" command line
53  * argument must be an exact match for the mounted-on path of the DS.
54  * It should be done before any forced dismount is performed on the path
55  * and should work even when the mount point is hung.
56  */
57 int
58 main(int argc, char *argv[])
59 {
60 	struct nfsd_pnfsd_args pnfsdarg;
61 	int ch, force;
62 
63 	if (geteuid() != 0)
64 		errx(1, "Must be run as root/su");
65 	force = 0;
66 	while ((ch = getopt_long(argc, argv, "f", longopts, NULL)) != -1) {
67 		switch (ch) {
68 		case 'f':
69 			force = 1;
70 			break;
71 		default:
72 			usage();
73 		}
74 	}
75 	argc -= optind;
76 	argv += optind;
77 	if (argc != 1)
78 		usage();
79 
80 	if (force != 0)
81 		pnfsdarg.op = PNFSDOP_FORCEDELDS;
82 	else
83 		pnfsdarg.op = PNFSDOP_DELDSSERVER;
84 	pnfsdarg.dspath = *argv;
85 	if (nfssvc(NFSSVC_PNFSDS, &pnfsdarg) < 0)
86 		err(1, "Can't kill %s", *argv);
87 }
88 
89 static void
90 usage(void)
91 {
92 
93 	fprintf(stderr, "pnfsdsfile [-f] mounted-on-DS-dir\n");
94 	exit(1);
95 }
96 
97