1 /* $Slackware: inetd.c 1.79s 2001/02/06 13:18:00 volkerdi Exp $ */
2 /* $OpenBSD: inetd.c,v 1.79 2001/01/30 08:30:57 deraadt Exp $ */
3 /* $NetBSD: inetd.c,v 1.11 1996/02/22 11:14:41 mycroft Exp $ */
4 /* Busybox port by Vladimir Oleynik (C) 2001-2005 <dzo@simtreas.ru> */
6 * Copyright (c) 1983,1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * Inetd - Internet super-server
41 * This program invokes all internet services as needed.
42 * connection-oriented services are invoked each time a
43 * connection is made, by creating a process. This process
44 * is passed the connection as file descriptor 0 and is
45 * expected to do a getpeername to find out the source host
48 * Datagram oriented services are invoked when a datagram
49 * arrives; a process is created and passed a pending message
50 * on file descriptor 0. Datagram servers may either connect
51 * to their peer, freeing up the original socket for inetd
52 * to receive further messages on, or ``take over the socket'',
53 * processing all arriving datagrams and, eventually, timing
54 * out. The first type of server is said to be ``multi-threaded'';
55 * the second type of server ``single-threaded''.
57 * Inetd uses a configuration file which is read at startup
58 * and, possibly, at some later time in response to a hangup signal.
59 * The configuration file is ``free format'' with fields given in the
60 * order shown below. Continuation lines for an entry must begin with
61 * a space or tab. All fields must be present in each entry.
63 * service name must be in /etc/services
64 * socket type stream/dgram/raw/rdm/seqpacket
65 * protocol must be in /etc/protocols
66 * wait/nowait[.max] single-threaded/multi-threaded, max #
67 * user[.group] or user[:group] user/group to run daemon as
68 * server program full path name
69 * server program arguments maximum of MAXARGS (20)
72 * service name/version must be in /etc/rpc
73 * socket type stream/dgram/raw/rdm/seqpacket
74 * protocol must be in /etc/protocols
75 * wait/nowait[.max] single-threaded/multi-threaded
76 * user[.group] or user[:group] user to run daemon as
77 * server program full path name
78 * server program arguments maximum of MAXARGS (20)
80 * For non-RPC services, the "service name" can be of the form
81 * hostaddress:servicename, in which case the hostaddress is used
82 * as the host portion of the address to listen on. If hostaddress
83 * consists of a single `*' character, INADDR_ANY is used.
85 * A line can also consist of just
87 * where hostaddress is as in the preceding paragraph. Such a line must
88 * have no further fields; the specified hostaddress is remembered and
89 * used for all further lines that have no hostaddress specified,
90 * until the next such line (or EOF). (This is why * is provided to
91 * allow explicit specification of INADDR_ANY.) A line
93 * is implicitly in effect at the beginning of the file.
95 * The hostaddress specifier may (and often will) contain dots;
96 * the service name must not.
98 * For RPC services, host-address specifiers are accepted and will
99 * work to some extent; however, because of limitations in the
100 * portmapper interface, it will not work to try to give more than
101 * one line for any given RPC service, even if the host-address
102 * specifiers are different.
104 * Comment lines are indicated by a `#' in column 1.
108 * Here's the scoop concerning the user[.:]group feature:
110 * 1) set-group-option off.
112 * a) user = root: NO setuid() or setgid() is done
114 * b) other: setgid(primary group as found in passwd)
115 * initgroups(name, primary group)
118 * 2) set-group-option on.
120 * a) user = root: setgid(specified group)
124 * b) other: setgid(specified group)
125 * initgroups(name, specified group)
130 #include <sys/param.h>
131 #include <sys/stat.h>
132 #include <sys/ioctl.h>
133 #include <sys/socket.h>
135 #include <sys/file.h>
136 #include <sys/wait.h>
137 #include <sys/time.h>
138 #include <sys/resource.h>
141 #include <netinet/in.h>
142 #include <arpa/inet.h>
157 //#define CONFIG_FEATURE_INETD_RPC
158 //#define CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
159 //#define CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
160 //#define CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
161 //#define CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
162 //#define CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
163 //#define CONFIG_FEATURE_IPV6
165 #ifdef CONFIG_FEATURE_INETD_RPC
167 #include <rpc/pmap_clnt.h>
168 #include <rpcsvc/nfs_prot.h>
171 #define _PATH_INETDCONF "/etc/inetd.conf"
172 #define _PATH_INETDPID "/var/run/inetd.pid"
175 #define TOOMANY 0 /* don't start more than TOOMANY */
177 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
178 #define RETRYTIME (60*10) /* retry after bind or server fail */
180 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
182 #ifndef RLIMIT_NOFILE
183 #define RLIMIT_NOFILE RLIMIT_OFILE
190 /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
191 #define FD_MARGIN (8)
192 static rlim_t rlim_ofile_cur = OPEN_MAX;
193 static struct rlimit rlim_ofile;
196 /* Check unsupporting builtin */
197 #if defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO || \
198 defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD || \
199 defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME || \
200 defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME || \
201 defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
202 # define INETD_FEATURE_ENABLED
205 #if defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO || \
206 defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD || \
207 defined CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
208 # define INETD_SETPROCTITLE
211 typedef struct servtab
213 char *se_hostaddr; /* host address to listen on */
214 char *se_service; /* name of service */
215 int se_socktype; /* type of socket to use */
216 int se_family; /* address family */
217 char *se_proto; /* protocol used */
218 #ifdef CONFIG_FEATURE_INETD_RPC
219 int se_rpcprog; /* rpc program number */
220 int se_rpcversl; /* rpc program lowest version */
221 int se_rpcversh; /* rpc program highest version */
222 #define isrpcservice(sep) ((sep)->se_rpcversl != 0)
224 #define isrpcservice(sep) 0
226 pid_t se_wait; /* single threaded server */
227 short se_checked; /* looked at during merge */
228 char *se_user; /* user name to run as */
229 char *se_group; /* group name to run as */
230 #ifdef INETD_FEATURE_ENABLED
231 const struct biltin *se_bi; /* if built-in, description */
233 char *se_server; /* server program */
235 char *se_argv[MAXARGV + 1]; /* program arguments */
236 int se_fd; /* open descriptor */
239 struct sockaddr se_un_ctrladdr;
240 struct sockaddr_in se_un_ctrladdr_in;
241 #ifdef CONFIG_FEATURE_IPV6
242 struct sockaddr_in6 se_un_ctrladdr_in6;
244 struct sockaddr_un se_un_ctrladdr_un;
245 } se_un; /* bound address */
246 #define se_ctrladdr se_un.se_un_ctrladdr
247 #define se_ctrladdr_in se_un.se_un_ctrladdr_in
248 #define se_ctrladdr_in6 se_un.se_un_ctrladdr_in6
249 #define se_ctrladdr_un se_un.se_un_ctrladdr_un
250 int se_ctrladdr_size;
251 int se_max; /* max # of instances of this service */
252 int se_count; /* number started since se_time */
253 struct timeval se_time; /* start of se_count */
254 struct servtab *se_next;
257 static servtab_t *servtab;
259 #ifdef INETD_FEATURE_ENABLED
262 const char *bi_service; /* internally provided service name */
263 int bi_socktype; /* type of socket supported */
264 short bi_fork; /* 1 if should fork before call */
265 short bi_wait; /* 1 if should wait for child */
266 void (*bi_fn) (int, servtab_t *);
269 /* Echo received data */
270 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
271 static void echo_stream (int, servtab_t *);
272 static void echo_dg (int, servtab_t *);
274 /* Internet /dev/null */
275 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
276 static void discard_stream (int, servtab_t *);
277 static void discard_dg (int, servtab_t *);
279 /* Return 32 bit time since 1900 */
280 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
281 static void machtime_stream (int, servtab_t *);
282 static void machtime_dg (int, servtab_t *);
284 /* Return human-readable time */
285 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
286 static void daytime_stream (int, servtab_t *);
287 static void daytime_dg (int, servtab_t *);
289 /* Familiar character generator */
290 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
291 static void chargen_stream (int, servtab_t *);
292 static void chargen_dg (int, servtab_t *);
295 static const struct biltin biltins[] = {
296 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
297 /* Echo received data */
298 {"echo", SOCK_STREAM, 1, 0, echo_stream,},
299 {"echo", SOCK_DGRAM, 0, 0, echo_dg,},
301 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
302 /* Internet /dev/null */
303 {"discard", SOCK_STREAM, 1, 0, discard_stream,},
304 {"discard", SOCK_DGRAM, 0, 0, discard_dg,},
306 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
307 /* Return 32 bit time since 1900 */
308 {"time", SOCK_STREAM, 0, 0, machtime_stream,},
309 {"time", SOCK_DGRAM, 0, 0, machtime_dg,},
311 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
312 /* Return human-readable time */
313 {"daytime", SOCK_STREAM, 0, 0, daytime_stream,},
314 {"daytime", SOCK_DGRAM, 0, 0, daytime_dg,},
316 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
317 /* Familiar character generator */
318 {"chargen", SOCK_STREAM, 1, 0, chargen_stream,},
319 {"chargen", SOCK_DGRAM, 0, 0, chargen_dg,},
321 {NULL, 0, 0, 0, NULL}
323 #endif /* INETD_FEATURE_ENABLED */
325 static int global_queuelen = 128;
326 static int nsock, maxsock;
327 static fd_set allsock;
328 static int toomany = TOOMANY;
329 static int timingout;
330 static struct servent *sp;
333 static char *CONFIG = _PATH_INETDCONF;
335 static FILE *fconfig;
336 static char line[1024];
337 static char *defhost;
339 static char *newstr (char *cp)
341 if ((cp = strdup (cp ? cp : "")))
343 syslog (LOG_ERR, "strdup: %m");
347 static int setconfig (void)
350 defhost = newstr ("*");
351 if (fconfig != NULL) {
352 fseek (fconfig, 0L, SEEK_SET);
355 fconfig = fopen (CONFIG, "r");
356 return (fconfig != NULL);
359 static void endconfig (void)
362 (void) fclose (fconfig);
369 #ifdef CONFIG_FEATURE_INETD_RPC
370 static void register_rpc (servtab_t *sep)
373 struct sockaddr_in ir_sin;
377 if ((pp = getprotobyname (sep->se_proto + 4)) == NULL) {
378 syslog (LOG_ERR, "%s: getproto: %m", sep->se_proto);
381 size = sizeof ir_sin;
382 if (getsockname (sep->se_fd, (struct sockaddr *) &ir_sin, &size) < 0) {
383 syslog (LOG_ERR, "%s/%s: getsockname: %m",
384 sep->se_service, sep->se_proto);
388 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
389 (void) pmap_unset (sep->se_rpcprog, n);
390 if (!pmap_set (sep->se_rpcprog, n, pp->p_proto, ntohs (ir_sin.sin_port)))
391 syslog (LOG_ERR, "%s %s: pmap_set: %u %u %u %u: %m",
392 sep->se_service, sep->se_proto,
393 sep->se_rpcprog, n, pp->p_proto, ntohs (ir_sin.sin_port));
397 static void unregister_rpc (servtab_t *sep)
401 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
402 if (!pmap_unset (sep->se_rpcprog, n))
403 syslog (LOG_ERR, "pmap_unset(%u, %u)", sep->se_rpcprog, n);
406 #endif /* CONFIG_FEATURE_INETD_RPC */
408 static void freeconfig (servtab_t *cp)
412 free (cp->se_hostaddr);
413 free (cp->se_service);
417 free (cp->se_server);
418 for (i = 0; i < MAXARGV; i++)
420 free (cp->se_argv[i]);
423 static int bump_nofile (void)
429 if (getrlimit (RLIMIT_NOFILE, &rl) < 0) {
430 syslog (LOG_ERR, "getrlimit: %m");
433 rl.rlim_cur = MIN (rl.rlim_max, rl.rlim_cur + FD_CHUNK);
434 rl.rlim_cur = MIN (FD_SETSIZE, rl.rlim_cur + FD_CHUNK);
435 if (rl.rlim_cur <= rlim_ofile_cur) {
436 syslog (LOG_ERR, "bump_nofile: cannot extend file limit, max = %d",
441 if (setrlimit (RLIMIT_NOFILE, &rl) < 0) {
442 syslog (LOG_ERR, "setrlimit: %m");
446 rlim_ofile_cur = rl.rlim_cur;
450 static void setup (servtab_t *sep)
455 if ((sep->se_fd = socket (sep->se_family, sep->se_socktype, 0)) < 0) {
456 syslog (LOG_ERR, "%s/%s: socket: %m", sep->se_service, sep->se_proto);
459 #define turnon(fd, opt) \
460 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
461 if (turnon (sep->se_fd, SO_REUSEADDR) < 0)
462 syslog (LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
465 #ifdef CONFIG_FEATURE_INETD_RPC
466 if (isrpcservice (sep)) {
470 * for RPC services, attempt to use a reserved port
471 * if they are going to be running as root.
473 * Also, zero out the port for all RPC services; let bind()
476 sep->se_ctrladdr_in.sin_port = 0;
477 if (sep->se_user && (pwd = getpwnam (sep->se_user)) &&
478 pwd->pw_uid == 0 && uid == 0)
479 r = bindresvport (sep->se_fd, &sep->se_ctrladdr_in);
481 r = bind (sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size);
483 socklen_t len = sep->se_ctrladdr_size;
484 int saveerrno = errno;
486 /* update se_ctrladdr_in.sin_port */
487 r = getsockname (sep->se_fd, &sep->se_ctrladdr, &len);
494 r = bind (sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size);
496 syslog (LOG_ERR, "%s/%s (%d): bind: %m",
497 sep->se_service, sep->se_proto, sep->se_ctrladdr.sa_family);
506 if (sep->se_socktype == SOCK_STREAM)
507 listen (sep->se_fd, global_queuelen);
509 FD_SET (sep->se_fd, &allsock);
511 if (sep->se_fd > maxsock) {
512 maxsock = sep->se_fd;
513 if ((rlim_t)maxsock > rlim_ofile_cur - FD_MARGIN)
518 static char *nextline (void)
523 if (fgets (line, sizeof (line), fd) == NULL)
525 cp = strchr (line, '\n');
531 static char *skip (char **cpp) /* int report; */
539 /* syslog(LOG_ERR, "syntax error in inetd config file"); */
544 while (*cp == ' ' || *cp == '\t')
550 (void) ungetc (c, fconfig);
551 if (c == ' ' || c == '\t')
552 if ((cp = nextline ()))
559 while (*cp && *cp != ' ' && *cp != '\t')
563 /* if ((*cpp = cp) == NULL) */
570 static servtab_t *new_servtab(void)
574 sep = (servtab_t *) malloc (sizeof (servtab_t));
576 syslog (LOG_ERR, bb_msg_memory_exhausted);
582 static servtab_t *dupconfig (servtab_t *sep)
587 newtab = new_servtab();
588 memset (newtab, 0, sizeof (servtab_t));
589 newtab->se_service = sep->se_service ? newstr (sep->se_service) : NULL;
590 newtab->se_socktype = sep->se_socktype;
591 newtab->se_family = sep->se_family;
592 newtab->se_proto = sep->se_proto ? newstr (sep->se_proto) : NULL;
593 #ifdef CONFIG_FEATURE_INETD_RPC
594 newtab->se_rpcprog = sep->se_rpcprog;
595 newtab->se_rpcversl = sep->se_rpcversl;
596 newtab->se_rpcversh = sep->se_rpcversh;
598 newtab->se_wait = sep->se_wait;
599 newtab->se_user = sep->se_user ? newstr (sep->se_user) : NULL;
600 newtab->se_group = sep->se_group ? newstr (sep->se_group) : NULL;
601 #ifdef INETD_FEATURE_ENABLED
602 newtab->se_bi = sep->se_bi;
604 newtab->se_server = sep->se_server ? newstr (sep->se_server) : 0;
606 for (argc = 0; argc <= MAXARGV; argc++)
607 newtab->se_argv[argc] = sep->se_argv[argc] ?
608 newstr (sep->se_argv[argc]) : NULL;
609 newtab->se_max = sep->se_max;
614 static servtab_t *getconfigent (void)
625 /* memset(sep, 0, sizeof *sep); */
627 /* freeconfig(sep); */
629 while ((cp = nextline ()) && *cp == '#');
635 memset ((char *) sep, 0, sizeof *sep);
642 /* Check for a host name. */
643 hostdelim = strrchr (arg, ':');
646 sep->se_hostaddr = newstr (arg);
649 * If the line is of the form `host:', then just change the
650 * default host for the following lines.
656 defhost = sep->se_hostaddr;
661 sep->se_hostaddr = newstr (defhost);
663 sep->se_service = newstr (arg);
666 if (strcmp (arg, "stream") == 0)
667 sep->se_socktype = SOCK_STREAM;
668 else if (strcmp (arg, "dgram") == 0)
669 sep->se_socktype = SOCK_DGRAM;
670 else if (strcmp (arg, "rdm") == 0)
671 sep->se_socktype = SOCK_RDM;
672 else if (strcmp (arg, "seqpacket") == 0)
673 sep->se_socktype = SOCK_SEQPACKET;
674 else if (strcmp (arg, "raw") == 0)
675 sep->se_socktype = SOCK_RAW;
677 sep->se_socktype = -1;
679 sep->se_proto = newstr (skip (&cp));
681 if (strcmp (sep->se_proto, "unix") == 0) {
682 sep->se_family = AF_UNIX;
684 sep->se_family = AF_INET;
685 if (sep->se_proto[strlen (sep->se_proto) - 1] == '6')
686 #ifdef CONFIG_FEATURE_IPV6
687 sep->se_family = AF_INET6;
689 syslog (LOG_ERR, "%s: IPV6 not supported", sep->se_proto);
691 if (strncmp (sep->se_proto, "rpc/", 4) == 0) {
692 #ifdef CONFIG_FEATURE_INETD_RPC
696 p = strchr (sep->se_service, '/');
698 syslog (LOG_ERR, "%s: no rpc version", sep->se_service);
702 l = strtol (p, &ccp, 0);
703 if (ccp == p || l < 0 || l > INT_MAX) {
705 syslog (LOG_ERR, "%s/%s: bad rpc version", sep->se_service, p);
708 sep->se_rpcversl = sep->se_rpcversh = l;
711 l = strtol (p, &ccp, 0);
712 if (ccp == p || l < 0 || l > INT_MAX || l < sep->se_rpcversl || *ccp)
714 sep->se_rpcversh = l;
715 } else if (*ccp != '\0')
718 syslog (LOG_ERR, "%s: rpc services not supported", sep->se_service);
727 char *s = strchr (arg, '.');
730 sep->se_max = atoi (s);
732 sep->se_max = toomany;
734 sep->se_wait = strcmp (arg, "wait") == 0;
735 /* if ((arg = skip(&cp, 1)) == NULL) */
737 sep->se_user = newstr (skip (&cp));
738 arg = strchr (sep->se_user, '.');
740 arg = strchr (sep->se_user, ':');
743 sep->se_group = newstr (arg);
745 /* if ((arg = skip(&cp, 1)) == NULL) */
748 sep->se_server = newstr (skip (&cp));
749 if (strcmp (sep->se_server, "internal") == 0) {
750 #ifdef INETD_FEATURE_ENABLED
751 const struct biltin *bi;
753 for (bi = biltins; bi->bi_service; bi++)
754 if (bi->bi_socktype == sep->se_socktype &&
755 strcmp (bi->bi_service, sep->se_service) == 0)
757 if (bi->bi_service == 0) {
758 syslog (LOG_ERR, "internal service %s unknown", sep->se_service);
762 sep->se_wait = bi->bi_wait;
764 syslog (LOG_ERR, "internal service %s unknown", sep->se_service);
768 #ifdef INETD_FEATURE_ENABLED
773 for (arg = skip (&cp); cp; arg = skip (&cp)) {
775 sep->se_argv[argc++] = newstr (arg);
777 while (argc <= MAXARGV)
778 sep->se_argv[argc++] = NULL;
781 * Now that we've processed the entire line, check if the hostname
782 * specifier was a comma separated list of hostnames. If so
783 * we'll make new entries for each address.
785 while ((hostdelim = strrchr (sep->se_hostaddr, ',')) != NULL) {
786 nsep = dupconfig (sep);
789 * NULL terminate the hostname field of the existing entry,
790 * and make a dup for the new entry.
793 nsep->se_hostaddr = newstr (hostdelim);
795 nsep->se_next = sep->se_next;
800 while (nsep != NULL) {
801 nsep->se_checked = 1;
802 if (nsep->se_family == AF_INET) {
803 if (!strcmp (nsep->se_hostaddr, "*"))
804 nsep->se_ctrladdr_in.sin_addr.s_addr = INADDR_ANY;
805 else if (!inet_aton (nsep->se_hostaddr, &nsep->se_ctrladdr_in.sin_addr)) {
808 hp = gethostbyname (nsep->se_hostaddr);
810 syslog (LOG_ERR, "%s: unknown host", nsep->se_hostaddr);
811 nsep->se_checked = 0;
813 } else if (hp->h_addrtype != AF_INET) {
815 "%s: address isn't an Internet "
816 "address", nsep->se_hostaddr);
817 nsep->se_checked = 0;
822 memmove (&nsep->se_ctrladdr_in.sin_addr,
823 hp->h_addr_list[0], sizeof (struct in_addr));
824 while (hp->h_addr_list[i] != NULL) {
825 psep = dupconfig (nsep);
826 psep->se_hostaddr = newstr (nsep->se_hostaddr);
827 psep->se_checked = 1;
828 memmove (&psep->se_ctrladdr_in.sin_addr,
829 hp->h_addr_list[i], sizeof (struct in_addr));
830 psep->se_ctrladdr_size = sizeof (psep->se_ctrladdr_in);
832 /* Prepend to list, don't want to look up its */
833 /* hostname again. */
840 /* XXX BUG?: is this skip: label supposed to remain? */
842 nsep = nsep->se_next;
846 * Finally, free any entries which failed the gethostbyname
851 while (nsep != NULL) {
854 if (nsep->se_checked == 0) {
860 nsep = nsep->se_next;
861 psep->se_next = nsep;
865 nsep->se_checked = 0;
867 nsep = nsep->se_next;
874 #define Block_Using_Signals(m) do { sigemptyset(&m); \
875 sigaddset(&m, SIGCHLD); \
876 sigaddset(&m, SIGHUP); \
877 sigaddset(&m, SIGALRM); \
878 sigprocmask(SIG_BLOCK, &m, NULL); \
882 static servtab_t *enter (servtab_t *cp)
890 #ifdef CONFIG_FEATURE_INETD_RPC
891 sep->se_rpcprog = -1;
893 Block_Using_Signals(omask);
894 sep->se_next = servtab;
896 sigprocmask(SIG_UNBLOCK, &omask, NULL);
900 static int matchconf (servtab_t *old, servtab_t *new)
902 if (strcmp (old->se_service, new->se_service) != 0)
905 if (strcmp (old->se_hostaddr, new->se_hostaddr) != 0)
908 if (strcmp (old->se_proto, new->se_proto) != 0)
912 * If the new servtab is bound to a specific address, check that the
913 * old servtab is bound to the same entry. If the new service is not
914 * bound to a specific address then the check of se_hostaddr above
918 if (old->se_family == AF_INET && new->se_family == AF_INET &&
919 memcmp (&old->se_ctrladdr_in.sin_addr,
920 &new->se_ctrladdr_in.sin_addr,
921 sizeof (new->se_ctrladdr_in.sin_addr)) != 0)
924 #ifdef CONFIG_FEATURE_IPV6
925 if (old->se_family == AF_INET6 && new->se_family == AF_INET6 &&
926 memcmp (&old->se_ctrladdr_in6.sin6_addr,
927 &new->se_ctrladdr_in6.sin6_addr,
928 sizeof (new->se_ctrladdr_in6.sin6_addr)) != 0)
934 static void config (int sig __attribute__((unused)))
936 servtab_t *sep, *cp, **sepp;
943 syslog (LOG_ERR, "%s: %m", CONFIG);
946 for (sep = servtab; sep; sep = sep->se_next)
948 cp = getconfigent ();
950 for (sep = servtab; sep; sep = sep->se_next)
951 if (matchconf (sep, cp))
957 #define SWAP(type, a, b) do {type c=(type)a; a=(type)b; b=(type)c;} while (0)
959 Block_Using_Signals(omask);
961 * sep->se_wait may be holding the pid of a daemon
962 * that we're waiting for. If so, don't overwrite
963 * it unless the config file explicitly says don't
967 #ifdef INETD_FEATURE_ENABLED
970 (sep->se_wait == 1 || cp->se_wait == 0))
971 sep->se_wait = cp->se_wait;
972 SWAP (int, cp->se_max, sep->se_max);
973 SWAP (char *, sep->se_user, cp->se_user);
974 SWAP (char *, sep->se_group, cp->se_group);
975 SWAP (char *, sep->se_server, cp->se_server);
976 for (i = 0; i < MAXARGV; i++)
977 SWAP (char *, sep->se_argv[i], cp->se_argv[i]);
980 #ifdef CONFIG_FEATURE_INETD_RPC
981 if (isrpcservice (sep))
982 unregister_rpc (sep);
983 sep->se_rpcversl = cp->se_rpcversl;
984 sep->se_rpcversh = cp->se_rpcversh;
986 sigprocmask(SIG_UNBLOCK, &omask, NULL);
994 switch (sep->se_family) {
996 if (sep->se_fd != -1)
998 (void) unlink (sep->se_service);
999 n = strlen (sep->se_service);
1000 if (n > sizeof sep->se_ctrladdr_un.sun_path - 1)
1001 n = sizeof sep->se_ctrladdr_un.sun_path - 1;
1002 safe_strncpy (sep->se_ctrladdr_un.sun_path, sep->se_service, n + 1);
1003 sep->se_ctrladdr_un.sun_family = AF_UNIX;
1004 sep->se_ctrladdr_size = n + sizeof sep->se_ctrladdr_un.sun_family;
1008 sep->se_ctrladdr_in.sin_family = AF_INET;
1009 /* se_ctrladdr_in was set in getconfigent */
1010 sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in;
1012 #ifdef CONFIG_FEATURE_INETD_RPC
1013 if (isrpcservice (sep)) {
1016 sep->se_rpcprog = atoi (sep->se_service);
1017 if (sep->se_rpcprog == 0) {
1018 rp = getrpcbyname (sep->se_service);
1020 syslog (LOG_ERR, "%s: unknown rpc service", sep->se_service);
1023 sep->se_rpcprog = rp->r_number;
1025 if (sep->se_fd == -1)
1027 if (sep->se_fd != -1)
1032 u_short port = htons (atoi (sep->se_service));
1035 /*XXX*/ strncpy (protoname, sep->se_proto, sizeof (protoname));
1036 if (isdigit (protoname[strlen (protoname) - 1]))
1037 protoname[strlen (protoname) - 1] = '\0';
1038 sp = getservbyname (sep->se_service, protoname);
1041 "%s/%s: unknown service", sep->se_service, sep->se_proto);
1046 if (port != sep->se_ctrladdr_in.sin_port) {
1047 sep->se_ctrladdr_in.sin_port = port;
1048 if (sep->se_fd != -1) {
1049 FD_CLR (sep->se_fd, &allsock);
1051 (void) close (sep->se_fd);
1055 if (sep->se_fd == -1)
1059 #ifdef CONFIG_FEATURE_IPV6
1061 sep->se_ctrladdr_in6.sin6_family = AF_INET6;
1062 /* se_ctrladdr_in was set in getconfigent */
1063 sep->se_ctrladdr_size = sizeof sep->se_ctrladdr_in6;
1065 #ifdef CONFIG_FEATURE_INETD_RPC
1066 if (isrpcservice (sep)) {
1069 sep->se_rpcprog = atoi (sep->se_service);
1070 if (sep->se_rpcprog == 0) {
1071 rp = getrpcbyname (sep->se_service);
1073 syslog (LOG_ERR, "%s: unknown rpc service", sep->se_service);
1076 sep->se_rpcprog = rp->r_number;
1078 if (sep->se_fd == -1)
1080 if (sep->se_fd != -1)
1085 u_short port = htons (atoi (sep->se_service));
1088 /*XXX*/ strncpy (protoname, sep->se_proto, sizeof (protoname));
1089 if (isdigit (protoname[strlen (protoname) - 1]))
1090 protoname[strlen (protoname) - 1] = '\0';
1091 sp = getservbyname (sep->se_service, protoname);
1094 "%s/%s: unknown service", sep->se_service, sep->se_proto);
1099 if (port != sep->se_ctrladdr_in6.sin6_port) {
1100 sep->se_ctrladdr_in6.sin6_port = port;
1101 if (sep->se_fd != -1) {
1102 FD_CLR (sep->se_fd, &allsock);
1104 (void) close (sep->se_fd);
1108 if (sep->se_fd == -1)
1112 #endif /* CONFIG_FEATURE_IPV6 */
1115 if (cp->se_next != NULL) {
1116 servtab_t *tmp = cp;
1122 cp = getconfigent ();
1127 * Purge anything not looked at above.
1129 Block_Using_Signals(omask);
1131 while ((sep = *sepp)) {
1132 if (sep->se_checked) {
1133 sepp = &sep->se_next;
1136 *sepp = sep->se_next;
1137 if (sep->se_fd != -1) {
1138 FD_CLR (sep->se_fd, &allsock);
1140 (void) close (sep->se_fd);
1142 #ifdef CONFIG_FEATURE_INETD_RPC
1143 if (isrpcservice (sep))
1144 unregister_rpc (sep);
1146 if (sep->se_family == AF_UNIX)
1147 (void) unlink (sep->se_service);
1151 sigprocmask(SIG_UNBLOCK, &omask, NULL);
1155 static void reapchild (int sig __attribute__((unused)))
1158 int save_errno = errno, status;
1162 pid = wait3 (&status, WNOHANG, NULL);
1165 for (sep = servtab; sep; sep = sep->se_next)
1166 if (sep->se_wait == pid) {
1167 if (WIFEXITED (status) && WEXITSTATUS (status))
1168 syslog (LOG_WARNING,
1169 "%s: exit status 0x%x",
1170 sep->se_server, WEXITSTATUS (status));
1171 else if (WIFSIGNALED (status))
1172 syslog (LOG_WARNING,
1173 "%s: exit signal 0x%x", sep->se_server, WTERMSIG (status));
1175 FD_SET (sep->se_fd, &allsock);
1182 static void retry (int sig __attribute__((unused)))
1187 for (sep = servtab; sep; sep = sep->se_next) {
1188 if (sep->se_fd == -1) {
1189 switch (sep->se_family) {
1192 #ifdef CONFIG_FEATURE_IPV6
1196 #ifdef CONFIG_FEATURE_INETD_RPC
1197 if (sep->se_fd != -1 && isrpcservice (sep))
1206 static void goaway (int sig __attribute__((unused)))
1210 /* XXX signal race walking sep list */
1211 for (sep = servtab; sep; sep = sep->se_next) {
1212 if (sep->se_fd == -1)
1215 switch (sep->se_family) {
1217 (void) unlink (sep->se_service);
1220 #ifdef CONFIG_FEATURE_IPV6
1223 #ifdef CONFIG_FEATURE_INETD_RPC
1224 if (sep->se_wait == 1 && isrpcservice (sep))
1225 unregister_rpc (sep); /* XXX signal race */
1229 (void) close (sep->se_fd);
1231 (void) unlink (_PATH_INETDPID);
1236 #ifdef INETD_SETPROCTITLE
1238 static char *LastArg;
1241 inetd_setproctitle (char *a, int s)
1245 struct sockaddr_in prt_sin;
1249 size = sizeof (prt_sin);
1250 (void) snprintf (buf, sizeof buf, "-%s", a);
1251 if (getpeername (s, (struct sockaddr *) &prt_sin, &size) == 0) {
1252 char *sa = inet_ntoa (prt_sin.sin_addr);
1254 buf[sizeof (buf) - 1 - strlen (sa) - 3] = '\0';
1259 strncpy (cp, buf, LastArg - cp);
1261 while (cp < LastArg)
1268 inetd_main (int argc, char *argv[])
1272 struct group *grp = NULL;
1274 struct sigaction sa, sapipe;
1280 #ifdef INETD_SETPROCTITLE
1281 extern char **environ;
1282 char **envp = environ;
1285 if (envp == 0 || *envp == 0)
1289 LastArg = envp[-1] + strlen (envp[-1]);
1292 openlog (bb_applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
1294 opt = bb_getopt_ulflags (argc, argv, "R:f", &stoomany);
1298 toomany = strtoul (stoomany, &e, 0);
1299 if (!(toomany >= 0 && *e == '\0')) {
1301 syslog (LOG_ERR, "-R %s: bad value for service invocation rate", stoomany);
1313 bb_error_msg_and_die ("non-root must specify a config file");
1316 #if defined(__uClinux__)
1317 /* reexec for vfork() do continue parent */
1318 vfork_daemon_rexec (0, 0, argc, argv, "-f");
1321 #endif /* uClinux */
1327 gid_t gid = getgid ();
1329 /* If run by hand, ensure groups vector gets trashed */
1330 setgroups (1, &gid);
1336 if ((fp = fopen (_PATH_INETDPID, "w")) != NULL) {
1337 fprintf (fp, "%u\n", getpid ());
1342 if (getrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0) {
1343 syslog (LOG_ERR, "getrlimit: %m");
1345 rlim_ofile_cur = rlim_ofile.rlim_cur;
1346 if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
1347 rlim_ofile_cur = OPEN_MAX;
1350 memset ((char *) &sa, 0, sizeof (sa));
1351 sigemptyset (&sa.sa_mask);
1352 sigaddset (&sa.sa_mask, SIGALRM);
1353 sigaddset (&sa.sa_mask, SIGCHLD);
1354 sigaddset (&sa.sa_mask, SIGHUP);
1355 sa.sa_handler = retry;
1356 sigaction (SIGALRM, &sa, NULL);
1359 sa.sa_handler = config;
1360 sigaction (SIGHUP, &sa, NULL);
1361 sa.sa_handler = reapchild;
1362 sigaction (SIGCHLD, &sa, NULL);
1363 sa.sa_handler = goaway;
1364 sigaction (SIGTERM, &sa, NULL);
1365 sa.sa_handler = goaway;
1366 sigaction (SIGINT, &sa, NULL);
1367 sa.sa_handler = SIG_IGN;
1368 sigaction (SIGPIPE, &sa, &sapipe);
1371 /* space for daemons to overwrite environment for ps */
1372 #define DUMMYSIZE 100
1373 char dummy[DUMMYSIZE];
1375 (void) memset (dummy, 'x', DUMMYSIZE - 1);
1376 dummy[DUMMYSIZE - 1] = '\0';
1378 (void) setenv ("inetd_dummy", dummy, 1);
1386 (void) sigblock (SIGBLOCK);
1389 (void) sigsetmask (0L);
1393 if ((n = select (maxsock + 1, &readable, NULL, NULL, NULL)) <= 0) {
1394 if (n < 0 && errno != EINTR) {
1395 syslog (LOG_WARNING, "select: %m");
1400 for (sep = servtab; n && sep; sep = sep->se_next)
1401 if (sep->se_fd != -1 && FD_ISSET (sep->se_fd, &readable)) {
1403 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
1404 ctrl = accept (sep->se_fd, NULL, NULL);
1408 syslog (LOG_WARNING, "accept (for %s): %m", sep->se_service);
1411 if (sep->se_family == AF_INET && sep->se_socktype == SOCK_STREAM) {
1412 struct sockaddr_in peer;
1413 socklen_t plen = sizeof (peer);
1415 if (getpeername (ctrl, (struct sockaddr *) &peer, &plen) < 0) {
1416 syslog (LOG_WARNING, "could not getpeername");
1420 if (ntohs (peer.sin_port) == 20) {
1421 /* XXX ftp bounce */
1428 (void) sigblock (SIGBLOCK);
1430 #ifdef INETD_FEATURE_ENABLED
1431 if (sep->se_bi == 0 || sep->se_bi->bi_fork)
1434 if (sep->se_count++ == 0)
1435 (void) gettimeofday (&sep->se_time, NULL);
1436 else if (toomany > 0 && sep->se_count >= sep->se_max) {
1439 (void) gettimeofday (&now, NULL);
1440 if (now.tv_sec - sep->se_time.tv_sec > CNT_INTVL) {
1444 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
1446 if (sep->se_family == AF_INET &&
1447 ntohs (sep->se_ctrladdr_in.sin_port) >= IPPORT_RESERVED) {
1449 * Cannot close it -- there are
1450 * thieves on the system.
1451 * Simply ignore the connection.
1457 "%s/%s server failing (looping), service terminated",
1458 sep->se_service, sep->se_proto);
1459 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
1461 FD_CLR (sep->se_fd, &allsock);
1462 (void) close (sep->se_fd);
1477 syslog (LOG_ERR, "fork: %m");
1478 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
1484 if (pid && sep->se_wait) {
1486 FD_CLR (sep->se_fd, &allsock);
1491 #ifdef INETD_FEATURE_ENABLED
1493 (*sep->se_bi->bi_fn) (ctrl, sep);
1497 if ((pwd = getpwnam (sep->se_user)) == NULL) {
1498 syslog (LOG_ERR, "getpwnam: %s: No such user", sep->se_user);
1499 if (sep->se_socktype != SOCK_STREAM)
1500 recv (0, buf, sizeof (buf), 0);
1504 syslog (LOG_ERR, "%s: setsid: %m", sep->se_service);
1505 if (sep->se_group && (grp = getgrnam (sep->se_group)) == NULL) {
1506 syslog (LOG_ERR, "getgrnam: %s: No such group", sep->se_group);
1507 if (sep->se_socktype != SOCK_STREAM)
1508 recv (0, buf, sizeof (buf), 0);
1512 /* a user running private inetd */
1513 if (uid != pwd->pw_uid)
1515 } else if (pwd->pw_uid) {
1516 if (sep->se_group) {
1517 pwd->pw_gid = grp->gr_gid;
1519 setgid ((gid_t) pwd->pw_gid);
1520 initgroups (pwd->pw_name, pwd->pw_gid);
1521 setuid ((uid_t) pwd->pw_uid);
1522 } else if (sep->se_group) {
1523 setgid (grp->gr_gid);
1524 setgroups (1, &grp->gr_gid);
1530 if (rlim_ofile.rlim_cur != rlim_ofile_cur)
1531 if (setrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0)
1532 syslog (LOG_ERR, "setrlimit: %m");
1534 for (tmpint = rlim_ofile_cur - 1; --tmpint > 2;)
1535 (void) close (tmpint);
1536 sigaction (SIGPIPE, &sapipe, NULL);
1537 execv (sep->se_server, sep->se_argv);
1538 if (sep->se_socktype != SOCK_STREAM)
1539 recv (0, buf, sizeof (buf), 0);
1540 syslog (LOG_ERR, "execv %s: %m", sep->se_server);
1544 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
1551 * Internet services provided internally by inetd:
1553 #define BUFSIZE 4096
1555 #if defined(CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO) || \
1556 defined(CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN) || \
1557 defined(CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME)
1558 static int dg_badinput (struct sockaddr_in *dg_sin)
1560 if (ntohs (dg_sin->sin_port) < IPPORT_RESERVED)
1562 if (dg_sin->sin_addr.s_addr == htonl (INADDR_BROADCAST))
1564 /* XXX compare against broadcast addresses in SIOCGIFCONF list? */
1569 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO
1570 /* Echo service -- echo data back */
1573 echo_stream (int s, servtab_t *sep)
1575 char buffer[BUFSIZE];
1578 inetd_setproctitle (sep->se_service, s);
1579 while ((i = read (s, buffer, sizeof (buffer))) > 0 &&
1580 write (s, buffer, i) > 0);
1584 /* Echo service -- echo data back */
1587 echo_dg (int s, servtab_t *sep __attribute__((unused)))
1589 char buffer[BUFSIZE];
1592 /* struct sockaddr_storage ss; */
1596 if ((i = recvfrom (s, buffer, sizeof (buffer), 0, &sa, &size)) < 0)
1598 if (dg_badinput ((struct sockaddr_in *) &sa))
1600 (void) sendto (s, buffer, i, 0, &sa, sizeof (sa));
1602 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_ECHO */
1604 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD
1605 /* Discard service -- ignore data */
1608 discard_stream (int s, servtab_t *sep)
1610 char buffer[BUFSIZE];
1612 inetd_setproctitle (sep->se_service, s);
1613 while ((errno = 0, read (s, buffer, sizeof (buffer)) > 0) ||
1618 /* Discard service -- ignore data */
1621 discard_dg (int s, servtab_t *sep __attribute__((unused)))
1623 char buffer[BUFSIZE];
1625 (void) read (s, buffer, sizeof (buffer));
1627 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DISCARD */
1630 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN
1632 static char ring[128];
1633 static char *endring;
1642 for (i = 0; i <= 128; ++i)
1647 /* Character generator */
1650 chargen_stream (int s, servtab_t *sep)
1654 char text[LINESIZ + 2];
1656 inetd_setproctitle (sep->se_service, s);
1663 text[LINESIZ] = '\r';
1664 text[LINESIZ + 1] = '\n';
1666 if ((len = endring - rs) >= LINESIZ)
1667 memmove (text, rs, LINESIZ);
1669 memmove (text, rs, len);
1670 memmove (text + len, ring, LINESIZ - len);
1672 if (++rs == endring)
1674 if (write (s, text, sizeof (text)) != sizeof (text))
1680 /* Character generator */
1683 chargen_dg (int s, servtab_t *sep __attribute__((unused)))
1685 /* struct sockaddr_storage ss; */
1689 char text[LINESIZ + 2];
1698 if (recvfrom (s, text, sizeof (text), 0, &sa, &size) < 0)
1700 if (dg_badinput ((struct sockaddr_in *) &sa))
1703 if ((len = endring - rs) >= LINESIZ)
1704 memmove (text, rs, LINESIZ);
1706 memmove (text, rs, len);
1707 memmove (text + len, ring, LINESIZ - len);
1709 if (++rs == endring)
1711 text[LINESIZ] = '\r';
1712 text[LINESIZ + 1] = '\n';
1713 (void) sendto (s, text, sizeof (text), 0, &sa, sizeof (sa));
1715 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_CHARGEN */
1718 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME
1720 * Return a machine readable date and time, in the form of the
1721 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday
1722 * returns the number of seconds since midnight, Jan 1, 1970,
1723 * we must add 2208988800 seconds to this figure to make up for
1724 * some seventy years Bell Labs was asleep.
1727 static u_int machtime (void)
1731 if (gettimeofday (&tv, NULL) < 0) {
1732 fprintf (stderr, "Unable to get time of day\n");
1735 return (htonl ((u_int) tv.tv_sec + 2208988800UL));
1740 machtime_stream (int s, servtab_t *sep __attribute__((unused)))
1744 result = machtime ();
1745 (void) write (s, (char *) &result, sizeof (result));
1750 machtime_dg (int s, servtab_t *sep __attribute__((unused)))
1753 /* struct sockaddr_storage ss; */
1755 struct sockaddr_in *dg_sin;
1759 if (recvfrom (s, (char *) &result, sizeof (result), 0, &sa, &size) < 0)
1761 /* if (dg_badinput((struct sockaddr *)&ss)) */
1762 dg_sin = (struct sockaddr_in *) &sa;
1763 if (dg_sin->sin_addr.s_addr == htonl (INADDR_BROADCAST) ||
1764 ntohs (dg_sin->sin_port) < IPPORT_RESERVED / 2)
1766 result = machtime ();
1767 (void) sendto (s, (char *) &result, sizeof (result), 0, &sa, sizeof (sa));
1769 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_TIME */
1772 #ifdef CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME
1773 /* Return human-readable time of day */
1775 static void daytime_stream (int s, servtab_t *sep __attribute__((unused)))
1782 (void) sprintf (buffer, "%.24s\r\n", ctime (&t));
1783 (void) write (s, buffer, strlen (buffer));
1786 /* Return human-readable time of day */
1789 daytime_dg (int s, servtab_t *sep __attribute__((unused)))
1793 /* struct sockaddr_storage ss; */
1797 t = time ((time_t *) 0);
1800 if (recvfrom (s, buffer, sizeof (buffer), 0, &sa, &size) < 0)
1802 if (dg_badinput ((struct sockaddr_in *) &sa))
1804 (void) sprintf (buffer, "%.24s\r\n", ctime (&t));
1805 (void) sendto (s, buffer, strlen (buffer), 0, &sa, sizeof (sa));
1807 #endif /* CONFIG_FEATURE_INETD_SUPPORT_BILTIN_DAYTIME */