21 #include <sys/types.h>
29 #define MAX_OS_PATH_LEN 4096
30 #define MAX_WEB_PATH_LEN 2048
31 #define MAX_RANDOM_UUID_LEN 37
33 #define OS_HOST_NAME_MAX 64
36 #define BIT(x) (1U << (x))
40 #define ARRAY_SIZE(s) (sizeof(s) / sizeof(s[0]))
43 #define BD_NO_CHDIR 01
44 #define BD_NO_CLOSE_FILES 02
45 #define BD_NO_REOPEN_STD_FDS \
48 #define BD_NO_UMASK0 010
49 #define BD_MAX_CLOSE \
55 #define os_strlen(s) strlen((s))
59 #define os_snprintf(s, maxlen, ...) snprintf((s), (maxlen), __VA_ARGS__)
63 #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
72 #define os_strstr(s1, s2) strstr((s1), (s2))
76 #define os_strcmp(s1, s2) strcmp((s1), (s2))
85 #define os_strchr(s, c) strchr((s), (c))
114 #define os_get_time(t) edge_os_get_time(t)
137 #ifndef os_get_reltime
138 #define os_get_reltime(t) edge_os_get_reltime((t))
148 static inline int os_reltime_before(
const struct os_reltime *a,
160 static inline void os_reltime_sub(
const struct os_reltime *a,
169 res->
usec += 1000000;
189 #define os_get_random(buf, len) edge_os_get_random((buf), (len))
254 #define os_strlcpy(dest, src, siz) edge_os_strlcpy((dest), (src), (siz))
280 #define os_memcmp_const(a, b, len) edge_os_memcmp_const((a), (b), (len))
310 #define aliasing_hide_typecast(a, t) (t *)__hide_aliasing_typecast((a))
340 char **
copy_argv(
const char *
const argv[]);
408 char *
concat_paths(
const char *path_left,
const char *path_right);
427 char *
construct_path(
const char *path_left,
const char *path_right);
437 char *
get_secure_path(
const UT_array *bin_path_arr,
const char *filename,
471 pid_t
is_proc_app(
const char *path,
const char *proc_name);
529 char *
rtrim(
char *str,
const char *seps);
598 int create_dir(
const char *dirpath, mode_t mode);
660 ssize_t
read_file(
const char *path, uint8_t **out);
File containing the definition of the hashmap utilities.
File containing the implementation of the logging functions.
int edge_os_get_random(unsigned char *buf, size_t len)
Get cryptographically strong pseudo random data.
Definition: os.c:237
void generate_radom_uuid(char rid[static MAX_RANDOM_UUID_LEN])
Generates a random UUID string of MAX_RANDOM_UUID_LEN - 1 characters long not including '\0'.
Definition: os.c:1153
size_t edge_os_strlcpy(char *restrict dest, const char *restrict src, size_t siz)
Copy a string with size bound and NUL-termination.
Definition: os.c:210
int list_dir(const char *dirpath, list_dir_fn fun, void *args)
List the files in a directory.
Definition: os.c:773
int become_daemon(int flags)
Becomes a daemon.
Definition: os.c:44
char * construct_path(const char *path_left, const char *path_right)
Construct a valid path from two paths.
Definition: os.c:679
size_t os_strnlen_s(const char *str, size_t max_len)
Returns the size of string with a give max length.
Definition: os.c:1159
void(* process_callback_fn)(void *ctx, void *buf, size_t count)
Callback function for run_command() and similar functions.
Definition: os.h:317
int get_hostname(char buf[static OS_HOST_NAME_MAX])
Get the hostname of the running machine.
Definition: os.c:1293
int create_pipe_file(const char *path)
Creates a FIFO pipe file.
Definition: os.c:1247
int check_file_exists(const char *path, struct stat *sb)
Check if a file exists.
Definition: os.c:1266
char * rtrim(char *str, const char *seps)
Right trim the string.
Definition: os.c:1100
int run_argv_command(const char *path, const char *const argv[], process_callback_fn fn, void *ctx)
Executes a command with argument.
Definition: os.c:497
char * get_valid_path(const char *path)
Get the valid path string.
Definition: os.c:642
ssize_t split_string(const char *str, char sep, split_string_fn fun, void *data)
Splits a string into substrings (execute callback function)
Definition: os.c:561
int make_file_exec_fd(int fd)
Makes a file given by descriptor executable.
Definition: os.c:1079
bool is_number(const char *ptr)
Check if a string is a number.
Definition: os.c:118
int exist_dir(const char *dirpath)
Check if folder exists.
Definition: os.c:1178
void replace_string_char(char *s, char in, char out)
Replace a character in a string with a given characater.
Definition: os.c:1121
int read_file_string(const char *path, char **out)
Read the entire file into a string.
Definition: os.c:1448
void * __hide_aliasing_typecast(void *foo)
Definition: os.c:276
int edge_hexstr2bin(const char *hex, uint8_t *buf, size_t len)
Convert ASCII hex string into binary data.
Definition: os.c:196
int edge_os_memcmp_const(const void *a, const void *b, size_t len)
Constant time memory comparison.
Definition: os.c:225
void os_init_random_seed(void)
Initialises the random seed.
Definition: os.c:257
char * string_append_char(const char *str, char character)
Append a character to a string and return the new string.
Definition: os.c:1534
#define OS_HOST_NAME_MAX
Definition: os.h:33
ssize_t split_string_array(const char *str, char sep, UT_array *arr)
Splits a string into substrings (save to array)
Definition: os.c:598
int create_dir(const char *dirpath, mode_t mode)
Creates a folder recursively.
Definition: os.c:1222
int run_process(char *argv[], pid_t *child_pid)
Executes a background process with an array of string arguments.
Definition: os.c:1006
char * string_array2string(const char *const strings[])
Concatenates an array of strings into a single string.
Definition: os.c:980
bool kill_process(char *proc_name)
Kill a process by name.
Definition: os.c:976
char ** copy_argv(const char *const argv[])
Makes a copy of argv.
Definition: os.c:301
bool find_dir_proc_fn(char *path, void *args)
Callback function for list_dir function to check if process running.
Definition: os.c:1168
int os_get_timestamp(uint64_t *timestamp)
get the timestamp in microseconds from system time
Definition: os.c:1140
void os_to_timestamp(struct timeval ts, uint64_t *timestamp)
get the timestamp in microseconds from struct timeval
Definition: os.c:1133
char * concat_paths(const char *path_left, const char *path_right)
Concatenate two string paths.
Definition: os.c:607
int(* split_string_fn)(const char *, size_t, void *)
Definition: os.h:374
int check_sock_file_exists(const char *path)
Check if a socket file exists.
Definition: os.c:1279
int make_dirs_to_path(const char *file_path, mode_t mode)
Recurisvely create directories to the given path.
Definition: os.c:1197
int run_command(char *const argv[], char *const envp[], process_callback_fn fn, void *ctx)
Executes a command.
Definition: os.c:365
char * get_secure_path(const UT_array *bin_path_arr, const char *filename, bool real)
Get the secure path string of a binary.
Definition: os.c:714
int is_proc_running(const char *name)
Check if a process is running.
Definition: os.c:762
int edge_os_get_time(struct os_time *t)
Get current time (sec, usec) in seconds since UNIX epoch.
Definition: os.c:148
long os_time_t
Definition: os.h:91
ssize_t read_file(const char *path, uint8_t **out)
Read the entire file.
Definition: os.c:1396
ssize_t open_write_nonblock(const char *path, int *fd, const uint8_t *buffer, size_t length)
Opens a file for writing and write a a buffer in nonblocking mode.
Definition: os.c:1472
int os_get_random_number_s(unsigned char *buf, size_t len)
Get a random number string.
Definition: os.c:262
int get_commands_paths(const char *commands[], const UT_array *bin_path_arr, hmap_str_keychar **hmap_bin_paths)
Returns the absolute path of system binaries.
Definition: os.c:1500
pid_t is_proc_app(const char *path, const char *proc_name)
Check if the given process's basename matches proc_name.
Definition: os.c:875
#define MAX_RANDOM_UUID_LEN
Definition: os.h:31
int os_get_random_int_range(int low, int up)
Return a random int from a give range.
Definition: os.c:253
int edge_os_get_reltime(struct os_reltime *t)
Get relative time (sec, usec)
Definition: os.c:157
bool signal_process(const char *proc_name, int sig)
Signal a process.
Definition: os.c:947
bool(* list_dir_fn)(char *, void *args)
Definition: os.h:438
int create_pid_file(const char *pid_file, int flags)
Open/create the file named in 'pidFile', lock it, optionally set the close-on-exec flag for the file ...
Definition: os.c:1320
int8_t hex2num(char c)
Hex char to number.
Definition: os.c:138
int proc_running
Definition: os.h:87
const char * proc_name
Definition: os.h:88
keyd array hasmap structure definition
Definition: hashmap.h:23
os_time_t usec
Definition: os.h:100
os_time_t sec
Definition: os.h:99
os_time_t usec
Definition: os.h:95
os_time_t sec
Definition: os.h:94