inetd.c 49 KB

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