2 This file is part of GNUnet.
3 (C) 2009, 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/service.c
23 * @brief functions related to starting services
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_configuration_lib.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_directories.h"
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_service_lib.h"
45 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
47 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
49 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
52 /* ******************* access control ******************** */
55 * @brief IPV4 network in CIDR notation.
62 struct in_addr network;
67 struct in_addr netmask;
72 * @brief network in CIDR notation for IPV6.
79 struct in6_addr network;
84 struct in6_addr netmask;
89 GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg);
92 GNUNET_SPEEDUP_stop_ (void);
96 * Parse a network specification. The argument specifies
97 * a list of networks. The format is
98 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
99 * with a semicolon). The network must be given in dotted-decimal
100 * notation. The netmask can be given in CIDR notation (/16) or
101 * in dotted-decimal (/255.255.0.0).
103 * @param routeList a string specifying the forbidden networks
104 * @return the converted list, NULL if the synatx is flawed
106 static struct IPv4NetworkSet *
107 parse_ipv4_specification (const char *routeList)
115 unsigned int temps[8];
117 struct IPv4NetworkSet *result;
119 if (NULL == routeList)
121 len = strlen (routeList);
125 for (i = 0; i < len; i++)
126 if (routeList[i] == ';')
128 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
134 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
135 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
136 &temps[6], &temps[7]);
139 for (j = 0; j < 8; j++)
142 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
144 GNUNET_free (result);
147 result[i].network.s_addr =
148 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
150 result[i].netmask.s_addr =
151 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
153 while (routeList[pos] != ';')
159 /* try second notation */
161 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
162 &temps[2], &temps[3], &slash);
165 for (j = 0; j < 4; j++)
168 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
170 GNUNET_free (result);
173 result[i].network.s_addr =
174 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
176 if ((slash <= 32) && (slash >= 0))
178 result[i].netmask.s_addr = 0;
181 result[i].netmask.s_addr =
182 (result[i].netmask.s_addr >> 1) + 0x80000000;
185 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
186 while (';' != routeList[pos])
194 LOG (GNUNET_ERROR_TYPE_ERROR,
195 _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
197 GNUNET_free (result);
198 return NULL; /* error */
201 /* try third notation */
204 SSCANF (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
205 &temps[2], &temps[3]);
208 for (j = 0; j < 4; j++)
211 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
213 GNUNET_free (result);
216 result[i].network.s_addr =
217 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
219 result[i].netmask.s_addr = 0;
222 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
225 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
226 while (routeList[pos] != ';')
232 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
234 GNUNET_free (result);
235 return NULL; /* error */
237 if (pos < strlen (routeList))
239 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
241 GNUNET_free (result);
242 return NULL; /* oops */
244 return result; /* ok */
249 * Parse a network specification. The argument specifies
250 * a list of networks. The format is
251 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
252 * with a semicolon). The network must be given in colon-hex
253 * notation. The netmask must be given in CIDR notation (/16) or
254 * can be omitted to specify a single host.
256 * @param routeListX a string specifying the forbidden networks
257 * @return the converted list, NULL if the synatx is flawed
259 static struct IPv6NetworkSet *
260 parse_ipv6_specification (const char *routeListX)
270 struct IPv6NetworkSet *result;
275 if (NULL == routeListX)
277 len = strlen (routeListX);
280 routeList = GNUNET_strdup (routeListX);
282 for (i = 0; i < len; i++)
283 if (';' == routeList[i])
285 if (';' != routeList[len - 1])
287 LOG (GNUNET_ERROR_TYPE_ERROR,
288 _("Invalid network notation (does not end with ';': `%s')\n"),
290 GNUNET_free (routeList);
294 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
300 while (';' != routeList[pos])
303 while ((slash >= start) && (routeList[slash] != '/'))
307 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
312 routeList[pos] = '\0';
313 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
317 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128))
320 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"),
321 &routeList[slash + 1]);
325 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
327 GNUNET_free (result);
328 GNUNET_free (routeList);
334 result[i].netmask.s6_addr[off++] = 0xFF;
339 result[i].netmask.s6_addr[off] =
340 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
345 routeList[slash] = '\0';
346 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
350 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"),
351 &routeList[slash + 1]);
353 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
354 GNUNET_free (result);
355 GNUNET_free (routeList);
361 GNUNET_free (routeList);
367 * Check if the given IP address is in the list of IP addresses.
369 * @param list a list of networks
370 * @param add the IP to check (in network byte order)
371 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
374 check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
381 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
383 if ((add->s_addr & list[i].netmask.s_addr) ==
384 (list[i].network.s_addr & list[i].netmask.s_addr))
393 * Check if the given IP address is in the list of IP addresses.
395 * @param list a list of networks
396 * @param ip the IP to check (in network byte order)
397 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
400 check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip)
404 struct in6_addr zero;
408 memset (&zero, 0, sizeof (struct in6_addr));
411 while (0 != memcmp (&zero, &list[i].network, sizeof (struct in6_addr)))
413 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
414 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
415 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
426 /* ****************** service struct ****************** */
430 * Context for "service_task".
432 struct GNUNET_SERVICE_Context
437 const struct GNUNET_CONFIGURATION_Handle *cfg;
440 * Handle for the server.
442 struct GNUNET_SERVER_Handle *server;
445 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
448 struct sockaddr **addrs;
451 * Name of our service.
453 const char *service_name;
456 * Main service-specific task to run.
458 GNUNET_SERVICE_Main task;
466 * IPv4 addresses that are not allowed to connect.
468 struct IPv4NetworkSet *v4_denied;
471 * IPv6 addresses that are not allowed to connect.
473 struct IPv6NetworkSet *v6_denied;
476 * IPv4 addresses that are allowed to connect (if not
477 * set, all are allowed).
479 struct IPv4NetworkSet *v4_allowed;
482 * IPv6 addresses that are allowed to connect (if not
483 * set, all are allowed).
485 struct IPv6NetworkSet *v6_allowed;
488 * My (default) message handlers. Adjusted copy
491 struct GNUNET_SERVER_MessageHandler *my_handlers;
494 * Array of the lengths of the entries in addrs.
499 * NULL-terminated array of listen sockets we should take over.
501 struct GNUNET_NETWORK_Handle **lsocks;
504 * Task ID of the shutdown task.
506 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
509 * Idle timeout for server.
511 struct GNUNET_TIME_Relative timeout;
514 * Overall success/failure of the service start.
519 * If we are daemonizing, this FD is set to the
520 * pipe to the parent. Send '.' if we started
521 * ok, '!' if not. -1 if we are not daemonizing.
523 int ready_confirm_fd;
526 * Do we close connections if we receive messages
527 * for which we have no handler?
532 * Do we require a matching UID for UNIX domain socket connections?
533 * GNUNET_NO means that the UID does not have to match (however,
534 * "match_gid" may still impose other access control checks).
539 * Do we require a matching GID for UNIX domain socket connections?
540 * Ignored if "match_uid" is GNUNET_YES. Note that this is about
541 * checking that the client's UID is in our group OR that the
542 * client's GID is our GID. If both "match_gid" and "match_uid" are
543 * "GNUNET_NO", all users on the local system have access.
550 enum GNUNET_SERVICE_Options options;
555 /* ****************** message handlers ****************** */
558 * Send a 'TEST' message back to the client.
560 * @param cls the 'struct GNUNET_SERVER_Client' to send TEST to
561 * @param size number of bytes available in 'buf'
562 * @param buf where to copy the message
563 * @return number of bytes written to 'buf'
566 write_test (void *cls, size_t size, void *buf)
568 struct GNUNET_SERVER_Client *client = cls;
569 struct GNUNET_MessageHeader *msg;
571 if (size < sizeof (struct GNUNET_MessageHeader))
573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
574 return 0; /* client disconnected */
576 msg = (struct GNUNET_MessageHeader *) buf;
577 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
578 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
579 GNUNET_SERVER_receive_done (client, GNUNET_OK);
580 return sizeof (struct GNUNET_MessageHeader);
585 * Handler for TEST message.
587 * @param cls closure (refers to service)
588 * @param client identification of the client
589 * @param message the actual message
592 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
593 const struct GNUNET_MessageHeader *message)
595 /* simply bounce message back to acknowledge */
597 GNUNET_SERVER_notify_transmit_ready (client,
598 sizeof (struct GNUNET_MessageHeader),
599 GNUNET_TIME_UNIT_FOREVER_REL,
600 &write_test, client))
601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
606 * Default handlers for all services. Will be copied and the
607 * "callback_cls" fields will be replaced with the specific service
610 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
611 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
612 sizeof (struct GNUNET_MessageHeader)},
617 /* ****************** service core routines ************** */
621 * Check if access to the service is allowed from the given address.
624 * @param uc credentials, if available, otherwise NULL
625 * @param addr address
626 * @param addrlen length of address
627 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
628 * for unknown address family (will be denied).
631 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
632 const struct sockaddr *addr, socklen_t addrlen)
634 struct GNUNET_SERVICE_Context *sctx = cls;
635 const struct sockaddr_in *i4;
636 const struct sockaddr_in6 *i6;
639 switch (addr->sa_family)
642 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
643 i4 = (const struct sockaddr_in *) addr;
644 ret = ((NULL == sctx->v4_allowed) ||
645 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
646 ((NULL == sctx->v4_denied) ||
647 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
650 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
651 i6 = (const struct sockaddr_in6 *) addr;
652 ret = ((NULL == sctx->v6_allowed) ||
653 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
654 ((NULL == sctx->v6_denied) ||
655 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
659 ret = GNUNET_OK; /* always OK for now */
660 if (GNUNET_YES == sctx->match_uid)
662 /* UID match required */
663 ret = (NULL != uc) && ( (0 == uc->uid) || (uc->uid == geteuid ()) );
665 else if ( (GNUNET_YES == sctx->match_gid) &&
668 (uc->uid != geteuid ()) ) ) )
670 /* group match required and UID does not match */
673 /* no credentials, group match not possible */
681 if (uc->gid != getegid())
683 /* default group did not match, but maybe the user is in our group, let's check */
684 grp = getgrgid (getegid ());
687 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "getgrgid");
691 for (i=0; NULL != grp->gr_mem[i]; i++)
693 struct passwd *nam = getpwnam (grp->gr_mem[i]);
695 continue; /* name in group that is not in user DB !? */
696 if (nam->pw_uid == uc->uid)
698 /* yes, uid is in our group, allow! */
706 if (GNUNET_NO == ret)
707 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
708 (NULL == uc) ? -1 : uc->uid, (NULL == uc) ? -1 : uc->gid);
712 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
714 return GNUNET_SYSERR;
716 if (GNUNET_OK != ret)
718 LOG (GNUNET_ERROR_TYPE_WARNING,
719 _("Access from `%s' denied to service `%s'\n"),
720 GNUNET_a2s (addr, addrlen),
728 * Get the name of the file where we will
729 * write the PID of the service.
731 * @param sctx service context
732 * @return name of the file for the process ID
735 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
740 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
748 * Parse an IPv4 access control list.
750 * @param ret location where to write the ACL (set)
751 * @param sctx service context to use to get the configuration
752 * @param option name of the ACL option to parse
753 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
757 process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
762 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
767 GNUNET_break (GNUNET_OK ==
768 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
771 if (NULL == (*ret = parse_ipv4_specification (opt)))
773 LOG (GNUNET_ERROR_TYPE_WARNING,
774 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
775 opt, sctx->service_name, option);
777 return GNUNET_SYSERR;
785 * Parse an IPv6 access control list.
787 * @param ret location where to write the ACL (set)
788 * @param sctx service context to use to get the configuration
789 * @param option name of the ACL option to parse
790 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
794 process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
799 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
804 GNUNET_break (GNUNET_OK ==
805 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
808 if (NULL == (*ret = parse_ipv6_specification (opt)))
810 LOG (GNUNET_ERROR_TYPE_WARNING,
811 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
812 opt, sctx->service_name, option);
814 return GNUNET_SYSERR;
822 * Add the given UNIX domain path as an address to the
823 * list (as the first entry).
825 * @param saddrs array to update
826 * @param saddrlens where to store the address length
827 * @param unixpath path to add
830 add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
831 const char *unixpath)
834 struct sockaddr_un *un;
837 un = GNUNET_malloc (sizeof (struct sockaddr_un));
838 un->sun_family = AF_UNIX;
839 slen = strlen (unixpath) + 1;
840 if (slen >= sizeof (un->sun_path))
841 slen = sizeof (un->sun_path) - 1;
842 memcpy (un->sun_path, unixpath, slen);
843 un->sun_path[slen] = '\0';
844 slen = sizeof (struct sockaddr_un);
846 un->sun_path[0] = '\0';
848 #if HAVE_SOCKADDR_IN_SIN_LEN
849 un->sun_len = (u_char) slen;
851 *saddrs = (struct sockaddr *) un;
854 /* this function should never be called
855 * unless AF_UNIX is defined! */
862 * Get the list of addresses that a server for the given service
865 * @param service_name name of the service
866 * @param cfg configuration (which specifies the addresses)
867 * @param addrs set (call by reference) to an array of pointers to the
868 * addresses the server should bind to and listen on; the
869 * array will be NULL-terminated (on success)
870 * @param addr_lens set (call by reference) to an array of the lengths
871 * of the respective 'struct sockaddr' struct in the 'addrs'
873 * @return number of addresses found on success,
874 * GNUNET_SYSERR if the configuration
875 * did not specify reasonable finding information or
876 * if it specified a hostname that could not be resolved;
877 * GNUNET_NO if the number of addresses configured is
878 * zero (in this case, '*addrs' and '*addr_lens' will be
882 GNUNET_SERVICE_get_server_addresses (const char *service_name,
883 const struct GNUNET_CONFIGURATION_Handle
884 *cfg, struct sockaddr ***addrs,
885 socklen_t ** addr_lens)
888 struct GNUNET_NETWORK_Handle *desc;
889 unsigned long long port;
891 struct addrinfo hints;
892 struct addrinfo *res;
893 struct addrinfo *pos;
894 struct addrinfo *next;
898 struct sockaddr **saddrs;
899 socklen_t *saddrlens;
905 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
909 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
910 return GNUNET_SYSERR;
913 disablev6 = GNUNET_NO;
917 /* probe IPv6 support */
918 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
921 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
924 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
925 return GNUNET_SYSERR;
927 LOG (GNUNET_ERROR_TYPE_INFO,
929 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
930 service_name, STRERROR (errno));
931 disablev6 = GNUNET_YES;
935 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
941 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
944 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
947 LOG (GNUNET_ERROR_TYPE_ERROR,
948 _("Require valid port number for service `%s' in configuration!\n"),
953 LOG (GNUNET_ERROR_TYPE_ERROR,
954 _("Require valid port number for service `%s' in configuration!\n"),
956 return GNUNET_SYSERR;
960 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
962 GNUNET_break (GNUNET_OK ==
963 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
964 "BINDTO", &hostname));
972 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
974 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
976 (0 < strlen (unixpath)))
978 /* probe UNIX support */
979 struct sockaddr_un s_un;
981 if (strlen (unixpath) >= sizeof (s_un.sun_path))
983 LOG (GNUNET_ERROR_TYPE_WARNING,
984 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
985 (unsigned long long) sizeof (s_un.sun_path));
986 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
987 LOG (GNUNET_ERROR_TYPE_INFO,
988 _("Using `%s' instead\n"), unixpath);
992 if (NULL != unixpath)
994 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
997 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
1000 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
1001 GNUNET_free_non_null (hostname);
1002 GNUNET_free (unixpath);
1003 return GNUNET_SYSERR;
1005 LOG (GNUNET_ERROR_TYPE_INFO,
1007 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
1008 service_name, STRERROR (errno));
1009 GNUNET_free (unixpath);
1014 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1020 if ((0 == port) && (NULL == unixpath))
1022 LOG (GNUNET_ERROR_TYPE_ERROR,
1024 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
1026 GNUNET_free_non_null (hostname);
1027 return GNUNET_SYSERR;
1031 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
1032 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
1033 add_unixpath (saddrs, saddrlens, unixpath);
1034 GNUNET_free_non_null (unixpath);
1035 GNUNET_free_non_null (hostname);
1037 *addr_lens = saddrlens;
1041 if (NULL != hostname)
1043 LOG (GNUNET_ERROR_TYPE_DEBUG,
1044 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
1046 memset (&hints, 0, sizeof (struct addrinfo));
1048 hints.ai_family = AF_INET;
1049 hints.ai_protocol = IPPROTO_TCP;
1050 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1053 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
1054 gai_strerror (ret));
1055 GNUNET_free (hostname);
1056 GNUNET_free_non_null (unixpath);
1057 return GNUNET_SYSERR;
1061 while (NULL != (pos = next))
1063 next = pos->ai_next;
1064 if ((disablev6) && (pos->ai_family == AF_INET6))
1070 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
1071 disablev6 ? "IPv4 " : "", hostname);
1073 GNUNET_free (hostname);
1074 GNUNET_free_non_null (unixpath);
1075 return GNUNET_SYSERR;
1078 if (NULL != unixpath)
1080 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1081 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1083 if (NULL != unixpath)
1085 add_unixpath (saddrs, saddrlens, unixpath);
1089 while (NULL != (pos = next))
1091 next = pos->ai_next;
1092 if ((disablev6) && (AF_INET6 == pos->ai_family))
1094 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1095 continue; /* not TCP */
1096 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1097 continue; /* huh? */
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1099 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1100 if (AF_INET == pos->ai_family)
1102 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
1103 saddrlens[i] = pos->ai_addrlen;
1104 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1105 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1106 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1110 GNUNET_assert (AF_INET6 == pos->ai_family);
1111 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
1112 saddrlens[i] = pos->ai_addrlen;
1113 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1114 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1115 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1119 GNUNET_free (hostname);
1125 /* will bind against everything, just set port */
1130 if (NULL != unixpath)
1133 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1134 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1135 if (NULL != unixpath)
1137 add_unixpath (saddrs, saddrlens, unixpath);
1140 saddrlens[i] = sizeof (struct sockaddr_in);
1141 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1142 #if HAVE_SOCKADDR_IN_SIN_LEN
1143 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1145 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1146 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1152 if (NULL != unixpath)
1154 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1155 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1157 if (NULL != unixpath)
1159 add_unixpath (saddrs, saddrlens, unixpath);
1162 saddrlens[i] = sizeof (struct sockaddr_in6);
1163 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1164 #if HAVE_SOCKADDR_IN_SIN_LEN
1165 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1167 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1168 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1170 saddrlens[i] = sizeof (struct sockaddr_in);
1171 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1172 #if HAVE_SOCKADDR_IN_SIN_LEN
1173 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1175 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1176 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1179 GNUNET_free_non_null (unixpath);
1181 *addr_lens = saddrlens;
1188 * Read listen sockets from the parent process (ARM).
1190 * @param sctx service context to initialize
1191 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
1192 * and GNUNET_SYSERR on error.
1195 receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1197 const char *env_buf;
1203 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1204 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
1206 /* Using W32 API directly here, because this pipe will
1207 * never be used outside of this function, and it's just too much of a bother
1208 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1210 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1211 if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
1220 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1221 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
1224 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
1227 for (i = 0; i < count; i++)
1229 WSAPROTOCOL_INFOA pi;
1233 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1234 if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) )
1236 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1237 if ( (0 == ret) || (sizeof (pi) != rd))
1239 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
1240 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1241 if (NULL == sctx->lsocks[i])
1243 else if (i == count - 1)
1248 sctx->lsocks[count] = NULL;
1253 CloseHandle (lsocks_pipe);
1257 LOG (GNUNET_ERROR_TYPE_ERROR,
1258 _("Could not access a pre-bound socket, will try to bind myself\n"));
1259 for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++)
1260 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
1261 GNUNET_free_non_null (sctx->lsocks);
1262 sctx->lsocks = NULL;
1271 * Setup addr, addrlen, idle_timeout
1272 * based on configuration!
1274 * Configuration may specify:
1275 * - PORT (where to bind to for TCP)
1276 * - UNIXPATH (where to bind to for UNIX domain sockets)
1277 * - TIMEOUT (after how many ms does an inactive service timeout);
1278 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1279 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1280 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1281 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1282 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1283 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1285 * @param sctx service context to initialize
1286 * @return GNUNET_OK if configuration succeeded
1289 setup_service (struct GNUNET_SERVICE_Context *sctx)
1291 struct GNUNET_TIME_Relative idleout;
1302 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, "TIMEOUT"))
1305 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->service_name,
1306 "TIMEOUT", &idleout))
1308 LOG (GNUNET_ERROR_TYPE_ERROR,
1309 _("Specified value for `%s' of service `%s' is invalid\n"),
1310 "TIMEOUT", sctx->service_name);
1311 return GNUNET_SYSERR;
1313 sctx->timeout = idleout;
1316 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1318 if (GNUNET_CONFIGURATION_have_value
1319 (sctx->cfg, sctx->service_name, "TOLERANT"))
1321 if (GNUNET_SYSERR ==
1323 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1326 LOG (GNUNET_ERROR_TYPE_ERROR,
1327 _("Specified value for `%s' of service `%s' is invalid\n"),
1328 "TOLERANT", sctx->service_name);
1329 return GNUNET_SYSERR;
1333 tolerant = GNUNET_NO;
1337 if ((NULL != (lpid = getenv ("LISTEN_PID"))) &&
1338 (1 == SSCANF (lpid, "%u", &pid)) && (getpid () == (pid_t) pid) &&
1339 (NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1340 (1 == SSCANF (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1341 (cnt + 4 < FD_SETSIZE))
1344 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1347 flags = fcntl (3 + cnt, F_GETFD);
1348 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1350 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1352 LOG (GNUNET_ERROR_TYPE_ERROR,
1354 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1355 (unsigned int) 3 + cnt);
1357 while (sctx->lsocks[cnt] != NULL)
1358 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1359 GNUNET_free (sctx->lsocks);
1360 sctx->lsocks = NULL;
1364 unsetenv ("LISTEN_PID");
1365 unsetenv ("LISTEN_FDS");
1368 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1370 receive_sockets_from_parent (sctx);
1371 putenv ("GNUNET_OS_READ_LSOCKS=");
1375 if ((NULL == sctx->lsocks) &&
1377 GNUNET_SERVICE_get_server_addresses (sctx->service_name, sctx->cfg,
1378 &sctx->addrs, &sctx->addrlens)))
1379 return GNUNET_SYSERR;
1380 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1382 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1385 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1387 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1388 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1389 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1390 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1397 * Get the name of the user that'll be used
1398 * to provide the service.
1400 * @param sctx service context
1401 * @return value of the 'USERNAME' option
1404 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1409 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
1418 * @param sctx service context
1419 * @param pid PID to write (should be equal to 'getpid()'
1420 * @return GNUNET_OK on success (including no work to be done)
1423 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1431 if (NULL == (pif = get_pid_file_name (sctx)))
1432 return GNUNET_OK; /* no file desired */
1433 user = get_user_name (sctx);
1434 rdir = GNUNET_strdup (pif);
1435 len = strlen (rdir);
1436 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1439 if (0 != ACCESS (rdir, F_OK))
1441 /* we get to create a directory -- and claim it
1443 GNUNET_DISK_directory_create (rdir);
1444 if ((NULL != user) && (0 < strlen (user)))
1445 GNUNET_DISK_file_change_owner (rdir, user);
1447 if (0 != ACCESS (rdir, W_OK | X_OK))
1449 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1451 GNUNET_free_non_null (user);
1453 return GNUNET_SYSERR;
1456 pidfd = FOPEN (pif, "w");
1459 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1461 GNUNET_free_non_null (user);
1462 return GNUNET_SYSERR;
1464 if (0 > FPRINTF (pidfd, "%u", pid))
1465 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1466 GNUNET_break (0 == FCLOSE (pidfd));
1467 if ((NULL != user) && (0 < strlen (user)))
1468 GNUNET_DISK_file_change_owner (pif, user);
1469 GNUNET_free_non_null (user);
1476 * Task run during shutdown. Stops the server/service.
1478 * @param cls the 'struct GNUNET_SERVICE_Context'
1482 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1484 struct GNUNET_SERVICE_Context *service = cls;
1485 struct GNUNET_SERVER_Handle *server = service->server;
1487 service->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1488 if (0 != (service->options & GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN))
1489 GNUNET_SERVER_stop_listening (server);
1491 GNUNET_SERVER_destroy (server);
1496 * Initial task for the service.
1498 * @param cls service context
1502 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1504 struct GNUNET_SERVICE_Context *sctx = cls;
1507 GNUNET_RESOLVER_connect (sctx->cfg);
1508 if (NULL != sctx->lsocks)
1510 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1511 sctx->timeout, sctx->require_found);
1514 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1515 sctx->timeout, sctx->require_found);
1516 if (NULL == sctx->server)
1518 if (NULL != sctx->addrs)
1521 while (NULL != sctx->addrs[i])
1523 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1524 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1528 sctx->ret = GNUNET_SYSERR;
1531 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1533 /* install a task that will kill the server
1534 * process if the scheduler ever gets a shutdown signal */
1535 sctx->shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1538 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1539 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1541 while (NULL != sctx->my_handlers[i].callback)
1542 sctx->my_handlers[i++].callback_cls = sctx;
1543 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1544 if (-1 != sctx->ready_confirm_fd)
1546 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1547 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1548 sctx->ready_confirm_fd = -1;
1549 write_pid_file (sctx, getpid ());
1551 if (NULL != sctx->addrs)
1554 while (NULL != sctx->addrs[i])
1556 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1557 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1561 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1566 * Detach from terminal.
1568 * @param sctx service context
1569 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1572 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1579 if (0 != PIPE (filedes))
1581 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1582 return GNUNET_SYSERR;
1587 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1588 return GNUNET_SYSERR;
1595 GNUNET_break (0 == CLOSE (filedes[1]));
1597 if (1 != READ (filedes[0], &c, sizeof (char)))
1598 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1605 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1608 LOG (GNUNET_ERROR_TYPE_INFO,
1609 _("Service process could not initialize server function\n"));
1612 LOG (GNUNET_ERROR_TYPE_INFO,
1613 _("Service process failed to report status\n"));
1616 exit (1); /* child reported error */
1618 GNUNET_break (0 == CLOSE (0));
1619 GNUNET_break (0 == CLOSE (1));
1620 GNUNET_break (0 == CLOSE (filedes[0]));
1621 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1623 return GNUNET_SYSERR;
1624 /* set stdin/stdout to /dev/null */
1625 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1627 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1628 (void) CLOSE (nullfd);
1629 return GNUNET_SYSERR;
1631 (void) CLOSE (nullfd);
1632 /* Detach from controlling terminal */
1635 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1636 sctx->ready_confirm_fd = filedes[1];
1638 /* FIXME: we probably need to do something else
1639 * elsewhere in order to fork the process itself... */
1649 * @param sctx service context
1650 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1653 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1657 if (NULL == (user = get_user_name (sctx)))
1658 return GNUNET_OK; /* keep */
1663 pws = getpwnam (user);
1666 LOG (GNUNET_ERROR_TYPE_ERROR,
1667 _("Cannot obtain information about user `%s': %s\n"), user,
1668 errno == 0 ? _("No such user") : STRERROR (errno));
1670 return GNUNET_SYSERR;
1672 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1674 (0 != initgroups (user, pws->pw_gid)) ||
1676 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1678 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1679 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1681 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1682 user, STRERROR (errno));
1684 return GNUNET_SYSERR;
1694 * Delete the PID file that was created by our parent.
1696 * @param sctx service context
1699 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1701 char *pif = get_pid_file_name (sctx);
1704 return; /* no PID file */
1705 if (0 != UNLINK (pif))
1706 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1712 * Run a standard GNUnet service startup sequence (initialize loggers
1713 * and configuration, parse options).
1715 * @param argc number of command line arguments
1716 * @param argv command line arguments
1717 * @param service_name our service name
1718 * @param options service options
1719 * @param task main task of the service
1720 * @param task_cls closure for task
1721 * @return GNUNET_SYSERR on error, GNUNET_OK
1722 * if we shutdown nicely
1725 GNUNET_SERVICE_run (int argc, char *const *argv, const char *service_name,
1726 enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_Main task,
1729 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1738 unsigned long long skew_offset;
1739 unsigned long long skew_variance;
1740 long long clock_offset;
1741 struct GNUNET_SERVICE_Context sctx;
1742 struct GNUNET_CONFIGURATION_Handle *cfg;
1744 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1745 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1746 {'d', "daemonize", NULL,
1747 gettext_noop ("do daemonize (detach from terminal)"), 0,
1748 GNUNET_GETOPT_set_one, &do_daemonize},
1749 GNUNET_GETOPT_OPTION_HELP (NULL),
1750 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1751 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1752 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
1753 GNUNET_GETOPT_OPTION_END
1759 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1760 memset (&sctx, 0, sizeof (sctx));
1761 sctx.options = options;
1762 sctx.ready_confirm_fd = -1;
1763 sctx.ret = GNUNET_OK;
1764 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1766 sctx.task_cls = task_cls;
1767 sctx.service_name = service_name;
1768 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1770 /* setup subsystems */
1771 ret = GNUNET_GETOPT_run (service_name, service_options, argc, argv);
1772 if (GNUNET_SYSERR == ret)
1774 if (GNUNET_NO == ret)
1779 if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
1782 GNUNET_DISK_file_test (cfg_fn))
1783 (void) GNUNET_CONFIGURATION_load (cfg, cfg_fn);
1786 (void) GNUNET_CONFIGURATION_load (cfg, NULL);
1787 if (0 != strcmp (cfg_fn, GNUNET_DEFAULT_USER_CONFIG_FILE))
1788 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1789 _("Could not access configuration file `%s'\n"),
1792 if (GNUNET_OK != setup_service (&sctx))
1794 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1796 if (GNUNET_OK != set_user_id (&sctx))
1798 LOG (GNUNET_ERROR_TYPE_DEBUG,
1799 "Service `%s' runs with configuration from `%s'\n", service_name, cfg_fn);
1801 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1802 "SKEW_OFFSET", &skew_offset)) &&
1804 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1805 "SKEW_VARIANCE", &skew_variance)))
1807 clock_offset = skew_offset - skew_variance;
1808 GNUNET_TIME_set_offset (clock_offset);
1809 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1811 /* actually run service */
1813 GNUNET_SCHEDULER_run (&service_task, &sctx);
1814 GNUNET_SPEEDUP_start_ (cfg);
1816 if ((1 == do_daemonize) && (NULL != sctx.server))
1817 pid_file_delete (&sctx);
1818 GNUNET_free_non_null (sctx.my_handlers);
1821 if (-1 != sctx.ready_confirm_fd)
1823 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1824 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1825 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1832 GNUNET_CONFIGURATION_have_value (sctx.cfg, service_name,
1835 GNUNET_CONFIGURATION_get_value_string (sctx.cfg, service_name,
1842 GAUGER (service_name, counter, mi.usmblks, "blocks");
1843 GNUNET_free (counter);
1847 GNUNET_SPEEDUP_stop_ ();
1848 GNUNET_CONFIGURATION_destroy (cfg);
1850 if (NULL != sctx.addrs)
1851 while (NULL != sctx.addrs[i])
1852 GNUNET_free (sctx.addrs[i++]);
1853 GNUNET_free_non_null (sctx.addrs);
1854 GNUNET_free_non_null (sctx.addrlens);
1855 GNUNET_free_non_null (logfile);
1856 GNUNET_free_non_null (loglev);
1857 GNUNET_free (cfg_fn);
1858 GNUNET_free_non_null (sctx.v4_denied);
1859 GNUNET_free_non_null (sctx.v6_denied);
1860 GNUNET_free_non_null (sctx.v4_allowed);
1861 GNUNET_free_non_null (sctx.v6_allowed);
1863 return err ? GNUNET_SYSERR : sctx.ret;
1868 * Run a service startup sequence within an existing
1869 * initialized system.
1871 * @param service_name our service name
1872 * @param cfg configuration to use
1873 * @param options service options
1874 * @return NULL on error, service handle
1876 struct GNUNET_SERVICE_Context *
1877 GNUNET_SERVICE_start (const char *service_name,
1878 const struct GNUNET_CONFIGURATION_Handle *cfg,
1879 enum GNUNET_SERVICE_Options options)
1882 struct GNUNET_SERVICE_Context *sctx;
1884 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1885 sctx->ready_confirm_fd = -1; /* no daemonizing */
1886 sctx->ret = GNUNET_OK;
1887 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1888 sctx->service_name = service_name;
1890 sctx->options = options;
1892 /* setup subsystems */
1893 if (GNUNET_OK != setup_service (sctx))
1895 GNUNET_SERVICE_stop (sctx);
1898 if (NULL != sctx->lsocks)
1900 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1901 sctx->timeout, sctx->require_found);
1904 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1905 sctx->timeout, sctx->require_found);
1907 if (NULL == sctx->server)
1909 GNUNET_SERVICE_stop (sctx);
1912 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1913 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1915 while ((sctx->my_handlers[i].callback != NULL))
1916 sctx->my_handlers[i++].callback_cls = sctx;
1917 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1923 * Obtain the server used by a service. Note that the server must NOT
1924 * be destroyed by the caller.
1926 * @param ctx the service context returned from the start function
1927 * @return handle to the server for this service, NULL if there is none
1929 struct GNUNET_SERVER_Handle *
1930 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1937 * Stop a service that was started with "GNUNET_SERVICE_start".
1939 * @param sctx the service context returned from the start function
1942 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1951 GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name,
1954 GNUNET_CONFIGURATION_get_value_string (sctx->cfg, sctx->service_name,
1961 GAUGER (sctx->service_name, counter, mi.usmblks, "blocks");
1962 GNUNET_free (counter);
1966 if (GNUNET_SCHEDULER_NO_TASK != sctx->shutdown_task)
1968 GNUNET_SCHEDULER_cancel (sctx->shutdown_task);
1969 sctx->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1971 if (NULL != sctx->server)
1972 GNUNET_SERVER_destroy (sctx->server);
1973 GNUNET_free_non_null (sctx->my_handlers);
1974 if (NULL != sctx->addrs)
1977 while (NULL != sctx->addrs[i])
1978 GNUNET_free (sctx->addrs[i++]);
1979 GNUNET_free (sctx->addrs);
1981 GNUNET_free_non_null (sctx->addrlens);
1982 GNUNET_free_non_null (sctx->v4_denied);
1983 GNUNET_free_non_null (sctx->v6_denied);
1984 GNUNET_free_non_null (sctx->v4_allowed);
1985 GNUNET_free_non_null (sctx->v6_allowed);
1990 /* end of service.c */