Greenbone Vulnerability Manager  22.4.0~dev1
Macros | Functions
utils.c File Reference

Generic utilities. More...

#include "utils.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <gvm/base/gvm_sentry.h>

Macros

#define _XOPEN_SOURCE
 Enable extra functions. More...
 
#define _POSIX_C_SOURCE   199309L
 Needed for nanosleep.
 
#define G_LOG_DOMAIN   "md manage"
 GLib log domain.
 

Functions

int gvm_usleep (unsigned int microseconds)
 Sleep for some number of microseconds, handling interrupts. More...
 
int gvm_sleep (unsigned int seconds)
 Sleep for some number of seconds, handling interrupts. More...
 
static int parse_utc_time (const char *format, const char *text_time)
 Convert a UTC time into seconds since epoch. More...
 
static char * strptime_with_reset (const char *text_time, const char *format, struct tm *tm)
 Parses a time string using strptime, resetting the data structure. More...
 
static time_t mktime_with_tz (struct tm *tm, const char *new_tz)
 Converts a tm struct into seconds since epoch with a given timezone. More...
 
int parse_utc_ctime (const char *text_time)
 Convert a UTC ctime string into seconds since the epoch. More...
 
int parse_feed_timestamp (const char *text_time)
 Convert a feed timestamp into seconds since epoch. More...
 
int parse_ctime (const char *text_time)
 Convert a ctime into seconds since epoch. More...
 
int days_from_now (time_t *epoch_time)
 Calculate difference between now and epoch_time in days. More...
 
time_t parse_iso_time_tz (const char *text_time, const char *fallback_tz)
 Convert an ISO time into seconds since epoch. More...
 
static char * iso_time_internal (time_t *epoch_time, const char **abbrev)
 Create an ISO time from seconds since epoch. More...
 
char * iso_time (time_t *epoch_time)
 Create an ISO time from seconds since epoch. More...
 
char * iso_time_tz (time_t *epoch_time, const char *zone, const char **abbrev)
 Create an ISO time from seconds since epoch, given a timezone. More...
 
static int lock_internal (lockfile_t *lockfile, const gchar *lockfile_name, int operation, gboolean name_is_full_path)
 Lock a file. More...
 
int lockfile_lock (lockfile_t *lockfile, const gchar *lockfile_basename)
 Lock a file exclusively. More...
 
int lockfile_lock_nb (lockfile_t *lockfile, const gchar *lockfile_basename)
 Lock a file exclusively, without blocking. More...
 
int lockfile_lock_path_nb (lockfile_t *lockfile, const gchar *lockfile_path)
 Lock a file exclusively, without blocking, given a full path. More...
 
int lockfile_lock_shared_nb (lockfile_t *lockfile, const gchar *lockfile_basename)
 Lock a file with a shared lock. More...
 
int lockfile_unlock (lockfile_t *lockfile)
 Unlock a file. More...
 
int lockfile_locked (const gchar *lockfile_basename)
 Check if a file is locked. More...
 
int is_uuid (const char *uuid)
 Check whether a string is a UUID. More...
 
int parse_xml_file (const gchar *path, entity_t *config)
 Create entity from XML file. More...
 
void setup_signal_handler (int signal, void(*handler)(int), int block)
 Setup signal handler. More...
 
void setup_signal_handler_info (int signal, void(*handler)(int, siginfo_t *, void *), int block)
 Setup signal handler. More...
 
int fork_with_handlers ()
 Fork, setting default handlers for TERM, INT and QUIT in child. More...
 

Detailed Description

Generic utilities.

Generic helper utilities. None of these are GVM specific. They could be used anywhere.

Macro Definition Documentation

◆ _XOPEN_SOURCE

#define _XOPEN_SOURCE

Enable extra functions.

time.h in glibc2 needs this for strptime.

Function Documentation

◆ days_from_now()

int days_from_now ( time_t *  epoch_time)

Calculate difference between now and epoch_time in days.

Parameters
[in]epoch_timeTime in seconds from epoch.
Returns
Int days bettween now and epoch_time or -1 if epoch_time is in the past

◆ fork_with_handlers()

int fork_with_handlers ( )

Fork, setting default handlers for TERM, INT and QUIT in child.

This should be used for pretty much all processes forked directly from the main gvmd process, because the main process's signal handlers will not longer work, because the child does not use the pselect loop.

Returns
PID from fork.

◆ gvm_sleep()

int gvm_sleep ( unsigned int  seconds)

Sleep for some number of seconds, handling interrupts.

Parameters
[in]secondsNumber of seconds.
Returns
0 success, -1 error (with errno set).

◆ gvm_usleep()

int gvm_usleep ( unsigned int  microseconds)

Sleep for some number of microseconds, handling interrupts.

Parameters
[in]microsecondsNumber of microseconds.
Returns
0 success, -1 error (with errno set).

◆ is_uuid()

int is_uuid ( const char *  uuid)

Check whether a string is a UUID.

Parameters
[in]uuidPotential UUID.
Returns
1 yes, 0 no.

◆ iso_time()

char* iso_time ( time_t *  epoch_time)

Create an ISO time from seconds since epoch.

Parameters
[in]epoch_timeTime in seconds from epoch.
Returns
Pointer to ISO time in static memory, or NULL on error.

◆ iso_time_internal()

static char* iso_time_internal ( time_t *  epoch_time,
const char **  abbrev 
)
static

Create an ISO time from seconds since epoch.

Parameters
[in]epoch_timeTime in seconds from epoch.
[out]abbrevAbbreviation for current timezone.
Returns
Pointer to ISO time in static memory, or NULL on error.

◆ iso_time_tz()

char* iso_time_tz ( time_t *  epoch_time,
const char *  zone,
const char **  abbrev 
)

Create an ISO time from seconds since epoch, given a timezone.

Parameters
[in]epoch_timeTime in seconds from epoch.
[in]zoneTimezone.
[out]abbrevTimezone abbreviation.
Returns
Pointer to ISO time in static memory, or NULL on error.

◆ lock_internal()

static int lock_internal ( lockfile_t lockfile,
const gchar *  lockfile_name,
int  operation,
gboolean  name_is_full_path 
)
static

Lock a file.

Parameters
[in]lockfileLockfile.
[in]lockfile_nameBasename or full path of lock file.
[in]operationLOCK_EX (exclusive) or LOCK_SH (shared). Maybe ORd with LOCK_NB to prevent blocking.
[in]name_is_full_pathWhether the name is a full path.
Returns
0 success, 1 already locked, -1 error

◆ lockfile_lock()

int lockfile_lock ( lockfile_t lockfile,
const gchar *  lockfile_basename 
)

Lock a file exclusively.

Block until file is locked.

Parameters
[in]lockfileLockfile.
[in]lockfile_basenameBasename of lock file.
Returns
0 success, 1 already locked, -1 error

◆ lockfile_lock_nb()

int lockfile_lock_nb ( lockfile_t lockfile,
const gchar *  lockfile_basename 
)

Lock a file exclusively, without blocking.

Parameters
[in]lockfileLockfile.
[in]lockfile_basenameBasename of lock file.
Returns
0 success, 1 already locked, -1 error

◆ lockfile_lock_path_nb()

int lockfile_lock_path_nb ( lockfile_t lockfile,
const gchar *  lockfile_path 
)

Lock a file exclusively, without blocking, given a full path.

Parameters
[in]lockfileLockfile.
[in]lockfile_pathFull path of lock file.
Returns
0 success, 1 already locked, -1 error

◆ lockfile_lock_shared_nb()

int lockfile_lock_shared_nb ( lockfile_t lockfile,
const gchar *  lockfile_basename 
)

Lock a file with a shared lock.

Parameters
[in]lockfileLockfile.
[in]lockfile_basenameBasename of lock file.
Returns
0 success, 1 already locked, -1 error

◆ lockfile_locked()

int lockfile_locked ( const gchar *  lockfile_basename)

Check if a file is locked.

Parameters
[in]lockfile_basenameBasename of lock file.
Returns
0 free, 1 locked, -1 error

◆ lockfile_unlock()

int lockfile_unlock ( lockfile_t lockfile)

Unlock a file.

Parameters
[in]lockfileLockfile.
Returns
0 success, -1 error

◆ mktime_with_tz()

static time_t mktime_with_tz ( struct tm *  tm,
const char *  new_tz 
)
static

Converts a tm struct into seconds since epoch with a given timezone.

Parameters
[in]tmThe time data structure.
[in]new_tzThe timezone to use or NULL for UTC.
Returns
The seconds since epoch from the given time data.

◆ parse_ctime()

int parse_ctime ( const char *  text_time)

Convert a ctime into seconds since epoch.

Use the current timezone.

Parameters
[in]text_timeTime as text in ctime format.
Returns
Time since epoch.

◆ parse_feed_timestamp()

int parse_feed_timestamp ( const char *  text_time)

Convert a feed timestamp into seconds since epoch.

Parameters
[in]text_timeTime as text in ctime format.
Returns
Time since epoch. 0 on error.

◆ parse_iso_time_tz()

time_t parse_iso_time_tz ( const char *  text_time,
const char *  fallback_tz 
)

Convert an ISO time into seconds since epoch.

If no offset is specified, the given timezone is used (UTC in case of NULL).

Parameters
[in]text_timeTime as text in ISO format: 2011-11-03T09:23:28+02:00.
[in]fallback_tzThe fallback timezone if offset is missing.
Returns
Time since epoch. 0 on error.

◆ parse_utc_ctime()

int parse_utc_ctime ( const char *  text_time)

Convert a UTC ctime string into seconds since the epoch.

Parameters
[in]text_timeTime as text in ctime format.
Returns
Time since epoch. 0 on error.

◆ parse_utc_time()

static int parse_utc_time ( const char *  format,
const char *  text_time 
)
static

Convert a UTC time into seconds since epoch.

Parameters
[in]formatFormat of time.
[in]text_timeTime as text.
Returns
Time since epoch. 0 on error.

◆ parse_xml_file()

int parse_xml_file ( const gchar *  path,
entity_t *  config 
)

Create entity from XML file.

Parameters
[in]pathPath to XML.
[out]configConfig tree.
Returns
0 success, -1 error.

◆ setup_signal_handler()

void setup_signal_handler ( int  signal,
void(*)(int)  handler,
int  block 
)

Setup signal handler.

Exit on failure.

Parameters
[in]signalSignal.
[in]handlerHandler.
[in]blockWhether to block all other signals during handler.

◆ setup_signal_handler_info()

void setup_signal_handler_info ( int  signal,
void(*)(int, siginfo_t *, void *)  handler,
int  block 
)

Setup signal handler.

Exit on failure.

Parameters
[in]signalSignal.
[in]handlerHandler.
[in]blockWhether to block all other signals during handler.

◆ strptime_with_reset()

static char* strptime_with_reset ( const char *  text_time,
const char *  format,
struct tm *  tm 
)
static

Parses a time string using strptime, resetting the data structure.

Parameters
[in]text_timeThe time string to parse.
[in]formatThe format string.
[out]tmThe tm date structure to write to.
Returns
Pointer to first character not processed by strptime.