aboutsummaryrefslogtreecommitdiff
path: root/src/util/helper.c
blob: a84b06e6645d013c3e18fedf80afb88e42ef3440 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
     This file is part of GNUnet.
     Copyright (C) 2011, 2012 Christian Grothoff

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file util/helper.c
 * @brief API for dealing with (SUID) helper processes that communicate via
 *          GNUNET_MessageHeaders on stdin/stdout
 * @author Philipp Toelke
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_mst_lib.h"


/**
 * Entry in the queue of messages we need to transmit to the helper.
 */
struct GNUNET_HELPER_SendHandle
{

  /**
   * This is an entry in a DLL.
   */
  struct GNUNET_HELPER_SendHandle *next;

  /**
   * This is an entry in a DLL.
   */
  struct GNUNET_HELPER_SendHandle *prev;

  /**
   * Message to transmit (allocated at the end of this struct)
   */
  const struct GNUNET_MessageHeader *msg;

  /**
   * The handle to a helper process.
   */
  struct GNUNET_HELPER_Handle *h;

  /**
   * Function to call upon completion.
   */
  GNUNET_HELPER_Continuation cont;

  /**
   * Closure to 'cont'.
   */
  void *cont_cls;

  /**
   * Current write position.
   */
  unsigned int wpos;

};


/**
 * The handle to a helper process.
 */
struct GNUNET_HELPER_Handle
{

  /**
   * PipeHandle to receive data from the helper
   */
  struct GNUNET_DISK_PipeHandle *helper_in;

  /**
   * PipeHandle to send data to the helper
   */
  struct GNUNET_DISK_PipeHandle *helper_out;

  /**
   * FileHandle to receive data from the helper
   */
  const struct GNUNET_DISK_FileHandle *fh_from_helper;

  /**
   * FileHandle to send data to the helper
   */
  const struct GNUNET_DISK_FileHandle *fh_to_helper;

  /**
   * The process id of the helper
   */
  struct GNUNET_OS_Process *helper_proc;

  /**
   * The Message-Tokenizer that tokenizes the messages comming from the helper
   */
  struct GNUNET_MessageStreamTokenizer *mst;

  /**
   * The exception callback
   */
  GNUNET_HELPER_ExceptionCallback exp_cb;

  /**
   * The closure for callbacks
   */
  void *cb_cls;

  /**
   * First message queued for transmission to helper.
   */
  struct GNUNET_HELPER_SendHandle *sh_head;

  /**
   * Last message queued for transmission to helper.
   */
  struct GNUNET_HELPER_SendHandle *sh_tail;

  /**
   * Binary to run.
   */
  char *binary_name;

  /**
   * NULL-terminated list of command-line arguments.
   */
  char **binary_argv;

  /**
   * Task to read from the helper.
   */
  struct GNUNET_SCHEDULER_Task *read_task;

  /**
   * Task to read from the helper.
   */
  struct GNUNET_SCHEDULER_Task *write_task;

  /**
   * Restart task.
   */
  struct GNUNET_SCHEDULER_Task *restart_task;

  /**
   * Does the helper support the use of a control pipe for signalling?
   */
  int with_control_pipe;

  /**
   * Count start attempts to increase linear back off
   */
  unsigned int retry_back_off;
};


/**
 * Sends termination signal to the helper process.  The helper process is not
 * reaped; call GNUNET_HELPER_wait() for reaping the dead helper process.
 *
 * @param h the helper handle
 * @param soft_kill if GNUNET_YES, signals termination by closing the helper's
 *          stdin; GNUNET_NO to signal termination by sending SIGTERM to helper
 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
 */
int
GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h,
		    int soft_kill)
{
  struct GNUNET_HELPER_SendHandle *sh;
  int ret;

  while (NULL != (sh = h->sh_head))
  {
    GNUNET_CONTAINER_DLL_remove (h->sh_head,
				 h->sh_tail,
				 sh);
    if (NULL != sh->cont)
      sh->cont (sh->cont_cls, GNUNET_NO);
    GNUNET_free (sh);
  }
  if (NULL != h->restart_task)
  {
    GNUNET_SCHEDULER_cancel (h->restart_task);
    h->restart_task = NULL;
  }
  if (NULL != h->read_task)
  {
    GNUNET_SCHEDULER_cancel (h->read_task);
    h->read_task = NULL;
  }
  if (NULL == h->helper_proc)
    return GNUNET_SYSERR;
  if (GNUNET_YES == soft_kill)
  {
    /* soft-kill only possible with pipes */
    GNUNET_assert (NULL != h->helper_in);
    ret = GNUNET_DISK_pipe_close (h->helper_in);
    h->helper_in = NULL;
    h->fh_to_helper = NULL;
    return ret;
  }
  if (0 != GNUNET_OS_process_kill (h->helper_proc, GNUNET_TERM_SIG))
    return GNUNET_SYSERR;
  return GNUNET_OK;
}


/**
 * Reap the helper process.  This call is blocking(!).  The helper process
 * should either be sent a termination signal before or should be dead before
 * calling this function
 *
 * @param h the helper handle
 * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
 */
int
GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h)
{
  struct GNUNET_HELPER_SendHandle *sh;
  int ret;

  ret = GNUNET_SYSERR;
  if (NULL != h->helper_proc)
  {
    ret = GNUNET_OS_process_wait (h->helper_proc);
    GNUNET_OS_process_destroy (h->helper_proc);
    h->helper_proc = NULL;
  }
  if (NULL != h->read_task)
  {
    GNUNET_SCHEDULER_cancel (h->read_task);
    h->read_task = NULL;
  }
  if (NULL != h->write_task)
  {
    GNUNET_SCHEDULER_cancel (h->write_task);
    h->write_task = NULL;
  }
  if (NULL != h->helper_in)
  {
    GNUNET_DISK_pipe_close (h->helper_in);
    h->helper_in = NULL;
    h->fh_to_helper = NULL;
  }
  if (NULL != h->helper_out)
  {
    GNUNET_DISK_pipe_close (h->helper_out);
    h->helper_out = NULL;
    h->fh_from_helper = NULL;
  }
  while (NULL != (sh = h->sh_head))
  {
    GNUNET_CONTAINER_DLL_remove (h->sh_head,
				 h->sh_tail,
				 sh);
    if (NULL != sh->cont)
      sh->cont (sh->cont_cls, GNUNET_NO);
    GNUNET_free (sh);
  }
  /* purge MST buffer */
  if (NULL != h->mst)
    (void) GNUNET_MST_from_buffer (h->mst,
                                   NULL, 0,
                                   GNUNET_YES,
                                   GNUNET_NO);
  return ret;
}


/**
 * Stop the helper process, we're closing down or had an error.
 *
 * @param h handle to the helper process
 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
 *          stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
 */
static void
stop_helper (struct GNUNET_HELPER_Handle *h,
	     int soft_kill)
{
  if (NULL != h->restart_task)
  {
    GNUNET_SCHEDULER_cancel (h->restart_task);
    h->restart_task = NULL;
  }
  else
  {
    GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
    GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (h));
  }
}


/**
 * Restart the helper process.
 *
 * @param cls handle to the helper process
 */
static void
restart_task (void *cls);


/**
 * Read from the helper-process
 *
 * @param cls handle to the helper process
 */
static void
helper_read (void *cls)
{
  struct GNUNET_HELPER_Handle *h = cls;
  char buf[GNUNET_MAX_MESSAGE_SIZE] GNUNET_ALIGN;
  ssize_t t;

  h->read_task = NULL;
  t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
  if (t < 0)
  {
    /* On read-error, restart the helper */
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _("Error reading from `%s': %s\n"),
		h->binary_name,
		STRERROR (errno));
    if (NULL != h->exp_cb)
    {
      h->exp_cb (h->cb_cls);
      GNUNET_HELPER_stop (h, GNUNET_NO);
      return;
    }
    stop_helper (h, GNUNET_NO);
    /* Restart the helper */
    h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
										   h->retry_back_off),
						    &restart_task, h);
    return;
  }
  if (0 == t)
  {
    /* this happens if the helper is shut down via a
       signal, so it is not a "hard" error */
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Got 0 bytes from helper `%s' (EOF)\n",
		h->binary_name);
    if (NULL != h->exp_cb)
    {
      h->exp_cb (h->cb_cls);
      GNUNET_HELPER_stop (h, GNUNET_NO);
      return;
    }
    stop_helper (h, GNUNET_NO);
    /* Restart the helper */
    h->restart_task
      = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
								   h->retry_back_off),
				     &restart_task, h);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Got %u bytes from helper `%s'\n",
	      (unsigned int) t,
	      h->binary_name);
  h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
						 h->fh_from_helper,
						 &helper_read, h);
  if (GNUNET_SYSERR ==
      GNUNET_MST_from_buffer (h->mst,
                              buf, t,
                              GNUNET_NO,
                              GNUNET_NO))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
		_("Failed to parse inbound message from helper `%s'\n"),
		h->binary_name);
    if (NULL != h->exp_cb)
    {
      h->exp_cb (h->cb_cls);
      GNUNET_HELPER_stop (h, GNUNET_NO);
      return;
    }
    stop_helper (h, GNUNET_NO);
    /* Restart the helper */
    h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
										  h->retry_back_off),
						    &restart_task, h);
    return;
  }
}


/**
 * Start the helper process.
 *
 * @param h handle to the helper process
 */
static void
start_helper (struct GNUNET_HELPER_Handle *h)
{
  h->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
  h->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
  if ( (h->helper_in == NULL) || (h->helper_out == NULL))
  {
    /* out of file descriptors? try again later... */
    stop_helper (h, GNUNET_NO);
    h->restart_task =
      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
								  h->retry_back_off),
				    &restart_task, h);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Starting HELPER process `%s'\n",
	      h->binary_name);
  h->fh_from_helper =
      GNUNET_DISK_pipe_handle (h->helper_out, GNUNET_DISK_PIPE_END_READ);
  h->fh_to_helper =
      GNUNET_DISK_pipe_handle (h->helper_in, GNUNET_DISK_PIPE_END_WRITE);
  h->helper_proc =
    GNUNET_OS_start_process_vap (h->with_control_pipe, GNUNET_OS_INHERIT_STD_ERR,
				 h->helper_in, h->helper_out, NULL,
				 h->binary_name,
				 h->binary_argv);
  if (NULL == h->helper_proc)
  {
    /* failed to start process? try again later... */
    stop_helper (h, GNUNET_NO);
    h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
										  h->retry_back_off),
						    &restart_task, h);
    return;
  }
  GNUNET_DISK_pipe_close_end (h->helper_out, GNUNET_DISK_PIPE_END_WRITE);
  GNUNET_DISK_pipe_close_end (h->helper_in, GNUNET_DISK_PIPE_END_READ);
  if (NULL != h->mst)
    h->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
						   h->fh_from_helper,
						   &helper_read,
						   h);
}


/**
 * Restart the helper process.
 *
 * @param cls handle to the helper process
 */
static void
restart_task (void *cls)
{
  struct GNUNET_HELPER_Handle*h = cls;

  h->restart_task = NULL;
  h->retry_back_off++;
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "Restarting helper with back-off %u\n",
              h->retry_back_off);
  start_helper (h);
}


/**
 * Starts a helper and begins reading from it. The helper process is
 * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
 * or when the exp_cb callback is not NULL.
 *
 * @param with_control_pipe does the helper support the use of a control pipe for signalling?
 * @param binary_name name of the binary to run
 * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
 *                    argument must not be modified by the client for
 *                     the lifetime of the helper handle)
 * @param cb function to call if we get messages from the helper
 * @param exp_cb the exception callback to call. Set this to NULL if the helper
 *          process has to be restarted automatically when it dies/crashes
 * @param cb_cls closure for the above callback
 * @return the new Handle, NULL on error
 */
struct GNUNET_HELPER_Handle *
GNUNET_HELPER_start (int with_control_pipe,
		     const char *binary_name,
		     char *const binary_argv[],
		     GNUNET_MessageTokenizerCallback cb,
		     GNUNET_HELPER_ExceptionCallback exp_cb,
		     void *cb_cls)
{
  struct GNUNET_HELPER_Handle *h;
  unsigned int c;

  h = GNUNET_new (struct GNUNET_HELPER_Handle);
  h->with_control_pipe = with_control_pipe;
  /* Lookup in libexec path only if we are starting gnunet helpers */
  if (NULL != strstr (binary_name, "gnunet"))
    h->binary_name = GNUNET_OS_get_libexec_binary_path (binary_name);
  else
    h->binary_name = GNUNET_strdup (binary_name);
  for (c = 0; NULL != binary_argv[c]; c++);
  h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
  for (c = 0; NULL != binary_argv[c]; c++)
    h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
  h->binary_argv[c] = NULL;
  h->cb_cls = cb_cls;
  if (NULL != cb)
    h->mst = GNUNET_MST_create (cb,
                                h->cb_cls);
  h->exp_cb = exp_cb;
  h->retry_back_off = 0;
  start_helper (h);
  return h;
}


/**
 * Free's the resources occupied by the helper handle
 *
 * @param h the helper handle to free
 */
void
GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h)
{
  unsigned int c;
  struct GNUNET_HELPER_SendHandle *sh;

  if (NULL != h->write_task)
  {
    GNUNET_SCHEDULER_cancel (h->write_task);
    h->write_task = NULL;
  }
  GNUNET_assert (NULL == h->read_task);
  GNUNET_assert (NULL == h->restart_task);
  while (NULL != (sh = h->sh_head))
  {
    GNUNET_CONTAINER_DLL_remove (h->sh_head,
				 h->sh_tail,
				 sh);
    if (NULL != sh->cont)
      sh->cont (sh->cont_cls, GNUNET_SYSERR);
    GNUNET_free (sh);
  }
  if (NULL != h->mst)
    GNUNET_MST_destroy (h->mst);
  GNUNET_free (h->binary_name);
  for (c = 0; h->binary_argv[c] != NULL; c++)
    GNUNET_free (h->binary_argv[c]);
  GNUNET_free (h->binary_argv);
  GNUNET_free (h);
}


/**
 * Kills the helper, closes the pipe and frees the handle
 *
 * @param h handle to helper to stop
 * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
 *          stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
 */
void
GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h,
		    int soft_kill)
{
  h->exp_cb = NULL;
  stop_helper (h, soft_kill);
  GNUNET_HELPER_destroy (h);
}


/**
 * Write to the helper-process
 *
 * @param cls handle to the helper process
 */
static void
helper_write (void *cls)
{
  struct GNUNET_HELPER_Handle *h = cls;
  struct GNUNET_HELPER_SendHandle *sh;
  const char *buf;
  ssize_t t;

  h->write_task = NULL;
  if (NULL == (sh = h->sh_head))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Helper write had no work!\n");
    return; /* how did this happen? */
  }
  buf = (const char*) sh->msg;
  t = GNUNET_DISK_file_write (h->fh_to_helper,
			      &buf[sh->wpos],
			      ntohs (sh->msg->size) - sh->wpos);
  if (-1 == t)
  {
    /* On write-error, restart the helper */
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
		_("Error writing to `%s': %s\n"),
		h->binary_name,
		STRERROR (errno));
    if (NULL != h->exp_cb)
    {
      h->exp_cb (h->cb_cls);
      GNUNET_HELPER_stop (h, GNUNET_NO);
      return;
    }
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Stopping and restarting helper task!\n");
    stop_helper (h, GNUNET_NO);
    /* Restart the helper */
    h->restart_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS,
										  h->retry_back_off),
						    &restart_task, h);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Transmitted %u bytes to %s\n",
	      (unsigned int) t,
	      h->binary_name);
  sh->wpos += t;
  if (sh->wpos == ntohs (sh->msg->size))
  {
    GNUNET_CONTAINER_DLL_remove (h->sh_head,
				 h->sh_tail,
				 sh);
    if (NULL != sh->cont)
      sh->cont (sh->cont_cls, GNUNET_YES);
    GNUNET_free (sh);
  }
  if (NULL != h->sh_head)
    h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
						     h->fh_to_helper,
						     &helper_write,
						     h);
}


/**
 * Send an message to the helper.
 *
 * @param h helper to send message to
 * @param msg message to send
 * @param can_drop can the message be dropped if there is already one in the queue?
 * @param cont continuation to run once the message is out (#GNUNET_OK on succees, #GNUNET_NO
 *             if the helper process died, #GNUNET_SYSERR during #GNUNET_HELPER_destroy).
 * @param cont_cls closure for @a cont
 * @return NULL if the message was dropped,
 *         otherwise handle to cancel *cont* (actual transmission may
 *         not be abortable)
 */
struct GNUNET_HELPER_SendHandle *
GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
		    const struct GNUNET_MessageHeader *msg,
		    int can_drop,
		    GNUNET_HELPER_Continuation cont,
		    void *cont_cls)
{
  struct GNUNET_HELPER_SendHandle *sh;
  uint16_t mlen;

  if (NULL == h->fh_to_helper)
    return NULL;
  if ( (GNUNET_YES == can_drop) &&
       (NULL != h->sh_head) )
    return NULL;
  mlen = ntohs (msg->size);
  sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
  sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
  GNUNET_memcpy (&sh[1], msg, mlen);
  sh->h = h;
  sh->cont = cont;
  sh->cont_cls = cont_cls;
  GNUNET_CONTAINER_DLL_insert_tail (h->sh_head,
				    h->sh_tail,
				    sh);
  if (NULL == h->write_task)
    h->write_task = GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
						     h->fh_to_helper,
						     &helper_write,
						     h);

  return sh;
}

/**
 * Cancel a #GNUNET_HELPER_send operation.  If possible, transmitting the
 * message is also aborted, but at least 'cont' won't be
 * called.
 *
 * @param sh operation to cancel
 */
void
GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh)
{
  struct GNUNET_HELPER_Handle *h = sh->h;

  sh->cont = NULL;
  sh->cont_cls = NULL;
  if (0 == sh->wpos)
  {
    GNUNET_CONTAINER_DLL_remove (h->sh_head, h->sh_tail, sh);
    GNUNET_free (sh);
    if (NULL == h->sh_head)
    {
      GNUNET_SCHEDULER_cancel (h->write_task);
      h->write_task = NULL;
    }
  }
}


/* end of helper.c */