"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 2 | /* $Slackware: inetd.c 1.79s 2001/02/06 13:18:00 volkerdi Exp $ */ |
| 3 | /* $OpenBSD: inetd.c,v 1.79 2001/01/30 08:30:57 deraadt Exp $ */ |
| 4 | /* $NetBSD: inetd.c,v 1.11 1996/02/22 11:14:41 mycroft Exp $ */ |
| 5 | /* Busybox port by Vladimir Oleynik (C) 2001-2005 <dzo@simtreas.ru> */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 6 | /* IPv6 support, many bug fixes by Denys Vlasenko (c) 2008 */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 7 | /* |
| 8 | * Copyright (c) 1983,1991 The Regents of the University of California. |
| 9 | * All rights reserved. |
| 10 | * |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * 1. Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in the |
| 18 | * documentation and/or other materials provided with the distribution. |
| 19 | * 3. All advertising materials mentioning features or use of this software |
| 20 | * must display the following acknowledgement: |
| 21 | * This product includes software developed by the University of |
| 22 | * California, Berkeley and its contributors. |
| 23 | * 4. Neither the name of the University nor the names of its contributors |
| 24 | * may be used to endorse or promote products derived from this software |
| 25 | * without specific prior written permission. |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 26 | * |
Denis Vlasenko | f8138d1 | 2007-01-11 23:26:13 +0000 | [diff] [blame] | 27 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 37 | * SUCH DAMAGE. |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
Denis Vlasenko | f8138d1 | 2007-01-11 23:26:13 +0000 | [diff] [blame] | 40 | /* Inetd - Internet super-server |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 41 | * |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 42 | * This program invokes configured services when a connection |
| 43 | * from a peer is established or a datagram arrives. |
| 44 | * Connection-oriented services are invoked each time a |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 45 | * connection is made, by creating a process. This process |
| 46 | * is passed the connection as file descriptor 0 and is |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 47 | * expected to do a getpeername to find out peer's host |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 48 | * and port. |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 49 | * Datagram oriented services are invoked when a datagram |
| 50 | * arrives; a process is created and passed a pending message |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 51 | * on file descriptor 0. peer's address can be obtained |
| 52 | * using recvfrom. |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 53 | * |
| 54 | * Inetd uses a configuration file which is read at startup |
| 55 | * and, possibly, at some later time in response to a hangup signal. |
Denis Vlasenko | f8138d1 | 2007-01-11 23:26:13 +0000 | [diff] [blame] | 56 | * The configuration file is "free format" with fields given in the |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 57 | * order shown below. Continuation lines for an entry must begin with |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 58 | * a space or tab. All fields must be present in each entry. |
| 59 | * |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 60 | * service_name must be in /etc/services |
| 61 | * socket_type stream/dgram/raw/rdm/seqpacket |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 62 | * protocol must be in /etc/protocols |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 63 | * (usually "tcp" or "udp") |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 64 | * wait/nowait[.max] single-threaded/multi-threaded, max # |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 65 | * user[.group] or user[:group] user/group to run daemon as |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 66 | * server_program full path name |
| 67 | * server_program_arguments maximum of MAXARGS (20) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 68 | * |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 69 | * For RPC services |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 70 | * service_name/version must be in /etc/rpc |
| 71 | * socket_type stream/dgram/raw/rdm/seqpacket |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 72 | * rpc/protocol "rpc/tcp" etc |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 73 | * wait/nowait[.max] single-threaded/multi-threaded |
| 74 | * user[.group] or user[:group] user to run daemon as |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 75 | * server_program full path name |
| 76 | * server_program_arguments maximum of MAXARGS (20) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 77 | * |
| 78 | * For non-RPC services, the "service name" can be of the form |
| 79 | * hostaddress:servicename, in which case the hostaddress is used |
| 80 | * as the host portion of the address to listen on. If hostaddress |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 81 | * consists of a single '*' character, INADDR_ANY is used. |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 82 | * |
| 83 | * A line can also consist of just |
| 84 | * hostaddress: |
| 85 | * where hostaddress is as in the preceding paragraph. Such a line must |
| 86 | * have no further fields; the specified hostaddress is remembered and |
| 87 | * used for all further lines that have no hostaddress specified, |
| 88 | * until the next such line (or EOF). (This is why * is provided to |
| 89 | * allow explicit specification of INADDR_ANY.) A line |
| 90 | * *: |
| 91 | * is implicitly in effect at the beginning of the file. |
| 92 | * |
| 93 | * The hostaddress specifier may (and often will) contain dots; |
| 94 | * the service name must not. |
| 95 | * |
| 96 | * For RPC services, host-address specifiers are accepted and will |
| 97 | * work to some extent; however, because of limitations in the |
| 98 | * portmapper interface, it will not work to try to give more than |
| 99 | * one line for any given RPC service, even if the host-address |
| 100 | * specifiers are different. |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 101 | * |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 102 | * Comment lines are indicated by a '#' in column 1. |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 103 | */ |
| 104 | |
Denis Vlasenko | f8138d1 | 2007-01-11 23:26:13 +0000 | [diff] [blame] | 105 | /* inetd rules for passing file descriptors to children |
| 106 | * (http://www.freebsd.org/cgi/man.cgi?query=inetd): |
| 107 | * |
| 108 | * The wait/nowait entry specifies whether the server that is invoked by |
| 109 | * inetd will take over the socket associated with the service access point, |
| 110 | * and thus whether inetd should wait for the server to exit before listen- |
| 111 | * ing for new service requests. Datagram servers must use "wait", as |
| 112 | * they are always invoked with the original datagram socket bound to the |
| 113 | * specified service address. These servers must read at least one datagram |
| 114 | * from the socket before exiting. If a datagram server connects to its |
| 115 | * peer, freeing the socket so inetd can receive further messages on the |
| 116 | * socket, it is said to be a "multi-threaded" server; it should read one |
| 117 | * datagram from the socket and create a new socket connected to the peer. |
| 118 | * It should fork, and the parent should then exit to allow inetd to check |
| 119 | * for new service requests to spawn new servers. Datagram servers which |
| 120 | * process all incoming datagrams on a socket and eventually time out are |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 121 | * said to be "single-threaded". The comsat(8), biff(1) and talkd(8) |
Denis Vlasenko | f8138d1 | 2007-01-11 23:26:13 +0000 | [diff] [blame] | 122 | * utilities are both examples of the latter type of datagram server. The |
| 123 | * tftpd(8) utility is an example of a multi-threaded datagram server. |
| 124 | * |
| 125 | * Servers using stream sockets generally are multi-threaded and use the |
| 126 | * "nowait" entry. Connection requests for these services are accepted by |
| 127 | * inetd, and the server is given only the newly-accepted socket connected |
| 128 | * to a client of the service. Most stream-based services operate in this |
| 129 | * manner. Stream-based servers that use "wait" are started with the lis- |
| 130 | * tening service socket, and must accept at least one connection request |
| 131 | * before exiting. Such a server would normally accept and process incoming |
| 132 | * connection requests until a timeout. |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 133 | * |
| 134 | * In short: "stream" can be "wait" or "nowait"; "dgram" must be "wait". |
Denis Vlasenko | f8138d1 | 2007-01-11 23:26:13 +0000 | [diff] [blame] | 135 | */ |
| 136 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 137 | /* Here's the scoop concerning the user[:group] feature: |
| 138 | * 1) group is not specified: |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 139 | * a) user = root: NO setuid() or setgid() is done |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 140 | * b) other: setgid(primary group as found in passwd) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 141 | * initgroups(name, primary group) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 142 | * setuid() |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 143 | * 2) group is specified: |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 144 | * a) user = root: setgid(specified group) |
| 145 | * NO initgroups() |
| 146 | * NO setuid() |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 147 | * b) other: setgid(specified group) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 148 | * initgroups(name, specified group) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 149 | * setuid() |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 150 | */ |
| 151 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 152 | #include <syslog.h> |
Rob Landley | 099ed50 | 2006-08-28 09:41:49 +0000 | [diff] [blame] | 153 | #include <sys/un.h> |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 154 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 155 | #include "libbb.h" |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 156 | |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 157 | #if ENABLE_FEATURE_INETD_RPC |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 158 | #include <rpc/rpc.h> |
| 159 | #include <rpc/pmap_clnt.h> |
Glenn L McGrath | ff6ec8a | 2004-01-17 02:47:45 +0000 | [diff] [blame] | 160 | #endif |
| 161 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 162 | #if !BB_MMU |
| 163 | /* stream version of chargen is forking but not execing, |
| 164 | * can't do that (easily) on NOMMU */ |
| 165 | #undef ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN |
| 166 | #define ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN 0 |
| 167 | #endif |
| 168 | |
Denis Vlasenko | 5b34083 | 2007-05-17 23:02:14 +0000 | [diff] [blame] | 169 | #define _PATH_INETDPID "/var/run/inetd.pid" |
| 170 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 171 | #define CNT_INTERVAL 60 /* servers in CNT_INTERVAL sec. */ |
| 172 | #define RETRYTIME 60 /* retry after bind or server fail */ |
| 173 | |
| 174 | // TODO: explain, or get rid of setrlimit games |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 175 | |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 176 | #ifndef RLIMIT_NOFILE |
| 177 | #define RLIMIT_NOFILE RLIMIT_OFILE |
| 178 | #endif |
| 179 | |
| 180 | #ifndef OPEN_MAX |
| 181 | #define OPEN_MAX 64 |
| 182 | #endif |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 183 | |
| 184 | /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */ |
Denis Vlasenko | f8138d1 | 2007-01-11 23:26:13 +0000 | [diff] [blame] | 185 | #define FD_MARGIN 8 |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 186 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 187 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD \ |
| 188 | || ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO \ |
| 189 | || ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN \ |
| 190 | || ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME \ |
| 191 | || ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME |
| 192 | # define INETD_BUILTINS_ENABLED |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 193 | #endif |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 194 | |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 195 | typedef struct servtab_t { |
Denis Vlasenko | 3982407 | 2007-09-26 10:46:18 +0000 | [diff] [blame] | 196 | /* The most frequently referenced one: */ |
| 197 | int se_fd; /* open descriptor */ |
| 198 | /* NB: 'biggest fields last' saves on code size (~250 bytes) */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 199 | /* [addr:]service socktype proto wait user[:group] prog [args] */ |
| 200 | char *se_local_hostname; /* addr to listen on */ |
| 201 | char *se_service; /* "80" or "www" or "mount/2[-3]" */ |
| 202 | /* socktype is in se_socktype */ /* "stream" "dgram" "raw" "rdm" "seqpacket" */ |
| 203 | char *se_proto; /* "unix" or "[rpc/]tcp[6]" */ |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 204 | #if ENABLE_FEATURE_INETD_RPC |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 205 | int se_rpcprog; /* rpc program number */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 206 | int se_rpcver_lo; /* rpc program lowest version */ |
| 207 | int se_rpcver_hi; /* rpc program highest version */ |
| 208 | #define is_rpc_service(sep) ((sep)->se_rpcver_lo != 0) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 209 | #else |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 210 | #define is_rpc_service(sep) 0 |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 211 | #endif |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 212 | pid_t se_wait; /* 0:"nowait", 1:"wait", >1:"wait" */ |
| 213 | /* and waiting for this pid */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 214 | socktype_t se_socktype; /* SOCK_STREAM/DGRAM/RDM/... */ |
| 215 | family_t se_family; /* AF_UNIX/INET[6] */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 216 | /* se_proto_no is used by RPC code only... hmm */ |
| 217 | smallint se_proto_no; /* IPPROTO_TCP/UDP, n/a for AF_UNIX */ |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 218 | smallint se_checked; /* looked at during merge */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 219 | unsigned se_max; /* allowed instances per minute */ |
| 220 | unsigned se_count; /* number started since se_time */ |
| 221 | unsigned se_time; /* whem we started counting */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 222 | char *se_user; /* user name to run as */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 223 | char *se_group; /* group name to run as, can be NULL */ |
| 224 | #ifdef INETD_BUILTINS_ENABLED |
| 225 | const struct builtin *se_builtin; /* if built-in, description */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 226 | #endif |
Denis Vlasenko | 3982407 | 2007-09-26 10:46:18 +0000 | [diff] [blame] | 227 | struct servtab_t *se_next; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 228 | len_and_sockaddr *se_lsa; |
| 229 | char *se_program; /* server program */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 230 | #define MAXARGV 20 |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 231 | char *se_argv[MAXARGV + 1]; /* program arguments */ |
Glenn L McGrath | 03a0643 | 2004-02-18 13:19:58 +0000 | [diff] [blame] | 232 | } servtab_t; |
| 233 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 234 | #ifdef INETD_BUILTINS_ENABLED |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 235 | /* Echo received data */ |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 236 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 237 | static void echo_stream(int, servtab_t *); |
| 238 | static void echo_dg(int, servtab_t *); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 239 | #endif |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 240 | /* Internet /dev/null */ |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 241 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 242 | static void discard_stream(int, servtab_t *); |
| 243 | static void discard_dg(int, servtab_t *); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 244 | #endif |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 245 | /* Return 32 bit time since 1900 */ |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 246 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 247 | static void machtime_stream(int, servtab_t *); |
| 248 | static void machtime_dg(int, servtab_t *); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 249 | #endif |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 250 | /* Return human-readable time */ |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 251 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 252 | static void daytime_stream(int, servtab_t *); |
| 253 | static void daytime_dg(int, servtab_t *); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 254 | #endif |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 255 | /* Familiar character generator */ |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 256 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 257 | static void chargen_stream(int, servtab_t *); |
| 258 | static void chargen_dg(int, servtab_t *); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 259 | #endif |
| 260 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 261 | struct builtin { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 262 | /* NB: not necessarily NUL terminated */ |
| 263 | char bi_service7[7]; /* internally provided service name */ |
| 264 | uint8_t bi_fork; /* 1 if stream fn should run in child */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 265 | void (*bi_stream_fn)(int, servtab_t *); |
| 266 | void (*bi_dgram_fn)(int, servtab_t *); |
| 267 | }; |
| 268 | |
Bernhard Reutner-Fischer | a4acf66 | 2006-04-10 12:26:47 +0000 | [diff] [blame] | 269 | static const struct builtin builtins[] = { |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 270 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 271 | { "echo", 1, echo_stream, echo_dg }, |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 272 | #endif |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 273 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 274 | { "discard", 1, discard_stream, discard_dg }, |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 275 | #endif |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 276 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 277 | { "chargen", 1, chargen_stream, chargen_dg }, |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 278 | #endif |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 279 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 280 | { "time", 0, machtime_stream, machtime_dg }, |
| 281 | #endif |
| 282 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 283 | { "daytime", 0, daytime_stream, daytime_dg }, |
| 284 | #endif |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 285 | }; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 286 | #endif /* INETD_BUILTINS_ENABLED */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 287 | |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 288 | struct globals { |
| 289 | rlim_t rlim_ofile_cur; |
| 290 | struct rlimit rlim_ofile; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 291 | servtab_t *serv_list; |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 292 | int global_queuelen; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 293 | int prev_maxsock; |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 294 | int maxsock; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 295 | unsigned max_concurrency; |
| 296 | smallint alarm_armed; |
| 297 | uid_t real_uid; /* user ID who ran us */ |
| 298 | unsigned config_lineno; |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 299 | const char *config_filename; |
| 300 | FILE *fconfig; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 301 | char *default_local_hostname; |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 302 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 303 | char *end_ring; |
| 304 | char *ring_pos; |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 305 | char ring[128]; |
| 306 | #endif |
| 307 | fd_set allsock; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 308 | /* Used in next_line(), and as scratch read buffer */ |
| 309 | char line[256]; /* _at least_ 256, see LINE_SIZE */ |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 310 | }; |
| 311 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 312 | enum { LINE_SIZE = COMMON_BUFSIZE - offsetof(struct globals, line) }; |
| 313 | struct BUG_G_too_big { |
| 314 | char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1]; |
| 315 | }; |
| 316 | #define rlim_ofile_cur (G.rlim_ofile_cur ) |
| 317 | #define rlim_ofile (G.rlim_ofile ) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 318 | #define serv_list (G.serv_list ) |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 319 | #define global_queuelen (G.global_queuelen) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 320 | #define prev_maxsock (G.prev_maxsock ) |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 321 | #define maxsock (G.maxsock ) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 322 | #define max_concurrency (G.max_concurrency) |
| 323 | #define alarm_armed (G.alarm_armed ) |
| 324 | #define real_uid (G.real_uid ) |
| 325 | #define config_lineno (G.config_lineno ) |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 326 | #define config_filename (G.config_filename) |
| 327 | #define fconfig (G.fconfig ) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 328 | #define default_local_hostname (G.default_local_hostname) |
| 329 | #define first_ps_byte (G.first_ps_byte ) |
| 330 | #define last_ps_byte (G.last_ps_byte ) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 331 | #define end_ring (G.end_ring ) |
| 332 | #define ring_pos (G.ring_pos ) |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 333 | #define ring (G.ring ) |
| 334 | #define allsock (G.allsock ) |
| 335 | #define line (G.line ) |
| 336 | #define INIT_G() do { \ |
| 337 | rlim_ofile_cur = OPEN_MAX; \ |
| 338 | global_queuelen = 128; \ |
| 339 | config_filename = "/etc/inetd.conf"; \ |
| 340 | } while (0) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 341 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 342 | static void maybe_close(int fd) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 343 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 344 | if (fd >= 0) |
| 345 | close(fd); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 348 | // TODO: move to libbb? |
| 349 | static len_and_sockaddr *xzalloc_lsa(int family) |
Glenn L McGrath | df7d84c | 2004-02-22 11:25:13 +0000 | [diff] [blame] | 350 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 351 | len_and_sockaddr *lsa; |
| 352 | int sz; |
| 353 | |
| 354 | sz = sizeof(struct sockaddr_in); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 355 | if (family == AF_UNIX) |
| 356 | sz = sizeof(struct sockaddr_un); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 357 | #if ENABLE_FEATURE_IPV6 |
| 358 | if (family == AF_INET6) |
| 359 | sz = sizeof(struct sockaddr_in6); |
| 360 | #endif |
| 361 | lsa = xzalloc(LSA_LEN_SIZE + sz); |
| 362 | lsa->len = sz; |
| 363 | lsa->u.sa.sa_family = family; |
| 364 | return lsa; |
Glenn L McGrath | df7d84c | 2004-02-22 11:25:13 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 367 | static void rearm_alarm(void) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 368 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 369 | if (!alarm_armed) { |
| 370 | alarm_armed = 1; |
| 371 | alarm(RETRYTIME); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 372 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void block_CHLD_HUP_ALRM(sigset_t *m) |
| 376 | { |
| 377 | sigemptyset(m); |
| 378 | sigaddset(m, SIGCHLD); |
| 379 | sigaddset(m, SIGHUP); |
| 380 | sigaddset(m, SIGALRM); |
| 381 | sigprocmask(SIG_BLOCK, m, NULL); |
| 382 | } |
| 383 | |
| 384 | static void unblock_sigs(sigset_t *m) |
| 385 | { |
| 386 | sigprocmask(SIG_UNBLOCK, m, NULL); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 387 | } |
Glenn L McGrath | 53766c4 | 2004-01-18 08:58:06 +0000 | [diff] [blame] | 388 | |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 389 | #if ENABLE_FEATURE_INETD_RPC |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 390 | static void register_rpc(servtab_t *sep) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 391 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 392 | int n; |
| 393 | struct sockaddr_in ir_sin; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 394 | socklen_t size; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 395 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 396 | size = sizeof(ir_sin); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 397 | if (getsockname(sep->se_fd, (struct sockaddr *) &ir_sin, &size) < 0) { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 398 | bb_perror_msg("getsockname"); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 399 | return; |
| 400 | } |
Glenn L McGrath | eaf5bc0 | 2004-01-20 15:32:39 +0000 | [diff] [blame] | 401 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 402 | for (n = sep->se_rpcver_lo; n <= sep->se_rpcver_hi; n++) { |
| 403 | pmap_unset(sep->se_rpcprog, n); |
| 404 | if (!pmap_set(sep->se_rpcprog, n, sep->se_proto_no, ntohs(ir_sin.sin_port))) |
| 405 | bb_perror_msg("%s %s: pmap_set(%u,%u,%u,%u)", |
| 406 | sep->se_service, sep->se_proto, |
| 407 | sep->se_rpcprog, n, sep->se_proto_no, ntohs(ir_sin.sin_port)); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 408 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 409 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 410 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 411 | static void unregister_rpc(servtab_t *sep) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 412 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 413 | int n; |
Glenn L McGrath | eaf5bc0 | 2004-01-20 15:32:39 +0000 | [diff] [blame] | 414 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 415 | for (n = sep->se_rpcver_lo; n <= sep->se_rpcver_hi; n++) { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 416 | if (!pmap_unset(sep->se_rpcprog, n)) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 417 | bb_perror_msg("pmap_unset(%u,%u)", sep->se_rpcprog, n); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 418 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 419 | } |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 420 | #endif /* FEATURE_INETD_RPC */ |
Glenn L McGrath | eaf5bc0 | 2004-01-20 15:32:39 +0000 | [diff] [blame] | 421 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 422 | static void bump_nofile(void) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 423 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 424 | enum { FD_CHUNK = 32 }; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 425 | struct rlimit rl; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 426 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 427 | /* Never fails under Linux (except if you pass it bad arguments) */ |
| 428 | getrlimit(RLIMIT_NOFILE, &rl); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 429 | rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK); |
| 430 | rl.rlim_cur = MIN(FD_SETSIZE, rl.rlim_cur + FD_CHUNK); |
| 431 | if (rl.rlim_cur <= rlim_ofile_cur) { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 432 | bb_error_msg("can't extend file limit, max = %d", |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 433 | (int) rl.rlim_cur); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 434 | return; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 435 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 436 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 437 | if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { |
| 438 | bb_perror_msg("setrlimit"); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 439 | return; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 440 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 441 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 442 | rlim_ofile_cur = rl.rlim_cur; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 445 | static void remove_fd_from_set(int fd) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 446 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 447 | if (fd >= 0) { |
| 448 | FD_CLR(fd, &allsock); |
| 449 | maxsock = -1; |
| 450 | } |
| 451 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 452 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 453 | static void add_fd_to_set(int fd) |
| 454 | { |
| 455 | if (fd >= 0) { |
| 456 | FD_SET(fd, &allsock); |
| 457 | if (maxsock >= 0 && fd > maxsock) { |
| 458 | prev_maxsock = maxsock = fd; |
| 459 | if ((rlim_t)maxsock > rlim_ofile_cur - FD_MARGIN) |
| 460 | bump_nofile(); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | static void recalculate_maxsock(void) |
| 466 | { |
| 467 | int fd = 0; |
| 468 | while (fd <= prev_maxsock) { |
| 469 | if (FD_ISSET(fd, &allsock)) |
| 470 | maxsock = fd; |
| 471 | fd++; |
| 472 | } |
| 473 | prev_maxsock = maxsock; |
| 474 | if ((rlim_t)maxsock > rlim_ofile_cur - FD_MARGIN) |
| 475 | bump_nofile(); |
| 476 | } |
| 477 | |
| 478 | static void prepare_socket_fd(servtab_t *sep) |
| 479 | { |
| 480 | int r, fd; |
| 481 | |
| 482 | fd = socket(sep->se_family, sep->se_socktype, 0); |
| 483 | if (fd < 0) { |
| 484 | bb_perror_msg("socket"); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 485 | return; |
| 486 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 487 | setsockopt_reuseaddr(fd); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 488 | |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 489 | #if ENABLE_FEATURE_INETD_RPC |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 490 | if (is_rpc_service(sep)) { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 491 | struct passwd *pwd; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 492 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 493 | /* zero out the port for all RPC services; let bind() |
| 494 | * find one. */ |
| 495 | set_nport(sep->se_lsa, 0); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 496 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 497 | /* for RPC services, attempt to use a reserved port |
| 498 | * if they are going to be running as root. */ |
| 499 | if (real_uid == 0 && sep->se_family == AF_INET |
| 500 | && (pwd = getpwnam(sep->se_user)) != NULL |
| 501 | && pwd->pw_uid == 0 |
| 502 | ) { |
| 503 | r = bindresvport(fd, &sep->se_lsa->u.sin); |
| 504 | } else { |
| 505 | r = bind(fd, &sep->se_lsa->u.sa, sep->se_lsa->len); |
| 506 | } |
| 507 | if (r == 0) { |
| 508 | int saveerrno = errno; |
| 509 | /* update lsa with port# */ |
| 510 | getsockname(fd, &sep->se_lsa->u.sa, &sep->se_lsa->len); |
| 511 | errno = saveerrno; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 512 | } |
| 513 | } else |
Glenn L McGrath | 53766c4 | 2004-01-18 08:58:06 +0000 | [diff] [blame] | 514 | #endif |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 515 | { |
| 516 | if (sep->se_family == AF_UNIX) { |
| 517 | struct sockaddr_un *sun; |
| 518 | sun = (struct sockaddr_un*)&(sep->se_lsa->u.sa); |
| 519 | unlink(sun->sun_path); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 520 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 521 | r = bind(fd, &sep->se_lsa->u.sa, sep->se_lsa->len); |
| 522 | } |
| 523 | if (r < 0) { |
| 524 | bb_perror_msg("%s/%s: bind", |
| 525 | sep->se_service, sep->se_proto); |
| 526 | close(fd); |
| 527 | rearm_alarm(); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 528 | return; |
Glenn L McGrath | 53766c4 | 2004-01-18 08:58:06 +0000 | [diff] [blame] | 529 | } |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 530 | if (sep->se_socktype == SOCK_STREAM) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 531 | listen(fd, global_queuelen); |
Glenn L McGrath | 53766c4 | 2004-01-18 08:58:06 +0000 | [diff] [blame] | 532 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 533 | add_fd_to_set(fd); |
| 534 | sep->se_fd = fd; |
| 535 | } |
| 536 | |
| 537 | static int reopen_config_file(void) |
| 538 | { |
| 539 | free(default_local_hostname); |
| 540 | default_local_hostname = xstrdup("*"); |
| 541 | if (fconfig != NULL) |
| 542 | fclose(fconfig); |
| 543 | config_lineno = 0; |
| 544 | fconfig = fopen_or_warn(config_filename, "r"); |
| 545 | return (fconfig != NULL); |
| 546 | } |
| 547 | |
| 548 | static void close_config_file(void) |
| 549 | { |
| 550 | if (fconfig) { |
| 551 | fclose(fconfig); |
| 552 | fconfig = NULL; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 553 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 556 | static char *next_line(void) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 557 | { |
Denis Vlasenko | 6df9e3c | 2007-11-12 21:21:35 +0000 | [diff] [blame] | 558 | if (fgets(line, LINE_SIZE, fconfig) == NULL) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 559 | return NULL; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 560 | config_lineno++; |
Denis Vlasenko | c03e872 | 2007-12-26 20:56:55 +0000 | [diff] [blame] | 561 | *strchrnul(line, '\n') = '\0'; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 562 | return line; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 565 | static char *next_word(char **cpp) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 566 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 567 | char *start; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 568 | char *cp = *cpp; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 569 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 570 | if (cp == NULL) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 571 | return NULL; |
Denis Vlasenko | ce074df | 2007-03-24 12:07:31 +0000 | [diff] [blame] | 572 | again: |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 573 | while (*cp == ' ' || *cp == '\t') |
| 574 | cp++; |
| 575 | if (*cp == '\0') { |
Denis Vlasenko | 6df9e3c | 2007-11-12 21:21:35 +0000 | [diff] [blame] | 576 | int c = getc(fconfig); |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 577 | ungetc(c, fconfig); |
| 578 | if (c == ' ' || c == '\t') { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 579 | cp = next_line(); |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 580 | if (cp) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 581 | goto again; |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 582 | } |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 583 | *cpp = NULL; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 584 | return NULL; |
| 585 | } |
| 586 | start = cp; |
| 587 | while (*cp && *cp != ' ' && *cp != '\t') |
| 588 | cp++; |
| 589 | if (*cp != '\0') |
| 590 | *cp++ = '\0'; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 591 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 592 | *cpp = cp; |
| 593 | return start; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 596 | static void free_servtab_strings(servtab_t *cp) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 597 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 598 | int i; |
| 599 | |
| 600 | free(cp->se_local_hostname); |
| 601 | free(cp->se_service); |
| 602 | free(cp->se_proto); |
| 603 | free(cp->se_user); |
| 604 | free(cp->se_group); |
| 605 | free(cp->se_lsa); /* not a string in fact */ |
| 606 | free(cp->se_program); |
| 607 | for (i = 0; i < MAXARGV; i++) |
| 608 | free(cp->se_argv[i]); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 611 | static servtab_t *new_servtab(void) |
| 612 | { |
| 613 | servtab_t *newtab = xzalloc(sizeof(servtab_t)); |
| 614 | newtab->se_fd = -1; /* paranoia */ |
| 615 | return newtab; |
| 616 | } |
| 617 | |
| 618 | static servtab_t *dup_servtab(servtab_t *sep) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 619 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 620 | servtab_t *newtab; |
| 621 | int argc; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 622 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 623 | newtab = new_servtab(); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 624 | *newtab = *sep; /* struct copy */ |
| 625 | /* deep-copying strings */ |
| 626 | newtab->se_service = xstrdup(newtab->se_service); |
| 627 | newtab->se_proto = xstrdup(newtab->se_proto); |
| 628 | newtab->se_user = xstrdup(newtab->se_user); |
| 629 | newtab->se_group = xstrdup(newtab->se_group); |
| 630 | newtab->se_program = xstrdup(newtab->se_program); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 631 | for (argc = 0; argc <= MAXARGV; argc++) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 632 | newtab->se_argv[argc] = xstrdup(newtab->se_argv[argc]); |
| 633 | /* NB: se_fd, se_hostaddr and se_next are always |
| 634 | * overwrittend by callers, so we don't bother resetting them |
| 635 | * to NULL/0/-1 etc */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 636 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 637 | return newtab; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 640 | /* gcc generates much more code if this is inlined */ |
| 641 | static NOINLINE servtab_t *parse_one_line(void) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 642 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 643 | int argc; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 644 | char *p, *cp, *arg; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 645 | char *hostdelim; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 646 | servtab_t *sep; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 647 | servtab_t *nsep; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 648 | new: |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 649 | sep = new_servtab(); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 650 | more: |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 651 | while ((cp = next_line()) && *cp == '#') |
Denis Vlasenko | 6df9e3c | 2007-11-12 21:21:35 +0000 | [diff] [blame] | 652 | continue; /* skip comment lines */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 653 | if (cp == NULL) { |
Denis Vlasenko | 6df9e3c | 2007-11-12 21:21:35 +0000 | [diff] [blame] | 654 | free(sep); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 655 | return NULL; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 656 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 657 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 658 | arg = next_word(&cp); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 659 | if (arg == NULL) /* a blank line. */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 660 | goto more; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 661 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 662 | /* [host:]service socktype proto wait user[:group] prog [args] */ |
| 663 | /* Check for "host:...." line */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 664 | hostdelim = strrchr(arg, ':'); |
| 665 | if (hostdelim) { |
| 666 | *hostdelim = '\0'; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 667 | sep->se_local_hostname = xstrdup(arg); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 668 | arg = hostdelim + 1; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 669 | if (*arg == '\0') { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 670 | arg = next_word(&cp); |
| 671 | if (arg == NULL) { |
| 672 | /* Line has just "host:", change the |
| 673 | * default host for the following lines. */ |
| 674 | free(default_local_hostname); |
| 675 | default_local_hostname = sep->se_local_hostname; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 676 | goto more; |
| 677 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 678 | } |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 679 | } else |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 680 | sep->se_local_hostname = xstrdup(default_local_hostname); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 681 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 682 | /* service socktype proto wait user[:group] prog [args] */ |
| 683 | sep->se_service = xstrdup(arg); |
| 684 | /* socktype proto wait user[:group] prog [args] */ |
| 685 | arg = next_word(&cp); |
| 686 | if (arg == NULL) { |
| 687 | parse_err: |
| 688 | bb_error_msg("parse error on line %u, line is ignored", |
| 689 | config_lineno); |
| 690 | free_servtab_strings(sep); |
| 691 | /* Just "goto more" can make sep to carry over e.g. |
| 692 | * "rpc"-ness (by having se_rpcver_lo != 0). |
| 693 | * We will be more paranoid: */ |
| 694 | free(sep); |
| 695 | goto new; |
| 696 | } |
Denis Vlasenko | 9d1afdb | 2007-10-30 19:54:39 +0000 | [diff] [blame] | 697 | { |
| 698 | static int8_t SOCK_xxx[] ALIGN1 = { |
| 699 | -1, |
| 700 | SOCK_STREAM, SOCK_DGRAM, SOCK_RDM, |
| 701 | SOCK_SEQPACKET, SOCK_RAW |
| 702 | }; |
| 703 | sep->se_socktype = SOCK_xxx[1 + index_in_strings( |
| 704 | "stream""\0" "dgram""\0" "rdm""\0" |
| 705 | "seqpacket""\0" "raw""\0" |
| 706 | , arg)]; |
| 707 | } |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 708 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 709 | /* {unix,[rpc/]{tcp,udp}[6]} wait user[:group] prog [args] */ |
| 710 | sep->se_proto = arg = xstrdup(next_word(&cp)); |
| 711 | if (arg == NULL) |
| 712 | goto parse_err; |
| 713 | if (strcmp(arg, "unix") == 0) { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 714 | sep->se_family = AF_UNIX; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 715 | } else { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 716 | char *six; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 717 | sep->se_family = AF_INET; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 718 | six = last_char_is(arg, '6'); |
| 719 | if (six) { |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 720 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 721 | *six = '\0'; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 722 | sep->se_family = AF_INET6; |
| 723 | #else |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 724 | bb_error_msg("%s: no support for IPv6", sep->se_proto); |
| 725 | goto parse_err; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 726 | #endif |
| 727 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 728 | if (strncmp(arg, "rpc/", 4) == 0) { |
| 729 | #if ENABLE_FEATURE_INETD_RPC |
| 730 | unsigned n; |
| 731 | arg += 4; |
| 732 | p = strchr(sep->se_service, '/'); |
| 733 | if (p == NULL) { |
| 734 | bb_error_msg("no rpc version: '%s'", sep->se_service); |
| 735 | goto parse_err; |
| 736 | } |
| 737 | *p++ = '\0'; |
| 738 | n = bb_strtou(p, &p, 10); |
| 739 | if (n > INT_MAX) { |
| 740 | bad_ver_spec: |
| 741 | bb_error_msg("bad rpc version"); |
| 742 | goto parse_err; |
| 743 | } |
| 744 | sep->se_rpcver_lo = sep->se_rpcver_hi = n; |
| 745 | if (*p == '-') { |
| 746 | p++; |
| 747 | n = bb_strtou(p, &p, 10); |
| 748 | if (n > INT_MAX || n < sep->se_rpcver_lo) |
| 749 | goto bad_ver_spec; |
| 750 | sep->se_rpcver_hi = n; |
| 751 | } |
| 752 | if (*p != '\0') |
| 753 | goto bad_ver_spec; |
| 754 | #else |
| 755 | bb_error_msg("no support for rpc services"); |
| 756 | goto parse_err; |
| 757 | #endif |
| 758 | } |
| 759 | /* we don't really need getprotobyname()! */ |
| 760 | if (strcmp(arg, "tcp") == 0) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 761 | sep->se_proto_no = IPPROTO_TCP; /* = 6 */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 762 | if (strcmp(arg, "udp") == 0) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 763 | sep->se_proto_no = IPPROTO_UDP; /* = 17 */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 764 | if (six) |
| 765 | *six = '6'; |
| 766 | if (!sep->se_proto_no) /* not tcp/udp?? */ |
| 767 | goto parse_err; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 768 | } |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 769 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 770 | /* [no]wait[.max] user[:group] prog [args] */ |
| 771 | arg = next_word(&cp); |
| 772 | if (arg == NULL) |
| 773 | goto parse_err; |
| 774 | sep->se_max = max_concurrency; |
| 775 | p = strchr(arg, '.'); |
| 776 | if (p) { |
| 777 | *p++ = '\0'; |
| 778 | sep->se_max = bb_strtou(p, NULL, 10); |
| 779 | if (errno) |
| 780 | goto parse_err; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 781 | } |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 782 | sep->se_wait = (arg[0] != 'n' || arg[1] != 'o'); |
| 783 | if (!sep->se_wait) /* "no" seen */ |
| 784 | arg += 2; |
| 785 | if (strcmp(arg, "wait") != 0) |
| 786 | goto parse_err; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 787 | |
| 788 | /* user[:group] prog [args] */ |
| 789 | sep->se_user = xstrdup(next_word(&cp)); |
| 790 | if (sep->se_user == NULL) |
| 791 | goto parse_err; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 792 | arg = strchr(sep->se_user, '.'); |
| 793 | if (arg == NULL) |
| 794 | arg = strchr(sep->se_user, ':'); |
| 795 | if (arg) { |
| 796 | *arg++ = '\0'; |
| 797 | sep->se_group = xstrdup(arg); |
| 798 | } |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 799 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 800 | /* prog [args] */ |
| 801 | sep->se_program = xstrdup(next_word(&cp)); |
| 802 | if (sep->se_program == NULL) |
| 803 | goto parse_err; |
| 804 | #ifdef INETD_BUILTINS_ENABLED |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 805 | if (strcmp(sep->se_program, "internal") == 0 |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 806 | && strlen(sep->se_service) <= 7 |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 807 | && (sep->se_socktype == SOCK_STREAM |
| 808 | || sep->se_socktype == SOCK_DGRAM) |
| 809 | ) { |
| 810 | int i; |
| 811 | for (i = 0; i < ARRAY_SIZE(builtins); i++) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 812 | if (strncmp(builtins[i].bi_service7, sep->se_service, 7) == 0) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 813 | goto found_bi; |
| 814 | bb_error_msg("unknown internal service %s", sep->se_service); |
| 815 | goto parse_err; |
| 816 | found_bi: |
| 817 | sep->se_builtin = &builtins[i]; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 818 | /* stream builtins must be "nowait", dgram must be "wait" */ |
| 819 | if (sep->se_wait != (sep->se_socktype == SOCK_DGRAM)) |
| 820 | goto parse_err; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 821 | } |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 822 | #endif |
| 823 | argc = 0; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 824 | while ((arg = next_word(&cp)) != NULL && argc < MAXARGV) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 825 | sep->se_argv[argc++] = xstrdup(arg); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 826 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 827 | /* catch mixups. "<service> stream udp ..." == wtf */ |
| 828 | if (sep->se_socktype == SOCK_STREAM) { |
| 829 | if (sep->se_proto_no == IPPROTO_UDP) |
| 830 | goto parse_err; |
| 831 | } |
| 832 | if (sep->se_socktype == SOCK_DGRAM) { |
| 833 | if (sep->se_proto_no == IPPROTO_TCP) |
| 834 | goto parse_err; |
| 835 | /* "udp nowait" is a small fork bomb :) */ |
| 836 | if (!sep->se_wait) |
| 837 | goto parse_err; |
| 838 | } |
| 839 | |
| 840 | /* check if the hostname specifier is a comma separated list |
| 841 | * of hostnames. we'll make new entries for each address. */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 842 | while ((hostdelim = strrchr(sep->se_local_hostname, ',')) != NULL) { |
| 843 | nsep = dup_servtab(sep); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 844 | /* NUL terminate the hostname field of the existing entry, |
| 845 | * and make a dup for the new entry. */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 846 | *hostdelim++ = '\0'; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 847 | nsep->se_local_hostname = xstrdup(hostdelim); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 848 | nsep->se_next = sep->se_next; |
| 849 | sep->se_next = nsep; |
| 850 | } |
| 851 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 852 | /* was doing it here: */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 853 | /* DNS resolution, create copies for each IP address */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 854 | /* IPv6-ization destroyed it :( */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 855 | |
| 856 | return sep; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 859 | static servtab_t *insert_in_servlist(servtab_t *cp) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 860 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 861 | servtab_t *sep; |
| 862 | sigset_t omask; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 863 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 864 | sep = new_servtab(); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 865 | *sep = *cp; /* struct copy */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 866 | sep->se_fd = -1; |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 867 | #if ENABLE_FEATURE_INETD_RPC |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 868 | sep->se_rpcprog = -1; |
| 869 | #endif |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 870 | block_CHLD_HUP_ALRM(&omask); |
| 871 | sep->se_next = serv_list; |
| 872 | serv_list = sep; |
| 873 | unblock_sigs(&omask); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 874 | return sep; |
| 875 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 876 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 877 | static int same_serv_addr_proto(servtab_t *old, servtab_t *new) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 878 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 879 | if (strcmp(old->se_local_hostname, new->se_local_hostname) != 0) |
| 880 | return 0; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 881 | if (strcmp(old->se_service, new->se_service) != 0) |
| 882 | return 0; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 883 | if (strcmp(old->se_proto, new->se_proto) != 0) |
| 884 | return 0; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 885 | return 1; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 888 | static void reread_config_file(int sig ATTRIBUTE_UNUSED) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 889 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 890 | servtab_t *sep, *cp, **sepp; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 891 | len_and_sockaddr *lsa; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 892 | sigset_t omask; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 893 | unsigned n; |
| 894 | uint16_t port; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 895 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 896 | if (!reopen_config_file()) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 897 | return; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 898 | for (sep = serv_list; sep; sep = sep->se_next) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 899 | sep->se_checked = 0; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 900 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 901 | goto first_line; |
| 902 | while (1) { |
| 903 | if (cp == NULL) { |
| 904 | first_line: |
| 905 | cp = parse_one_line(); |
| 906 | if (cp == NULL) |
| 907 | break; |
| 908 | } |
| 909 | for (sep = serv_list; sep; sep = sep->se_next) |
| 910 | if (same_serv_addr_proto(sep, cp)) |
| 911 | goto equal_servtab; |
| 912 | /* not an "equal" servtab */ |
| 913 | sep = insert_in_servlist(cp); |
| 914 | goto after_check; |
| 915 | equal_servtab: |
| 916 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 917 | int i; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 918 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 919 | block_CHLD_HUP_ALRM(&omask); |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 920 | #if ENABLE_FEATURE_INETD_RPC |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 921 | if (is_rpc_service(sep)) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 922 | unregister_rpc(sep); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 923 | sep->se_rpcver_lo = cp->se_rpcver_lo; |
| 924 | sep->se_rpcver_hi = cp->se_rpcver_hi; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 925 | #endif |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 926 | if (cp->se_wait == 0) { |
| 927 | /* New config says "nowait". If old one |
| 928 | * was "wait", we currently may be waiting |
| 929 | * for a child (and not accepting connects). |
| 930 | * Stop waiting, start listening again. |
| 931 | * (if it's not true, this op is harmless) */ |
| 932 | add_fd_to_set(sep->se_fd); |
| 933 | } |
| 934 | sep->se_wait = cp->se_wait; |
| 935 | sep->se_max = cp->se_max; |
| 936 | /* string fields need more love - we don't want to leak them */ |
| 937 | #define SWAP(type, a, b) do { type c = (type)a; a = (type)b; b = (type)c; } while (0) |
| 938 | SWAP(char*, sep->se_user, cp->se_user); |
| 939 | SWAP(char*, sep->se_group, cp->se_group); |
| 940 | SWAP(char*, sep->se_program, cp->se_program); |
| 941 | for (i = 0; i < MAXARGV; i++) |
| 942 | SWAP(char*, sep->se_argv[i], cp->se_argv[i]); |
| 943 | #undef SWAP |
| 944 | unblock_sigs(&omask); |
| 945 | free_servtab_strings(cp); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 946 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 947 | after_check: |
| 948 | /* cp->string_fields are consumed by insert_in_servlist() |
| 949 | * or freed at this point, cp itself is not yet freed. */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 950 | sep->se_checked = 1; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 951 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 952 | /* create new len_and_sockaddr */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 953 | switch (sep->se_family) { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 954 | struct sockaddr_un *sun; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 955 | case AF_UNIX: |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 956 | lsa = xzalloc_lsa(AF_UNIX); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 957 | sun = (struct sockaddr_un*)&lsa->u.sa; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 958 | safe_strncpy(sun->sun_path, sep->se_service, sizeof(sun->sun_path)); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 959 | break; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 960 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 961 | default: /* case AF_INET, case AF_INET6 */ |
| 962 | n = bb_strtou(sep->se_service, NULL, 10); |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 963 | #if ENABLE_FEATURE_INETD_RPC |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 964 | if (is_rpc_service(sep)) { |
| 965 | sep->se_rpcprog = n; |
| 966 | if (errno) { /* se_service is not numeric */ |
| 967 | struct rpcent *rp = getrpcbyname(sep->se_service); |
| 968 | if (rp == NULL) { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 969 | bb_error_msg("%s: unknown rpc service", sep->se_service); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 970 | goto next_cp; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 971 | } |
| 972 | sep->se_rpcprog = rp->r_number; |
| 973 | } |
| 974 | if (sep->se_fd == -1) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 975 | prepare_socket_fd(sep); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 976 | if (sep->se_fd != -1) |
| 977 | register_rpc(sep); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 978 | goto next_cp; |
| 979 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 980 | #endif |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 981 | /* what port to listen on? */ |
| 982 | port = htons(n); |
| 983 | if (errno || n > 0xffff) { /* se_service is not numeric */ |
| 984 | char protoname[4]; |
| 985 | struct servent *sp; |
| 986 | /* can result only in "tcp" or "udp": */ |
| 987 | safe_strncpy(protoname, sep->se_proto, 4); |
| 988 | sp = getservbyname(sep->se_service, protoname); |
| 989 | if (sp == NULL) { |
| 990 | bb_error_msg("%s/%s: unknown service", |
| 991 | sep->se_service, sep->se_proto); |
| 992 | goto next_cp; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 993 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 994 | port = sp->s_port; |
| 995 | } |
| 996 | if (LONE_CHAR(sep->se_local_hostname, '*')) { |
| 997 | lsa = xzalloc_lsa(sep->se_family); |
| 998 | set_nport(lsa, port); |
| 999 | } else { |
| 1000 | lsa = host_and_af2sockaddr(sep->se_local_hostname, |
| 1001 | ntohs(port), sep->se_family); |
| 1002 | if (!lsa) { |
| 1003 | bb_error_msg("%s/%s: unknown host '%s'", |
| 1004 | sep->se_service, sep->se_proto, |
| 1005 | sep->se_local_hostname); |
| 1006 | goto next_cp; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1007 | } |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1008 | } |
| 1009 | break; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1010 | } /* end of "switch (sep->se_family)" */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1011 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1012 | /* did lsa change? Then close/open */ |
| 1013 | if (sep->se_lsa == NULL |
| 1014 | || lsa->len != sep->se_lsa->len |
| 1015 | || memcmp(&lsa->u.sa, &sep->se_lsa->u.sa, lsa->len) != 0 |
| 1016 | ) { |
| 1017 | remove_fd_from_set(sep->se_fd); |
| 1018 | maybe_close(sep->se_fd); |
| 1019 | free(sep->se_lsa); |
| 1020 | sep->se_lsa = lsa; |
| 1021 | sep->se_fd = -1; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1022 | } else { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1023 | free(lsa); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1024 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1025 | if (sep->se_fd == -1) |
| 1026 | prepare_socket_fd(sep); |
| 1027 | next_cp: |
| 1028 | sep = cp->se_next; |
| 1029 | free(cp); |
| 1030 | cp = sep; |
| 1031 | } /* end of "while (1) parse lines" */ |
| 1032 | close_config_file(); |
| 1033 | |
| 1034 | /* Purge anything not looked at above - these are stale entries, |
| 1035 | * new config file doesnt have them. */ |
| 1036 | block_CHLD_HUP_ALRM(&omask); |
| 1037 | sepp = &serv_list; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1038 | while ((sep = *sepp)) { |
| 1039 | if (sep->se_checked) { |
| 1040 | sepp = &sep->se_next; |
| 1041 | continue; |
| 1042 | } |
| 1043 | *sepp = sep->se_next; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1044 | remove_fd_from_set(sep->se_fd); |
| 1045 | maybe_close(sep->se_fd); |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1046 | #if ENABLE_FEATURE_INETD_RPC |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1047 | if (is_rpc_service(sep)) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1048 | unregister_rpc(sep); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1049 | #endif |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1050 | if (sep->se_family == AF_UNIX) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1051 | unlink(sep->se_service); |
| 1052 | free_servtab_strings(sep); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1053 | free(sep); |
| 1054 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1055 | unblock_sigs(&omask); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1058 | static void reap_child(int sig ATTRIBUTE_UNUSED) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1059 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1060 | pid_t pid; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1061 | int status; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1062 | servtab_t *sep; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1063 | int save_errno = errno; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1064 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1065 | for (;;) { |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1066 | pid = wait_any_nohang(&status); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1067 | if (pid <= 0) |
| 1068 | break; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1069 | for (sep = serv_list; sep; sep = sep->se_next) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1070 | if (sep->se_wait == pid) { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1071 | /* One of our "wait" services */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1072 | if (WIFEXITED(status) && WEXITSTATUS(status)) |
| 1073 | bb_error_msg("%s: exit status 0x%x", |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1074 | sep->se_program, WEXITSTATUS(status)); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1075 | else if (WIFSIGNALED(status)) |
| 1076 | bb_error_msg("%s: exit signal 0x%x", |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1077 | sep->se_program, WTERMSIG(status)); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1078 | sep->se_wait = 1; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1079 | add_fd_to_set(sep->se_fd); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | errno = save_errno; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1085 | static void retry_network_setup(int sig ATTRIBUTE_UNUSED) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1086 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1087 | servtab_t *sep; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1088 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1089 | alarm_armed = 0; |
| 1090 | for (sep = serv_list; sep; sep = sep->se_next) { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1091 | if (sep->se_fd == -1) { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1092 | prepare_socket_fd(sep); |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1093 | #if ENABLE_FEATURE_INETD_RPC |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1094 | if (sep->se_fd != -1 && is_rpc_service(sep)) |
| 1095 | register_rpc(sep); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1096 | #endif |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1097 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1098 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1101 | static void clean_up_and_exit(int sig ATTRIBUTE_UNUSED) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1102 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1103 | servtab_t *sep; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1104 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1105 | /* XXX signal race walking sep list */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1106 | for (sep = serv_list; sep; sep = sep->se_next) { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1107 | if (sep->se_fd == -1) |
| 1108 | continue; |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1109 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1110 | switch (sep->se_family) { |
| 1111 | case AF_UNIX: |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1112 | unlink(sep->se_service); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1113 | break; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1114 | default: /* case AF_INET, AF_INET6 */ |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1115 | #if ENABLE_FEATURE_INETD_RPC |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1116 | if (sep->se_wait == 1 && is_rpc_service(sep)) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1117 | unregister_rpc(sep); /* XXX signal race */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1118 | #endif |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1119 | break; |
| 1120 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1121 | if (ENABLE_FEATURE_CLEAN_UP) |
| 1122 | close(sep->se_fd); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1123 | } |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 1124 | remove_pidfile(_PATH_INETDPID); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1125 | exit(0); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1128 | int inetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 1129 | int inetd_main(int argc, char **argv) |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1130 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1131 | struct sigaction sa, saved_pipe_handler; |
| 1132 | servtab_t *sep, *sep2; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1133 | struct passwd *pwd; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1134 | struct group *grp = grp; /* for compiler */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1135 | int opt; |
| 1136 | pid_t pid; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1137 | sigset_t omask; |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 1138 | |
| 1139 | INIT_G(); |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1140 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1141 | real_uid = getuid(); |
| 1142 | if (real_uid != 0) /* run by non-root user */ |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 1143 | config_filename = NULL; |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 1144 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1145 | opt_complementary = "R+:q+"; /* -q N, -R N */ |
| 1146 | opt = getopt32(argv, "R:feq:", &max_concurrency, &global_queuelen); |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 1147 | argv += optind; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1148 | //argc -= optind; |
| 1149 | if (argv[0]) |
Denis Vlasenko | 55f30b0 | 2007-03-24 22:42:29 +0000 | [diff] [blame] | 1150 | config_filename = argv[0]; |
| 1151 | if (config_filename == NULL) |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1152 | bb_error_msg_and_die("non-root must specify config file"); |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 1153 | if (!(opt & 2)) |
| 1154 | bb_daemonize_or_rexec(0, argv - optind); |
| 1155 | else |
| 1156 | bb_sanitize_stdio(); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1157 | if (!(opt & 4)) { |
| 1158 | openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON); |
| 1159 | logmode = LOGMODE_SYSLOG; |
| 1160 | } |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 1161 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1162 | if (real_uid == 0) { |
| 1163 | /* run by root, ensure groups vector gets trashed */ |
Denis Vlasenko | 7a431b3 | 2007-01-14 01:29:06 +0000 | [diff] [blame] | 1164 | gid_t gid = getgid(); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1165 | setgroups(1, &gid); |
| 1166 | } |
| 1167 | |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 1168 | write_pidfile(_PATH_INETDPID); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1169 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1170 | /* never fails under Linux (except if you pass it bad arguments) */ |
| 1171 | getrlimit(RLIMIT_NOFILE, &rlim_ofile); |
| 1172 | rlim_ofile_cur = rlim_ofile.rlim_cur; |
| 1173 | if (rlim_ofile_cur == RLIM_INFINITY) /* ! */ |
| 1174 | rlim_ofile_cur = OPEN_MAX; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1175 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1176 | memset(&sa, 0, sizeof(sa)); |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1177 | /*sigemptyset(&sa.sa_mask); - memset did it */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1178 | sigaddset(&sa.sa_mask, SIGALRM); |
| 1179 | sigaddset(&sa.sa_mask, SIGCHLD); |
| 1180 | sigaddset(&sa.sa_mask, SIGHUP); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1181 | sa.sa_handler = retry_network_setup; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1182 | sigaction(SIGALRM, &sa, NULL); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1183 | sa.sa_handler = reread_config_file; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1184 | sigaction(SIGHUP, &sa, NULL); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1185 | sa.sa_handler = reap_child; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1186 | sigaction(SIGCHLD, &sa, NULL); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1187 | sa.sa_handler = clean_up_and_exit; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1188 | sigaction(SIGTERM, &sa, NULL); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1189 | sa.sa_handler = clean_up_and_exit; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1190 | sigaction(SIGINT, &sa, NULL); |
| 1191 | sa.sa_handler = SIG_IGN; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1192 | sigaction(SIGPIPE, &sa, &saved_pipe_handler); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1193 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1194 | reread_config_file(SIGHUP); /* load config from file */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1195 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1196 | for (;;) { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1197 | int ready_fd_cnt; |
| 1198 | int ctrl, accepted_fd; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1199 | fd_set readable; |
| 1200 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1201 | if (maxsock < 0) |
| 1202 | recalculate_maxsock(); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1203 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1204 | readable = allsock; /* struct copy */ |
| 1205 | /* if there are no fds to wait on, we will block |
| 1206 | * until signal wakes us up */ |
| 1207 | ready_fd_cnt = select(maxsock + 1, &readable, NULL, NULL, NULL); |
| 1208 | if (ready_fd_cnt < 0) { |
| 1209 | if (errno != EINTR) { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1210 | bb_perror_msg("select"); |
| 1211 | sleep(1); |
| 1212 | } |
Glenn L McGrath | 82d42db | 2004-02-18 13:12:53 +0000 | [diff] [blame] | 1213 | continue; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1214 | } |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1215 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1216 | for (sep = serv_list; ready_fd_cnt && sep; sep = sep->se_next) { |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1217 | if (sep->se_fd == -1 || !FD_ISSET(sep->se_fd, &readable)) |
| 1218 | continue; |
| 1219 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1220 | ready_fd_cnt--; |
| 1221 | ctrl = sep->se_fd; |
| 1222 | accepted_fd = -1; |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1223 | if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1224 | ctrl = accepted_fd = accept(sep->se_fd, NULL, NULL); |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1225 | if (ctrl < 0) { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1226 | if (errno != EINTR) |
| 1227 | bb_perror_msg("accept (for %s)", sep->se_service); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1228 | continue; |
| 1229 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1230 | } |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1231 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1232 | block_CHLD_HUP_ALRM(&omask); |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1233 | pid = 0; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1234 | #ifdef INETD_BUILTINS_ENABLED |
| 1235 | /* do we need to fork? */ |
| 1236 | if (sep->se_builtin == NULL |
| 1237 | || (sep->se_socktype == SOCK_STREAM |
| 1238 | && sep->se_builtin->bi_fork)) |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1239 | #endif |
| 1240 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1241 | if (sep->se_max != 0) { |
| 1242 | if (++sep->se_count == 1) |
| 1243 | sep->se_time = monotonic_sec(); |
| 1244 | else if (sep->se_count >= sep->se_max) { |
| 1245 | unsigned now = monotonic_sec(); |
| 1246 | /* did we accumulate se_max connects too quickly? */ |
| 1247 | if (now - sep->se_time <= CNT_INTERVAL) { |
| 1248 | bb_error_msg("%s/%s: too many connections, pausing", |
| 1249 | sep->se_service, sep->se_proto); |
| 1250 | remove_fd_from_set(sep->se_fd); |
| 1251 | close(sep->se_fd); |
| 1252 | sep->se_fd = -1; |
| 1253 | sep->se_count = 0; |
| 1254 | rearm_alarm(); /* will revive it in RETRYTIME sec */ |
| 1255 | unblock_sigs(&omask); |
| 1256 | maybe_close(accepted_fd); |
| 1257 | continue; /* -> check next fd in fd set */ |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1258 | } |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1259 | sep->se_count = 0; |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1260 | } |
| 1261 | } |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1262 | /* on NOMMU, streamed chargen |
| 1263 | * builtin wouldn't work, but it is |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1264 | * not allowed on NOMMU (ifdefed out) */ |
| 1265 | #ifdef INETD_BUILTINS_ENABLED |
| 1266 | if (BB_MMU && sep->se_builtin) |
| 1267 | pid = fork(); |
| 1268 | else |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1269 | #endif |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1270 | pid = vfork(); |
| 1271 | |
| 1272 | if (pid < 0) { /* fork error */ |
| 1273 | bb_perror_msg("fork"); |
| 1274 | sleep(1); |
| 1275 | unblock_sigs(&omask); |
| 1276 | maybe_close(accepted_fd); |
| 1277 | continue; /* -> check next fd in fd set */ |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1278 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1279 | if (pid == 0) |
| 1280 | pid--; /* -1: "we did fork and we are child" */ |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1281 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1282 | /* if pid == 0 here, we never forked */ |
| 1283 | |
| 1284 | if (pid > 0) { /* parent */ |
| 1285 | if (sep->se_wait) { |
| 1286 | sep->se_wait = pid; |
| 1287 | remove_fd_from_set(sep->se_fd); |
| 1288 | /* we passed listening socket to child, |
| 1289 | * will wait for child to terminate */ |
| 1290 | } |
| 1291 | unblock_sigs(&omask); |
| 1292 | maybe_close(accepted_fd); |
| 1293 | continue; /* -> check next fd in fd set */ |
| 1294 | } |
| 1295 | |
| 1296 | /* we are either child or didn't fork at all */ |
| 1297 | #ifdef INETD_BUILTINS_ENABLED |
| 1298 | if (sep->se_builtin) { |
| 1299 | if (pid) { /* "pid" is -1: we did fork */ |
| 1300 | close(sep->se_fd); /* listening socket */ |
| 1301 | logmode = 0; /* make xwrite etc silent */ |
| 1302 | } |
| 1303 | unblock_sigs(&omask); |
| 1304 | if (sep->se_socktype == SOCK_STREAM) |
| 1305 | sep->se_builtin->bi_stream_fn(ctrl, sep); |
| 1306 | else |
| 1307 | sep->se_builtin->bi_dgram_fn(ctrl, sep); |
| 1308 | if (pid) /* we did fork */ |
| 1309 | _exit(0); |
| 1310 | maybe_close(accepted_fd); |
| 1311 | continue; /* -> check next fd in fd set */ |
| 1312 | } |
| 1313 | #endif |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1314 | /* child */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1315 | setsid(); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1316 | #if 0 |
| 1317 | /* This does not work. |
| 1318 | * Actually, it _almost_ works. The idea behind it is: child |
| 1319 | * can peek at (already received and buffered by kernel) UDP packet, |
| 1320 | * and perform connect() on the socket so that it is linked only |
| 1321 | * to this peer. But this also affects parent, because descriptors |
| 1322 | * are shared after fork() a-la dup(). When parent returns to |
| 1323 | * select(), it will see this descriptor attached to the peer (!) |
| 1324 | * and likely still readable, will act on it and mess things up |
| 1325 | * (can create many copies of same child, etc). |
| 1326 | * If child will create new socket instead, then bind() and |
| 1327 | * connect() it to peer's address, descriptor aliasing problem |
| 1328 | * is solved, but first packet cannot be "transferred" to the new |
| 1329 | * socket. It is not a problem if child can account for this, |
| 1330 | * but our child will exec - and exec'ed program does not know |
| 1331 | * about this "lost packet" problem! Pity... */ |
| 1332 | /* "nowait" udp[6]. Hmmm... */ |
| 1333 | if (!sep->se_wait |
| 1334 | && sep->se_socktype == SOCK_DGRAM |
| 1335 | && sep->se_family != AF_UNIX |
| 1336 | ) { |
| 1337 | len_and_sockaddr *lsa = xzalloc_lsa(sep->se_family); |
| 1338 | /* peek at the packet and remember peer addr */ |
| 1339 | int r = recvfrom(ctrl, NULL, 0, MSG_PEEK|MSG_DONTWAIT, |
| 1340 | &lsa->u.sa, &lsa->len); |
| 1341 | if (r >= 0) |
| 1342 | /* make this socket "connected" to peer addr: |
| 1343 | * only packets from this peer will be recv'ed, |
| 1344 | * and bare write()/send() will work on it */ |
| 1345 | connect(ctrl, &lsa->u.sa, lsa->len); |
| 1346 | free(lsa); |
| 1347 | } |
| 1348 | #endif |
| 1349 | /* prepare env and exec program */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1350 | pwd = getpwnam(sep->se_user); |
| 1351 | if (pwd == NULL) { |
| 1352 | bb_error_msg("%s: no such user", sep->se_user); |
| 1353 | goto do_exit1; |
| 1354 | } |
| 1355 | if (sep->se_group && (grp = getgrnam(sep->se_group)) == NULL) { |
| 1356 | bb_error_msg("%s: no such group", sep->se_group); |
| 1357 | goto do_exit1; |
| 1358 | } |
| 1359 | if (real_uid != 0 && real_uid != pwd->pw_uid) { |
| 1360 | /* a user running private inetd */ |
| 1361 | bb_error_msg("non-root must run services as himself"); |
| 1362 | goto do_exit1; |
| 1363 | } |
| 1364 | if (pwd->pw_uid) { |
| 1365 | if (sep->se_group) |
| 1366 | pwd->pw_gid = grp->gr_gid; |
| 1367 | xsetgid(pwd->pw_gid); |
| 1368 | initgroups(pwd->pw_name, pwd->pw_gid); |
| 1369 | xsetuid(pwd->pw_uid); |
| 1370 | } else if (sep->se_group) { |
| 1371 | xsetgid(grp->gr_gid); |
| 1372 | setgroups(1, &grp->gr_gid); |
| 1373 | } |
| 1374 | if (rlim_ofile.rlim_cur != rlim_ofile_cur) |
| 1375 | if (setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) |
| 1376 | bb_perror_msg("setrlimit"); |
| 1377 | closelog(); |
| 1378 | xmove_fd(ctrl, 0); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1379 | xdup2(0, 1); |
| 1380 | xdup2(0, 2); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1381 | /* NB: among others, this loop closes listening socket |
| 1382 | * for nowait stream children */ |
| 1383 | for (sep2 = serv_list; sep2; sep2 = sep2->se_next) |
| 1384 | maybe_close(sep2->se_fd); |
| 1385 | sigaction(SIGPIPE, &saved_pipe_handler, NULL); |
| 1386 | unblock_sigs(&omask); |
| 1387 | BB_EXECVP(sep->se_program, sep->se_argv); |
| 1388 | bb_perror_msg("exec %s", sep->se_program); |
| 1389 | do_exit1: |
| 1390 | /* eat packet in udp case */ |
| 1391 | if (sep->se_socktype != SOCK_STREAM) |
| 1392 | recv(0, line, LINE_SIZE, MSG_DONTWAIT); |
| 1393 | _exit(1); |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1394 | } /* for (sep = servtab...) */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 1395 | } /* for (;;) */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1396 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1397 | |
| 1398 | /* |
| 1399 | * Internet services provided internally by inetd: |
| 1400 | */ |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1401 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1402 | /* Echo service -- echo data back. */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1403 | /* ARGSUSED */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1404 | static void echo_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1405 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1406 | #if BB_MMU |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1407 | while (1) { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1408 | ssize_t sz = safe_read(s, line, LINE_SIZE); |
| 1409 | if (sz <= 0) |
| 1410 | break; |
| 1411 | xwrite(s, line, sz); |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1412 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1413 | #else |
| 1414 | static const char *const args[] = { "cat", NULL }; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1415 | /* no error messages */ |
| 1416 | xmove_fd(xopen("/dev/null", O_WRONLY), STDERR_FILENO); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1417 | BB_EXECVP("cat", (char**)args); |
| 1418 | _exit(1); |
| 1419 | #endif |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1420 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1421 | static void echo_dg(int s, servtab_t *sep) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1422 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1423 | enum { BUFSIZE = 12*1024 }; /* for jumbo sized packets! :) */ |
| 1424 | char *buf = xmalloc(BUFSIZE); /* too big for stack */ |
| 1425 | int sz; |
| 1426 | len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1427 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1428 | lsa->len = sep->se_lsa->len; |
| 1429 | /* dgram builtins are non-forking - DONT BLOCK! */ |
| 1430 | sz = recvfrom(s, buf, BUFSIZE, MSG_DONTWAIT, &lsa->u.sa, &lsa->len); |
| 1431 | if (sz > 0) |
| 1432 | sendto(s, buf, sz, 0, &lsa->u.sa, lsa->len); |
| 1433 | free(buf); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1434 | } |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1435 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_ECHO */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1436 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1437 | |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1438 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1439 | /* Discard service -- ignore data. MMU arches only. */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1440 | /* ARGSUSED */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1441 | static void discard_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1442 | { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1443 | #if BB_MMU |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1444 | while (safe_read(s, line, LINE_SIZE) > 0) |
| 1445 | continue; |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1446 | #else |
| 1447 | static const char *const args[] = { "dd", "of=/dev/null", NULL }; |
| 1448 | /* no error messages */ |
| 1449 | xmove_fd(xopen("/dev/null", O_WRONLY), STDERR_FILENO); |
| 1450 | BB_EXECVP("dd", (char**)args); |
| 1451 | _exit(1); |
| 1452 | #endif |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1453 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1454 | /* ARGSUSED */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1455 | static void discard_dg(int s, servtab_t *sep ATTRIBUTE_UNUSED) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1456 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1457 | /* dgram builtins are non-forking - DONT BLOCK! */ |
| 1458 | recv(s, line, LINE_SIZE, MSG_DONTWAIT); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1459 | } |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1460 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_DISCARD */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1461 | |
| 1462 | |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1463 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1464 | #define LINESIZ 72 |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1465 | static void init_ring(void) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1466 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1467 | int i; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1468 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1469 | end_ring = ring; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1470 | for (i = 0; i <= 128; ++i) |
| 1471 | if (isprint(i)) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1472 | *end_ring++ = i; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1473 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1474 | /* Character generator. MMU arches only. */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1475 | /* ARGSUSED */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1476 | static void chargen_stream(int s, servtab_t *sep) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1477 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1478 | char *rs; |
| 1479 | int len; |
| 1480 | char text[LINESIZ + 2]; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1481 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1482 | if (!end_ring) { |
| 1483 | init_ring(); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1484 | rs = ring; |
| 1485 | } |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1486 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1487 | text[LINESIZ] = '\r'; |
| 1488 | text[LINESIZ + 1] = '\n'; |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1489 | rs = ring; |
| 1490 | for (;;) { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1491 | len = end_ring - rs; |
Denis Vlasenko | c1876d7 | 2006-09-23 15:58:01 +0000 | [diff] [blame] | 1492 | if (len >= LINESIZ) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1493 | memmove(text, rs, LINESIZ); |
| 1494 | else { |
| 1495 | memmove(text, rs, len); |
| 1496 | memmove(text + len, ring, LINESIZ - len); |
| 1497 | } |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1498 | if (++rs == end_ring) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1499 | rs = ring; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1500 | xwrite(s, text, sizeof(text)); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1501 | } |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1502 | } |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1503 | /* ARGSUSED */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1504 | static void chargen_dg(int s, servtab_t *sep) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1505 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1506 | int len; |
| 1507 | char text[LINESIZ + 2]; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1508 | len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len); |
| 1509 | |
| 1510 | /* Eat UDP packet which started it all */ |
| 1511 | /* dgram builtins are non-forking - DONT BLOCK! */ |
| 1512 | lsa->len = sep->se_lsa->len; |
| 1513 | if (recvfrom(s, text, sizeof(text), MSG_DONTWAIT, &lsa->u.sa, &lsa->len) < 0) |
| 1514 | return; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1515 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1516 | if (!end_ring) { |
| 1517 | init_ring(); |
| 1518 | ring_pos = ring; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1521 | len = end_ring - ring_pos; |
Denis Vlasenko | 512a545 | 2007-09-24 10:41:30 +0000 | [diff] [blame] | 1522 | if (len >= LINESIZ) |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1523 | memmove(text, ring_pos, LINESIZ); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1524 | else { |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1525 | memmove(text, ring_pos, len); |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1526 | memmove(text + len, ring, LINESIZ - len); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1527 | } |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1528 | if (++ring_pos == end_ring) |
| 1529 | ring_pos = ring; |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1530 | text[LINESIZ] = '\r'; |
| 1531 | text[LINESIZ + 1] = '\n'; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1532 | sendto(s, text, sizeof(text), 0, &lsa->u.sa, lsa->len); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1533 | } |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1534 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1535 | |
| 1536 | |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1537 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1538 | /* |
| 1539 | * Return a machine readable date and time, in the form of the |
| 1540 | * number of seconds since midnight, Jan 1, 1900. Since gettimeofday |
| 1541 | * returns the number of seconds since midnight, Jan 1, 1970, |
| 1542 | * we must add 2208988800 seconds to this figure to make up for |
| 1543 | * some seventy years Bell Labs was asleep. |
| 1544 | */ |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame^] | 1545 | static uint32_t machtime(void) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1546 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1547 | struct timeval tv; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1548 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1549 | gettimeofday(&tv, NULL); |
| 1550 | return htonl((uint32_t)(tv.tv_sec + 2208988800)); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1551 | } |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1552 | /* ARGSUSED */ |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1553 | static void machtime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1554 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1555 | uint32_t result; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1556 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1557 | result = machtime(); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1558 | full_write(s, &result, sizeof(result)); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1559 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1560 | static void machtime_dg(int s, servtab_t *sep) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1561 | { |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1562 | uint32_t result; |
| 1563 | len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1564 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1565 | lsa->len = sep->se_lsa->len; |
| 1566 | if (recvfrom(s, line, LINE_SIZE, MSG_DONTWAIT, &lsa->u.sa, &lsa->len) < 0) |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1567 | return; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1568 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1569 | result = machtime(); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1570 | sendto(s, &result, sizeof(result), 0, &lsa->u.sa, lsa->len); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1571 | } |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1572 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_TIME */ |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1573 | |
| 1574 | |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1575 | #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1576 | /* Return human-readable time of day */ |
"Vladimir N. Oleynik" | 1a2f4d9 | 2005-10-03 08:08:58 +0000 | [diff] [blame] | 1577 | /* ARGSUSED */ |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1578 | static void daytime_stream(int s, servtab_t *sep ATTRIBUTE_UNUSED) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1579 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1580 | time_t t; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1581 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1582 | t = time(NULL); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1583 | fdprintf(s, "%.24s\r\n", ctime(&t)); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1584 | } |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1585 | static void daytime_dg(int s, servtab_t *sep) |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1586 | { |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1587 | time_t t; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1588 | len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len); |
| 1589 | |
| 1590 | lsa->len = sep->se_lsa->len; |
| 1591 | if (recvfrom(s, line, LINE_SIZE, MSG_DONTWAIT, &lsa->u.sa, &lsa->len) < 0) |
| 1592 | return; |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1593 | |
Denis Vlasenko | ec17d43 | 2006-09-23 15:18:38 +0000 | [diff] [blame] | 1594 | t = time(NULL); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 1595 | sprintf(line, "%.24s\r\n", ctime(&t)); |
| 1596 | sendto(s, line, strlen(line), 0, &lsa->u.sa, lsa->len); |
Glenn L McGrath | 06e9565 | 2003-02-09 06:51:14 +0000 | [diff] [blame] | 1597 | } |
Denis Vlasenko | d1a6e8d | 2007-01-14 14:46:18 +0000 | [diff] [blame] | 1598 | #endif /* FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME */ |