GNUnet  0.10.x
helper.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2011, 2012 Christian Grothoff
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_mst_lib.h"
31 
32 
37 {
38 
43 
48 
52  const struct GNUNET_MessageHeader *msg;
53 
58 
63 
67  void *cont_cls;
68 
72  unsigned int wpos;
73 
74 };
75 
76 
81 {
82 
87 
92 
97 
102 
107 
112 
117 
121  void *cb_cls;
122 
127 
132 
136  char *binary_name;
137 
141  char **binary_argv;
142 
147 
152 
157 
162 
166  unsigned int retry_back_off;
167 };
168 
169 
179 int
181  int soft_kill)
182 {
184  int ret;
185 
186  while (NULL != (sh = h->sh_head))
187  {
189  h->sh_tail,
190  sh);
191  if (NULL != sh->cont)
192  sh->cont (sh->cont_cls, GNUNET_NO);
193  GNUNET_free (sh);
194  }
195  if (NULL != h->restart_task)
196  {
198  h->restart_task = NULL;
199  }
200  if (NULL != h->read_task)
201  {
203  h->read_task = NULL;
204  }
205  if (NULL == h->helper_proc)
206  return GNUNET_SYSERR;
207  if (GNUNET_YES == soft_kill)
208  {
209  /* soft-kill only possible with pipes */
210  GNUNET_assert (NULL != h->helper_in);
212  h->helper_in = NULL;
213  h->fh_to_helper = NULL;
214  return ret;
215  }
217  return GNUNET_SYSERR;
218  return GNUNET_OK;
219 }
220 
221 
230 int
232 {
234  int ret;
235 
236  ret = GNUNET_SYSERR;
237  if (NULL != h->helper_proc)
238  {
241  h->helper_proc = NULL;
242  }
243  if (NULL != h->read_task)
244  {
246  h->read_task = NULL;
247  }
248  if (NULL != h->write_task)
249  {
251  h->write_task = NULL;
252  }
253  if (NULL != h->helper_in)
254  {
256  h->helper_in = NULL;
257  h->fh_to_helper = NULL;
258  }
259  if (NULL != h->helper_out)
260  {
262  h->helper_out = NULL;
263  h->fh_from_helper = NULL;
264  }
265  while (NULL != (sh = h->sh_head))
266  {
268  h->sh_tail,
269  sh);
270  if (NULL != sh->cont)
271  sh->cont (sh->cont_cls, GNUNET_NO);
272  GNUNET_free (sh);
273  }
274  /* purge MST buffer */
275  if (NULL != h->mst)
276  (void) GNUNET_MST_from_buffer (h->mst,
277  NULL, 0,
278  GNUNET_YES,
279  GNUNET_NO);
280  return ret;
281 }
282 
283 
291 static void
293  int soft_kill)
294 {
295  if (NULL != h->restart_task)
296  {
298  h->restart_task = NULL;
299  }
300  else
301  {
302  GNUNET_break (GNUNET_OK == GNUNET_HELPER_kill (h, soft_kill));
304  }
305 }
306 
307 
313 static void
314 restart_task (void *cls);
315 
316 
322 static void
323 helper_read (void *cls)
324 {
325  struct GNUNET_HELPER_Handle *h = cls;
327  ssize_t t;
328 
329  h->read_task = NULL;
330  t = GNUNET_DISK_file_read (h->fh_from_helper, &buf, sizeof (buf));
331  if (t < 0)
332  {
333  /* On read-error, restart the helper */
335  _("Error reading from `%s': %s\n"),
336  h->binary_name,
337  STRERROR (errno));
338  if (NULL != h->exp_cb)
339  {
340  h->exp_cb (h->cb_cls);
342  return;
343  }
344  stop_helper (h, GNUNET_NO);
345  /* Restart the helper */
347  h->retry_back_off),
348  &restart_task, h);
349  return;
350  }
351  if (0 == t)
352  {
353  /* this happens if the helper is shut down via a
354  signal, so it is not a "hard" error */
356  "Got 0 bytes from helper `%s' (EOF)\n",
357  h->binary_name);
358  if (NULL != h->exp_cb)
359  {
360  h->exp_cb (h->cb_cls);
362  return;
363  }
364  stop_helper (h, GNUNET_NO);
365  /* Restart the helper */
366  h->restart_task
368  h->retry_back_off),
369  &restart_task, h);
370  return;
371  }
373  "Got %u bytes from helper `%s'\n",
374  (unsigned int) t,
375  h->binary_name);
377  h->fh_from_helper,
378  &helper_read, h);
379  if (GNUNET_SYSERR ==
381  buf, t,
382  GNUNET_NO,
383  GNUNET_NO))
384  {
386  _("Failed to parse inbound message from helper `%s'\n"),
387  h->binary_name);
388  if (NULL != h->exp_cb)
389  {
390  h->exp_cb (h->cb_cls);
392  return;
393  }
394  stop_helper (h, GNUNET_NO);
395  /* Restart the helper */
397  h->retry_back_off),
398  &restart_task, h);
399  return;
400  }
401 }
402 
403 
409 static void
411 {
414  if ( (h->helper_in == NULL) || (h->helper_out == NULL))
415  {
416  /* out of file descriptors? try again later... */
417  stop_helper (h, GNUNET_NO);
418  h->restart_task =
420  h->retry_back_off),
421  &restart_task, h);
422  return;
423  }
425  "Starting HELPER process `%s'\n",
426  h->binary_name);
427  h->fh_from_helper =
429  h->fh_to_helper =
431  h->helper_proc =
433  h->helper_in, h->helper_out, NULL,
434  h->binary_name,
435  h->binary_argv);
436  if (NULL == h->helper_proc)
437  {
438  /* failed to start process? try again later... */
439  stop_helper (h, GNUNET_NO);
441  h->retry_back_off),
442  &restart_task, h);
443  return;
444  }
447  if (NULL != h->mst)
449  h->fh_from_helper,
450  &helper_read,
451  h);
452 }
453 
454 
460 static void
461 restart_task (void *cls)
462 {
463  struct GNUNET_HELPER_Handle*h = cls;
464 
465  h->restart_task = NULL;
466  h->retry_back_off++;
468  "Restarting helper with back-off %u\n",
469  h->retry_back_off);
470  start_helper (h);
471 }
472 
473 
490 struct GNUNET_HELPER_Handle *
492  const char *binary_name,
493  char *const binary_argv[],
496  void *cb_cls)
497 {
498  struct GNUNET_HELPER_Handle *h;
499  unsigned int c;
500 
501  h = GNUNET_new (struct GNUNET_HELPER_Handle);
503  /* Lookup in libexec path only if we are starting gnunet helpers */
504  if (NULL != strstr (binary_name, "gnunet"))
506  else
507  h->binary_name = GNUNET_strdup (binary_name);
508  for (c = 0; NULL != binary_argv[c]; c++);
509  h->binary_argv = GNUNET_malloc (sizeof (char *) * (c + 1));
510  for (c = 0; NULL != binary_argv[c]; c++)
511  h->binary_argv[c] = GNUNET_strdup (binary_argv[c]);
512  h->binary_argv[c] = NULL;
513  h->cb_cls = cb_cls;
514  if (NULL != cb)
515  h->mst = GNUNET_MST_create (cb,
516  h->cb_cls);
517  h->exp_cb = exp_cb;
518  h->retry_back_off = 0;
519  start_helper (h);
520  return h;
521 }
522 
523 
529 void
531 {
532  unsigned int c;
534 
535  if (NULL != h->write_task)
536  {
538  h->write_task = NULL;
539  }
540  GNUNET_assert (NULL == h->read_task);
541  GNUNET_assert (NULL == h->restart_task);
542  while (NULL != (sh = h->sh_head))
543  {
545  h->sh_tail,
546  sh);
547  if (NULL != sh->cont)
548  sh->cont (sh->cont_cls, GNUNET_SYSERR);
549  GNUNET_free (sh);
550  }
551  if (NULL != h->mst)
552  GNUNET_MST_destroy (h->mst);
554  for (c = 0; h->binary_argv[c] != NULL; c++)
555  GNUNET_free (h->binary_argv[c]);
557  GNUNET_free (h);
558 }
559 
560 
568 void
570  int soft_kill)
571 {
572  h->exp_cb = NULL;
573  stop_helper (h, soft_kill);
575 }
576 
577 
583 static void
584 helper_write (void *cls)
585 {
586  struct GNUNET_HELPER_Handle *h = cls;
588  const char *buf;
589  ssize_t t;
590 
591  h->write_task = NULL;
592  if (NULL == (sh = h->sh_head))
593  {
595  "Helper write had no work!\n");
596  return; /* how did this happen? */
597  }
598  buf = (const char*) sh->msg;
600  &buf[sh->wpos],
601  ntohs (sh->msg->size) - sh->wpos);
602  if (-1 == t)
603  {
604  /* On write-error, restart the helper */
606  _("Error writing to `%s': %s\n"),
607  h->binary_name,
608  STRERROR (errno));
609  if (NULL != h->exp_cb)
610  {
611  h->exp_cb (h->cb_cls);
613  return;
614  }
616  "Stopping and restarting helper task!\n");
617  stop_helper (h, GNUNET_NO);
618  /* Restart the helper */
620  h->retry_back_off),
621  &restart_task, h);
622  return;
623  }
625  "Transmitted %u bytes to %s\n",
626  (unsigned int) t,
627  h->binary_name);
628  sh->wpos += t;
629  if (sh->wpos == ntohs (sh->msg->size))
630  {
632  h->sh_tail,
633  sh);
634  if (NULL != sh->cont)
635  sh->cont (sh->cont_cls, GNUNET_YES);
636  GNUNET_free (sh);
637  }
638  if (NULL != h->sh_head)
640  h->fh_to_helper,
641  &helper_write,
642  h);
643 }
644 
645 
661  const struct GNUNET_MessageHeader *msg,
662  int can_drop,
664  void *cont_cls)
665 {
667  uint16_t mlen;
668 
669  if (NULL == h->fh_to_helper)
670  return NULL;
671  if ( (GNUNET_YES == can_drop) &&
672  (NULL != h->sh_head) )
673  return NULL;
674  mlen = ntohs (msg->size);
675  sh = GNUNET_malloc (sizeof (struct GNUNET_HELPER_SendHandle) + mlen);
676  sh->msg = (const struct GNUNET_MessageHeader*) &sh[1];
677  GNUNET_memcpy (&sh[1], msg, mlen);
678  sh->h = h;
679  sh->cont = cont;
680  sh->cont_cls = cont_cls;
682  h->sh_tail,
683  sh);
684  if (NULL == h->write_task)
686  h->fh_to_helper,
687  &helper_write,
688  h);
689 
690  return sh;
691 }
692 
700 void
702 {
703  struct GNUNET_HELPER_Handle *h = sh->h;
704 
705  sh->cont = NULL;
706  sh->cont_cls = NULL;
707  if (0 == sh->wpos)
708  {
710  GNUNET_free (sh);
711  if (NULL == h->sh_head)
712  {
714  h->write_task = NULL;
715  }
716  }
717 }
718 
719 
720 /* end of helper.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
int GNUNET_HELPER_kill(struct GNUNET_HELPER_Handle *h, int soft_kill)
Sends termination signal to the helper process.
Definition: helper.c:180
const struct GNUNET_DISK_FileHandle * fh_to_helper
FileHandle to send data to the helper.
Definition: helper.c:101
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:92
static void restart_task(void *cls)
Restart the helper process.
Definition: helper.c:461
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:364
struct GNUNET_SCHEDULER_Task * restart_task
Restart task.
Definition: helper.c:156
void(* GNUNET_HELPER_ExceptionCallback)(void *cls)
Callback that will be called when the helper process dies.
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:881
The handle to a helper process.
Definition: helper.c:80
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)
Starts a helper and begins reading from it.
Definition: helper.c:491
unsigned int wpos
Current write position.
Definition: helper.c:72
struct GNUNET_DISK_PipeHandle * helper_out
PipeHandle to send data to the helper.
Definition: helper.c:91
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_HELPER_SendHandle * prev
This is an entry in a DLL.
Definition: helper.c:47
void(* GNUNET_HELPER_Continuation)(void *cls, int result)
Continuation function.
#define GNUNET_TIME_UNIT_SECONDS
One second.
#define STRERROR(i)
Definition: plibc.h:676
void GNUNET_HELPER_stop(struct GNUNET_HELPER_Handle *h, int soft_kill)
Kills the helper, closes the pipe and frees the handle.
Definition: helper.c:569
Entry in the queue of messages we need to transmit to the helper.
Definition: helper.c:36
#define GNUNET_NO
Definition: gnunet_common.h:81
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_write_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *wfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1675
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1643
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_HELPER_SendHandle * sh_tail
Last message queued for transmission to helper.
Definition: helper.c:131
static struct GNUNET_SCHEDULER_Task * t
Main task.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
int with_control_pipe
Does the helper support the use of a control pipe for signalling?
Definition: helper.c:161
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
const struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd n)
Get the handle to a particular pipe end.
Definition: disk.c:2641
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
int(* GNUNET_MessageTokenizerCallback)(void *cls, const struct GNUNET_MessageHeader *message)
Functions with this signature are called whenever a complete message is received by the tokenizer...
struct GNUNET_SCHEDULER_Task * read_task
Task to read from the helper.
Definition: helper.c:146
struct GNUNET_SCHEDULER_Task * write_task
Task to read from the helper.
Definition: helper.c:151
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1246
void GNUNET_HELPER_send_cancel(struct GNUNET_HELPER_SendHandle *sh)
Cancel a GNUNET_HELPER_send operation.
Definition: helper.c:701
struct GNUNET_HELPER_Handle * h
The handle to a helper process.
Definition: helper.c:57
#define GNUNET_memcpy(dst, src, n)
void GNUNET_MST_destroy(struct GNUNET_MessageStreamTokenizer *mst)
Destroys a tokenizer.
Definition: mst.c:413
unsigned int retry_back_off
Count start attempts to increase linear back off.
Definition: helper.c:166
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:1049
static struct SolverHandle * sh
Handle to a message stream tokenizer.
Definition: mst.c:43
struct GNUNET_DISK_PipeHandle * helper_in
PipeHandle to receive data from the helper.
Definition: helper.c:86
struct GNUNET_OS_Process * GNUNET_OS_start_process_vap(int pipe_control, enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:1321
static char buf[2048]
struct GNUNET_MessageStreamTokenizer * GNUNET_MST_create(GNUNET_MessageTokenizerCallback cb, void *cb_cls)
Create a message stream tokenizer.
Definition: mst.c:87
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:439
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
The writing-end of a pipe.
The reading-end of a pipe.
int GNUNET_MST_from_buffer(struct GNUNET_MessageStreamTokenizer *mst, const char *buf, size_t size, int purge, int one_shot)
Add incoming data to the receive buffer and call the callback for all complete messages.
Definition: mst.c:116
struct GNUNET_HELPER_SendHandle * sh_head
First message queued for transmission to helper.
Definition: helper.c:126
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(int blocking_read, int blocking_write, int inherit_read, int inherit_write)
Creates an interprocess channel.
Definition: disk.c:2289
char * binary_name
Binary to run.
Definition: helper.c:136
GNUNET_HELPER_ExceptionCallback exp_cb
The exception callback.
Definition: helper.c:116
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
char ** binary_argv
NULL-terminated list of command-line arguments.
Definition: helper.c:141
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
void * cb_cls
The closure for callbacks.
Definition: helper.c:121
struct GNUNET_MessageStreamTokenizer * mst
The Message-Tokenizer that tokenizes the messages comming from the helper.
Definition: helper.c:111
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
static void start_helper(struct GNUNET_HELPER_Handle *h)
Start the helper process.
Definition: helper.c:410
static void stop_helper(struct GNUNET_HELPER_Handle *h, int soft_kill)
Stop the helper process, we&#39;re closing down or had an error.
Definition: helper.c:292
int GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:2603
int GNUNET_DISK_pipe_close_end(struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
Closes one half of an interprocess channel.
Definition: disk.c:2532
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to &#39;struct&#39;s...
void GNUNET_HELPER_destroy(struct GNUNET_HELPER_Handle *h)
Free&#39;s the resources occupied by the helper handle.
Definition: helper.c:530
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:1762
struct GNUNET_OS_Process * helper_proc
The process id of the helper.
Definition: helper.c:106
struct GNUNET_HELPER_SendHandle * next
This is an entry in a DLL.
Definition: helper.c:42
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)
Send an message to the helper.
Definition: helper.c:660
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
int GNUNET_HELPER_wait(struct GNUNET_HELPER_Handle *h)
Reap the helper process.
Definition: helper.c:231
char * GNUNET_OS_get_libexec_binary_path(const char *progname)
Given the name of a gnunet-helper, gnunet-service or gnunet-daemon binary, try to prefix it with the ...
Header for all communications.
#define GNUNET_YES
Definition: gnunet_common.h:80
static void helper_read(void *cls)
Read from the helper-process.
Definition: helper.c:323
#define GNUNET_TERM_SIG
The termination signal.
Definition: platform.h:282
Handle used to access files (and pipes).
Handle used to manage a pipe.
Definition: disk.c:66
const struct GNUNET_DISK_FileHandle * fh_from_helper
FileHandle to receive data from the helper.
Definition: helper.c:96
#define GNUNET_malloc(size)
Wrapper around malloc.
const struct GNUNET_MessageHeader * msg
Message to transmit (allocated at the end of this struct)
Definition: helper.c:52
GNUNET_HELPER_Continuation cont
Function to call upon completion.
Definition: helper.c:62
static void helper_write(void *cls)
Write to the helper-process.
Definition: helper.c:584
void * cont_cls
Closure to &#39;cont&#39;.
Definition: helper.c:67
#define GNUNET_free(ptr)
Wrapper around free.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:251
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965