utils: Add datum_reexec

This commit is contained in:
Luke Dashjr 2024-12-13 22:25:23 +00:00
parent 0b3ea0f6bc
commit befc30777c
No known key found for this signature in database
GPG Key ID: A291A2C45D0C504A
2 changed files with 28 additions and 0 deletions

View File

@ -34,6 +34,9 @@
*/
#include <assert.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <sodium.h>
#include <stdio.h>
#include <stdarg.h>
@ -49,6 +52,8 @@
#include <sys/time.h>
#include <inttypes.h>
#include "datum_gateway.h"
#include "datum_logger.h"
#include "datum_utils.h"
#include "thirdparty_base58.h"
#include "thirdparty_segwit_addr.h"
@ -759,6 +764,28 @@ char **datum_deepcopy_charpp(const char * const * const p) {
return out;
}
void datum_reexec() {
// FIXME: kill other threads (except logging?) before closing fds
DIR * const D = opendir("/proc/self/fd");
if (D) {
for (struct dirent *ent; (ent = readdir(D)) != NULL; ) {
const int fd = datum_atoi_strict(ent->d_name, strlen(ent->d_name));
if (fd < 3) continue;
fcntl(fd, F_SETFD, FD_CLOEXEC);
}
closedir(D);
} else {
DLOG_ERROR("%s: Failed to close files, this could cause issues! (Is /proc mounted?)", __func__);
}
execv((void*)datum_argv[0], (void*)datum_argv);
// execv shouldn't return!
DLOG_FATAL("Failed to restart! We're too deep in to recover!");
abort();
}
bool datum_secure_strequals(const char *secret, const size_t secret_len, const char *guess) {
const size_t guess_len = strlen(guess);
size_t acc = secret_len ^ guess_len;

View File

@ -73,6 +73,7 @@ uint64_t datum_atoi_strict_u64(const char *s, size_t size);
int datum_atoi_strict(const char *s, size_t size);
bool datum_str_to_bool_strict(const char *s, bool *out);
char **datum_deepcopy_charpp(const char * const *p);
void datum_reexec();
bool datum_secure_strequals(const char *secret, const size_t secret_len, const char *guess);