Macros | Functions | Variables
MAIN: Command line option parser

Macros

#define PRINT_ERROR(s)   { print_error(MAIN_ERR_PREFIX s); }
 Prepend module prefix and print error message.
 
#define MAIN_ERR_PREFIX   "MAIN: "
 Message prefix for MAIN module.
 
#define SECURE_CL_PERM   (posix_mode_t) (POSIX_S_IRUSR | POSIX_S_IWUSR)
 Permissions for Cancel-Lock secret file.
 
#define SECURE_CL_SECSIZE   (size_t) 64
 Size of secret (64 octets is sufficient for the SHA2 family)
 
#define MAIN_ERR_PREFIX   "MAIN: "
 Message prefix for security related functions.
 
#define MAIN_ERR_PREFIX   "MAIN: "
 Message prefix for MAIN module.
 
#define MAIN_ERR_PREFIX   "MAIN: "
 Message prefix for MAIN module.
 

Functions

int ts_lock_ui (void)
 Lock UI thread. More...
 
int ts_unlock_ui (void)
 Unlock UI thread. More...
 
void print_error (const char *msg)
 Print error message. More...
 
int main (int argc, char **argv)
 Program entry point. More...
 
void secure_clear_memory (char *p, size_t len)
 Remove string from memory. More...
 
void secure_clear_string (char *p)
 Remove string from memory. More...
 
void secure_cl_secret (const char *pathname)
 Generate file with new secret if CL secret file is missing. More...
 
int sighandler_install (void)
 Install signal handlers. More...
 
int sighandler_exec_prepare (void)
 Prepare for exec() More...
 
int sighandler_check_abort (void)
 Check abort flag. More...
 
void ts_environ_init (void)
 Copy environment variables. More...
 
void ts_environ_exit (void)
 Destroy copy of environment variables. More...
 
int ts_getenv (const char *name, const char **buf)
 Thread safe replacement for getenv() More...
 

Variables

const char BDATE []
 
int main_debug = 0
 Enable additional debug output if nonzero.
 
const char * main_confprefix = NULL
 Configuration directory path from command line option or NULL otherwise.
 

Detailed Description

The POSIX getenv() function is not required to be thread safe. Therefore we must provide our own. It was considered to clone the getenv_r() function from BSD, but this function has no buffer management and is inconvenient to use. The function ts_getenv() dynamically allocate or resize the thread local buffer internally.

Note
A copy of the enviroment is used, therefore all modifications of the enviroment at runtime are invisible via ts_getenv(). ts_environ_init() and ts_environ_exit() must be called to create and destroy this copy of the enviroment.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Program entry point.

Parameters
[in]argcNumber of command line arguments
[in]argvArray containing command line argument strings

Parse command line and control startup and shutdown

Returns
  • EXIT_SUCCESS on success
  • EXIT_FAILURE on error

Definition at line 315 of file main.cxx.

References log_delete_logfile(), sighandler_install(), and ts_environ_init().

◆ print_error()

void print_error ( const char *  msg)

Print error message.

Exported as C style function

Parameters
[in]msgString (Unicode error message)

This function executes as NOP if msg is NULL . Otherwise msg must be a NUL-terminated string.
The Unicode encoding of msg is checked and is allowed to be invalid.

Attention
Currently only the US-ASCII subset of Unicode will be readable in output.
Note
This function is guaranteed to work before other modules are initialized.

Definition at line 276 of file main.cxx.

◆ secure_cl_secret()

void secure_cl_secret ( const char *  pathname)

Generate file with new secret if CL secret file is missing.

Parameters
[in]pathnamePathname string of CL secret file

If pathname is NULL no operation is excecuted.

Attention
This function currently only works if TLS support is available (because it uses the cryptographic PRNG provided by OpenSSL). The caller must ensure, that the OpenSSL PRNG is seeded!

Definition at line 138 of file secure.c.

References fu_check_file(), fu_close_file(), fu_open_file(), fu_unlink_file(), fu_write_to_filedesc(), MAIN_ERR_PREFIX, PRINT_ERROR, SECURE_CL_PERM, SECURE_CL_SECSIZE, and secure_clear_memory().

Referenced by core_get_cancel_key().

◆ secure_clear_memory()

void secure_clear_memory ( char *  p,
size_t  len 
)

Remove string from memory.

Parameters
[in]pPointer to memory block
[in]lenNumber of bytes to clear

This function overwrites len bytes starting at location p with undefined data.

Note
One or both parameters are allowed to be zero. This function executes as NOP in this case.
Attention
This function is currently only secure if TLS support is available (because it share a cryptographic function provided by OpenSSL).

Definition at line 72 of file secure.c.

Referenced by core_get_cancel_key(), secure_cl_secret(), and secure_clear_string().

◆ secure_clear_string()

void secure_clear_string ( char *  p)

Remove string from memory.

Parameters
[in]pPointer to string
Attention
p must point to a NUL terminated string.

The complete string is overwritten and then NUL is written to the first byte. The function therefore returns with p pointing to an empty string.

Attention
This function is currently only secure if TLS support is available (because it share a cryptographic function provided by OpenSSL).

Definition at line 116 of file secure.c.

References secure_clear_memory().

Referenced by conf_string_replace().

◆ sighandler_check_abort()

int sighandler_check_abort ( void  )

Check abort flag.

Returns
  • False if abort flag is not set
  • True if abort flag is set

Definition at line 155 of file sighandler.c.

◆ sighandler_exec_prepare()

int sighandler_exec_prepare ( void  )

Prepare for exec()

Restore default state for ignored signals.

Returns
  • 0 on success
  • Negative value on error

Definition at line 119 of file sighandler.c.

References PRINT_ERROR.

Referenced by ext_editor(), ext_inews(), and ext_pp_filter().

◆ sighandler_install()

int sighandler_install ( void  )

Install signal handlers.

Ignored signals: SIGHUP, SIGPIPE

Signals that trigger save exit: SIGTERM, SIGQUIT, SIGINT

Returns
  • 0 on success
  • Negative value on error

Definition at line 66 of file sighandler.c.

Referenced by main().

◆ ts_environ_exit()

void ts_environ_exit ( void  )

Destroy copy of environment variables.

Call this function once after last use of ts_getenv() .

Definition at line 107 of file ts_functions.c.

◆ ts_environ_init()

void ts_environ_init ( void  )

Copy environment variables.

Must be called once before ts_getenv() is used and before additional threads are created.

Definition at line 58 of file ts_functions.c.

Referenced by main().

◆ ts_getenv()

int ts_getenv ( const char *  name,
const char **  buf 
)

Thread safe replacement for getenv()

Parameters
[in]nameName of the requested environment variable
[in,out]bufPointer to pointer to buffer for result

Dereferenced buf must be either NULL or an existing dynamically allocated buffer that can be resized with realloc() if required.

The buffer pointed to by buf is created or resized so that it can hold the result and the value of the environment variable name is copied to it.

The caller is responsible to free the memory allocated for the buffer on success.

Returns
  • 0 on success
  • Negative value on error

Definition at line 136 of file ts_functions.c.

Referenced by core_get_homedir(), core_get_signature(), and xdg_get_confdir().

◆ ts_lock_ui()

int ts_lock_ui ( void  )

Lock UI thread.

Exported as C style function

Note
It is save to call this function for all threads, including the UI thread itself.
Attention
It is not allowed for a thread to call this function multiple times without unlock between them!
Returns
  • 0 on success
  • Negative value on error

Definition at line 217 of file main.cxx.

References core_check_thread_ui(), and ui_lock().

◆ ts_unlock_ui()

int ts_unlock_ui ( void  )

Unlock UI thread.

Exported as C style function

Note
It is save to call this function for all threads, including the UI thread itself.
Returns
  • 0 on success
  • Negative value on error

Definition at line 244 of file main.cxx.

References core_check_thread_ui(), and ui_unlock().


Generated at 2024-04-27 using  doxygen