gnunet-statistics can retrieve stats from remote hosts without config
[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   fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
245                                       NULL);
246   if (fn == NULL)
247     return;
248   if ((GNUNET_OK != GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) || (fsize == 0))
249   {
250     GNUNET_free (fn);
251     return;
252   }
253   buf = GNUNET_malloc (fsize);
254   rh = GNUNET_BIO_read_open (fn);
255   if (!rh)
256   {
257     GNUNET_free (buf);
258     GNUNET_free (fn);
259     return;
260   }
261   if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
262   {
263     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
264     GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
265     GNUNET_free (buf);
266     GNUNET_free_non_null (emsg);
267     GNUNET_free (fn);
268     return;
269   }
270   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
271               _("Loading %llu bytes of statistics from `%s'\n"),
272               fsize, fn);
273   mst = GNUNET_SERVER_mst_create (&inject_message, server);
274   GNUNET_break (GNUNET_OK ==
275                 GNUNET_SERVER_mst_receive (mst, NULL, buf, fsize,
276                                            GNUNET_YES, GNUNET_NO));
277   GNUNET_SERVER_mst_destroy (mst);
278   GNUNET_free (buf);
279   GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
280   GNUNET_free_non_null (emsg);
281   GNUNET_free (fn);
282 }
283
284
285 /**
286  * Write persistent statistics to disk.
287  */
288 static void
289 save ()
290 {
291   struct StatsEntry *pos;
292   char *fn;
293   struct GNUNET_BIO_WriteHandle *wh;
294   
295   uint16_t size;
296   unsigned long long total;
297
298   wh = NULL;
299   fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
300                                       NULL);
301   if (fn != NULL)
302     wh = GNUNET_BIO_write_open (fn);
303   total = 0;
304   while (NULL != (pos = start))
305   {
306     start = pos->next;
307     if ((pos->persistent) && (NULL != wh))
308     {
309       size = htons (pos->msg->header.size);
310       if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
311       {
312         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
313         if (GNUNET_OK != GNUNET_BIO_write_close (wh))
314           GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
315         wh = NULL;
316       }
317       else
318         total += size;
319     }
320     GNUNET_free (pos);
321   }
322   if (NULL != wh)
323   {
324     if (GNUNET_OK != GNUNET_BIO_write_close (wh))
325       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
326     if (total == 0)
327       GNUNET_break (0 == UNLINK (fn));
328     else
329       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
330                   _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
331   }
332   GNUNET_free_non_null (fn);
333 }
334
335
336 /**
337  * Transmit the given stats value.
338  *
339  * @param client receiver of the value
340  * @param e value to transmit
341  */
342 static void
343 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
344 {
345   struct GNUNET_STATISTICS_ReplyMessage *m;
346   size_t size;
347
348   size =
349       sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
350       strlen (e->name) + 1;
351   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
352   m = GNUNET_malloc (size);
353   m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
354   m->header.size = htons (size);
355   m->uid = htonl (e->uid);
356   if (e->persistent)
357     m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
358   m->value = GNUNET_htonll (e->value);
359   size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
360   GNUNET_assert (size ==
361                  GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
362                                              e->service, e->name));
363   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364               "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
365               e->name, e->persistent, e->value);
366   GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
367                                               GNUNET_NO);
368   GNUNET_free (m);
369 }
370
371
372 /**
373  * Does this entry match the request?
374  *
375  * @param e an entry
376  * @param service name of service to match
377  * @param name value to match
378  * @return 1 if they match, 0 if not
379  */
380 static int
381 matches (const struct StatsEntry *e, const char *service, const char *name)
382 {
383   return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
384       ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
385 }
386
387
388 /**
389  * Find a client entry for the given client handle, or create one.
390  *
391  * @param client handle to match
392  * @return corresponding client entry struct
393  */
394 static struct ClientEntry *
395 make_client_entry (struct GNUNET_SERVER_Client *client)
396 {
397   struct ClientEntry *ce;
398
399   GNUNET_assert (client != NULL);
400   ce = client_head;
401   while (ce != NULL)
402   {
403     if (ce->client == client)
404       return ce;
405     ce = ce->next;
406   }
407   if (NULL == nc)
408   {
409     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
410     return NULL;
411   }
412   ce = GNUNET_malloc (sizeof (struct ClientEntry));
413   ce->client = client;
414   GNUNET_SERVER_client_keep (client);
415   GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
416   GNUNET_SERVER_notification_context_add (nc, client);
417   return ce;
418 }
419
420
421 /**
422  * Handle GET-message.
423  *
424  * @param cls closure
425  * @param client identification of the client
426  * @param message the actual message
427  * @return GNUNET_OK to keep the connection open,
428  *         GNUNET_SYSERR to close it (signal serious error)
429  */
430 static void
431 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
432             const struct GNUNET_MessageHeader *message)
433 {
434   struct GNUNET_MessageHeader end;
435   char *service;
436   char *name;
437   struct StatsEntry *pos;
438   size_t size;
439
440   if ( (NULL != client) &&
441        (NULL == make_client_entry (client)) )
442     return; /* new client during shutdown */
443   size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
444   if (size !=
445       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
446                                       &service, &name))
447   {
448     GNUNET_break (0);
449     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
450     return;
451   }
452   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453               "Received request for statistics on `%s:%s'\n",
454               strlen (service) ? service : "*", strlen (name) ? name : "*");
455   for (pos = start; NULL != pos; pos = pos->next)
456     if (matches (pos, service, name))
457       transmit (client, pos);
458   end.size = htons (sizeof (struct GNUNET_MessageHeader));
459   end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
460   GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
461   GNUNET_SERVER_receive_done (client, GNUNET_OK);
462 }
463
464
465 /**
466  * Notify all clients listening about a change to a value.
467  *
468  * @param se value that changed
469  */
470 static void
471 notify_change (struct StatsEntry *se)
472 {
473   struct GNUNET_STATISTICS_WatchValueMessage wvm;
474   struct WatchEntry *pos;
475
476   for (pos = se->we_head; NULL != pos; pos = pos->next)
477   {
478     if (GNUNET_YES == pos->last_value_set)
479     {
480       if (pos->last_value == se->value)
481         continue;
482     }
483     else
484     {
485       pos->last_value_set = GNUNET_YES;
486     }
487     wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
488     wvm.header.size =
489       htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
490     wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
491     wvm.wid = htonl (pos->wid);
492     wvm.reserved = htonl (0);
493     wvm.value = GNUNET_htonll (se->value);
494     GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
495                                                 GNUNET_NO);
496     pos->last_value = se->value;
497   }
498 }
499
500 /**
501  * Handle SET-message.
502  *
503  * @param cls closure
504  * @param client identification of the client
505  * @param message the actual message
506  */
507 static void
508 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
509             const struct GNUNET_MessageHeader *message)
510 {
511   char *service;
512   char *name;
513   uint16_t msize;
514   uint16_t size;
515   const struct GNUNET_STATISTICS_SetMessage *msg;
516   struct StatsEntry *pos;
517   struct StatsEntry *prev;
518   uint32_t flags;
519   uint64_t value;
520   int64_t delta;
521   int changed;
522   int initial_set;
523
524   if ( (NULL != client) &&
525        (NULL == make_client_entry (client)) )
526     return; /* new client during shutdown */
527   msize = ntohs (message->size);
528   if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
529   {
530     GNUNET_break (0);
531     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
532     return;
533   }
534   size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
535   msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
536
537   if (size !=
538       GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
539                                       &name))
540   {
541     GNUNET_break (0);
542     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
543     return;
544   }
545   flags = ntohl (msg->flags);
546   value = GNUNET_ntohll (msg->value);
547   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548               "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
549               service, name, (unsigned int) flags, (unsigned long long) value);
550   pos = start;
551   prev = NULL;
552   while (pos != NULL)
553   {
554     if (matches (pos, service, name))
555     {
556       initial_set = 0;
557       if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
558       {
559         changed = (pos->value != value);
560         pos->value = value;
561       }
562       else
563       {
564         delta = (int64_t) value;
565         if ((delta < 0) && (pos->value < -delta))
566         {
567           changed = (pos->value != 0);
568           pos->value = 0;
569         }
570         else
571         {
572           changed = (delta != 0);
573           GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
574           pos->value += delta;
575         }
576       }
577       if (GNUNET_NO == pos->set)
578       {
579         pos->set = GNUNET_YES;
580         initial_set = 1;
581       }
582       pos->msg->value = GNUNET_htonll (pos->value);
583       pos->msg->flags = msg->flags;
584       pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
585       if (prev != NULL)
586       {
587         /* move to front for faster setting next time! */
588         prev->next = pos->next;
589         pos->next = start;
590         start = pos;
591       }
592       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593                   "Statistic `%s:%s' updated to value %llu.\n", service, name,
594                   pos->value);
595       if ((changed) || (1 == initial_set))
596         notify_change (pos);
597       GNUNET_SERVER_receive_done (client, GNUNET_OK);
598       return;
599     }
600     prev = pos;
601     pos = pos->next;
602   }
603   pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
604   pos->next = start;
605   if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
606       (0 < (int64_t) GNUNET_ntohll (msg->value)))
607   {
608     pos->value = GNUNET_ntohll (msg->value);
609     pos->set = GNUNET_YES;
610   }
611   else
612   {
613     pos->set = GNUNET_NO;
614   }
615   pos->uid = uidgen++;
616   pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
617   pos->msg = (void *) &pos[1];
618   memcpy (pos->msg, message, ntohs (message->size));
619   pos->service = (const char *) &pos->msg[1];
620   pos->name = &pos->service[strlen (pos->service) + 1];
621
622   start = pos;
623   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624               "New statistic on `%s:%s' with value %llu created.\n", service,
625               name, pos->value);
626   GNUNET_SERVER_receive_done (client, GNUNET_OK);
627 }
628
629
630 /**
631  * Handle WATCH-message.
632  *
633  * @param cls closure
634  * @param client identification of the client
635  * @param message the actual message
636  */
637 static void
638 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
639               const struct GNUNET_MessageHeader *message)
640 {
641   char *service;
642   char *name;
643   uint16_t msize;
644   uint16_t size;
645   struct StatsEntry *pos;
646   struct ClientEntry *ce;
647   struct WatchEntry *we;
648   size_t slen;
649
650   if (NULL == nc)
651   {
652     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
653     return;
654   }
655   GNUNET_SERVER_client_mark_monitor (client);
656   ce = make_client_entry (client);
657   msize = ntohs (message->size);
658   if (msize < sizeof (struct GNUNET_MessageHeader))
659   {
660     GNUNET_break (0);
661     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
662     return;
663   }
664   size = msize - sizeof (struct GNUNET_MessageHeader);
665   if (size !=
666       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
667                                       &service, &name))
668   {
669     GNUNET_break (0);
670     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
671     return;
672   }
673   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674               "Received request to watch statistic on `%s:%s'\n", service,
675               name);
676   pos = start;
677   while (pos != NULL)
678   {
679     if (matches (pos, service, name))
680       break;
681     pos = pos->next;
682   }
683   if (pos == NULL)
684   {
685     pos =
686         GNUNET_malloc (sizeof (struct StatsEntry) +
687                        sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
688     pos->next = start;
689     pos->uid = uidgen++;
690     pos->set = GNUNET_NO;
691     pos->msg = (void *) &pos[1];
692     pos->msg->header.size =
693         htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
694     pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
695     pos->service = (const char *) &pos->msg[1];
696     slen = strlen (service) + 1;
697     memcpy ((void *) pos->service, service, slen);
698     pos->name = &pos->service[slen];
699     memcpy ((void *) pos->name, name, strlen (name) + 1);
700     start = pos;
701     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702                 "New statistic on `%s:%s' with value %llu created.\n", service,
703                 name, pos->value);
704   }
705   we = GNUNET_malloc (sizeof (struct WatchEntry));
706   we->client = client;
707   we->last_value_set = GNUNET_NO;
708   GNUNET_SERVER_client_keep (client);
709   we->wid = ce->max_wid++;
710   GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
711   if (pos->value != 0)
712     notify_change (pos);
713   GNUNET_SERVER_receive_done (client, GNUNET_OK);
714 }
715
716
717 /**
718  * Actually perform the shutdown.
719  */
720 static void 
721 do_shutdown ()
722 {
723   struct WatchEntry *we;
724   struct StatsEntry *se;
725
726   if (NULL == nc)
727     return;
728   save ();
729   GNUNET_SERVER_notification_context_destroy (nc);
730   nc = NULL;  
731   GNUNET_assert (NULL == client_head);
732   while (NULL != (se = start))
733   {
734     start = se->next;
735     while (NULL != (we = se->we_head))
736     {
737       GNUNET_SERVER_client_drop (we->client);
738       GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
739       GNUNET_free (we);
740     }
741     GNUNET_free (se);
742   }
743 }
744
745
746 /**
747  * Task run during shutdown.
748  *
749  * @param cls unused
750  * @param tc unused
751  */
752 static void
753 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
754 {
755   in_shutdown = GNUNET_YES;
756   if (NULL != client_head)
757     return;
758   do_shutdown ();
759 }
760
761
762 /**
763  * A client disconnected.  Remove all of its data structure entries.
764  *
765  * @param cls closure, NULL
766  * @param client identification of the client
767  */
768 static void
769 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
770 {
771   struct ClientEntry *ce;
772   struct WatchEntry *we;
773   struct WatchEntry *wen;
774   struct StatsEntry *se;
775
776   ce = client_head;
777   while (NULL != ce)
778   {
779     if (ce->client == client)
780     {
781       GNUNET_SERVER_client_drop (ce->client);
782       GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
783       GNUNET_free (ce);
784       break;
785     }
786     ce = ce->next;
787   }
788   se = start;
789   while (NULL != se)
790   {
791     wen = se->we_head;
792     while (NULL != (we = wen))
793     {
794       wen = we->next;
795       if (we->client != client)
796         continue;
797       GNUNET_SERVER_client_drop (we->client);
798       GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
799       GNUNET_free (we);
800     }
801     se = se->next;
802   }
803   if ( (NULL == client_head) &&
804        (GNUNET_YES == in_shutdown) )
805     do_shutdown ();
806 }
807
808
809 /**
810  * Process statistics requests.
811  *
812  * @param cls closure
813  * @param server the initialized server
814  * @param c configuration to use
815  */
816 static void
817 run (void *cls, struct GNUNET_SERVER_Handle *server,
818      const struct GNUNET_CONFIGURATION_Handle *c)
819 {
820   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
821     {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
822     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
823     {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
824     {NULL, NULL, 0, 0}
825   };
826   cfg = c;
827   srv = server;
828   GNUNET_SERVER_add_handlers (server, handlers);
829   nc = GNUNET_SERVER_notification_context_create (server, 16);
830   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
831   load (server);
832   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
833                                 NULL);
834 }
835
836
837 /**
838  * The main function for the statistics service.
839  *
840  * @param argc number of arguments from the command line
841  * @param argv command line arguments
842  * @return 0 ok, 1 on error
843  */
844 int
845 main (int argc, char *const *argv)
846 {
847   return (GNUNET_OK ==
848           GNUNET_SERVICE_run (argc, argv, "statistics",
849                               GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run, NULL)) ? 0 : 1;
850 }
851
852 #ifdef LINUX
853 #include <malloc.h>
854
855 /**
856  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
857  */
858 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
859 {
860   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
861   mallopt (M_TOP_PAD, 1 * 1024);
862   malloc_trim (0);
863 }
864 #endif
865
866
867 /* end of gnunet-service-statistics.c */