lockd.c revision 74462
1/* $NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $ */ 2/* $FreeBSD: head/usr.sbin/rpc.lockd/lockd.c 74462 2001-03-19 12:50:13Z alfred $ */ 3 4/* 5 * Copyright (c) 1995 6 * A.R. Gordon (andrew.gordon@net-tel.co.uk). All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 22 * 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 33 * SUCH DAMAGE. 34 * 35 */ 36 37#include <sys/cdefs.h> 38#ifndef lint 39__RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $"); 40#endif 41 42/* 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. 45 * 46 * The actual program logic is in the file lock_proc.c 47 */ 48 49#include <sys/types.h> 50#include <sys/socket.h> 51 52#include <err.h> 53#include <stdio.h> 54#include <stdlib.h> 55#include <errno.h> 56#include <syslog.h> 57#include <signal.h> 58#include <string.h> 59#include <unistd.h> 60#include <libutil.h> 61#include <netconfig.h> 62 63#include <rpc/rpc.h> 64#include <rpcsvc/sm_inter.h> 65 66#include "lockd.h" 67#include <rpcsvc/nlm_prot.h> 68 69int debug_level = 0; /* 0 = no debugging syslog() calls */ 70int _rpcsvcdirty = 0; 71 72int grace_expired; 73 74int main __P((int, char **)); 75void nlm_prog_0 __P((struct svc_req *, SVCXPRT *)); 76void nlm_prog_1 __P((struct svc_req *, SVCXPRT *)); 77void nlm_prog_3 __P((struct svc_req *, SVCXPRT *)); 78void nlm_prog_4 __P((struct svc_req *, SVCXPRT *)); 79void usage __P((void)); 80 81void sigalarm_handler __P((int)); 82 83char *transports[] = { "udp", "tcp", "udp6", "tcp6" }; 84 85int 86main(argc, argv) 87 int argc; 88 char **argv; 89{ 90 SVCXPRT *transp; 91 int ch, i, maxindex, s; 92 struct sigaction sigchild, sigalarm; 93 int grace_period = 30; 94 struct netconfig *nconf; 95 96 while ((ch = getopt(argc, argv, "d:g:")) != (-1)) { 97 switch (ch) { 98 case 'd': 99 debug_level = atoi(optarg); 100 if (!debug_level) { 101 usage(); 102 /* NOTREACHED */ 103 } 104 break; 105 case 'g': 106 grace_period = atoi(optarg); 107 if (!grace_period) { 108 usage(); 109 /* NOTREACHED */ 110 } 111 break; 112 default: 113 case '?': 114 usage(); 115 /* NOTREACHED */ 116 } 117 } 118 if (geteuid()) { /* This command allowed only to root */ 119 fprintf(stderr, "Sorry. You are not superuser\n"); 120 exit(1); 121 } 122 123 (void)rpcb_unset(NLM_PROG, NLM_SM, NULL); 124 (void)rpcb_unset(NLM_PROG, NLM_VERS, NULL); 125 (void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL); 126 (void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL); 127 128 /* 129 * Check if IPv6 support is present. 130 */ 131 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 132 if (s < 0) 133 maxindex = 2; 134 else { 135 close(s); 136 maxindex = 4; 137 } 138 139 for (i = 0; i < maxindex; i++) { 140 nconf = getnetconfigent(transports[i]); 141 if (nconf == NULL) 142 errx(1, "cannot get udp netconf."); 143 144 transp = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); 145 if (transp == NULL) { 146 errx(1, "cannot create %s service.", transports[i]); 147 /* NOTREACHED */ 148 } 149 if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0, nconf)) { 150 errx(1, "unable to register (NLM_PROG, NLM_SM, %s)", 151 transports[i]); 152 /* NOTREACHED */ 153 } 154 if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1, nconf)) { 155 errx(1, "unable to register (NLM_PROG, NLM_VERS, %s)", 156 transports[i]); 157 /* NOTREACHED */ 158 } 159 if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, nconf)) { 160 errx(1, "unable to register (NLM_PROG, NLM_VERSX, %s)", 161 transports[i]); 162 /* NOTREACHED */ 163 } 164 if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, nconf)) { 165 errx(1, "unable to register (NLM_PROG, NLM_VERS4, %s)", 166 transports[i]); 167 /* NOTREACHED */ 168 } 169 freenetconfigent(nconf); 170 } 171 172 /* 173 * Note that it is NOT sensible to run this program from inetd - the 174 * protocol assumes that it will run immediately at boot time. 175 */ 176 if (daemon(0, 0)) { 177 err(1, "cannot fork"); 178 /* NOTREACHED */ 179 } 180 181 openlog("rpc.lockd", 0, LOG_DAEMON); 182 if (debug_level) 183 syslog(LOG_INFO, "Starting, debug level %d", debug_level); 184 else 185 syslog(LOG_INFO, "Starting"); 186 187 sigchild.sa_handler = sigchild_handler; 188 sigemptyset(&sigchild.sa_mask); 189 sigchild.sa_flags = SA_RESTART; 190 if (sigaction(SIGCHLD, &sigchild, NULL) != 0) { 191 syslog(LOG_WARNING, "sigaction(SIGCHLD) failed: %s", 192 strerror(errno)); 193 exit(1); 194 } 195 sigalarm.sa_handler = sigalarm_handler; 196 sigemptyset(&sigalarm.sa_mask); 197 sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */ 198 sigalarm.sa_flags |= SA_RESTART; 199 if (sigaction(SIGALRM, &sigalarm, NULL) != 0) { 200 syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s", 201 strerror(errno)); 202 exit(1); 203 } 204 grace_expired = 0; 205 if (alarm(10) < 0) { 206 syslog(LOG_WARNING, "alarm failed: %s", 207 strerror(errno)); 208 exit(1); 209 } 210 211 svc_run(); /* Should never return */ 212 exit(1); 213} 214 215void 216sigalarm_handler(s) 217 int s; 218{ 219 grace_expired = 1; 220} 221 222void 223usage() 224{ 225 errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>]"); 226} 227