GNUnet  0.10.x
Data Structures | Macros | Typedefs | Enumerations | Functions
OS library

Low level process routines. More...

Data Structures

struct  GNUNET_OS_ProjectData
 Project-specific data used to help the OS subsystem find installation paths. More...
 

Macros

#define GNUNET_OS_get_hostname_max_length()   255
 Get maximum string length returned by gethostname() More...
 

Typedefs

typedef int(* GNUNET_OS_NetworkInterfaceProcessor) (void *cls, const char *name, int isDefault, const struct sockaddr *addr, const struct sockaddr *broadcast_addr, const struct sockaddr *netmask, socklen_t addrlen)
 Callback function invoked for each interface found. More...
 
typedef void(* GNUNET_OS_LineProcessor) (void *cls, const char *line)
 Type of a function to process a line of output. More...
 

Enumerations

enum  GNUNET_OS_InheritStdioFlags {
  GNUNET_OS_INHERIT_STD_NONE = 0, GNUNET_OS_INHERIT_STD_IN = 1, GNUNET_OS_INHERIT_STD_OUT = 2, GNUNET_OS_INHERIT_STD_ERR = 4,
  GNUNET_OS_INHERIT_STD_OUT_AND_ERR = 6, GNUNET_OS_INHERIT_STD_ALL = 7
}
 Flags that determine which of the standard streams should be inherited by the child process. More...
 
enum  GNUNET_OS_InstallationPathKind {
  GNUNET_OS_IPK_PREFIX, GNUNET_OS_IPK_BINDIR, GNUNET_OS_IPK_LIBDIR, GNUNET_OS_IPK_DATADIR,
  GNUNET_OS_IPK_LOCALEDIR, GNUNET_OS_IPK_SELF_PREFIX, GNUNET_OS_IPK_ICONDIR, GNUNET_OS_IPK_DOCDIR,
  GNUNET_OS_IPK_LIBEXECDIR
}
 Possible installation paths to request. More...
 
enum  GNUNET_OS_ProcessStatusType {
  GNUNET_OS_PROCESS_UNKNOWN, GNUNET_OS_PROCESS_RUNNING, GNUNET_OS_PROCESS_STOPPED, GNUNET_OS_PROCESS_EXITED,
  GNUNET_OS_PROCESS_SIGNALED
}
 Process status types. More...
 

Functions

const struct GNUNET_OS_ProjectDataGNUNET_OS_project_data_default (void)
 Return default project data used by 'libgnunetutil' for GNUnet. More...
 
const struct GNUNET_OS_ProjectDataGNUNET_OS_project_data_get (void)
 
void GNUNET_OS_init (const struct GNUNET_OS_ProjectData *pd)
 Setup OS subsystem with project data. More...
 
char * GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind)
 Get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation directory. More...
 
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 libexec/-directory to get the full path. More...
 
char * GNUNET_OS_get_suid_binary_path (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *progname)
 Given the name of a helper, service or daemon binary construct the full path to the binary using the SUID_BINARY_PATH in the PATHS section of the configuration. More...
 
void GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc, void *proc_cls)
 Enumerate all network interfaces. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_process_current (void)
 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...
 
void GNUNET_OS_process_destroy (struct GNUNET_OS_Process *proc)
 Cleans up process structure contents (OS-dependent) and deallocates it. More...
 
pid_t GNUNET_OS_process_get_pid (struct GNUNET_OS_Process *proc)
 Get the pid of the process in question. More...
 
struct GNUNET_OS_ProcessGNUNET_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. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process (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,...)
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_va (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, va_list va)
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_v (int pipe_control, enum GNUNET_OS_InheritStdioFlags std_inheritance, const SOCKTYPE *lsocks, const char *filename, char *const argv[])
 Start a process. More...
 
struct GNUNET_OS_ProcessGNUNET_OS_start_process_s (int pipe_control, unsigned int std_inheritance, const SOCKTYPE *lsocks, const char *filename,...)
 Start a process. More...
 
void GNUNET_OS_command_stop (struct GNUNET_OS_CommandHandle *cmd)
 Stop/kill a command. 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...
 
int GNUNET_OS_process_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...
 
int 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...
 
void GNUNET_OS_install_parent_control_handler (void *cls)
 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...
 
int GNUNET_OS_check_helper_binary (const char *binary, int check_suid, const char *params)
 Check whether an executable exists and possibly if the suid bit is set on the file. More...
 

Detailed Description

Low level process routines.

This code manages child processes. We can communicate with child processes using signals. Because signals are not supported on W32 and Java (at least not nicely), we can alternatively use a pipe to send signals to the child processes (if the child process is a full-blown GNUnet process that supports reading signals from a pipe, of course). Naturally, this also only works for 'normal' termination via signals, and not as a replacement for SIGKILL. Thus using pipes to communicate signals should only be enabled if the child is a Java process OR if we are on Windoze.

Macro Definition Documentation

◆ GNUNET_OS_get_hostname_max_length

#define GNUNET_OS_get_hostname_max_length ( )    255

Get maximum string length returned by gethostname()

Definition at line 377 of file gnunet_os_lib.h.

Referenced by GNUNET_RESOLVER_hostname_resolve(), GNUNET_RESOLVER_local_fqdn_get(), GST_stats_init(), logger_run(), and run().

Typedef Documentation

◆ GNUNET_OS_NetworkInterfaceProcessor

typedef int(* GNUNET_OS_NetworkInterfaceProcessor) (void *cls, const char *name, int isDefault, const struct sockaddr *addr, const struct sockaddr *broadcast_addr, const struct sockaddr *netmask, socklen_t addrlen)

Callback function invoked for each interface found.

Parameters
clsclosure
namename of the interface (can be NULL for unknown)
isDefaultis this presumably the default interface
addraddress of this interface (can be NULL for unknown or unassigned)
broadcast_addrthe broadcast address (can be NULL for unknown or unassigned)
netmaskthe network mask (can be NULL for unknown or unassigned)
addrlenlength of the address
Returns
GNUNET_OK to continue iteration, GNUNET_SYSERR to abort

Definition at line 350 of file gnunet_os_lib.h.

◆ GNUNET_OS_LineProcessor

typedef void(* GNUNET_OS_LineProcessor) (void *cls, const char *line)

Type of a function to process a line of output.

Parameters
clsclosure
lineline of output from a command, NULL for the end

Definition at line 546 of file gnunet_os_lib.h.

Enumeration Type Documentation

◆ GNUNET_OS_InheritStdioFlags

Flags that determine which of the standard streams should be inherited by the child process.

Enumerator
GNUNET_OS_INHERIT_STD_NONE 

No standard streams should be inherited.

GNUNET_OS_INHERIT_STD_IN 

When this flag is set, the child process will inherit stdin of the parent.

GNUNET_OS_INHERIT_STD_OUT 

When this flag is set, the child process will inherit stdout of the parent.

GNUNET_OS_INHERIT_STD_ERR 

When this flag is set, the child process will inherit stderr of the parent.

GNUNET_OS_INHERIT_STD_OUT_AND_ERR 

When these flags are set, the child process will inherit stdout and stderr of the parent.

GNUNET_OS_INHERIT_STD_ALL 

Use this option to have all of the standard streams (stdin, stdout and stderror) be inherited.

Definition at line 68 of file gnunet_os_lib.h.

69 {
70 
75 
81 
87 
93 
99 
105 };
When this flag is set, the child process will inherit stderr of the parent.
Definition: gnunet_os_lib.h:92
When this flag is set, the child process will inherit stdin of the parent.
Definition: gnunet_os_lib.h:80
Use this option to have all of the standard streams (stdin, stdout and stderror) be inherited...
No standard streams should be inherited.
Definition: gnunet_os_lib.h:74
When this flag is set, the child process will inherit stdout of the parent.
Definition: gnunet_os_lib.h:86
When these flags are set, the child process will inherit stdout and stderr of the parent...
Definition: gnunet_os_lib.h:98

◆ GNUNET_OS_InstallationPathKind

Possible installation paths to request.

Enumerator
GNUNET_OS_IPK_PREFIX 

Return the "PREFIX" directory given to configure.

GNUNET_OS_IPK_BINDIR 

Return the directory where the program binaries are installed.

(bin/)

GNUNET_OS_IPK_LIBDIR 

Return the directory where libraries are installed.

(lib/gnunet/)

GNUNET_OS_IPK_DATADIR 

Return the directory where data is installed (share/gnunet/)

GNUNET_OS_IPK_LOCALEDIR 

Return the directory where translations are installed (share/locale/)

GNUNET_OS_IPK_SELF_PREFIX 

Return the installation directory of this application, not the one of the overall GNUnet installation (in case they are different).

GNUNET_OS_IPK_ICONDIR 

Return the prefix of the path with application icons (share/icons/).

GNUNET_OS_IPK_DOCDIR 

Return the prefix of the path with documentation files, including the license (share/doc/gnunet/).

GNUNET_OS_IPK_LIBEXECDIR 

Return the directory where helper binaries are installed (lib/gnunet/libexec/)

Definition at line 117 of file gnunet_os_lib.h.

118 {
123 
128 
133 
138 
143 
150 
155 
161 
166 };
Return the directory where translations are installed (share/locale/)
Return the directory where libraries are installed.
Return the "PREFIX" directory given to configure.
Return the prefix of the path with documentation files, including the license (share/doc/gnunet/).
Return the directory where helper binaries are installed (lib/gnunet/libexec/)
Return the installation directory of this application, not the one of the overall GNUnet installation...
Return the directory where the program binaries are installed.
Return the prefix of the path with application icons (share/icons/).
Return the directory where data is installed (share/gnunet/)

◆ GNUNET_OS_ProcessStatusType

Process status types.

Enumerator
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.

GNUNET_OS_PROCESS_STOPPED 

The process is paused (but could be resumed).

GNUNET_OS_PROCESS_EXITED 

The process exited with a return code.

GNUNET_OS_PROCESS_SIGNALED 

The process was killed by a signal.

Definition at line 172 of file gnunet_os_lib.h.

173 {
179 
184 
189 
194 
199 };
The process exited with a return code.
The process was killed by a signal.
The process is still running.
The process is not known to the OS (or at least not one of our children).
The process is paused (but could be resumed).

Function Documentation

◆ GNUNET_OS_project_data_default()

const struct GNUNET_OS_ProjectData* GNUNET_OS_project_data_default ( void  )

Return default project data used by 'libgnunetutil' for GNUnet.

Definition at line 78 of file os_installation.c.

References default_pd.

79 {
80  return &default_pd;
81 }
static const struct GNUNET_OS_ProjectData default_pd
Default project data used for installation path detection for GNUnet (core).

◆ GNUNET_OS_project_data_get()

const struct GNUNET_OS_ProjectData* GNUNET_OS_project_data_get ( void  )
Returns
current (actual) project data.
current project data.

Definition at line 88 of file os_installation.c.

References current_pd.

Referenced by format_help(), GNUNET_CONFIGURATION_load(), GNUNET_PROGRAM_run2(), GNUNET_SERVICE_run_(), LEGACY_SERVICE_run(), and run().

89 {
90  return current_pd;
91 }
static const struct GNUNET_OS_ProjectData * current_pd
Which project data do we currently use for installation path detection? Never NULL.
Here is the caller graph for this function:

◆ GNUNET_OS_init()

void GNUNET_OS_init ( const struct GNUNET_OS_ProjectData pd)

Setup OS subsystem with project data.

Parameters
pdproject data used to determine paths.
pdproject data used to determine paths

Definition at line 100 of file os_installation.c.

References buf, dir, DllMain(), removetrailingwhitespace::f, FCLOSE, fn, FOPEN, GNUNET_asprintf(), GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_malloc, GNUNET_memcpy, GNUNET_realloc, GNUNET_snprintf(), GNUNET_strdup, len, GNUNET_OS_ProjectData::libname, line, LOG_STRERROR_FILE, p, GNUNET_OS_ProjectData::project_dirname, size, SSCANF, and zero.

101 {
102  GNUNET_assert (NULL != pd);
103  current_pd = pd;
104 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static const struct GNUNET_OS_ProjectData * current_pd
Which project data do we currently use for installation path detection? Never NULL.
Here is the call graph for this function:

◆ GNUNET_OS_installation_get_path()

char* GNUNET_OS_installation_get_path ( enum GNUNET_OS_InstallationPathKind  dirkind)

Get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation directory.

Parameters
dirkindwhat kind of directory is desired?
Returns
a pointer to the dir path (to be freed by the caller)

Get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation directory.

Returns
a pointer to the dir path (to be freed by the caller)

Definition at line 548 of file os_installation.c.

References DIR_SEPARATOR, DIR_SEPARATOR_STR, GNUNET_asprintf(), GNUNET_DISK_directory_test(), GNUNET_free, GNUNET_free_non_null, GNUNET_OS_IPK_BINDIR, GNUNET_OS_IPK_DATADIR, GNUNET_OS_IPK_DOCDIR, GNUNET_OS_IPK_ICONDIR, GNUNET_OS_IPK_LIBDIR, GNUNET_OS_IPK_LIBEXECDIR, GNUNET_OS_IPK_LOCALEDIR, GNUNET_OS_IPK_PREFIX, GNUNET_OS_IPK_SELF_PREFIX, GNUNET_strdup, GNUNET_YES, os_get_exec_path(), os_get_gnunet_path(), and GNUNET_OS_ProjectData::project_dirname.

Referenced by GNUNET_CONFIGURATION_load(), GNUNET_OS_get_libexec_binary_path(), GNUNET_PLUGIN_load_all(), GNUNET_PROGRAM_run2(), hostkeys_load(), load_keys(), plugin_init(), run(), and start_process().

549 {
550  size_t n;
551  char *dirname;
552  char *execpath = NULL;
553  char *tmp;
554  char *multiarch;
555  char *libdir;
556  int isbasedir;
557 
558  /* if wanted, try to get the current app's bin/ */
559  if (dirkind == GNUNET_OS_IPK_SELF_PREFIX)
560  execpath = os_get_exec_path ();
561 
562  /* try to get GNUnet's bin/ or lib/, or if previous was unsuccessful some
563  * guess for the current app */
564  if (NULL == execpath)
565  execpath = os_get_gnunet_path ();
566 
567  if (NULL == execpath)
568  return NULL;
569 
570  n = strlen (execpath);
571  if (0 == n)
572  {
573  /* should never happen, but better safe than sorry */
574  GNUNET_free (execpath);
575  return NULL;
576  }
577  /* remove filename itself */
578  while ((n > 1) && (DIR_SEPARATOR == execpath[n - 1]))
579  execpath[--n] = '\0';
580 
581  isbasedir = 1;
582  if ((n > 6) &&
583  ((0 == strcasecmp (&execpath[n - 6], "/lib32")) ||
584  (0 == strcasecmp (&execpath[n - 6], "/lib64"))))
585  {
586  if ( (GNUNET_OS_IPK_LIBDIR != dirkind) &&
587  (GNUNET_OS_IPK_LIBEXECDIR != dirkind) )
588  {
589  /* strip '/lib32' or '/lib64' */
590  execpath[n - 6] = '\0';
591  n -= 6;
592  }
593  else
594  isbasedir = 0;
595  }
596  else if ((n > 4) &&
597  ((0 == strcasecmp (&execpath[n - 4], "/bin")) ||
598  (0 == strcasecmp (&execpath[n - 4], "/lib"))))
599  {
600  /* strip '/bin' or '/lib' */
601  execpath[n - 4] = '\0';
602  n -= 4;
603  }
604  multiarch = NULL;
605  if (NULL != (libdir = strstr (execpath, "/lib/")))
606  {
607  /* test for multi-arch path of the form "PREFIX/lib/MULTIARCH/";
608  here we need to re-add 'multiarch' to lib and libexec paths later! */
609  multiarch = &libdir[5];
610  if (NULL == strchr (multiarch, '/'))
611  libdir[0] = '\0'; /* Debian multiarch format, cut of from 'execpath' but preserve in multicarch */
612  else
613  multiarch = NULL; /* maybe not, multiarch still has a '/', which is not OK */
614  }
615  /* in case this was a directory named foo-bin, remove "foo-" */
616  while ((n > 1) && (execpath[n - 1] == DIR_SEPARATOR))
617  execpath[--n] = '\0';
618  switch (dirkind)
619  {
622  dirname = GNUNET_strdup (DIR_SEPARATOR_STR);
623  break;
626  break;
628  if (isbasedir)
629  {
630  GNUNET_asprintf (&tmp,
631  "%s%s%s%s%s%s%s",
632  execpath,
633  DIR_SEPARATOR_STR "lib",
634  (NULL != multiarch) ? DIR_SEPARATOR_STR : "",
635  (NULL != multiarch) ? multiarch : "",
636  DIR_SEPARATOR_STR,
638  DIR_SEPARATOR_STR);
639  if (GNUNET_YES ==
641  {
642  GNUNET_free (execpath);
643  return tmp;
644  }
645  GNUNET_free (tmp);
646  tmp = NULL;
647  dirname = NULL;
648  if (4 == sizeof (void *))
649  {
650  GNUNET_asprintf (&dirname,
651  DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR,
653  GNUNET_asprintf (&tmp,
654  "%s%s",
655  execpath,
656  dirname);
657  }
658  if (8 == sizeof (void *))
659  {
660  GNUNET_asprintf (&dirname,
661  DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR,
663  GNUNET_asprintf (&tmp,
664  "%s%s",
665  execpath,
666  dirname);
667  }
668 
669  if ( (NULL != tmp) &&
670  (GNUNET_YES ==
672  {
673  GNUNET_free (execpath);
674  GNUNET_free_non_null (dirname);
675  return tmp;
676  }
677  GNUNET_free (tmp);
678  GNUNET_free_non_null (dirname);
679  }
680  GNUNET_asprintf (&dirname,
681  DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR,
683  break;
685  GNUNET_asprintf (&dirname,
686  DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR,
688  break;
690  dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "locale" DIR_SEPARATOR_STR);
691  break;
693  dirname = GNUNET_strdup (DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "icons" DIR_SEPARATOR_STR);
694  break;
696  GNUNET_asprintf (&dirname,
697  DIR_SEPARATOR_STR "share" DIR_SEPARATOR_STR "doc" DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR,
699  break;
701  if (isbasedir)
702  {
703  GNUNET_asprintf (&dirname,
704  DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR "libexec" DIR_SEPARATOR_STR,
706  GNUNET_asprintf (&tmp,
707  "%s%s%s%s",
708  execpath,
709  DIR_SEPARATOR_STR "lib" DIR_SEPARATOR_STR,
710  (NULL != multiarch) ? multiarch : "",
711  dirname);
712  if (GNUNET_YES ==
714  {
715  GNUNET_free (execpath);
716  GNUNET_free (dirname);
717  return tmp;
718  }
719  GNUNET_free (tmp);
720  tmp = NULL;
721  dirname = NULL;
722  if (4 == sizeof (void *))
723  {
724  GNUNET_asprintf (&dirname,
725  DIR_SEPARATOR_STR "lib32" DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR "libexec" DIR_SEPARATOR_STR,
727  GNUNET_asprintf (&tmp,
728  "%s%s",
729  execpath,
730  dirname);
731  }
732  if (8 == sizeof (void *))
733  {
734  GNUNET_asprintf (&dirname,
735  DIR_SEPARATOR_STR "lib64" DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR "libexec" DIR_SEPARATOR_STR,
737  GNUNET_asprintf (&tmp,
738  "%s%s",
739  execpath,
740  dirname);
741  }
742  if ( (NULL != tmp) &&
743  (GNUNET_YES ==
745  {
746  GNUNET_free (execpath);
747  GNUNET_free_non_null (dirname);
748  return tmp;
749  }
750  GNUNET_free (tmp);
751  GNUNET_free_non_null (dirname);
752  }
753  GNUNET_asprintf (&dirname,
754  DIR_SEPARATOR_STR "%s" DIR_SEPARATOR_STR "libexec" DIR_SEPARATOR_STR,
756  break;
757  default:
758  GNUNET_free (execpath);
759  return NULL;
760  }
761  GNUNET_asprintf (&tmp,
762  "%s%s",
763  execpath,
764  dirname);
765  GNUNET_free (dirname);
766  GNUNET_free (execpath);
767  return tmp;
768 }
Return the directory where translations are installed (share/locale/)
Return the directory where libraries are installed.
const char * project_dirname
Name of the project that is used in the "libexec" prefix, For example, "gnunet".
static char * os_get_gnunet_path()
get the path to GNUnet bin/ or lib/, prefering the lib/ path
int GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:628
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
Return the "PREFIX" directory given to configure.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
Return the prefix of the path with documentation files, including the license (share/doc/gnunet/).
Return the directory where helper binaries are installed (lib/gnunet/libexec/)
#define DIR_SEPARATOR
Definition: plibc.h:631
static char * os_get_exec_path()
get the path to current app's bin/
static const struct GNUNET_OS_ProjectData * current_pd
Which project data do we currently use for installation path detection? Never NULL.
Return the installation directory of this application, not the one of the overall GNUnet installation...
Return the directory where the program binaries are installed.
#define GNUNET_YES
Definition: gnunet_common.h:80
Return the prefix of the path with application icons (share/icons/).
#define DIR_SEPARATOR_STR
Definition: plibc.h:632
Return the directory where data is installed (share/gnunet/)
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_get_libexec_binary_path()

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 libexec/-directory to get the full path.

Parameters
prognamename of the binary
Returns
full path to the binary, if possible, otherwise copy of 'progname'

Definition at line 780 of file os_installation.c.

References cache, DIR_SEPARATOR, GNUNET_asprintf(), GNUNET_NO, GNUNET_OS_installation_get_path(), GNUNET_OS_IPK_LIBEXECDIR, GNUNET_strdup, GNUNET_STRINGS_path_is_absolute(), and GNUNET_YES.

Referenced by communicator_start(), GNUNET_HELPER_start(), GNUNET_OS_get_suid_binary_path(), GNUNET_TESTBED_controller_start(), GNUNET_TESTBED_is_host_habitable(), GNUNET_TESTING_peer_configure(), GNUNET_TESTING_service_run(), LIBGNUNET_PLUGIN_TRANSPORT_INIT(), start_arm_service(), start_process(), start_shared_service_instance(), test_icmp_client(), test_icmp_server(), and tokenizer_cb().

781 {
782  static char *cache;
783  char *libexecdir;
784  char *binary;
785 
786  if ( (DIR_SEPARATOR == progname[0]) ||
787  (GNUNET_YES ==
789  GNUNET_NO,
790  NULL, NULL)) )
791  return GNUNET_strdup (progname);
792  if (NULL != cache)
793  libexecdir = cache;
794  else
796  if (NULL == libexecdir)
797  return GNUNET_strdup (progname);
798  GNUNET_asprintf (&binary,
799  "%s%s",
800  libexecdir,
801  progname);
802  cache = libexecdir;
803  return binary;
804 }
int GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it's an URI.
Definition: strings.c:1171
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
char * GNUNET_OS_installation_get_path(enum GNUNET_OS_InstallationPathKind dirkind)
get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX, the current running apps installation directory
Return the directory where helper binaries are installed (lib/gnunet/libexec/)
#define DIR_SEPARATOR
Definition: plibc.h:631
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
#define GNUNET_YES
Definition: gnunet_common.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_get_suid_binary_path()

char* GNUNET_OS_get_suid_binary_path ( const struct GNUNET_CONFIGURATION_Handle cfg,
const char *  progname 
)

Given the name of a helper, service or daemon binary construct the full path to the binary using the SUID_BINARY_PATH in the PATHS section of the configuration.

If that option is not present, fall back to GNUNET_OS_get_libexec_binary_path. If progname is an absolute path, a copy of this path is returned.

Parameters
cfgconfiguration to inspect
prognamename of the binary
Returns
full path to the binary, if possible, a copy of progname otherwise

Definition at line 820 of file os_installation.c.

References cache, DIR_SEPARATOR, DIR_SEPARATOR_STR, GNUNET_asprintf(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_NO, GNUNET_OS_get_libexec_binary_path(), GNUNET_strdup, GNUNET_STRINGS_path_is_absolute(), and GNUNET_YES.

Referenced by GN_request_connection_reversal(), restart_nat_server(), and run().

822 {
823  static char *cache;
824  char *binary = NULL;
825  char *path = NULL;
826  size_t path_len;
827 
828  if (GNUNET_YES ==
830  GNUNET_NO,
831  NULL, NULL))
832  {
833  return GNUNET_strdup (progname);
834  }
835  if (NULL != cache)
836  path = cache;
837  else
839  "PATHS",
840  "SUID_BINARY_PATH",
841  &path);
842  if (NULL == path || 0 == strlen (path))
843  return GNUNET_OS_get_libexec_binary_path (progname);
844  path_len = strlen (path);
845  GNUNET_asprintf (&binary,
846  "%s%s%s",
847  path,
848  (path[path_len - 1] == DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR,
849  progname);
850  cache = path;
851  return binary;
852 }
int GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it's an URI.
Definition: strings.c:1171
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define DIR_SEPARATOR
Definition: plibc.h:631
int GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
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 ...
#define GNUNET_YES
Definition: gnunet_common.h:80
#define DIR_SEPARATOR_STR
Definition: plibc.h:632
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_network_interfaces_list()

void GNUNET_OS_network_interfaces_list ( GNUNET_OS_NetworkInterfaceProcessor  proc,
void *  proc_cls 
)

Enumerate all network interfaces.

Parameters
procthe callback function
proc_clsclosure for proc

Definition at line 388 of file os_network.c.

References EnumNICs3_results::addr_size, address, EnumNICs3_results::broadcast, EnumNICs3(), ENUMNICS3_BCAST_OK, EnumNICs3_free(), ENUMNICS3_MASK_OK, EnumNICs3_results::flags, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_WARNING, GNUNET_OK, EnumNICs3_results::is_default, LOG, EnumNICs3_results::mask, EnumNICs3_results::pretty_name, results, try_ifconfig(), and try_ip().

Referenced by do_broadcast(), get_addresses(), GNUNET_NT_scanner_init(), host_habitable_cb(), run_scan(), setup_broadcast(), and test_local_ip().

390 {
391 #ifdef MINGW
392  int r;
393  int i;
394  struct EnumNICs3_results *results = NULL;
395  int results_count;
396 
397  r = EnumNICs3 (&results, &results_count);
398  if (r != GNUNET_OK)
399  return;
400 
401  for (i = 0; i < results_count; i++)
402  {
403  if (GNUNET_OK !=
404  proc (proc_cls, results[i].pretty_name, results[i].is_default,
405  (const struct sockaddr *) &results[i].address,
406  results[i].
408  (const struct sockaddr *) &results[i].broadcast : NULL,
409  results[i].flags & ENUMNICS3_MASK_OK ?
410  (const struct sockaddr *) &results[i].mask : NULL,
411  results[i].addr_size))
412  break;
413  }
414  EnumNICs3_free (results);
415  return;
416 
417 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
418 
419  struct ifaddrs *ifa_first;
420  struct ifaddrs *ifa_ptr;
421  socklen_t alen;
422 
423  if (getifaddrs (&ifa_first) == 0)
424  {
425  for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
426  {
427  if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
428  (ifa_ptr->ifa_flags & IFF_UP) != 0)
429  {
430  if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
431  (ifa_ptr->ifa_addr->sa_family != AF_INET6))
432  continue;
433  if (ifa_ptr->ifa_addr->sa_family == AF_INET)
434  alen = sizeof (struct sockaddr_in);
435  else
436  alen = sizeof (struct sockaddr_in6);
437  if (GNUNET_OK !=
438  proc (proc_cls, ifa_ptr->ifa_name,
439  0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE),
440  ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr,
441  ifa_ptr->ifa_netmask, alen))
442  break;
443  }
444  }
445  freeifaddrs (ifa_first);
446  }
447 #else
448  if (GNUNET_OK ==
449  try_ip (proc,
450  proc_cls))
451  return;
452  if (GNUNET_OK ==
453  try_ifconfig (proc,
454  proc_cls))
455  return;
457  "Failed to enumerate network interfaces\n");
458 #endif
459 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define LOG(kind,...)
Definition: os_network.c:34
char pretty_name[1001]
Definition: winproc.h:219
static int try_ip(GNUNET_OS_NetworkInterfaceProcessor proc, void *proc_cls)
Try to enumerate all network interfaces using &#39;ip&#39;.
Definition: os_network.c:238
int EnumNICs3(struct EnumNICs3_results **, int *EnumNICs3_results_count)
Definition: win.c:361
size_t addr_size
Definition: winproc.h:220
SOCKADDR_STORAGE broadcast
Definition: winproc.h:223
unsigned char flags
Definition: winproc.h:217
void EnumNICs3_free(struct EnumNICs3_results *)
Definition: win.c:623
#define ENUMNICS3_BCAST_OK
Definition: winproc.h:213
static int try_ifconfig(GNUNET_OS_NetworkInterfaceProcessor proc, void *proc_cls)
Try to enumerate all network interfaces using &#39;ifconfig&#39;.
Definition: os_network.c:47
static unsigned int results
Definition: gnunet-search.c:52
#define ENUMNICS3_MASK_OK
Definition: winproc.h:212
static char * address
GNS address for this phone.
SOCKADDR_STORAGE mask
Definition: winproc.h:222
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_process_current()

struct GNUNET_OS_Process* GNUNET_OS_process_current ( void  )

Get process structure for current process.

The pointer it returns points to static memory location and must not be deallocated/closed

Returns
pointer to the process sturcutre for this process

The pointer it returns points to static memory location and must not be deallocated/closed.

Returns
pointer to the process sturcutre for this process

Definition at line 231 of file os_priority.c.

References current_process, and GNUNET_OS_Process::pid.

232 {
233 #if WINDOWS
234  current_process.pid = GetCurrentProcessId ();
235  current_process.handle = GetCurrentProcess ();
236 #else
237  current_process.pid = 0;
238 #endif
239  return &current_process;
240 }
pid_t pid
PID of the process.
Definition: os_priority.c:46
static struct GNUNET_OS_Process current_process
Handle for &#39;this&#39; process.
Definition: os_priority.c:66

◆ GNUNET_OS_process_kill()

int GNUNET_OS_process_kill ( struct GNUNET_OS_Process proc,
int  sig 
)

Sends a signal to the process.

Parameters
procpointer to process structure
sigsignal
Returns
0 on success, -1 on error

Definition at line 251 of file os_priority.c.

References GNUNET_OS_Process::control_pipe, GNUNET_DISK_file_write(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_NO, GNUNET_TERM_SIG, GNUNET_YES, LOG, GNUNET_OS_Process::pid, PLIBC_KILL, ret, and SafeTerminateProcess().

Referenced by do_stop_task(), GN_stop_gnunet_nat_server_(), GNUNET_HELPER_kill(), GNUNET_NAT_mini_get_external_ipv4_cancel_(), GNUNET_OS_command_stop(), GNUNET_TESTBED_is_host_habitable_cancel(), GNUNET_TESTING_peer_kill(), handle_stop(), maint_child_death(), nat_server_read(), shutdown_communicator(), shutdown_task(), stop_shared_service_instance(), and terminate_task().

253 {
254  int ret;
255  char csig;
256 
257  csig = (char) sig;
258  if (NULL != proc->control_pipe)
259  {
261  "Sending signal %d to pid: %u via pipe\n",
262  sig,
263  proc->pid);
265  &csig,
266  sizeof (csig));
267  if (sizeof (csig) == ret)
268  return 0;
269  }
270  /* pipe failed or non-existent, try other methods */
271  switch (sig)
272  {
273 #if !defined (WINDOWS)
274  case SIGHUP:
275 #endif
276  case SIGINT:
277  case SIGKILL:
278  case SIGTERM:
279 #if (SIGTERM != GNUNET_TERM_SIG)
280  case GNUNET_TERM_SIG:
281 #endif
282 #if defined(WINDOWS) && !defined(__CYGWIN__)
283  {
284  DWORD exitcode;
285  int must_kill = GNUNET_YES;
286  if (0 != GetExitCodeProcess (proc->handle, &exitcode))
287  must_kill = (exitcode == STILL_ACTIVE) ? GNUNET_YES : GNUNET_NO;
288  if (GNUNET_YES == must_kill)
289  {
290  if (0 == SafeTerminateProcess (proc->handle, 0, 0))
291  {
292  DWORD error_code = GetLastError ();
293  if ( (error_code != WAIT_TIMEOUT) &&
294  (error_code != ERROR_PROCESS_ABORTED) )
295  {
296  LOG ((error_code == ERROR_ACCESS_DENIED) ?
298  "SafeTermiateProcess failed with code %lu\n",
299  error_code);
300  /* The problem here is that a process that is already dying
301  * might cause SafeTerminateProcess to fail with
302  * ERROR_ACCESS_DENIED, but the process WILL die eventually.
303  * If we really had a permissions problem, hanging up (which
304  * is what will happen in process_wait() in that case) is
305  * a valid option.
306  */
307  if (ERROR_ACCESS_DENIED == error_code)
308  {
309  errno = 0;
310  }
311  else
312  {
313  SetErrnoFromWinError (error_code);
314  return -1;
315  }
316  }
317  }
318  }
319  }
320  return 0;
321 #else
323  "Sending signal %d to pid: %u via system call\n",
324  sig,
325  proc->pid);
326  return PLIBC_KILL (proc->pid, sig);
327 #endif
328  default:
329 #if defined (WINDOWS)
330  errno = EINVAL;
331  return -1;
332 #else
334  "Sending signal %d to pid: %u via system call\n",
335  sig,
336  proc->pid);
337  return PLIBC_KILL (proc->pid, sig);
338 #endif
339  }
340 }
BOOL SafeTerminateProcess(HANDLE hProcess, UINT uExitCode, DWORD dwTimeout)
Terminate a process by creating a remote thread within it, which proceeds to call ExitProcess() insid...
Definition: win.c:1288
pid_t pid
PID of the process.
Definition: os_priority.c:46
#define GNUNET_NO
Definition: gnunet_common.h:81
static int ret
Final status code.
Definition: gnunet-arm.c:89
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
#define PLIBC_KILL(p, s)
Definition: plibc.h:659
struct GNUNET_DISK_FileHandle * control_pipe
Pipe we use to signal the process.
Definition: os_priority.c:59
#define GNUNET_YES
Definition: gnunet_common.h:80
#define GNUNET_TERM_SIG
The termination signal.
Definition: platform.h:282
#define LOG(kind,...)
Definition: os_priority.c:32
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_process_destroy()

void GNUNET_OS_process_destroy ( struct GNUNET_OS_Process proc)

Cleans up process structure contents (OS-dependent) and deallocates it.

Parameters
procpointer to process structure

Definition at line 364 of file os_priority.c.

References find_typedefs::arg, GNUNET_OS_Process::control_pipe, GNUNET_DISK_file_close(), GNUNET_free, GNUNET_malloc, len, and result.

Referenced by child_death_task(), GN_request_connection_reversal(), GN_stop_gnunet_nat_server_(), GNUNET_HELPER_wait(), GNUNET_NAT_mini_get_external_ipv4_cancel_(), GNUNET_OS_command_stop(), GNUNET_TESTBED_is_host_habitable_cancel(), GNUNET_TESTING_peer_wait(), habitability_check(), main(), maint_child_death(), nat_server_read(), server_log(), shutdown_communicator(), shutdown_task(), start_arm_service(), and stop_shared_service_instance().

365 {
366  if (NULL != proc->control_pipe)
368 #if defined (WINDOWS)
369  if (NULL != proc->handle)
370  CloseHandle (proc->handle);
371 #endif
372  GNUNET_free (proc);
373 }
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1817
struct GNUNET_DISK_FileHandle * control_pipe
Pipe we use to signal the process.
Definition: os_priority.c:59
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_process_get_pid()

pid_t GNUNET_OS_process_get_pid ( struct GNUNET_OS_Process proc)

Get the pid of the process in question.

Parameters
procthe process to get the pid of
Returns
the current process id

Definition at line 351 of file os_priority.c.

References GNUNET_OS_Process::pid.

Referenced by maint_child_death().

352 {
353  return proc->pid;
354 }
pid_t pid
PID of the process.
Definition: os_priority.c:46
Here is the caller graph for this function:

◆ GNUNET_OS_start_process_vap()

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.

Parameters
pipe_controlshould a pipe be used to send signals to the child?
std_inheritancea set of GNUNET_OS_INHERIT_STD_* flags
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 to get error output from child process (or NULL)
filenamename of the binary
argvNULL-terminated array of arguments to the process
Returns
pointer to process structure of the new process, NULL on error
Parameters
pipe_controlshould a pipe be used to send signals to the child?
std_inheritancea set of GNUNET_OS_INHERIT_STD_* flags
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 to get output from child process (or NULL)
filenamename of the binary
argvNULL-terminated array of arguments to the process
Returns
pointer to process structure of the new process, NULL on error

Definition at line 1321 of file os_priority.c.

References start_process().

Referenced by GNUNET_OS_start_process_va(), GNUNET_TESTBED_is_host_habitable(), main(), run(), start_helper(), and work().

1328 {
1329  return start_process (pipe_control,
1330  std_inheritance,
1331  pipe_stdin,
1332  pipe_stdout,
1333  pipe_stderr,
1334  NULL,
1335  filename,
1336  argv);
1337 }
static char * filename
static struct GNUNET_OS_Process * start_process(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 SOCKTYPE *lsocks, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:566
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_start_process()

struct GNUNET_OS_Process* GNUNET_OS_start_process ( 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,
  ... 
)

Start a process.

Parameters
pipe_controlshould a pipe be used to send signals to the child?
std_inheritancea set of GNUNET_OS_INHERIT_STD_* flags
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 to get error output from child process (or NULL)
filenamename of the binary
...NULL-terminated list of arguments to the process
Returns
pointer to process structure of the new process, NULL on error
Parameters
pipe_controlshould a pipe be used to send signals to the child?
std_inheritancea set of GNUNET_OS_INHERIT_STD_* flags
pipe_stdinpipe to use to send input to child process (or NULL)
pipe_stdoutpipe to use to get output from child process (or NULL)
filenamename of the binary
...NULL-terminated list of arguments to the process
Returns
pointer to process structure of the new process, NULL on error

Definition at line 1400 of file os_priority.c.

References GNUNET_OS_start_process_va(), and ret.

Referenced by communicator_start(), GN_request_connection_reversal(), GNUNET_NAT_mini_get_external_ipv4_(), gnunet_uri(), main(), restart_nat_server(), run(), server_log(), start_shared_service_instance(), and tokenizer_cb().

1406 {
1407  struct GNUNET_OS_Process *ret;
1408  va_list ap;
1409 
1410  va_start (ap, filename);
1411  ret = GNUNET_OS_start_process_va (pipe_control,
1412  std_inheritance,
1413  pipe_stdin,
1414  pipe_stdout,
1415  pipe_stderr,
1416  filename,
1417  ap);
1418  va_end (ap);
1419  return ret;
1420 }
struct GNUNET_OS_Process * GNUNET_OS_start_process_va(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, va_list va)
Start a process.
Definition: os_priority.c:1353
static int ret
Final status code.
Definition: gnunet-arm.c:89
static char * filename
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_start_process_va()

struct GNUNET_OS_Process* GNUNET_OS_start_process_va ( 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,
va_list  va 
)

Start a process.

Parameters
pipe_controlshould a pipe be used to send signals to the child?
std_inheritancea set of GNUNET_OS_INHERIT_STD_* flags
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 to get error output from child process (or NULL)
filenamename of the binary
vaNULL-terminated list of arguments to the process
Returns
pointer to process structure of the new process, NULL on error
Parameters
pipe_controlshould a pipe be used to send signals to the child?
std_inheritancea set of GNUNET_OS_INHERIT_STD_* flags
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 to get output from child process (or NULL)
filenamename of the binary
vaNULL-terminated list of arguments to the process
Returns
pointer to process structure of the new process, NULL on error

Definition at line 1353 of file os_priority.c.

References GNUNET_free, GNUNET_malloc, GNUNET_OS_start_process_vap(), and ret.

Referenced by GNUNET_OS_command_run(), GNUNET_OS_start_process(), and run_process_and_wait().

1359 {
1360  struct GNUNET_OS_Process *ret;
1361  va_list ap;
1362  char **argv;
1363  int argc;
1364 
1365  argc = 0;
1366  va_copy (ap, va);
1367  while (NULL != va_arg (ap, char *))
1368  argc++;
1369  va_end (ap);
1370  argv = GNUNET_malloc (sizeof (char *) * (argc + 1));
1371  argc = 0;
1372  va_copy (ap, va);
1373  while (NULL != (argv[argc] = va_arg (ap, char *)))
1374  argc++;
1375  va_end (ap);
1376  ret = GNUNET_OS_start_process_vap (pipe_control,
1377  std_inheritance,
1378  pipe_stdin,
1379  pipe_stdout,
1380  pipe_stderr,
1381  filename,
1382  argv);
1383  GNUNET_free (argv);
1384  return ret;
1385 }
static int ret
Final status code.
Definition: gnunet-arm.c:89
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 * filename
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_start_process_v()

struct GNUNET_OS_Process* GNUNET_OS_start_process_v ( int  pipe_control,
enum GNUNET_OS_InheritStdioFlags  std_inheritance,
const SOCKTYPE lsocks,
const char *  filename,
char *const  argv[] 
)

Start a process.

Parameters
pipe_controlshould a pipe be used to send signals to the child?
std_inheritancea set of GNUNET_OS_INHERIT_STD_* flags
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, including the process name as the first argument
Returns
pointer to process structure of the new process, NULL on error
Parameters
pipe_controlshould a pipe be used to send signals to the child?
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).
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 1438 of file os_priority.c.

References start_process().

Referenced by GNUNET_OS_start_process_s().

1443 {
1444  return start_process (pipe_control,
1445  std_inheritance,
1446  NULL,
1447  NULL,
1448  NULL,
1449  lsocks,
1450  filename,
1451  argv);
1452 }
static char * filename
static struct GNUNET_OS_Process * start_process(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 SOCKTYPE *lsocks, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:566
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_start_process_s()

struct GNUNET_OS_Process* GNUNET_OS_start_process_s ( int  pipe_control,
unsigned int  std_inheritance,
const SOCKTYPE lsocks,
const char *  filename,
  ... 
)

Start a process.

This function is similar to the GNUNET_OS_start_process_* except that the filename and arguments can have whole strings which contain the arguments. These arguments are to be separated by spaces and are parsed in the order they appear. Arguments containing spaces can be used by quoting them with ".

Parameters
pipe_controlshould a pipe be used to send signals to the child?
std_inheritancea set of GNUNET_OS_INHERIT_STD_* flags
lsocksarray of listen sockets to dup systemd-style (or NULL); must be NULL on platforms where dup is not supported
filenamename of the binary. It is valid to have the arguments in this string when they are separated by spaces.
...more arguments. Should be of type char *. It is valid to have the arguments in these strings when they are separated by spaces. The last argument MUST be NULL.
Returns
pointer to process structure of the new process, NULL on error

Definition at line 1474 of file os_priority.c.

References find_typedefs::arg, filename, GNUNET_free, GNUNET_malloc, GNUNET_OS_start_process_v(), GNUNET_strdup, and len.

Referenced by GNUNET_TESTING_peer_start(), start_arm_service(), and start_process().

1478 {
1479  va_list ap;
1480  char **argv;
1481  unsigned int argv_size;
1482  const char *arg;
1483  const char *rpos;
1484  char *pos;
1485  char *cp;
1486  const char *last;
1487  struct GNUNET_OS_Process *proc;
1488  char *binary_path;
1489  int quote_on;
1490  unsigned int i;
1491  size_t len;
1492 
1493  argv_size = 1;
1494  va_start (ap, filename);
1495  arg = filename;
1496  last = NULL;
1497  do
1498  {
1499  rpos = arg;
1500  quote_on = 0;
1501  while ('\0' != *rpos)
1502  {
1503  if ('"' == *rpos)
1504  {
1505  if (1 == quote_on)
1506  quote_on = 0;
1507  else
1508  quote_on = 1;
1509  }
1510  if ( (' ' == *rpos) && (0 == quote_on) )
1511  {
1512  if (NULL != last)
1513  argv_size++;
1514  last = NULL;
1515  rpos++;
1516  while (' ' == *rpos)
1517  rpos++;
1518  }
1519  if ( (NULL == last) && ('\0' != *rpos) ) // FIXME: == or !=?
1520  last = rpos;
1521  if ('\0' != *rpos)
1522  rpos++;
1523  }
1524  if (NULL != last)
1525  argv_size++;
1526  }
1527  while (NULL != (arg = (va_arg (ap, const char*))));
1528  va_end (ap);
1529 
1530  argv = GNUNET_malloc (argv_size * sizeof (char *));
1531  argv_size = 0;
1532  va_start (ap, filename);
1533  arg = filename;
1534  last = NULL;
1535  do
1536  {
1537  cp = GNUNET_strdup (arg);
1538  quote_on = 0;
1539  pos = cp;
1540  while ('\0' != *pos)
1541  {
1542  if ('"' == *pos)
1543  {
1544  if (1 == quote_on)
1545  quote_on = 0;
1546  else
1547  quote_on = 1;
1548  }
1549  if ( (' ' == *pos) && (0 == quote_on) )
1550  {
1551  *pos = '\0';
1552  if (NULL != last)
1553  argv[argv_size++] = GNUNET_strdup (last);
1554  last = NULL;
1555  pos++;
1556  while (' ' == *pos)
1557  pos++;
1558  }
1559  if ( (NULL == last) && ('\0' != *pos)) // FIXME: == or !=?
1560  last = pos;
1561  if ('\0' != *pos)
1562  pos++;
1563  }
1564  if (NULL != last)
1565  argv[argv_size++] = GNUNET_strdup (last);
1566  last = NULL;
1567  GNUNET_free (cp);
1568  }
1569  while (NULL != (arg = (va_arg (ap, const char*))));
1570  va_end (ap);
1571  argv[argv_size] = NULL;
1572 
1573  for(i = 0; i < argv_size; i++)
1574  {
1575  len = strlen (argv[i]);
1576  if ( (argv[i][0] == '"') && (argv[i][len-1] == '"'))
1577  {
1578  memmove (&argv[i][0], &argv[i][1], len - 2);
1579  argv[i][len-2] = '\0';
1580  }
1581  }
1582  binary_path = argv[0];
1583  proc = GNUNET_OS_start_process_v (pipe_control, std_inheritance, lsocks,
1584  binary_path, argv);
1585  while (argv_size > 0)
1586  GNUNET_free (argv[--argv_size]);
1587  GNUNET_free (argv);
1588  return proc;
1589 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static char * filename
struct GNUNET_OS_Process * GNUNET_OS_start_process_v(int pipe_control, enum GNUNET_OS_InheritStdioFlags std_inheritance, const SOCKTYPE *lsocks, const char *filename, char *const argv[])
Start a process.
Definition: os_priority.c:1438
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_command_stop()

void GNUNET_OS_command_stop ( struct GNUNET_OS_CommandHandle cmd)

Stop/kill a command.

Parameters
cmdhandle to the process

Must ONLY be called either from the callback after 'NULL' was passed for 'line' OR from an independent task (not within the line processor).

Parameters
cmdhandle to the process

Definition at line 1862 of file os_priority.c.

References GNUNET_OS_CommandHandle::eip, GNUNET_assert, GNUNET_break, GNUNET_DISK_pipe_close(), GNUNET_free, GNUNET_OK, GNUNET_OS_process_destroy(), GNUNET_OS_process_kill(), GNUNET_OS_process_wait(), GNUNET_SCHEDULER_cancel(), GNUNET_OS_CommandHandle::opipe, GNUNET_OS_CommandHandle::proc, and GNUNET_OS_CommandHandle::rtask.

Referenced by do_refresh(), GNUNET_NAT_mini_map_stop(), process_map_output(), process_refresh_output(), and process_unmap_output().

1863 {
1864  if (NULL != cmd->proc)
1865  {
1866  GNUNET_assert (NULL != cmd->rtask);
1868  }
1869  (void) GNUNET_OS_process_kill (cmd->eip, SIGKILL);
1873  GNUNET_free (cmd);
1874 }
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_DISK_PipeHandle * opipe
Handle to the output pipe.
Definition: os_priority.c:1815
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_OS_Process * eip
Process handle.
Definition: os_priority.c:1810
struct GNUNET_SCHEDULER_Task * rtask
Task reading from pipe.
Definition: os_priority.c:1840
int GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:2603
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:1762
GNUNET_OS_LineProcessor proc
Function to call on each line of output.
Definition: os_priority.c:1825
#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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_command_run()

struct GNUNET_OS_CommandHandle* GNUNET_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.

Parameters
procfunction to call for each line of the output
proc_clsclosure for proc
timeoutwhen to time out
binarycommand to run
...arguments to command
Returns
NULL on error
Parameters
procfunction to call for each line of the output
proc_clsclosure for proc
timeoutwhen to time out
binarycommand to run
...arguments to command
Returns
NULL on error

Definition at line 1948 of file os_priority.c.

References cmd_read(), GNUNET_OS_CommandHandle::eip, GNUNET_DISK_pipe(), GNUNET_DISK_pipe_close(), GNUNET_DISK_pipe_close_end(), GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_PIPE_END_WRITE, GNUNET_DISK_pipe_handle(), GNUNET_new, GNUNET_NO, GNUNET_OS_start_process_va(), GNUNET_SCHEDULER_add_read_file(), GNUNET_TIME_relative_to_absolute(), GNUNET_YES, GNUNET_OS_CommandHandle::opipe, GNUNET_OS_CommandHandle::proc, GNUNET_OS_CommandHandle::proc_cls, GNUNET_OS_CommandHandle::r, GNUNET_OS_CommandHandle::rtask, and GNUNET_OS_CommandHandle::timeout.

Referenced by do_refresh(), GNUNET_NAT_mini_map_stop(), and run_upnpc_r().

1953 {
1954  struct GNUNET_OS_CommandHandle *cmd;
1955  struct GNUNET_OS_Process *eip;
1956  struct GNUNET_DISK_PipeHandle *opipe;
1957  va_list ap;
1958 
1961  if (NULL == opipe)
1962  return NULL;
1963  va_start (ap, binary);
1964  /* redirect stdout, don't inherit stderr/stdin */
1965  eip = GNUNET_OS_start_process_va (GNUNET_NO, 0, NULL,
1966  opipe, NULL, binary,
1967  ap);
1968  va_end (ap);
1969  if (NULL == eip)
1970  {
1971  GNUNET_DISK_pipe_close (opipe);
1972  return NULL;
1973  }
1975  cmd = GNUNET_new (struct GNUNET_OS_CommandHandle);
1976  cmd->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1977  cmd->eip = eip;
1978  cmd->opipe = opipe;
1979  cmd->proc = proc;
1980  cmd->proc_cls = proc_cls;
1981  cmd->r = GNUNET_DISK_pipe_handle (opipe,
1983  cmd->rtask = GNUNET_SCHEDULER_add_read_file (timeout,
1984  cmd->r,
1985  &cmd_read,
1986  cmd);
1987  return cmd;
1988 }
const struct GNUNET_DISK_FileHandle * r
Read-end of output pipe.
Definition: os_priority.c:1820
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
Definition: time.c:245
struct GNUNET_DISK_PipeHandle * opipe
Handle to the output pipe.
Definition: os_priority.c:1815
#define GNUNET_NO
Definition: gnunet_common.h:81
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_new(type)
Allocate a struct or union of the given type.
struct GNUNET_OS_Process * GNUNET_OS_start_process_va(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, va_list va)
Start a process.
Definition: os_priority.c:1353
struct GNUNET_TIME_Absolute timeout
When to time out.
Definition: os_priority.c:1845
void * proc_cls
Closure for proc.
Definition: os_priority.c:1830
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
struct GNUNET_OS_Process * eip
Process handle.
Definition: os_priority.c:1810
static void cmd_read(void *cls)
Read from the process and call the line processor.
Definition: os_priority.c:1883
The writing-end of a pipe.
The reading-end of a pipe.
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
struct GNUNET_SCHEDULER_Task * rtask
Task reading from pipe.
Definition: os_priority.c:1840
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
Handle to a command.
Definition: os_priority.c:1804
GNUNET_OS_LineProcessor proc
Function to call on each line of output.
Definition: os_priority.c:1825
#define GNUNET_YES
Definition: gnunet_common.h:80
Handle used to manage a pipe.
Definition: disk.c:66
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_process_status()

int GNUNET_OS_process_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.

Nonblocking version.

Parameters
procpointer to process structure
typestatus type
codereturn code/signal number
Returns
GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise

Nonblocking version.

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

Definition at line 1719 of file os_priority.c.

References process_status().

Referenced by child_death_task(), habitability_check(), main(), maint_child_death(), and run_process_and_wait().

1722 {
1723  return process_status (proc,
1724  type,
1725  code,
1726  WNOHANG);
1727 }
static int 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.
Definition: os_priority.c:1603
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_process_wait()

int GNUNET_OS_process_wait ( struct GNUNET_OS_Process proc)

Wait for a process to terminate.

The return code is discarded. You must not use GNUNET_OS_process_status() on the same process after calling this function! This function is blocking and should thus only be used if the child process is known to have terminated or to terminate very soon.

Parameters
procpointer to process structure of the process to wait for
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

The return code is discarded. You must not use GNUNET_OS_process_status() on the same process after calling this function! This function is blocking and should thus only be used if the child process is known to have terminated or to terminate very soon.

Parameters
procpointer to process structure
Returns
GNUNET_OK on success, GNUNET_SYSERR otherwise

Definition at line 1762 of file os_priority.c.

References GNUNET_ERROR_TYPE_WARNING, GNUNET_OK, GNUNET_SYSERR, h, LOG, LOG_STRERROR, GNUNET_OS_Process::pid, and ret.

Referenced by GN_request_connection_reversal(), GN_stop_gnunet_nat_server_(), GNUNET_HELPER_wait(), GNUNET_NAT_mini_get_external_ipv4_cancel_(), GNUNET_OS_command_stop(), GNUNET_TESTBED_is_host_habitable_cancel(), GNUNET_TESTING_peer_wait(), main(), nat_server_read(), run_process_and_wait(), server_log(), shutdown_task(), and stop_shared_service_instance().

1763 {
1764 #ifndef MINGW
1765  pid_t pid = proc->pid;
1766  pid_t ret;
1767 
1768  while ( (pid != (ret = waitpid (pid, NULL, 0))) &&
1769  (EINTR == errno) ) ;
1770  if (pid != ret)
1771  {
1773  "waitpid");
1774  return GNUNET_SYSERR;
1775  }
1776  return GNUNET_OK;
1777 #else
1778  HANDLE h;
1779 
1780  h = proc->handle;
1781  if (NULL == h)
1782  {
1784  "Invalid process information {%d, %08X}\n",
1785  proc->pid, h);
1786  return GNUNET_SYSERR;
1787  }
1788  if (NULL == h)
1789  h = GetCurrentProcess ();
1790 
1791  if (WAIT_OBJECT_0 != WaitForSingleObject (h, INFINITE))
1792  {
1793  SetErrnoFromWinError (GetLastError ());
1794  return GNUNET_SYSERR;
1795  }
1796  return GNUNET_OK;
1797 #endif
1798 }
#define LOG_STRERROR(kind, syscall)
Definition: os_priority.c:34
pid_t pid
PID of the process.
Definition: os_priority.c:46
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
static int ret
Final status code.
Definition: gnunet-arm.c:89
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static struct GNUNET_PeerIdentity pid
Identity of the peer we transmit to / connect to.
#define LOG(kind,...)
Definition: os_priority.c:32
Here is the caller graph for this function:

◆ GNUNET_OS_process_wait_status()

int 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.

Blocking version.

Parameters
procpointer to process structure
typestatus type
codereturn code/signal number
Returns
GNUNET_OK on success, GNUNET_NO if the process is still running, GNUNET_SYSERR otherwise

Definition at line 1740 of file os_priority.c.

References process_status().

1743 {
1744  return process_status (proc,
1745  type,
1746  code,
1747  0);
1748 }
static int 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.
Definition: os_priority.c:1603
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Here is the call graph for this function:

◆ GNUNET_OS_install_parent_control_handler()

void GNUNET_OS_install_parent_control_handler ( void *  cls)

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.

Parameters
clsclosure (unused)

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.

Parameters
clsclosure (unused)

Definition at line 150 of file os_priority.c.

References GNUNET_OS_Process::control_pipe, FD_SETSIZE, getenv(), GNUNET_break, GNUNET_DISK_get_handle_from_int_fd(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_OS_CONTROL_PIPE, GNUNET_SCHEDULER_add_read_file(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_TIME_UNIT_FOREVER_REL, LOG, LOG_STRERROR_FILE, parent_control_handler(), and shutdown_pch().

Referenced by install_parent_control_handler().

151 {
152  const char *env_buf;
153  char *env_buf_end;
154  struct GNUNET_DISK_FileHandle *control_pipe;
155  uint64_t pipe_fd;
156 
157  (void) cls;
158  if (NULL != pch)
159  {
160  /* already done, we've been called twice... */
161  GNUNET_break (0);
162  return;
163  }
164  env_buf = getenv (GNUNET_OS_CONTROL_PIPE);
165  if ( (NULL == env_buf) || (strlen (env_buf) <= 0) )
166  {
168  "Not installing a handler because $%s is empty\n",
170  putenv (GNUNET_OS_CONTROL_PIPE "=");
171  return;
172  }
173  errno = 0;
174  pipe_fd = strtoull (env_buf, &env_buf_end, 16);
175  if ((0 != errno) || (env_buf == env_buf_end))
176  {
178  "strtoull",
179  env_buf);
180  putenv (GNUNET_OS_CONTROL_PIPE "=");
181  return;
182  }
183 #if !defined (WINDOWS)
184  if (pipe_fd >= FD_SETSIZE)
185 #else
186  if ((FILE_TYPE_UNKNOWN == GetFileType ((HANDLE) (uintptr_t) pipe_fd))
187  && (0 != GetLastError ()))
188 #endif
189  {
191  "GNUNET_OS_CONTROL_PIPE `%s' contains garbage?\n",
192  env_buf);
193  putenv (GNUNET_OS_CONTROL_PIPE "=");
194  return;
195  }
196 #if WINDOWS
197  control_pipe = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) (uintptr_t) pipe_fd);
198 #else
199  control_pipe = GNUNET_DISK_get_handle_from_int_fd ((int) pipe_fd);
200 #endif
201  if (NULL == control_pipe)
202  {
204  "open",
205  env_buf);
206  putenv (GNUNET_OS_CONTROL_PIPE "=");
207  return;
208  }
210  "Adding parent control handler pipe `%s' to the scheduler\n",
211  env_buf);
213  control_pipe,
215  control_pipe);
217  control_pipe);
218  putenv (GNUNET_OS_CONTROL_PIPE "=");
219 }
#define GNUNET_OS_CONTROL_PIPE
Definition: os_priority.c:38
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1293
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_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_SCHEDULER_Task * spch
Handle for the shutdown_pch() Task.
Definition: os_priority.c:76
#define FD_SETSIZE
Definition: winproc.h:39
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
char * getenv()
#define LOG_STRERROR_FILE(kind, syscall, filename)
Definition: os_priority.c:36
static struct GNUNET_SCHEDULER_Task * pch
Handle for the parent_control_handler() Task.
Definition: os_priority.c:71
struct GNUNET_DISK_FileHandle * GNUNET_DISK_get_handle_from_int_fd(int fno)
Get a handle from a native integer FD.
Definition: disk.c:1937
static void shutdown_pch(void *cls)
This handler is called on shutdown to remove the pch.
Definition: os_priority.c:85
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:102
Handle used to access files (and pipes).
#define LOG(kind,...)
Definition: os_priority.c:32
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_OS_check_helper_binary()

int GNUNET_OS_check_helper_binary ( const char *  binary,
int  check_suid,
const char *  params 
)

Check whether an executable exists and possibly if the suid bit is set on the file.

Attempts to find the file using the current PATH environment variable as a search path.

Parameters
binarythe name of the file to check. W32: must not have an .exe suffix.
check_suidinput true if the binary should be checked for SUID (*nix) W32: checks if the program has sufficient privileges by executing this binary with the -d flag. -d omits a programs main loop and only executes all privileged operations in an binary.
paramsparameters used for w32 privilege checking (can be NULL for != w32, or when not checking for suid/permissions )
Returns
GNUNET_YES if the file is SUID (*nix) or can be executed with current privileges (W32), GNUNET_NO if not SUID (but binary exists), GNUNET_SYSERR on error (no such binary or not executable)

Attempts to find the file using the current PATH environment variable as a search path.

Parameters
binarythe name of the file to check. W32: must not have an .exe suffix.
check_suidinput true if the binary should be checked for SUID (*nix) W32: checks if the program has sufficient privileges by executing this binary with the -d flag. -d omits a programs main loop and only executes all privileged operations in an binary.
paramsparameters used for w32 privilege checking (can be NULL for != w32 )
Returns
GNUNET_YES if the file is SUID (*nix) or can be executed with current privileges (W32), GNUNET_NO if not SUID (but binary exists), GNUNET_SYSERR on error (no such binary or not executable)

Definition at line 872 of file os_installation.c.

References _, ACCESS, get_path_from_PATH(), GNUNET_asprintf(), GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_NO, GNUNET_snprintf(), GNUNET_strdup, GNUNET_STRINGS_path_is_absolute(), GNUNET_SYSERR, GNUNET_YES, LOG, LOG_STRERROR_FILE, p, start, and STAT.

Referenced by GNUNET_NAT_mini_get_external_ipv4_(), GNUNET_NAT_mini_map_start(), handle_autoconfig_request(), LIBGNUNET_PLUGIN_TRANSPORT_INIT(), restart_nat_server(), run(), start_process(), test_icmp_client(), test_icmp_server(), and test_upnpc().

875 {
876  struct stat statbuf;
877  char *p;
878  char *pf;
879 #ifdef MINGW
880  char *binaryexe;
881 
882  GNUNET_asprintf (&binaryexe,
883  "%s.exe",
884  binary);
885  if ( (GNUNET_YES ==
887  GNUNET_NO,
888  NULL, NULL)) ||
889  (0 == strncmp (binary, "./", 2)) )
890  p = GNUNET_strdup (binaryexe);
891  else
892  {
893  p = get_path_from_PATH (binaryexe);
894  if (NULL != p)
895  {
896  GNUNET_asprintf (&pf, "%s/%s", p, binaryexe);
897  GNUNET_free (p);
898  p = pf;
899  }
900  }
901  GNUNET_free (binaryexe);
902 #else
903  if ( (GNUNET_YES ==
905  GNUNET_NO,
906  NULL,
907  NULL)) ||
908  (0 == strncmp (binary, "./", 2)) )
909  {
910  p = GNUNET_strdup (binary);
911  }
912  else
913  {
914  p = get_path_from_PATH (binary);
915  if (NULL != p)
916  {
917  GNUNET_asprintf (&pf,
918  "%s/%s",
919  p,
920  binary);
921  GNUNET_free (p);
922  p = pf;
923  }
924  }
925 #endif
926  if (NULL == p)
927  {
929  _("Could not find binary `%s' in PATH!\n"),
930  binary);
931  return GNUNET_SYSERR;
932  }
933  if (0 != ACCESS (p,
934  X_OK))
935  {
937  "access",
938  p);
939  GNUNET_free (p);
940  return GNUNET_SYSERR;
941  }
942 #ifndef MINGW
943  if (0 == getuid ())
944  {
945  /* as we run as root, we don't insist on SUID */
946  GNUNET_free (p);
947  return GNUNET_YES;
948  }
949 #endif
950  if (0 != STAT (p,
951  &statbuf))
952  {
954  "stat",
955  p);
956  GNUNET_free (p);
957  return GNUNET_SYSERR;
958  }
959  if (check_suid)
960  {
961 #ifndef MINGW
962  (void) params;
963  if ( (0 != (statbuf.st_mode & S_ISUID)) &&
964  (0 == statbuf.st_uid) )
965  {
966  GNUNET_free (p);
967  return GNUNET_YES;
968  }
970  _("Binary `%s' exists, but is not SUID\n"),
971  p);
972  /* binary exists, but not SUID */
973 #else
974  STARTUPINFO start;
975  char parameters[512];
976  PROCESS_INFORMATION proc;
977  DWORD exit_value;
978 
979  GNUNET_snprintf (parameters,
980  sizeof (parameters),
981  "-d %s", params);
982  memset (&start, 0, sizeof (start));
983  start.cb = sizeof (start);
984  memset (&proc, 0, sizeof (proc));
985 
986 
987  // Start the child process.
988  if ( ! (CreateProcess( p, // current windows (2k3 and up can handle / instead of \ in paths))
989  parameters, // execute dryrun/priviliege checking mode
990  NULL, // Process handle not inheritable
991  NULL, // Thread handle not inheritable
992  FALSE, // Set handle inheritance to FALSE
993  CREATE_DEFAULT_ERROR_MODE, // No creation flags
994  NULL, // Use parent's environment block
995  NULL, // Use parent's starting directory
996  &start, // Pointer to STARTUPINFO structure
997  &proc ) // Pointer to PROCESS_INFORMATION structure
998  ))
999  {
1001  _("CreateProcess failed for binary %s (%d).\n"),
1002  p, GetLastError());
1003  return GNUNET_SYSERR;
1004  }
1005 
1006  // Wait until child process exits.
1007  WaitForSingleObject( proc.hProcess, INFINITE );
1008 
1009  if ( ! GetExitCodeProcess (proc.hProcess, &exit_value)){
1011  _("GetExitCodeProcess failed for binary %s (%d).\n"),
1012  p, GetLastError() );
1013  return GNUNET_SYSERR;
1014  }
1015  // Close process and thread handles.
1016  CloseHandle( proc.hProcess );
1017  CloseHandle( proc.hThread );
1018 
1019  if (!exit_value)
1020  return GNUNET_YES;
1021 #endif
1022  }
1023  GNUNET_free (p);
1024  return GNUNET_NO;
1025 }
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
int GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it&#39;s an URI.
Definition: strings.c:1171
#define GNUNET_NO
Definition: gnunet_common.h:81
#define STAT(p, b)
Definition: plibc.h:663
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define LOG(kind,...)
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#define LOG_STRERROR_FILE(kind, syscall, filename)
static char * get_path_from_PATH(const char *binary)
Return the actual path to a file found in the current PATH environment variable.
#define GNUNET_log(kind,...)
#define GNUNET_YES
Definition: gnunet_common.h:80
#define ACCESS(p, m)
Definition: plibc.h:656
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function: