GNUnet  0.17.6
Data Structures | Macros | Functions | Variables
os_priority.c File Reference

Methods to set process priority. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "disk.h"
#include <unistr.h>
Include dependency graph for os_priority.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_OS_Process
 
struct  GNUNET_OS_CommandHandle
 Handle to a command. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-os-priority", __VA_ARGS__)
 
#define LOG_STRERROR(kind, syscall)    GNUNET_log_from_strerror (kind, "util-os-priority", syscall)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)    GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename)
 
#define GNUNET_OS_CONTROL_PIPE   "GNUNET_OS_CONTROL_PIPE"
 

Functions

static void shutdown_pch (void *cls)
 This handler is called on shutdown to remove the pch. More...
 
static void parent_control_handler (void *cls)
 This handler is called when there are control data to be read on the pipe. More...
 
void GNUNET_OS_install_parent_control_handler (void *cls)
 Task that connects this process to its parent via pipe; essentially, the parent control handler will read signal numbers from the GNUNET_OS_CONTROL_PIPE (as given in an environment variable) and raise those signals. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_process_current ()
 Get process structure for current process. More...
 
int GNUNET_OS_process_kill (struct GNUNET_OS_Process *proc, int sig)
 Sends a signal to the process. More...
 
pid_t GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc)
 Get the pid of the process in question. More...
 
void GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
 Cleans up process structure contents (OS-dependent) and deallocates it. More...
 
static void open_dev_null (int target_fd, int flags)
 Open '/dev/null' and make the result the given file descriptor. More...
 
static struct GNUNET_OS_Processstart_process (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 int *lsocks, const char *filename, char *const argv[])
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_vap (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. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_va (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, va_list va)
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process (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,...)
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_v (enum GNUNET_OS_InheritStdioFlags std_inheritance, const int *lsocks, const char *filename, char *const argv[])
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_s (enum GNUNET_OS_InheritStdioFlags std_inheritance, const int *lsocks, const char *filename,...)
 Start a process. More...
 
static enum GNUNET_GenericReturnValue process_status (struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code, int options)
 Retrieve the status of a process, waiting on it if dead. More...
 
enum GNUNET_GenericReturnValue GNUNET_OS_process_status (struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
 Retrieve the status of a process. More...
 
enum GNUNET_GenericReturnValue GNUNET_OS_process_wait_status (struct GNUNET_OS_Process *proc, enum GNUNET_OS_ProcessStatusType *type, unsigned long *code)
 Retrieve the status of a process, waiting on it if dead. More...
 
int GNUNET_OS_process_wait (struct GNUNET_OS_Process *proc)
 Wait for a process to terminate. More...
 
void GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
 Stop/kill a command. More...
 
static void cmd_read (void *cls)
 Read from the process and call the line processor. More...
 
struct GNUNET_OS_CommandHandleGNUNET_OS_command_run (GNUNET_OS_LineProcessor proc, void *proc_cls, struct GNUNET_TIME_Relative timeout, const char *binary,...)
 Run the given command line and call the given function for each line of the output. More...
 

Variables

static struct GNUNET_OS_Process current_process
 Handle for 'this' process. More...
 
static struct GNUNET_SCHEDULER_Taskpch
 Handle for the parent_control_handler() Task. More...
 
static struct GNUNET_SCHEDULER_Taskspch
 Handle for the shutdown_pch() Task. More...
 

Detailed Description

Methods to set process priority.

Author
Nils Durner

Definition in file os_priority.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-os-priority", __VA_ARGS__)

Definition at line 32 of file os_priority.c.

◆ LOG_STRERROR

#define LOG_STRERROR (   kind,
  syscall 
)     GNUNET_log_from_strerror (kind, "util-os-priority", syscall)

Definition at line 34 of file os_priority.c.

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)     GNUNET_log_from_strerror_file (kind, "util-os-priority", syscall, filename)

Definition at line 37 of file os_priority.c.

◆ GNUNET_OS_CONTROL_PIPE

#define GNUNET_OS_CONTROL_PIPE   "GNUNET_OS_CONTROL_PIPE"

Definition at line 40 of file os_priority.c.

Function Documentation

◆ shutdown_pch()

static void shutdown_pch ( void *  cls)
static

This handler is called on shutdown to remove the pch.

Parameters
clsthe struct GNUNET_DISK_FileHandle of the control pipe

Definition at line 80 of file os_priority.c.

81 {
82  struct GNUNET_DISK_FileHandle *control_pipe = cls;
83 
85  pch = NULL;
86  GNUNET_DISK_file_close (control_pipe);
87  control_pipe = NULL;
88 }
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1306
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:957
static struct GNUNET_SCHEDULER_Task * pch
Handle for the parent_control_handler() Task.
Definition: os_priority.c:66
Handle used to access files (and pipes).

References GNUNET_DISK_file_close(), GNUNET_SCHEDULER_cancel(), and pch.

Referenced by GNUNET_OS_install_parent_control_handler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parent_control_handler()

static void parent_control_handler ( void *  cls)
static

This handler is called when there are control data to be read on the pipe.

Parameters
clsthe struct GNUNET_DISK_FileHandle of the control pipe

Definition at line 97 of file os_priority.c.

98 {
99  struct GNUNET_DISK_FileHandle *control_pipe = cls;
100  char sig;
101  char *pipe_fd;
102  ssize_t ret;
103 
104  pch = NULL;
105  ret = GNUNET_DISK_file_read (control_pipe, &sig, sizeof(sig));
106  if (sizeof(sig) != ret)
107  {
108  if (-1 == ret)
109  LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "GNUNET_DISK_file_read");
110  LOG (GNUNET_ERROR_TYPE_DEBUG, "Closing control pipe\n");
111  GNUNET_DISK_file_close (control_pipe);
112  control_pipe = NULL;
114  spch = NULL;
115  return;
116  }
117  pipe_fd = getenv (GNUNET_OS_CONTROL_PIPE);
118  GNUNET_assert ((NULL == pipe_fd) || (strlen (pipe_fd) <= 0));
120  "Got control code %d from parent via pipe %s\n",
121  sig,
122  pipe_fd);
124  control_pipe,
126  control_pipe);
127  GNUNET_SIGNAL_raise ((int) sig);
128 }
char * getenv()
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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:622
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
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:1652
void GNUNET_SIGNAL_raise(const int sig)
Raise the given signal by calling the installed signal handlers.
Definition: signal.c:97
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_OS_CONTROL_PIPE
Definition: os_priority.c:40
static void parent_control_handler(void *cls)
This handler is called when there are control data to be read on the pipe.
Definition: os_priority.c:97
static struct GNUNET_SCHEDULER_Task * spch
Handle for the shutdown_pch() Task.
Definition: os_priority.c:71
#define LOG(kind,...)
Definition: os_priority.c:32
#define LOG_STRERROR(kind, syscall)
Definition: os_priority.c:34

References getenv(), GNUNET_assert, GNUNET_DISK_file_close(), GNUNET_DISK_file_read(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_OS_CONTROL_PIPE, GNUNET_SCHEDULER_add_read_file(), GNUNET_SCHEDULER_cancel(), GNUNET_SIGNAL_raise(), GNUNET_TIME_UNIT_FOREVER_REL, LOG, LOG_STRERROR, pch, ret, and spch.

Referenced by GNUNET_OS_install_parent_control_handler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ open_dev_null()

static void open_dev_null ( int  target_fd,
int  flags 
)
static

Open '/dev/null' and make the result the given file descriptor.

Parameters
target_fddesired FD to point to /dev/null
flagsopen flags (O_RDONLY, O_WRONLY)

Definition at line 297 of file os_priority.c.

299 {
300  int fd;
301 
302  fd = open ("/dev/null", flags);
303  if (-1 == fd)
304  {
305  GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", "/dev/null");
306  return;
307  }
308  if (fd == target_fd)
309  return;
310  if (-1 == dup2 (fd, target_fd))
311  {
313  GNUNET_break (0 == close (fd));
314  return;
315  }
316  GNUNET_break (0 == close (fd));
317 }
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...

References GNUNET_break, GNUNET_ERROR_TYPE_ERROR, GNUNET_log_strerror, and GNUNET_log_strerror_file.

Referenced by start_process().

Here is the caller graph for this function:

◆ start_process()

static struct GNUNET_OS_Process* start_process ( 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 int *  lsocks,
const char *  filename,
char *const  argv[] 
)
static

Start a process.

Parameters
std_inheritancea set of GNUNET_OS_INHERIT_STD_* flags controlling which std handles of the parent are inherited by the child. pipe_stdin and pipe_stdout take priority over std_inheritance (when they are non-NULL).
pipe_stdinpipe to use to send input to child process (or NULL)
pipe_stdoutpipe to use to get output from child process (or NULL)
pipe_stderrpipe to use for stderr for child process (or NULL)
lsocksarray of listen sockets to dup systemd-style (or NULL); must be NULL on platforms where dup is not supported
filenamename of the binary
argvNULL-terminated list of arguments to the process
Returns
process ID of the new process, -1 on error

Definition at line 337 of file os_priority.c.

344 {
345  pid_t ret;
346  char fds[16];
347  struct GNUNET_OS_Process *gnunet_proc;
348  struct GNUNET_DISK_FileHandle *childpipe_read;
349  struct GNUNET_DISK_FileHandle *childpipe_write;
350  int childpipe_read_fd;
351  int i;
352  int j;
353  int k;
354  int tgt;
355  int flags;
356  int *lscp;
357  unsigned int ls;
358  int fd_stdout_write;
359  int fd_stdout_read;
360  int fd_stderr_write;
361  int fd_stderr_read;
362  int fd_stdin_read;
363  int fd_stdin_write;
364 
365  if (GNUNET_SYSERR ==
367  return NULL; /* not executable */
368  if (0 != (std_inheritance & GNUNET_OS_USE_PIPE_CONTROL))
369  {
370  struct GNUNET_DISK_PipeHandle *childpipe;
371  int dup_childpipe_read_fd = -1;
372 
374  if (NULL == childpipe)
375  return NULL;
376  childpipe_read =
378  childpipe_write =
380  GNUNET_DISK_pipe_close (childpipe);
381  if ((NULL == childpipe_read) || (NULL == childpipe_write) ||
382  (GNUNET_OK != GNUNET_DISK_internal_file_handle_ (childpipe_read,
383  &childpipe_read_fd,
384  sizeof(int))) ||
385  (-1 == (dup_childpipe_read_fd = dup (childpipe_read_fd))))
386  {
387  if (NULL != childpipe_read)
388  GNUNET_DISK_file_close (childpipe_read);
389  if (NULL != childpipe_write)
390  GNUNET_DISK_file_close (childpipe_write);
391  if (0 <= dup_childpipe_read_fd)
392  GNUNET_break (0 == close (dup_childpipe_read_fd));
393  return NULL;
394  }
395  childpipe_read_fd = dup_childpipe_read_fd;
396  GNUNET_DISK_file_close (childpipe_read);
397  }
398  else
399  {
400  childpipe_write = NULL;
401  childpipe_read_fd = -1;
402  }
403  if (NULL != pipe_stdin)
404  {
405  GNUNET_assert (
406  GNUNET_OK ==
409  &fd_stdin_read,
410  sizeof(int)));
411  GNUNET_assert (
412  GNUNET_OK ==
415  &fd_stdin_write,
416  sizeof(int)));
417  }
418  if (NULL != pipe_stdout)
419  {
420  GNUNET_assert (
421  GNUNET_OK ==
424  &fd_stdout_write,
425  sizeof(int)));
426  GNUNET_assert (
427  GNUNET_OK ==
430  &fd_stdout_read,
431  sizeof(int)));
432  }
433  if (NULL != pipe_stderr)
434  {
435  GNUNET_assert (
436  GNUNET_OK ==
439  &fd_stderr_read,
440  sizeof(int)));
441  GNUNET_assert (
442  GNUNET_OK ==
445  &fd_stderr_write,
446  sizeof(int)));
447  }
448  lscp = NULL;
449  ls = 0;
450  if (NULL != lsocks)
451  {
452  i = 0;
453  while (-1 != (k = lsocks[i++]))
454  GNUNET_array_append (lscp, ls, k);
455  GNUNET_array_append (lscp, ls, -1);
456  }
457 #if DARWIN
458  /* see https://web.archive.org/web/20150924082249/gnunet.org/vfork */
459  ret = vfork ();
460 #else
461  ret = fork ();
462 #endif
463  if (-1 == ret)
464  {
465  int eno = errno;
467  GNUNET_array_grow (lscp, ls, 0);
468  if (NULL != childpipe_write)
469  GNUNET_DISK_file_close (childpipe_write);
470  if (0 <= childpipe_read_fd)
471  GNUNET_break (0 == close (childpipe_read_fd));
472  errno = eno;
473  return NULL;
474  }
475  if (0 != ret)
476  {
477  unsetenv (GNUNET_OS_CONTROL_PIPE);
478  gnunet_proc = GNUNET_new (struct GNUNET_OS_Process);
479  gnunet_proc->pid = ret;
480  gnunet_proc->control_pipe = childpipe_write;
481  if (0 != (std_inheritance & GNUNET_OS_USE_PIPE_CONTROL))
482  {
483  GNUNET_break (0 == close (childpipe_read_fd));
484  }
485  GNUNET_array_grow (lscp, ls, 0);
486  return gnunet_proc;
487  }
488  if (0 <= childpipe_read_fd)
489  {
490  char fdbuf[100];
491 #ifndef DARWIN
492  /* due to vfork, we must NOT free memory on DARWIN! */
493  GNUNET_DISK_file_close (childpipe_write);
494 #endif
495  snprintf (fdbuf, 100, "%x", childpipe_read_fd);
496  setenv (GNUNET_OS_CONTROL_PIPE, fdbuf, 1);
497  }
498  else
499  unsetenv (GNUNET_OS_CONTROL_PIPE);
500  if (NULL != pipe_stdin)
501  {
502  GNUNET_break (0 == close (fd_stdin_write));
503  if (-1 == dup2 (fd_stdin_read, 0))
505  GNUNET_break (0 == close (fd_stdin_read));
506  }
507  else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_IN))
508  {
509  GNUNET_break (0 == close (0));
510  open_dev_null (0, O_RDONLY);
511  }
512  if (NULL != pipe_stdout)
513  {
514  GNUNET_break (0 == close (fd_stdout_read));
515  if (-1 == dup2 (fd_stdout_write, 1))
517  GNUNET_break (0 == close (fd_stdout_write));
518  }
519  else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_OUT))
520  {
521  GNUNET_break (0 == close (1));
522  open_dev_null (1, O_WRONLY);
523  }
524  if (NULL != pipe_stderr)
525  {
526  GNUNET_break (0 == close (fd_stderr_read));
527  if (-1 == dup2 (fd_stderr_write, 2))
529  GNUNET_break (0 == close (fd_stderr_write));
530  }
531  else if (0 == (std_inheritance & GNUNET_OS_INHERIT_STD_ERR))
532  {
533  GNUNET_break (0 == close (2));
534  open_dev_null (2, O_WRONLY);
535  }
536  if (NULL != lscp)
537  {
538  /* read systemd documentation... */
539  i = 0;
540  tgt = 3;
541  while (-1 != lscp[i])
542  {
543  j = i + 1;
544  while (-1 != lscp[j])
545  {
546  if (lscp[j] == tgt)
547  {
548  /* dup away */
549  k = dup (lscp[j]);
550  GNUNET_assert (-1 != k);
551  GNUNET_assert (0 == close (lscp[j]));
552  lscp[j] = k;
553  break;
554  }
555  j++;
556  }
557  if (lscp[i] != tgt)
558  {
559  /* Bury any existing FD, no matter what; they should all be closed
560  * on exec anyway and the important ones have been dup'ed away */
561  GNUNET_break (0 == close (tgt));
562  GNUNET_assert (-1 != dup2 (lscp[i], tgt));
563  }
564  /* unset close-on-exec flag */
565  flags = fcntl (tgt, F_GETFD);
566  GNUNET_assert (flags >= 0);
567  flags &= ~FD_CLOEXEC;
568  fflush (stderr);
569  (void) fcntl (tgt, F_SETFD, flags);
570  tgt++;
571  i++;
572  }
573  GNUNET_snprintf (fds, sizeof(fds), "%u", i);
574  setenv ("LISTEN_FDS", fds, 1);
575  }
576 #ifndef DARWIN
577  /* due to vfork, we must NOT free memory on DARWIN! */
578  GNUNET_array_grow (lscp, ls, 0);
579 #endif
580  execvp (filename, argv);
582  _exit (1);
583 }
enum GNUNET_GenericReturnValue GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, void *dst, size_t dst_len)
Retrieve OS file handle.
Definition: disk.c:1633
static char * filename
static struct GNUNET_NETWORK_Handle * ls
Listen socket for STUN processing.
Definition: gnunet-nat.c:85
struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_detach_end(struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
Detaches one of the ends from the pipe.
Definition: disk.c:1558
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1585
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:1616
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
Definition: disk.c:1442
@ GNUNET_DISK_PF_NONE
No special options, use non-blocking read/write operations.
@ GNUNET_DISK_PIPE_END_WRITE
The writing-end of a pipe.
@ GNUNET_DISK_PIPE_END_READ
The reading-end of a pipe.
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_NO
Definition: gnunet_common.h:98
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
enum GNUNET_GenericReturnValue GNUNET_OS_check_helper_binary(const char *binary, bool check_suid, const char *params)
Check whether an executable exists and possibly if the suid bit is set on the file.
@ GNUNET_OS_INHERIT_STD_IN
When this flag is set, the child process will inherit stdin of the parent.
Definition: gnunet_os_lib.h:83
@ GNUNET_OS_INHERIT_STD_OUT
When this flag is set, the child process will inherit stdout of the parent.
Definition: gnunet_os_lib.h:89
@ GNUNET_OS_INHERIT_STD_ERR
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:95
@ GNUNET_OS_USE_PIPE_CONTROL
Should a pipe be used to send signals to the child?
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: os_priority.c:37
static void open_dev_null(int target_fd, int flags)
Open '/dev/null' and make the result the given file descriptor.
Definition: os_priority.c:297
Handle used to manage a pipe.
Definition: disk.c:69
pid_t pid
PID of the process.
Definition: os_priority.c:48
struct GNUNET_DISK_FileHandle * control_pipe
Pipe we use to signal the process.
Definition: os_priority.c:54

References GNUNET_OS_Process::control_pipe, filename, GNUNET_array_append, GNUNET_array_grow, GNUNET_assert, GNUNET_break, GNUNET_DISK_file_close(), GNUNET_DISK_internal_file_handle_(), GNUNET_DISK_PF_NONE, GNUNET_DISK_pipe(), GNUNET_DISK_pipe_close(), GNUNET_DISK_pipe_detach_end(), GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_PIPE_END_WRITE, GNUNET_DISK_pipe_handle(), GNUNET_ERROR_TYPE_ERROR, GNUNET_new, GNUNET_NO, GNUNET_OK, GNUNET_OS_check_helper_binary(), GNUNET_OS_CONTROL_PIPE, GNUNET_OS_INHERIT_STD_ERR, GNUNET_OS_INHERIT_STD_IN, GNUNET_OS_INHERIT_STD_OUT, GNUNET_OS_USE_PIPE_CONTROL, GNUNET_snprintf(), GNUNET_SYSERR, LOG_STRERROR, LOG_STRERROR_FILE, ls, open_dev_null(), GNUNET_OS_Process::pid, and ret.

Referenced by GNUNET_OS_start_process_v(), and GNUNET_OS_start_process_vap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_status()

static enum GNUNET_GenericReturnValue process_status ( struct GNUNET_OS_Process proc,
enum GNUNET_OS_ProcessStatusType type,
unsigned long *  code,
int  options 
)
static

Retrieve the status of a process, waiting on it if dead.

Nonblocking version.

Parameters
procprocess ID
typestatus type
codereturn code/signal number
optionsWNOHANG if non-blocking is desired
Returns
GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise

Definition at line 741 of file os_priority.c.

876 {
877  int status;
878  int ret;
879 
880  GNUNET_assert (0 != proc);
881  ret = waitpid (proc->pid,
882  &status,
883  options);
884  if (ret < 0)
885  {
887  "waitpid");
888  return GNUNET_SYSERR;
889  }
890  if (0 == ret)
891  {
893  *code = 0;
894  return GNUNET_NO;
895  }
896  if (proc->pid != ret)
897  {
899  "waitpid");
900  return GNUNET_SYSERR;
901  }
902  if (WIFEXITED (status))
903  {
905  *code = WEXITSTATUS (status);
906  }
907  else if (WIFSIGNALED (status))
908  {
910  *code = WTERMSIG (status);
911  }
912  else if (WIFSTOPPED (status))
913  {
915  *code = WSTOPSIG (status);
916  }
917 #ifdef WIFCONTINUED
918  else if (WIFCONTINUED (status))
919  {
921  *code = 0;
922  }
923 #endif
924  else
925  {
927  *code = 0;
928  }
929 
930  return GNUNET_OK;
931 }
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
uint16_t status
See PRISM_STATUS_*-constants.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_OS_PROCESS_SIGNALED
The process was killed by a signal.
@ GNUNET_OS_PROCESS_EXITED
The process exited with a return code.
@ GNUNET_OS_PROCESS_UNKNOWN
The process is not known to the OS (or at least not one of our children).
@ GNUNET_OS_PROCESS_RUNNING
The process is still running.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

◆ cmd_read()

static void cmd_read ( void *  cls)
static

Read from the process and call the line processor.

Parameters
clsthe struct GNUNET_OS_CommandHandle *

Definition at line 1078 of file os_priority.c.

1079 {
1080  struct GNUNET_OS_CommandHandle *cmd = cls;
1081  const struct GNUNET_SCHEDULER_TaskContext *tc;
1083  char *end;
1084  ssize_t ret;
1085 
1086  cmd->rtask = NULL;
1089  {
1090  /* timeout */
1091  proc = cmd->proc;
1092  cmd->proc = NULL;
1093  proc (cmd->proc_cls, NULL);
1094  return;
1095  }
1096  ret = GNUNET_DISK_file_read (cmd->r,
1097  &cmd->buf[cmd->off],
1098  sizeof(cmd->buf) - cmd->off);
1099  if (ret <= 0)
1100  {
1101  if ((cmd->off > 0) && (cmd->off < sizeof(cmd->buf)))
1102  {
1103  cmd->buf[cmd->off] = '\0';
1104  cmd->proc (cmd->proc_cls, cmd->buf);
1105  }
1106  proc = cmd->proc;
1107  cmd->proc = NULL;
1108  proc (cmd->proc_cls, NULL);
1109  return;
1110  }
1111  end = memchr (&cmd->buf[cmd->off], '\n', ret);
1112  cmd->off += ret;
1113  while (NULL != end)
1114  {
1115  *end = '\0';
1116  cmd->proc (cmd->proc_cls, cmd->buf);
1117  memmove (cmd->buf, end + 1, cmd->off - (end + 1 - cmd->buf));
1118  cmd->off -= (end + 1 - cmd->buf);
1119  end = memchr (cmd->buf, '\n', cmd->off);
1120  }
1121  cmd->rtask =
1123  cmd->timeout),
1124  cmd->r,
1125  &cmd_read,
1126  cmd);
1127 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
@ GNUNET_YES
int GNUNET_NETWORK_fdset_handle_isset(const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
Check if a file handle is part of an fd set.
Definition: network.c:1151
void(* GNUNET_OS_LineProcessor)(void *cls, const char *line)
Type of a function to process a line of output.
const struct GNUNET_SCHEDULER_TaskContext * GNUNET_SCHEDULER_get_task_context(void)
Obtain the reasoning why the current task was started.
Definition: scheduler.c:737
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:404
static void cmd_read(void *cls)
Read from the process and call the line processor.
Definition: os_priority.c:1078
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
Definition: scheduler.c:423
Handle to a command.
Definition: os_priority.c:1001
size_t off
Current read offset in buf.
Definition: os_priority.c:1045
char buf[1024]
Buffer for the output.
Definition: os_priority.c:1030
struct GNUNET_SCHEDULER_Task * rtask
Task reading from pipe.
Definition: os_priority.c:1035
struct GNUNET_TIME_Absolute timeout
When to time out.
Definition: os_priority.c:1040
void * proc_cls
Closure for proc.
Definition: os_priority.c:1025
GNUNET_OS_LineProcessor proc
Function to call on each line of output.
Definition: os_priority.c:1020
const struct GNUNET_DISK_FileHandle * r
Read-end of output pipe.
Definition: os_priority.c:1015
Context information passed to each scheduler task.
const struct GNUNET_NETWORK_FDSet * read_ready
Set of file descriptors ready for reading; note that additional bits may be set that were not in the ...

References GNUNET_OS_CommandHandle::buf, end, GNUNET_DISK_file_read(), GNUNET_NETWORK_fdset_handle_isset(), GNUNET_SCHEDULER_add_read_file(), GNUNET_SCHEDULER_get_task_context(), GNUNET_TIME_absolute_get_remaining(), GNUNET_YES, GNUNET_OS_CommandHandle::off, GNUNET_OS_CommandHandle::proc, GNUNET_OS_CommandHandle::proc_cls, GNUNET_OS_CommandHandle::r, GNUNET_SCHEDULER_TaskContext::read_ready, ret, GNUNET_OS_CommandHandle::rtask, tc, and GNUNET_OS_CommandHandle::timeout.

Referenced by GNUNET_OS_command_run().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ current_process

struct GNUNET_OS_Process current_process
static

Handle for 'this' process.

Definition at line 1 of file os_priority.c.

Referenced by GNUNET_OS_process_current().

◆ pch

struct GNUNET_SCHEDULER_Task* pch
static

◆ spch

struct GNUNET_SCHEDULER_Task* spch
static

Handle for the shutdown_pch() Task.

Definition at line 71 of file os_priority.c.

Referenced by GNUNET_OS_install_parent_control_handler(), and parent_control_handler().