session time out for http client/server
[oweals/gnunet.git] / src / statistics / gnunet-service-statistics.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file statistics/gnunet-service-statistics.c
23  * @brief program that tracks statistics
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_bio_lib.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_disk_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_service_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_strings_lib.h"
35 #include "gnunet_time_lib.h"
36 #include "statistics.h"
37
38 /**
39  * Watch entry.
40  */
41 struct WatchEntry
42 {
43
44   /**
45    * Watch entries are kept in a linked list.
46    */
47   struct WatchEntry *next;
48
49   /**
50    * Watch entries are kept in a linked list.
51    */
52   struct WatchEntry *prev;
53
54   /**
55    * For which client is this watch entry?
56    */
57   struct GNUNET_SERVER_Client *client;
58
59   /**
60    * Last value we communicated to the client for this watch entry.
61    */
62   uint64_t last_value;
63
64   /**
65    * Unique watch number for this client and this watched value.
66    */
67   uint32_t wid;
68
69   /**
70    * Is last_value valid
71    * GNUNET_NO : last_value is n/a, GNUNET_YES: last_value is valid
72    */
73   int last_value_set;
74
75 };
76
77
78 /**
79  * Client entry.
80  */
81 struct ClientEntry
82 {
83   /**
84    * Clients are kept in a linked list.
85    */
86   struct ClientEntry *next;
87
88   /**
89    * Clients are kept in a linked list.
90    */
91   struct ClientEntry *prev;
92
93   /**
94    * Corresponding server handle.
95    */
96   struct GNUNET_SERVER_Client *client;
97
98   /**
99    * Maximum watch ID used by this client so far.
100    */
101   uint32_t max_wid;
102
103 };
104
105 /**
106  * Entry in the statistics list.
107  */
108 struct StatsEntry
109 {
110   /**
111    * This is a linked list.
112    */
113   struct StatsEntry *next;
114
115   /**
116    * Name of the service, points into the
117    * middle of msg.
118    */
119   const char *service;
120
121   /**
122    * Name for the value, points into
123    * the middle of msg.
124    */
125   const char *name;
126
127   /**
128    * Message that can be used to set this value,
129    * stored at the end of the memory used by
130    * this struct.
131    */
132   struct GNUNET_STATISTICS_SetMessage *msg;
133
134   /**
135    * Watch context for changes to this
136    * value, or NULL for none.
137    */
138   struct WatchEntry *we_head;
139
140   /**
141    * Watch context for changes to this
142    * value, or NULL for none.
143    */
144   struct WatchEntry *we_tail;
145
146   /**
147    * Our value.
148    */
149   uint64_t value;
150
151   /**
152    * Unique ID.
153    */
154   uint32_t uid;
155
156   /**
157    * Is this value persistent?
158    */
159   int persistent;
160
161   /**
162    * Is this value set?
163    * GNUNET_NO : value is n/a, GNUNET_YES: value is valid
164    */
165   int set;
166
167 };
168
169 /**
170  * Our configuration.
171  */
172 static const struct GNUNET_CONFIGURATION_Handle *cfg;
173
174 /**
175  * Linked list of our active statistics.
176  */
177 static struct StatsEntry *start;
178
179 /**
180  * Head of linked list of connected clients.
181  */
182 static struct ClientEntry *client_head;
183
184 /**
185  * Tail of linked list of connected clients.
186  */
187 static struct ClientEntry *client_tail;
188
189 /**
190  * Handle to our server.
191  */
192 static struct GNUNET_SERVER_Handle *srv;
193
194 /**
195  * Our notification context.
196  */
197 static struct GNUNET_SERVER_NotificationContext *nc;
198
199 /**
200  * Counter used to generate unique values.
201  */
202 static uint32_t uidgen;
203
204 /**
205  * Set to YES if we are shutting down as soon as possible.
206  */
207 static int in_shutdown;
208
209
210 /**
211  * Inject a message to our server with a client of 'NULL'.
212  *
213  * @param cls the 'struct GNUNET_SERVER_Handle'
214  * @param client unused
215  * @param msg message to inject
216  */
217 static int
218 inject_message (void *cls, void *client, const struct GNUNET_MessageHeader *msg)
219 {
220   struct GNUNET_SERVER_Handle *server = cls;
221
222   GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
223   return GNUNET_OK;
224 }
225
226
227 /**
228  * Load persistent values from disk.  Disk format is
229  * exactly the same format that we also use for
230  * setting the values over the network.
231  *
232  * @param server handle to the server context
233  */
234 static void
235 load (struct GNUNET_SERVER_Handle *server)
236 {
237   char *fn;
238   struct GNUNET_BIO_ReadHandle *rh;
239   uint64_t fsize;
240   char *buf;
241   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
242   char *emsg;
243
244   if (GNUNET_OK !=
245       GNUNET_CONFIGURATION_get_value_filename (cfg,
246                                                "STATISTICS",
247                                                "DATABASE",
248                                                &fn))
249   {
250     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
251                                "STATISTICS",
252                                "DATABASE");
253     return;
254   }
255   if ( (GNUNET_OK !=
256         GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) ||
257        (0 == fsize) )
258   {
259     GNUNET_free (fn);
260     return;
261   }
262   buf = GNUNET_malloc (fsize);
263   rh = GNUNET_BIO_read_open (fn);
264   if (!rh)
265   {
266     GNUNET_free (buf);
267     GNUNET_free (fn);
268     return;
269   }
270   if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
271   {
272     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
273     GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
274     GNUNET_free (buf);
275     GNUNET_free_non_null (emsg);
276     GNUNET_free (fn);
277     return;
278   }
279   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
280               _("Loading %llu bytes of statistics from `%s'\n"),
281               fsize, fn);
282   mst = GNUNET_SERVER_mst_create (&inject_message, server);
283   GNUNET_break (GNUNET_OK ==
284                 GNUNET_SERVER_mst_receive (mst, NULL, buf, fsize,
285                                            GNUNET_YES, GNUNET_NO));
286   GNUNET_SERVER_mst_destroy (mst);
287   GNUNET_free (buf);
288   GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
289   GNUNET_free_non_null (emsg);
290   GNUNET_free (fn);
291 }
292
293
294 /**
295  * Write persistent statistics to disk.
296  */
297 static void
298 save ()
299 {
300   struct StatsEntry *pos;
301   char *fn;
302   struct GNUNET_BIO_WriteHandle *wh;
303   uint16_t size;
304   unsigned long long total;
305
306   if (GNUNET_OK !=
307       GNUNET_CONFIGURATION_get_value_filename (cfg,
308                                                "STATISTICS",
309                                                "DATABASE",
310                                                &fn))
311   {
312     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
313                                "STATISTICS",
314                                "DATABASE");
315     return;
316   }
317   (void) GNUNET_DISK_directory_create_for_file (fn);
318   wh = GNUNET_BIO_write_open (fn);
319   total = 0;
320   while (NULL != (pos = start))
321   {
322     start = pos->next;
323     if ((pos->persistent) && (NULL != wh))
324     {
325       size = htons (pos->msg->header.size);
326       if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
327       {
328         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
329         if (GNUNET_OK != GNUNET_BIO_write_close (wh))
330           GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
331         wh = NULL;
332       }
333       else
334         total += size;
335     }
336     GNUNET_free (pos);
337   }
338   if (NULL != wh)
339   {
340     if (GNUNET_OK != GNUNET_BIO_write_close (wh))
341       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
342     if (total == 0)
343       GNUNET_break (0 == UNLINK (fn));
344     else
345       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
346                   _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
347   }
348   GNUNET_free_non_null (fn);
349 }
350
351
352 /**
353  * Transmit the given stats value.
354  *
355  * @param client receiver of the value
356  * @param e value to transmit
357  */
358 static void
359 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
360 {
361   struct GNUNET_STATISTICS_ReplyMessage *m;
362   size_t size;
363
364   size =
365       sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
366       strlen (e->name) + 1;
367   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
368   m = GNUNET_malloc (size);
369   m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
370   m->header.size = htons (size);
371   m->uid = htonl (e->uid);
372   if (e->persistent)
373     m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
374   m->value = GNUNET_htonll (e->value);
375   size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
376   GNUNET_assert (size ==
377                  GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
378                                              e->service, e->name));
379   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380               "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
381               e->name, e->persistent, e->value);
382   GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
383                                               GNUNET_NO);
384   GNUNET_free (m);
385 }
386
387
388 /**
389  * Does this entry match the request?
390  *
391  * @param e an entry
392  * @param service name of service to match
393  * @param name value to match
394  * @return 1 if they match, 0 if not
395  */
396 static int
397 matches (const struct StatsEntry *e, const char *service, const char *name)
398 {
399   return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
400       ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
401 }
402
403
404 /**
405  * Find a client entry for the given client handle, or create one.
406  *
407  * @param client handle to match
408  * @return corresponding client entry struct
409  */
410 static struct ClientEntry *
411 make_client_entry (struct GNUNET_SERVER_Client *client)
412 {
413   struct ClientEntry *ce;
414
415   GNUNET_assert (client != NULL);
416   ce = client_head;
417   while (ce != NULL)
418   {
419     if (ce->client == client)
420       return ce;
421     ce = ce->next;
422   }
423   if (NULL == nc)
424   {
425     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
426     return NULL;
427   }
428   ce = GNUNET_new (struct ClientEntry);
429   ce->client = client;
430   GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
431   GNUNET_SERVER_notification_context_add (nc, client);
432   return ce;
433 }
434
435
436 /**
437  * Handle GET-message.
438  *
439  * @param cls closure
440  * @param client identification of the client
441  * @param message the actual message
442  * @return GNUNET_OK to keep the connection open,
443  *         GNUNET_SYSERR to close it (signal serious error)
444  */
445 static void
446 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
447             const struct GNUNET_MessageHeader *message)
448 {
449   struct GNUNET_MessageHeader end;
450   char *service;
451   char *name;
452   struct StatsEntry *pos;
453   size_t size;
454
455   if ( (NULL != client) &&
456        (NULL == make_client_entry (client)) )
457     return; /* new client during shutdown */
458   size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
459   if (size !=
460       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
461                                       &service, &name))
462   {
463     GNUNET_break (0);
464     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
465     return;
466   }
467   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468               "Received request for statistics on `%s:%s'\n",
469               strlen (service) ? service : "*", strlen (name) ? name : "*");
470   for (pos = start; NULL != pos; pos = pos->next)
471     if (matches (pos, service, name))
472       transmit (client, pos);
473   end.size = htons (sizeof (struct GNUNET_MessageHeader));
474   end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
475   GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
476   GNUNET_SERVER_receive_done (client, GNUNET_OK);
477 }
478
479
480 /**
481  * Notify all clients listening about a change to a value.
482  *
483  * @param se value that changed
484  */
485 static void
486 notify_change (struct StatsEntry *se)
487 {
488   struct GNUNET_STATISTICS_WatchValueMessage wvm;
489   struct WatchEntry *pos;
490
491   for (pos = se->we_head; NULL != pos; pos = pos->next)
492   {
493     if (GNUNET_YES == pos->last_value_set)
494     {
495       if (pos->last_value == se->value)
496         continue;
497     }
498     else
499     {
500       pos->last_value_set = GNUNET_YES;
501     }
502     wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
503     wvm.header.size =
504       htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
505     wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
506     wvm.wid = htonl (pos->wid);
507     wvm.reserved = htonl (0);
508     wvm.value = GNUNET_htonll (se->value);
509     GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
510                                                 GNUNET_NO);
511     pos->last_value = se->value;
512   }
513 }
514
515
516 /**
517  * Handle SET-message.
518  *
519  * @param cls closure
520  * @param client identification of the client
521  * @param message the actual message
522  */
523 static void
524 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
525             const struct GNUNET_MessageHeader *message)
526 {
527   char *service;
528   char *name;
529   uint16_t msize;
530   uint16_t size;
531   const struct GNUNET_STATISTICS_SetMessage *msg;
532   struct StatsEntry *pos;
533   struct StatsEntry *prev;
534   uint32_t flags;
535   uint64_t value;
536   int64_t delta;
537   int changed;
538   int initial_set;
539
540   if ( (NULL != client) &&
541        (NULL == make_client_entry (client)) )
542     return; /* new client during shutdown */
543   msize = ntohs (message->size);
544   if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
545   {
546     GNUNET_break (0);
547     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
548     return;
549   }
550   size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
551   msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
552
553   if (size !=
554       GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
555                                       &name))
556   {
557     GNUNET_break (0);
558     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
559     return;
560   }
561   flags = ntohl (msg->flags);
562   value = GNUNET_ntohll (msg->value);
563   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564               "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
565               service, name, (unsigned int) flags, (unsigned long long) value);
566   pos = start;
567   prev = NULL;
568   while (pos != NULL)
569   {
570     if (matches (pos, service, name))
571     {
572       initial_set = 0;
573       if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
574       {
575         changed = (pos->value != value);
576         pos->value = value;
577       }
578       else
579       {
580         delta = (int64_t) value;
581         if ((delta < 0) && (pos->value < -delta))
582         {
583           changed = (pos->value != 0);
584           pos->value = 0;
585         }
586         else
587         {
588           changed = (delta != 0);
589           GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
590           pos->value += delta;
591         }
592       }
593       if (GNUNET_NO == pos->set)
594       {
595         pos->set = GNUNET_YES;
596         initial_set = 1;
597       }
598       pos->msg->value = GNUNET_htonll (pos->value);
599       pos->msg->flags = msg->flags;
600       pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
601       if (prev != NULL)
602       {
603         /* move to front for faster setting next time! */
604         prev->next = pos->next;
605         pos->next = start;
606         start = pos;
607       }
608       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
609                   "Statistic `%s:%s' updated to value %llu.\n", service, name,
610                   pos->value);
611       if ((changed) || (1 == initial_set))
612         notify_change (pos);
613       GNUNET_SERVER_receive_done (client, GNUNET_OK);
614       return;
615     }
616     prev = pos;
617     pos = pos->next;
618   }
619   pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
620   pos->next = start;
621   if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
622       (0 < (int64_t) GNUNET_ntohll (msg->value)))
623   {
624     pos->value = GNUNET_ntohll (msg->value);
625     pos->set = GNUNET_YES;
626   }
627   else
628   {
629     pos->set = GNUNET_NO;
630   }
631   pos->uid = uidgen++;
632   pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
633   pos->msg = (void *) &pos[1];
634   memcpy (pos->msg, message, ntohs (message->size));
635   pos->service = (const char *) &pos->msg[1];
636   pos->name = &pos->service[strlen (pos->service) + 1];
637
638   start = pos;
639   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640               "New statistic on `%s:%s' with value %llu created.\n", service,
641               name, pos->value);
642   GNUNET_SERVER_receive_done (client, GNUNET_OK);
643 }
644
645
646 /**
647  * Handle WATCH-message.
648  *
649  * @param cls closure
650  * @param client identification of the client
651  * @param message the actual message
652  */
653 static void
654 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
655               const struct GNUNET_MessageHeader *message)
656 {
657   char *service;
658   char *name;
659   uint16_t msize;
660   uint16_t size;
661   struct StatsEntry *pos;
662   struct ClientEntry *ce;
663   struct WatchEntry *we;
664   size_t slen;
665
666   if (NULL == nc)
667   {
668     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
669     return;
670   }
671   GNUNET_SERVER_client_mark_monitor (client);
672   ce = make_client_entry (client);
673   msize = ntohs (message->size);
674   if (msize < sizeof (struct GNUNET_MessageHeader))
675   {
676     GNUNET_break (0);
677     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
678     return;
679   }
680   size = msize - sizeof (struct GNUNET_MessageHeader);
681   if (size !=
682       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
683                                       &service, &name))
684   {
685     GNUNET_break (0);
686     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
687     return;
688   }
689   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690               "Received request to watch statistic on `%s:%s'\n", service,
691               name);
692   pos = start;
693   while (pos != NULL)
694   {
695     if (matches (pos, service, name))
696       break;
697     pos = pos->next;
698   }
699   if (pos == NULL)
700   {
701     pos =
702         GNUNET_malloc (sizeof (struct StatsEntry) +
703                        sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
704     pos->next = start;
705     pos->uid = uidgen++;
706     pos->set = GNUNET_NO;
707     pos->msg = (void *) &pos[1];
708     pos->msg->header.size =
709         htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
710     pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
711     pos->service = (const char *) &pos->msg[1];
712     slen = strlen (service) + 1;
713     memcpy ((void *) pos->service, service, slen);
714     pos->name = &pos->service[slen];
715     memcpy ((void *) pos->name, name, strlen (name) + 1);
716     start = pos;
717     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718                 "New statistic on `%s:%s' with value %llu created.\n", service,
719                 name, pos->value);
720   }
721   we = GNUNET_new (struct WatchEntry);
722   we->client = client;
723   we->last_value_set = GNUNET_NO;
724   we->wid = ce->max_wid++;
725   GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
726   if (pos->value != 0)
727     notify_change (pos);
728   GNUNET_SERVER_receive_done (client, GNUNET_OK);
729 }
730
731
732 /**
733  * Actually perform the shutdown.
734  */
735 static void
736 do_shutdown ()
737 {
738   struct WatchEntry *we;
739   struct StatsEntry *se;
740
741   if (NULL == nc)
742     return;
743   save ();
744   GNUNET_SERVER_notification_context_destroy (nc);
745   nc = NULL;
746   GNUNET_assert (NULL == client_head);
747   while (NULL != (se = start))
748   {
749     start = se->next;
750     while (NULL != (we = se->we_head))
751     {
752       GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
753       GNUNET_free (we);
754     }
755     GNUNET_free (se);
756   }
757 }
758
759
760 /**
761  * Task run during shutdown.
762  *
763  * @param cls unused
764  * @param tc unused
765  */
766 static void
767 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
768 {
769   in_shutdown = GNUNET_YES;
770   if (NULL != client_head)
771     return;
772   do_shutdown ();
773 }
774
775
776 /**
777  * A client disconnected.  Remove all of its data structure entries.
778  *
779  * @param cls closure, NULL
780  * @param client identification of the client
781  */
782 static void
783 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
784 {
785   struct ClientEntry *ce;
786   struct WatchEntry *we;
787   struct WatchEntry *wen;
788   struct StatsEntry *se;
789
790   ce = client_head;
791   while (NULL != ce)
792   {
793     if (ce->client == client)
794     {
795       GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
796       GNUNET_free (ce);
797       break;
798     }
799     ce = ce->next;
800   }
801   se = start;
802   while (NULL != se)
803   {
804     wen = se->we_head;
805     while (NULL != (we = wen))
806     {
807       wen = we->next;
808       if (we->client != client)
809         continue;
810       GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
811       GNUNET_free (we);
812     }
813     se = se->next;
814   }
815   if ( (NULL == client_head) &&
816        (GNUNET_YES == in_shutdown) )
817     do_shutdown ();
818 }
819
820
821 /**
822  * Process statistics requests.
823  *
824  * @param cls closure
825  * @param server the initialized server
826  * @param c configuration to use
827  */
828 static void
829 run (void *cls, struct GNUNET_SERVER_Handle *server,
830      const struct GNUNET_CONFIGURATION_Handle *c)
831 {
832   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
833     {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
834     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
835     {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
836     {NULL, NULL, 0, 0}
837   };
838   cfg = c;
839   srv = server;
840   GNUNET_SERVER_add_handlers (server, handlers);
841   nc = GNUNET_SERVER_notification_context_create (server, 16);
842   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
843   load (server);
844   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
845                                 NULL);
846 }
847
848
849 /**
850  * The main function for the statistics service.
851  *
852  * @param argc number of arguments from the command line
853  * @param argv command line arguments
854  * @return 0 ok, 1 on error
855  */
856 int
857 main (int argc, char *const *argv)
858 {
859   return (GNUNET_OK ==
860           GNUNET_SERVICE_run (argc, argv, "statistics",
861                               GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run, NULL)) ? 0 : 1;
862 }
863
864 #ifdef LINUX
865 #include <malloc.h>
866
867 /**
868  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
869  */
870 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
871 {
872   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
873   mallopt (M_TOP_PAD, 1 * 1024);
874   malloc_trim (0);
875 }
876 #endif
877
878
879 /* end of gnunet-service-statistics.c */