Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / rpc.lockd / lockd.c
1 /*
2  * Copyright (c) 1995
3  *      A.R. Gordon (andrew.gordon@net-tel.co.uk).  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed for the FreeBSD project
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33
34 #ifndef lint
35 static const char rcsid[] =
36   "$FreeBSD: src/usr.sbin/rpc.lockd/lockd.c,v 1.5.2.1 2001/08/01 06:39:36 alfred Exp $";
37 #endif /* not lint */
38
39 /* main() function for NFS lock daemon.  Most of the code in this       */
40 /* file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x  */
41 /* The actual program logic is in the file procs.c                      */
42
43 #include <err.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <rpc/rpc.h>
47 #include <rpc/pmap_clnt.h>
48 #include "lockd.h"
49
50 void nlm_prog_1 __P((struct svc_req *, SVCXPRT *));
51 void nlm_prog_3 __P((struct svc_req *, SVCXPRT *));
52 void nlm_prog_4 __P((struct svc_req *, SVCXPRT *));
53 static void usage __P((void));
54
55 int debug_level = 0;    /* Zero means no debugging syslog() calls       */
56
57 int
58 main(int argc, char **argv)
59 {
60   SVCXPRT *transp;
61
62   if (argc > 1)
63   {
64     if (strncmp(argv[1], "-d", 2))
65       usage();
66     if (argc > 2) debug_level = atoi(argv[2]);
67     else debug_level = atoi(argv[1] + 2);
68     /* Ensure at least some debug if -d with no specified level         */
69     if (!debug_level) debug_level = 1;
70   }
71
72   (void)pmap_unset(NLM_PROG, NLM_VERS);
73   (void)pmap_unset(NLM_PROG, NLM_VERSX);
74   (void)pmap_unset(NLM_PROG, NLM4_VERS);
75
76   transp = svcudp_create(RPC_ANYSOCK);
77   if (transp == NULL)
78     errx(1, "cannot create udp service");
79   if (!svc_register(transp, NLM_PROG, NLM_VERS, nlm_prog_1, IPPROTO_UDP))
80     errx(1, "unable to register (NLM_PROG, NLM_VERS, udp)");
81   if (!svc_register(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, IPPROTO_UDP))
82     errx(1, "unable to register (NLM_PROG, NLM_VERSX, udp)");
83   if (!svc_register(transp, NLM_PROG, NLM4_VERS, nlm_prog_4, IPPROTO_UDP))
84     errx(1, "unable to register (NLM_PROG, NLM4_VERS, udp)");
85
86   transp = svctcp_create(RPC_ANYSOCK, 0, 0);
87   if (transp == NULL)
88     errx(1, "cannot create tcp service");
89   if (!svc_register(transp, NLM_PROG, NLM_VERS, nlm_prog_1, IPPROTO_TCP))
90     errx(1, "unable to register (NLM_PROG, NLM_VERS, tcp)");
91   if (!svc_register(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, IPPROTO_TCP))
92     errx(1, "unable to register (NLM_PROG, NLM_VERSX, tcp)");
93   if (!svc_register(transp, NLM_PROG, NLM4_VERS, nlm_prog_4, IPPROTO_TCP))
94     errx(1, "unable to register (NLM_PROG, NLM4_VERS, tcp)");
95
96   /* Note that it is NOT sensible to run this program from inetd - the  */
97   /* protocol assumes that it will run immediately at boot time.        */
98   if (daemon(0,0))
99     err(1, "fork");
100   openlog("rpc.lockd", 0, LOG_DAEMON);
101   if (debug_level) syslog(LOG_INFO, "Starting, debug level %d", debug_level);
102   else syslog(LOG_INFO, "Starting");
103
104   svc_run();    /* Should never return                                  */
105   exit(1);
106 }
107
108 static void
109 usage()
110 {
111       fprintf(stderr, "usage: rpc.lockd [-d [debuglevel]]\n");
112       exit(1);
113 }