GNUnet  0.10.x
Macros | Functions | Variables
os_installation.c File Reference

get paths used by the program More...

#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <unistr.h>
#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for os_installation.c:

Go to the source code of this file.

Macros

#define LOG(kind, ...)   GNUNET_log_from(kind, "util-os-installation", __VA_ARGS__)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)
 

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 GNUNET_OS_init (const struct GNUNET_OS_ProjectData *pd)
 Setup OS subsystem with project data. More...
 
static char * get_path_from_PATH (const char *binary)
 Return the actual path to a file found in the current PATH environment variable. More...
 
static char * get_path_from_GNUNET_PREFIX ()
 Try to obtain the installation path using the "GNUNET_PREFIX" environment variable. More...
 
static char * os_get_gnunet_path ()
 get the path to GNUnet bin/ or lib/, prefering the lib/ path More...
 
static char * os_get_exec_path ()
 get the path to current app's bin/ 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...
 
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...
 

Variables

static const struct GNUNET_OS_ProjectData default_pd
 Default project data used for installation path detection for GNUnet (core). More...
 
static const struct GNUNET_OS_ProjectDatacurrent_pd = &default_pd
 Which project data do we currently use for installation path detection? Never NULL. More...
 
static int gettextinit = 0
 Wether or not gettext has been initialized for the library. More...
 

Detailed Description

get paths used by the program

Author
Milan
Christian Fuchs
Christian Grothoff
Matthias Wachs
Heikki Lindholm
LRN

Definition in file os_installation.c.

Macro Definition Documentation

◆ LOG

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

Definition at line 45 of file os_installation.c.

Referenced by GNUNET_OS_check_helper_binary(), and os_get_gnunet_path().

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)
Value:
"util-os-installation", \
syscall, \
#define GNUNET_log_from_strerror_file(level, component, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char * filename

Definition at line 48 of file os_installation.c.

Referenced by GNUNET_OS_check_helper_binary(), and GNUNET_OS_init().

Function Documentation

◆ get_path_from_PATH()

static char* get_path_from_PATH ( const char *  binary)
static

Return the actual path to a file found in the current PATH environment variable.

Parameters
binarythe name of the file to find
Returns
path to binary, NULL if not found

Definition at line 306 of file os_installation.c.

References buf, end, getenv(), GNUNET_DISK_file_test(), GNUNET_free, GNUNET_malloc, GNUNET_strdup, GNUNET_YES, p, and PATH_SEPARATOR.

Referenced by GNUNET_OS_check_helper_binary(), and os_get_gnunet_path().

307 {
308  char *path;
309  char *pos;
310  char *end;
311  char *buf;
312  const char *p;
313 
314  if (NULL == (p = getenv("PATH")))
315  return NULL;
316 
317  path = GNUNET_strdup(p); /* because we write on it */
318 
319  buf = GNUNET_malloc(strlen(path) + strlen(binary) + 1 + 1);
320  pos = path;
321  while (NULL != (end = strchr(pos, PATH_SEPARATOR)))
322  {
323  *end = '\0';
324  sprintf(buf, "%s/%s", pos, binary);
325  if (GNUNET_DISK_file_test(buf) == GNUNET_YES)
326  {
327  pos = GNUNET_strdup(pos);
328  GNUNET_free(buf);
329  GNUNET_free(path);
330  return pos;
331  }
332  pos = end + 1;
333  }
334  sprintf(buf, "%s/%s", pos, binary);
335  if (GNUNET_YES == GNUNET_DISK_file_test(buf))
336  {
337  pos = GNUNET_strdup(pos);
338  GNUNET_free(buf);
339  GNUNET_free(path);
340  return pos;
341  }
342  GNUNET_free(buf);
343  GNUNET_free(path);
344  return NULL;
345 }
int GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
Definition: disk.c:541
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
#define PATH_SEPARATOR
Definition: platform.h:169
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
static char buf[2048]
char * getenv()
#define GNUNET_YES
Definition: gnunet_common.h:77
#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:

◆ get_path_from_GNUNET_PREFIX()

static char* get_path_from_GNUNET_PREFIX ( )
static

Try to obtain the installation path using the "GNUNET_PREFIX" environment variable.

Returns
NULL on error (environment variable not set)

Definition at line 355 of file os_installation.c.

References GNUNET_OS_ProjectData::env_varname, GNUNET_OS_ProjectData::env_varname_alt, getenv(), GNUNET_strdup, and p.

Referenced by os_get_gnunet_path().

356 {
357  const char *p;
358 
359  if ((NULL != current_pd->env_varname) &&
360  (NULL != (p = getenv(current_pd->env_varname))))
361  return GNUNET_strdup(p);
362  if ((NULL != current_pd->env_varname_alt) &&
363  (NULL != (p = getenv(current_pd->env_varname_alt))))
364  return GNUNET_strdup(p);
365  return NULL;
366 }
const char * env_varname_alt
Alternative name of an environment variable that can be used to override installation path detection...
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
const char * env_varname
Name of an environment variable that can be used to override installation path detection, for example "GNUNET_PREFIX".
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
char * getenv()
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:
Here is the caller graph for this function:

◆ os_get_gnunet_path()

static char* os_get_gnunet_path ( )
static

get the path to GNUnet bin/ or lib/, prefering the lib/ path

Author
Milan
Returns
a pointer to the executable path, or NULL on error

Definition at line 376 of file os_installation.c.

References _, GNUNET_OS_ProjectData::binary_name, GNUNET_OS_ProjectData::env_varname, get_path_from_GNUNET_PREFIX(), get_path_from_PATH(), GNUNET_ERROR_TYPE_ERROR, LOG, GNUNET_OS_ProjectData::project_dirname, and ret.

Referenced by GNUNET_OS_installation_get_path().

377 {
378  char *ret;
379 
380  if (NULL != (ret = get_path_from_GNUNET_PREFIX()))
381  return ret;
382 #if LINUX
383  if (NULL != (ret = get_path_from_proc_maps()))
384  return ret;
385  /* try path *first*, before /proc/exe, as /proc/exe can be wrong */
386  if ((NULL != current_pd->binary_name) &&
387  (NULL != (ret = get_path_from_PATH(current_pd->binary_name))))
388  return ret;
389  if (NULL != (ret = get_path_from_proc_exe()))
390  return ret;
391 #endif
392 #if DARWIN
393  if (NULL != (ret = get_path_from_dyld_image()))
394  return ret;
395  if (NULL != (ret = get_path_from_NSGetExecutablePath()))
396  return ret;
397 #endif
398  if ((NULL != current_pd->binary_name) &&
399  (NULL != (ret = get_path_from_PATH(current_pd->binary_name))))
400  return ret;
401  /* other attempts here */
403  _(
404  "Could not determine installation path for %s. Set `%s' environment variable.\n"),
407  return NULL;
408 }
const char * project_dirname
Name of the project that is used in the "libexec" prefix, For example, "gnunet".
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
const char * env_varname
Name of an environment variable that can be used to override installation path detection, for example "GNUNET_PREFIX".
#define LOG(kind,...)
const char * binary_name
Name of a project-specific binary that should be in "$PREFIX/bin/".
static const struct GNUNET_OS_ProjectData * current_pd
Which project data do we currently use for installation path detection? Never NULL.
static char * get_path_from_PATH(const char *binary)
Return the actual path to a file found in the current PATH environment variable.
static char * get_path_from_GNUNET_PREFIX()
Try to obtain the installation path using the "GNUNET_PREFIX" environment variable.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ os_get_exec_path()

static char* os_get_exec_path ( )
static

get the path to current app's bin/

Returns
a pointer to the executable path, or NULL on error

Definition at line 416 of file os_installation.c.

References ret.

Referenced by GNUNET_OS_installation_get_path().

417 {
418  char *ret = NULL;
419 
420 #if LINUX
421  if (NULL != (ret = get_path_from_proc_exe()))
422  return ret;
423 #endif
424 #if DARWIN
425  if (NULL != (ret = get_path_from_NSGetExecutablePath()))
426  return ret;
427 #endif
428  /* other attempts here */
429  return ret;
430 }
static int ret
Final status code.
Definition: gnunet-arm.c:89
Here is the caller graph for this function:

Variable Documentation

◆ default_pd

const struct GNUNET_OS_ProjectData default_pd
static
Initial value:
= {
.libname = "libgnunetutil",
.project_dirname = "gnunet",
.binary_name = "gnunet-arm",
.version = PACKAGE_VERSION " " VCS_VERSION,
.env_varname = "GNUNET_PREFIX",
.base_config_varname = "GNUNET_BASE_CONFIG",
.bug_email = "gnunet-developers@gnu.org",
.homepage = "http://www.gnu.org/s/gnunet/",
.config_file = "gnunet.conf",
.user_config_file = "~/.config/gnunet.conf",
.is_gnu = 1,
.gettext_domain = PACKAGE,
.gettext_path = NULL,
}

Default project data used for installation path detection for GNUnet (core).

Definition at line 59 of file os_installation.c.

Referenced by GNUNET_OS_project_data_default().

◆ current_pd

const struct GNUNET_OS_ProjectData* current_pd = &default_pd
static

Which project data do we currently use for installation path detection? Never NULL.

Definition at line 79 of file os_installation.c.

Referenced by GNUNET_OS_project_data_get().

◆ gettextinit

int gettextinit = 0
static

Wether or not gettext has been initialized for the library.

Note that the gettext initialization done within GNUNET_PROGRAM_run2 is for the specific application.

Definition at line 86 of file os_installation.c.

Referenced by GNUNET_OS_init(), and GNUNET_OS_project_data_get().