Greenbone Vulnerability Manager
22.4.0~dev1
|
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... | |
Generic utilities.
Generic helper utilities. None of these are GVM specific. They could be used anywhere.
#define _XOPEN_SOURCE |
Enable extra functions.
time.h in glibc2 needs this for strptime.
int days_from_now | ( | time_t * | epoch_time | ) |
Calculate difference between now and epoch_time in days.
[in] | epoch_time | Time in seconds from epoch. |
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.
int gvm_sleep | ( | unsigned int | seconds | ) |
Sleep for some number of seconds, handling interrupts.
[in] | seconds | Number of seconds. |
int gvm_usleep | ( | unsigned int | microseconds | ) |
Sleep for some number of microseconds, handling interrupts.
[in] | microseconds | Number of microseconds. |
int is_uuid | ( | const char * | uuid | ) |
Check whether a string is a UUID.
[in] | uuid | Potential UUID. |
char* iso_time | ( | time_t * | epoch_time | ) |
Create an ISO time from seconds since epoch.
[in] | epoch_time | Time in seconds from epoch. |
|
static |
Create an ISO time from seconds since epoch.
[in] | epoch_time | Time in seconds from epoch. |
[out] | abbrev | Abbreviation for current timezone. |
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.
[in] | epoch_time | Time in seconds from epoch. |
[in] | zone | Timezone. |
[out] | abbrev | Timezone abbreviation. |
|
static |
Lock a file.
[in] | lockfile | Lockfile. |
[in] | lockfile_name | Basename or full path of lock file. |
[in] | operation | LOCK_EX (exclusive) or LOCK_SH (shared). Maybe ORd with LOCK_NB to prevent blocking. |
[in] | name_is_full_path | Whether the name is a full path. |
int lockfile_lock | ( | lockfile_t * | lockfile, |
const gchar * | lockfile_basename | ||
) |
Lock a file exclusively.
Block until file is locked.
[in] | lockfile | Lockfile. |
[in] | lockfile_basename | Basename of lock file. |
int lockfile_lock_nb | ( | lockfile_t * | lockfile, |
const gchar * | lockfile_basename | ||
) |
Lock a file exclusively, without blocking.
[in] | lockfile | Lockfile. |
[in] | lockfile_basename | Basename of lock file. |
int lockfile_lock_path_nb | ( | lockfile_t * | lockfile, |
const gchar * | lockfile_path | ||
) |
Lock a file exclusively, without blocking, given a full path.
[in] | lockfile | Lockfile. |
[in] | lockfile_path | Full path of lock file. |
int lockfile_lock_shared_nb | ( | lockfile_t * | lockfile, |
const gchar * | lockfile_basename | ||
) |
Lock a file with a shared lock.
[in] | lockfile | Lockfile. |
[in] | lockfile_basename | Basename of lock file. |
int lockfile_locked | ( | const gchar * | lockfile_basename | ) |
Check if a file is locked.
[in] | lockfile_basename | Basename of lock file. |
int lockfile_unlock | ( | lockfile_t * | lockfile | ) |
Unlock a file.
[in] | lockfile | Lockfile. |
|
static |
Converts a tm struct into seconds since epoch with a given timezone.
[in] | tm | The time data structure. |
[in] | new_tz | The timezone to use or NULL for UTC. |
int parse_ctime | ( | const char * | text_time | ) |
Convert a ctime into seconds since epoch.
Use the current timezone.
[in] | text_time | Time as text in ctime format. |
int parse_feed_timestamp | ( | const char * | text_time | ) |
Convert a feed timestamp into seconds since epoch.
[in] | text_time | Time as text in ctime format. |
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).
[in] | text_time | Time as text in ISO format: 2011-11-03T09:23:28+02:00. |
[in] | fallback_tz | The fallback timezone if offset is missing. |
int parse_utc_ctime | ( | const char * | text_time | ) |
Convert a UTC ctime string into seconds since the epoch.
[in] | text_time | Time as text in ctime format. |
|
static |
Convert a UTC time into seconds since epoch.
[in] | format | Format of time. |
[in] | text_time | Time as text. |
int parse_xml_file | ( | const gchar * | path, |
entity_t * | config | ||
) |
Create entity from XML file.
[in] | path | Path to XML. |
[out] | config | Config tree. |
void setup_signal_handler | ( | int | signal, |
void(*)(int) | handler, | ||
int | block | ||
) |
Setup signal handler.
Exit on failure.
[in] | signal | Signal. |
[in] | handler | Handler. |
[in] | block | Whether to block all other signals during handler. |
void setup_signal_handler_info | ( | int | signal, |
void(*)(int, siginfo_t *, void *) | handler, | ||
int | block | ||
) |
Setup signal handler.
Exit on failure.
[in] | signal | Signal. |
[in] | handler | Handler. |
[in] | block | Whether to block all other signals during handler. |
|
static |
Parses a time string using strptime, resetting the data structure.
[in] | text_time | The time string to parse. |
[in] | format | The format string. |
[out] | tm | The tm date structure to write to. |