-towards implementing regex service
[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 /**
502  * Handle SET-message.
503  *
504  * @param cls closure
505  * @param client identification of the client
506  * @param message the actual message
507  */
508 static void
509 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
510             const struct GNUNET_MessageHeader *message)
511 {
512   char *service;
513   char *name;
514   uint16_t msize;
515   uint16_t size;
516   const struct GNUNET_STATISTICS_SetMessage *msg;
517   struct StatsEntry *pos;
518   struct StatsEntry *prev;
519   uint32_t flags;
520   uint64_t value;
521   int64_t delta;
522   int changed;
523   int initial_set;
524
525   if ( (NULL != client) &&
526        (NULL == make_client_entry (client)) )
527     return; /* new client during shutdown */
528   msize = ntohs (message->size);
529   if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
530   {
531     GNUNET_break (0);
532     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
533     return;
534   }
535   size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
536   msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
537
538   if (size !=
539       GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
540                                       &name))
541   {
542     GNUNET_break (0);
543     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
544     return;
545   }
546   flags = ntohl (msg->flags);
547   value = GNUNET_ntohll (msg->value);
548   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549               "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
550               service, name, (unsigned int) flags, (unsigned long long) value);
551   pos = start;
552   prev = NULL;
553   while (pos != NULL)
554   {
555     if (matches (pos, service, name))
556     {
557       initial_set = 0;
558       if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
559       {
560         changed = (pos->value != value);
561         pos->value = value;
562       }
563       else
564       {
565         delta = (int64_t) value;
566         if ((delta < 0) && (pos->value < -delta))
567         {
568           changed = (pos->value != 0);
569           pos->value = 0;
570         }
571         else
572         {
573           changed = (delta != 0);
574           GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
575           pos->value += delta;
576         }
577       }
578       if (GNUNET_NO == pos->set)
579       {
580         pos->set = GNUNET_YES;
581         initial_set = 1;
582       }
583       pos->msg->value = GNUNET_htonll (pos->value);
584       pos->msg->flags = msg->flags;
585       pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
586       if (prev != NULL)
587       {
588         /* move to front for faster setting next time! */
589         prev->next = pos->next;
590         pos->next = start;
591         start = pos;
592       }
593       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
594                   "Statistic `%s:%s' updated to value %llu.\n", service, name,
595                   pos->value);
596       if ((changed) || (1 == initial_set))
597         notify_change (pos);
598       GNUNET_SERVER_receive_done (client, GNUNET_OK);
599       return;
600     }
601     prev = pos;
602     pos = pos->next;
603   }
604   pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
605   pos->next = start;
606   if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
607       (0 < (int64_t) GNUNET_ntohll (msg->value)))
608   {
609     pos->value = GNUNET_ntohll (msg->value);
610     pos->set = GNUNET_YES;
611   }
612   else
613   {
614     pos->set = GNUNET_NO;
615   }
616   pos->uid = uidgen++;
617   pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
618   pos->msg = (void *) &pos[1];
619   memcpy (pos->msg, message, ntohs (message->size));
620   pos->service = (const char *) &pos->msg[1];
621   pos->name = &pos->service[strlen (pos->service) + 1];
622
623   start = pos;
624   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625               "New statistic on `%s:%s' with value %llu created.\n", service,
626               name, pos->value);
627   GNUNET_SERVER_receive_done (client, GNUNET_OK);
628 }
629
630
631 /**
632  * Handle WATCH-message.
633  *
634  * @param cls closure
635  * @param client identification of the client
636  * @param message the actual message
637  */
638 static void
639 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
640               const struct GNUNET_MessageHeader *message)
641 {
642   char *service;
643   char *name;
644   uint16_t msize;
645   uint16_t size;
646   struct StatsEntry *pos;
647   struct ClientEntry *ce;
648   struct WatchEntry *we;
649   size_t slen;
650
651   if (NULL == nc)
652   {
653     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
654     return;
655   }
656   GNUNET_SERVER_client_mark_monitor (client);
657   ce = make_client_entry (client);
658   msize = ntohs (message->size);
659   if (msize < sizeof (struct GNUNET_MessageHeader))
660   {
661     GNUNET_break (0);
662     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
663     return;
664   }
665   size = msize - sizeof (struct GNUNET_MessageHeader);
666   if (size !=
667       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
668                                       &service, &name))
669   {
670     GNUNET_break (0);
671     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
672     return;
673   }
674   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675               "Received request to watch statistic on `%s:%s'\n", service,
676               name);
677   pos = start;
678   while (pos != NULL)
679   {
680     if (matches (pos, service, name))
681       break;
682     pos = pos->next;
683   }
684   if (pos == NULL)
685   {
686     pos =
687         GNUNET_malloc (sizeof (struct StatsEntry) +
688                        sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
689     pos->next = start;
690     pos->uid = uidgen++;
691     pos->set = GNUNET_NO;
692     pos->msg = (void *) &pos[1];
693     pos->msg->header.size =
694         htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
695     pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
696     pos->service = (const char *) &pos->msg[1];
697     slen = strlen (service) + 1;
698     memcpy ((void *) pos->service, service, slen);
699     pos->name = &pos->service[slen];
700     memcpy ((void *) pos->name, name, strlen (name) + 1);
701     start = pos;
702     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
703                 "New statistic on `%s:%s' with value %llu created.\n", service,
704                 name, pos->value);
705   }
706   we = GNUNET_malloc (sizeof (struct WatchEntry));
707   we->client = client;
708   we->last_value_set = GNUNET_NO;
709   GNUNET_SERVER_client_keep (client);
710   we->wid = ce->max_wid++;
711   GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
712   if (pos->value != 0)
713     notify_change (pos);
714   GNUNET_SERVER_receive_done (client, GNUNET_OK);
715 }
716
717
718 /**
719  * Actually perform the shutdown.
720  */
721 static void 
722 do_shutdown ()
723 {
724   struct WatchEntry *we;
725   struct StatsEntry *se;
726
727   if (NULL == nc)
728     return;
729   save ();
730   GNUNET_SERVER_notification_context_destroy (nc);
731   nc = NULL;  
732   GNUNET_assert (NULL == client_head);
733   while (NULL != (se = start))
734   {
735     start = se->next;
736     while (NULL != (we = se->we_head))
737     {
738       GNUNET_SERVER_client_drop (we->client);
739       GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
740       GNUNET_free (we);
741     }
742     GNUNET_free (se);
743   }
744 }
745
746
747 /**
748  * Task run during shutdown.
749  *
750  * @param cls unused
751  * @param tc unused
752  */
753 static void
754 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
755 {
756   in_shutdown = GNUNET_YES;
757   if (NULL != client_head)
758     return;
759   do_shutdown ();
760 }
761
762
763 /**
764  * A client disconnected.  Remove all of its data structure entries.
765  *
766  * @param cls closure, NULL
767  * @param client identification of the client
768  */
769 static void
770 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
771 {
772   struct ClientEntry *ce;
773   struct WatchEntry *we;
774   struct WatchEntry *wen;
775   struct StatsEntry *se;
776
777   ce = client_head;
778   while (NULL != ce)
779   {
780     if (ce->client == client)
781     {
782       GNUNET_SERVER_client_drop (ce->client);
783       GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
784       GNUNET_free (ce);
785       break;
786     }
787     ce = ce->next;
788   }
789   se = start;
790   while (NULL != se)
791   {
792     wen = se->we_head;
793     while (NULL != (we = wen))
794     {
795       wen = we->next;
796       if (we->client != client)
797         continue;
798       GNUNET_SERVER_client_drop (we->client);
799       GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
800       GNUNET_free (we);
801     }
802     se = se->next;
803   }
804   if ( (NULL == client_head) &&
805        (GNUNET_YES == in_shutdown) )
806     do_shutdown ();
807 }
808
809
810 /**
811  * Process statistics requests.
812  *
813  * @param cls closure
814  * @param server the initialized server
815  * @param c configuration to use
816  */
817 static void
818 run (void *cls, struct GNUNET_SERVER_Handle *server,
819      const struct GNUNET_CONFIGURATION_Handle *c)
820 {
821   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
822     {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
823     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
824     {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
825     {NULL, NULL, 0, 0}
826   };
827   cfg = c;
828   srv = server;
829   GNUNET_SERVER_add_handlers (server, handlers);
830   nc = GNUNET_SERVER_notification_context_create (server, 16);
831   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
832   load (server);
833   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
834                                 NULL);
835 }
836
837
838 /**
839  * The main function for the statistics service.
840  *
841  * @param argc number of arguments from the command line
842  * @param argv command line arguments
843  * @return 0 ok, 1 on error
844  */
845 int
846 main (int argc, char *const *argv)
847 {
848   return (GNUNET_OK ==
849           GNUNET_SERVICE_run (argc, argv, "statistics",
850                               GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run, NULL)) ? 0 : 1;
851 }
852
853 #ifdef LINUX
854 #include <malloc.h>
855
856 /**
857  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
858  */
859 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
860 {
861   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
862   mallopt (M_TOP_PAD, 1 * 1024);
863   malloc_trim (0);
864 }
865 #endif
866
867
868 /* end of gnunet-service-statistics.c */