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 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1509 GNUNET_SPEEDUP_start_ (sctx->cfg);
1510 GNUNET_RESOLVER_connect (sctx->cfg);
1511 if (NULL != sctx->lsocks)
1513 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1514 sctx->timeout, sctx->require_found);
1517 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1518 sctx->timeout, sctx->require_found);
1519 if (NULL == sctx->server)
1521 if (NULL != sctx->addrs)
1524 while (NULL != sctx->addrs[i])
1526 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1527 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1531 sctx->ret = GNUNET_SYSERR;
1534 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1536 /* install a task that will kill the server
1537 * process if the scheduler ever gets a shutdown signal */
1538 sctx->shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1541 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1542 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1544 while (NULL != sctx->my_handlers[i].callback)
1545 sctx->my_handlers[i++].callback_cls = sctx;
1546 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1547 if (-1 != sctx->ready_confirm_fd)
1549 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1550 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1551 sctx->ready_confirm_fd = -1;
1552 write_pid_file (sctx, getpid ());
1554 if (NULL != sctx->addrs)
1557 while (NULL != sctx->addrs[i])
1559 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1560 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1564 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1569 * Detach from terminal.
1571 * @param sctx service context
1572 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1575 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1582 if (0 != PIPE (filedes))
1584 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1585 return GNUNET_SYSERR;
1590 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1591 return GNUNET_SYSERR;
1598 GNUNET_break (0 == CLOSE (filedes[1]));
1600 if (1 != READ (filedes[0], &c, sizeof (char)))
1601 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1608 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1611 LOG (GNUNET_ERROR_TYPE_INFO,
1612 _("Service process could not initialize server function\n"));
1615 LOG (GNUNET_ERROR_TYPE_INFO,
1616 _("Service process failed to report status\n"));
1619 exit (1); /* child reported error */
1621 GNUNET_break (0 == CLOSE (0));
1622 GNUNET_break (0 == CLOSE (1));
1623 GNUNET_break (0 == CLOSE (filedes[0]));
1624 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1626 return GNUNET_SYSERR;
1627 /* set stdin/stdout to /dev/null */
1628 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1630 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1631 (void) CLOSE (nullfd);
1632 return GNUNET_SYSERR;
1634 (void) CLOSE (nullfd);
1635 /* Detach from controlling terminal */
1638 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1639 sctx->ready_confirm_fd = filedes[1];
1641 /* FIXME: we probably need to do something else
1642 * elsewhere in order to fork the process itself... */
1652 * @param sctx service context
1653 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1656 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1660 if (NULL == (user = get_user_name (sctx)))
1661 return GNUNET_OK; /* keep */
1666 pws = getpwnam (user);
1669 LOG (GNUNET_ERROR_TYPE_ERROR,
1670 _("Cannot obtain information about user `%s': %s\n"), user,
1671 errno == 0 ? _("No such user") : STRERROR (errno));
1673 return GNUNET_SYSERR;
1675 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1677 (0 != initgroups (user, pws->pw_gid)) ||
1679 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1681 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1682 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1684 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1685 user, STRERROR (errno));
1687 return GNUNET_SYSERR;
1697 * Delete the PID file that was created by our parent.
1699 * @param sctx service context
1702 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1704 char *pif = get_pid_file_name (sctx);
1707 return; /* no PID file */
1708 if (0 != UNLINK (pif))
1709 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1715 * Run a standard GNUnet service startup sequence (initialize loggers
1716 * and configuration, parse options).
1718 * @param argc number of command line arguments
1719 * @param argv command line arguments
1720 * @param service_name our service name
1721 * @param options service options
1722 * @param task main task of the service
1723 * @param task_cls closure for task
1724 * @return GNUNET_SYSERR on error, GNUNET_OK
1725 * if we shutdown nicely
1728 GNUNET_SERVICE_run (int argc, char *const *argv, const char *service_name,
1729 enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_Main task,
1732 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1741 unsigned long long skew_offset;
1742 unsigned long long skew_variance;
1743 long long clock_offset;
1744 struct GNUNET_SERVICE_Context sctx;
1745 struct GNUNET_CONFIGURATION_Handle *cfg;
1747 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1748 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1749 {'d', "daemonize", NULL,
1750 gettext_noop ("do daemonize (detach from terminal)"), 0,
1751 GNUNET_GETOPT_set_one, &do_daemonize},
1752 GNUNET_GETOPT_OPTION_HELP (NULL),
1753 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1754 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1755 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
1756 GNUNET_GETOPT_OPTION_END
1762 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1763 memset (&sctx, 0, sizeof (sctx));
1764 sctx.options = options;
1765 sctx.ready_confirm_fd = -1;
1766 sctx.ret = GNUNET_OK;
1767 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1769 sctx.task_cls = task_cls;
1770 sctx.service_name = service_name;
1771 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1773 /* setup subsystems */
1774 ret = GNUNET_GETOPT_run (service_name, service_options, argc, argv);
1775 if (GNUNET_SYSERR == ret)
1777 if (GNUNET_NO == ret)
1782 if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
1785 GNUNET_DISK_file_test (cfg_fn))
1786 (void) GNUNET_CONFIGURATION_load (cfg, cfg_fn);
1789 (void) GNUNET_CONFIGURATION_load (cfg, NULL);
1790 if (0 != strcmp (cfg_fn, GNUNET_DEFAULT_USER_CONFIG_FILE))
1791 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1792 _("Could not access configuration file `%s'\n"),
1795 if (GNUNET_OK != setup_service (&sctx))
1797 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1799 if (GNUNET_OK != set_user_id (&sctx))
1801 LOG (GNUNET_ERROR_TYPE_DEBUG,
1802 "Service `%s' runs with configuration from `%s'\n", service_name, cfg_fn);
1804 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1805 "SKEW_OFFSET", &skew_offset)) &&
1807 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1808 "SKEW_VARIANCE", &skew_variance)))
1810 clock_offset = skew_offset - skew_variance;
1811 GNUNET_TIME_set_offset (clock_offset);
1812 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1814 /* actually run service */
1816 GNUNET_SCHEDULER_run (&service_task, &sctx);
1818 if ((1 == do_daemonize) && (NULL != sctx.server))
1819 pid_file_delete (&sctx);
1820 GNUNET_free_non_null (sctx.my_handlers);
1823 if (-1 != sctx.ready_confirm_fd)
1825 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1826 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1827 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1834 GNUNET_CONFIGURATION_have_value (sctx.cfg, service_name,
1837 GNUNET_CONFIGURATION_get_value_string (sctx.cfg, service_name,
1844 GAUGER (service_name, counter, mi.usmblks, "blocks");
1845 GNUNET_free (counter);
1849 GNUNET_SPEEDUP_stop_ ();
1850 GNUNET_CONFIGURATION_destroy (cfg);
1852 if (NULL != sctx.addrs)
1853 while (NULL != sctx.addrs[i])
1854 GNUNET_free (sctx.addrs[i++]);
1855 GNUNET_free_non_null (sctx.addrs);
1856 GNUNET_free_non_null (sctx.addrlens);
1857 GNUNET_free_non_null (logfile);
1858 GNUNET_free_non_null (loglev);
1859 GNUNET_free (cfg_fn);
1860 GNUNET_free_non_null (sctx.v4_denied);
1861 GNUNET_free_non_null (sctx.v6_denied);
1862 GNUNET_free_non_null (sctx.v4_allowed);
1863 GNUNET_free_non_null (sctx.v6_allowed);
1865 return err ? GNUNET_SYSERR : sctx.ret;
1870 * Run a service startup sequence within an existing
1871 * initialized system.
1873 * @param service_name our service name
1874 * @param cfg configuration to use
1875 * @param options service options
1876 * @return NULL on error, service handle
1878 struct GNUNET_SERVICE_Context *
1879 GNUNET_SERVICE_start (const char *service_name,
1880 const struct GNUNET_CONFIGURATION_Handle *cfg,
1881 enum GNUNET_SERVICE_Options options)
1884 struct GNUNET_SERVICE_Context *sctx;
1886 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1887 sctx->ready_confirm_fd = -1; /* no daemonizing */
1888 sctx->ret = GNUNET_OK;
1889 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1890 sctx->service_name = service_name;
1892 sctx->options = options;
1894 /* setup subsystems */
1895 if (GNUNET_OK != setup_service (sctx))
1897 GNUNET_SERVICE_stop (sctx);
1900 if (NULL != sctx->lsocks)
1902 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1903 sctx->timeout, sctx->require_found);
1906 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1907 sctx->timeout, sctx->require_found);
1909 if (NULL == sctx->server)
1911 GNUNET_SERVICE_stop (sctx);
1914 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1915 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1917 while ((sctx->my_handlers[i].callback != NULL))
1918 sctx->my_handlers[i++].callback_cls = sctx;
1919 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1925 * Obtain the server used by a service. Note that the server must NOT
1926 * be destroyed by the caller.
1928 * @param ctx the service context returned from the start function
1929 * @return handle to the server for this service, NULL if there is none
1931 struct GNUNET_SERVER_Handle *
1932 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1939 * Stop a service that was started with "GNUNET_SERVICE_start".
1941 * @param sctx the service context returned from the start function
1944 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1953 GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name,
1956 GNUNET_CONFIGURATION_get_value_string (sctx->cfg, sctx->service_name,
1963 GAUGER (sctx->service_name, counter, mi.usmblks, "blocks");
1964 GNUNET_free (counter);
1968 if (GNUNET_SCHEDULER_NO_TASK != sctx->shutdown_task)
1970 GNUNET_SCHEDULER_cancel (sctx->shutdown_task);
1971 sctx->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1973 if (NULL != sctx->server)
1974 GNUNET_SERVER_destroy (sctx->server);
1975 GNUNET_free_non_null (sctx->my_handlers);
1976 if (NULL != sctx->addrs)
1979 while (NULL != sctx->addrs[i])
1980 GNUNET_free (sctx->addrs[i++]);
1981 GNUNET_free (sctx->addrs);
1983 GNUNET_free_non_null (sctx->addrlens);
1984 GNUNET_free_non_null (sctx->v4_denied);
1985 GNUNET_free_non_null (sctx->v6_denied);
1986 GNUNET_free_non_null (sctx->v4_allowed);
1987 GNUNET_free_non_null (sctx->v6_allowed);
1992 /* end of service.c */