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 3, 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 * Start task that may speed up our system clock artificially
91 * @param cfg configuration to use
92 * @return GNUNET_OK on success, GNUNET_SYSERR if the speedup was not configured
95 GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg);
98 * Stop tasks that modify clock behavior.
101 GNUNET_SPEEDUP_stop_ (void);
105 * Parse a network specification. The argument specifies
106 * a list of networks. The format is
107 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
108 * with a semicolon). The network must be given in dotted-decimal
109 * notation. The netmask can be given in CIDR notation (/16) or
110 * in dotted-decimal (/255.255.0.0).
112 * @param routeList a string specifying the forbidden networks
113 * @return the converted list, NULL if the synatx is flawed
115 static struct IPv4NetworkSet *
116 parse_ipv4_specification (const char *routeList)
124 unsigned int temps[8];
126 struct IPv4NetworkSet *result;
128 if (NULL == routeList)
130 len = strlen (routeList);
134 for (i = 0; i < len; i++)
135 if (routeList[i] == ';')
137 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
143 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
144 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
145 &temps[6], &temps[7]);
148 for (j = 0; j < 8; j++)
151 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
153 GNUNET_free (result);
156 result[i].network.s_addr =
157 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
159 result[i].netmask.s_addr =
160 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
162 while (routeList[pos] != ';')
168 /* try second notation */
170 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
171 &temps[2], &temps[3], &slash);
174 for (j = 0; j < 4; j++)
177 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
179 GNUNET_free (result);
182 result[i].network.s_addr =
183 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
185 if ((slash <= 32) && (slash >= 0))
187 result[i].netmask.s_addr = 0;
190 result[i].netmask.s_addr =
191 (result[i].netmask.s_addr >> 1) + 0x80000000;
194 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
195 while (';' != routeList[pos])
203 LOG (GNUNET_ERROR_TYPE_ERROR,
204 _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
206 GNUNET_free (result);
207 return NULL; /* error */
210 /* try third notation */
213 SSCANF (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
214 &temps[2], &temps[3]);
217 for (j = 0; j < 4; j++)
220 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
222 GNUNET_free (result);
225 result[i].network.s_addr =
226 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
228 result[i].netmask.s_addr = 0;
231 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
234 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
235 while (routeList[pos] != ';')
241 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
243 GNUNET_free (result);
244 return NULL; /* error */
246 if (pos < strlen (routeList))
248 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
250 GNUNET_free (result);
251 return NULL; /* oops */
253 return result; /* ok */
258 * Parse a network specification. The argument specifies
259 * a list of networks. The format is
260 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
261 * with a semicolon). The network must be given in colon-hex
262 * notation. The netmask must be given in CIDR notation (/16) or
263 * can be omitted to specify a single host.
265 * @param routeListX a string specifying the forbidden networks
266 * @return the converted list, NULL if the synatx is flawed
268 static struct IPv6NetworkSet *
269 parse_ipv6_specification (const char *routeListX)
279 struct IPv6NetworkSet *result;
284 if (NULL == routeListX)
286 len = strlen (routeListX);
289 routeList = GNUNET_strdup (routeListX);
291 for (i = 0; i < len; i++)
292 if (';' == routeList[i])
294 if (';' != routeList[len - 1])
296 LOG (GNUNET_ERROR_TYPE_ERROR,
297 _("Invalid network notation (does not end with ';': `%s')\n"),
299 GNUNET_free (routeList);
303 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
309 while (';' != routeList[pos])
312 while ((slash >= start) && (routeList[slash] != '/'))
316 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
321 routeList[pos] = '\0';
322 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
326 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128))
329 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"),
330 &routeList[slash + 1]);
334 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
336 GNUNET_free (result);
337 GNUNET_free (routeList);
343 result[i].netmask.s6_addr[off++] = 0xFF;
348 result[i].netmask.s6_addr[off] =
349 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
354 routeList[slash] = '\0';
355 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
359 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"),
360 &routeList[slash + 1]);
362 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
363 GNUNET_free (result);
364 GNUNET_free (routeList);
370 GNUNET_free (routeList);
376 * Check if the given IP address is in the list of IP addresses.
378 * @param list a list of networks
379 * @param add the IP to check (in network byte order)
380 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
383 check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
390 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
392 if ((add->s_addr & list[i].netmask.s_addr) ==
393 (list[i].network.s_addr & list[i].netmask.s_addr))
402 * Check if the given IP address is in the list of IP addresses.
404 * @param list a list of networks
405 * @param ip the IP to check (in network byte order)
406 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
409 check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip)
413 struct in6_addr zero;
417 memset (&zero, 0, sizeof (struct in6_addr));
420 while (0 != memcmp (&zero, &list[i].network, sizeof (struct in6_addr)))
422 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
423 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
424 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
435 /* ****************** service struct ****************** */
439 * Context for "service_task".
441 struct GNUNET_SERVICE_Context
446 const struct GNUNET_CONFIGURATION_Handle *cfg;
449 * Handle for the server.
451 struct GNUNET_SERVER_Handle *server;
454 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
457 struct sockaddr **addrs;
460 * Name of our service.
462 const char *service_name;
465 * Main service-specific task to run.
467 GNUNET_SERVICE_Main task;
475 * IPv4 addresses that are not allowed to connect.
477 struct IPv4NetworkSet *v4_denied;
480 * IPv6 addresses that are not allowed to connect.
482 struct IPv6NetworkSet *v6_denied;
485 * IPv4 addresses that are allowed to connect (if not
486 * set, all are allowed).
488 struct IPv4NetworkSet *v4_allowed;
491 * IPv6 addresses that are allowed to connect (if not
492 * set, all are allowed).
494 struct IPv6NetworkSet *v6_allowed;
497 * My (default) message handlers. Adjusted copy
500 struct GNUNET_SERVER_MessageHandler *my_handlers;
503 * Array of the lengths of the entries in addrs.
508 * NULL-terminated array of listen sockets we should take over.
510 struct GNUNET_NETWORK_Handle **lsocks;
513 * Task ID of the shutdown task.
515 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
518 * Idle timeout for server.
520 struct GNUNET_TIME_Relative timeout;
523 * Overall success/failure of the service start.
528 * If we are daemonizing, this FD is set to the
529 * pipe to the parent. Send '.' if we started
530 * ok, '!' if not. -1 if we are not daemonizing.
532 int ready_confirm_fd;
535 * Do we close connections if we receive messages
536 * for which we have no handler?
541 * Do we require a matching UID for UNIX domain socket connections?
542 * GNUNET_NO means that the UID does not have to match (however,
543 * "match_gid" may still impose other access control checks).
548 * Do we require a matching GID for UNIX domain socket connections?
549 * Ignored if "match_uid" is GNUNET_YES. Note that this is about
550 * checking that the client's UID is in our group OR that the
551 * client's GID is our GID. If both "match_gid" and "match_uid" are
552 * "GNUNET_NO", all users on the local system have access.
559 enum GNUNET_SERVICE_Options options;
564 /* ****************** message handlers ****************** */
567 * Send a 'TEST' message back to the client.
569 * @param cls the 'struct GNUNET_SERVER_Client' to send TEST to
570 * @param size number of bytes available in 'buf'
571 * @param buf where to copy the message
572 * @return number of bytes written to 'buf'
575 write_test (void *cls, size_t size, void *buf)
577 struct GNUNET_SERVER_Client *client = cls;
578 struct GNUNET_MessageHeader *msg;
580 if (size < sizeof (struct GNUNET_MessageHeader))
582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
583 return 0; /* client disconnected */
585 msg = (struct GNUNET_MessageHeader *) buf;
586 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
587 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
588 GNUNET_SERVER_receive_done (client, GNUNET_OK);
589 return sizeof (struct GNUNET_MessageHeader);
594 * Handler for TEST message.
596 * @param cls closure (refers to service)
597 * @param client identification of the client
598 * @param message the actual message
601 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
602 const struct GNUNET_MessageHeader *message)
604 /* simply bounce message back to acknowledge */
606 GNUNET_SERVER_notify_transmit_ready (client,
607 sizeof (struct GNUNET_MessageHeader),
608 GNUNET_TIME_UNIT_FOREVER_REL,
609 &write_test, client))
610 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
615 * Default handlers for all services. Will be copied and the
616 * "callback_cls" fields will be replaced with the specific service
619 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
620 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
621 sizeof (struct GNUNET_MessageHeader)},
626 /* ****************** service core routines ************** */
630 * Check if access to the service is allowed from the given address.
633 * @param uc credentials, if available, otherwise NULL
634 * @param addr address
635 * @param addrlen length of address
636 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
637 * for unknown address family (will be denied).
640 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
641 const struct sockaddr *addr, socklen_t addrlen)
643 struct GNUNET_SERVICE_Context *sctx = cls;
644 const struct sockaddr_in *i4;
645 const struct sockaddr_in6 *i6;
648 switch (addr->sa_family)
651 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
652 i4 = (const struct sockaddr_in *) addr;
653 ret = ((NULL == sctx->v4_allowed) ||
654 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
655 ((NULL == sctx->v4_denied) ||
656 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
659 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
660 i6 = (const struct sockaddr_in6 *) addr;
661 ret = ((NULL == sctx->v6_allowed) ||
662 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
663 ((NULL == sctx->v6_denied) ||
664 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
668 ret = GNUNET_OK; /* always OK for now */
669 if (GNUNET_YES == sctx->match_uid)
671 /* UID match required */
672 ret = (NULL != uc) && ( (0 == uc->uid) || (uc->uid == geteuid ()) );
674 else if ( (GNUNET_YES == sctx->match_gid) &&
677 (uc->uid != geteuid ()) ) ) )
679 /* group match required and UID does not match */
682 /* no credentials, group match not possible */
690 if (uc->gid != getegid())
692 /* default group did not match, but maybe the user is in our group, let's check */
693 grp = getgrgid (getegid ());
696 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "getgrgid");
700 for (i=0; NULL != grp->gr_mem[i]; i++)
702 struct passwd *nam = getpwnam (grp->gr_mem[i]);
704 continue; /* name in group that is not in user DB !? */
705 if (nam->pw_uid == uc->uid)
707 /* yes, uid is in our group, allow! */
715 if (GNUNET_NO == ret)
716 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
717 (NULL == uc) ? -1 : uc->uid, (NULL == uc) ? -1 : uc->gid);
721 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
723 return GNUNET_SYSERR;
725 if (GNUNET_OK != ret)
727 LOG (GNUNET_ERROR_TYPE_WARNING,
728 _("Access from `%s' denied to service `%s'\n"),
729 GNUNET_a2s (addr, addrlen),
737 * Get the name of the file where we will
738 * write the PID of the service.
740 * @param sctx service context
741 * @return name of the file for the process ID
744 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
749 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
757 * Parse an IPv4 access control list.
759 * @param ret location where to write the ACL (set)
760 * @param sctx service context to use to get the configuration
761 * @param option name of the ACL option to parse
762 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
766 process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
771 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
776 GNUNET_break (GNUNET_OK ==
777 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
780 if (NULL == (*ret = parse_ipv4_specification (opt)))
782 LOG (GNUNET_ERROR_TYPE_WARNING,
783 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
784 opt, sctx->service_name, option);
786 return GNUNET_SYSERR;
794 * Parse an IPv6 access control list.
796 * @param ret location where to write the ACL (set)
797 * @param sctx service context to use to get the configuration
798 * @param option name of the ACL option to parse
799 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
803 process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
808 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
813 GNUNET_break (GNUNET_OK ==
814 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
817 if (NULL == (*ret = parse_ipv6_specification (opt)))
819 LOG (GNUNET_ERROR_TYPE_WARNING,
820 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
821 opt, sctx->service_name, option);
823 return GNUNET_SYSERR;
831 * Add the given UNIX domain path as an address to the
832 * list (as the first entry).
834 * @param saddrs array to update
835 * @param saddrlens where to store the address length
836 * @param unixpath path to add
839 add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
840 const char *unixpath)
843 struct sockaddr_un *un;
846 un = GNUNET_malloc (sizeof (struct sockaddr_un));
847 un->sun_family = AF_UNIX;
848 slen = strlen (unixpath) + 1;
849 if (slen >= sizeof (un->sun_path))
850 slen = sizeof (un->sun_path) - 1;
851 memcpy (un->sun_path, unixpath, slen);
852 un->sun_path[slen] = '\0';
853 slen = sizeof (struct sockaddr_un);
855 un->sun_path[0] = '\0';
857 #if HAVE_SOCKADDR_IN_SIN_LEN
858 un->sun_len = (u_char) slen;
860 *saddrs = (struct sockaddr *) un;
863 /* this function should never be called
864 * unless AF_UNIX is defined! */
871 * Get the list of addresses that a server for the given service
874 * @param service_name name of the service
875 * @param cfg configuration (which specifies the addresses)
876 * @param addrs set (call by reference) to an array of pointers to the
877 * addresses the server should bind to and listen on; the
878 * array will be NULL-terminated (on success)
879 * @param addr_lens set (call by reference) to an array of the lengths
880 * of the respective 'struct sockaddr' struct in the 'addrs'
882 * @return number of addresses found on success,
883 * GNUNET_SYSERR if the configuration
884 * did not specify reasonable finding information or
885 * if it specified a hostname that could not be resolved;
886 * GNUNET_NO if the number of addresses configured is
887 * zero (in this case, '*addrs' and '*addr_lens' will be
891 GNUNET_SERVICE_get_server_addresses (const char *service_name,
892 const struct GNUNET_CONFIGURATION_Handle
893 *cfg, struct sockaddr ***addrs,
894 socklen_t ** addr_lens)
897 struct GNUNET_NETWORK_Handle *desc;
898 unsigned long long port;
900 struct addrinfo hints;
901 struct addrinfo *res;
902 struct addrinfo *pos;
903 struct addrinfo *next;
907 struct sockaddr **saddrs;
908 socklen_t *saddrlens;
914 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
918 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
919 return GNUNET_SYSERR;
922 disablev6 = GNUNET_NO;
926 /* probe IPv6 support */
927 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
930 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
933 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
934 return GNUNET_SYSERR;
936 LOG (GNUNET_ERROR_TYPE_INFO,
938 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
939 service_name, STRERROR (errno));
940 disablev6 = GNUNET_YES;
944 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
950 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
953 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
956 LOG (GNUNET_ERROR_TYPE_ERROR,
957 _("Require valid port number for service `%s' in configuration!\n"),
962 LOG (GNUNET_ERROR_TYPE_ERROR,
963 _("Require valid port number for service `%s' in configuration!\n"),
965 return GNUNET_SYSERR;
969 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
971 GNUNET_break (GNUNET_OK ==
972 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
973 "BINDTO", &hostname));
981 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
983 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
985 (0 < strlen (unixpath)))
987 /* probe UNIX support */
988 struct sockaddr_un s_un;
990 if (strlen (unixpath) >= sizeof (s_un.sun_path))
992 LOG (GNUNET_ERROR_TYPE_WARNING,
993 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
994 (unsigned long long) sizeof (s_un.sun_path));
995 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
996 LOG (GNUNET_ERROR_TYPE_INFO,
997 _("Using `%s' instead\n"), unixpath);
1001 if (NULL != unixpath)
1003 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
1006 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
1009 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
1010 GNUNET_free_non_null (hostname);
1011 GNUNET_free (unixpath);
1012 return GNUNET_SYSERR;
1014 LOG (GNUNET_ERROR_TYPE_INFO,
1016 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
1017 service_name, STRERROR (errno));
1018 GNUNET_free (unixpath);
1023 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1029 if ((0 == port) && (NULL == unixpath))
1031 LOG (GNUNET_ERROR_TYPE_ERROR,
1033 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
1035 GNUNET_free_non_null (hostname);
1036 return GNUNET_SYSERR;
1040 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
1041 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
1042 add_unixpath (saddrs, saddrlens, unixpath);
1043 GNUNET_free_non_null (unixpath);
1044 GNUNET_free_non_null (hostname);
1046 *addr_lens = saddrlens;
1050 if (NULL != hostname)
1052 LOG (GNUNET_ERROR_TYPE_DEBUG,
1053 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
1055 memset (&hints, 0, sizeof (struct addrinfo));
1057 hints.ai_family = AF_INET;
1058 hints.ai_protocol = IPPROTO_TCP;
1059 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1062 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
1063 gai_strerror (ret));
1064 GNUNET_free (hostname);
1065 GNUNET_free_non_null (unixpath);
1066 return GNUNET_SYSERR;
1070 while (NULL != (pos = next))
1072 next = pos->ai_next;
1073 if ((disablev6) && (pos->ai_family == AF_INET6))
1079 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
1080 disablev6 ? "IPv4 " : "", hostname);
1082 GNUNET_free (hostname);
1083 GNUNET_free_non_null (unixpath);
1084 return GNUNET_SYSERR;
1087 if (NULL != unixpath)
1089 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1090 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1092 if (NULL != unixpath)
1094 add_unixpath (saddrs, saddrlens, unixpath);
1098 while (NULL != (pos = next))
1100 next = pos->ai_next;
1101 if ((disablev6) && (AF_INET6 == pos->ai_family))
1103 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1104 continue; /* not TCP */
1105 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1106 continue; /* huh? */
1107 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1108 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1109 if (AF_INET == pos->ai_family)
1111 GNUNET_assert (sizeof (struct sockaddr_in) == 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_in *) saddrs[i])->sin_port = htons (port);
1119 GNUNET_assert (AF_INET6 == pos->ai_family);
1120 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
1121 saddrlens[i] = pos->ai_addrlen;
1122 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1123 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1124 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1128 GNUNET_free (hostname);
1134 /* will bind against everything, just set port */
1139 if (NULL != unixpath)
1142 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1143 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1144 if (NULL != unixpath)
1146 add_unixpath (saddrs, saddrlens, unixpath);
1149 saddrlens[i] = sizeof (struct sockaddr_in);
1150 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1151 #if HAVE_SOCKADDR_IN_SIN_LEN
1152 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1154 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1155 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1161 if (NULL != unixpath)
1163 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1164 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1166 if (NULL != unixpath)
1168 add_unixpath (saddrs, saddrlens, unixpath);
1171 saddrlens[i] = sizeof (struct sockaddr_in6);
1172 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1173 #if HAVE_SOCKADDR_IN_SIN_LEN
1174 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1176 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1177 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1179 saddrlens[i] = sizeof (struct sockaddr_in);
1180 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1181 #if HAVE_SOCKADDR_IN_SIN_LEN
1182 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1184 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1185 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1188 GNUNET_free_non_null (unixpath);
1190 *addr_lens = saddrlens;
1197 * Read listen sockets from the parent process (ARM).
1199 * @param sctx service context to initialize
1200 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
1201 * and GNUNET_SYSERR on error.
1204 receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1206 const char *env_buf;
1212 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1213 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
1215 /* Using W32 API directly here, because this pipe will
1216 * never be used outside of this function, and it's just too much of a bother
1217 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1219 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1220 if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
1229 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1230 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
1233 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
1236 for (i = 0; i < count; i++)
1238 WSAPROTOCOL_INFOA pi;
1242 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1243 if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) )
1245 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1246 if ( (0 == ret) || (sizeof (pi) != rd))
1248 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
1249 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1250 if (NULL == sctx->lsocks[i])
1252 else if (i == count - 1)
1257 sctx->lsocks[count] = NULL;
1262 CloseHandle (lsocks_pipe);
1266 LOG (GNUNET_ERROR_TYPE_ERROR,
1267 _("Could not access a pre-bound socket, will try to bind myself\n"));
1268 for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++)
1269 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
1270 GNUNET_free_non_null (sctx->lsocks);
1271 sctx->lsocks = NULL;
1280 * Setup addr, addrlen, idle_timeout
1281 * based on configuration!
1283 * Configuration may specify:
1284 * - PORT (where to bind to for TCP)
1285 * - UNIXPATH (where to bind to for UNIX domain sockets)
1286 * - TIMEOUT (after how many ms does an inactive service timeout);
1287 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1288 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1289 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1290 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1291 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1292 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1294 * @param sctx service context to initialize
1295 * @return GNUNET_OK if configuration succeeded
1298 setup_service (struct GNUNET_SERVICE_Context *sctx)
1300 struct GNUNET_TIME_Relative idleout;
1309 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, "TIMEOUT"))
1312 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->service_name,
1313 "TIMEOUT", &idleout))
1315 LOG (GNUNET_ERROR_TYPE_ERROR,
1316 _("Specified value for `%s' of service `%s' is invalid\n"),
1317 "TIMEOUT", sctx->service_name);
1318 return GNUNET_SYSERR;
1320 sctx->timeout = idleout;
1323 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1325 if (GNUNET_CONFIGURATION_have_value
1326 (sctx->cfg, sctx->service_name, "TOLERANT"))
1328 if (GNUNET_SYSERR ==
1330 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1333 LOG (GNUNET_ERROR_TYPE_ERROR,
1334 _("Specified value for `%s' of service `%s' is invalid\n"),
1335 "TOLERANT", sctx->service_name);
1336 return GNUNET_SYSERR;
1340 tolerant = GNUNET_NO;
1344 if ((NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1345 (1 == SSCANF (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1346 (cnt + 4 < FD_SETSIZE))
1349 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1352 flags = fcntl (3 + cnt, F_GETFD);
1353 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1355 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1357 LOG (GNUNET_ERROR_TYPE_ERROR,
1359 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1360 (unsigned int) 3 + cnt);
1362 while (sctx->lsocks[cnt] != NULL)
1363 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1364 GNUNET_free (sctx->lsocks);
1365 sctx->lsocks = NULL;
1369 unsetenv ("LISTEN_FDS");
1372 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1374 receive_sockets_from_parent (sctx);
1375 putenv ("GNUNET_OS_READ_LSOCKS=");
1379 if ((NULL == sctx->lsocks) &&
1381 GNUNET_SERVICE_get_server_addresses (sctx->service_name, sctx->cfg,
1382 &sctx->addrs, &sctx->addrlens)))
1383 return GNUNET_SYSERR;
1384 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1386 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1389 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1391 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1392 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1393 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1394 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1401 * Get the name of the user that'll be used
1402 * to provide the service.
1404 * @param sctx service context
1405 * @return value of the 'USERNAME' option
1408 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1413 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
1422 * @param sctx service context
1423 * @param pid PID to write (should be equal to 'getpid()'
1424 * @return GNUNET_OK on success (including no work to be done)
1427 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1435 if (NULL == (pif = get_pid_file_name (sctx)))
1436 return GNUNET_OK; /* no file desired */
1437 user = get_user_name (sctx);
1438 rdir = GNUNET_strdup (pif);
1439 len = strlen (rdir);
1440 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1443 if (0 != ACCESS (rdir, F_OK))
1445 /* we get to create a directory -- and claim it
1447 (void) GNUNET_DISK_directory_create (rdir);
1448 if ((NULL != user) && (0 < strlen (user)))
1449 GNUNET_DISK_file_change_owner (rdir, user);
1451 if (0 != ACCESS (rdir, W_OK | X_OK))
1453 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1455 GNUNET_free_non_null (user);
1457 return GNUNET_SYSERR;
1460 pidfd = FOPEN (pif, "w");
1463 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1465 GNUNET_free_non_null (user);
1466 return GNUNET_SYSERR;
1468 if (0 > FPRINTF (pidfd, "%u", pid))
1469 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1470 GNUNET_break (0 == FCLOSE (pidfd));
1471 if ((NULL != user) && (0 < strlen (user)))
1472 GNUNET_DISK_file_change_owner (pif, user);
1473 GNUNET_free_non_null (user);
1480 * Task run during shutdown. Stops the server/service.
1482 * @param cls the 'struct GNUNET_SERVICE_Context'
1486 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1488 struct GNUNET_SERVICE_Context *service = cls;
1489 struct GNUNET_SERVER_Handle *server = service->server;
1491 service->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1492 if (0 != (service->options & GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN))
1493 GNUNET_SERVER_stop_listening (server);
1495 GNUNET_SERVER_destroy (server);
1500 * Initial task for the service.
1502 * @param cls service context
1506 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1508 struct GNUNET_SERVICE_Context *sctx = cls;
1511 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1513 (void) GNUNET_SPEEDUP_start_ (sctx->cfg);
1514 GNUNET_RESOLVER_connect (sctx->cfg);
1515 if (NULL != sctx->lsocks)
1517 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1518 sctx->timeout, sctx->require_found);
1521 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1522 sctx->timeout, sctx->require_found);
1523 if (NULL == sctx->server)
1525 if (NULL != sctx->addrs)
1528 while (NULL != sctx->addrs[i])
1530 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1531 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1535 sctx->ret = GNUNET_SYSERR;
1538 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1540 /* install a task that will kill the server
1541 * process if the scheduler ever gets a shutdown signal */
1542 sctx->shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1545 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1546 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1548 while (NULL != sctx->my_handlers[i].callback)
1549 sctx->my_handlers[i++].callback_cls = sctx;
1550 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1551 if (-1 != sctx->ready_confirm_fd)
1553 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1554 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1555 sctx->ready_confirm_fd = -1;
1556 write_pid_file (sctx, getpid ());
1558 if (NULL != sctx->addrs)
1561 while (NULL != sctx->addrs[i])
1563 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1564 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1568 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1573 * Detach from terminal.
1575 * @param sctx service context
1576 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1579 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1586 if (0 != PIPE (filedes))
1588 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1589 return GNUNET_SYSERR;
1594 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1595 return GNUNET_SYSERR;
1602 GNUNET_break (0 == CLOSE (filedes[1]));
1604 if (1 != READ (filedes[0], &c, sizeof (char)))
1605 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1612 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1615 LOG (GNUNET_ERROR_TYPE_INFO,
1616 _("Service process could not initialize server function\n"));
1619 LOG (GNUNET_ERROR_TYPE_INFO,
1620 _("Service process failed to report status\n"));
1623 exit (1); /* child reported error */
1625 GNUNET_break (0 == CLOSE (0));
1626 GNUNET_break (0 == CLOSE (1));
1627 GNUNET_break (0 == CLOSE (filedes[0]));
1628 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1630 return GNUNET_SYSERR;
1631 /* set stdin/stdout to /dev/null */
1632 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1634 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1635 (void) CLOSE (nullfd);
1636 return GNUNET_SYSERR;
1638 (void) CLOSE (nullfd);
1639 /* Detach from controlling terminal */
1642 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1643 sctx->ready_confirm_fd = filedes[1];
1645 /* FIXME: we probably need to do something else
1646 * elsewhere in order to fork the process itself... */
1656 * @param sctx service context
1657 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1660 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1664 if (NULL == (user = get_user_name (sctx)))
1665 return GNUNET_OK; /* keep */
1670 pws = getpwnam (user);
1673 LOG (GNUNET_ERROR_TYPE_ERROR,
1674 _("Cannot obtain information about user `%s': %s\n"), user,
1675 errno == 0 ? _("No such user") : STRERROR (errno));
1677 return GNUNET_SYSERR;
1679 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1681 (0 != initgroups (user, pws->pw_gid)) ||
1683 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1685 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1686 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1688 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1689 user, STRERROR (errno));
1691 return GNUNET_SYSERR;
1701 * Delete the PID file that was created by our parent.
1703 * @param sctx service context
1706 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1708 char *pif = get_pid_file_name (sctx);
1711 return; /* no PID file */
1712 if (0 != UNLINK (pif))
1713 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1719 * Run a standard GNUnet service startup sequence (initialize loggers
1720 * and configuration, parse options).
1722 * @param argc number of command line arguments
1723 * @param argv command line arguments
1724 * @param service_name our service name
1725 * @param options service options
1726 * @param task main task of the service
1727 * @param task_cls closure for task
1728 * @return GNUNET_SYSERR on error, GNUNET_OK
1729 * if we shutdown nicely
1732 GNUNET_SERVICE_run (int argc, char *const *argv, const char *service_name,
1733 enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_Main task,
1736 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1745 unsigned long long skew_offset;
1746 unsigned long long skew_variance;
1747 long long clock_offset;
1748 struct GNUNET_SERVICE_Context sctx;
1749 struct GNUNET_CONFIGURATION_Handle *cfg;
1751 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1752 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1753 {'d', "daemonize", NULL,
1754 gettext_noop ("do daemonize (detach from terminal)"), 0,
1755 GNUNET_GETOPT_set_one, &do_daemonize},
1756 GNUNET_GETOPT_OPTION_HELP (NULL),
1757 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1758 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1759 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
1760 GNUNET_GETOPT_OPTION_END
1766 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1767 memset (&sctx, 0, sizeof (sctx));
1768 sctx.options = options;
1769 sctx.ready_confirm_fd = -1;
1770 sctx.ret = GNUNET_OK;
1771 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1773 sctx.task_cls = task_cls;
1774 sctx.service_name = service_name;
1775 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1777 /* setup subsystems */
1778 ret = GNUNET_GETOPT_run (service_name, service_options, argc, argv);
1779 if (GNUNET_SYSERR == ret)
1781 if (GNUNET_NO == ret)
1786 if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
1789 GNUNET_DISK_file_test (cfg_fn))
1790 (void) GNUNET_CONFIGURATION_load (cfg, cfg_fn);
1793 (void) GNUNET_CONFIGURATION_load (cfg, NULL);
1794 if (0 != strcmp (cfg_fn, GNUNET_DEFAULT_USER_CONFIG_FILE))
1795 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1796 _("Could not access configuration file `%s'\n"),
1799 if (GNUNET_OK != setup_service (&sctx))
1801 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1803 if (GNUNET_OK != set_user_id (&sctx))
1805 LOG (GNUNET_ERROR_TYPE_DEBUG,
1806 "Service `%s' runs with configuration from `%s'\n", service_name, cfg_fn);
1808 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1809 "SKEW_OFFSET", &skew_offset)) &&
1811 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1812 "SKEW_VARIANCE", &skew_variance)))
1814 clock_offset = skew_offset - skew_variance;
1815 GNUNET_TIME_set_offset (clock_offset);
1816 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1818 /* actually run service */
1820 GNUNET_SCHEDULER_run (&service_task, &sctx);
1822 if ((1 == do_daemonize) && (NULL != sctx.server))
1823 pid_file_delete (&sctx);
1824 GNUNET_free_non_null (sctx.my_handlers);
1827 if (-1 != sctx.ready_confirm_fd)
1829 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1830 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1831 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1838 GNUNET_CONFIGURATION_have_value (sctx.cfg, service_name,
1841 GNUNET_CONFIGURATION_get_value_string (sctx.cfg, service_name,
1848 GAUGER (service_name, counter, mi.usmblks, "blocks");
1849 GNUNET_free (counter);
1853 GNUNET_SPEEDUP_stop_ ();
1854 GNUNET_CONFIGURATION_destroy (cfg);
1856 if (NULL != sctx.addrs)
1857 while (NULL != sctx.addrs[i])
1858 GNUNET_free (sctx.addrs[i++]);
1859 GNUNET_free_non_null (sctx.addrs);
1860 GNUNET_free_non_null (sctx.addrlens);
1861 GNUNET_free_non_null (logfile);
1862 GNUNET_free_non_null (loglev);
1863 GNUNET_free (cfg_fn);
1864 GNUNET_free_non_null (sctx.v4_denied);
1865 GNUNET_free_non_null (sctx.v6_denied);
1866 GNUNET_free_non_null (sctx.v4_allowed);
1867 GNUNET_free_non_null (sctx.v6_allowed);
1869 return err ? GNUNET_SYSERR : sctx.ret;
1874 * Run a service startup sequence within an existing
1875 * initialized system.
1877 * @param service_name our service name
1878 * @param cfg configuration to use
1879 * @param options service options
1880 * @return NULL on error, service handle
1882 struct GNUNET_SERVICE_Context *
1883 GNUNET_SERVICE_start (const char *service_name,
1884 const struct GNUNET_CONFIGURATION_Handle *cfg,
1885 enum GNUNET_SERVICE_Options options)
1888 struct GNUNET_SERVICE_Context *sctx;
1890 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1891 sctx->ready_confirm_fd = -1; /* no daemonizing */
1892 sctx->ret = GNUNET_OK;
1893 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1894 sctx->service_name = service_name;
1896 sctx->options = options;
1898 /* setup subsystems */
1899 if (GNUNET_OK != setup_service (sctx))
1901 GNUNET_SERVICE_stop (sctx);
1904 if (NULL != sctx->lsocks)
1906 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1907 sctx->timeout, sctx->require_found);
1910 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1911 sctx->timeout, sctx->require_found);
1913 if (NULL == sctx->server)
1915 GNUNET_SERVICE_stop (sctx);
1918 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1919 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1921 while ((sctx->my_handlers[i].callback != NULL))
1922 sctx->my_handlers[i++].callback_cls = sctx;
1923 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1929 * Obtain the server used by a service. Note that the server must NOT
1930 * be destroyed by the caller.
1932 * @param ctx the service context returned from the start function
1933 * @return handle to the server for this service, NULL if there is none
1935 struct GNUNET_SERVER_Handle *
1936 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1943 * Stop a service that was started with "GNUNET_SERVICE_start".
1945 * @param sctx the service context returned from the start function
1948 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1957 GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name,
1960 GNUNET_CONFIGURATION_get_value_string (sctx->cfg, sctx->service_name,
1967 GAUGER (sctx->service_name, counter, mi.usmblks, "blocks");
1968 GNUNET_free (counter);
1972 if (GNUNET_SCHEDULER_NO_TASK != sctx->shutdown_task)
1974 GNUNET_SCHEDULER_cancel (sctx->shutdown_task);
1975 sctx->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1977 if (NULL != sctx->server)
1978 GNUNET_SERVER_destroy (sctx->server);
1979 GNUNET_free_non_null (sctx->my_handlers);
1980 if (NULL != sctx->addrs)
1983 while (NULL != sctx->addrs[i])
1984 GNUNET_free (sctx->addrs[i++]);
1985 GNUNET_free (sctx->addrs);
1987 GNUNET_free_non_null (sctx->addrlens);
1988 GNUNET_free_non_null (sctx->v4_denied);
1989 GNUNET_free_non_null (sctx->v6_denied);
1990 GNUNET_free_non_null (sctx->v4_allowed);
1991 GNUNET_free_non_null (sctx->v6_allowed);
1996 /* end of service.c */