Fix the use of (GNUNET_)strdup and (GNUNET_)free
[oweals/gnunet.git] / src / util / helper.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 Christian Grothoff
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 util/helper.c
23  * @brief API for dealing with (SUID) helper processes that communicate via
24  *          GNUNET_MessageHeaders on stdin/stdout
25  * @author Philipp Toelke
26  * @author Christian Grothoff
27  */
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30
31
32 /**
33  * Entry in the queue of messages we need to transmit to the helper.
34  */
35 struct GNUNET_HELPER_SendHandle
36 {
37
38   /**
39    * This is an entry in a DLL.
40    */
41   struct GNUNET_HELPER_SendHandle *next;
42
43   /**
44    * This is an entry in a DLL.
45    */
46   struct GNUNET_HELPER_SendHandle *prev;
47
48   /**
49    * Message to transmit (allocated at the end of this struct)
50    */
51   const struct GNUNET_MessageHeader *msg;
52
53   /**
54    * The handle to a helper process.
55    */
56   struct GNUNET_HELPER_Handle *h;
57
58   /**
59    * Function to call upon completion.
60    */
61   GNUNET_HELPER_Continuation cont;
62
63   /**
64    * Closure to 'cont'.
65    */
66   void *cont_cls;
67
68   /**
69    * Current write position.
70    */
71   unsigned int wpos;
72
73 };
74
75
76 /**
77  * The handle to a helper process.
78  */
79 struct GNUNET_HELPER_Handle
80 {
81
82   /**
83    * PipeHandle to receive data from the helper
84    */
85   struct GNUNET_DISK_PipeHandle *helper_in;
86
87   /**
88    * PipeHandle to send data to the helper
89    */
90   struct GNUNET_DISK_PipeHandle *helper_out;
91
92   /**
93    * FileHandle to receive data from the helper
94    */
95   const struct GNUNET_DISK_FileHandle *fh_from_helper;
96
97   /**
98    * FileHandle to send data to the helper
99    */
100   const struct GNUNET_DISK_FileHandle *fh_to_helper;
101
102   /**
103    * The process id of the helper
104    */
105   struct GNUNET_OS_Process *helper_proc;
106
107   /**
108    * The Message-Tokenizer that tokenizes the messages comming from the helper
109    */
110   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
111
112   /**
113    * The exception callback
114    */
115   GNUNET_HELPER_ExceptionCallback exp_cb;
116
117   /**
118    * The closure for callbacks
119    */
120   void *cb_cls;
121
122   /**
123    * First message queued for transmission to helper.
124    */
125   struct GNUNET_HELPER_SendHandle *sh_head;
126
127   /**
128    * Last message queued for transmission to helper.
129    */
130   struct GNUNET_HELPER_SendHandle *sh_tail;
131
132   /**
133    * Binary to run.
134    */
135   char *binary_name;
136
137   /**
138    * NULL-terminated list of command-line arguments.
139    */
140   char **binary_argv;
141                 
142   /**
143    * Task to read from the helper.
144    */
145   struct GNUNET_SCHEDULER_Task * read_task;
146
147   /**
148    * Task to read from the helper.
149    */
150   struct GNUNET_SCHEDULER_Task * write_task;
151
152   /**
153    * Restart task.
154    */
155   struct GNUNET_SCHEDULER_Task * restart_task;
156
157   /**
158    * Does the helper support the use of a control pipe for signalling?
159    */
160   int with_control_pipe;
161
162 };
163
164
165 /**
166  * Sends termination signal to the helper process.  The helper process is not
167  * reaped; call GNUNET_HELPER_wait() for reaping the dead helper process.
168  *
169  * @param h the helper handle
170  * @param soft_kill if GNUNET_YES, signals termination by closing the helper's
171  *          stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
172  * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
173  */
174 int
175 GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h,
176                     int soft_kill)
177 {
178   struct GNUNET_HELPER_SendHandle *sh;
179   int ret;
180
181   while (NULL != (sh = h->sh_head))
182   {
183     GNUNET_CONTAINER_DLL_remove (h->sh_head,
184                                  h->sh_tail,
185                                  sh);
186     if (NULL != sh->cont)
187       sh->cont (sh->cont_cls, GNUNET_NO);
188     GNUNET_free (sh);
189   }
190   if (NULL != h->restart_task)
191   {
192     GNUNET_SCHEDULER_cancel (h->restart_task);
193     h->restart_task = NULL;
194   }
195   if (NULL != h->read_task)
196   {
197     GNUNET_SCHEDULER_cancel (h->read_task);
198     h->read_task = NULL;
199   }
200   if (NULL == h->helper_proc)
201     return GNUNET_SYSERR;
202   if (GNUNET_YES == soft_kill)
203   {
204     /* soft-kill only possible with pipes */
205     GNUNET_assert (NULL != h->helper_in);
206     ret = GNUNET_DISK_pipe_close (h->helper_in);
207     h->helper_in = NULL;
208     h->fh_to_helper = NULL;
209     return ret;
210   }
211   if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG))
212     return GNUNET_SYSERR;
213   return GNUNET_OK;
214 }
215
216
217 /**
218  * Reap the helper process.  This call is blocking(!).  The helper process
219  * should either be sent a termination signal before or should be dead before
220  * calling this function
221  *
222  * @param h the helper handle
223  * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
224  */
225 int
226 GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
227 {
228   struct GNUNET_HELPER_SendHandle *sh;
229   int ret;
230
231   ret = GNUNET_SYSERR;
232   if (NULL != h->helper_proc)
233   {
234     ret = GNUNET_OS_process_wait (h->helper_proc);
235     GNUNET_OS_process_destroy (h->helper_proc);
236     h->helper_proc = NULL;
237   }
238   if (NULL != h->read_task)
239   {
240     GNUNET_SCHEDULER_cancel (h->read_task);
241     h->read_task = NULL;
242   }
243   if (NULL != h->write_task)
244   {
245     GNUNET_SCHEDULER_cancel (h->write_task);
246     h->write_task = NULL;
247   }
248   if (NULL != h->helper_in)
249   {
250     GNUNET_DISK_pipe_close (h->helper_in);
251     h->helper_in = NULL;
252     h->fh_to_helper = NULL;
253   }
254   if (NULL != h->helper_out)
255   {
256     GNUNET_DISK_pipe_close (h->helper_out);
257     h->helper_out = NULL;
258     h->fh_from_helper = NULL;
259   }
260   while (NULL != (sh = h->sh_head))
261   {
262     GNUNET_CONTAINER_DLL_remove (h->sh_head,
263                                  h->sh_tail,
264                                  sh);
265     if (NULL != sh->cont)
266       sh->cont (sh->cont_cls, GNUNET_NO);
267     GNUNET_free (sh);
268   }
269   /* purge MST buffer */
270   if (NULL != h->mst)
271     (void) GNUNET_SERVER_mst_receive (h->mst, NULL, NULL, 0, GNUNET_YES, GNUNET_NO);
272   return ret;
273 }
274
275
276 /**
277  * Stop the helper process, we're closing down or had an error.
278  *
279  * @param h handle to the helper process
280  * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
281  *          stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
282  */
283 static void
284 stop_helper (struct GNUNET_HELPER_Handle *h,
285              int soft_kill)
286 {
287   if (NULL != h->restart_task)
288   {
289     GNUNET_SCHEDULER_cancel (h->restart_task);
290     h->restart_task = NULL;
291   }
292   else
293   {
294     GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
295     GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h));
296   }
297 }
298
299
300 /**
301  * Restart the helper process.
302  *
303  * @param cls handle to the helper process
304  * @param tc scheduler context
305  */
306 static void
307 restart_task (void *cls,
308               const struct GNUNET_SCHEDULER_TaskContext *tc);
309
310
311 /**
312  * Read from the helper-process
313  *
314  * @param cls handle to the helper process
315  * @param tc scheduler context
316  */
317 static void
318 helper_read (void *cls,
319              const struct GNUNET_SCHEDULER_TaskContext *tc)
320 {
321   struct GNUNET_HELPER_Handle *h = cls;
322   char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
323   ssize_t t;
324
325   h->read_task = NULL;
326   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
327   {
328     /* try again */
329     h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
330                                                    h->fh_from_helper, &helper_read, h);
331     return;
332   }
333   t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
334   if (t < 0)
335   {
336     /* On read-error, restart the helper */
337     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
338                 _("Error reading from `%s': %s\n"),
339                 h->binary_name,
340                 STRERROR (errno));
341     if (NULL != h->exp_cb)
342     {
343       h->exp_cb (h->cb_cls);
344       GNUNET_HELPER_stop (h, GNUNET_NO);
345       return;
346     }
347     stop_helper (h, GNUNET_NO);
348     /* Restart the helper */
349     h->restart_task =
350         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &restart_task, h);
351     return;
352   }
353   if (0 == t)
354   {
355     /* this happens if the helper is shut down via a
356        signal, so it is not a "hard" error */
357     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358                 "Got 0 bytes from helper `%s' (EOF)\n",
359                 h->binary_name);
360     if (NULL != h->exp_cb)
361     {
362       h->exp_cb (h->cb_cls);
363       GNUNET_HELPER_stop (h, GNUNET_NO);
364       return;
365     }
366     stop_helper (h, GNUNET_NO);
367     /* Restart the helper */
368     h->restart_task =
369       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
370                                     &restart_task, h);
371     return;
372   }
373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374               "Got %u bytes from helper `%s'\n",
375               (unsigned int) t,
376               h->binary_name);
377   h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
378                                                  h->fh_from_helper, &helper_read, h);
379   if (GNUNET_SYSERR ==
380       GNUNET_SERVER_mst_receive (h->mst, NULL, buf, t, GNUNET_NO, GNUNET_NO))
381   {
382     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
383                 _("Failed to parse inbound message from helper `%s'\n"),
384                 h->binary_name);
385     if (NULL != h->exp_cb)
386     {
387       h->exp_cb (h->cb_cls);
388       GNUNET_HELPER_stop (h, GNUNET_NO);
389       return;
390     }
391     stop_helper (h, GNUNET_NO);
392     /* Restart the helper */
393     h->restart_task =
394         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
395                                       &restart_task, h);
396     return;
397   }
398 }
399
400
401 /**
402  * Start the helper process.
403  *
404  * @param h handle to the helper process
405  */
406 static void
407 start_helper (struct GNUNET_HELPER_Handle *h)
408 {
409   h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
410   h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
411   if ( (h->helper_in == NULL) || (h->helper_out == NULL))
412   {
413     /* out of file descriptors? try again later... */
414     stop_helper (h, GNUNET_NO);
415     h->restart_task =
416       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
417                                     &restart_task, h);
418     return;
419   }
420   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421               "Starting HELPER process `%s'\n",
422               h->binary_name);
423   h->fh_from_helper =
424       GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
425   h->fh_to_helper =
426       GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
427   h->helper_proc =
428     GNUNET_OS_start_process_vap (h->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR,
429                                  h->helper_in, h->helper_out, NULL,
430                                  h->binary_name,
431                                  h->binary_argv);
432   if (NULL == h->helper_proc)
433   {
434     /* failed to start process? try again later... */
435     stop_helper (h, GNUNET_NO);
436     h->restart_task =
437       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
438                                     &restart_task, h);
439     return;
440   }
441   GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
442   GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
443   if (NULL != h->mst)
444     h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
445                                                    h->fh_from_helper,
446                                                    &helper_read,
447                                                    h);
448 }
449
450
451 /**
452  * Restart the helper process.
453  *
454  * @param cls handle to the helper process
455  * @param tc scheduler context
456  */
457 static void
458 restart_task (void *cls,
459               const struct GNUNET_SCHEDULER_TaskContext *tc)
460 {
461   struct GNUNET_HELPER_Handle*h = cls;
462
463   h->restart_task = NULL;
464   start_helper (h);
465 }
466
467
468 /**
469  * Starts a helper and begins reading from it. The helper process is
470  * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
471  * or when the exp_cb callback is not NULL.
472  *
473  * @param with_control_pipe does the helper support the use of a control pipe for signalling?
474  * @param binary_name name of the binary to run
475  * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
476  *                    argument must not be modified by the client for
477  *                     the lifetime of the helper handle)
478  * @param cb function to call if we get messages from the helper
479  * @param exp_cb the exception callback to call. Set this to NULL if the helper
480  *          process has to be restarted automatically when it dies/crashes
481  * @param cb_cls closure for the above callback
482  * @return the new Handle, NULL on error
483  */
484 struct GNUNET_HELPER_Handle *
485 GNUNET_HELPER_start (int with_control_pipe,
486                      const char *binary_name,
487                      char *const binary_argv[],
488                      GNUNET_SERVER_MessageTokenizerCallback cb,
489                      GNUNET_HELPER_ExceptionCallback exp_cb,
490                      void *cb_cls)
491 {
492   struct GNUNET_HELPER_Handle *h;
493   unsigned int c;
494
495   h = GNUNET_new (struct GNUNET_HELPER_Handle);
496   h->with_control_pipe = with_control_pipe;
497   /* Lookup in libexec path only if we are starting gnunet helpers */
498   if (NULL != strstr (binary_name, "gnunet"))
499     h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name);
500   else
501     h->binary_name = GNUNET_strdup (binary_name);
502   for (c = 0; NULL != binary_argv[c]; c++);
503   h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
504   for (c = 0; NULL != binary_argv[c]; c++)
505     h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
506   h->binary_argv[c] = NULL;
507   h->cb_cls = cb_cls;
508   if (NULL != cb)
509     h->mst = GNUNET_SERVER_mst_create (cb, h->cb_cls);
510   h->exp_cb = exp_cb;
511   start_helper (h);
512   return h;
513 }
514
515
516 /**
517  * Free's the resources occupied by the helper handle
518  *
519  * @param h the helper handle to free
520  */
521 void
522 GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
523 {
524   unsigned int c;
525   struct GNUNET_HELPER_SendHandle *sh;
526
527   if (NULL != h->write_task)
528   {
529     GNUNET_SCHEDULER_cancel (h->write_task);
530     h->write_task = NULL;
531   }
532   GNUNET_assert (NULL == h->read_task);
533   GNUNET_assert (NULL == h->restart_task);
534   while (NULL != (sh = h->sh_head))
535   {
536     GNUNET_CONTAINER_DLL_remove (h->sh_head,
537                                  h->sh_tail,
538                                  sh);
539     if (NULL != sh->cont)
540       sh->cont (sh->cont_cls, GNUNET_SYSERR);
541     GNUNET_free (sh);
542   }
543   if (NULL != h->mst)
544     GNUNET_SERVER_mst_destroy (h->mst);
545   GNUNET_free (h->binary_name);
546   for (c = 0; h->binary_argv[c] != NULL; c++)
547     GNUNET_free (h->binary_argv[c]);
548   GNUNET_free (h->binary_argv);
549   GNUNET_free (h);
550 }
551
552
553 /**
554  * Kills the helper, closes the pipe and frees the handle
555  *
556  * @param h handle to helper to stop
557  * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
558  *          stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
559  */
560 void
561 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h,
562                     int soft_kill)
563 {
564   h->exp_cb = NULL;
565   stop_helper (h, soft_kill);
566   GNUNET_HELPER_destroy (h);
567 }
568
569
570 /**
571  * Write to the helper-process
572  *
573  * @param cls handle to the helper process
574  * @param tc scheduler context
575  */
576 static void
577 helper_write (void *cls,
578              const struct GNUNET_SCHEDULER_TaskContext *tc)
579 {
580   struct GNUNET_HELPER_Handle *h = cls;
581   struct GNUNET_HELPER_SendHandle *sh;
582   const char *buf;
583   ssize_t t;
584
585   h->write_task = NULL;
586   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
587   {
588     /* try again */
589     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590                 "Helper write triggered during shutdown, retrying\n");
591     h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
592                                                      h->fh_to_helper, &helper_write, h);
593     return;
594   }
595   if (NULL == (sh = h->sh_head))
596   {
597     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598                 "Helper write had no work!\n");
599     return; /* how did this happen? */
600   }
601   buf = (const char*) sh->msg;
602   t = GNUNET_DISK_file_write (h->fh_to_helper,
603                               &buf[sh->wpos],
604                               ntohs (sh->msg->size) - sh->wpos);
605   if (-1 == t)
606   {
607     /* On write-error, restart the helper */
608     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
609                 _("Error writing to `%s': %s\n"),
610                 h->binary_name,
611                 STRERROR (errno));
612     if (NULL != h->exp_cb)
613     {
614       h->exp_cb (h->cb_cls);
615       GNUNET_HELPER_stop (h, GNUNET_NO);
616       return;
617     }
618     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619                 "Stopping and restarting helper task!\n");
620     stop_helper (h, GNUNET_NO);
621     /* Restart the helper */
622     h->restart_task =
623       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
624                                     &restart_task, h);
625     return;
626   }
627   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628               "Transmitted %u bytes to %s\n",
629               (unsigned int) t,
630               h->binary_name);
631   sh->wpos += t;
632   if (sh->wpos == ntohs (sh->msg->size))
633   {
634     GNUNET_CONTAINER_DLL_remove (h->sh_head,
635                                  h->sh_tail,
636                                  sh);
637     if (NULL != sh->cont)
638       sh->cont (sh->cont_cls, GNUNET_YES);
639     GNUNET_free (sh);
640   }
641   if (NULL != h->sh_head)
642     h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
643                                                      h->fh_to_helper,
644                                                      &helper_write,
645                                                      h);
646 }
647
648
649 /**
650  * Send an message to the helper.
651  *
652  * @param h helper to send message to
653  * @param msg message to send
654  * @param can_drop can the message be dropped if there is already one in the queue?
655  * @param cont continuation to run once the message is out (#GNUNET_OK on succees, #GNUNET_NO
656  *             if the helper process died, #GNUNET_SYSERR during #GNUNET_HELPER_destroy).
657  * @param cont_cls closure for @a cont
658  * @return NULL if the message was dropped,
659  *         otherwise handle to cancel *cont* (actual transmission may
660  *         not be abortable)
661  */
662 struct GNUNET_HELPER_SendHandle *
663 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
664                     const struct GNUNET_MessageHeader *msg,
665                     int can_drop,
666                     GNUNET_HELPER_Continuation cont,
667                     void *cont_cls)
668 {
669   struct GNUNET_HELPER_SendHandle *sh;
670   uint16_t mlen;
671
672   if (NULL == h->fh_to_helper)
673     return NULL;
674   if ( (GNUNET_YES == can_drop) &&
675        (NULL != h->sh_head) )
676     return NULL;
677   mlen = ntohs (msg->size);
678   sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
679   sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
680   memcpy (&sh[1], msg, mlen);
681   sh->h = h;
682   sh->cont = cont;
683   sh->cont_cls = cont_cls;
684   GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
685                                     h->sh_tail,
686                                     sh);
687   if (NULL == h->write_task)
688     h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
689                                                      h->fh_to_helper,
690                                                      &helper_write,
691                                                      h);
692
693   return sh;
694 }
695
696 /**
697  * Cancel a #GNUNET_HELPER_send operation.  If possible, transmitting the
698  * message is also aborted, but at least 'cont' won't be
699  * called.
700  *
701  * @param sh operation to cancel
702  */
703 void
704 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
705 {
706   struct GNUNET_HELPER_Handle *h = sh->h;
707
708   sh->cont = NULL;
709   sh->cont_cls = NULL;
710   if (0 == sh->wpos)
711   {
712     GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
713     GNUNET_free (sh);
714     if (NULL == h->sh_head)
715     {
716       GNUNET_SCHEDULER_cancel (h->write_task);
717       h->write_task = NULL;
718     }
719   }
720 }
721
722
723 /* end of helper.c */