1/*	$NetBSD: dgram_server.c,v 1.3 2022/10/08 16:12:46 christos Exp $	*/
2
3/*++
4/* NAME
5/*	dgram_server 3
6/* SUMMARY
7/*	skeleton datagram server subsystem
8/* SYNOPSIS
9/*	#include <mail_server.h>
10/*
11/*	NORETURN dgram_server_main(argc, argv, service, key, value, ...)
12/*	int	argc;
13/*	char	**argv;
14/*	void	(*service)(char *buf, int len, char *service_name, char **argv);
15/*	int	key;
16/* DESCRIPTION
17/*	This module implements a skeleton for mail subsystem programs
18/*	that wake up on client request and perform some activity
19/*	without further client interaction.  This module supports
20/*	local IPC via a UNIX-domain datagram socket. The resulting
21/*	program expects to be run from the \fBmaster\fR process.
22/*
23/*	dgram_server_main() is the skeleton entry point. It should
24/*	be called from the application main program. The skeleton
25/*	does the generic command-line options processing, initialization
26/*	of configurable parameters, and receiving datagrams. The
27/*	skeleton never returns.
28/*
29/*	Arguments:
30/* .IP "void (*service)(char *buf, int len, char *service_name, char **argv)"
31/*	A pointer to a function that is called by the skeleton each
32/*	time a client sends a datagram to the program's service
33/*	port. The function is run after the program has irrevocably
34/*	dropped its privileges.  The buffer argument specifies the
35/*	data read from the datagram port; this data corresponds to
36/*	request.  The len argument specifies how much client data
37/*	is available.  The maximal size of the buffer is specified
38/*	via the DGRAM_BUF_SIZE manifest constant.  The service name
39/*	argument corresponds to the service name in the master.cf
40/*	file.  The argv argument specifies command-line arguments
41/*	left over after options processing.
42/* .PP
43/*	Optional arguments are specified as a null-terminated list
44/*	with macros that have zero or more arguments:
45/* .IP "CA_MAIL_SERVER_INT_TABLE(CONFIG_INT_TABLE *)"
46/*	A table with configurable parameters, to be loaded from the
47/*	global Postfix configuration file. Tables are loaded in the
48/*	order as specified, and multiple instances of the same type
49/*	are allowed.
50/* .IP "CA_MAIL_SERVER_LONG_TABLE(CONFIG_LONG_TABLE *)"
51/*	A table with configurable parameters, to be loaded from the
52/*	global Postfix configuration file. Tables are loaded in the
53/*	order as specified, and multiple instances of the same type
54/*	are allowed.
55/* .IP "CA_MAIL_SERVER_STR_TABLE(CONFIG_STR_TABLE *)"
56/*	A table with configurable parameters, to be loaded from the
57/*	global Postfix configuration file. Tables are loaded in the
58/*	order as specified, and multiple instances of the same type
59/*	are allowed.
60/* .IP "CA_MAIL_SERVER_BOOL_TABLE(CONFIG_BOOL_TABLE *)"
61/*	A table with configurable parameters, to be loaded from the
62/*	global Postfix configuration file. Tables are loaded in the
63/*	order as specified, and multiple instances of the same type
64/*	are allowed.
65/* .IP "CA_MAIL_SERVER_TIME_TABLE(CONFIG_TIME_TABLE *)"
66/*	A table with configurable parameters, to be loaded from the
67/*	global Postfix configuration file. Tables are loaded in the
68/*	order as specified, and multiple instances of the same type
69/*	are allowed.
70/* .IP "CA_MAIL_SERVER_RAW_TABLE(CONFIG_RAW_TABLE *)"
71/*	A table with configurable parameters, to be loaded from the
72/*	global Postfix configuration file. Tables are loaded in the
73/*	order as specified, and multiple instances of the same type
74/*	are allowed. Raw parameters are not subjected to $name
75/*	evaluation.
76/* .IP "CA_MAIL_SERVER_NINT_TABLE(CONFIG_NINT_TABLE *)"
77/*	A table with configurable parameters, to be loaded from the
78/*	global Postfix configuration file. Tables are loaded in the
79/*	order as specified, and multiple instances of the same type
80/*	are allowed.
81/* .IP "CA_MAIL_SERVER_NBOOL_TABLE(CONFIG_NBOOL_TABLE *)"
82/*	A table with configurable parameters, to be loaded from the
83/*	global Postfix configuration file. Tables are loaded in the
84/*	order as specified, and multiple instances of the same type
85/*	are allowed.
86/* .IP "CA_MAIL_SERVER_PRE_INIT(void *(char *service_name, char **argv))"
87/*	A pointer to a function that is called once
88/*	by the skeleton after it has read the global configuration file
89/*	and after it has processed command-line arguments, but before
90/*	the skeleton has optionally relinquished the process privileges.
91/* .sp
92/*	Only the last instance of this parameter type is remembered.
93/* .IP "CA_MAIL_SERVER_POST_INIT(void *(char *service_name, char **argv))"
94/*	A pointer to a function that is called once
95/*	by the skeleton after it has optionally relinquished the process
96/*	privileges, but before servicing client connection requests.
97/* .sp
98/*	Only the last instance of this parameter type is remembered.
99/* .IP "CA_MAIL_SERVER_LOOP(int *(char *service_name, char **argv))"
100/*	A pointer to function that is executed from
101/*	within the event loop, whenever an I/O or timer event has happened,
102/*	or whenever nothing has happened for a specified amount of time.
103/*	The result value of the function specifies how long to wait until
104/*	the next event. Specify -1 to wait for "as long as it takes".
105/* .sp
106/*	Only the last instance of this parameter type is remembered.
107/* .IP "CA_MAIL_SERVER_EXIT(void *(char *service_name, char **argv))"
108/*	A pointer to function that is executed immediately before normal
109/*	process termination.
110/* .sp
111/*	Only the last instance of this parameter type is remembered.
112/* .IP "CA_MAIL_SERVER_PRE_ACCEPT(void *(char *service_name, char **argv))"
113/*	Function to be executed prior to accepting a new request.
114/* .sp
115/*	Only the last instance of this parameter type is remembered.
116/* .IP "CA_MAIL_SERVER_IN_FLOW_DELAY(none)"
117/*	Pause $in_flow_delay seconds when no "mail flow control token"
118/*	is available. A token is consumed for each connection request.
119/* .IP CA_MAIL_SERVER_SOLITARY
120/*	This service must be configured with process limit of 1.
121/* .IP CA_MAIL_SERVER_UNLIMITED
122/*	This service must be configured with process limit of 0.
123/* .IP CA_MAIL_SERVER_PRIVILEGED
124/*	This service must be configured as privileged.
125/* .IP "CA_MAIL_SERVER_WATCHDOG(int *)"
126/*	Override the default 1000s watchdog timeout. The value is
127/*	used after command-line and main.cf file processing.
128/* .IP "CA_MAIL_SERVER_BOUNCE_INIT(const char *, const char **)"
129/*	Initialize the DSN filter for the bounce/defer service
130/*	clients with the specified map source and map names.
131/* .PP
132/*	The var_use_limit variable limits the number of clients that
133/*	a server can service before it commits suicide.
134/*	This value is taken from the global \fBmain.cf\fR configuration
135/*	file. Setting \fBvar_use_limit\fR to zero disables the client limit.
136/*
137/*	The var_idle_limit variable limits the time that a service
138/*	receives no client connection requests before it commits suicide.
139/*	This value is taken from the global \fBmain.cf\fR configuration
140/*	file. Setting \fBvar_use_limit\fR to zero disables the idle limit.
141/* DIAGNOSTICS
142/*	Problems and transactions are logged to \fBsyslogd\fR(8)
143/*	or \fBpostlogd\fR(8).
144/* SEE ALSO
145/*	master(8), master process
146/*	postlogd(8), Postfix logging
147/*	syslogd(8), system logging
148/* LICENSE
149/* .ad
150/* .fi
151/*	The Secure Mailer license must be distributed with this software.
152/* AUTHOR(S)
153/*	Wietse Venema
154/*	Google, Inc.
155/*	111 8th Avenue
156/*	New York, NY 10011, USA
157/*--*/
158
159/* System library. */
160
161#include <sys_defs.h>
162#include <sys/socket.h>
163#include <unistd.h>
164#include <signal.h>
165#include <stdlib.h>
166#include <limits.h>
167#include <string.h>
168#include <errno.h>
169#include <fcntl.h>
170#include <stdarg.h>
171#ifdef STRCASECMP_IN_STRINGS_H
172#include <strings.h>
173#endif
174#include <time.h>
175
176/* Utility library. */
177
178#include <msg.h>
179#include <msg_vstream.h>
180#include <chroot_uid.h>
181#include <vstring.h>
182#include <vstream.h>
183#include <msg_vstream.h>
184#include <mymalloc.h>
185#include <events.h>
186#include <iostuff.h>
187#include <stringops.h>
188#include <sane_accept.h>
189#include <myflock.h>
190#include <safe_open.h>
191#include <listen.h>
192#include <watchdog.h>
193#include <split_at.h>
194
195/* Global library. */
196
197#include <mail_params.h>
198#include <mail_task.h>
199#include <debug_process.h>
200#include <mail_conf.h>
201#include <mail_dict.h>
202#include <resolve_local.h>
203#include <mail_flow.h>
204#include <mail_version.h>
205#include <bounce.h>
206#include <maillog_client.h>
207
208/* Process manager. */
209
210#include "master_proto.h"
211
212/* Application-specific */
213
214#include "mail_server.h"
215
216 /*
217  * Global state.
218  */
219static int use_count;
220
221static DGRAM_SERVER_FN dgram_server_service;
222static char *dgram_server_name;
223static char **dgram_server_argv;
224static void (*dgram_server_accept) (int, void *);
225static void (*dgram_server_onexit) (char *, char **);
226static void (*dgram_server_pre_accept) (char *, char **);
227static VSTREAM *dgram_server_lock;
228static int dgram_server_in_flow_delay;
229static unsigned dgram_server_generation;
230static int dgram_server_watchdog = 1000;
231
232/* dgram_server_exit - normal termination */
233
234static NORETURN dgram_server_exit(void)
235{
236    if (dgram_server_onexit)
237	dgram_server_onexit(dgram_server_name, dgram_server_argv);
238    exit(0);
239}
240
241/* dgram_server_abort - terminate after abnormal master exit */
242
243static void dgram_server_abort(int unused_event, void *unused_context)
244{
245    if (msg_verbose)
246	msg_info("master disconnect -- exiting");
247    dgram_server_exit();
248}
249
250/* dgram_server_timeout - idle time exceeded */
251
252static void dgram_server_timeout(int unused_event, void *unused_context)
253{
254    if (msg_verbose)
255	msg_info("idle timeout -- exiting");
256    dgram_server_exit();
257}
258
259/* dgram_server_wakeup - wake up application */
260
261static void dgram_server_wakeup(int fd)
262{
263    char    buf[DGRAM_BUF_SIZE];
264    ssize_t len;
265
266    /*
267     * Commit suicide when the master process disconnected from us, after
268     * handling the client request.
269     */
270    if (master_notify(var_pid, dgram_server_generation, MASTER_STAT_TAKEN) < 0)
271	 /* void */ ;
272    if (dgram_server_in_flow_delay && mail_flow_get(1) < 0)
273	doze(var_in_flow_delay * 1000000);
274    if ((len = recv(fd, buf, sizeof(buf), 0)) >= 0)
275	dgram_server_service(buf, len, dgram_server_name, dgram_server_argv);
276    if (master_notify(var_pid, dgram_server_generation, MASTER_STAT_AVAIL) < 0)
277	dgram_server_abort(EVENT_NULL_TYPE, EVENT_NULL_CONTEXT);
278    if (var_idle_limit > 0)
279	event_request_timer(dgram_server_timeout, (void *) 0, var_idle_limit);
280    /* Avoid integer wrap-around in a persistent process.  */
281    if (use_count < INT_MAX)
282	use_count++;
283}
284
285/* dgram_server_accept_unix - handle UNIX-domain socket event */
286
287static void dgram_server_accept_unix(int unused_event, void *context)
288{
289    const char *myname = "dgram_server_accept";
290    int     listen_fd = CAST_ANY_PTR_TO_INT(context);
291
292    if (dgram_server_lock != 0
293	&& myflock(vstream_fileno(dgram_server_lock), INTERNAL_LOCK,
294		   MYFLOCK_OP_NONE) < 0)
295	msg_fatal("select unlock: %m");
296
297    if (msg_verbose)
298	msg_info("%s: request arrived", myname);
299
300    /*
301     * Read whatever the other side wrote. The socket is non-blocking so we
302     * won't get stuck when multiple processes wake up.
303     */
304    if (dgram_server_pre_accept)
305	dgram_server_pre_accept(dgram_server_name, dgram_server_argv);
306    dgram_server_wakeup(listen_fd);
307}
308
309/* dgram_server_main - the real main program */
310
311NORETURN dgram_server_main(int argc, char **argv, DGRAM_SERVER_FN service,...)
312{
313    const char *myname = "dgram_server_main";
314    char   *root_dir = 0;
315    char   *user_name = 0;
316    int     debug_me = 0;
317    int     daemon_mode = 1;
318    char   *service_name = basename(argv[0]);
319    int     delay;
320    int     c;
321    int     socket_count = 1;
322    int     fd;
323    va_list ap;
324    MAIL_SERVER_INIT_FN pre_init = 0;
325    MAIL_SERVER_INIT_FN post_init = 0;
326    MAIL_SERVER_LOOP_FN loop = 0;
327    int     key;
328    char   *transport = 0;
329    char   *lock_path;
330    VSTRING *why;
331    int     alone = 0;
332    int     zerolimit = 0;
333    WATCHDOG *watchdog;
334    char   *oname_val;
335    char   *oname;
336    char   *oval;
337    const char *err;
338    char   *generation;
339    int     msg_vstream_needed = 0;
340    const char *dsn_filter_title;
341    const char **dsn_filter_maps;
342
343    /*
344     * Process environment options as early as we can.
345     */
346    if (getenv(CONF_ENV_VERB))
347	msg_verbose = 1;
348    if (getenv(CONF_ENV_DEBUG))
349	debug_me = 1;
350
351    /*
352     * Don't die when a process goes away unexpectedly.
353     */
354    signal(SIGPIPE, SIG_IGN);
355
356    /*
357     * Don't die for frivolous reasons.
358     */
359#ifdef SIGXFSZ
360    signal(SIGXFSZ, SIG_IGN);
361#endif
362
363    /*
364     * May need this every now and then.
365     */
366    var_procname = mystrdup(basename(argv[0]));
367    set_mail_conf_str(VAR_PROCNAME, var_procname);
368
369    /*
370     * Initialize logging and exit handler. Do the syslog first, so that its
371     * initialization completes before we enter the optional chroot jail.
372     */
373    maillog_client_init(mail_task(var_procname), MAILLOG_CLIENT_FLAG_NONE);
374    if (msg_verbose)
375	msg_info("daemon started");
376
377    /*
378     * Check the Postfix library version as soon as we enable logging.
379     */
380    MAIL_VERSION_CHECK;
381
382    /*
383     * Initialize from the configuration file. Allow command-line options to
384     * override compiled-in defaults or configured parameter values.
385     */
386    mail_conf_suck();
387
388    /*
389     * After database open error, continue execution with reduced
390     * functionality.
391     */
392    dict_allow_surrogate = 1;
393
394    /*
395     * Pick up policy settings from master process. Shut up error messages to
396     * stderr, because no-one is going to see them.
397     */
398    opterr = 0;
399    while ((c = GETOPT(argc, argv, "cdDi:lm:n:o:s:t:uvVz")) > 0) {
400	switch (c) {
401	case 'c':
402	    root_dir = "setme";
403	    break;
404	case 'd':
405	    daemon_mode = 0;
406	    break;
407	case 'D':
408	    debug_me = 1;
409	    break;
410	case 'i':
411	    mail_conf_update(VAR_MAX_IDLE, optarg);
412	    break;
413	case 'l':
414	    alone = 1;
415	    break;
416	case 'm':
417	    mail_conf_update(VAR_MAX_USE, optarg);
418	    break;
419	case 'n':
420	    service_name = optarg;
421	    break;
422	case 'o':
423	    oname_val = mystrdup(optarg);
424	    if ((err = split_nameval(oname_val, &oname, &oval)) != 0)
425		msg_fatal("invalid \"-o %s\" option value: %s", optarg, err);
426	    mail_conf_update(oname, oval);
427	    myfree(oname_val);
428	    break;
429	case 's':
430	    if ((socket_count = atoi(optarg)) <= 0)
431		msg_fatal("invalid socket_count: %s", optarg);
432	    break;
433	case 't':
434	    transport = optarg;
435	    break;
436	case 'u':
437	    user_name = "setme";
438	    break;
439	case 'v':
440	    msg_verbose++;
441	    break;
442	case 'V':
443	    if (++msg_vstream_needed == 1)
444		msg_vstream_init(mail_task(var_procname), VSTREAM_ERR);
445	    break;
446	case 'z':
447	    zerolimit = 1;
448	    break;
449	default:
450	    msg_fatal("invalid option: %c", optopt);
451	    break;
452	}
453    }
454    set_mail_conf_str(VAR_SERVNAME, service_name);
455
456    /*
457     * Initialize generic parameters and re-initialize logging in case of a
458     * non-default program name or logging destination.
459     */
460    mail_params_init();
461    maillog_client_init(mail_task(var_procname), MAILLOG_CLIENT_FLAG_NONE);
462
463    /*
464     * Register higher-level dictionaries and initialize the support for
465     * dynamically-loaded dictionaries.
466     */
467    mail_dict_init();
468
469    /*
470     * If not connected to stdin, stdin must not be a terminal.
471     */
472    if (daemon_mode && isatty(STDIN_FILENO)) {
473	msg_vstream_init(var_procname, VSTREAM_ERR);
474	msg_fatal("do not run this command by hand");
475    }
476
477    /*
478     * Application-specific initialization.
479     */
480    va_start(ap, service);
481    while ((key = va_arg(ap, int)) != 0) {
482	switch (key) {
483	case MAIL_SERVER_INT_TABLE:
484	    get_mail_conf_int_table(va_arg(ap, CONFIG_INT_TABLE *));
485	    break;
486	case MAIL_SERVER_LONG_TABLE:
487	    get_mail_conf_long_table(va_arg(ap, CONFIG_LONG_TABLE *));
488	    break;
489	case MAIL_SERVER_STR_TABLE:
490	    get_mail_conf_str_table(va_arg(ap, CONFIG_STR_TABLE *));
491	    break;
492	case MAIL_SERVER_BOOL_TABLE:
493	    get_mail_conf_bool_table(va_arg(ap, CONFIG_BOOL_TABLE *));
494	    break;
495	case MAIL_SERVER_TIME_TABLE:
496	    get_mail_conf_time_table(va_arg(ap, CONFIG_TIME_TABLE *));
497	    break;
498	case MAIL_SERVER_RAW_TABLE:
499	    get_mail_conf_raw_table(va_arg(ap, CONFIG_RAW_TABLE *));
500	    break;
501	case MAIL_SERVER_NINT_TABLE:
502	    get_mail_conf_nint_table(va_arg(ap, CONFIG_NINT_TABLE *));
503	    break;
504	case MAIL_SERVER_NBOOL_TABLE:
505	    get_mail_conf_nbool_table(va_arg(ap, CONFIG_NBOOL_TABLE *));
506	    break;
507	case MAIL_SERVER_PRE_INIT:
508	    pre_init = va_arg(ap, MAIL_SERVER_INIT_FN);
509	    break;
510	case MAIL_SERVER_POST_INIT:
511	    post_init = va_arg(ap, MAIL_SERVER_INIT_FN);
512	    break;
513	case MAIL_SERVER_LOOP:
514	    loop = va_arg(ap, MAIL_SERVER_LOOP_FN);
515	    break;
516	case MAIL_SERVER_EXIT:
517	    dgram_server_onexit = va_arg(ap, MAIL_SERVER_EXIT_FN);
518	    break;
519	case MAIL_SERVER_PRE_ACCEPT:
520	    dgram_server_pre_accept = va_arg(ap, MAIL_SERVER_ACCEPT_FN);
521	    break;
522	case MAIL_SERVER_IN_FLOW_DELAY:
523	    dgram_server_in_flow_delay = 1;
524	    break;
525	case MAIL_SERVER_SOLITARY:
526	    if (!alone)
527		msg_fatal("service %s requires a process limit of 1",
528			  service_name);
529	    break;
530	case MAIL_SERVER_UNLIMITED:
531	    if (!zerolimit)
532		msg_fatal("service %s requires a process limit of 0",
533			  service_name);
534	    break;
535	case MAIL_SERVER_PRIVILEGED:
536	    if (user_name)
537		msg_fatal("service %s requires privileged operation",
538			  service_name);
539	    break;
540	case MAIL_SERVER_WATCHDOG:
541	    dgram_server_watchdog = *va_arg(ap, int *);
542	    break;
543	case MAIL_SERVER_BOUNCE_INIT:
544	    dsn_filter_title = va_arg(ap, const char *);
545	    dsn_filter_maps = va_arg(ap, const char **);
546	    bounce_client_init(dsn_filter_title, *dsn_filter_maps);
547	    break;
548	default:
549	    msg_panic("%s: unknown argument type: %d", myname, key);
550	}
551    }
552    va_end(ap);
553
554    if (root_dir)
555	root_dir = var_queue_dir;
556    if (user_name)
557	user_name = var_mail_owner;
558
559    /*
560     * Can options be required?
561     */
562    if (transport == 0)
563	msg_fatal("no transport type specified");
564    else if (strcasecmp(transport, MASTER_XPORT_NAME_UXDG) == 0)
565	dgram_server_accept = dgram_server_accept_unix;
566    else
567	msg_fatal("unsupported transport type: %s", transport);
568
569    /*
570     * Retrieve process generation from environment.
571     */
572    if ((generation = getenv(MASTER_GEN_NAME)) != 0) {
573	if (!alldig(generation))
574	    msg_fatal("bad generation: %s", generation);
575	OCTAL_TO_UNSIGNED(dgram_server_generation, generation);
576	if (msg_verbose)
577	    msg_info("process generation: %s (%o)",
578		     generation, dgram_server_generation);
579    }
580
581    /*
582     * Optionally start the debugger on ourself.
583     */
584    if (debug_me)
585	debug_process();
586
587    /*
588     * Traditionally, BSD select() can't handle multiple processes selecting
589     * on the same socket, and wakes up every process in select(). See TCP/IP
590     * Illustrated volume 2 page 532. We avoid select() collisions with an
591     * external lock file.
592     */
593    if (!alone) {
594	lock_path = concatenate(DEF_PID_DIR, "/", transport,
595				".", service_name, (char *) 0);
596	why = vstring_alloc(1);
597	if ((dgram_server_lock = safe_open(lock_path, O_CREAT | O_RDWR, 0600,
598				      (struct stat *) 0, -1, -1, why)) == 0)
599	    msg_fatal("open lock file %s: %s", lock_path, vstring_str(why));
600	close_on_exec(vstream_fileno(dgram_server_lock), CLOSE_ON_EXEC);
601	myfree(lock_path);
602	vstring_free(why);
603    }
604
605    /*
606     * Set up call-back info.
607     */
608    dgram_server_service = service;
609    dgram_server_name = service_name;
610    dgram_server_argv = argv + optind;
611
612    /*
613     * Run pre-jail initialization.
614     */
615    if (chdir(var_queue_dir) < 0)
616	msg_fatal("chdir(\"%s\"): %m", var_queue_dir);
617    if (pre_init)
618	pre_init(dgram_server_name, dgram_server_argv);
619
620    /*
621     * Optionally, restrict the damage that this process can do.
622     */
623    resolve_local_init();
624    tzset();
625    chroot_uid(root_dir, user_name);
626
627    /*
628     * Run post-jail initialization.
629     */
630    if (post_init)
631	post_init(dgram_server_name, dgram_server_argv);
632
633    /*
634     * Running as a semi-resident server. Service requests. Terminate when we
635     * have serviced a sufficient number of requests, when no-one has been
636     * talking to us for a configurable amount of time, or when the master
637     * process terminated abnormally.
638     */
639    if (var_idle_limit > 0)
640	event_request_timer(dgram_server_timeout, (void *) 0, var_idle_limit);
641    for (fd = MASTER_LISTEN_FD; fd < MASTER_LISTEN_FD + socket_count; fd++) {
642	event_enable_read(fd, dgram_server_accept, CAST_INT_TO_VOID_PTR(fd));
643	close_on_exec(fd, CLOSE_ON_EXEC);
644    }
645    event_enable_read(MASTER_STATUS_FD, dgram_server_abort, (void *) 0);
646    close_on_exec(MASTER_STATUS_FD, CLOSE_ON_EXEC);
647    close_on_exec(MASTER_FLOW_READ, CLOSE_ON_EXEC);
648    close_on_exec(MASTER_FLOW_WRITE, CLOSE_ON_EXEC);
649    watchdog = watchdog_create(dgram_server_watchdog,
650			       (WATCHDOG_FN) 0, (void *) 0);
651
652    /*
653     * The event loop, at last.
654     */
655    while (var_use_limit == 0 || use_count < var_use_limit) {
656	if (dgram_server_lock != 0) {
657	    watchdog_stop(watchdog);
658	    if (myflock(vstream_fileno(dgram_server_lock), INTERNAL_LOCK,
659			MYFLOCK_OP_EXCLUSIVE) < 0)
660		msg_fatal("select lock: %m");
661	}
662	watchdog_start(watchdog);
663	delay = loop ? loop(dgram_server_name, dgram_server_argv) : -1;
664	event_loop(delay);
665    }
666    dgram_server_exit();
667}
668