Also use _cleanup_free_ in one more place.
}
int cg_path_get_unit(const char *path, char **ret) {
+ _cleanup_free_ char *unit = NULL;
const char *e;
- char *unit;
int r;
assert(path);
return r;
/* We skipped over the slices, don't accept any now */
- if (endswith(unit, ".slice")) {
- free(unit);
+ if (endswith(unit, ".slice"))
return -ENXIO;
- }
- *ret = unit;
+ *ret = TAKE_PTR(unit);
return 0;
}
}
void close_many(const int fds[], size_t n_fd) {
- size_t i;
-
assert(fds || n_fd <= 0);
- for (i = 0; i < n_fd; i++)
+ for (size_t i = 0; i < n_fd; i++)
safe_close(fds[i]);
}
}
_pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
- size_t i;
-
assert(n_fdset == 0 || fdset);
- for (i = 0; i < n_fdset; i++)
+ for (size_t i = 0; i < n_fdset; i++)
if (fdset[i] == fd)
return true;
{ "K", UINT64_C(1000) },
};
const suffix_table *table;
- size_t n, i;
+ size_t n;
assert_cc(ELEMENTSOF(table_iec) == ELEMENTSOF(table_si));
table = flag & FORMAT_BYTES_USE_IEC ? table_iec : table_si;
n = ELEMENTSOF(table_iec);
- for (i = 0; i < n; i++)
+ for (size_t i = 0; i < n; i++)
if (t >= table[i].factor) {
if (flag & FORMAT_BYTES_BELOW_POINT) {
snprintf(buf, l,
}
void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
- size_t i;
-
- for (i = 0; i < iovw->count; i++)
+ for (size_t i = 0; i < iovw->count; i++)
iovw->iovec[i].iov_base = (char *)iovw->iovec[i].iov_base - old + new;
}
size_t iovw_size(struct iovec_wrapper *iovw) {
- size_t n = 0, i;
+ size_t n = 0;
- for (i = 0; i < iovw->count; i++)
+ for (size_t i = 0; i < iovw->count; i++)
n += iovw->iovec[i].iov_len;
return n;
}
int parse_fractional_part_u(const char **p, size_t digits, unsigned *res) {
- size_t i;
unsigned val = 0;
const char *s;
s = *p;
/* accept any number of digits, strtoull is limited to 19 */
- for (i=0; i < digits; i++,s++) {
+ for (size_t i = 0; i < digits; i++,s++) {
if (*s < '0' || *s > '9') {
if (i == 0)
return -EINVAL;
}
int setrlimit_closest_all(const struct rlimit *const *rlim, int *which_failed) {
- int i, r;
+ int r;
assert(rlim);
/* On failure returns the limit's index that failed in *which_failed, but only if non-NULL */
- for (i = 0; i < _RLIMIT_MAX; i++) {
+ for (int i = 0; i < _RLIMIT_MAX; i++) {
if (!rlim[i])
continue;
static volatile sig_atomic_t n_sigbus_queue = 0;
static void sigbus_push(void *addr) {
- unsigned u;
-
assert(addr);
/* Find a free place, increase the number of entries and leave, if we can */
- for (u = 0; u < SIGBUS_QUEUE_MAX; u++)
+ for (size_t u = 0; u < SIGBUS_QUEUE_MAX; u++)
if (__sync_bool_compare_and_swap(&sigbus_queue[u], NULL, addr)) {
__sync_fetch_and_add(&n_sigbus_queue, 1);
return;
.sa_handler = SIG_DFL,
.sa_flags = SA_RESTART,
};
- int sig, r = 0;
+ int r = 0;
- for (sig = 1; sig < _NSIG; sig++) {
+ for (int sig = 1; sig < _NSIG; sig++) {
/* These two cannot be caught... */
if (IN_SET(sig, SIGKILL, SIGSTOP))
}
char *strrep(const char *s, unsigned n) {
- size_t l;
char *r, *p;
- unsigned i;
+ size_t l;
assert(s);
if (!r)
return NULL;
- for (i = 0; i < n; i++)
+ for (unsigned i = 0; i < n; i++)
p = stpcpy(p, s);
*p = 0;
{ "us", 1 },
};
- size_t i;
char *p = buf;
bool something = false;
/* The result of this function can be parsed with parse_sec */
- for (i = 0; i < ELEMENTSOF(table); i++) {
+ for (size_t i = 0; i < ELEMENTSOF(table); i++) {
int k = 0;
size_t n;
bool done = false;
{ "us", 1ULL },
{ "µs", 1ULL },
};
- size_t i;
- for (i = 0; i < ELEMENTSOF(table); i++) {
+ for (size_t i = 0; i < ELEMENTSOF(table); i++) {
char *e;
e = startswith(p, table[i].suffix);
const char *fn;
char *t, *x;
uint64_t u;
- unsigned i;
assert(ret);
x = stpcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), ".#"), extra), fn);
u = random_u64();
- for (i = 0; i < 16; i++) {
+ for (unsigned i = 0; i < 16; i++) {
*(x++) = hexchar(u & 0xF);
u >>= 4;
}
int tempfn_random_child(const char *p, const char *extra, char **ret) {
char *t, *x;
uint64_t u;
- unsigned i;
int r;
assert(ret);
x = stpcpy(stpcpy(stpcpy(t, p), "/.#"), extra);
u = random_u64();
- for (i = 0; i < 16; i++) {
+ for (unsigned i = 0; i < 16; i++) {
*(x++) = hexchar(u & 0xF);
u >>= 4;
}
const char* runlevel_to_target(const char *word) {
const char * const *rlmap_ptr;
- size_t i;
if (!word)
return NULL;
rlmap_ptr = in_initrd() ? rlmap_initrd : rlmap;
- for (i = 0; rlmap_ptr[i]; i += 2)
+ for (size_t i = 0; rlmap_ptr[i]; i += 2)
if (streq(word, rlmap_ptr[i]))
return rlmap_ptr[i+1];
/* decode one unicode char */
int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
char32_t unichar;
- size_t len, i;
+ size_t len;
assert(str);
return -EINVAL;
}
- for (i = 1; i < len; i++) {
+ for (size_t i = 1; i < len; i++) {
if (((char32_t)str[i] & 0xc0) != 0x80)
return -EINVAL;
}
char *ascii_is_valid_n(const char *str, size_t len) {
- size_t i;
-
/* Very similar to ascii_is_valid(), but checks exactly len
* bytes and rejects any NULs in that range. */
assert(str);
- for (i = 0; i < len; i++)
+ for (size_t i = 0; i < len; i++)
if ((unsigned char) str[i] >= 128 || str[i] == 0)
return NULL;
char16_t *utf8_to_utf16(const char *s, size_t length) {
char16_t *n, *p;
- size_t i;
int r;
assert(s);
p = n;
- for (i = 0; i < length;) {
+ for (size_t i = 0; i < length;) {
char32_t unichar;
size_t e;
/* validate one encoded unicode char and return its length */
int utf8_encoded_valid_unichar(const char *str, size_t length /* bytes */) {
char32_t unichar;
- size_t len, i;
+ size_t len;
int r;
assert(str);
return 1;
/* check if expected encoded chars are available */
- for (i = 0; i < len; i++)
+ for (size_t i = 0; i < len; i++)
if ((str[i] & 0x80) != 0x80)
return -EINVAL;
/* https://wiki.freebsd.org/bhyve */
{ "BHYVE", VIRTUALIZATION_BHYVE },
};
- unsigned i;
int r;
- for (i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
+ for (size_t i = 0; i < ELEMENTSOF(dmi_vendors); i++) {
_cleanup_free_ char *s = NULL;
unsigned j;
sd_bus_error *error) {
CGroupMask *mask = userdata;
- CGroupController ctrl;
int r;
assert(bus);
if (r < 0)
return r;
- for (ctrl = 0; ctrl < _CGROUP_CONTROLLER_MAX; ctrl++) {
+ for (CGroupController ctrl = 0; ctrl < _CGROUP_CONTROLLER_MAX; ctrl++) {
if ((*mask & CGROUP_CONTROLLER_TO_MASK(ctrl)) == 0)
continue;
sd_bus_error *error) {
ExecContext *c = userdata;
- unsigned i;
bool ro;
int r;
if (r < 0)
return r;
- for (i = 0; i < c->n_bind_mounts; i++) {
+ for (size_t i = 0; i < c->n_bind_mounts; i++) {
if (ro != c->bind_mounts[i].read_only)
continue;
sd_bus_error *error) {
ExecContext *c = userdata;
- unsigned i;
int r;
assert(bus);
if (r < 0)
return r;
- for (i = 0; i < c->n_temporary_filesystems; i++) {
+ for (unsigned i = 0; i < c->n_temporary_filesystems; i++) {
TemporaryFileSystem *t = c->temporary_filesystems + i;
r = sd_bus_message_append(
sd_bus_error *error) {
ExecContext *c = userdata;
- size_t i;
int r;
assert(bus);
if (r < 0)
return r;
- for (i = 0; i < c->n_log_extra_fields; i++) {
+ for (size_t i = 0; i < c->n_log_extra_fields; i++) {
r = sd_bus_message_append_array(reply, 'y', c->log_extra_fields[i].iov_base, c->log_extra_fields[i].iov_len);
if (r < 0)
return r;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_free_ Job **list = NULL;
Job *j = userdata;
- int r, i, n;
+ int r, n;
if (strstr(sd_bus_message_get_member(message), "After"))
n = job_get_after(j, &list);
if (r < 0)
return r;
- for (i = 0; i < n; i ++) {
+ for (int i = 0; i < n; i ++) {
_cleanup_free_ char *unit_path = NULL, *job_path = NULL;
job_path = job_dbus_path(list[i]);
UnitFileChange *changes,
size_t n_changes) {
- size_t i;
int r;
- for (i = 0; i < n_changes; i++)
+ for (size_t i = 0; i < n_changes; i++)
switch(changes[i].type) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
bool bad = false, good = false;
- size_t i;
int r;
if (unit_file_changes_have_modification(changes, n_changes)) {
if (r < 0)
goto fail;
- for (i = 0; i < n_changes; i++) {
+ for (size_t i = 0; i < n_changes; i++) {
if (changes[i].type < 0) {
bad = true;
static int device_found_to_string_many(DeviceFound flags, char **ret) {
_cleanup_free_ char *s = NULL;
- unsigned i;
assert(ret);
- for (i = 0; i < ELEMENTSOF(device_found_map); i++) {
+ for (size_t i = 0; i < ELEMENTSOF(device_found_map); i++) {
if (!FLAGS_SET(flags, device_found_map[i].flag))
continue;
}
void exec_command_done_array(ExecCommand *c, size_t n) {
- size_t i;
-
- for (i = 0; i < n; i++)
+ for (size_t i = 0; i < n; i++)
exec_command_done(c+i);
}
}
static int append_bind_mounts(MountEntry **p, const BindMount *binds, size_t n) {
- size_t i;
-
assert(p);
- for (i = 0; i < n; i++) {
+ for (size_t i = 0; i < n; i++) {
const BindMount *b = binds + i;
*((*p)++) = (MountEntry) {
}
static int append_static_mounts(MountEntry **p, const MountEntry *mounts, size_t n, bool ignore_protect) {
- size_t i;
-
assert(p);
assert(mounts);
/* Adds a list of static pre-defined entries */
- for (i = 0; i < n; i++)
+ for (size_t i = 0; i < n; i++)
*((*p)++) = (MountEntry) {
.path_const = mount_entry_path(mounts+i),
.mode = mounts[i].mode,
}
static int prefix_where_needed(MountEntry *m, size_t n, const char *root_directory) {
- size_t i;
-
/* Prefixes all paths in the bind mount table with the root directory if the entry needs that. */
- for (i = 0; i < n; i++) {
+ assert(m || n == 0);
+
+ for (size_t i = 0; i < n; i++) {
char *s;
if (m[i].has_prefix)
}
void bind_mount_free_many(BindMount *b, size_t n) {
- size_t i;
-
assert(b || n == 0);
- for (i = 0; i < n; i++) {
+ for (size_t i = 0; i < n; i++) {
free(b[i].source);
free(b[i].destination);
}
}
MountImage* mount_image_free_many(MountImage *m, size_t *n) {
- size_t i;
-
assert(n);
assert(m || *n == 0);
- for (i = 0; i < *n; i++) {
+ for (size_t i = 0; i < *n; i++) {
free(m[i].source);
free(m[i].destination);
mount_options_free_all(m[i].mount_options);
}
void temporary_filesystem_free_many(TemporaryFileSystem *t, size_t n) {
- size_t i;
-
assert(t || n == 0);
- for (i = 0; i < n; i++) {
+ for (size_t i = 0; i < n; i++) {
free(t[i].path);
free(t[i].options);
}
static int usbffs_dispatch_eps(SocketPort *p) {
_cleanup_free_ struct dirent **ent = NULL;
- size_t n, k, i;
+ size_t n, k;
int r;
r = scandir(p->path, &ent, usbffs_select_ep, alphasort);
p->n_auxiliary_fds = n;
k = 0;
- for (i = 0; i < n; ++i) {
+ for (size_t i = 0; i < n; ++i) {
_cleanup_free_ char *ep = NULL;
ep = path_make_absolute(ent[i]->d_name, p->path);
p->n_auxiliary_fds = 0;
clear:
- for (i = 0; i < n; ++i)
+ for (size_t i = 0; i < n; ++i)
free(ent[i]);
return r;
}
static int swap_load_proc_swaps(Manager *m, bool set_flags) {
- unsigned i;
-
assert(m);
rewind(m->proc_swaps);
(void) fscanf(m->proc_swaps, "%*s %*s %*s %*s %*s\n");
- for (i = 1;; i++) {
+ for (unsigned i = 1;; i++) {
_cleanup_free_ char *dev = NULL, *d = NULL;
int prio = 0, k;
};
int r;
- unsigned k;
assert(t);
/* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
* ordering manually we won't add anything in here to make sure we don't create a loop. */
- for (k = 0; k < ELEMENTSOF(deps); k++) {
+ for (size_t k = 0; k < ELEMENTSOF(deps); k++) {
Unit *other;
void *v;
};
int r = 0;
- unsigned i;
assert(fd >= 0);
/* Attach some metadata to coredumps via extended
* attributes. Just because we can. */
- for (i = 0; i < _META_MAX; i++) {
+ for (unsigned i = 0; i < _META_MAX; i++) {
int k;
if (isempty(context->meta[i]) || !xattrs[i])
}
static int save_context(Context *context, const struct iovec_wrapper *iovw) {
- unsigned n, i, count = 0;
+ unsigned count = 0;
const char *unit;
int r;
/* The context does not allocate any memory on its own */
- for (n = 0; n < iovw->count; n++) {
+ for (size_t n = 0; n < iovw->count; n++) {
struct iovec *iovec = iovw->iovec + n;
- for (i = 0; i < ELEMENTSOF(meta_field_names); i++) {
+ for (size_t i = 0; i < ELEMENTSOF(meta_field_names); i++) {
char *p;
/* Note that these strings are NUL terminated, because we made sure that a
Context context = {};
struct iovec_wrapper iovw = {};
struct iovec iovec;
- int i, r;
+ int r;
assert(fd >= 0);
goto finish;
/* Make sure we received at least all fields we need. */
- for (i = 0; i < _META_MANDATORY_MAX; i++)
+ for (int i = 0; i < _META_MANDATORY_MAX; i++)
if (!context.meta[i]) {
r = log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"A mandatory argument (%i) has not been sent, aborting.",
.un.sun_path = "/run/systemd/coredump",
};
_cleanup_close_ int fd = -1;
- size_t i;
int r;
assert(iovw);
if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0)
return log_error_errno(errno, "Failed to connect to coredump service: %m");
- for (i = 0; i < iovw->count; i++) {
+ for (size_t i = 0; i < iovw->count; i++) {
struct msghdr mh = {
.msg_iov = iovw->iovec + i,
.msg_iovlen = 1,
int argc, char **argv) {
_cleanup_free_ char *free_timestamp = NULL;
- int i, r, signo;
+ int r, signo;
char *t;
/* We gather all metadata that were passed via argv[] into an array of iovecs that
"Not enough arguments passed by the kernel (%i, expected %i).",
argc, _META_ARGV_MAX);
- for (i = 0; i < _META_ARGV_MAX; i++) {
+ for (int i = 0; i < _META_ARGV_MAX; i++) {
t = argv[i];
Context context = {};
struct iovec_wrapper *iovw;
char *message;
- size_t i;
int r;
_cleanup_(journal_importer_cleanup) JournalImporter importer = JOURNAL_IMPORTER_INIT(STDIN_FILENO);
/* The imported iovecs are not supposed to be freed by us so let's store
* them at the end of the array so we can skip them while freeing the
* rest. */
- for (i = 0; i < importer.iovw.count; i++) {
+ for (size_t i = 0; i < importer.iovw.count; i++) {
struct iovec *iovec = importer.iovw.iovec + i;
iovw_put(iovw, iovec->iov_base, iovec->iov_len);
#define MIN_NUMBER_OF_RUNS 4
int main(int argc, char **argv) {
- int i, r;
- size_t size;
- char *name;
+ int r;
test_setup_logging(LOG_DEBUG);
- for (i = 1; i < argc; i++) {
+ for (int i = 1; i < argc; i++) {
_cleanup_free_ char *buf = NULL;
+ size_t size;
+ char *name;
name = argv[i];
r = read_full_file(name, &buf, &size);
}
int sd_dhcp_server_forcerenew(sd_dhcp_server *server) {
- unsigned i;
int r = 0;
assert_return(server, -EINVAL);
assert(server->bound_leases);
- for (i = 0; i < server->pool_size; i++) {
+ for (uint32_t i = 0; i < server->pool_size; i++) {
DHCPLease *lease = server->bound_leases[i];
if (!lease || lease == &server->invalid_lease)
}
void bus_flush_memfd(sd_bus *b) {
- unsigned i;
-
assert(b);
- for (i = 0; i < b->n_memfd_cache; i++)
+ for (unsigned i = 0; i < b->n_memfd_cache; i++)
close_and_munmap(b->memfd_cache[i].fd, b->memfd_cache[i].address, b->memfd_cache[i].mapped);
}
_public_ int sd_listen_fds(int unset_environment) {
const char *e;
- int n, r, fd;
+ int n, r;
pid_t pid;
e = getenv("LISTEN_PID");
goto finish;
}
- for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
+ for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
r = fd_cloexec(fd, true);
if (r < 0)
goto finish;
}
static sd_device_enumerator *device_enumerator_free(sd_device_enumerator *enumerator) {
- size_t i;
-
assert(enumerator);
- for (i = 0; i < enumerator->n_devices; i++)
+ for (size_t i = 0; i < enumerator->n_devices; i++)
sd_device_unref(enumerator->devices[i]);
free(enumerator->devices);
int device_enumerator_scan_devices(sd_device_enumerator *enumerator) {
int r = 0, k;
- size_t i;
assert(enumerator);
enumerator->type == DEVICE_ENUMERATION_TYPE_DEVICES)
return 0;
- for (i = 0; i < enumerator->n_devices; i++)
+ for (size_t i = 0; i < enumerator->n_devices; i++)
sd_device_unref(enumerator->devices[i]);
enumerator->n_devices = 0;
int device_enumerator_scan_subsystems(sd_device_enumerator *enumerator) {
const char *subsysdir;
int r = 0, k;
- size_t i;
assert(enumerator);
enumerator->type == DEVICE_ENUMERATION_TYPE_SUBSYSTEMS)
return 0;
- for (i = 0; i < enumerator->n_devices; i++)
+ for (size_t i = 0; i < enumerator->n_devices; i++)
sd_device_unref(enumerator->devices[i]);
enumerator->n_devices = 0;