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