1 /* $NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $ */
6 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the FreeBSD project
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $");
43 * main() function for NFS lock daemon. Most of the code in this
44 * file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x.
46 * The actual program logic is in the file lock_proc.c
49 #include <sys/types.h>
50 #include <sys/socket.h>
61 #include <netconfig.h>
64 #include <rpcsvc/sm_inter.h>
67 #include <rpcsvc/nlm_prot.h>
69 int debug_level = 0; /* 0 = no debugging syslog() calls */
78 int main __P((int, char **));
79 void nlm_prog_0 __P((struct svc_req *, SVCXPRT *));
80 void nlm_prog_1 __P((struct svc_req *, SVCXPRT *));
81 void nlm_prog_3 __P((struct svc_req *, SVCXPRT *));
82 void nlm_prog_4 __P((struct svc_req *, SVCXPRT *));
83 void usage __P((void));
85 void sigalarm_handler __P((int));
87 char *transports[] = { "udp", "tcp", "udp6", "tcp6" };
95 int ch, i, maxindex, s;
96 struct sigaction sigchild, sigalarm;
97 int grace_period = 30;
98 struct netconfig *nconf;
100 while ((ch = getopt(argc, argv, "d:g:")) != (-1)) {
103 debug_level = atoi(optarg);
110 grace_period = atoi(optarg);
122 if (geteuid()) { /* This command allowed only to root */
123 fprintf(stderr, "Sorry. You are not superuser\n");
127 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
128 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
129 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
130 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
133 * Check if IPv6 support is present.
135 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
143 for (i = 0; i < maxindex; i++) {
144 nconf = getnetconfigent(transports[i]);
146 errx(1, "cannot get udp netconf.");
148 transp = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
149 if (transp == NULL) {
150 errx(1, "cannot create %s service.", transports[i]);
153 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0, nconf)) {
154 errx(1, "unable to register (NLM_PROG, NLM_SM, %s)",
158 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1, nconf)) {
159 errx(1, "unable to register (NLM_PROG, NLM_VERS, %s)",
163 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, nconf)) {
164 errx(1, "unable to register (NLM_PROG, NLM_VERSX, %s)",
168 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, nconf)) {
169 errx(1, "unable to register (NLM_PROG, NLM_VERS4, %s)",
173 freenetconfigent(nconf);
177 * Note that it is NOT sensible to run this program from inetd - the
178 * protocol assumes that it will run immediately at boot time.
181 err(1, "cannot fork");
185 openlog("rpc.lockd", 0, LOG_DAEMON);
187 syslog(LOG_INFO, "Starting, debug level %d", debug_level);
189 syslog(LOG_INFO, "Starting");
191 sigchild.sa_handler = sigchild_handler;
192 sigemptyset(&sigchild.sa_mask);
193 sigchild.sa_flags = SA_RESTART;
194 if (sigaction(SIGCHLD, &sigchild, NULL) != 0) {
195 syslog(LOG_WARNING, "sigaction(SIGCHLD) failed: %s",
199 sigalarm.sa_handler = sigalarm_handler;
200 sigemptyset(&sigalarm.sa_mask);
201 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
202 sigalarm.sa_flags |= SA_RESTART;
203 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
204 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
210 syslog(LOG_WARNING, "alarm failed: %s",
217 client_pid = client_request();
219 svc_run(); /* Should never return */
233 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>]");
238 * Reset the NSM state-of-the-world and acquire its state.
249 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
250 * as I know. Leave it empty for now.
252 memset(&id, 0, sizeof(id));
253 id.my_name = "NFS NLM";
257 * The statd program must already be registered when lockd runs.
260 ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
261 xdr_my_id, &id, xdr_sm_stat, &stat);
262 if (ret == RPC_PROGUNAVAIL) {
263 warnx("%lu %s", SM_PROG, clnt_sperrno(ret));
271 errx(1, "%lu %s", SM_PROG, clnt_sperrno(ret));
274 nsm_state = stat.state;
276 /* setup constant data for SM_MON calls */
277 mon_host.mon_id.my_id.my_name = "localhost";
278 mon_host.mon_id.my_id.my_prog = NLM_PROG;
279 mon_host.mon_id.my_id.my_vers = NLM_SM;
280 mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY; /* bsdi addition */