gnunet-service-arm.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file arm/gnunet-service-arm.c
  19. * @brief the automated restart manager service
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_arm_service.h"
  25. #include "gnunet_protocols.h"
  26. #include "arm.h"
  27. /**
  28. * How many messages do we queue up at most for optional
  29. * notifications to a client? (this can cause notifications
  30. * about outgoing messages to be dropped).
  31. */
  32. #define MAX_NOTIFY_QUEUE 1024
  33. /**
  34. * List of our services.
  35. */
  36. struct ServiceList;
  37. /**
  38. * Record with information about a listen socket we have open.
  39. */
  40. struct ServiceListeningInfo
  41. {
  42. /**
  43. * This is a linked list.
  44. */
  45. struct ServiceListeningInfo *next;
  46. /**
  47. * This is a linked list.
  48. */
  49. struct ServiceListeningInfo *prev;
  50. /**
  51. * Address this socket is listening on.
  52. */
  53. struct sockaddr *service_addr;
  54. /**
  55. * Service this listen socket is for.
  56. */
  57. struct ServiceList *sl;
  58. /**
  59. * Number of bytes in 'service_addr'
  60. */
  61. socklen_t service_addr_len;
  62. /**
  63. * Our listening socket.
  64. */
  65. struct GNUNET_NETWORK_Handle *listen_socket;
  66. /**
  67. * Task doing the accepting.
  68. */
  69. struct GNUNET_SCHEDULER_Task * accept_task;
  70. };
  71. /**
  72. * List of our services.
  73. */
  74. struct ServiceList
  75. {
  76. /**
  77. * This is a doubly-linked list.
  78. */
  79. struct ServiceList *next;
  80. /**
  81. * This is a doubly-linked list.
  82. */
  83. struct ServiceList *prev;
  84. /**
  85. * Linked list of listen sockets associated with this service.
  86. */
  87. struct ServiceListeningInfo *listen_head;
  88. /**
  89. * Linked list of listen sockets associated with this service.
  90. */
  91. struct ServiceListeningInfo *listen_tail;
  92. /**
  93. * Name of the service.
  94. */
  95. char *name;
  96. /**
  97. * Name of the binary used.
  98. */
  99. char *binary;
  100. /**
  101. * Name of the configuration file used.
  102. */
  103. char *config;
  104. /**
  105. * Client to notify upon kill completion (waitpid), NULL
  106. * if we should simply restart the process.
  107. */
  108. struct GNUNET_SERVER_Client *killing_client;
  109. /**
  110. * ID of the request that killed the service (for reporting back).
  111. */
  112. uint64_t killing_client_request_id;
  113. /**
  114. * Process structure pointer of the child.
  115. */
  116. struct GNUNET_OS_Process *proc;
  117. /**
  118. * Process exponential backoff time
  119. */
  120. struct GNUNET_TIME_Relative backoff;
  121. /**
  122. * Absolute time at which the process is scheduled to restart in case of death
  123. */
  124. struct GNUNET_TIME_Absolute restart_at;
  125. /**
  126. * Time we asked the service to shut down (used to calculate time it took
  127. * the service to terminate).
  128. */
  129. struct GNUNET_TIME_Absolute killed_at;
  130. /**
  131. * Is this service to be started by default (or did a client tell us explicitly
  132. * to start it)? #GNUNET_NO if the service is started only upon 'accept' on a
  133. * listen socket or possibly explicitly by a client changing the value.
  134. */
  135. int force_start;
  136. /**
  137. * Should we use pipes to signal this process? (YES for Java binaries and if we
  138. * are on Windoze).
  139. */
  140. int pipe_control;
  141. };
  142. /**
  143. * List of running services.
  144. */
  145. static struct ServiceList *running_head;
  146. /**
  147. * List of running services.
  148. */
  149. static struct ServiceList *running_tail;
  150. /**
  151. * Our configuration
  152. */
  153. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  154. /**
  155. * Command to prepend to each actual command.
  156. */
  157. static char *prefix_command;
  158. /**
  159. * Option to append to each actual command.
  160. */
  161. static char *final_option;
  162. /**
  163. * ID of task called whenever we get a SIGCHILD.
  164. */
  165. static struct GNUNET_SCHEDULER_Task * child_death_task;
  166. /**
  167. * ID of task called whenever the timeout for restarting a child
  168. * expires.
  169. */
  170. static struct GNUNET_SCHEDULER_Task * child_restart_task;
  171. /**
  172. * Pipe used to communicate shutdown via signal.
  173. */
  174. static struct GNUNET_DISK_PipeHandle *sigpipe;
  175. /**
  176. * Are we in shutdown mode?
  177. */
  178. static int in_shutdown;
  179. /**
  180. * Are we starting user services?
  181. */
  182. static int start_user = GNUNET_YES;
  183. /**
  184. * Are we starting system services?
  185. */
  186. static int start_system = GNUNET_YES;
  187. /**
  188. * Handle to our server instance. Our server is a bit special in that
  189. * its service is not immediately stopped once we get a shutdown
  190. * request (since we need to continue service until all of our child
  191. * processes are dead). This handle is used to shut down the server
  192. * (and thus trigger process termination) once all child processes are
  193. * also dead. A special option in the ARM configuration modifies the
  194. * behaviour of the service implementation to not do the shutdown
  195. * immediately.
  196. */
  197. static struct GNUNET_SERVER_Handle *server;
  198. /**
  199. * Context for notifications we need to send to our clients.
  200. */
  201. static struct GNUNET_SERVER_NotificationContext *notifier;
  202. /**
  203. * Transmit a status result message.
  204. *
  205. * @param cls a `unit16_t *` with message type
  206. * @param size number of bytes available in @a buf
  207. * @param buf where to copy the message, NULL on error
  208. * @return number of bytes copied to @a buf
  209. */
  210. static size_t
  211. write_result (void *cls, size_t size, void *buf)
  212. {
  213. struct GNUNET_ARM_ResultMessage *msg = cls;
  214. size_t msize;
  215. if (NULL == buf)
  216. {
  217. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  218. _("Could not send status result to client\n"));
  219. GNUNET_free (msg);
  220. return 0; /* error, not much we can do */
  221. }
  222. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  223. "Sending status response %u to client\n",
  224. (unsigned int) msg->result);
  225. msize = msg->arm_msg.header.size;
  226. GNUNET_assert (size >= msize);
  227. msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
  228. msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
  229. msg->result = htonl (msg->result);
  230. msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
  231. memcpy (buf, msg, msize);
  232. GNUNET_free (msg);
  233. return msize;
  234. }
  235. /**
  236. * Transmit the list of running services.
  237. *
  238. * @param cls pointer to `struct GNUNET_ARM_ListResultMessage` with the message
  239. * @param size number of bytes available in @a buf
  240. * @param buf where to copy the message, NULL on error
  241. * @return number of bytes copied to @a buf
  242. */
  243. static size_t
  244. write_list_result (void *cls, size_t size, void *buf)
  245. {
  246. struct GNUNET_ARM_ListResultMessage *msg = cls;
  247. size_t rslt_size;
  248. if (NULL == buf)
  249. {
  250. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  251. _("Could not send list result to client\n"));
  252. GNUNET_free (msg);
  253. return 0; /* error, not much we can do */
  254. }
  255. rslt_size = msg->arm_msg.header.size;
  256. GNUNET_assert (size >= rslt_size);
  257. msg->arm_msg.header.size = htons (msg->arm_msg.header.size);
  258. msg->arm_msg.header.type = htons (msg->arm_msg.header.type);
  259. msg->arm_msg.request_id = GNUNET_htonll (msg->arm_msg.request_id);
  260. msg->count = htons (msg->count);
  261. memcpy (buf, msg, rslt_size);
  262. GNUNET_free (msg);
  263. return rslt_size;
  264. }
  265. /**
  266. * Signal our client that we will start or stop the
  267. * service.
  268. *
  269. * @param client who is being signalled
  270. * @param name name of the service
  271. * @param request_id id of the request that is being responded to.
  272. * @param result message type to send
  273. * @return NULL if it was not found
  274. */
  275. static void
  276. signal_result (struct GNUNET_SERVER_Client *client,
  277. const char *name,
  278. uint64_t request_id,
  279. enum GNUNET_ARM_Result result)
  280. {
  281. struct GNUNET_ARM_ResultMessage *msg;
  282. size_t msize;
  283. msize = sizeof (struct GNUNET_ARM_ResultMessage);
  284. msg = GNUNET_malloc (msize);
  285. msg->arm_msg.header.size = msize;
  286. msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_RESULT;
  287. msg->result = result;
  288. msg->arm_msg.request_id = request_id;
  289. GNUNET_SERVER_notify_transmit_ready (client, msize,
  290. GNUNET_TIME_UNIT_FOREVER_REL,
  291. write_result, msg);
  292. }
  293. /**
  294. * Tell all clients about status change of a service.
  295. *
  296. * @param name name of the service
  297. * @param status message type to send
  298. * @param unicast if not NULL, send to this client only.
  299. * otherwise, send to all clients in the notifier
  300. */
  301. static void
  302. broadcast_status (const char *name,
  303. enum GNUNET_ARM_ServiceStatus status,
  304. struct GNUNET_SERVER_Client *unicast)
  305. {
  306. struct GNUNET_ARM_StatusMessage *msg;
  307. size_t namelen;
  308. if (NULL == notifier)
  309. return;
  310. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  311. "Sending status %u of service `%s' to client\n",
  312. (unsigned int) status, name);
  313. namelen = strlen (name);
  314. msg = GNUNET_malloc (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
  315. msg->header.size = htons (sizeof (struct GNUNET_ARM_StatusMessage) + namelen + 1);
  316. msg->header.type = htons (GNUNET_MESSAGE_TYPE_ARM_STATUS);
  317. msg->status = htonl ((uint32_t) (status));
  318. memcpy ((char *) &msg[1], name, namelen + 1);
  319. if (NULL == unicast)
  320. GNUNET_SERVER_notification_context_broadcast (notifier,
  321. (struct GNUNET_MessageHeader *) msg, GNUNET_YES);
  322. else
  323. GNUNET_SERVER_notification_context_unicast (notifier, unicast,
  324. (const struct GNUNET_MessageHeader *) msg, GNUNET_NO);
  325. GNUNET_free (msg);
  326. }
  327. /**
  328. * Actually start the process for the given service.
  329. *
  330. * @param sl identifies service to start
  331. * @param client that asked to start the service (may be NULL)
  332. * @param request_id id of the request in response to which the process is
  333. * being started. 0 if starting was not requested.
  334. */
  335. static void
  336. start_process (struct ServiceList *sl,
  337. struct GNUNET_SERVER_Client *client,
  338. uint64_t request_id)
  339. {
  340. char *loprefix;
  341. char *options;
  342. char *optpos;
  343. char *optend;
  344. const char *next;
  345. int use_debug;
  346. char b;
  347. char *val;
  348. struct ServiceListeningInfo *sli;
  349. SOCKTYPE *lsocks;
  350. unsigned int ls;
  351. char *binary;
  352. char *quotedbinary;
  353. /* calculate listen socket list */
  354. lsocks = NULL;
  355. ls = 0;
  356. for (sli = sl->listen_head; NULL != sli; sli = sli->next)
  357. {
  358. GNUNET_array_append (lsocks, ls,
  359. GNUNET_NETWORK_get_fd (sli->listen_socket));
  360. if (sli->accept_task != NULL)
  361. {
  362. GNUNET_SCHEDULER_cancel (sli->accept_task);
  363. sli->accept_task = NULL;
  364. }
  365. }
  366. #if WINDOWS
  367. GNUNET_array_append (lsocks, ls, INVALID_SOCKET);
  368. #else
  369. GNUNET_array_append (lsocks, ls, -1);
  370. #endif
  371. /* obtain configuration */
  372. if (GNUNET_OK !=
  373. GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "PREFIX",
  374. &loprefix))
  375. loprefix = GNUNET_strdup (prefix_command);
  376. if (GNUNET_OK !=
  377. GNUNET_CONFIGURATION_get_value_string (cfg, sl->name, "OPTIONS",
  378. &options))
  379. {
  380. options = GNUNET_strdup (final_option);
  381. if (NULL == strstr (options, "%"))
  382. {
  383. /* replace '{}' with service name */
  384. while (NULL != (optpos = strstr (options, "{}")))
  385. {
  386. optpos[0] = '%';
  387. optpos[1] = 's';
  388. GNUNET_asprintf (&optpos, options, sl->name);
  389. GNUNET_free (options);
  390. options = optpos;
  391. }
  392. /* replace '$PATH' with value associated with "PATH" */
  393. while (NULL != (optpos = strstr (options, "$")))
  394. {
  395. optend = optpos + 1;
  396. while (isupper ((unsigned char) *optend))
  397. optend++;
  398. b = *optend;
  399. if ('\0' == b)
  400. next = "";
  401. else
  402. next = optend + 1;
  403. *optend = '\0';
  404. if (GNUNET_OK !=
  405. GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
  406. optpos + 1, &val))
  407. val = GNUNET_strdup ("");
  408. *optpos = '\0';
  409. GNUNET_asprintf (&optpos, "%s%s%c%s", options, val, b, next);
  410. GNUNET_free (options);
  411. GNUNET_free (val);
  412. options = optpos;
  413. }
  414. }
  415. }
  416. use_debug = GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name, "DEBUG");
  417. /* actually start process */
  418. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  419. "Starting service `%s' using binary `%s' and configuration `%s'\n",
  420. sl->name, sl->binary, sl->config);
  421. binary = GNUNET_OS_get_libexec_binary_path (sl->binary);
  422. GNUNET_asprintf (&quotedbinary,
  423. "\"%s\"",
  424. binary);
  425. GNUNET_assert (NULL == sl->proc);
  426. if (GNUNET_YES == use_debug)
  427. {
  428. if (NULL == sl->config)
  429. sl->proc =
  430. GNUNET_OS_start_process_s (sl->pipe_control,
  431. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  432. lsocks, loprefix, quotedbinary, "-L",
  433. "DEBUG", options, NULL);
  434. else
  435. sl->proc =
  436. GNUNET_OS_start_process_s (sl->pipe_control,
  437. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  438. lsocks, loprefix, quotedbinary, "-c",
  439. sl->config, "-L",
  440. "DEBUG", options, NULL);
  441. }
  442. else
  443. {
  444. if (NULL == sl->config)
  445. sl->proc =
  446. GNUNET_OS_start_process_s (sl->pipe_control,
  447. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  448. lsocks, loprefix, quotedbinary,
  449. options, NULL);
  450. else
  451. sl->proc =
  452. GNUNET_OS_start_process_s (sl->pipe_control,
  453. GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  454. lsocks, loprefix, quotedbinary, "-c",
  455. sl->config, options, NULL);
  456. }
  457. GNUNET_free (binary);
  458. GNUNET_free (quotedbinary);
  459. if (sl->proc == NULL)
  460. {
  461. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  462. _("Failed to start service `%s'\n"),
  463. sl->name);
  464. if (client)
  465. signal_result (client,
  466. sl->name,
  467. request_id,
  468. GNUNET_ARM_RESULT_START_FAILED);
  469. }
  470. else
  471. {
  472. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  473. _("Starting service `%s'\n"),
  474. sl->name);
  475. broadcast_status (sl->name, GNUNET_ARM_SERVICE_STARTING, NULL);
  476. if (client)
  477. signal_result (client, sl->name, request_id, GNUNET_ARM_RESULT_STARTING);
  478. }
  479. /* clean up */
  480. GNUNET_free (loprefix);
  481. GNUNET_free (options);
  482. GNUNET_array_grow (lsocks, ls, 0);
  483. }
  484. /**
  485. * Find the process with the given service
  486. * name in the given list and return it.
  487. *
  488. * @param name which service entry to look up
  489. * @return NULL if it was not found
  490. */
  491. static struct ServiceList *
  492. find_service (const char *name)
  493. {
  494. struct ServiceList *sl;
  495. sl = running_head;
  496. while (sl != NULL)
  497. {
  498. if (0 == strcasecmp (sl->name, name))
  499. return sl;
  500. sl = sl->next;
  501. }
  502. return NULL;
  503. }
  504. /**
  505. * First connection has come to the listening socket associated with the service,
  506. * create the service in order to relay the incoming connection to it
  507. *
  508. * @param cls callback data, `struct ServiceListeningInfo` describing a listen socket
  509. * @param tc context
  510. */
  511. static void
  512. accept_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  513. {
  514. struct ServiceListeningInfo *sli = cls;
  515. struct ServiceList *sl = sli->sl;
  516. sli->accept_task = NULL;
  517. GNUNET_assert (GNUNET_NO == in_shutdown);
  518. if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
  519. return;
  520. start_process (sl, NULL, 0);
  521. }
  522. /**
  523. * Creating a listening socket for each of the service's addresses and
  524. * wait for the first incoming connection to it
  525. *
  526. * @param sa address associated with the service
  527. * @param addr_len length of @a sa
  528. * @param sl service entry for the service in question
  529. */
  530. static void
  531. create_listen_socket (struct sockaddr *sa, socklen_t addr_len,
  532. struct ServiceList *sl)
  533. {
  534. static int on = 1;
  535. struct GNUNET_NETWORK_Handle *sock;
  536. struct ServiceListeningInfo *sli;
  537. #ifndef WINDOWS
  538. int match_uid;
  539. int match_gid;
  540. #endif
  541. switch (sa->sa_family)
  542. {
  543. case AF_INET:
  544. sock = GNUNET_NETWORK_socket_create (PF_INET, SOCK_STREAM, 0);
  545. break;
  546. case AF_INET6:
  547. sock = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
  548. break;
  549. case AF_UNIX:
  550. if (strcmp (GNUNET_a2s (sa, addr_len), "@") == 0) /* Do not bind to blank UNIX path! */
  551. return;
  552. sock = GNUNET_NETWORK_socket_create (PF_UNIX, SOCK_STREAM, 0);
  553. break;
  554. default:
  555. GNUNET_break (0);
  556. sock = NULL;
  557. errno = EAFNOSUPPORT;
  558. break;
  559. }
  560. if (NULL == sock)
  561. {
  562. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  563. _("Unable to create socket for service `%s': %s\n"),
  564. sl->name, STRERROR (errno));
  565. GNUNET_free (sa);
  566. return;
  567. }
  568. if (GNUNET_NETWORK_socket_setsockopt
  569. (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
  570. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  571. "setsockopt");
  572. #ifdef IPV6_V6ONLY
  573. if ((sa->sa_family == AF_INET6) &&
  574. (GNUNET_NETWORK_socket_setsockopt
  575. (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)) != GNUNET_OK))
  576. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  577. "setsockopt");
  578. #endif
  579. if (GNUNET_OK !=
  580. GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) sa, addr_len))
  581. {
  582. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  583. _
  584. ("Unable to bind listening socket for service `%s' to address `%s': %s\n"),
  585. sl->name, GNUNET_a2s (sa, addr_len), STRERROR (errno));
  586. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
  587. GNUNET_free (sa);
  588. return;
  589. }
  590. #ifndef WINDOWS
  591. if ((AF_UNIX == sa->sa_family)
  592. #ifdef LINUX
  593. /* Permission settings are not required when abstract sockets are used */
  594. && ('\0' != ((const struct sockaddr_un *)sa)->sun_path[0])
  595. #endif
  596. )
  597. {
  598. match_uid =
  599. GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name,
  600. "UNIX_MATCH_UID");
  601. match_gid =
  602. GNUNET_CONFIGURATION_get_value_yesno (cfg, sl->name,
  603. "UNIX_MATCH_GID");
  604. GNUNET_DISK_fix_permissions (((const struct sockaddr_un *)sa)->sun_path,
  605. match_uid,
  606. match_gid);
  607. }
  608. #endif
  609. if (GNUNET_NETWORK_socket_listen (sock, 5) != GNUNET_OK)
  610. {
  611. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "listen");
  612. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
  613. GNUNET_free (sa);
  614. return;
  615. }
  616. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  617. _("ARM now monitors connections to service `%s' at `%s'\n"),
  618. sl->name, GNUNET_a2s (sa, addr_len));
  619. sli = GNUNET_new (struct ServiceListeningInfo);
  620. sli->service_addr = sa;
  621. sli->service_addr_len = addr_len;
  622. sli->listen_socket = sock;
  623. sli->sl = sl;
  624. sli->accept_task =
  625. GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, sock,
  626. &accept_connection, sli);
  627. GNUNET_CONTAINER_DLL_insert (sl->listen_head, sl->listen_tail, sli);
  628. }
  629. /**
  630. * Remove and free an entry in the service list. Listen sockets
  631. * must have already been cleaned up. Only to be called during shutdown.
  632. *
  633. * @param sl entry to free
  634. */
  635. static void
  636. free_service (struct ServiceList *sl)
  637. {
  638. GNUNET_assert (GNUNET_YES == in_shutdown);
  639. GNUNET_CONTAINER_DLL_remove (running_head, running_tail, sl);
  640. GNUNET_assert (NULL == sl->listen_head);
  641. GNUNET_free_non_null (sl->config);
  642. GNUNET_free_non_null (sl->binary);
  643. GNUNET_free (sl->name);
  644. GNUNET_free (sl);
  645. }
  646. /**
  647. * Handle START-message.
  648. *
  649. * @param cls closure (always NULL)
  650. * @param client identification of the client
  651. * @param message the actual message
  652. * @return #GNUNET_OK to keep the connection open,
  653. * #GNUNET_SYSERR to close it (signal serious error)
  654. */
  655. static void
  656. handle_start (void *cls,
  657. struct GNUNET_SERVER_Client *client,
  658. const struct GNUNET_MessageHeader *message)
  659. {
  660. const char *servicename;
  661. struct ServiceList *sl;
  662. uint16_t size;
  663. uint64_t request_id;
  664. struct GNUNET_ARM_Message *amsg;
  665. amsg = (struct GNUNET_ARM_Message *) message;
  666. request_id = GNUNET_ntohll (amsg->request_id);
  667. size = ntohs (amsg->header.size);
  668. size -= sizeof (struct GNUNET_ARM_Message);
  669. servicename = (const char *) &amsg[1];
  670. if ((size == 0) || (servicename[size - 1] != '\0'))
  671. {
  672. GNUNET_break (0);
  673. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  674. return;
  675. }
  676. if (GNUNET_YES == in_shutdown)
  677. {
  678. signal_result (client, servicename, request_id,
  679. GNUNET_ARM_RESULT_IN_SHUTDOWN);
  680. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  681. return;
  682. }
  683. sl = find_service (servicename);
  684. if (NULL == sl)
  685. {
  686. signal_result (client, servicename, request_id,
  687. GNUNET_ARM_RESULT_IS_NOT_KNOWN);
  688. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  689. return;
  690. }
  691. sl->force_start = GNUNET_YES;
  692. if (NULL != sl->proc)
  693. {
  694. signal_result (client, servicename, request_id,
  695. GNUNET_ARM_RESULT_IS_STARTED_ALREADY);
  696. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  697. return;
  698. }
  699. start_process (sl, client, request_id);
  700. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  701. }
  702. /**
  703. * Start a shutdown sequence.
  704. *
  705. * @param cls closure (refers to service)
  706. * @param tc task context
  707. */
  708. static void
  709. trigger_shutdown (void *cls,
  710. const struct GNUNET_SCHEDULER_TaskContext *tc)
  711. {
  712. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  713. "Triggering shutdown\n");
  714. GNUNET_SCHEDULER_shutdown ();
  715. }
  716. /**
  717. * Handle STOP-message.
  718. *
  719. * @param cls closure (always NULL)
  720. * @param client identification of the client
  721. * @param message the actual message
  722. * @return #GNUNET_OK to keep the connection open,
  723. * #GNUNET_SYSERR to close it (signal serious error)
  724. */
  725. static void
  726. handle_stop (void *cls,
  727. struct GNUNET_SERVER_Client *client,
  728. const struct GNUNET_MessageHeader *message)
  729. {
  730. struct ServiceList *sl;
  731. const char *servicename;
  732. uint16_t size;
  733. uint64_t request_id;
  734. struct GNUNET_ARM_Message *amsg;
  735. amsg = (struct GNUNET_ARM_Message *) message;
  736. request_id = GNUNET_ntohll (amsg->request_id);
  737. size = ntohs (amsg->header.size);
  738. size -= sizeof (struct GNUNET_ARM_Message);
  739. servicename = (const char *) &amsg[1];
  740. if ((size == 0) || (servicename[size - 1] != '\0'))
  741. {
  742. GNUNET_break (0);
  743. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  744. return;
  745. }
  746. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  747. _("Preparing to stop `%s'\n"),
  748. servicename);
  749. if (0 == strcasecmp (servicename, "arm"))
  750. {
  751. broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
  752. signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_STOPPING);
  753. GNUNET_SERVER_client_persist_ (client);
  754. GNUNET_SCHEDULER_add_now (trigger_shutdown, NULL);
  755. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  756. return;
  757. }
  758. sl = find_service (servicename);
  759. if (sl == NULL)
  760. {
  761. signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IS_NOT_KNOWN);
  762. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  763. return;
  764. }
  765. sl->force_start = GNUNET_NO;
  766. if (GNUNET_YES == in_shutdown)
  767. {
  768. /* shutdown in progress */
  769. signal_result (client, servicename, request_id, GNUNET_ARM_RESULT_IN_SHUTDOWN);
  770. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  771. return;
  772. }
  773. if (NULL != sl->killing_client)
  774. {
  775. /* killing already in progress */
  776. signal_result (client, servicename, request_id,
  777. GNUNET_ARM_RESULT_IS_STOPPING_ALREADY);
  778. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  779. return;
  780. }
  781. if (NULL == sl->proc)
  782. {
  783. /* process is down */
  784. signal_result (client, servicename, request_id,
  785. GNUNET_ARM_RESULT_IS_STOPPED_ALREADY);
  786. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  787. return;
  788. }
  789. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  790. "Sending kill signal to service `%s', waiting for process to die.\n",
  791. servicename);
  792. broadcast_status (servicename, GNUNET_ARM_SERVICE_STOPPING, NULL);
  793. /* no signal_start - only when it's STOPPED */
  794. sl->killed_at = GNUNET_TIME_absolute_get ();
  795. if (0 != GNUNET_OS_process_kill (sl->proc, GNUNET_TERM_SIG))
  796. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  797. sl->killing_client = client;
  798. sl->killing_client_request_id = request_id;
  799. GNUNET_SERVER_client_keep (client);
  800. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  801. }
  802. /**
  803. * Handle LIST-message.
  804. *
  805. * @param cls closure (always NULL)
  806. * @param client identification of the client
  807. * @param message the actual message
  808. */
  809. static void
  810. handle_list (void *cls, struct GNUNET_SERVER_Client *client,
  811. const struct GNUNET_MessageHeader *message)
  812. {
  813. struct GNUNET_ARM_ListResultMessage *msg;
  814. struct GNUNET_ARM_Message *request;
  815. size_t string_list_size;
  816. size_t total_size;
  817. struct ServiceList *sl;
  818. uint16_t count;
  819. if (NULL == client)
  820. return;
  821. request = (struct GNUNET_ARM_Message *) message;
  822. GNUNET_break (0 == ntohl (request->reserved));
  823. count = 0;
  824. string_list_size = 0;
  825. /* first count the running processes get their name's size */
  826. for (sl = running_head; NULL != sl; sl = sl->next)
  827. {
  828. if (NULL != sl->proc)
  829. {
  830. string_list_size += strlen (sl->name);
  831. string_list_size += strlen (sl->binary);
  832. string_list_size += 4;
  833. count++;
  834. }
  835. }
  836. total_size = sizeof (struct GNUNET_ARM_ListResultMessage)
  837. + string_list_size;
  838. msg = GNUNET_malloc (total_size);
  839. msg->arm_msg.header.size = total_size;
  840. msg->arm_msg.header.type = GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT;
  841. msg->arm_msg.reserved = htonl (0);
  842. msg->arm_msg.request_id = GNUNET_ntohll (request->request_id);
  843. msg->count = count;
  844. char *pos = (char *)&msg[1];
  845. for (sl = running_head; NULL != sl; sl = sl->next)
  846. {
  847. if (NULL != sl->proc)
  848. {
  849. size_t s = strlen (sl->name) + strlen (sl->binary) + 4;
  850. GNUNET_snprintf (pos, s, "%s (%s)", sl->name, sl->binary);
  851. pos += s;
  852. }
  853. }
  854. GNUNET_SERVER_notify_transmit_ready (client,
  855. total_size,
  856. GNUNET_TIME_UNIT_FOREVER_REL,
  857. &write_list_result, msg);
  858. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  859. }
  860. /**
  861. * We are done with everything. Stop remaining
  862. * tasks, signal handler and the server.
  863. */
  864. static void
  865. do_shutdown ()
  866. {
  867. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last shutdown phase\n");
  868. if (NULL != notifier)
  869. {
  870. GNUNET_SERVER_notification_context_destroy (notifier);
  871. notifier = NULL;
  872. }
  873. if (NULL != server)
  874. {
  875. GNUNET_SERVER_destroy (server);
  876. server = NULL;
  877. }
  878. if (NULL != child_death_task)
  879. {
  880. GNUNET_SCHEDULER_cancel (child_death_task);
  881. child_death_task = NULL;
  882. }
  883. }
  884. /**
  885. * Count how many services are still active.
  886. *
  887. * @param running_head list of services
  888. * @return number of active services found
  889. */
  890. static unsigned int
  891. list_count (struct ServiceList *running_head)
  892. {
  893. struct ServiceList *i;
  894. unsigned int res = 0;
  895. for (res = 0, i = running_head; i; i = i->next, res++)
  896. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  897. "%s\n",
  898. i->name);
  899. return res;
  900. }
  901. /**
  902. * Task run for shutdown.
  903. *
  904. * @param cls closure, NULL if we need to self-restart
  905. * @param tc context
  906. */
  907. static void
  908. shutdown_task (void *cls,
  909. const struct GNUNET_SCHEDULER_TaskContext *tc)
  910. {
  911. struct ServiceList *pos;
  912. struct ServiceList *nxt;
  913. struct ServiceListeningInfo *sli;
  914. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  915. "First shutdown phase\n");
  916. if (NULL != child_restart_task)
  917. {
  918. GNUNET_SCHEDULER_cancel (child_restart_task);
  919. child_restart_task = NULL;
  920. }
  921. in_shutdown = GNUNET_YES;
  922. /* first, stop listening */
  923. for (pos = running_head; NULL != pos; pos = pos->next)
  924. {
  925. while (NULL != (sli = pos->listen_head))
  926. {
  927. GNUNET_CONTAINER_DLL_remove (pos->listen_head,
  928. pos->listen_tail, sli);
  929. if (sli->accept_task != NULL)
  930. {
  931. GNUNET_SCHEDULER_cancel (sli->accept_task);
  932. sli->accept_task = NULL;
  933. }
  934. GNUNET_break (GNUNET_OK ==
  935. GNUNET_NETWORK_socket_close (sli->listen_socket));
  936. GNUNET_free (sli->service_addr);
  937. GNUNET_free (sli);
  938. }
  939. }
  940. /* then, shutdown all existing service processes */
  941. nxt = running_head;
  942. while (NULL != (pos = nxt))
  943. {
  944. nxt = pos->next;
  945. if (pos->proc != NULL)
  946. {
  947. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  948. "Stopping service `%s'\n",
  949. pos->name);
  950. pos->killed_at = GNUNET_TIME_absolute_get ();
  951. if (0 != GNUNET_OS_process_kill (pos->proc, GNUNET_TERM_SIG))
  952. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  953. }
  954. else
  955. {
  956. free_service (pos);
  957. }
  958. }
  959. /* finally, should all service processes be already gone, terminate for real */
  960. if (running_head == NULL)
  961. do_shutdown ();
  962. else
  963. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  964. "Delaying shutdown, have %u childs still running\n",
  965. list_count (running_head));
  966. }
  967. /**
  968. * Task run whenever it is time to restart a child that died.
  969. *
  970. * @param cls closure, always NULL
  971. * @param tc context
  972. */
  973. static void
  974. delayed_restart_task (void *cls,
  975. const struct GNUNET_SCHEDULER_TaskContext *tc)
  976. {
  977. struct ServiceList *sl;
  978. struct GNUNET_TIME_Relative lowestRestartDelay;
  979. struct ServiceListeningInfo *sli;
  980. child_restart_task = NULL;
  981. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  982. return;
  983. GNUNET_assert (GNUNET_NO == in_shutdown);
  984. lowestRestartDelay = GNUNET_TIME_UNIT_FOREVER_REL;
  985. /* check for services that need to be restarted due to
  986. * configuration changes or because the last restart failed */
  987. for (sl = running_head; NULL != sl; sl = sl->next)
  988. {
  989. if (NULL != sl->proc)
  990. continue;
  991. /* service is currently not running */
  992. if (0 == GNUNET_TIME_absolute_get_remaining (sl->restart_at).rel_value_us)
  993. {
  994. /* restart is now allowed */
  995. if (sl->force_start)
  996. {
  997. /* process should run by default, start immediately */
  998. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  999. _("Restarting service `%s'.\n"),
  1000. sl->name);
  1001. start_process (sl, NULL, 0);
  1002. }
  1003. else
  1004. {
  1005. /* process is run on-demand, ensure it is re-started if there is demand */
  1006. for (sli = sl->listen_head; NULL != sli; sli = sli->next)
  1007. if (NULL == sli->accept_task)
  1008. {
  1009. /* accept was actually paused, so start it again */
  1010. sli->accept_task =
  1011. GNUNET_SCHEDULER_add_read_net
  1012. (GNUNET_TIME_UNIT_FOREVER_REL, sli->listen_socket,
  1013. &accept_connection, sli);
  1014. }
  1015. }
  1016. }
  1017. else
  1018. {
  1019. /* update calculation for earliest time to reactivate a service */
  1020. lowestRestartDelay =
  1021. GNUNET_TIME_relative_min (lowestRestartDelay,
  1022. GNUNET_TIME_absolute_get_remaining
  1023. (sl->restart_at));
  1024. }
  1025. }
  1026. if (lowestRestartDelay.rel_value_us != GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
  1027. {
  1028. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1029. "Will restart process in %s\n",
  1030. GNUNET_STRINGS_relative_time_to_string (lowestRestartDelay, GNUNET_YES));
  1031. child_restart_task =
  1032. GNUNET_SCHEDULER_add_delayed_with_priority (lowestRestartDelay,
  1033. GNUNET_SCHEDULER_PRIORITY_IDLE,
  1034. &delayed_restart_task, NULL);
  1035. }
  1036. }
  1037. /**
  1038. * Task triggered whenever we receive a SIGCHLD (child
  1039. * process died).
  1040. *
  1041. * @param cls closure, NULL if we need to self-restart
  1042. * @param tc context
  1043. */
  1044. static void
  1045. maint_child_death (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  1046. {
  1047. struct ServiceList *pos;
  1048. struct ServiceList *next;
  1049. struct ServiceListeningInfo *sli;
  1050. const char *statstr;
  1051. int statcode;
  1052. int ret;
  1053. char c[16];
  1054. enum GNUNET_OS_ProcessStatusType statusType;
  1055. unsigned long statusCode;
  1056. const struct GNUNET_DISK_FileHandle *pr;
  1057. pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
  1058. child_death_task = NULL;
  1059. if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
  1060. {
  1061. /* shutdown scheduled us, ignore! */
  1062. child_death_task =
  1063. GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
  1064. pr, &maint_child_death, NULL);
  1065. return;
  1066. }
  1067. /* consume the signal */
  1068. GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
  1069. /* check for services that died (WAITPID) */
  1070. next = running_head;
  1071. while (NULL != (pos = next))
  1072. {
  1073. next = pos->next;
  1074. if (pos->proc == NULL)
  1075. {
  1076. if (GNUNET_YES == in_shutdown)
  1077. free_service (pos);
  1078. continue;
  1079. }
  1080. if ((GNUNET_SYSERR ==
  1081. (ret =
  1082. GNUNET_OS_process_status (pos->proc, &statusType, &statusCode)))
  1083. || ((ret == GNUNET_NO) || (statusType == GNUNET_OS_PROCESS_STOPPED)
  1084. || (statusType == GNUNET_OS_PROCESS_RUNNING)))
  1085. continue;
  1086. if (statusType == GNUNET_OS_PROCESS_EXITED)
  1087. {
  1088. statstr = _( /* process termination method */ "exit");
  1089. statcode = statusCode;
  1090. }
  1091. else if (statusType == GNUNET_OS_PROCESS_SIGNALED)
  1092. {
  1093. statstr = _( /* process termination method */ "signal");
  1094. statcode = statusCode;
  1095. }
  1096. else
  1097. {
  1098. statstr = _( /* process termination method */ "unknown");
  1099. statcode = 0;
  1100. }
  1101. if (0 != pos->killed_at.abs_value_us)
  1102. {
  1103. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1104. _("Service `%s' took %s to terminate\n"),
  1105. pos->name,
  1106. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->killed_at), GNUNET_YES));
  1107. }
  1108. GNUNET_OS_process_destroy (pos->proc);
  1109. pos->proc = NULL;
  1110. broadcast_status (pos->name, GNUNET_ARM_SERVICE_STOPPED, NULL);
  1111. if (NULL != pos->killing_client)
  1112. {
  1113. signal_result (pos->killing_client, pos->name,
  1114. pos->killing_client_request_id, GNUNET_ARM_RESULT_STOPPED);
  1115. GNUNET_SERVER_client_drop (pos->killing_client);
  1116. pos->killing_client = NULL;
  1117. pos->killing_client_request_id = 0;
  1118. }
  1119. if (GNUNET_YES != in_shutdown)
  1120. {
  1121. if ((statusType == GNUNET_OS_PROCESS_EXITED) && (statcode == 0))
  1122. {
  1123. /* process terminated normally, allow restart at any time */
  1124. pos->restart_at.abs_value_us = 0;
  1125. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1126. _("Service `%s' terminated normally, will restart at any time\n"),
  1127. pos->name);
  1128. /* process can still be re-started on-demand, ensure it is re-started if there is demand */
  1129. for (sli = pos->listen_head; NULL != sli; sli = sli->next)
  1130. {
  1131. GNUNET_break (NULL == sli->accept_task);
  1132. sli->accept_task =
  1133. GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  1134. sli->listen_socket, &accept_connection, sli);
  1135. }
  1136. }
  1137. else
  1138. {
  1139. if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  1140. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1141. _("Service `%s' terminated with status %s/%d, will restart in %s\n"),
  1142. pos->name, statstr, statcode,
  1143. GNUNET_STRINGS_relative_time_to_string (pos->backoff, GNUNET_YES));
  1144. /* schedule restart */
  1145. pos->restart_at = GNUNET_TIME_relative_to_absolute (pos->backoff);
  1146. pos->backoff = GNUNET_TIME_STD_BACKOFF (pos->backoff);
  1147. if (NULL != child_restart_task)
  1148. GNUNET_SCHEDULER_cancel (child_restart_task);
  1149. child_restart_task = GNUNET_SCHEDULER_add_with_priority (
  1150. GNUNET_SCHEDULER_PRIORITY_IDLE, &delayed_restart_task, NULL);
  1151. }
  1152. }
  1153. else
  1154. {
  1155. free_service (pos);
  1156. }
  1157. }
  1158. child_death_task = GNUNET_SCHEDULER_add_read_file (
  1159. GNUNET_TIME_UNIT_FOREVER_REL, pr, &maint_child_death, NULL);
  1160. if ((NULL == running_head) && (GNUNET_YES == in_shutdown))
  1161. do_shutdown ();
  1162. else if (GNUNET_YES == in_shutdown)
  1163. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1164. "Delaying shutdown after child's death, still have %u children\n",
  1165. list_count (running_head));
  1166. }
  1167. /**
  1168. * Signal handler called for SIGCHLD. Triggers the
  1169. * respective handler by writing to the trigger pipe.
  1170. */
  1171. static void
  1172. sighandler_child_death ()
  1173. {
  1174. static char c;
  1175. int old_errno = errno; /* back-up errno */
  1176. GNUNET_break (1 ==
  1177. GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
  1178. (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
  1179. &c, sizeof (c)));
  1180. errno = old_errno; /* restore errno */
  1181. }
  1182. /**
  1183. * Setup our service record for the given section in the configuration file
  1184. * (assuming the section is for a service).
  1185. *
  1186. * @param cls unused
  1187. * @param section a section in the configuration file
  1188. * @return #GNUNET_OK (continue)
  1189. */
  1190. static void
  1191. setup_service (void *cls, const char *section)
  1192. {
  1193. struct ServiceList *sl;
  1194. char *binary;
  1195. char *config;
  1196. struct stat sbuf;
  1197. struct sockaddr **addrs;
  1198. socklen_t *addr_lens;
  1199. int ret;
  1200. unsigned int i;
  1201. if (strcasecmp (section, "arm") == 0)
  1202. return;
  1203. if (GNUNET_OK !=
  1204. GNUNET_CONFIGURATION_get_value_string (cfg, section, "BINARY", &binary))
  1205. {
  1206. /* not a service section */
  1207. return;
  1208. }
  1209. if ((GNUNET_YES ==
  1210. GNUNET_CONFIGURATION_have_value (cfg, section, "USER_SERVICE")) &&
  1211. (GNUNET_YES ==
  1212. GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "USER_SERVICE")))
  1213. {
  1214. if (GNUNET_NO == start_user)
  1215. {
  1216. GNUNET_free (binary);
  1217. return; /* user service, and we don't deal with those */
  1218. }
  1219. }
  1220. else
  1221. {
  1222. if (GNUNET_NO == start_system)
  1223. {
  1224. GNUNET_free (binary);
  1225. return; /* system service, and we don't deal with those */
  1226. }
  1227. }
  1228. sl = find_service (section);
  1229. if (NULL != sl)
  1230. {
  1231. /* got the same section twice!? */
  1232. GNUNET_break (0);
  1233. GNUNET_free (binary);
  1234. return;
  1235. }
  1236. config = NULL;
  1237. if (( (GNUNET_OK !=
  1238. GNUNET_CONFIGURATION_get_value_filename (cfg, section, "CONFIG",
  1239. &config)) &&
  1240. (GNUNET_OK !=
  1241. GNUNET_CONFIGURATION_get_value_filename (cfg, "PATHS", "DEFAULTCONFIG",
  1242. &config)) ) ||
  1243. (0 != STAT (config, &sbuf)))
  1244. {
  1245. if (NULL != config)
  1246. {
  1247. GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
  1248. section, "CONFIG",
  1249. STRERROR (errno));
  1250. GNUNET_free (config);
  1251. config = NULL;
  1252. }
  1253. }
  1254. sl = GNUNET_new (struct ServiceList);
  1255. sl->name = GNUNET_strdup (section);
  1256. sl->binary = binary;
  1257. sl->config = config;
  1258. sl->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
  1259. sl->restart_at = GNUNET_TIME_UNIT_FOREVER_ABS;
  1260. #if WINDOWS
  1261. sl->pipe_control = GNUNET_YES;
  1262. #else
  1263. if (GNUNET_CONFIGURATION_have_value (cfg, section, "PIPECONTROL"))
  1264. sl->pipe_control = GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "PIPECONTROL");
  1265. #endif
  1266. GNUNET_CONTAINER_DLL_insert (running_head, running_tail, sl);
  1267. if (GNUNET_YES ==
  1268. GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "FORCESTART"))
  1269. {
  1270. sl->force_start = GNUNET_YES;
  1271. return;
  1272. }
  1273. else
  1274. {
  1275. if (GNUNET_YES !=
  1276. GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "AUTOSTART"))
  1277. return;
  1278. }
  1279. if (0 >= (ret = GNUNET_SERVICE_get_server_addresses (section, cfg,
  1280. &addrs, &addr_lens)))
  1281. return;
  1282. /* this will free (or capture) addrs[i] */
  1283. for (i = 0; i < ret; i++)
  1284. create_listen_socket (addrs[i], addr_lens[i], sl);
  1285. GNUNET_free (addrs);
  1286. GNUNET_free (addr_lens);
  1287. }
  1288. /**
  1289. * A client connected, add it to the notification context.
  1290. *
  1291. * @param cls closure
  1292. * @param client identification of the client
  1293. */
  1294. static void
  1295. handle_client_connecting (void *cls, struct GNUNET_SERVER_Client *client)
  1296. {
  1297. /* All clients are considered to be of the "monitor" kind
  1298. * (that is, they don't affect ARM shutdown).
  1299. */
  1300. if (NULL != client)
  1301. GNUNET_SERVER_client_mark_monitor (client);
  1302. }
  1303. /**
  1304. * Handle MONITOR-message.
  1305. *
  1306. * @param cls closure (always NULL)
  1307. * @param client identification of the client
  1308. * @param message the actual message
  1309. * @return #GNUNET_OK to keep the connection open,
  1310. * #GNUNET_SYSERR to close it (signal serious error)
  1311. */
  1312. static void
  1313. handle_monitor (void *cls, struct GNUNET_SERVER_Client *client,
  1314. const struct GNUNET_MessageHeader *message)
  1315. {
  1316. /* Removal is handled by the server implementation, internally. */
  1317. if ((NULL != client) && (NULL != notifier))
  1318. {
  1319. GNUNET_SERVER_notification_context_add (notifier, client);
  1320. broadcast_status ("arm", GNUNET_ARM_SERVICE_MONITORING_STARTED, client);
  1321. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  1322. }
  1323. }
  1324. /**
  1325. * Process arm requests.
  1326. *
  1327. * @param cls closure
  1328. * @param serv the initialized server
  1329. * @param c configuration to use
  1330. */
  1331. static void
  1332. run (void *cls, struct GNUNET_SERVER_Handle *serv,
  1333. const struct GNUNET_CONFIGURATION_Handle *c)
  1334. {
  1335. static const struct GNUNET_SERVER_MessageHandler handlers[] = {
  1336. {&handle_start, NULL, GNUNET_MESSAGE_TYPE_ARM_START, 0},
  1337. {&handle_stop, NULL, GNUNET_MESSAGE_TYPE_ARM_STOP, 0},
  1338. {&handle_monitor, NULL, GNUNET_MESSAGE_TYPE_ARM_MONITOR,
  1339. sizeof (struct GNUNET_MessageHeader)},
  1340. {&handle_list, NULL, GNUNET_MESSAGE_TYPE_ARM_LIST,
  1341. sizeof (struct GNUNET_ARM_Message)},
  1342. {NULL, NULL, 0, 0}
  1343. };
  1344. struct ServiceList *sl;
  1345. cfg = c;
  1346. server = serv;
  1347. GNUNET_assert (NULL != serv);
  1348. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
  1349. &shutdown_task,
  1350. NULL);
  1351. child_death_task =
  1352. GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
  1353. GNUNET_DISK_pipe_handle (sigpipe,
  1354. GNUNET_DISK_PIPE_END_READ),
  1355. &maint_child_death, NULL);
  1356. if (GNUNET_OK !=
  1357. GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_PREFIX",
  1358. &prefix_command))
  1359. prefix_command = GNUNET_strdup ("");
  1360. if (GNUNET_OK !=
  1361. GNUNET_CONFIGURATION_get_value_string (cfg, "ARM", "GLOBAL_POSTFIX",
  1362. &final_option))
  1363. final_option = GNUNET_strdup ("");
  1364. if (GNUNET_YES ==
  1365. GNUNET_CONFIGURATION_get_value_yesno (cfg, "ARM", "USER_ONLY"))
  1366. {
  1367. GNUNET_break (GNUNET_YES == start_user);
  1368. start_system = GNUNET_NO;
  1369. }
  1370. if (GNUNET_YES ==
  1371. GNUNET_CONFIGURATION_get_value_yesno (cfg, "ARM", "SYSTEM_ONLY"))
  1372. {
  1373. GNUNET_break (GNUNET_YES == start_system);
  1374. start_user = GNUNET_NO;
  1375. }
  1376. GNUNET_CONFIGURATION_iterate_sections (cfg, &setup_service, NULL);
  1377. /* start default services... */
  1378. for (sl = running_head; NULL != sl; sl = sl->next)
  1379. if (GNUNET_YES == sl->force_start)
  1380. start_process (sl, NULL, 0);
  1381. notifier
  1382. = GNUNET_SERVER_notification_context_create (server,
  1383. MAX_NOTIFY_QUEUE);
  1384. GNUNET_SERVER_connect_notify (server,
  1385. &handle_client_connecting, NULL);
  1386. /* process client requests */
  1387. GNUNET_SERVER_add_handlers (server,
  1388. handlers);
  1389. }
  1390. /**
  1391. * The main function for the arm service.
  1392. *
  1393. * @param argc number of arguments from the command line
  1394. * @param argv command line arguments
  1395. * @return 0 ok, 1 on error
  1396. */
  1397. int
  1398. main (int argc, char *const *argv)
  1399. {
  1400. int ret;
  1401. struct GNUNET_SIGNAL_Context *shc_chld;
  1402. sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
  1403. GNUNET_assert (sigpipe != NULL);
  1404. shc_chld =
  1405. GNUNET_SIGNAL_handler_install (GNUNET_SIGCHLD, &sighandler_child_death);
  1406. ret =
  1407. (GNUNET_OK ==
  1408. GNUNET_SERVICE_run (argc, argv, "arm",
  1409. GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN, &run, NULL)) ? 0 : 1;
  1410. GNUNET_SIGNAL_handler_uninstall (shc_chld);
  1411. shc_chld = NULL;
  1412. GNUNET_DISK_pipe_close (sigpipe);
  1413. sigpipe = NULL;
  1414. return ret;
  1415. }
  1416. #ifdef LINUX
  1417. #include <malloc.h>
  1418. /**
  1419. * MINIMIZE heap size (way below 128k) since this process doesn't need much.
  1420. */
  1421. void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
  1422. {
  1423. mallopt (M_TRIM_THRESHOLD, 4 * 1024);
  1424. mallopt (M_TOP_PAD, 1 * 1024);
  1425. malloc_trim (0);
  1426. }
  1427. #endif
  1428. /* end of gnunet-service-arm.c */