libosmocore 1.9.0.192-1c24
Osmocom core library
exec.c File Reference
#include "config.h"
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <pwd.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/exec.h>

Macros

#define _GNU_SOURCE
 

Functions

static bool str_in_list (const char **list, const char *key)
 
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...
 
char ** environ
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Function Documentation

◆ osmo_close_all_fds_above()

int osmo_close_all_fds_above ( int  last_fd_to_keep)

References DLGLOBAL, LOGL_ERROR, and LOGP.

Referenced by osmo_system_nowait2().

◆ osmo_environment_append()

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.

Parameters
[out]outcaller-allocated array of pointers for the generated output
[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().

◆ osmo_environment_filter()

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.

Parameters
[out]outcaller-allocated array of pointers for the generated output
[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().

◆ osmo_system_nowait()

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().

◆ 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().

◆ str_in_list()

static bool str_in_list ( const char **  list,
const char *  key 
)
static

References list.

Referenced by osmo_environment_filter().

Variable Documentation

◆ environ

char** environ
extern

Referenced by osmo_system_nowait2().

◆ osmo_environment_whitelist

const char* osmo_environment_whitelist[]
Initial value:
= {
"USER", "LOGNAME", "HOME",
"LANG", "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME",
"PATH",
"PWD",
"SHELL",
"TERM",
"TMPDIR",
"LD_LIBRARY_PATH",
"LD_PRELOAD",
"POSIXLY_CORRECT",
"HOSTALIASES",
"TZ", "TZDIR",
"TERMCAP",
"COLUMNS", "LINES",
NULL
}

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