libosmocore  1.5.1
Osmocom core library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
exec.h File Reference

Go to the source code of this file.

Functions

int osmo_environment_filter (char **out, size_t out_len, char **in, const char **whitelist)
 filtered a process environment by whitelist; only copying pointers, no actual strings. More...
 
int osmo_environment_append (char **out, size_t out_len, char **in)
 append one environment to another; only copying pointers, not actual strings. More...
 
int osmo_close_all_fds_above (int last_fd_to_keep)
 
int osmo_system_nowait2 (const char *command, const char **env_whitelist, char **addl_env, const char *user)
 call an external shell command as 'user' without waiting for it. More...
 
int osmo_system_nowait (const char *command, const char **env_whitelist, char **addl_env)
 call an external shell command without waiting for it. More...
 

Variables

const char * osmo_environment_whitelist []
 suggested list of environment variables to pass (if they exist) to a sub-process/script More...
 

Function Documentation

int osmo_close_all_fds_above ( int  last_fd_to_keep)

References DLGLOBAL, LOGL_ERROR, and LOGP.

Referenced by osmo_system_nowait2().

int osmo_environment_append ( char **  out,
size_t  out_len,
char **  in 
)

append one environment to another; only copying pointers, not actual strings.

This function is useful if you'd like to append soem entries to an environment befoer passing it to exec*e() functions.

It will append all entries from 'in' to the environment in 'out', as long as 'out' has space (determined by 'out_len').

Constraints: If the same key exists in 'out' and 'in', duplicate keys are generated. It is a simple append, without any duplicate checks.

[out] out caller-allocated array of pointers for the generated output

Parameters
[in]out_lensize of out (number of pointers)
[in]ininput environment (NULL-terminated list of pointers like **environ)
Returns
number of entries filled in 'out'; negative on error

References OSMO_ASSERT.

Referenced by osmo_system_nowait2().

int osmo_environment_filter ( char **  out,
size_t  out_len,
char **  in,
const char **  whitelist 
)

filtered a process environment by whitelist; only copying pointers, no actual strings.

This function is useful if you'd like to generate an environment to pass exec*e() functions. It will create a new environment containing only those entries whose keys (as per environment convention KEY=VALUE) are contained in the whitelist. The function will not copy the actual strings, but just create a new pointer array, pointing to the same memory as the input strings.

Constraints: Keys up to a maximum length of 255 characters are supported.

[out] out caller-allocated array of pointers for the generated output

Parameters
[in]out_lensize of out (number of pointers)
[in]ininput environment (NULL-terminated list of pointers like **environ)
[in]whitelistwhitelist of permitted keys in environment (like **environ)
Returns
number of entries filled in 'out'; negtive on error

References ARRAY_SIZE, OSMO_ASSERT, and str_in_list().

Referenced by osmo_system_nowait2().

int osmo_system_nowait ( const char *  command,
const char **  env_whitelist,
char **  addl_env 
)

call an external shell command without waiting for it.

This mimics the behavior of system(3), with the following differences:

  • it doesn't wait for completion of the child process
  • it closes all non-stdio file descriptors by iterating /proc/self/fd
  • it constructs a reduced environment where only whitelisted keys survive
  • it (optionally) appends additional variables to the environment
Parameters
[in]commandthe shell command to be executed, see system(3)
[in]env_whitelistA white-list of keys for environment variables
[in]addl_envany additional environment variables to be appended
Returns
PID of generated child process; negative on error

References osmo_system_nowait2().

int osmo_system_nowait2 ( const char *  command,
const char **  env_whitelist,
char **  addl_env,
const char *  user 
)

call an external shell command as 'user' without waiting for it.

This mimics the behavior of system(3), with the following differences:

  • it doesn't wait for completion of the child process
  • it closes all non-stdio file descriptors by iterating /proc/self/fd
  • it constructs a reduced environment where only whitelisted keys survive
  • it (optionally) appends additional variables to the environment
  • it (optionally) changes the user ID to that of 'user' (requires execution as root)
Parameters
[in]commandthe shell command to be executed, see system(3)
[in]env_whitelistA white-list of keys for environment variables
[in]addl_envany additional environment variables to be appended
[in]username of the user to which we should switch before executing the command
Returns
PID of generated child process; negative on error

References ARRAY_SIZE, DLGLOBAL, environ, LOGL_ERROR, LOGP, osmo_close_all_fds_above(), osmo_environment_append(), and osmo_environment_filter().

Referenced by osmo_system_nowait().

Variable Documentation

const char* osmo_environment_whitelist[]

suggested list of environment variables to pass (if they exist) to a sub-process/script