8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / fs.d / nfs / clear_locks / clear_locks.c
blobea3edd5a90ac9f2398fa4ab58b119078b763dc3a
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <sys/param.h>
34 #include <libintl.h>
35 #include <locale.h>
36 #include <rpc/rpc.h>
37 #include <rpcsvc/nlm_prot.h>
39 #include <sys/systeminfo.h>
40 #include <netdb.h>
41 #include <nss_dbdefs.h>
43 #include <nfs/nfs.h>
44 #include <nfs/export.h>
45 #include <nfs/nfssys.h>
47 extern char *optarg;
48 extern int optind;
49 extern int _nfssys(enum nfssys_op, void *);
51 static int share_zap(char *, char *);
54 * Clear locks and v4 related state held by
55 * 'client'.
57 static int
58 nfs4_clr_state(char *client)
60 int he_error;
61 char he_buf[NSS_BUFLEN_HOSTS];
62 struct hostent host_ent, *he;
63 char **ap;
64 struct nfs4clrst_args arg;
66 if ((he = gethostbyname_r(client, &host_ent, he_buf, sizeof (he_buf),
67 &he_error)) == NULL) {
68 (void) fprintf(stderr,
69 gettext("client name '%s' can not be resolved\n"),
70 client);
71 return (1);
74 if (he_error) {
75 perror("gethostbyname");
76 return (1);
80 * The NFS4 clear state interface is
81 * versioned in case we need to pass
82 * more information in the future.
84 arg.vers = NFS4_CLRST_VERSION;
85 arg.addr_type = he->h_addrtype;
88 * Iterate over IP Addresses clear
89 * state for each.
91 for (ap = he->h_addr_list; *ap; ap++) {
92 arg.ap = *ap;
93 _nfssys(NFS4_CLR_STATE, &arg);
95 return (0);
98 int
99 main(int argc, char *argv[])
101 int i, c, ret;
102 int sflag = 0;
103 int errflg = 0;
104 char myhostname[MAXHOSTNAMELEN];
106 if (geteuid() != (uid_t)0) {
107 (void) fprintf(stderr, gettext("clear_locks: must be root\n"));
108 exit(1);
111 (void) setlocale(LC_ALL, "");
113 #if !defined(TEXT_DOMAIN)
114 #define TEXT_DOMAIN "SYS_TEST"
115 #endif
116 (void) textdomain(TEXT_DOMAIN);
119 * Get the official hostname for this host
121 sysinfo(SI_HOSTNAME, myhostname, sizeof (myhostname));
123 while ((c = getopt(argc, argv, "s")) != EOF) {
124 switch (c) {
125 case 's':
126 sflag++;
127 break;
128 case '?':
129 errflg++;
133 i = argc - optind;
134 if (errflg || i != 1) {
135 (void) fprintf(stderr,
136 gettext("Usage: clear_locks [-s] hostname\n"));
137 exit(2);
140 if (sflag) {
141 (void) fprintf(stdout,
142 gettext("Clearing locks held for NFS client %s on server %s\n"),
143 myhostname, argv[optind]);
144 ret = share_zap(myhostname, argv[optind]);
145 } else {
146 (void) fprintf(stdout,
147 gettext("Clearing locks held for NFS client %s on server %s\n"),
148 argv[optind], myhostname);
149 ret = share_zap(argv[optind], myhostname);
150 ret += nfs4_clr_state(argv[optind]);
153 return (ret);
158 * Request that host 'server' free all locks held by
159 * host 'client'.
161 static int
162 share_zap(char *client, char *server)
164 struct nlm_notify notify;
165 enum clnt_stat rslt;
167 notify.state = 0;
168 notify.name = client;
169 rslt = rpc_call(server, NLM_PROG, NLM_VERSX, NLM_FREE_ALL,
170 xdr_nlm_notify, (char *)&notify, xdr_void, 0, NULL);
171 if (rslt != RPC_SUCCESS) {
172 clnt_perrno(rslt);
173 return (3);
175 (void) fprintf(stderr,
176 gettext("clear of locks held for %s on %s returned success\n"),
177 client, server);
178 return (0);