1 /* $NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1995 5 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the FreeBSD project 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $"); 39 #endif 40 41 /* 42 * main() function for NFS lock daemon. Most of the code in this 43 * file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x. 44 * 45 * The actual program logic is in the file lock_proc.c 46 */ 47 48 #include <sys/types.h> 49 #include <sys/socket.h> 50 51 #include <err.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <errno.h> 55 #include <syslog.h> 56 #include <signal.h> 57 #include <string.h> 58 #include <unistd.h> 59 #include <util.h> 60 #include <netconfig.h> 61 62 #include <rpc/rpc.h> 63 #include <rpcsvc/sm_inter.h> 64 65 #include "lockd.h" 66 #include <rpcsvc/nlm_prot.h> 67 68 int debug_level = 0; /* 0 = no debugging syslog() calls */ 69 int _rpcsvcdirty = 0; 70 71 int grace_expired; 72 73 int main __P((int, char **)); 74 void nlm_prog_0 __P((struct svc_req *, SVCXPRT *)); 75 void nlm_prog_1 __P((struct svc_req *, SVCXPRT *)); 76 void nlm_prog_3 __P((struct svc_req *, SVCXPRT *)); 77 void nlm_prog_4 __P((struct svc_req *, SVCXPRT *)); 78 void usage __P((void)); 79 80 void sigalarm_handler __P((int)); 81 82 char *transports[] = { "udp", "tcp", "udp6", "tcp6" }; 83 84 int 85 main(argc, argv) 86 int argc; 87 char **argv; 88 { 89 SVCXPRT *transp; 90 int ch, i, maxindex, s; 91 struct sigaction sigchild, sigalarm; 92 int grace_period = 30; 93 struct netconfig *nconf; 94 95 while ((ch = getopt(argc, argv, "d:g:")) != (-1)) { 96 switch (ch) { 97 case 'd': 98 debug_level = atoi(optarg); 99 if (!debug_level) { 100 usage(); 101 /* NOTREACHED */ 102 } 103 break; 104 case 'g': 105 grace_period = atoi(optarg); 106 if (!grace_period) { 107 usage(); 108 /* NOTREACHED */ 109 } 110 break; 111 default: 112 case '?': 113 usage(); 114 /* NOTREACHED */ 115 } 116 } 117 118 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL); 119 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL); 120 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL); 121 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL); 122 123 /* 124 * Check if IPv6 support is present. 125 */ 126 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 127 if (s < 0) 128 maxindex = 2; 129 else { 130 close(s); 131 maxindex = 4; 132 } 133 134 for (i = 0; i < maxindex; i++) { 135 nconf = getnetconfigent(transports[i]); 136 if (nconf == NULL) 137 errx(1, "cannot get udp netconf."); 138 139 transp = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); 140 if (transp == NULL) { 141 errx(1, "cannot create %s service.", transports[i]); 142 /* NOTREACHED */ 143 } 144 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0, nconf)) { 145 errx(1, "unable to register (NLM_PROG, NLM_SM, %s)", 146 transports[i]); 147 /* NOTREACHED */ 148 } 149 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1, nconf)) { 150 errx(1, "unable to register (NLM_PROG, NLM_VERS, %s)", 151 transports[i]); 152 /* NOTREACHED */ 153 } 154 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, nconf)) { 155 errx(1, "unable to register (NLM_PROG, NLM_VERSX, %s)", 156 transports[i]); 157 /* NOTREACHED */ 158 } 159 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, nconf)) { 160 errx(1, "unable to register (NLM_PROG, NLM_VERS4, %s)", 161 transports[i]); 162 /* NOTREACHED */ 163 } 164 freenetconfigent(nconf); 165 } 166 167 /* 168 * Note that it is NOT sensible to run this program from inetd - the 169 * protocol assumes that it will run immediately at boot time. 170 */ 171 if (daemon(0, 0)) { 172 err(1, "cannot fork"); 173 /* NOTREACHED */ 174 } 175 pidfile(NULL); 176 177 openlog("rpc.lockd", 0, LOG_DAEMON); 178 if (debug_level) 179 syslog(LOG_INFO, "Starting, debug level %d", debug_level); 180 else 181 syslog(LOG_INFO, "Starting"); 182 183 sigchild.sa_handler = sigchild_handler; 184 sigemptyset(&sigchild.sa_mask); 185 sigchild.sa_flags = SA_RESTART; 186 if (sigaction(SIGCHLD, &sigchild, NULL) != 0) { 187 syslog(LOG_WARNING, "sigaction(SIGCHLD) failed: %s", 188 strerror(errno)); 189 exit(1); 190 } 191 sigalarm.sa_handler = sigalarm_handler; 192 sigemptyset(&sigalarm.sa_mask); 193 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */ 194 sigalarm.sa_flags |= SA_RESTART; 195 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) { 196 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s", 197 strerror(errno)); 198 exit(1); 199 } 200 grace_expired = 0; 201 if (alarm(10) < 0) { 202 syslog(LOG_WARNING, "alarm failed: %s", 203 strerror(errno)); 204 exit(1); 205 } 206 207 svc_run(); /* Should never return */ 208 exit(1); 209 } 210 211 void 212 sigalarm_handler(s) 213 int s; 214 { 215 grace_expired = 1; 216 } 217 218 void 219 usage() 220 { 221 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>]"); 222 } 223