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_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_directories.h"
30 #include "gnunet_resolver_service.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
40 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
42 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
45 /* ******************* access control ******************** */
48 * @brief IPV4 network in CIDR notation.
55 struct in_addr network;
60 struct in_addr netmask;
65 * @brief network in CIDR notation for IPV6.
72 struct in6_addr network;
77 struct in6_addr netmask;
82 * Start task that may speed up our system clock artificially
84 * @param cfg configuration to use
85 * @return GNUNET_OK on success, GNUNET_SYSERR if the speedup was not configured
88 GNUNET_SPEEDUP_start_ (const struct GNUNET_CONFIGURATION_Handle *cfg);
91 * Stop tasks that modify clock behavior.
94 GNUNET_SPEEDUP_stop_ (void);
98 * Parse a network specification. The argument specifies
99 * a list of networks. The format is
100 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
101 * with a semicolon). The network must be given in dotted-decimal
102 * notation. The netmask can be given in CIDR notation (/16) or
103 * in dotted-decimal (/255.255.0.0).
105 * @param routeList a string specifying the forbidden networks
106 * @return the converted list, NULL if the synatx is flawed
108 static struct IPv4NetworkSet *
109 parse_ipv4_specification (const char *routeList)
117 unsigned int temps[8];
119 struct IPv4NetworkSet *result;
121 if (NULL == routeList)
123 len = strlen (routeList);
127 for (i = 0; i < len; i++)
128 if (routeList[i] == ';')
130 result = GNUNET_malloc (sizeof (struct IPv4NetworkSet) * (count + 1));
136 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u.%u.%u.%u;", &temps[0],
137 &temps[1], &temps[2], &temps[3], &temps[4], &temps[5],
138 &temps[6], &temps[7]);
141 for (j = 0; j < 8; j++)
144 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
146 GNUNET_free (result);
149 result[i].network.s_addr =
150 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
152 result[i].netmask.s_addr =
153 htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
155 while (routeList[pos] != ';')
161 /* try second notation */
163 SSCANF (&routeList[pos], "%u.%u.%u.%u/%u;", &temps[0], &temps[1],
164 &temps[2], &temps[3], &slash);
167 for (j = 0; j < 4; j++)
170 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
172 GNUNET_free (result);
175 result[i].network.s_addr =
176 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
178 if ((slash <= 32) && (slash >= 0))
180 result[i].netmask.s_addr = 0;
183 result[i].netmask.s_addr =
184 (result[i].netmask.s_addr >> 1) + 0x80000000;
187 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
188 while (';' != routeList[pos])
196 LOG (GNUNET_ERROR_TYPE_ERROR,
197 _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
199 GNUNET_free (result);
200 return NULL; /* error */
203 /* try third notation */
206 SSCANF (&routeList[pos], "%u.%u.%u.%u;", &temps[0], &temps[1],
207 &temps[2], &temps[3]);
210 for (j = 0; j < 4; j++)
213 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
215 GNUNET_free (result);
218 result[i].network.s_addr =
219 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
221 result[i].netmask.s_addr = 0;
224 result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
227 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
228 while (routeList[pos] != ';')
234 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
236 GNUNET_free (result);
237 return NULL; /* error */
239 if (pos < strlen (routeList))
241 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid format for IP: `%s'\n"),
243 GNUNET_free (result);
244 return NULL; /* oops */
246 return result; /* ok */
251 * Parse a network specification. The argument specifies
252 * a list of networks. The format is
253 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated
254 * with a semicolon). The network must be given in colon-hex
255 * notation. The netmask must be given in CIDR notation (/16) or
256 * can be omitted to specify a single host.
258 * @param routeListX a string specifying the forbidden networks
259 * @return the converted list, NULL if the synatx is flawed
261 static struct IPv6NetworkSet *
262 parse_ipv6_specification (const char *routeListX)
272 struct IPv6NetworkSet *result;
277 if (NULL == routeListX)
279 len = strlen (routeListX);
282 routeList = GNUNET_strdup (routeListX);
284 for (i = 0; i < len; i++)
285 if (';' == routeList[i])
287 if (';' != routeList[len - 1])
289 LOG (GNUNET_ERROR_TYPE_ERROR,
290 _("Invalid network notation (does not end with ';': `%s')\n"),
292 GNUNET_free (routeList);
296 result = GNUNET_malloc (sizeof (struct IPv6NetworkSet) * (count + 1));
302 while (';' != routeList[pos])
305 while ((slash >= start) && (routeList[slash] != '/'))
309 memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr));
314 routeList[pos] = '\0';
315 ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
319 if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits >= 128))
322 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for netmask\n"),
323 &routeList[slash + 1]);
327 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
329 GNUNET_free (result);
330 GNUNET_free (routeList);
336 result[i].netmask.s6_addr[off++] = 0xFF;
341 result[i].netmask.s6_addr[off] =
342 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
347 routeList[slash] = '\0';
348 ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
352 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong format `%s' for network\n"),
353 &routeList[slash + 1]);
355 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
356 GNUNET_free (result);
357 GNUNET_free (routeList);
363 GNUNET_free (routeList);
369 * Check if the given IP address is in the list of IP addresses.
371 * @param list a list of networks
372 * @param add the IP to check (in network byte order)
373 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
376 check_ipv4_listed (const struct IPv4NetworkSet *list, const struct in_addr *add)
383 while ((list[i].network.s_addr != 0) || (list[i].netmask.s_addr != 0))
385 if ((add->s_addr & list[i].netmask.s_addr) ==
386 (list[i].network.s_addr & list[i].netmask.s_addr))
395 * Check if the given IP address is in the list of IP addresses.
397 * @param list a list of networks
398 * @param ip the IP to check (in network byte order)
399 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is
402 check_ipv6_listed (const struct IPv6NetworkSet *list, const struct in6_addr *ip)
406 struct in6_addr zero;
410 memset (&zero, 0, sizeof (struct in6_addr));
413 while (0 != memcmp (&zero, &list[i].network, sizeof (struct in6_addr)))
415 for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
416 if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
417 (((int *) &list[i].network)[j] & ((int *) &list[i].netmask)[j]))
428 /* ****************** service struct ****************** */
432 * Context for "service_task".
434 struct GNUNET_SERVICE_Context
439 const struct GNUNET_CONFIGURATION_Handle *cfg;
442 * Handle for the server.
444 struct GNUNET_SERVER_Handle *server;
447 * NULL-terminated array of addresses to bind to, NULL if we got pre-bound
450 struct sockaddr **addrs;
453 * Name of our service.
455 const char *service_name;
458 * Main service-specific task to run.
460 GNUNET_SERVICE_Main task;
468 * IPv4 addresses that are not allowed to connect.
470 struct IPv4NetworkSet *v4_denied;
473 * IPv6 addresses that are not allowed to connect.
475 struct IPv6NetworkSet *v6_denied;
478 * IPv4 addresses that are allowed to connect (if not
479 * set, all are allowed).
481 struct IPv4NetworkSet *v4_allowed;
484 * IPv6 addresses that are allowed to connect (if not
485 * set, all are allowed).
487 struct IPv6NetworkSet *v6_allowed;
490 * My (default) message handlers. Adjusted copy
493 struct GNUNET_SERVER_MessageHandler *my_handlers;
496 * Array of the lengths of the entries in addrs.
501 * NULL-terminated array of listen sockets we should take over.
503 struct GNUNET_NETWORK_Handle **lsocks;
506 * Task ID of the shutdown task.
508 GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
511 * Idle timeout for server.
513 struct GNUNET_TIME_Relative timeout;
516 * Overall success/failure of the service start.
521 * If we are daemonizing, this FD is set to the
522 * pipe to the parent. Send '.' if we started
523 * ok, '!' if not. -1 if we are not daemonizing.
525 int ready_confirm_fd;
528 * Do we close connections if we receive messages
529 * for which we have no handler?
534 * Do we require a matching UID for UNIX domain socket connections?
535 * GNUNET_NO means that the UID does not have to match (however,
536 * "match_gid" may still impose other access control checks).
541 * Do we require a matching GID for UNIX domain socket connections?
542 * Ignored if "match_uid" is GNUNET_YES. Note that this is about
543 * checking that the client's UID is in our group OR that the
544 * client's GID is our GID. If both "match_gid" and "match_uid" are
545 * "GNUNET_NO", all users on the local system have access.
552 enum GNUNET_SERVICE_Options options;
557 /* ****************** message handlers ****************** */
560 * Send a 'TEST' message back to the client.
562 * @param cls the 'struct GNUNET_SERVER_Client' to send TEST to
563 * @param size number of bytes available in 'buf'
564 * @param buf where to copy the message
565 * @return number of bytes written to 'buf'
568 write_test (void *cls, size_t size, void *buf)
570 struct GNUNET_SERVER_Client *client = cls;
571 struct GNUNET_MessageHeader *msg;
573 if (size < sizeof (struct GNUNET_MessageHeader))
575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
576 return 0; /* client disconnected */
578 msg = (struct GNUNET_MessageHeader *) buf;
579 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
580 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
581 GNUNET_SERVER_receive_done (client, GNUNET_OK);
582 return sizeof (struct GNUNET_MessageHeader);
587 * Handler for TEST message.
589 * @param cls closure (refers to service)
590 * @param client identification of the client
591 * @param message the actual message
594 handle_test (void *cls, struct GNUNET_SERVER_Client *client,
595 const struct GNUNET_MessageHeader *message)
597 /* simply bounce message back to acknowledge */
599 GNUNET_SERVER_notify_transmit_ready (client,
600 sizeof (struct GNUNET_MessageHeader),
601 GNUNET_TIME_UNIT_FOREVER_REL,
602 &write_test, client))
603 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
608 * Default handlers for all services. Will be copied and the
609 * "callback_cls" fields will be replaced with the specific service
612 static const struct GNUNET_SERVER_MessageHandler defhandlers[] = {
613 {&handle_test, NULL, GNUNET_MESSAGE_TYPE_TEST,
614 sizeof (struct GNUNET_MessageHeader)},
619 /* ****************** service core routines ************** */
623 * Check if access to the service is allowed from the given address.
626 * @param uc credentials, if available, otherwise NULL
627 * @param addr address
628 * @param addrlen length of address
629 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
630 * for unknown address family (will be denied).
633 check_access (void *cls, const struct GNUNET_CONNECTION_Credentials *uc,
634 const struct sockaddr *addr, socklen_t addrlen)
636 struct GNUNET_SERVICE_Context *sctx = cls;
637 const struct sockaddr_in *i4;
638 const struct sockaddr_in6 *i6;
641 switch (addr->sa_family)
644 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
645 i4 = (const struct sockaddr_in *) addr;
646 ret = ((NULL == sctx->v4_allowed) ||
647 (check_ipv4_listed (sctx->v4_allowed, &i4->sin_addr))) &&
648 ((NULL == sctx->v4_denied) ||
649 (!check_ipv4_listed (sctx->v4_denied, &i4->sin_addr)));
652 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
653 i6 = (const struct sockaddr_in6 *) addr;
654 ret = ((NULL == sctx->v6_allowed) ||
655 (check_ipv6_listed (sctx->v6_allowed, &i6->sin6_addr))) &&
656 ((NULL == sctx->v6_denied) ||
657 (!check_ipv6_listed (sctx->v6_denied, &i6->sin6_addr)));
661 ret = GNUNET_OK; /* always OK for now */
662 if (GNUNET_YES == sctx->match_uid)
664 /* UID match required */
665 ret = (NULL != uc) && ( (0 == uc->uid) || (uc->uid == geteuid ()) );
667 else if ( (GNUNET_YES == sctx->match_gid) &&
670 (uc->uid != geteuid ()) ) ) )
672 /* group match required and UID does not match */
675 /* no credentials, group match not possible */
683 if (uc->gid != getegid())
685 /* default group did not match, but maybe the user is in our group, let's check */
686 grp = getgrgid (getegid ());
689 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "getgrgid");
693 for (i=0; NULL != grp->gr_mem[i]; i++)
695 struct passwd *nam = getpwnam (grp->gr_mem[i]);
697 continue; /* name in group that is not in user DB !? */
698 if (nam->pw_uid == uc->uid)
700 /* yes, uid is in our group, allow! */
708 if (GNUNET_NO == ret)
709 LOG (GNUNET_ERROR_TYPE_WARNING, _("Access denied to UID %d / GID %d\n"),
710 (NULL == uc) ? -1 : uc->uid, (NULL == uc) ? -1 : uc->gid);
714 LOG (GNUNET_ERROR_TYPE_WARNING, _("Unknown address family %d\n"),
716 return GNUNET_SYSERR;
718 if (GNUNET_OK != ret)
720 LOG (GNUNET_ERROR_TYPE_WARNING,
721 _("Access from `%s' denied to service `%s'\n"),
722 GNUNET_a2s (addr, addrlen),
730 * Get the name of the file where we will
731 * write the PID of the service.
733 * @param sctx service context
734 * @return name of the file for the process ID
737 get_pid_file_name (struct GNUNET_SERVICE_Context *sctx)
742 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
750 * Parse an IPv4 access control list.
752 * @param ret location where to write the ACL (set)
753 * @param sctx service context to use to get the configuration
754 * @param option name of the ACL option to parse
755 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
759 process_acl4 (struct IPv4NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
764 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
769 GNUNET_break (GNUNET_OK ==
770 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
773 if (NULL == (*ret = parse_ipv4_specification (opt)))
775 LOG (GNUNET_ERROR_TYPE_WARNING,
776 _("Could not parse IPv4 network specification `%s' for `%s:%s'\n"),
777 opt, sctx->service_name, option);
779 return GNUNET_SYSERR;
787 * Parse an IPv6 access control list.
789 * @param ret location where to write the ACL (set)
790 * @param sctx service context to use to get the configuration
791 * @param option name of the ACL option to parse
792 * @return GNUNET_SYSERR on parse error, GNUNET_OK on success (including
796 process_acl6 (struct IPv6NetworkSet **ret, struct GNUNET_SERVICE_Context *sctx,
801 if (!GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, option))
806 GNUNET_break (GNUNET_OK ==
807 GNUNET_CONFIGURATION_get_value_string (sctx->cfg,
810 if (NULL == (*ret = parse_ipv6_specification (opt)))
812 LOG (GNUNET_ERROR_TYPE_WARNING,
813 _("Could not parse IPv6 network specification `%s' for `%s:%s'\n"),
814 opt, sctx->service_name, option);
816 return GNUNET_SYSERR;
824 * Add the given UNIX domain path as an address to the
825 * list (as the first entry).
827 * @param saddrs array to update
828 * @param saddrlens where to store the address length
829 * @param unixpath path to add
832 add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens,
833 const char *unixpath)
836 struct sockaddr_un *un;
839 un = GNUNET_malloc (sizeof (struct sockaddr_un));
840 un->sun_family = AF_UNIX;
841 slen = strlen (unixpath) + 1;
842 if (slen >= sizeof (un->sun_path))
843 slen = sizeof (un->sun_path) - 1;
844 memcpy (un->sun_path, unixpath, slen);
845 un->sun_path[slen] = '\0';
846 slen = sizeof (struct sockaddr_un);
848 un->sun_path[0] = '\0';
850 #if HAVE_SOCKADDR_IN_SIN_LEN
851 un->sun_len = (u_char) slen;
853 *saddrs = (struct sockaddr *) un;
856 /* this function should never be called
857 * unless AF_UNIX is defined! */
864 * Get the list of addresses that a server for the given service
867 * @param service_name name of the service
868 * @param cfg configuration (which specifies the addresses)
869 * @param addrs set (call by reference) to an array of pointers to the
870 * addresses the server should bind to and listen on; the
871 * array will be NULL-terminated (on success)
872 * @param addr_lens set (call by reference) to an array of the lengths
873 * of the respective 'struct sockaddr' struct in the 'addrs'
875 * @return number of addresses found on success,
876 * GNUNET_SYSERR if the configuration
877 * did not specify reasonable finding information or
878 * if it specified a hostname that could not be resolved;
879 * GNUNET_NO if the number of addresses configured is
880 * zero (in this case, '*addrs' and '*addr_lens' will be
884 GNUNET_SERVICE_get_server_addresses (const char *service_name,
885 const struct GNUNET_CONFIGURATION_Handle
886 *cfg, struct sockaddr ***addrs,
887 socklen_t ** addr_lens)
890 struct GNUNET_NETWORK_Handle *desc;
891 unsigned long long port;
893 struct addrinfo hints;
894 struct addrinfo *res;
895 struct addrinfo *pos;
896 struct addrinfo *next;
900 struct sockaddr **saddrs;
901 socklen_t *saddrlens;
907 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
911 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
912 return GNUNET_SYSERR;
915 disablev6 = GNUNET_NO;
919 /* probe IPv6 support */
920 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
923 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
926 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
927 return GNUNET_SYSERR;
929 LOG (GNUNET_ERROR_TYPE_INFO,
931 ("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
932 service_name, STRERROR (errno));
933 disablev6 = GNUNET_YES;
937 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
943 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
946 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
949 LOG (GNUNET_ERROR_TYPE_ERROR,
950 _("Require valid port number for service `%s' in configuration!\n"),
955 LOG (GNUNET_ERROR_TYPE_ERROR,
956 _("Require valid port number for service `%s' in configuration!\n"),
958 return GNUNET_SYSERR;
962 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
964 GNUNET_break (GNUNET_OK ==
965 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
966 "BINDTO", &hostname));
974 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
976 GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH",
978 (0 < strlen (unixpath)))
980 /* probe UNIX support */
981 struct sockaddr_un s_un;
983 if (strlen (unixpath) >= sizeof (s_un.sun_path))
985 LOG (GNUNET_ERROR_TYPE_WARNING,
986 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
987 (unsigned long long) sizeof (s_un.sun_path));
988 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
989 LOG (GNUNET_ERROR_TYPE_INFO,
990 _("Using `%s' instead\n"), unixpath);
993 if (NULL != unixpath)
995 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
998 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
1001 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
1002 GNUNET_free_non_null (hostname);
1003 GNUNET_free (unixpath);
1004 return GNUNET_SYSERR;
1006 LOG (GNUNET_ERROR_TYPE_INFO,
1008 ("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
1009 service_name, STRERROR (errno));
1010 GNUNET_free (unixpath);
1015 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
1021 if ((0 == port) && (NULL == unixpath))
1023 LOG (GNUNET_ERROR_TYPE_ERROR,
1025 ("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
1027 GNUNET_free_non_null (hostname);
1028 return GNUNET_SYSERR;
1032 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
1033 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
1034 add_unixpath (saddrs, saddrlens, unixpath);
1035 GNUNET_free_non_null (unixpath);
1036 GNUNET_free_non_null (hostname);
1038 *addr_lens = saddrlens;
1042 if (NULL != hostname)
1044 LOG (GNUNET_ERROR_TYPE_DEBUG,
1045 "Resolving `%s' since that is where `%s' will bind to.\n", hostname,
1047 memset (&hints, 0, sizeof (struct addrinfo));
1049 hints.ai_family = AF_INET;
1050 hints.ai_protocol = IPPROTO_TCP;
1051 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1054 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"), hostname,
1055 gai_strerror (ret));
1056 GNUNET_free (hostname);
1057 GNUNET_free_non_null (unixpath);
1058 return GNUNET_SYSERR;
1062 while (NULL != (pos = next))
1064 next = pos->ai_next;
1065 if ((disablev6) && (pos->ai_family == AF_INET6))
1071 LOG (GNUNET_ERROR_TYPE_ERROR, _("Failed to find %saddress for `%s'.\n"),
1072 disablev6 ? "IPv4 " : "", hostname);
1074 GNUNET_free (hostname);
1075 GNUNET_free_non_null (unixpath);
1076 return GNUNET_SYSERR;
1079 if (NULL != unixpath)
1081 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1082 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1084 if (NULL != unixpath)
1086 add_unixpath (saddrs, saddrlens, unixpath);
1090 while (NULL != (pos = next))
1092 next = pos->ai_next;
1093 if ((disablev6) && (AF_INET6 == pos->ai_family))
1095 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
1096 continue; /* not TCP */
1097 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
1098 continue; /* huh? */
1099 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
1100 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
1101 if (AF_INET == pos->ai_family)
1103 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
1104 saddrlens[i] = pos->ai_addrlen;
1105 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1106 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1107 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1111 GNUNET_assert (AF_INET6 == pos->ai_family);
1112 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
1113 saddrlens[i] = pos->ai_addrlen;
1114 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1115 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
1116 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1120 GNUNET_free (hostname);
1126 /* will bind against everything, just set port */
1131 if (NULL != unixpath)
1134 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1135 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1136 if (NULL != unixpath)
1138 add_unixpath (saddrs, saddrlens, unixpath);
1141 saddrlens[i] = sizeof (struct sockaddr_in);
1142 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1143 #if HAVE_SOCKADDR_IN_SIN_LEN
1144 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
1146 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1147 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1153 if (NULL != unixpath)
1155 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1156 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1158 if (NULL != unixpath)
1160 add_unixpath (saddrs, saddrlens, unixpath);
1163 saddrlens[i] = sizeof (struct sockaddr_in6);
1164 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1165 #if HAVE_SOCKADDR_IN_SIN_LEN
1166 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
1168 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
1169 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
1171 saddrlens[i] = sizeof (struct sockaddr_in);
1172 saddrs[i] = GNUNET_malloc (saddrlens[i]);
1173 #if HAVE_SOCKADDR_IN_SIN_LEN
1174 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
1176 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
1177 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
1180 GNUNET_free_non_null (unixpath);
1182 *addr_lens = saddrlens;
1189 * Read listen sockets from the parent process (ARM).
1191 * @param sctx service context to initialize
1192 * @return GNUNET_YES if ok, GNUNET_NO if not ok (must bind yourself),
1193 * and GNUNET_SYSERR on error.
1196 receive_sockets_from_parent (struct GNUNET_SERVICE_Context *sctx)
1198 const char *env_buf;
1204 env_buf = getenv ("GNUNET_OS_READ_LSOCKS");
1205 if ((NULL == env_buf) || (strlen (env_buf) <= 0))
1207 /* Using W32 API directly here, because this pipe will
1208 * never be used outside of this function, and it's just too much of a bother
1209 * to create a GNUnet API that boxes a HANDLE (the way it is done with socks)
1211 lsocks_pipe = (HANDLE) strtoul (env_buf, NULL, 10);
1212 if ( (0 == lsocks_pipe) || (INVALID_HANDLE_VALUE == lsocks_pipe))
1221 ret = ReadFile (lsocks_pipe, &count, sizeof (count), &rd, NULL);
1222 if ((0 == ret) || (sizeof (count) != rd) || (0 == count))
1225 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (count + 1));
1228 for (i = 0; i < count; i++)
1230 WSAPROTOCOL_INFOA pi;
1234 ret = ReadFile (lsocks_pipe, &size, sizeof (size), &rd, NULL);
1235 if ( (0 == ret) || (sizeof (size) != rd) || (sizeof (pi) != size) )
1237 ret = ReadFile (lsocks_pipe, &pi, sizeof (pi), &rd, NULL);
1238 if ( (0 == ret) || (sizeof (pi) != rd))
1240 s = WSASocketA (pi.iAddressFamily, pi.iSocketType, pi.iProtocol, &pi, 0, WSA_FLAG_OVERLAPPED);
1241 sctx->lsocks[i] = GNUNET_NETWORK_socket_box_native (s);
1242 if (NULL == sctx->lsocks[i])
1244 else if (i == count - 1)
1249 sctx->lsocks[count] = NULL;
1254 CloseHandle (lsocks_pipe);
1258 LOG (GNUNET_ERROR_TYPE_ERROR,
1259 _("Could not access a pre-bound socket, will try to bind myself\n"));
1260 for (i = 0; (i < count) && (NULL != sctx->lsocks[i]); i++)
1261 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[i]));
1262 GNUNET_free_non_null (sctx->lsocks);
1263 sctx->lsocks = NULL;
1272 * Setup addr, addrlen, idle_timeout
1273 * based on configuration!
1275 * Configuration may specify:
1276 * - PORT (where to bind to for TCP)
1277 * - UNIXPATH (where to bind to for UNIX domain sockets)
1278 * - TIMEOUT (after how many ms does an inactive service timeout);
1279 * - DISABLEV6 (disable support for IPv6, otherwise we use dual-stack)
1280 * - BINDTO (hostname or IP address to bind to, otherwise we take everything)
1281 * - ACCEPT_FROM (only allow connections from specified IPv4 subnets)
1282 * - ACCEPT_FROM6 (only allow connections from specified IPv6 subnets)
1283 * - REJECT_FROM (disallow allow connections from specified IPv4 subnets)
1284 * - REJECT_FROM6 (disallow allow connections from specified IPv6 subnets)
1286 * @param sctx service context to initialize
1287 * @return GNUNET_OK if configuration succeeded
1290 setup_service (struct GNUNET_SERVICE_Context *sctx)
1292 struct GNUNET_TIME_Relative idleout;
1301 if (GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name, "TIMEOUT"))
1304 GNUNET_CONFIGURATION_get_value_time (sctx->cfg, sctx->service_name,
1305 "TIMEOUT", &idleout))
1307 LOG (GNUNET_ERROR_TYPE_ERROR,
1308 _("Specified value for `%s' of service `%s' is invalid\n"),
1309 "TIMEOUT", sctx->service_name);
1310 return GNUNET_SYSERR;
1312 sctx->timeout = idleout;
1315 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1317 if (GNUNET_CONFIGURATION_have_value
1318 (sctx->cfg, sctx->service_name, "TOLERANT"))
1320 if (GNUNET_SYSERR ==
1322 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1325 LOG (GNUNET_ERROR_TYPE_ERROR,
1326 _("Specified value for `%s' of service `%s' is invalid\n"),
1327 "TOLERANT", sctx->service_name);
1328 return GNUNET_SYSERR;
1332 tolerant = GNUNET_NO;
1336 if ((NULL != (nfds = getenv ("LISTEN_FDS"))) &&
1337 (1 == SSCANF (nfds, "%u", &cnt)) && (cnt > 0) && (cnt < FD_SETSIZE) &&
1338 (cnt + 4 < FD_SETSIZE))
1341 GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (cnt + 1));
1344 flags = fcntl (3 + cnt, F_GETFD);
1345 if ((flags < 0) || (0 != (flags & FD_CLOEXEC)) ||
1347 (sctx->lsocks[cnt] = GNUNET_NETWORK_socket_box_native (3 + cnt))))
1349 LOG (GNUNET_ERROR_TYPE_ERROR,
1351 ("Could not access pre-bound socket %u, will try to bind myself\n"),
1352 (unsigned int) 3 + cnt);
1354 while (sctx->lsocks[cnt] != NULL)
1355 GNUNET_break (0 == GNUNET_NETWORK_socket_close (sctx->lsocks[cnt++]));
1356 GNUNET_free (sctx->lsocks);
1357 sctx->lsocks = NULL;
1361 unsetenv ("LISTEN_FDS");
1364 if (getenv ("GNUNET_OS_READ_LSOCKS") != NULL)
1366 receive_sockets_from_parent (sctx);
1367 putenv ("GNUNET_OS_READ_LSOCKS=");
1371 if ((NULL == sctx->lsocks) &&
1373 GNUNET_SERVICE_get_server_addresses (sctx->service_name, sctx->cfg,
1374 &sctx->addrs, &sctx->addrlens)))
1375 return GNUNET_SYSERR;
1376 sctx->require_found = tolerant ? GNUNET_NO : GNUNET_YES;
1378 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1381 GNUNET_CONFIGURATION_get_value_yesno (sctx->cfg, sctx->service_name,
1383 process_acl4 (&sctx->v4_denied, sctx, "REJECT_FROM");
1384 process_acl4 (&sctx->v4_allowed, sctx, "ACCEPT_FROM");
1385 process_acl6 (&sctx->v6_denied, sctx, "REJECT_FROM6");
1386 process_acl6 (&sctx->v6_allowed, sctx, "ACCEPT_FROM6");
1393 * Get the name of the user that'll be used
1394 * to provide the service.
1396 * @param sctx service context
1397 * @return value of the 'USERNAME' option
1400 get_user_name (struct GNUNET_SERVICE_Context *sctx)
1405 GNUNET_CONFIGURATION_get_value_filename (sctx->cfg, sctx->service_name,
1414 * @param sctx service context
1415 * @param pid PID to write (should be equal to 'getpid()'
1416 * @return GNUNET_OK on success (including no work to be done)
1419 write_pid_file (struct GNUNET_SERVICE_Context *sctx, pid_t pid)
1427 if (NULL == (pif = get_pid_file_name (sctx)))
1428 return GNUNET_OK; /* no file desired */
1429 user = get_user_name (sctx);
1430 rdir = GNUNET_strdup (pif);
1431 len = strlen (rdir);
1432 while ((len > 0) && (rdir[len] != DIR_SEPARATOR))
1435 if (0 != ACCESS (rdir, F_OK))
1437 /* we get to create a directory -- and claim it
1439 (void) GNUNET_DISK_directory_create (rdir);
1440 if ((NULL != user) && (0 < strlen (user)))
1441 GNUNET_DISK_file_change_owner (rdir, user);
1443 if (0 != ACCESS (rdir, W_OK | X_OK))
1445 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", rdir);
1447 GNUNET_free_non_null (user);
1449 return GNUNET_SYSERR;
1452 pidfd = FOPEN (pif, "w");
1455 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "fopen", pif);
1457 GNUNET_free_non_null (user);
1458 return GNUNET_SYSERR;
1460 if (0 > FPRINTF (pidfd, "%u", pid))
1461 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fprintf", pif);
1462 GNUNET_break (0 == FCLOSE (pidfd));
1463 if ((NULL != user) && (0 < strlen (user)))
1464 GNUNET_DISK_file_change_owner (pif, user);
1465 GNUNET_free_non_null (user);
1472 * Task run during shutdown. Stops the server/service.
1474 * @param cls the 'struct GNUNET_SERVICE_Context'
1478 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1480 struct GNUNET_SERVICE_Context *service = cls;
1481 struct GNUNET_SERVER_Handle *server = service->server;
1483 service->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1484 if (0 != (service->options & GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN))
1485 GNUNET_SERVER_stop_listening (server);
1487 GNUNET_SERVER_destroy (server);
1492 * Initial task for the service.
1494 * @param cls service context
1498 service_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1500 struct GNUNET_SERVICE_Context *sctx = cls;
1503 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
1505 (void) GNUNET_SPEEDUP_start_ (sctx->cfg);
1506 GNUNET_RESOLVER_connect (sctx->cfg);
1507 if (NULL != sctx->lsocks)
1509 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1510 sctx->timeout, sctx->require_found);
1513 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1514 sctx->timeout, sctx->require_found);
1515 if (NULL == sctx->server)
1517 if (NULL != sctx->addrs)
1520 while (NULL != sctx->addrs[i])
1522 LOG (GNUNET_ERROR_TYPE_INFO, _("Failed to start `%s' at `%s'\n"),
1523 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1527 sctx->ret = GNUNET_SYSERR;
1530 if (0 == (sctx->options & GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN))
1532 /* install a task that will kill the server
1533 * process if the scheduler ever gets a shutdown signal */
1534 sctx->shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1537 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1538 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1540 while (NULL != sctx->my_handlers[i].callback)
1541 sctx->my_handlers[i++].callback_cls = sctx;
1542 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1543 if (-1 != sctx->ready_confirm_fd)
1545 GNUNET_break (1 == WRITE (sctx->ready_confirm_fd, ".", 1));
1546 GNUNET_break (0 == CLOSE (sctx->ready_confirm_fd));
1547 sctx->ready_confirm_fd = -1;
1548 write_pid_file (sctx, getpid ());
1550 if (NULL != sctx->addrs)
1553 while (NULL != sctx->addrs[i])
1555 LOG (GNUNET_ERROR_TYPE_INFO, _("Service `%s' runs at %s\n"),
1556 sctx->service_name, GNUNET_a2s (sctx->addrs[i], sctx->addrlens[i]));
1560 sctx->task (sctx->task_cls, sctx->server, sctx->cfg);
1565 * Detach from terminal.
1567 * @param sctx service context
1568 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1571 detach_terminal (struct GNUNET_SERVICE_Context *sctx)
1578 if (0 != PIPE (filedes))
1580 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "pipe");
1581 return GNUNET_SYSERR;
1586 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "fork");
1587 return GNUNET_SYSERR;
1594 GNUNET_break (0 == CLOSE (filedes[1]));
1596 if (1 != READ (filedes[0], &c, sizeof (char)))
1597 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "read");
1604 LOG (GNUNET_ERROR_TYPE_INFO, _("Service process failed to initialize\n"));
1607 LOG (GNUNET_ERROR_TYPE_INFO,
1608 _("Service process could not initialize server function\n"));
1611 LOG (GNUNET_ERROR_TYPE_INFO,
1612 _("Service process failed to report status\n"));
1615 exit (1); /* child reported error */
1617 GNUNET_break (0 == CLOSE (0));
1618 GNUNET_break (0 == CLOSE (1));
1619 GNUNET_break (0 == CLOSE (filedes[0]));
1620 nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
1622 return GNUNET_SYSERR;
1623 /* set stdin/stdout to /dev/null */
1624 if ((dup2 (nullfd, 0) < 0) || (dup2 (nullfd, 1) < 0))
1626 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "dup2");
1627 (void) CLOSE (nullfd);
1628 return GNUNET_SYSERR;
1630 (void) CLOSE (nullfd);
1631 /* Detach from controlling terminal */
1634 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "setsid");
1635 sctx->ready_confirm_fd = filedes[1];
1637 /* FIXME: we probably need to do something else
1638 * elsewhere in order to fork the process itself... */
1648 * @param sctx service context
1649 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1652 set_user_id (struct GNUNET_SERVICE_Context *sctx)
1656 if (NULL == (user = get_user_name (sctx)))
1657 return GNUNET_OK; /* keep */
1662 pws = getpwnam (user);
1665 LOG (GNUNET_ERROR_TYPE_ERROR,
1666 _("Cannot obtain information about user `%s': %s\n"), user,
1667 errno == 0 ? _("No such user") : STRERROR (errno));
1669 return GNUNET_SYSERR;
1671 if ((0 != setgid (pws->pw_gid)) || (0 != setegid (pws->pw_gid)) ||
1673 (0 != initgroups (user, pws->pw_gid)) ||
1675 (0 != setuid (pws->pw_uid)) || (0 != seteuid (pws->pw_uid)))
1677 if ((0 != setregid (pws->pw_gid, pws->pw_gid)) ||
1678 (0 != setreuid (pws->pw_uid, pws->pw_uid)))
1680 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot change user/group to `%s': %s\n"),
1681 user, STRERROR (errno));
1683 return GNUNET_SYSERR;
1693 * Delete the PID file that was created by our parent.
1695 * @param sctx service context
1698 pid_file_delete (struct GNUNET_SERVICE_Context *sctx)
1700 char *pif = get_pid_file_name (sctx);
1703 return; /* no PID file */
1704 if (0 != UNLINK (pif))
1705 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", pif);
1711 * Run a standard GNUnet service startup sequence (initialize loggers
1712 * and configuration, parse options).
1714 * @param argc number of command line arguments
1715 * @param argv command line arguments
1716 * @param service_name our service name
1717 * @param options service options
1718 * @param task main task of the service
1719 * @param task_cls closure for task
1720 * @return GNUNET_SYSERR on error, GNUNET_OK
1721 * if we shutdown nicely
1724 GNUNET_SERVICE_run (int argc, char *const *argv, const char *service_name,
1725 enum GNUNET_SERVICE_Options options, GNUNET_SERVICE_Main task,
1728 #define HANDLE_ERROR do { GNUNET_break (0); goto shutdown; } while (0)
1737 unsigned long long skew_offset;
1738 unsigned long long skew_variance;
1739 long long clock_offset;
1740 struct GNUNET_SERVICE_Context sctx;
1741 struct GNUNET_CONFIGURATION_Handle *cfg;
1743 struct GNUNET_GETOPT_CommandLineOption service_options[] = {
1744 GNUNET_GETOPT_OPTION_CFG_FILE (&cfg_fn),
1745 {'d', "daemonize", NULL,
1746 gettext_noop ("do daemonize (detach from terminal)"), 0,
1747 GNUNET_GETOPT_set_one, &do_daemonize},
1748 GNUNET_GETOPT_OPTION_HELP (NULL),
1749 GNUNET_GETOPT_OPTION_LOGLEVEL (&loglev),
1750 GNUNET_GETOPT_OPTION_LOGFILE (&logfile),
1751 GNUNET_GETOPT_OPTION_VERSION (PACKAGE_VERSION " " VCS_VERSION),
1752 GNUNET_GETOPT_OPTION_END
1758 cfg_fn = GNUNET_strdup (GNUNET_DEFAULT_USER_CONFIG_FILE);
1759 memset (&sctx, 0, sizeof (sctx));
1760 sctx.options = options;
1761 sctx.ready_confirm_fd = -1;
1762 sctx.ret = GNUNET_OK;
1763 sctx.timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1765 sctx.task_cls = task_cls;
1766 sctx.service_name = service_name;
1767 sctx.cfg = cfg = GNUNET_CONFIGURATION_create ();
1769 /* setup subsystems */
1770 ret = GNUNET_GETOPT_run (service_name, service_options, argc, argv);
1771 if (GNUNET_SYSERR == ret)
1773 if (GNUNET_NO == ret)
1778 if (GNUNET_OK != GNUNET_log_setup (service_name, loglev, logfile))
1781 GNUNET_DISK_file_test (cfg_fn))
1782 (void) GNUNET_CONFIGURATION_load (cfg, cfg_fn);
1785 (void) GNUNET_CONFIGURATION_load (cfg, NULL);
1786 if (0 != strcmp (cfg_fn, GNUNET_DEFAULT_USER_CONFIG_FILE))
1787 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1788 _("Could not access configuration file `%s'\n"),
1791 if (GNUNET_OK != setup_service (&sctx))
1793 if ((1 == do_daemonize) && (GNUNET_OK != detach_terminal (&sctx)))
1795 if (GNUNET_OK != set_user_id (&sctx))
1797 LOG (GNUNET_ERROR_TYPE_DEBUG,
1798 "Service `%s' runs with configuration from `%s'\n", service_name, cfg_fn);
1800 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1801 "SKEW_OFFSET", &skew_offset)) &&
1803 GNUNET_CONFIGURATION_get_value_number (sctx.cfg, "TESTING",
1804 "SKEW_VARIANCE", &skew_variance)))
1806 clock_offset = skew_offset - skew_variance;
1807 GNUNET_TIME_set_offset (clock_offset);
1808 LOG (GNUNET_ERROR_TYPE_DEBUG, "Skewing clock by %dll ms\n", clock_offset);
1810 /* actually run service */
1812 GNUNET_SCHEDULER_run (&service_task, &sctx);
1814 if ((1 == do_daemonize) && (NULL != sctx.server))
1815 pid_file_delete (&sctx);
1816 GNUNET_free_non_null (sctx.my_handlers);
1819 if (-1 != sctx.ready_confirm_fd)
1821 if (1 != WRITE (sctx.ready_confirm_fd, err ? "I" : "S", 1))
1822 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "write");
1823 GNUNET_break (0 == CLOSE (sctx.ready_confirm_fd));
1830 GNUNET_CONFIGURATION_have_value (sctx.cfg, service_name,
1833 GNUNET_CONFIGURATION_get_value_string (sctx.cfg, service_name,
1840 GAUGER (service_name, counter, mi.usmblks, "blocks");
1841 GNUNET_free (counter);
1845 GNUNET_SPEEDUP_stop_ ();
1846 GNUNET_CONFIGURATION_destroy (cfg);
1848 if (NULL != sctx.addrs)
1849 while (NULL != sctx.addrs[i])
1850 GNUNET_free (sctx.addrs[i++]);
1851 GNUNET_free_non_null (sctx.addrs);
1852 GNUNET_free_non_null (sctx.addrlens);
1853 GNUNET_free_non_null (logfile);
1854 GNUNET_free_non_null (loglev);
1855 GNUNET_free (cfg_fn);
1856 GNUNET_free_non_null (sctx.v4_denied);
1857 GNUNET_free_non_null (sctx.v6_denied);
1858 GNUNET_free_non_null (sctx.v4_allowed);
1859 GNUNET_free_non_null (sctx.v6_allowed);
1861 return err ? GNUNET_SYSERR : sctx.ret;
1866 * Run a service startup sequence within an existing
1867 * initialized system.
1869 * @param service_name our service name
1870 * @param cfg configuration to use
1871 * @param options service options
1872 * @return NULL on error, service handle
1874 struct GNUNET_SERVICE_Context *
1875 GNUNET_SERVICE_start (const char *service_name,
1876 const struct GNUNET_CONFIGURATION_Handle *cfg,
1877 enum GNUNET_SERVICE_Options options)
1880 struct GNUNET_SERVICE_Context *sctx;
1882 sctx = GNUNET_malloc (sizeof (struct GNUNET_SERVICE_Context));
1883 sctx->ready_confirm_fd = -1; /* no daemonizing */
1884 sctx->ret = GNUNET_OK;
1885 sctx->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1886 sctx->service_name = service_name;
1888 sctx->options = options;
1890 /* setup subsystems */
1891 if (GNUNET_OK != setup_service (sctx))
1893 GNUNET_SERVICE_stop (sctx);
1896 if (NULL != sctx->lsocks)
1898 GNUNET_SERVER_create_with_sockets (&check_access, sctx, sctx->lsocks,
1899 sctx->timeout, sctx->require_found);
1902 GNUNET_SERVER_create (&check_access, sctx, sctx->addrs, sctx->addrlens,
1903 sctx->timeout, sctx->require_found);
1905 if (NULL == sctx->server)
1907 GNUNET_SERVICE_stop (sctx);
1910 sctx->my_handlers = GNUNET_malloc (sizeof (defhandlers));
1911 memcpy (sctx->my_handlers, defhandlers, sizeof (defhandlers));
1913 while ((sctx->my_handlers[i].callback != NULL))
1914 sctx->my_handlers[i++].callback_cls = sctx;
1915 GNUNET_SERVER_add_handlers (sctx->server, sctx->my_handlers);
1921 * Obtain the server used by a service. Note that the server must NOT
1922 * be destroyed by the caller.
1924 * @param ctx the service context returned from the start function
1925 * @return handle to the server for this service, NULL if there is none
1927 struct GNUNET_SERVER_Handle *
1928 GNUNET_SERVICE_get_server (struct GNUNET_SERVICE_Context *ctx)
1935 * Stop a service that was started with "GNUNET_SERVICE_start".
1937 * @param sctx the service context returned from the start function
1940 GNUNET_SERVICE_stop (struct GNUNET_SERVICE_Context *sctx)
1949 GNUNET_CONFIGURATION_have_value (sctx->cfg, sctx->service_name,
1952 GNUNET_CONFIGURATION_get_value_string (sctx->cfg, sctx->service_name,
1959 GAUGER (sctx->service_name, counter, mi.usmblks, "blocks");
1960 GNUNET_free (counter);
1964 if (GNUNET_SCHEDULER_NO_TASK != sctx->shutdown_task)
1966 GNUNET_SCHEDULER_cancel (sctx->shutdown_task);
1967 sctx->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
1969 if (NULL != sctx->server)
1970 GNUNET_SERVER_destroy (sctx->server);
1971 GNUNET_free_non_null (sctx->my_handlers);
1972 if (NULL != sctx->addrs)
1975 while (NULL != sctx->addrs[i])
1976 GNUNET_free (sctx->addrs[i++]);
1977 GNUNET_free (sctx->addrs);
1979 GNUNET_free_non_null (sctx->addrlens);
1980 GNUNET_free_non_null (sctx->v4_denied);
1981 GNUNET_free_non_null (sctx->v6_denied);
1982 GNUNET_free_non_null (sctx->v4_allowed);
1983 GNUNET_free_non_null (sctx->v6_allowed);
1988 /* end of service.c */