mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager
synced 2024-10-06 16:21:50 +00:00
systemd: merge branch systemd into main
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1411
This commit is contained in:
commit
ddcf379c1a
|
@ -84,3 +84,14 @@ static const char * const dhcp6_message_status_table[_DHCP6_STATUS_MAX] = {
|
|||
};
|
||||
|
||||
DEFINE_STRING_TABLE_LOOKUP(dhcp6_message_status, DHCP6Status);
|
||||
|
||||
int dhcp6_message_status_to_errno(DHCP6Status s) {
|
||||
switch (s) {
|
||||
case DHCP6_STATUS_SUCCESS:
|
||||
return 0;
|
||||
case DHCP6_STATUS_NO_BINDING:
|
||||
return -EADDRNOTAVAIL;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -154,3 +154,4 @@ const char *dhcp6_message_type_to_string(DHCP6MessageType s) _const_;
|
|||
DHCP6MessageType dhcp6_message_type_from_string(const char *s) _pure_;
|
||||
const char *dhcp6_message_status_to_string(DHCP6Status s) _const_;
|
||||
DHCP6Status dhcp6_message_status_from_string(const char *s) _pure_;
|
||||
int dhcp6_message_status_to_errno(DHCP6Status s);
|
||||
|
|
|
@ -213,7 +213,7 @@ static int dhcp6_client_set_duid_internal(
|
|||
if (r < 0)
|
||||
return log_dhcp6_client_errno(client, r, "Failed to validate length of DUID: %m");
|
||||
|
||||
log_dhcp6_client(client, "Using DUID of type %u of incorrect length, proceeding.", duid_type);
|
||||
log_dhcp6_client(client, "Using DUID of type %i of incorrect length, proceeding.", duid_type);
|
||||
}
|
||||
|
||||
client->duid.type = htobe16(duid_type);
|
||||
|
@ -553,13 +553,9 @@ static void client_notify(sd_dhcp6_client *client, int event) {
|
|||
client->callback(client, event, client->userdata);
|
||||
}
|
||||
|
||||
static void client_stop(sd_dhcp6_client *client, int error) {
|
||||
DHCP6_CLIENT_DONT_DESTROY(client);
|
||||
|
||||
static void client_cleanup(sd_dhcp6_client *client) {
|
||||
assert(client);
|
||||
|
||||
client_notify(client, error);
|
||||
|
||||
client->lease = sd_dhcp6_lease_unref(client->lease);
|
||||
|
||||
/* Reset IRT here. Otherwise, we cannot restart the client in the information requesting mode,
|
||||
|
@ -576,6 +572,16 @@ static void client_stop(sd_dhcp6_client *client, int error) {
|
|||
client_set_state(client, DHCP6_STATE_STOPPED);
|
||||
}
|
||||
|
||||
static void client_stop(sd_dhcp6_client *client, int error) {
|
||||
DHCP6_CLIENT_DONT_DESTROY(client);
|
||||
|
||||
assert(client);
|
||||
|
||||
client_notify(client, error);
|
||||
|
||||
client_cleanup(client);
|
||||
}
|
||||
|
||||
static int client_append_common_options_in_managed_mode(
|
||||
sd_dhcp6_client *client,
|
||||
uint8_t **opt,
|
||||
|
@ -694,6 +700,9 @@ static int client_append_oro(sd_dhcp6_client *client, uint8_t **opt, size_t *opt
|
|||
req_opts = client->req_opts;
|
||||
}
|
||||
|
||||
if (n == 0)
|
||||
return 0;
|
||||
|
||||
return dhcp6_option_append(opt, optlen, SD_DHCP6_OPTION_ORO, n * sizeof(be16_t), req_opts);
|
||||
}
|
||||
|
||||
|
@ -1143,6 +1152,20 @@ static int client_process_reply(
|
|||
return log_invalid_message_type(client, message);
|
||||
|
||||
r = dhcp6_lease_new_from_message(client, message, len, timestamp, server_address, &lease);
|
||||
if (r == -EADDRNOTAVAIL) {
|
||||
|
||||
/* If NoBinding status code is received, we cannot request the address anymore.
|
||||
* Let's restart transaction from the beginning. */
|
||||
|
||||
if (client->state == DHCP6_STATE_REQUEST)
|
||||
/* The lease is not acquired yet, hence it is not necessary to notify the restart. */
|
||||
client_cleanup(client);
|
||||
else
|
||||
/* We need to notify the previous lease was expired. */
|
||||
client_stop(client, SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE);
|
||||
|
||||
return client_start_transaction(client, DHCP6_STATE_SOLICITATION);
|
||||
}
|
||||
if (r < 0)
|
||||
return log_dhcp6_client_errno(client, r, "Failed to process received reply message, ignoring: %m");
|
||||
|
||||
|
|
|
@ -514,7 +514,7 @@ static int dhcp6_lease_parse_message(
|
|||
return log_dhcp6_client_errno(client, r, "Failed to parse status code: %m");
|
||||
|
||||
if (r > 0)
|
||||
return log_dhcp6_client_errno(client, SYNTHETIC_ERRNO(EINVAL),
|
||||
return log_dhcp6_client_errno(client, dhcp6_message_status_to_errno(r),
|
||||
"Received %s message with non-zero status: %s%s%s",
|
||||
dhcp6_message_type_to_string(message->type),
|
||||
strempty(msg), isempty(msg) ? "" : ": ",
|
||||
|
|
|
@ -194,11 +194,10 @@ static int lldp_rx_handle_datagram(sd_lldp_rx *lldp_rx, sd_lldp_neighbor *n) {
|
|||
static int lldp_rx_receive_datagram(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
|
||||
_cleanup_(sd_lldp_neighbor_unrefp) sd_lldp_neighbor *n = NULL;
|
||||
ssize_t space, length;
|
||||
sd_lldp_rx *lldp_rx = userdata;
|
||||
sd_lldp_rx *lldp_rx = ASSERT_PTR(userdata);
|
||||
struct timespec ts;
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(lldp_rx);
|
||||
|
||||
space = next_datagram_size_fd(fd);
|
||||
if (space < 0) {
|
||||
|
|
|
@ -99,6 +99,7 @@ struct sd_event_source {
|
|||
sd_event_signal_handler_t callback;
|
||||
struct signalfd_siginfo siginfo;
|
||||
int sig;
|
||||
bool unblock;
|
||||
} signal;
|
||||
struct {
|
||||
sd_event_child_handler_t callback;
|
||||
|
|
|
@ -155,6 +155,8 @@ struct sd_event {
|
|||
|
||||
LIST_HEAD(sd_event_source, sources);
|
||||
|
||||
sd_event_source *sigint_event_source, *sigterm_event_source;
|
||||
|
||||
usec_t last_run_usec, last_log_usec;
|
||||
unsigned delays[sizeof(usec_t) * 8];
|
||||
};
|
||||
|
@ -325,6 +327,9 @@ static sd_event *event_free(sd_event *e) {
|
|||
|
||||
assert(e);
|
||||
|
||||
e->sigterm_event_source = sd_event_source_unref(e->sigterm_event_source);
|
||||
e->sigint_event_source = sd_event_source_unref(e->sigint_event_source);
|
||||
|
||||
while ((s = e->sources)) {
|
||||
assert(s->floating);
|
||||
source_disconnect(s);
|
||||
|
@ -815,6 +820,7 @@ static void event_source_time_prioq_remove(
|
|||
|
||||
static void source_disconnect(sd_event_source *s) {
|
||||
sd_event *event;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
|
||||
|
@ -855,6 +861,20 @@ static void source_disconnect(sd_event_source *s) {
|
|||
s->event->signal_sources[s->signal.sig] = NULL;
|
||||
|
||||
event_gc_signal_data(s->event, &s->priority, s->signal.sig);
|
||||
|
||||
if (s->signal.unblock) {
|
||||
sigset_t new_ss;
|
||||
|
||||
if (sigemptyset(&new_ss) < 0)
|
||||
log_debug_errno(errno, "Failed to reset signal set, ignoring: %m");
|
||||
else if (sigaddset(&new_ss, s->signal.sig) < 0)
|
||||
log_debug_errno(errno, "Failed to add signal %i to signal mask, ignoring: %m", s->signal.sig);
|
||||
else {
|
||||
r = pthread_sigmask(SIG_UNBLOCK, &new_ss, NULL);
|
||||
if (r != 0)
|
||||
log_debug_errno(r, "Failed to unblock signal %i, ignoring: %m", s->signal.sig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -1331,23 +1351,38 @@ _public_ int sd_event_add_signal(
|
|||
|
||||
_cleanup_(source_freep) sd_event_source *s = NULL;
|
||||
struct signal_data *d;
|
||||
sigset_t new_ss;
|
||||
bool block_it;
|
||||
int r;
|
||||
|
||||
assert_return(e, -EINVAL);
|
||||
assert_return(e = event_resolve(e), -ENOPKG);
|
||||
assert_return(SIGNAL_VALID(sig), -EINVAL);
|
||||
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
|
||||
assert_return(!event_pid_changed(e), -ECHILD);
|
||||
|
||||
/* Let's make sure our special flag stays outside of the valid signal range */
|
||||
assert_cc(_NSIG < SD_EVENT_SIGNAL_PROCMASK);
|
||||
|
||||
if (sig & SD_EVENT_SIGNAL_PROCMASK) {
|
||||
sig &= ~SD_EVENT_SIGNAL_PROCMASK;
|
||||
assert_return(SIGNAL_VALID(sig), -EINVAL);
|
||||
|
||||
block_it = true;
|
||||
} else {
|
||||
assert_return(SIGNAL_VALID(sig), -EINVAL);
|
||||
|
||||
r = signal_is_blocked(sig);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
return -EBUSY;
|
||||
|
||||
block_it = false;
|
||||
}
|
||||
|
||||
if (!callback)
|
||||
callback = signal_exit_callback;
|
||||
|
||||
r = signal_is_blocked(sig);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
return -EBUSY;
|
||||
|
||||
if (!e->signal_sources) {
|
||||
e->signal_sources = new0(sd_event_source*, _NSIG);
|
||||
if (!e->signal_sources)
|
||||
|
@ -1366,9 +1401,34 @@ _public_ int sd_event_add_signal(
|
|||
|
||||
e->signal_sources[sig] = s;
|
||||
|
||||
if (block_it) {
|
||||
sigset_t old_ss;
|
||||
|
||||
if (sigemptyset(&new_ss) < 0)
|
||||
return -errno;
|
||||
|
||||
if (sigaddset(&new_ss, sig) < 0)
|
||||
return -errno;
|
||||
|
||||
r = pthread_sigmask(SIG_BLOCK, &new_ss, &old_ss);
|
||||
if (r != 0)
|
||||
return -r;
|
||||
|
||||
r = sigismember(&old_ss, sig);
|
||||
if (r < 0)
|
||||
return -errno;
|
||||
|
||||
s->signal.unblock = !r;
|
||||
} else
|
||||
s->signal.unblock = false;
|
||||
|
||||
r = event_make_signal_data(e, sig, &d);
|
||||
if (r < 0)
|
||||
if (r < 0) {
|
||||
if (s->signal.unblock)
|
||||
(void) pthread_sigmask(SIG_UNBLOCK, &new_ss, NULL);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/* Use the signal name as description for the event source by default */
|
||||
(void) sd_event_source_set_description(s, signal_to_string(sig));
|
||||
|
@ -3827,7 +3887,7 @@ static void event_close_inode_data_fds(sd_event *e) {
|
|||
|
||||
/* Close the fds pointing to the inodes to watch now. We need to close them as they might otherwise pin
|
||||
* filesystems. But we can't close them right-away as we need them as long as the user still wants to make
|
||||
* adjustments to the even source, such as changing the priority (which requires us to remove and re-add a watch
|
||||
* adjustments to the event source, such as changing the priority (which requires us to remove and re-add a watch
|
||||
* for the inode). Hence, let's close them when entering the first iteration after they were added, as a
|
||||
* compromise. */
|
||||
|
||||
|
@ -4538,8 +4598,8 @@ _public_ int sd_event_source_set_ratelimit_expire_callback(sd_event_source *s, s
|
|||
_public_ int sd_event_source_get_ratelimit(sd_event_source *s, uint64_t *ret_interval, unsigned *ret_burst) {
|
||||
assert_return(s, -EINVAL);
|
||||
|
||||
/* Querying whether an event source has ratelimiting configured is not a loggable offsense, hence
|
||||
* don't use assert_return(). Unlike turning on ratelimiting it's not really a programming error */
|
||||
/* Querying whether an event source has ratelimiting configured is not a loggable offense, hence
|
||||
* don't use assert_return(). Unlike turning on ratelimiting it's not really a programming error. */
|
||||
if (!EVENT_SOURCE_CAN_RATE_LIMIT(s->type))
|
||||
return -EDOM;
|
||||
|
||||
|
@ -4565,4 +4625,56 @@ _public_ int sd_event_source_is_ratelimited(sd_event_source *s) {
|
|||
|
||||
return s->ratelimited;
|
||||
}
|
||||
|
||||
_public_ int sd_event_set_signal_exit(sd_event *e, int b) {
|
||||
bool change = false;
|
||||
int r;
|
||||
|
||||
assert_return(e, -EINVAL);
|
||||
|
||||
if (b) {
|
||||
/* We want to maintain pointers to these event sources, so that we can destroy them when told
|
||||
* so. But we also don't want them to pin the event loop itself. Hence we mark them as
|
||||
* floating after creation (and undo this before deleting them again). */
|
||||
|
||||
if (!e->sigint_event_source) {
|
||||
r = sd_event_add_signal(e, &e->sigint_event_source, SIGINT | SD_EVENT_SIGNAL_PROCMASK, NULL, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
assert(sd_event_source_set_floating(e->sigint_event_source, true) >= 0);
|
||||
change = true;
|
||||
}
|
||||
|
||||
if (!e->sigterm_event_source) {
|
||||
r = sd_event_add_signal(e, &e->sigterm_event_source, SIGTERM | SD_EVENT_SIGNAL_PROCMASK, NULL, NULL);
|
||||
if (r < 0) {
|
||||
if (change) {
|
||||
assert(sd_event_source_set_floating(e->sigint_event_source, false) >= 0);
|
||||
e->sigint_event_source = sd_event_source_unref(e->sigint_event_source);
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
assert(sd_event_source_set_floating(e->sigterm_event_source, true) >= 0);
|
||||
change = true;
|
||||
}
|
||||
|
||||
} else {
|
||||
if (e->sigint_event_source) {
|
||||
assert(sd_event_source_set_floating(e->sigint_event_source, false) >= 0);
|
||||
e->sigint_event_source = sd_event_source_unref(e->sigint_event_source);
|
||||
change = true;
|
||||
}
|
||||
|
||||
if (e->sigterm_event_source) {
|
||||
assert(sd_event_source_set_floating(e->sigterm_event_source, false) >= 0);
|
||||
e->sigterm_event_source = sd_event_source_unref(e->sigterm_event_source);
|
||||
change = true;
|
||||
}
|
||||
}
|
||||
|
||||
return change;
|
||||
}
|
||||
#endif /* NM_IGNORED */
|
||||
|
|
|
@ -68,6 +68,8 @@ enum {
|
|||
SD_EVENT_PRIORITY_IDLE = 100
|
||||
};
|
||||
|
||||
#define SD_EVENT_SIGNAL_PROCMASK (1 << 30)
|
||||
|
||||
typedef int (*sd_event_handler_t)(sd_event_source *s, void *userdata);
|
||||
typedef int (*sd_event_io_handler_t)(sd_event_source *s, int fd, uint32_t revents, void *userdata);
|
||||
typedef int (*sd_event_time_handler_t)(sd_event_source *s, uint64_t usec, void *userdata);
|
||||
|
@ -114,6 +116,7 @@ int sd_event_get_exit_code(sd_event *e, int *code);
|
|||
int sd_event_set_watchdog(sd_event *e, int b);
|
||||
int sd_event_get_watchdog(sd_event *e);
|
||||
int sd_event_get_iteration(sd_event *e, uint64_t *ret);
|
||||
int sd_event_set_signal_exit(sd_event *e, int b);
|
||||
|
||||
sd_event_source* sd_event_source_ref(sd_event_source *s);
|
||||
sd_event_source* sd_event_source_unref(sd_event_source *s);
|
||||
|
|
|
@ -86,6 +86,7 @@ bool cpu_accounting_is_cheap(void);
|
|||
|
||||
/* Special values for all weight knobs on unified hierarchy */
|
||||
#define CGROUP_WEIGHT_INVALID UINT64_MAX
|
||||
#define CGROUP_WEIGHT_IDLE UINT64_C(0)
|
||||
#define CGROUP_WEIGHT_MIN UINT64_C(1)
|
||||
#define CGROUP_WEIGHT_MAX UINT64_C(10000)
|
||||
#define CGROUP_WEIGHT_DEFAULT UINT64_C(100)
|
||||
|
|
|
@ -446,11 +446,9 @@ static int merge_env_file_push(
|
|||
const char *key, char *value,
|
||||
void *userdata) {
|
||||
|
||||
char ***env = userdata;
|
||||
char ***env = ASSERT_PTR(userdata);
|
||||
char *expanded_value;
|
||||
|
||||
assert(env);
|
||||
|
||||
if (!value) {
|
||||
log_error("%s:%u: invalid syntax (around \"%s\"), ignoring.", strna(filename), line, key);
|
||||
return 0;
|
||||
|
|
|
@ -781,6 +781,18 @@ int getenv_bool_secure(const char *p) {
|
|||
}
|
||||
|
||||
#if 0 /* NM_IGNORED */
|
||||
int getenv_uint64_secure(const char *p, uint64_t *ret) {
|
||||
const char *e;
|
||||
|
||||
assert(p);
|
||||
|
||||
e = secure_getenv(p);
|
||||
if (!e)
|
||||
return -ENXIO;
|
||||
|
||||
return safe_atou64(e, ret);
|
||||
}
|
||||
|
||||
int set_unset_env(const char *name, const char *value, bool overwrite) {
|
||||
assert(name);
|
||||
|
||||
|
|
|
@ -57,6 +57,8 @@ char *strv_env_pairs_get(char **l, const char *name) _pure_;
|
|||
int getenv_bool(const char *p);
|
||||
int getenv_bool_secure(const char *p);
|
||||
|
||||
int getenv_uint64_secure(const char *p, uint64_t *ret);
|
||||
|
||||
/* Like setenv, but calls unsetenv if value == NULL. */
|
||||
int set_unset_env(const char *name, const char *value, bool overwrite);
|
||||
|
||||
|
|
|
@ -661,7 +661,7 @@ int rearrange_stdio(int original_input_fd, int original_output_fd, int original_
|
|||
goto finish;
|
||||
}
|
||||
|
||||
CLOSE_AND_REPLACE(null_fd, copy);
|
||||
close_and_replace(null_fd, copy);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -98,7 +98,7 @@ static inline int make_null_stdio(void) {
|
|||
})
|
||||
|
||||
/* Like free_and_replace(), but for file descriptors */
|
||||
#define CLOSE_AND_REPLACE(a, b) \
|
||||
#define close_and_replace(a, b) \
|
||||
({ \
|
||||
int *_fdp_ = &(a); \
|
||||
safe_close(*_fdp_); \
|
||||
|
|
|
@ -403,10 +403,6 @@ int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gi
|
|||
return ret;
|
||||
}
|
||||
|
||||
int touch(const char *path) {
|
||||
return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
|
||||
}
|
||||
|
||||
int symlink_idempotent(const char *from, const char *to, bool make_relative) {
|
||||
_cleanup_free_ char *relpath = NULL;
|
||||
int r;
|
||||
|
@ -415,13 +411,7 @@ int symlink_idempotent(const char *from, const char *to, bool make_relative) {
|
|||
assert(to);
|
||||
|
||||
if (make_relative) {
|
||||
_cleanup_free_ char *parent = NULL;
|
||||
|
||||
r = path_extract_directory(to, &parent);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = path_make_relative(parent, from, &relpath);
|
||||
r = path_make_relative_parent(to, from, &relpath);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
@ -447,29 +437,38 @@ int symlink_idempotent(const char *from, const char *to, bool make_relative) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int symlink_atomic(const char *from, const char *to) {
|
||||
_cleanup_free_ char *t = NULL;
|
||||
int symlinkat_atomic_full(const char *from, int atfd, const char *to, bool make_relative) {
|
||||
_cleanup_free_ char *relpath = NULL, *t = NULL;
|
||||
int r;
|
||||
|
||||
assert(from);
|
||||
assert(to);
|
||||
|
||||
if (make_relative) {
|
||||
r = path_make_relative_parent(to, from, &relpath);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
from = relpath;
|
||||
}
|
||||
|
||||
r = tempfn_random(to, NULL, &t);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (symlink(from, t) < 0)
|
||||
if (symlinkat(from, atfd, t) < 0)
|
||||
return -errno;
|
||||
|
||||
if (rename(t, to) < 0) {
|
||||
unlink_noerrno(t);
|
||||
return -errno;
|
||||
r = RET_NERRNO(renameat(atfd, t, atfd, to));
|
||||
if (r < 0) {
|
||||
(void) unlinkat(atfd, t, 0);
|
||||
return r;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
|
||||
int mknodat_atomic(int atfd, const char *path, mode_t mode, dev_t dev) {
|
||||
_cleanup_free_ char *t = NULL;
|
||||
int r;
|
||||
|
||||
|
@ -479,58 +478,36 @@ int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
|
|||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (mknod(t, mode, dev) < 0)
|
||||
if (mknodat(atfd, t, mode, dev) < 0)
|
||||
return -errno;
|
||||
|
||||
if (rename(t, path) < 0) {
|
||||
unlink_noerrno(t);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mkfifo_atomic(const char *path, mode_t mode) {
|
||||
_cleanup_free_ char *t = NULL;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
|
||||
r = tempfn_random(path, NULL, &t);
|
||||
if (r < 0)
|
||||
r = RET_NERRNO(renameat(atfd, t, atfd, path));
|
||||
if (r < 0) {
|
||||
(void) unlinkat(atfd, t, 0);
|
||||
return r;
|
||||
|
||||
if (mkfifo(t, mode) < 0)
|
||||
return -errno;
|
||||
|
||||
if (rename(t, path) < 0) {
|
||||
unlink_noerrno(t);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mkfifoat_atomic(int dirfd, const char *path, mode_t mode) {
|
||||
int mkfifoat_atomic(int atfd, const char *path, mode_t mode) {
|
||||
_cleanup_free_ char *t = NULL;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
|
||||
if (path_is_absolute(path))
|
||||
return mkfifo_atomic(path, mode);
|
||||
|
||||
/* We're only interested in the (random) filename. */
|
||||
r = tempfn_random_child("", NULL, &t);
|
||||
r = tempfn_random(path, NULL, &t);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (mkfifoat(dirfd, t, mode) < 0)
|
||||
if (mkfifoat(atfd, t, mode) < 0)
|
||||
return -errno;
|
||||
|
||||
if (renameat(dirfd, t, dirfd, path) < 0) {
|
||||
unlink_noerrno(t);
|
||||
return -errno;
|
||||
r = RET_NERRNO(renameat(atfd, t, atfd, path));
|
||||
if (r < 0) {
|
||||
(void) unlinkat(atfd, t, 0);
|
||||
return r;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include "alloc-util.h"
|
||||
#include "errno-util.h"
|
||||
#include "time-util.h"
|
||||
#include "user-util.h"
|
||||
|
||||
#define MODE_INVALID ((mode_t) -1)
|
||||
|
||||
|
@ -50,14 +51,27 @@ int stat_warn_permissions(const char *path, const struct stat *st);
|
|||
RET_NERRNO(faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW))
|
||||
|
||||
int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
|
||||
int touch(const char *path);
|
||||
|
||||
static inline int touch(const char *path) {
|
||||
return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID);
|
||||
}
|
||||
|
||||
int symlink_idempotent(const char *from, const char *to, bool make_relative);
|
||||
|
||||
int symlink_atomic(const char *from, const char *to);
|
||||
int mknod_atomic(const char *path, mode_t mode, dev_t dev);
|
||||
int mkfifo_atomic(const char *path, mode_t mode);
|
||||
int symlinkat_atomic_full(const char *from, int atfd, const char *to, bool make_relative);
|
||||
static inline int symlink_atomic(const char *from, const char *to) {
|
||||
return symlinkat_atomic_full(from, AT_FDCWD, to, false);
|
||||
}
|
||||
|
||||
int mknodat_atomic(int atfd, const char *path, mode_t mode, dev_t dev);
|
||||
static inline int mknod_atomic(const char *path, mode_t mode, dev_t dev) {
|
||||
return mknodat_atomic(AT_FDCWD, path, mode, dev);
|
||||
}
|
||||
|
||||
int mkfifoat_atomic(int dir_fd, const char *path, mode_t mode);
|
||||
static inline int mkfifo_atomic(const char *path, mode_t mode) {
|
||||
return mkfifoat_atomic(AT_FDCWD, path, mode);
|
||||
}
|
||||
|
||||
int get_files_in_directory(const char *path, char ***list);
|
||||
|
||||
|
|
|
@ -55,6 +55,7 @@ const char *special_glyph(SpecialGlyph code) {
|
|||
[SPECIAL_GLYPH_LIGHT_SHADE] = "-",
|
||||
[SPECIAL_GLYPH_DARK_SHADE] = "X",
|
||||
[SPECIAL_GLYPH_SIGMA] = "S",
|
||||
[SPECIAL_GLYPH_ARROW_LEFT] = "<-",
|
||||
[SPECIAL_GLYPH_ARROW_RIGHT] = "->",
|
||||
[SPECIAL_GLYPH_ARROW_UP] = "^",
|
||||
[SPECIAL_GLYPH_ARROW_DOWN] = "v",
|
||||
|
@ -101,6 +102,7 @@ const char *special_glyph(SpecialGlyph code) {
|
|||
[SPECIAL_GLYPH_ARROW_DOWN] = u8"↓", /* actually called: DOWNWARDS ARROW */
|
||||
|
||||
/* Single glyph in Unicode, two in ASCII */
|
||||
[SPECIAL_GLYPH_ARROW_LEFT] = u8"←", /* actually called: LEFTWARDS ARROW */
|
||||
[SPECIAL_GLYPH_ARROW_RIGHT] = u8"→", /* actually called: RIGHTWARDS ARROW */
|
||||
|
||||
/* Single glyph in Unicode, three in ASCII */
|
||||
|
|
|
@ -22,6 +22,7 @@ typedef enum SpecialGlyph {
|
|||
SPECIAL_GLYPH_MU,
|
||||
SPECIAL_GLYPH_CHECK_MARK,
|
||||
SPECIAL_GLYPH_CROSS_MARK,
|
||||
SPECIAL_GLYPH_ARROW_LEFT,
|
||||
SPECIAL_GLYPH_ARROW_RIGHT,
|
||||
SPECIAL_GLYPH_ARROW_UP,
|
||||
SPECIAL_GLYPH_ARROW_DOWN,
|
||||
|
|
|
@ -1192,7 +1192,7 @@ static int resize_buckets(HashmapBase *h, unsigned entries_add) {
|
|||
} while (rehash_next);
|
||||
}
|
||||
|
||||
assert(n_rehashed == n_entries(h));
|
||||
assert_se(n_rehashed == n_entries(h));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
@ -1886,11 +1886,10 @@ int _set_put_strdupv_full(Set **s, const struct hash_ops *hash_ops, char **l HA
|
|||
}
|
||||
|
||||
int set_put_strsplit(Set *s, const char *v, const char *separators, ExtractFlags flags) {
|
||||
const char *p = v;
|
||||
const char *p = ASSERT_PTR(v);
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
assert(v);
|
||||
|
||||
for (;;) {
|
||||
char *word;
|
||||
|
@ -2083,6 +2082,8 @@ static bool set_fnmatch_one(Set *patterns, const char *needle) {
|
|||
|
||||
assert(needle);
|
||||
|
||||
/* Any failure of fnmatch() is treated as equivalent to FNM_NOMATCH, i.e. as non-matching pattern */
|
||||
|
||||
SET_FOREACH(p, patterns)
|
||||
if (fnmatch(p, needle, 0) == 0)
|
||||
return true;
|
||||
|
|
|
@ -594,6 +594,7 @@ unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr *addr) {
|
|||
return 32U - u32ctz(be32toh(addr->s_addr));
|
||||
}
|
||||
|
||||
/* Calculate an IPv4 netmask from prefix length, for example /8 -> 255.0.0.0. */
|
||||
struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen) {
|
||||
assert(addr);
|
||||
assert(prefixlen <= 32);
|
||||
|
@ -607,6 +608,47 @@ struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned cha
|
|||
return addr;
|
||||
}
|
||||
|
||||
/* Calculate an IPv6 netmask from prefix length, for example /16 -> ffff::. */
|
||||
struct in6_addr* in6_addr_prefixlen_to_netmask(struct in6_addr *addr, unsigned char prefixlen) {
|
||||
assert(addr);
|
||||
assert(prefixlen <= 128);
|
||||
|
||||
for (unsigned i = 0; i < 16; i++) {
|
||||
uint8_t mask;
|
||||
|
||||
if (prefixlen >= 8) {
|
||||
mask = 0xFF;
|
||||
prefixlen -= 8;
|
||||
} else if (prefixlen > 0) {
|
||||
mask = 0xFF << (8 - prefixlen);
|
||||
prefixlen = 0;
|
||||
} else {
|
||||
assert(prefixlen == 0);
|
||||
mask = 0;
|
||||
}
|
||||
|
||||
addr->s6_addr[i] = mask;
|
||||
}
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
/* Calculate an IPv4 or IPv6 netmask from prefix length, for example /8 -> 255.0.0.0 or /16 -> ffff::. */
|
||||
int in_addr_prefixlen_to_netmask(int family, union in_addr_union *addr, unsigned char prefixlen) {
|
||||
assert(addr);
|
||||
|
||||
switch (family) {
|
||||
case AF_INET:
|
||||
in4_addr_prefixlen_to_netmask(&addr->in, prefixlen);
|
||||
return 0;
|
||||
case AF_INET6:
|
||||
in6_addr_prefixlen_to_netmask(&addr->in6, prefixlen);
|
||||
return 0;
|
||||
default:
|
||||
return -EAFNOSUPPORT;
|
||||
}
|
||||
}
|
||||
|
||||
int in4_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen) {
|
||||
uint8_t msb_octet = *(uint8_t*) addr;
|
||||
|
||||
|
|
|
@ -138,6 +138,8 @@ int in_addr_from_string_auto(const char *s, int *ret_family, union in_addr_union
|
|||
|
||||
unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr *addr);
|
||||
struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen);
|
||||
struct in6_addr* in6_addr_prefixlen_to_netmask(struct in6_addr *addr, unsigned char prefixlen);
|
||||
int in_addr_prefixlen_to_netmask(int family, union in_addr_union *addr, unsigned char prefixlen);
|
||||
int in4_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen);
|
||||
int in4_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask);
|
||||
int in4_addr_mask(struct in_addr *addr, unsigned char prefixlen);
|
||||
|
|
|
@ -53,11 +53,10 @@ int flush_fd(int fd) {
|
|||
#endif /* NM_IGNORED */
|
||||
|
||||
ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
|
||||
uint8_t *p = buf;
|
||||
uint8_t *p = ASSERT_PTR(buf);
|
||||
ssize_t n = 0;
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(buf);
|
||||
|
||||
/* If called with nbytes == 0, let's call read() at least
|
||||
* once, to validate the operation */
|
||||
|
@ -113,10 +112,9 @@ int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
|
|||
|
||||
#if 0 /* NM_IGNORED */
|
||||
int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
|
||||
const uint8_t *p = buf;
|
||||
const uint8_t *p = ASSERT_PTR(buf);
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(buf);
|
||||
|
||||
if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
|
||||
return -EINVAL;
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <syslog.h>
|
||||
|
||||
#include "macro.h"
|
||||
#include "ratelimit.h"
|
||||
|
||||
/* Some structures we reference but don't want to pull in headers for */
|
||||
struct iovec;
|
||||
|
@ -508,3 +509,41 @@ int log_syntax_invalid_utf8_internal(
|
|||
#define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG)
|
||||
|
||||
void log_setup(void);
|
||||
|
||||
typedef struct LogRateLimit {
|
||||
int error;
|
||||
int level;
|
||||
RateLimit ratelimit;
|
||||
} LogRateLimit;
|
||||
|
||||
#define log_ratelimit_internal(_level, _error, _format, _file, _line, _func, ...) \
|
||||
({ \
|
||||
int _log_ratelimit_error = (_error); \
|
||||
int _log_ratelimit_level = (_level); \
|
||||
static LogRateLimit _log_ratelimit = { \
|
||||
.ratelimit = { \
|
||||
.interval = 1 * USEC_PER_SEC, \
|
||||
.burst = 1, \
|
||||
}, \
|
||||
}; \
|
||||
unsigned _num_dropped_errors = ratelimit_num_dropped(&_log_ratelimit.ratelimit); \
|
||||
if (_log_ratelimit_error != _log_ratelimit.error || _log_ratelimit_level != _log_ratelimit.level) { \
|
||||
ratelimit_reset(&_log_ratelimit.ratelimit); \
|
||||
_log_ratelimit.error = _log_ratelimit_error; \
|
||||
_log_ratelimit.level = _log_ratelimit_level; \
|
||||
} \
|
||||
if (ratelimit_below(&_log_ratelimit.ratelimit)) \
|
||||
_log_ratelimit_error = _num_dropped_errors > 0 \
|
||||
? log_internal(_log_ratelimit_level, _log_ratelimit_error, _file, _line, _func, _format " (Dropped %u similar message(s))", __VA_ARGS__, _num_dropped_errors) \
|
||||
: log_internal(_log_ratelimit_level, _log_ratelimit_error, _file, _line, _func, _format, __VA_ARGS__); \
|
||||
_log_ratelimit_error; \
|
||||
})
|
||||
|
||||
#define log_ratelimit_full_errno(level, error, format, ...) \
|
||||
({ \
|
||||
int _level = (level), _e = (error); \
|
||||
_e = (log_get_max_level() >= LOG_PRI(_level)) \
|
||||
? log_ratelimit_internal(_level, _e, format, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
|
||||
: -ERRNO_VALUE(_e); \
|
||||
_e < 0 ? _e : -ESTRPIPE; \
|
||||
})
|
||||
|
|
|
@ -49,6 +49,8 @@ struct statx STATX_DEFINITION;
|
|||
/* Always define the newest version we are aware of as a distinct type, so that we can use it even if glibc
|
||||
* defines an older definition */
|
||||
struct new_statx STATX_DEFINITION;
|
||||
#else /* NM_IGNORED */
|
||||
struct new_statx;
|
||||
#endif /* NM_IGNORED */
|
||||
|
||||
/* a528d35e8bfcc521d7cb70aaf03e1bd296c8493f (4.11) */
|
||||
|
|
|
@ -482,69 +482,31 @@ int safe_atolli(const char *s, long long int *ret_lli) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int safe_atou8(const char *s, uint8_t *ret) {
|
||||
unsigned base = 0;
|
||||
unsigned long l;
|
||||
char *x = NULL;
|
||||
int safe_atou8_full(const char *s, unsigned base, uint8_t *ret) {
|
||||
unsigned u;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
|
||||
s += strspn(s, WHITESPACE);
|
||||
s = mangle_base(s, &base);
|
||||
|
||||
errno = 0;
|
||||
l = strtoul(s, &x, base);
|
||||
if (errno > 0)
|
||||
return -errno;
|
||||
if (!x || x == s || *x != 0)
|
||||
return -EINVAL;
|
||||
if (l != 0 && s[0] == '-')
|
||||
return -ERANGE;
|
||||
if ((unsigned long) (uint8_t) l != l)
|
||||
r = safe_atou_full(s, base, &u);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (u > UINT8_MAX)
|
||||
return -ERANGE;
|
||||
|
||||
if (ret)
|
||||
*ret = (uint8_t) l;
|
||||
*ret = (uint8_t) u;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
|
||||
char *x = NULL;
|
||||
unsigned long l;
|
||||
unsigned u;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
assert(SAFE_ATO_MASK_FLAGS(base) <= 16);
|
||||
|
||||
if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_WHITESPACE) &&
|
||||
strchr(WHITESPACE, s[0]))
|
||||
return -EINVAL;
|
||||
|
||||
s += strspn(s, WHITESPACE);
|
||||
|
||||
if (FLAGS_SET(base, SAFE_ATO_REFUSE_PLUS_MINUS) &&
|
||||
IN_SET(s[0], '+', '-'))
|
||||
return -EINVAL;
|
||||
|
||||
if (FLAGS_SET(base, SAFE_ATO_REFUSE_LEADING_ZERO) &&
|
||||
s[0] == '0' && s[1] != 0)
|
||||
return -EINVAL;
|
||||
|
||||
s = mangle_base(s, &base);
|
||||
|
||||
errno = 0;
|
||||
l = strtoul(s, &x, SAFE_ATO_MASK_FLAGS(base));
|
||||
if (errno > 0)
|
||||
return -errno;
|
||||
if (!x || x == s || *x != 0)
|
||||
return -EINVAL;
|
||||
if (l != 0 && s[0] == '-')
|
||||
return -ERANGE;
|
||||
if ((unsigned long) (uint16_t) l != l)
|
||||
r = safe_atou_full(s, base, &u);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (u > UINT16_MAX)
|
||||
return -ERANGE;
|
||||
|
||||
if (ret)
|
||||
*ret = (uint16_t) l;
|
||||
|
||||
*ret = (uint16_t) u;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,11 @@ static inline int safe_atou(const char *s, unsigned *ret_u) {
|
|||
int safe_atoi(const char *s, int *ret_i);
|
||||
int safe_atolli(const char *s, long long int *ret_i);
|
||||
|
||||
int safe_atou8(const char *s, uint8_t *ret);
|
||||
int safe_atou8_full(const char *s, unsigned base, uint8_t *ret);
|
||||
|
||||
static inline int safe_atou8(const char *s, uint8_t *ret) {
|
||||
return safe_atou8_full(s, 0, ret);
|
||||
}
|
||||
|
||||
int safe_atou16_full(const char *s, unsigned base, uint16_t *ret);
|
||||
|
||||
|
|
|
@ -3,22 +3,18 @@
|
|||
#include "nm-sd-adapt-shared.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fnmatch.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* When we include libgen.h because we need dirname() we immediately
|
||||
* undefine basename() since libgen.h defines it as a macro to the
|
||||
* POSIX version which is really broken. We prefer GNU basename(). */
|
||||
#include <libgen.h>
|
||||
#undef basename
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "chase-symlinks.h"
|
||||
#include "extract-word.h"
|
||||
#include "fd-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "glob-util.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "path-util.h"
|
||||
|
@ -200,6 +196,34 @@ int path_make_relative(const char *from, const char *to, char **ret) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int path_make_relative_parent(const char *from_child, const char *to, char **ret) {
|
||||
_cleanup_free_ char *from = NULL;
|
||||
int r;
|
||||
|
||||
assert(from_child);
|
||||
assert(to);
|
||||
assert(ret);
|
||||
|
||||
/* Similar to path_make_relative(), but provides the relative path from the parent directory of
|
||||
* 'from_child'. This may be useful when creating relative symlink.
|
||||
*
|
||||
* E.g.
|
||||
* - from = "/path/to/aaa", to = "/path/to/bbb"
|
||||
* path_make_relative(from, to) = "../bbb"
|
||||
* path_make_relative_parent(from, to) = "bbb"
|
||||
*
|
||||
* - from = "/path/to/aaa/bbb", to = "/path/to/ccc/ddd"
|
||||
* path_make_relative(from, to) = "../../ccc/ddd"
|
||||
* path_make_relative_parent(from, to) = "../ccc/ddd"
|
||||
*/
|
||||
|
||||
r = path_extract_directory(from_child, &from);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return path_make_relative(from, to, ret);
|
||||
}
|
||||
|
||||
char* path_startswith_strv(const char *p, char **set) {
|
||||
STRV_FOREACH(s, set) {
|
||||
char *t;
|
||||
|
@ -328,11 +352,9 @@ char **path_strv_resolve_uniq(char **l, const char *root) {
|
|||
|
||||
char *path_simplify(char *path) {
|
||||
bool add_slash = false;
|
||||
char *f = path;
|
||||
char *f = ASSERT_PTR(path);
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
|
||||
/* Removes redundant inner and trailing slashes. Also removes unnecessary dots.
|
||||
* Modifies the passed string in-place.
|
||||
*
|
||||
|
@ -765,38 +787,26 @@ static int executable_is_good(const char *executable) {
|
|||
"/dev/null");
|
||||
}
|
||||
|
||||
int fsck_exists(const char *fstype) {
|
||||
int fsck_exists(void) {
|
||||
return executable_is_good("fsck");
|
||||
}
|
||||
|
||||
int fsck_exists_for_fstype(const char *fstype) {
|
||||
const char *checker;
|
||||
int r;
|
||||
|
||||
assert(fstype);
|
||||
|
||||
if (streq(fstype, "auto"))
|
||||
return -EINVAL;
|
||||
|
||||
r = fsck_exists();
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
checker = strjoina("fsck.", fstype);
|
||||
return executable_is_good(checker);
|
||||
}
|
||||
|
||||
char* dirname_malloc(const char *path) {
|
||||
char *d, *dir, *dir2;
|
||||
|
||||
assert(path);
|
||||
|
||||
d = strdup(path);
|
||||
if (!d)
|
||||
return NULL;
|
||||
|
||||
dir = dirname(d);
|
||||
assert(dir);
|
||||
|
||||
if (dir == d)
|
||||
return d;
|
||||
|
||||
dir2 = strdup(dir);
|
||||
free(d);
|
||||
|
||||
return dir2;
|
||||
}
|
||||
#endif /* NM_IGNORED */
|
||||
|
||||
static const char *skip_slash_or_dot(const char *p) {
|
||||
|
@ -1319,4 +1329,67 @@ bool prefixed_path_strv_contains(char **l, const char *path) {
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
int path_glob_can_match(const char *pattern, const char *prefix, char **ret) {
|
||||
assert(pattern);
|
||||
assert(prefix);
|
||||
|
||||
for (const char *a = pattern, *b = prefix;;) {
|
||||
_cleanup_free_ char *g = NULL, *h = NULL;
|
||||
const char *p, *q;
|
||||
int r, s;
|
||||
|
||||
r = path_find_first_component(&a, /* accept_dot_dot = */ false, &p);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
s = path_find_first_component(&b, /* accept_dot_dot = */ false, &q);
|
||||
if (s < 0)
|
||||
return s;
|
||||
|
||||
if (s == 0) {
|
||||
/* The pattern matches the prefix. */
|
||||
if (ret) {
|
||||
char *t;
|
||||
|
||||
t = path_join(prefix, p);
|
||||
if (!t)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = t;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
if (r == s && strneq(p, q, r))
|
||||
continue; /* common component. Check next. */
|
||||
|
||||
g = strndup(p, r);
|
||||
if (!g)
|
||||
return -ENOMEM;
|
||||
|
||||
if (!string_is_glob(g))
|
||||
break;
|
||||
|
||||
/* We found a glob component. Check if the glob pattern matches the prefix component. */
|
||||
|
||||
h = strndup(q, s);
|
||||
if (!h)
|
||||
return -ENOMEM;
|
||||
|
||||
r = fnmatch(g, h, 0);
|
||||
if (r == FNM_NOMATCH)
|
||||
break;
|
||||
if (r != 0) /* Failure to process pattern? */
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* The pattern does not match the prefix. */
|
||||
if (ret)
|
||||
*ret = NULL;
|
||||
return false;
|
||||
}
|
||||
#endif /* NM_IGNORED */
|
||||
|
|
|
@ -63,6 +63,7 @@ char* path_make_absolute(const char *p, const char *prefix);
|
|||
int safe_getcwd(char **ret);
|
||||
int path_make_absolute_cwd(const char *p, char **ret);
|
||||
int path_make_relative(const char *from, const char *to, char **ret);
|
||||
int path_make_relative_parent(const char *from_child, const char *to, char **ret);
|
||||
char *path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) _pure_;
|
||||
static inline char* path_startswith(const char *path, const char *prefix) {
|
||||
return path_startswith_full(path, prefix, true);
|
||||
|
@ -104,7 +105,8 @@ static inline int find_executable(const char *name, char **ret_filename) {
|
|||
|
||||
bool paths_check_timestamp(const char* const* paths, usec_t *paths_ts_usec, bool update);
|
||||
|
||||
int fsck_exists(const char *fstype);
|
||||
int fsck_exists(void);
|
||||
int fsck_exists_for_fstype(const char *fstype);
|
||||
|
||||
/* Iterates through the path prefixes of the specified path, going up
|
||||
* the tree, to root. Also returns "" (and not "/"!) for the root
|
||||
|
@ -153,7 +155,6 @@ int fsck_exists(const char *fstype);
|
|||
_ret; \
|
||||
})
|
||||
|
||||
char* dirname_malloc(const char *path);
|
||||
int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret);
|
||||
int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret);
|
||||
const char *last_path_component(const char *path);
|
||||
|
@ -198,3 +199,5 @@ static inline const char *empty_to_root(const char *path) {
|
|||
|
||||
bool path_strv_contains(char **l, const char *path);
|
||||
bool prefixed_path_strv_contains(char **l, const char *path);
|
||||
|
||||
int path_glob_can_match(const char *pattern, const char *prefix, char **ret);
|
||||
|
|
|
@ -2,10 +2,6 @@
|
|||
|
||||
#include "nm-sd-adapt-shared.h"
|
||||
|
||||
#if defined(__i386__) || defined(__x86_64__)
|
||||
#include <cpuid.h>
|
||||
#endif
|
||||
|
||||
#include <elf.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
@ -35,11 +31,10 @@
|
|||
#include "sha256.h"
|
||||
#include "time-util.h"
|
||||
|
||||
/* This is a "best effort" kind of thing, but has no real security value.
|
||||
* So, this should only be used by random_bytes(), which is not meant for
|
||||
* crypto. This could be made better, but we're *not* trying to roll a
|
||||
* userspace prng here, or even have forward secrecy, but rather just do
|
||||
* the shortest thing that is at least better than libc rand(). */
|
||||
/* This is a "best effort" kind of thing, but has no real security value. So, this should only be used by
|
||||
* random_bytes(), which is not meant for crypto. This could be made better, but we're *not* trying to roll a
|
||||
* userspace prng here, or even have forward secrecy, but rather just do the shortest thing that is at least
|
||||
* better than libc rand(). */
|
||||
static void fallback_random_bytes(void *p, size_t n) {
|
||||
static thread_local uint64_t fallback_counter = 0;
|
||||
struct {
|
||||
|
@ -55,7 +50,7 @@ static void fallback_random_bytes(void *p, size_t n) {
|
|||
.stamp_mono = now(CLOCK_MONOTONIC),
|
||||
.stamp_real = now(CLOCK_REALTIME),
|
||||
.pid = getpid(),
|
||||
.tid = gettid()
|
||||
.tid = gettid(),
|
||||
};
|
||||
|
||||
#if HAVE_SYS_AUXV_H
|
||||
|
|
|
@ -32,9 +32,16 @@ bool ratelimit_below(RateLimit *r) {
|
|||
if (r->num < r->burst)
|
||||
goto good;
|
||||
|
||||
r->num++;
|
||||
return false;
|
||||
|
||||
good:
|
||||
r->num++;
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned ratelimit_num_dropped(RateLimit *r) {
|
||||
assert(r);
|
||||
|
||||
return r->num > r->burst ? r->num - r->burst : 0;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
#include <stdbool.h>
|
||||
|
||||
#include "time-util.h"
|
||||
#include "util.h"
|
||||
|
||||
typedef struct RateLimit {
|
||||
usec_t interval; /* Keep those two fields first so they can be initialized easily: */
|
||||
|
@ -22,3 +21,5 @@ static inline bool ratelimit_configured(RateLimit *rl) {
|
|||
}
|
||||
|
||||
bool ratelimit_below(RateLimit *r);
|
||||
|
||||
unsigned ratelimit_num_dropped(RateLimit *r);
|
||||
|
|
|
@ -373,6 +373,32 @@ bool stat_inode_unmodified(const struct stat *a, const struct stat *b) {
|
|||
(!(S_ISCHR(a->st_mode) || S_ISBLK(a->st_mode)) || a->st_rdev == b->st_rdev); /* if device node, also compare major/minor, because we can */
|
||||
}
|
||||
|
||||
bool statx_inode_same(const struct statx *a, const struct statx *b) {
|
||||
|
||||
/* Same as stat_inode_same() but for struct statx */
|
||||
|
||||
return a && b &&
|
||||
FLAGS_SET(a->stx_mask, STATX_TYPE|STATX_INO) && FLAGS_SET(b->stx_mask, STATX_TYPE|STATX_INO) &&
|
||||
(a->stx_mode & S_IFMT) != 0 &&
|
||||
((a->stx_mode ^ b->stx_mode) & S_IFMT) == 0 &&
|
||||
a->stx_dev_major == b->stx_dev_major &&
|
||||
a->stx_dev_minor == b->stx_dev_minor &&
|
||||
a->stx_ino == b->stx_ino;
|
||||
}
|
||||
|
||||
bool statx_mount_same(const struct new_statx *a, const struct new_statx *b) {
|
||||
if (!a || !b)
|
||||
return false;
|
||||
|
||||
/* if we have the mount ID, that's all we need */
|
||||
if (FLAGS_SET(a->stx_mask, STATX_MNT_ID) && FLAGS_SET(b->stx_mask, STATX_MNT_ID))
|
||||
return a->stx_mnt_id == b->stx_mnt_id;
|
||||
|
||||
/* Otherwise, major/minor of backing device must match */
|
||||
return a->stx_dev_major == b->stx_dev_major &&
|
||||
a->stx_dev_minor == b->stx_dev_minor;
|
||||
}
|
||||
|
||||
int statx_fallback(int dfd, const char *path, int flags, unsigned mask, struct statx *sx) {
|
||||
static bool avoid_statx = false;
|
||||
struct stat st;
|
||||
|
|
|
@ -73,6 +73,9 @@ int proc_mounted(void);
|
|||
bool stat_inode_same(const struct stat *a, const struct stat *b);
|
||||
bool stat_inode_unmodified(const struct stat *a, const struct stat *b);
|
||||
|
||||
bool statx_inode_same(const struct statx *a, const struct statx *b);
|
||||
bool statx_mount_same(const struct new_statx *a, const struct new_statx *b);
|
||||
|
||||
int statx_fallback(int dfd, const char *path, int flags, unsigned mask, struct statx *sx);
|
||||
|
||||
#if HAS_FEATURE_MEMORY_SANITIZER
|
||||
|
|
|
@ -516,7 +516,6 @@ char *cellescape(char *buf, size_t len, const char *s) {
|
|||
buf[i] = '\0';
|
||||
return buf;
|
||||
}
|
||||
#endif /* NM_IGNORED */
|
||||
|
||||
char* strshorten(char *s, size_t l) {
|
||||
assert(s);
|
||||
|
@ -527,6 +526,20 @@ char* strshorten(char *s, size_t l) {
|
|||
return s;
|
||||
}
|
||||
|
||||
int strgrowpad0(char **s, size_t l) {
|
||||
assert(s);
|
||||
|
||||
char *q = realloc(*s, l);
|
||||
if (!q)
|
||||
return -ENOMEM;
|
||||
*s = q;
|
||||
|
||||
size_t sz = strlen(*s);
|
||||
memzero(*s + sz, l - sz);
|
||||
return 0;
|
||||
}
|
||||
#endif /* NM_IGNORED */
|
||||
|
||||
char *strreplace(const char *text, const char *old_string, const char *new_string) {
|
||||
size_t l, old_len, new_len;
|
||||
char *t, *ret = NULL;
|
||||
|
|
|
@ -152,6 +152,8 @@ char *cellescape(char *buf, size_t len, const char *s);
|
|||
|
||||
char* strshorten(char *s, size_t l);
|
||||
|
||||
int strgrowpad0(char **s, size_t l);
|
||||
|
||||
char *strreplace(const char *text, const char *old_string, const char *new_string);
|
||||
|
||||
char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
|
||||
|
|
|
@ -840,13 +840,26 @@ char** strv_shell_escape(char **l, const char *bad) {
|
|||
}
|
||||
#endif /* NM_IGNORED */
|
||||
|
||||
bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *matched_pos) {
|
||||
for (size_t i = 0; patterns && patterns[i]; i++)
|
||||
if (fnmatch(patterns[i], s, flags) == 0) {
|
||||
if (matched_pos)
|
||||
*matched_pos = i;
|
||||
return true;
|
||||
}
|
||||
bool strv_fnmatch_full(
|
||||
char* const* patterns,
|
||||
const char *s,
|
||||
int flags,
|
||||
size_t *ret_matched_pos) {
|
||||
|
||||
assert(s);
|
||||
|
||||
if (patterns)
|
||||
for (size_t i = 0; patterns[i]; i++)
|
||||
/* NB: We treat all fnmatch() errors as equivalent to FNM_NOMATCH, i.e. if fnmatch() fails to
|
||||
* process the pattern for some reason we'll consider this equivalent to non-matching. */
|
||||
if (fnmatch(patterns[i], s, flags) == 0) {
|
||||
if (ret_matched_pos)
|
||||
*ret_matched_pos = i;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ret_matched_pos)
|
||||
*ret_matched_pos = SIZE_MAX;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -240,7 +240,7 @@ void strv_print(char * const *l);
|
|||
char** strv_reverse(char **l);
|
||||
char** strv_shell_escape(char **l, const char *bad);
|
||||
|
||||
bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *matched_pos);
|
||||
bool strv_fnmatch_full(char* const* patterns, const char *s, int flags, size_t *ret_matched_pos);
|
||||
static inline bool strv_fnmatch(char* const* patterns, const char *s) {
|
||||
return strv_fnmatch_full(patterns, s, 0, NULL);
|
||||
}
|
||||
|
|
|
@ -517,10 +517,9 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
|
|||
{ "us", 1 },
|
||||
};
|
||||
|
||||
char *p = buf;
|
||||
char *p = ASSERT_PTR(buf);
|
||||
bool something = false;
|
||||
|
||||
assert(buf);
|
||||
assert(l > 0);
|
||||
|
||||
if (t == USEC_INFINITY) {
|
||||
|
|
|
@ -189,10 +189,15 @@ static inline usec_t usec_sub_unsigned(usec_t timestamp, usec_t delta) {
|
|||
}
|
||||
|
||||
static inline usec_t usec_sub_signed(usec_t timestamp, int64_t delta) {
|
||||
if (delta == INT64_MIN) { /* prevent overflow */
|
||||
assert_cc(-(INT64_MIN + 1) == INT64_MAX);
|
||||
assert_cc(USEC_INFINITY > INT64_MAX);
|
||||
return usec_add(timestamp, (usec_t) INT64_MAX + 1);
|
||||
}
|
||||
if (delta < 0)
|
||||
return usec_add(timestamp, (usec_t) (-delta));
|
||||
else
|
||||
return usec_sub_unsigned(timestamp, (usec_t) delta);
|
||||
|
||||
return usec_sub_unsigned(timestamp, (usec_t) delta);
|
||||
}
|
||||
|
||||
#if SIZEOF_TIME_T == 8
|
||||
|
|
|
@ -90,12 +90,76 @@ int fmkostemp_safe(char *pattern, const char *mode, FILE **ret_f) {
|
|||
}
|
||||
#endif /* NM_IGNORED */
|
||||
|
||||
int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
|
||||
_cleanup_free_ char *d = NULL, *fn = NULL, *nf = NULL;
|
||||
static int tempfn_build(const char *p, const char *pre, const char *post, bool child, char **ret) {
|
||||
_cleanup_free_ char *d = NULL, *fn = NULL, *nf = NULL, *result = NULL;
|
||||
size_t len_pre, len_post, len_add;
|
||||
int r;
|
||||
|
||||
assert(p);
|
||||
assert(ret);
|
||||
|
||||
/*
|
||||
* Turns this:
|
||||
* /foo/bar/waldo
|
||||
*
|
||||
* Into this :
|
||||
* /foo/bar/waldo/.#<pre><post> (child == true)
|
||||
* /foo/bar/.#<pre>waldo<post> (child == false)
|
||||
*/
|
||||
|
||||
if (pre && strchr(pre, '/'))
|
||||
return -EINVAL;
|
||||
|
||||
if (post && strchr(post, '/'))
|
||||
return -EINVAL;
|
||||
|
||||
len_pre = strlen_ptr(pre);
|
||||
len_post = strlen_ptr(post);
|
||||
/* NAME_MAX is counted *without* the trailing NUL byte. */
|
||||
if (len_pre > NAME_MAX - STRLEN(".#") ||
|
||||
len_post > NAME_MAX - STRLEN(".#") - len_pre)
|
||||
return -EINVAL;
|
||||
|
||||
len_add = len_pre + len_post + STRLEN(".#");
|
||||
|
||||
if (child) {
|
||||
d = strdup(p);
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
} else {
|
||||
r = path_extract_directory(p, &d);
|
||||
if (r < 0 && r != -EDESTADDRREQ) /* EDESTADDRREQ → No directory specified, just a filename */
|
||||
return r;
|
||||
|
||||
r = path_extract_filename(p, &fn);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (strlen(fn) > NAME_MAX - len_add)
|
||||
/* We cannot simply prepend and append strings to the filename. Let's truncate the filename. */
|
||||
fn[NAME_MAX - len_add] = '\0';
|
||||
}
|
||||
|
||||
nf = strjoin(".#", strempty(pre), strempty(fn), strempty(post));
|
||||
if (!nf)
|
||||
return -ENOMEM;
|
||||
|
||||
if (d) {
|
||||
if (!path_extend(&d, nf))
|
||||
return -ENOMEM;
|
||||
|
||||
result = path_simplify(TAKE_PTR(d));
|
||||
} else
|
||||
result = TAKE_PTR(nf);
|
||||
|
||||
if (!path_is_valid(result)) /* New path is not valid? (Maybe because too long?) Refuse. */
|
||||
return -EINVAL;
|
||||
|
||||
*ret = TAKE_PTR(result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
|
||||
/*
|
||||
* Turns this:
|
||||
* /foo/bar/waldo
|
||||
|
@ -104,37 +168,14 @@ int tempfn_xxxxxx(const char *p, const char *extra, char **ret) {
|
|||
* /foo/bar/.#<extra>waldoXXXXXX
|
||||
*/
|
||||
|
||||
r = path_extract_directory(p, &d);
|
||||
if (r < 0 && r != -EDESTADDRREQ) /* EDESTADDRREQ → No directory specified, just a filename */
|
||||
return r;
|
||||
|
||||
r = path_extract_filename(p, &fn);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
nf = strjoin(".#", strempty(extra), fn, "XXXXXX");
|
||||
if (!nf)
|
||||
return -ENOMEM;
|
||||
|
||||
if (!filename_is_valid(nf)) /* New name is not valid? (Maybe because too long?) Refuse. */
|
||||
return -EINVAL;
|
||||
|
||||
if (d) {
|
||||
if (!path_extend(&d, nf))
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = path_simplify(TAKE_PTR(d));
|
||||
} else
|
||||
*ret = TAKE_PTR(nf);
|
||||
|
||||
return 0;
|
||||
return tempfn_build(p, extra, "XXXXXX", /* child = */ false, ret);
|
||||
}
|
||||
|
||||
#if 0 /* NM_IGNORED */
|
||||
int tempfn_random(const char *p, const char *extra, char **ret) {
|
||||
_cleanup_free_ char *d = NULL, *fn = NULL, *nf = NULL;
|
||||
int r;
|
||||
_cleanup_free_ char *s = NULL;
|
||||
|
||||
assert(p);
|
||||
assert(ret);
|
||||
|
||||
/*
|
||||
|
@ -145,37 +186,14 @@ int tempfn_random(const char *p, const char *extra, char **ret) {
|
|||
* /foo/bar/.#<extra>waldobaa2a261115984a9
|
||||
*/
|
||||
|
||||
r = path_extract_directory(p, &d);
|
||||
if (r < 0 && r != -EDESTADDRREQ) /* EDESTADDRREQ → No directory specified, just a filename */
|
||||
return r;
|
||||
|
||||
r = path_extract_filename(p, &fn);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (asprintf(&nf, ".#%s%s%016" PRIx64,
|
||||
strempty(extra),
|
||||
fn,
|
||||
random_u64()) < 0)
|
||||
if (asprintf(&s, "%016" PRIx64, random_u64()) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
if (!filename_is_valid(nf)) /* Not valid? (maybe because too long now?) — refuse early */
|
||||
return -EINVAL;
|
||||
|
||||
if (d) {
|
||||
if (!path_extend(&d, nf))
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = path_simplify(TAKE_PTR(d));
|
||||
} else
|
||||
*ret = TAKE_PTR(nf);
|
||||
|
||||
return 0;
|
||||
return tempfn_build(p, extra, s, /* child = */ false, ret);
|
||||
}
|
||||
|
||||
int tempfn_random_child(const char *p, const char *extra, char **ret) {
|
||||
char *t, *x;
|
||||
uint64_t u;
|
||||
_cleanup_free_ char *s = NULL;
|
||||
int r;
|
||||
|
||||
assert(ret);
|
||||
|
@ -192,27 +210,10 @@ int tempfn_random_child(const char *p, const char *extra, char **ret) {
|
|||
return r;
|
||||
}
|
||||
|
||||
extra = strempty(extra);
|
||||
|
||||
t = new(char, strlen(p) + 3 + strlen(extra) + 16 + 1);
|
||||
if (!t)
|
||||
if (asprintf(&s, "%016" PRIx64, random_u64()) < 0)
|
||||
return -ENOMEM;
|
||||
|
||||
if (isempty(p))
|
||||
x = stpcpy(stpcpy(t, ".#"), extra);
|
||||
else
|
||||
x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
|
||||
|
||||
u = random_u64();
|
||||
for (unsigned i = 0; i < 16; i++) {
|
||||
*(x++) = hexchar(u & 0xF);
|
||||
u >>= 4;
|
||||
}
|
||||
|
||||
*x = 0;
|
||||
|
||||
*ret = path_simplify(t);
|
||||
return 0;
|
||||
return tempfn_build(p, extra, s, /* child = */ true, ret);
|
||||
}
|
||||
|
||||
int open_tmpfile_unlinkable(const char *directory, int flags) {
|
||||
|
|
|
@ -13,12 +13,12 @@
|
|||
#include <unistd.h>
|
||||
|
||||
/* Users managed by systemd-homed. See https://systemd.io/UIDS-GIDS for details how this range fits into the rest of the world */
|
||||
#define HOME_UID_MIN 60001
|
||||
#define HOME_UID_MAX 60513
|
||||
#define HOME_UID_MIN ((uid_t) 60001)
|
||||
#define HOME_UID_MAX ((uid_t) 60513)
|
||||
|
||||
/* Users mapped from host into a container */
|
||||
#define MAP_UID_MIN 60514
|
||||
#define MAP_UID_MAX 60577
|
||||
#define MAP_UID_MIN ((uid_t) 60514)
|
||||
#define MAP_UID_MAX ((uid_t) 60577)
|
||||
|
||||
bool uid_is_valid(uid_t uid);
|
||||
|
||||
|
@ -55,7 +55,7 @@ int merge_gid_lists(const gid_t *list1, size_t size1, const gid_t *list2, size_t
|
|||
int getgroups_alloc(gid_t** gids);
|
||||
|
||||
int get_home_dir(char **ret);
|
||||
int get_shell(char **_ret);
|
||||
int get_shell(char **ret);
|
||||
|
||||
int reset_uid_gid(void);
|
||||
|
||||
|
@ -67,9 +67,9 @@ int take_etc_passwd_lock(const char *root);
|
|||
#define UID_NOBODY ((uid_t) 65534U)
|
||||
#define GID_NOBODY ((gid_t) 65534U)
|
||||
|
||||
/* If REMOUNT_IDMAP_HOST_ROOT is set for remount_idmap() we'll include a mapping here that maps the host root
|
||||
* user accessing the idmapped mount to the this user ID on the backing fs. This is the last valid UID in the
|
||||
* *signed* 32bit range. You might wonder why precisely use this specific UID for this purpose? Well, we
|
||||
/* If REMOUNT_IDMAPPING_HOST_ROOT is set for remount_idmap() we'll include a mapping here that maps the host
|
||||
* root user accessing the idmapped mount to the this user ID on the backing fs. This is the last valid UID in
|
||||
* the *signed* 32bit range. You might wonder why precisely use this specific UID for this purpose? Well, we
|
||||
* definitely cannot use the first 0…65536 UIDs for that, since in most cases that's precisely the file range
|
||||
* we intend to map to some high UID range, and since UID mappings have to be bijective we thus cannot use
|
||||
* them at all. Furthermore the UID range beyond INT32_MAX (i.e. the range above the signed 32bit range) is
|
||||
|
@ -130,6 +130,7 @@ int putsgent_sane(const struct sgrp *sg, FILE *stream);
|
|||
#endif
|
||||
|
||||
bool is_nologin_shell(const char *shell);
|
||||
const char* default_root_shell(const char *root);
|
||||
|
||||
int is_this_me(const char *username);
|
||||
|
||||
|
|
|
@ -81,18 +81,13 @@
|
|||
#endif
|
||||
|
||||
/* This passes the argument through after (if asserts are enabled) checking that it is not null. */
|
||||
#define ASSERT_PTR(expr) \
|
||||
({ \
|
||||
typeof(expr) _expr_ = (expr); \
|
||||
assert(_expr_); \
|
||||
_expr_; \
|
||||
})
|
||||
|
||||
#define ASSERT_SE_PTR(expr) \
|
||||
({ \
|
||||
typeof(expr) _expr_ = (expr); \
|
||||
assert_se(_expr_); \
|
||||
_expr_; \
|
||||
#define ASSERT_PTR(expr) _ASSERT_PTR(expr, UNIQ_T(_expr_, UNIQ), assert)
|
||||
#define ASSERT_SE_PTR(expr) _ASSERT_PTR(expr, UNIQ_T(_expr_, UNIQ), assert_se)
|
||||
#define _ASSERT_PTR(expr, var, check) \
|
||||
({ \
|
||||
typeof(expr) var = (expr); \
|
||||
check(var); \
|
||||
var; \
|
||||
})
|
||||
|
||||
#define ASSERT_NONNEG(expr) \
|
||||
|
|
|
@ -106,7 +106,7 @@ void sha256_init_ctx(struct sha256_ctx *ctx) {
|
|||
|
||||
/* Process the remaining bytes in the internal buffer and the usual
|
||||
prolog according to the standard and write the result to RESBUF. */
|
||||
void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf) {
|
||||
uint8_t *sha256_finish_ctx(struct sha256_ctx *ctx, uint8_t resbuf[static SHA256_DIGEST_SIZE]) {
|
||||
/* Take yet unprocessed bytes into account. */
|
||||
uint32_t bytes = ctx->buflen;
|
||||
size_t pad;
|
||||
|
@ -131,7 +131,7 @@ void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf) {
|
|||
/* Put result from CTX in first 32 bytes following RESBUF. */
|
||||
for (size_t i = 0; i < 8; ++i)
|
||||
if (UNALIGNED_P(resbuf))
|
||||
memcpy((uint8_t*) resbuf + i * sizeof(uint32_t), (uint32_t[]) { SWAP(ctx->H[i]) }, sizeof(uint32_t));
|
||||
memcpy(resbuf + i * sizeof(uint32_t), (uint32_t[]) { SWAP(ctx->H[i]) }, sizeof(uint32_t));
|
||||
else
|
||||
((uint32_t *) resbuf)[i] = SWAP(ctx->H[i]);
|
||||
|
||||
|
@ -199,10 +199,9 @@ void sha256_process_bytes(const void *buffer, size_t len, struct sha256_ctx *ctx
|
|||
/* Process LEN bytes of BUFFER, accumulating context into CTX.
|
||||
It is assumed that LEN % 64 == 0. */
|
||||
static void sha256_process_block(const void *buffer, size_t len, struct sha256_ctx *ctx) {
|
||||
const uint32_t *words = buffer;
|
||||
const uint32_t *words = ASSERT_PTR(buffer);
|
||||
size_t nwords = len / sizeof(uint32_t);
|
||||
|
||||
assert(buffer);
|
||||
assert(ctx);
|
||||
|
||||
uint32_t a = ctx->H[0];
|
||||
|
@ -291,3 +290,10 @@ static void sha256_process_block(const void *buffer, size_t len, struct sha256_c
|
|||
ctx->H[6] = g;
|
||||
ctx->H[7] = h;
|
||||
}
|
||||
|
||||
uint8_t* sha256_direct(const void *buffer, size_t sz, uint8_t result[static SHA256_DIGEST_SIZE]) {
|
||||
struct sha256_ctx ctx;
|
||||
sha256_init_ctx(&ctx);
|
||||
sha256_process_bytes(buffer, sz, &ctx);
|
||||
return sha256_finish_ctx(&ctx, result);
|
||||
}
|
||||
|
|
|
@ -25,5 +25,9 @@ struct sha256_ctx {
|
|||
};
|
||||
|
||||
void sha256_init_ctx(struct sha256_ctx *ctx);
|
||||
void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf);
|
||||
uint8_t *sha256_finish_ctx(struct sha256_ctx *ctx, uint8_t resbuf[static SHA256_DIGEST_SIZE]);
|
||||
void sha256_process_bytes(const void *buffer, size_t len, struct sha256_ctx *ctx);
|
||||
|
||||
uint8_t* sha256_direct(const void *buffer, size_t sz, uint8_t result[static SHA256_DIGEST_SIZE]);
|
||||
|
||||
#define SHA256_DIRECT(buffer, sz) sha256_direct(buffer, sz, (uint8_t[SHA256_DIGEST_SIZE]) {})
|
||||
|
|
Loading…
Reference in a new issue