inetd.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  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 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. goto more;
  622. }
  623. } else
  624. sep->se_local_hostname = xstrdup(default_local_hostname);
  625. /* service socktype proto wait user[:group] prog [args] */
  626. sep->se_service = xstrdup(arg);
  627. /* socktype proto wait user[:group] prog [args] */
  628. if (argc < 6) {
  629. parse_err:
  630. bb_error_msg("parse error on line %u, line is ignored",
  631. parser->lineno);
  632. free_servtab_strings(sep);
  633. /* Just "goto more" can make sep to carry over e.g.
  634. * "rpc"-ness (by having se_rpcver_lo != 0).
  635. * We will be more paranoid: */
  636. free(sep);
  637. goto new;
  638. }
  639. {
  640. static const int8_t SOCK_xxx[] ALIGN1 = {
  641. -1,
  642. SOCK_STREAM, SOCK_DGRAM, SOCK_RDM,
  643. SOCK_SEQPACKET, SOCK_RAW
  644. };
  645. sep->se_socktype = SOCK_xxx[1 + index_in_strings(
  646. "stream""\0" "dgram""\0" "rdm""\0"
  647. "seqpacket""\0" "raw""\0"
  648. , token[1])];
  649. }
  650. /* {unix,[rpc/]{tcp,udp}[6]} wait user[:group] prog [args] */
  651. sep->se_proto = arg = xstrdup(token[2]);
  652. if (strcmp(arg, "unix") == 0) {
  653. sep->se_family = AF_UNIX;
  654. } else {
  655. char *six;
  656. sep->se_family = AF_INET;
  657. six = last_char_is(arg, '6');
  658. if (six) {
  659. #if ENABLE_FEATURE_IPV6
  660. *six = '\0';
  661. sep->se_family = AF_INET6;
  662. #else
  663. bb_error_msg("%s: no support for IPv6", sep->se_proto);
  664. goto parse_err;
  665. #endif
  666. }
  667. if (strncmp(arg, "rpc/", 4) == 0) {
  668. #if ENABLE_FEATURE_INETD_RPC
  669. unsigned n;
  670. arg += 4;
  671. p = strchr(sep->se_service, '/');
  672. if (p == NULL) {
  673. bb_error_msg("no rpc version: '%s'", sep->se_service);
  674. goto parse_err;
  675. }
  676. *p++ = '\0';
  677. n = bb_strtou(p, &p, 10);
  678. if (n > INT_MAX) {
  679. bad_ver_spec:
  680. bb_error_msg("bad rpc version");
  681. goto parse_err;
  682. }
  683. sep->se_rpcver_lo = sep->se_rpcver_hi = n;
  684. if (*p == '-') {
  685. p++;
  686. n = bb_strtou(p, &p, 10);
  687. if (n > INT_MAX || (int)n < sep->se_rpcver_lo)
  688. goto bad_ver_spec;
  689. sep->se_rpcver_hi = n;
  690. }
  691. if (*p != '\0')
  692. goto bad_ver_spec;
  693. #else
  694. bb_error_msg("no support for rpc services");
  695. goto parse_err;
  696. #endif
  697. }
  698. /* we don't really need getprotobyname()! */
  699. if (strcmp(arg, "tcp") == 0)
  700. sep->se_proto_no = IPPROTO_TCP; /* = 6 */
  701. if (strcmp(arg, "udp") == 0)
  702. sep->se_proto_no = IPPROTO_UDP; /* = 17 */
  703. if (six)
  704. *six = '6';
  705. if (!sep->se_proto_no) /* not tcp/udp?? */
  706. goto parse_err;
  707. }
  708. /* [no]wait[.max] user[:group] prog [args] */
  709. arg = token[3];
  710. sep->se_max = max_concurrency;
  711. p = strchr(arg, '.');
  712. if (p) {
  713. *p++ = '\0';
  714. sep->se_max = bb_strtou(p, NULL, 10);
  715. if (errno)
  716. goto parse_err;
  717. }
  718. sep->se_wait = (arg[0] != 'n' || arg[1] != 'o');
  719. if (!sep->se_wait) /* "no" seen */
  720. arg += 2;
  721. if (strcmp(arg, "wait") != 0)
  722. goto parse_err;
  723. /* user[:group] prog [args] */
  724. sep->se_user = xstrdup(token[4]);
  725. arg = strchr(sep->se_user, '.');
  726. if (arg == NULL)
  727. arg = strchr(sep->se_user, ':');
  728. if (arg) {
  729. *arg++ = '\0';
  730. sep->se_group = xstrdup(arg);
  731. }
  732. /* prog [args] */
  733. sep->se_program = xstrdup(token[5]);
  734. #ifdef INETD_BUILTINS_ENABLED
  735. if (strcmp(sep->se_program, "internal") == 0
  736. && strlen(sep->se_service) <= 7
  737. && (sep->se_socktype == SOCK_STREAM
  738. || sep->se_socktype == SOCK_DGRAM)
  739. ) {
  740. unsigned i;
  741. for (i = 0; i < ARRAY_SIZE(builtins); i++)
  742. if (strncmp(builtins[i].bi_service7, sep->se_service, 7) == 0)
  743. goto found_bi;
  744. bb_error_msg("unknown internal service %s", sep->se_service);
  745. goto parse_err;
  746. found_bi:
  747. sep->se_builtin = &builtins[i];
  748. /* stream builtins must be "nowait", dgram must be "wait" */
  749. if (sep->se_wait != (sep->se_socktype == SOCK_DGRAM))
  750. goto parse_err;
  751. }
  752. #endif
  753. argc = 0;
  754. while ((arg = token[6+argc]) != NULL && argc < MAXARGV)
  755. sep->se_argv[argc++] = xstrdup(arg);
  756. /* Some inetd.conf files have no argv's, not even argv[0].
  757. * Fix them up.
  758. * (Technically, programs can be execed with argv[0] = NULL,
  759. * but many programs do not like that at all) */
  760. if (argc == 0)
  761. sep->se_argv[0] = xstrdup(sep->se_program);
  762. /* catch mixups. "<service> stream udp ..." == wtf */
  763. if (sep->se_socktype == SOCK_STREAM) {
  764. if (sep->se_proto_no == IPPROTO_UDP)
  765. goto parse_err;
  766. }
  767. if (sep->se_socktype == SOCK_DGRAM) {
  768. if (sep->se_proto_no == IPPROTO_TCP)
  769. goto parse_err;
  770. }
  771. // bb_info_msg(
  772. // "ENTRY[%s][%s][%s][%d][%d][%d][%d][%d][%s][%s][%s]",
  773. // sep->se_local_hostname, sep->se_service, sep->se_proto, sep->se_wait, sep->se_proto_no,
  774. // sep->se_max, sep->se_count, sep->se_time, sep->se_user, sep->se_group, sep->se_program);
  775. /* check if the hostname specifier is a comma separated list
  776. * of hostnames. we'll make new entries for each address. */
  777. while ((hostdelim = strrchr(sep->se_local_hostname, ',')) != NULL) {
  778. nsep = dup_servtab(sep);
  779. /* NUL terminate the hostname field of the existing entry,
  780. * and make a dup for the new entry. */
  781. *hostdelim++ = '\0';
  782. nsep->se_local_hostname = xstrdup(hostdelim);
  783. nsep->se_next = sep->se_next;
  784. sep->se_next = nsep;
  785. }
  786. /* was doing it here: */
  787. /* DNS resolution, create copies for each IP address */
  788. /* IPv6-ization destroyed it :( */
  789. return sep;
  790. }
  791. static servtab_t *insert_in_servlist(servtab_t *cp)
  792. {
  793. servtab_t *sep;
  794. sigset_t omask;
  795. sep = new_servtab();
  796. *sep = *cp; /* struct copy */
  797. sep->se_fd = -1;
  798. #if ENABLE_FEATURE_INETD_RPC
  799. sep->se_rpcprog = -1;
  800. #endif
  801. block_CHLD_HUP_ALRM(&omask);
  802. sep->se_next = serv_list;
  803. serv_list = sep;
  804. restore_sigmask(&omask);
  805. return sep;
  806. }
  807. static int same_serv_addr_proto(servtab_t *old, servtab_t *new)
  808. {
  809. if (strcmp(old->se_local_hostname, new->se_local_hostname) != 0)
  810. return 0;
  811. if (strcmp(old->se_service, new->se_service) != 0)
  812. return 0;
  813. if (strcmp(old->se_proto, new->se_proto) != 0)
  814. return 0;
  815. return 1;
  816. }
  817. static void reread_config_file(int sig UNUSED_PARAM)
  818. {
  819. servtab_t *sep, *cp, **sepp;
  820. len_and_sockaddr *lsa;
  821. sigset_t omask;
  822. unsigned n;
  823. uint16_t port;
  824. int save_errno = errno;
  825. if (!reopen_config_file())
  826. goto ret;
  827. for (sep = serv_list; sep; sep = sep->se_next)
  828. sep->se_checked = 0;
  829. goto first_line;
  830. while (1) {
  831. if (cp == NULL) {
  832. first_line:
  833. cp = parse_one_line();
  834. if (cp == NULL)
  835. break;
  836. }
  837. for (sep = serv_list; sep; sep = sep->se_next)
  838. if (same_serv_addr_proto(sep, cp))
  839. goto equal_servtab;
  840. /* not an "equal" servtab */
  841. sep = insert_in_servlist(cp);
  842. goto after_check;
  843. equal_servtab:
  844. {
  845. int i;
  846. block_CHLD_HUP_ALRM(&omask);
  847. #if ENABLE_FEATURE_INETD_RPC
  848. if (is_rpc_service(sep))
  849. unregister_rpc(sep);
  850. sep->se_rpcver_lo = cp->se_rpcver_lo;
  851. sep->se_rpcver_hi = cp->se_rpcver_hi;
  852. #endif
  853. if (cp->se_wait == 0) {
  854. /* New config says "nowait". If old one
  855. * was "wait", we currently may be waiting
  856. * for a child (and not accepting connects).
  857. * Stop waiting, start listening again.
  858. * (if it's not true, this op is harmless) */
  859. add_fd_to_set(sep->se_fd);
  860. }
  861. sep->se_wait = cp->se_wait;
  862. sep->se_max = cp->se_max;
  863. /* string fields need more love - we don't want to leak them */
  864. #define SWAP(type, a, b) do { type c = (type)a; a = (type)b; b = (type)c; } while (0)
  865. SWAP(char*, sep->se_user, cp->se_user);
  866. SWAP(char*, sep->se_group, cp->se_group);
  867. SWAP(char*, sep->se_program, cp->se_program);
  868. for (i = 0; i < MAXARGV; i++)
  869. SWAP(char*, sep->se_argv[i], cp->se_argv[i]);
  870. #undef SWAP
  871. restore_sigmask(&omask);
  872. free_servtab_strings(cp);
  873. }
  874. after_check:
  875. /* cp->string_fields are consumed by insert_in_servlist()
  876. * or freed at this point, cp itself is not yet freed. */
  877. sep->se_checked = 1;
  878. /* create new len_and_sockaddr */
  879. switch (sep->se_family) {
  880. struct sockaddr_un *sun;
  881. case AF_UNIX:
  882. lsa = xzalloc_lsa(AF_UNIX);
  883. sun = (struct sockaddr_un*)&lsa->u.sa;
  884. safe_strncpy(sun->sun_path, sep->se_service, sizeof(sun->sun_path));
  885. break;
  886. default: /* case AF_INET, case AF_INET6 */
  887. n = bb_strtou(sep->se_service, NULL, 10);
  888. #if ENABLE_FEATURE_INETD_RPC
  889. if (is_rpc_service(sep)) {
  890. sep->se_rpcprog = n;
  891. if (errno) { /* se_service is not numeric */
  892. struct rpcent *rp = getrpcbyname(sep->se_service);
  893. if (rp == NULL) {
  894. bb_error_msg("%s: unknown rpc service", sep->se_service);
  895. goto next_cp;
  896. }
  897. sep->se_rpcprog = rp->r_number;
  898. }
  899. if (sep->se_fd == -1)
  900. prepare_socket_fd(sep);
  901. if (sep->se_fd != -1)
  902. register_rpc(sep);
  903. goto next_cp;
  904. }
  905. #endif
  906. /* what port to listen on? */
  907. port = htons(n);
  908. if (errno || n > 0xffff) { /* se_service is not numeric */
  909. char protoname[4];
  910. struct servent *sp;
  911. /* can result only in "tcp" or "udp": */
  912. safe_strncpy(protoname, sep->se_proto, 4);
  913. sp = getservbyname(sep->se_service, protoname);
  914. if (sp == NULL) {
  915. bb_error_msg("%s/%s: unknown service",
  916. sep->se_service, sep->se_proto);
  917. goto next_cp;
  918. }
  919. port = sp->s_port;
  920. }
  921. if (LONE_CHAR(sep->se_local_hostname, '*')) {
  922. lsa = xzalloc_lsa(sep->se_family);
  923. set_nport(&lsa->u.sa, port);
  924. } else {
  925. lsa = host_and_af2sockaddr(sep->se_local_hostname,
  926. ntohs(port), sep->se_family);
  927. if (!lsa) {
  928. bb_error_msg("%s/%s: unknown host '%s'",
  929. sep->se_service, sep->se_proto,
  930. sep->se_local_hostname);
  931. goto next_cp;
  932. }
  933. }
  934. break;
  935. } /* end of "switch (sep->se_family)" */
  936. /* did lsa change? Then close/open */
  937. if (sep->se_lsa == NULL
  938. || lsa->len != sep->se_lsa->len
  939. || memcmp(&lsa->u.sa, &sep->se_lsa->u.sa, lsa->len) != 0
  940. ) {
  941. remove_fd_from_set(sep->se_fd);
  942. maybe_close(sep->se_fd);
  943. free(sep->se_lsa);
  944. sep->se_lsa = lsa;
  945. sep->se_fd = -1;
  946. } else {
  947. free(lsa);
  948. }
  949. if (sep->se_fd == -1)
  950. prepare_socket_fd(sep);
  951. next_cp:
  952. sep = cp->se_next;
  953. free(cp);
  954. cp = sep;
  955. } /* end of "while (1) parse lines" */
  956. close_config_file();
  957. /* Purge anything not looked at above - these are stale entries,
  958. * new config file doesnt have them. */
  959. block_CHLD_HUP_ALRM(&omask);
  960. sepp = &serv_list;
  961. while ((sep = *sepp) != NULL) {
  962. if (sep->se_checked) {
  963. sepp = &sep->se_next;
  964. continue;
  965. }
  966. *sepp = sep->se_next;
  967. remove_fd_from_set(sep->se_fd);
  968. maybe_close(sep->se_fd);
  969. #if ENABLE_FEATURE_INETD_RPC
  970. if (is_rpc_service(sep))
  971. unregister_rpc(sep);
  972. #endif
  973. if (sep->se_family == AF_UNIX)
  974. unlink(sep->se_service);
  975. free_servtab_strings(sep);
  976. free(sep);
  977. }
  978. restore_sigmask(&omask);
  979. ret:
  980. errno = save_errno;
  981. }
  982. static void reap_child(int sig UNUSED_PARAM)
  983. {
  984. pid_t pid;
  985. int status;
  986. servtab_t *sep;
  987. int save_errno = errno;
  988. for (;;) {
  989. pid = wait_any_nohang(&status);
  990. if (pid <= 0)
  991. break;
  992. for (sep = serv_list; sep; sep = sep->se_next) {
  993. if (sep->se_wait != pid)
  994. continue;
  995. /* One of our "wait" services */
  996. if (WIFEXITED(status) && WEXITSTATUS(status))
  997. bb_error_msg("%s: exit status %u",
  998. sep->se_program, WEXITSTATUS(status));
  999. else if (WIFSIGNALED(status))
  1000. bb_error_msg("%s: exit signal %u",
  1001. sep->se_program, WTERMSIG(status));
  1002. sep->se_wait = 1;
  1003. add_fd_to_set(sep->se_fd);
  1004. break;
  1005. }
  1006. }
  1007. errno = save_errno;
  1008. }
  1009. static void retry_network_setup(int sig UNUSED_PARAM)
  1010. {
  1011. int save_errno = errno;
  1012. servtab_t *sep;
  1013. alarm_armed = 0;
  1014. for (sep = serv_list; sep; sep = sep->se_next) {
  1015. if (sep->se_fd == -1) {
  1016. prepare_socket_fd(sep);
  1017. #if ENABLE_FEATURE_INETD_RPC
  1018. if (sep->se_fd != -1 && is_rpc_service(sep))
  1019. register_rpc(sep);
  1020. #endif
  1021. }
  1022. }
  1023. errno = save_errno;
  1024. }
  1025. static void clean_up_and_exit(int sig UNUSED_PARAM)
  1026. {
  1027. servtab_t *sep;
  1028. /* XXX signal race walking sep list */
  1029. for (sep = serv_list; sep; sep = sep->se_next) {
  1030. if (sep->se_fd == -1)
  1031. continue;
  1032. switch (sep->se_family) {
  1033. case AF_UNIX:
  1034. unlink(sep->se_service);
  1035. break;
  1036. default: /* case AF_INET, AF_INET6 */
  1037. #if ENABLE_FEATURE_INETD_RPC
  1038. if (sep->se_wait == 1 && is_rpc_service(sep))
  1039. unregister_rpc(sep); /* XXX signal race */
  1040. #endif
  1041. break;
  1042. }
  1043. if (ENABLE_FEATURE_CLEAN_UP)
  1044. close(sep->se_fd);
  1045. }
  1046. remove_pidfile(CONFIG_PID_FILE_PATH "/inetd.pid");
  1047. exit(EXIT_SUCCESS);
  1048. }
  1049. int inetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1050. int inetd_main(int argc UNUSED_PARAM, char **argv)
  1051. {
  1052. struct sigaction sa, saved_pipe_handler;
  1053. servtab_t *sep, *sep2;
  1054. struct passwd *pwd;
  1055. struct group *grp = grp; /* for compiler */
  1056. int opt;
  1057. pid_t pid;
  1058. sigset_t omask;
  1059. INIT_G();
  1060. real_uid = getuid();
  1061. if (real_uid != 0) /* run by non-root user */
  1062. config_filename = NULL;
  1063. opt_complementary = "R+:q+"; /* -q N, -R N */
  1064. opt = getopt32(argv, "R:feq:", &max_concurrency, &global_queuelen);
  1065. argv += optind;
  1066. //argc -= optind;
  1067. if (argv[0])
  1068. config_filename = argv[0];
  1069. if (config_filename == NULL)
  1070. bb_error_msg_and_die("non-root must specify config file");
  1071. if (!(opt & 2))
  1072. bb_daemonize_or_rexec(0, argv - optind);
  1073. else
  1074. bb_sanitize_stdio();
  1075. if (!(opt & 4)) {
  1076. /* LOG_NDELAY: connect to syslog daemon NOW.
  1077. * Otherwise, we may open syslog socket
  1078. * in vforked child, making opened fds and syslog()
  1079. * internal state inconsistent.
  1080. * This was observed to leak file descriptors. */
  1081. openlog(applet_name, LOG_PID | LOG_NDELAY, LOG_DAEMON);
  1082. logmode = LOGMODE_SYSLOG;
  1083. }
  1084. if (real_uid == 0) {
  1085. /* run by root, ensure groups vector gets trashed */
  1086. gid_t gid = getgid();
  1087. setgroups(1, &gid);
  1088. }
  1089. write_pidfile(CONFIG_PID_FILE_PATH "/inetd.pid");
  1090. /* never fails under Linux (except if you pass it bad arguments) */
  1091. getrlimit(RLIMIT_NOFILE, &rlim_ofile);
  1092. rlim_ofile_cur = rlim_ofile.rlim_cur;
  1093. if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
  1094. rlim_ofile_cur = OPEN_MAX;
  1095. memset(&sa, 0, sizeof(sa));
  1096. /*sigemptyset(&sa.sa_mask); - memset did it */
  1097. sigaddset(&sa.sa_mask, SIGALRM);
  1098. sigaddset(&sa.sa_mask, SIGCHLD);
  1099. sigaddset(&sa.sa_mask, SIGHUP);
  1100. //FIXME: explain why no SA_RESTART
  1101. //FIXME: retry_network_setup is unsafe to run in signal handler (many reasons)!
  1102. sa.sa_handler = retry_network_setup;
  1103. sigaction_set(SIGALRM, &sa);
  1104. //FIXME: reread_config_file is unsafe to run in signal handler(many reasons)!
  1105. sa.sa_handler = reread_config_file;
  1106. sigaction_set(SIGHUP, &sa);
  1107. //FIXME: reap_child is unsafe to run in signal handler (uses stdio)!
  1108. sa.sa_handler = reap_child;
  1109. sigaction_set(SIGCHLD, &sa);
  1110. //FIXME: clean_up_and_exit is unsafe to run in signal handler (uses stdio)!
  1111. sa.sa_handler = clean_up_and_exit;
  1112. sigaction_set(SIGTERM, &sa);
  1113. sa.sa_handler = clean_up_and_exit;
  1114. sigaction_set(SIGINT, &sa);
  1115. sa.sa_handler = SIG_IGN;
  1116. sigaction(SIGPIPE, &sa, &saved_pipe_handler);
  1117. reread_config_file(SIGHUP); /* load config from file */
  1118. for (;;) {
  1119. int ready_fd_cnt;
  1120. int ctrl, accepted_fd, new_udp_fd;
  1121. fd_set readable;
  1122. if (maxsock < 0)
  1123. recalculate_maxsock();
  1124. readable = allsock; /* struct copy */
  1125. /* if there are no fds to wait on, we will block
  1126. * until signal wakes us up (maxsock == 0, but readable
  1127. * never contains fds 0 and 1...) */
  1128. ready_fd_cnt = select(maxsock + 1, &readable, NULL, NULL, NULL);
  1129. if (ready_fd_cnt < 0) {
  1130. if (errno != EINTR) {
  1131. bb_perror_msg("select");
  1132. sleep(1);
  1133. }
  1134. continue;
  1135. }
  1136. dbg("ready_fd_cnt:%d\n", ready_fd_cnt);
  1137. for (sep = serv_list; ready_fd_cnt && sep; sep = sep->se_next) {
  1138. if (sep->se_fd == -1 || !FD_ISSET(sep->se_fd, &readable))
  1139. continue;
  1140. dbg("ready fd:%d\n", sep->se_fd);
  1141. ready_fd_cnt--;
  1142. ctrl = sep->se_fd;
  1143. accepted_fd = -1;
  1144. new_udp_fd = -1;
  1145. if (!sep->se_wait) {
  1146. if (sep->se_socktype == SOCK_STREAM) {
  1147. ctrl = accepted_fd = accept(sep->se_fd, NULL, NULL);
  1148. dbg("accepted_fd:%d\n", accepted_fd);
  1149. if (ctrl < 0) {
  1150. if (errno != EINTR)
  1151. bb_perror_msg("accept (for %s)", sep->se_service);
  1152. continue;
  1153. }
  1154. }
  1155. /* "nowait" udp */
  1156. if (sep->se_socktype == SOCK_DGRAM
  1157. && sep->se_family != AF_UNIX
  1158. ) {
  1159. /* How udp "nowait" works:
  1160. * child peeks at (received and buffered by kernel) UDP packet,
  1161. * performs connect() on the socket so that it is linked only
  1162. * to this peer. But this also affects parent, because descriptors
  1163. * are shared after fork() a-la dup(). When parent performs
  1164. * select(), it will see this descriptor connected to the peer (!)
  1165. * and still readable, will act on it and mess things up
  1166. * (can create many copies of same child, etc).
  1167. * Parent must create and use new socket instead. */
  1168. new_udp_fd = socket(sep->se_family, SOCK_DGRAM, 0);
  1169. dbg("new_udp_fd:%d\n", new_udp_fd);
  1170. if (new_udp_fd < 0) { /* error: eat packet, forget about it */
  1171. udp_err:
  1172. recv(sep->se_fd, line, LINE_SIZE, MSG_DONTWAIT);
  1173. continue;
  1174. }
  1175. setsockopt_reuseaddr(new_udp_fd);
  1176. /* TODO: better do bind after fork in parent,
  1177. * so that we don't have two wildcard bound sockets
  1178. * even for a brief moment? */
  1179. if (bind(new_udp_fd, &sep->se_lsa->u.sa, sep->se_lsa->len) < 0) {
  1180. dbg("bind(new_udp_fd) failed\n");
  1181. close(new_udp_fd);
  1182. goto udp_err;
  1183. }
  1184. dbg("bind(new_udp_fd) succeeded\n");
  1185. }
  1186. }
  1187. block_CHLD_HUP_ALRM(&omask);
  1188. pid = 0;
  1189. #ifdef INETD_BUILTINS_ENABLED
  1190. /* do we need to fork? */
  1191. if (sep->se_builtin == NULL
  1192. || (sep->se_socktype == SOCK_STREAM
  1193. && sep->se_builtin->bi_fork))
  1194. #endif
  1195. {
  1196. if (sep->se_max != 0) {
  1197. if (++sep->se_count == 1)
  1198. sep->se_time = monotonic_sec();
  1199. else if (sep->se_count >= sep->se_max) {
  1200. unsigned now = monotonic_sec();
  1201. /* did we accumulate se_max connects too quickly? */
  1202. if (now - sep->se_time <= CNT_INTERVAL) {
  1203. bb_error_msg("%s/%s: too many connections, pausing",
  1204. sep->se_service, sep->se_proto);
  1205. remove_fd_from_set(sep->se_fd);
  1206. close(sep->se_fd);
  1207. sep->se_fd = -1;
  1208. sep->se_count = 0;
  1209. rearm_alarm(); /* will revive it in RETRYTIME sec */
  1210. restore_sigmask(&omask);
  1211. maybe_close(new_udp_fd);
  1212. maybe_close(accepted_fd);
  1213. continue; /* -> check next fd in fd set */
  1214. }
  1215. sep->se_count = 0;
  1216. }
  1217. }
  1218. /* on NOMMU, streamed chargen
  1219. * builtin wouldn't work, but it is
  1220. * not allowed on NOMMU (ifdefed out) */
  1221. #ifdef INETD_BUILTINS_ENABLED
  1222. if (BB_MMU && sep->se_builtin)
  1223. pid = fork();
  1224. else
  1225. #endif
  1226. pid = vfork();
  1227. if (pid < 0) { /* fork error */
  1228. bb_perror_msg("vfork"+1);
  1229. sleep(1);
  1230. restore_sigmask(&omask);
  1231. maybe_close(new_udp_fd);
  1232. maybe_close(accepted_fd);
  1233. continue; /* -> check next fd in fd set */
  1234. }
  1235. if (pid == 0)
  1236. pid--; /* -1: "we did fork and we are child" */
  1237. }
  1238. /* if pid == 0 here, we didn't fork */
  1239. if (pid > 0) { /* parent */
  1240. if (sep->se_wait) {
  1241. /* wait: we passed socket to child,
  1242. * will wait for child to terminate */
  1243. sep->se_wait = pid;
  1244. remove_fd_from_set(sep->se_fd);
  1245. }
  1246. if (new_udp_fd >= 0) {
  1247. /* udp nowait: child connected the socket,
  1248. * we created and will use new, unconnected one */
  1249. xmove_fd(new_udp_fd, sep->se_fd);
  1250. dbg("moved new_udp_fd:%d to sep->se_fd:%d\n", new_udp_fd, sep->se_fd);
  1251. }
  1252. restore_sigmask(&omask);
  1253. maybe_close(accepted_fd);
  1254. continue; /* -> check next fd in fd set */
  1255. }
  1256. /* we are either child or didn't fork at all */
  1257. #ifdef INETD_BUILTINS_ENABLED
  1258. if (sep->se_builtin) {
  1259. if (pid) { /* "pid" is -1: we did fork */
  1260. close(sep->se_fd); /* listening socket */
  1261. dbg("closed sep->se_fd:%d\n", sep->se_fd);
  1262. logmode = LOGMODE_NONE; /* make xwrite etc silent */
  1263. }
  1264. restore_sigmask(&omask);
  1265. if (sep->se_socktype == SOCK_STREAM)
  1266. sep->se_builtin->bi_stream_fn(ctrl, sep);
  1267. else
  1268. sep->se_builtin->bi_dgram_fn(ctrl, sep);
  1269. if (pid) /* we did fork */
  1270. _exit(EXIT_FAILURE);
  1271. maybe_close(accepted_fd);
  1272. continue; /* -> check next fd in fd set */
  1273. }
  1274. #endif
  1275. /* child */
  1276. setsid();
  1277. /* "nowait" udp */
  1278. if (new_udp_fd >= 0) {
  1279. len_and_sockaddr *lsa;
  1280. int r;
  1281. close(new_udp_fd);
  1282. dbg("closed new_udp_fd:%d\n", new_udp_fd);
  1283. lsa = xzalloc_lsa(sep->se_family);
  1284. /* peek at the packet and remember peer addr */
  1285. r = recvfrom(ctrl, NULL, 0, MSG_PEEK|MSG_DONTWAIT,
  1286. &lsa->u.sa, &lsa->len);
  1287. if (r < 0)
  1288. goto do_exit1;
  1289. /* make this socket "connected" to peer addr:
  1290. * only packets from this peer will be recv'ed,
  1291. * and bare write()/send() will work on it */
  1292. connect(ctrl, &lsa->u.sa, lsa->len);
  1293. dbg("connected ctrl:%d to remote peer\n", ctrl);
  1294. free(lsa);
  1295. }
  1296. /* prepare env and exec program */
  1297. pwd = getpwnam(sep->se_user);
  1298. if (pwd == NULL) {
  1299. bb_error_msg("%s: no such %s", sep->se_user, "user");
  1300. goto do_exit1;
  1301. }
  1302. if (sep->se_group && (grp = getgrnam(sep->se_group)) == NULL) {
  1303. bb_error_msg("%s: no such %s", sep->se_group, "group");
  1304. goto do_exit1;
  1305. }
  1306. if (real_uid != 0 && real_uid != pwd->pw_uid) {
  1307. /* a user running private inetd */
  1308. bb_error_msg("non-root must run services as himself");
  1309. goto do_exit1;
  1310. }
  1311. if (pwd->pw_uid != 0) {
  1312. if (sep->se_group)
  1313. pwd->pw_gid = grp->gr_gid;
  1314. /* initgroups, setgid, setuid: */
  1315. change_identity(pwd);
  1316. } else if (sep->se_group) {
  1317. xsetgid(grp->gr_gid);
  1318. setgroups(1, &grp->gr_gid);
  1319. }
  1320. if (rlim_ofile.rlim_cur != rlim_ofile_cur)
  1321. if (setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0)
  1322. bb_perror_msg("setrlimit");
  1323. /* closelog(); - WRONG. we are after vfork,
  1324. * this may confuse syslog() internal state.
  1325. * Let's hope libc sets syslog fd to CLOEXEC...
  1326. */
  1327. xmove_fd(ctrl, STDIN_FILENO);
  1328. xdup2(STDIN_FILENO, STDOUT_FILENO);
  1329. dbg("moved ctrl:%d to fd 0,1[,2]\n", ctrl);
  1330. /* manpages of inetd I managed to find either say
  1331. * that stderr is also redirected to the network,
  1332. * or do not talk about redirection at all (!) */
  1333. if (!sep->se_wait) /* only for usual "tcp nowait" */
  1334. xdup2(STDIN_FILENO, STDERR_FILENO);
  1335. /* NB: among others, this loop closes listening sockets
  1336. * for nowait stream children */
  1337. for (sep2 = serv_list; sep2; sep2 = sep2->se_next)
  1338. if (sep2->se_fd != ctrl)
  1339. maybe_close(sep2->se_fd);
  1340. sigaction_set(SIGPIPE, &saved_pipe_handler);
  1341. restore_sigmask(&omask);
  1342. dbg("execing:'%s'\n", sep->se_program);
  1343. BB_EXECVP(sep->se_program, sep->se_argv);
  1344. bb_perror_msg("can't execute '%s'", sep->se_program);
  1345. do_exit1:
  1346. /* eat packet in udp case */
  1347. if (sep->se_socktype != SOCK_STREAM)
  1348. recv(0, line, LINE_SIZE, MSG_DONTWAIT);
  1349. _exit(EXIT_FAILURE);
  1350. } /* for (sep = servtab...) */
  1351. } /* for (;;) */
  1352. }
  1353. #if !BB_MMU
  1354. static const char *const cat_args[] = { "cat", NULL };
  1355. #endif
  1356. /*
  1357. * Internet services provided internally by inetd:
  1358. */
  1359. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_ECHO
  1360. /* Echo service -- echo data back. */
  1361. /* ARGSUSED */
  1362. static void FAST_FUNC echo_stream(int s, servtab_t *sep UNUSED_PARAM)
  1363. {
  1364. #if BB_MMU
  1365. while (1) {
  1366. ssize_t sz = safe_read(s, line, LINE_SIZE);
  1367. if (sz <= 0)
  1368. break;
  1369. xwrite(s, line, sz);
  1370. }
  1371. #else
  1372. /* We are after vfork here! */
  1373. /* move network socket to stdin/stdout */
  1374. xmove_fd(s, STDIN_FILENO);
  1375. xdup2(STDIN_FILENO, STDOUT_FILENO);
  1376. /* no error messages please... */
  1377. close(STDERR_FILENO);
  1378. xopen(bb_dev_null, O_WRONLY);
  1379. BB_EXECVP("cat", (char**)cat_args);
  1380. /* on failure we return to main, which does exit(EXIT_FAILURE) */
  1381. #endif
  1382. }
  1383. static void FAST_FUNC echo_dg(int s, servtab_t *sep)
  1384. {
  1385. enum { BUFSIZE = 12*1024 }; /* for jumbo sized packets! :) */
  1386. char *buf = xmalloc(BUFSIZE); /* too big for stack */
  1387. int sz;
  1388. len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len);
  1389. lsa->len = sep->se_lsa->len;
  1390. /* dgram builtins are non-forking - DONT BLOCK! */
  1391. sz = recvfrom(s, buf, BUFSIZE, MSG_DONTWAIT, &lsa->u.sa, &lsa->len);
  1392. if (sz > 0)
  1393. sendto(s, buf, sz, 0, &lsa->u.sa, lsa->len);
  1394. free(buf);
  1395. }
  1396. #endif /* FEATURE_INETD_SUPPORT_BUILTIN_ECHO */
  1397. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD
  1398. /* Discard service -- ignore data. */
  1399. /* ARGSUSED */
  1400. static void FAST_FUNC discard_stream(int s, servtab_t *sep UNUSED_PARAM)
  1401. {
  1402. #if BB_MMU
  1403. while (safe_read(s, line, LINE_SIZE) > 0)
  1404. continue;
  1405. #else
  1406. /* We are after vfork here! */
  1407. /* move network socket to stdin */
  1408. xmove_fd(s, STDIN_FILENO);
  1409. /* discard output */
  1410. close(STDOUT_FILENO);
  1411. xopen(bb_dev_null, O_WRONLY);
  1412. /* no error messages please... */
  1413. xdup2(STDOUT_FILENO, STDERR_FILENO);
  1414. BB_EXECVP("cat", (char**)cat_args);
  1415. /* on failure we return to main, which does exit(EXIT_FAILURE) */
  1416. #endif
  1417. }
  1418. /* ARGSUSED */
  1419. static void FAST_FUNC discard_dg(int s, servtab_t *sep UNUSED_PARAM)
  1420. {
  1421. /* dgram builtins are non-forking - DONT BLOCK! */
  1422. recv(s, line, LINE_SIZE, MSG_DONTWAIT);
  1423. }
  1424. #endif /* FEATURE_INETD_SUPPORT_BUILTIN_DISCARD */
  1425. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN
  1426. #define LINESIZ 72
  1427. static void init_ring(void)
  1428. {
  1429. int i;
  1430. end_ring = ring;
  1431. for (i = ' '; i < 127; i++)
  1432. *end_ring++ = i;
  1433. }
  1434. /* Character generator. MMU arches only. */
  1435. /* ARGSUSED */
  1436. static void FAST_FUNC chargen_stream(int s, servtab_t *sep UNUSED_PARAM)
  1437. {
  1438. char *rs;
  1439. int len;
  1440. char text[LINESIZ + 2];
  1441. if (!end_ring) {
  1442. init_ring();
  1443. rs = ring;
  1444. }
  1445. text[LINESIZ] = '\r';
  1446. text[LINESIZ + 1] = '\n';
  1447. rs = ring;
  1448. for (;;) {
  1449. len = end_ring - rs;
  1450. if (len >= LINESIZ)
  1451. memmove(text, rs, LINESIZ);
  1452. else {
  1453. memmove(text, rs, len);
  1454. memmove(text + len, ring, LINESIZ - len);
  1455. }
  1456. if (++rs == end_ring)
  1457. rs = ring;
  1458. xwrite(s, text, sizeof(text));
  1459. }
  1460. }
  1461. /* ARGSUSED */
  1462. static void FAST_FUNC chargen_dg(int s, servtab_t *sep)
  1463. {
  1464. int len;
  1465. char text[LINESIZ + 2];
  1466. len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len);
  1467. /* Eat UDP packet which started it all */
  1468. /* dgram builtins are non-forking - DONT BLOCK! */
  1469. lsa->len = sep->se_lsa->len;
  1470. if (recvfrom(s, text, sizeof(text), MSG_DONTWAIT, &lsa->u.sa, &lsa->len) < 0)
  1471. return;
  1472. if (!end_ring) {
  1473. init_ring();
  1474. ring_pos = ring;
  1475. }
  1476. len = end_ring - ring_pos;
  1477. if (len >= LINESIZ)
  1478. memmove(text, ring_pos, LINESIZ);
  1479. else {
  1480. memmove(text, ring_pos, len);
  1481. memmove(text + len, ring, LINESIZ - len);
  1482. }
  1483. if (++ring_pos == end_ring)
  1484. ring_pos = ring;
  1485. text[LINESIZ] = '\r';
  1486. text[LINESIZ + 1] = '\n';
  1487. sendto(s, text, sizeof(text), 0, &lsa->u.sa, lsa->len);
  1488. }
  1489. #endif /* FEATURE_INETD_SUPPORT_BUILTIN_CHARGEN */
  1490. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_TIME
  1491. /*
  1492. * Return a machine readable date and time, in the form of the
  1493. * number of seconds since midnight, Jan 1, 1900. Since gettimeofday
  1494. * returns the number of seconds since midnight, Jan 1, 1970,
  1495. * we must add 2208988800 seconds to this figure to make up for
  1496. * some seventy years Bell Labs was asleep.
  1497. */
  1498. static uint32_t machtime(void)
  1499. {
  1500. struct timeval tv;
  1501. gettimeofday(&tv, NULL);
  1502. return htonl((uint32_t)(tv.tv_sec + 2208988800));
  1503. }
  1504. /* ARGSUSED */
  1505. static void FAST_FUNC machtime_stream(int s, servtab_t *sep UNUSED_PARAM)
  1506. {
  1507. uint32_t result;
  1508. result = machtime();
  1509. full_write(s, &result, sizeof(result));
  1510. }
  1511. static void FAST_FUNC machtime_dg(int s, servtab_t *sep)
  1512. {
  1513. uint32_t result;
  1514. len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len);
  1515. lsa->len = sep->se_lsa->len;
  1516. if (recvfrom(s, line, LINE_SIZE, MSG_DONTWAIT, &lsa->u.sa, &lsa->len) < 0)
  1517. return;
  1518. result = machtime();
  1519. sendto(s, &result, sizeof(result), 0, &lsa->u.sa, lsa->len);
  1520. }
  1521. #endif /* FEATURE_INETD_SUPPORT_BUILTIN_TIME */
  1522. #if ENABLE_FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME
  1523. /* Return human-readable time of day */
  1524. /* ARGSUSED */
  1525. static void FAST_FUNC daytime_stream(int s, servtab_t *sep UNUSED_PARAM)
  1526. {
  1527. time_t t;
  1528. t = time(NULL);
  1529. fdprintf(s, "%.24s\r\n", ctime(&t));
  1530. }
  1531. static void FAST_FUNC daytime_dg(int s, servtab_t *sep)
  1532. {
  1533. time_t t;
  1534. len_and_sockaddr *lsa = alloca(LSA_LEN_SIZE + sep->se_lsa->len);
  1535. lsa->len = sep->se_lsa->len;
  1536. if (recvfrom(s, line, LINE_SIZE, MSG_DONTWAIT, &lsa->u.sa, &lsa->len) < 0)
  1537. return;
  1538. t = time(NULL);
  1539. sprintf(line, "%.24s\r\n", ctime(&t));
  1540. sendto(s, line, strlen(line), 0, &lsa->u.sa, lsa->len);
  1541. }
  1542. #endif /* FEATURE_INETD_SUPPORT_BUILTIN_DAYTIME */