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