static int dracut_install(const char *src, const char *dst, bool isdir, bool resolvedeps, bool hashdst);
-
-static inline void kmod_module_unrefp(struct kmod_module **p) {
+static inline void kmod_module_unrefp(struct kmod_module **p)
+{
if (*p)
kmod_module_unref(*p);
}
+
#define _cleanup_kmod_module_unref_ _cleanup_(kmod_module_unrefp)
-static inline void kmod_module_unref_listp(struct kmod_list **p) {
+static inline void kmod_module_unref_listp(struct kmod_list **p)
+{
if (*p)
kmod_module_unref_list(*p);
}
+
#define _cleanup_kmod_module_unref_list_ _cleanup_(kmod_module_unref_listp)
-static inline void kmod_module_info_free_listp(struct kmod_list **p) {
+static inline void kmod_module_info_free_listp(struct kmod_list **p)
+{
if (*p)
kmod_module_info_free_list(*p);
}
+
#define _cleanup_kmod_module_info_free_list_ _cleanup_(kmod_module_info_free_listp)
-static inline void kmod_unrefp(struct kmod_ctx **p) {
+static inline void kmod_unrefp(struct kmod_ctx **p)
+{
kmod_unref(*p);
}
+
#define _cleanup_kmod_unref_ _cleanup_(kmod_unrefp)
-static inline void kmod_module_dependency_symbols_free_listp(struct kmod_list **p) {
+static inline void kmod_module_dependency_symbols_free_listp(struct kmod_list **p)
+{
if (*p)
kmod_module_dependency_symbols_free_list(*p);
}
+
#define _cleanup_kmod_module_dependency_symbols_free_list_ _cleanup_(kmod_module_dependency_symbols_free_listp)
-static inline void fts_closep(FTS **p) {
+static inline void fts_closep(FTS ** p)
+{
if (*p)
fts_close(*p);
}
+
#define _cleanup_fts_close_ _cleanup_(fts_closep)
#define _cleanup_globfree_ _cleanup_(globfree)
-
static size_t dir_len(char const *file)
{
size_t length;
if (ret == 0) {
struct timeval tv[2];
if (fchown(dest_desc, sb.st_uid, sb.st_gid) != 0)
- if(fchown(dest_desc, (uid_t) - 1, sb.st_gid) != 0) {
+ if (fchown(dest_desc, (uid_t) - 1, sb.st_gid) != 0) {
if (geteuid() == 0)
log_error("Failed to chown %s: %m", dst);
else
pid = fork();
if (pid == 0) {
if (geteuid() == 0 && no_xattr == false)
- execlp("cp", "cp", "--reflink=auto", "--sparse=auto", "--preserve=mode,xattr,timestamps", "-fL", src, dst,
- NULL);
+ execlp("cp", "cp", "--reflink=auto", "--sparse=auto", "--preserve=mode,xattr,timestamps", "-fL",
+ src, dst, NULL);
else
- execlp("cp", "cp", "--reflink=auto", "--sparse=auto", "--preserve=mode,timestamps", "-fL", src, dst,
- NULL);
+ execlp("cp", "cp", "--reflink=auto", "--sparse=auto", "--preserve=mode,timestamps", "-fL", src,
+ dst, NULL);
_exit(EXIT_FAILURE);
}
if (errno != EINTR) {
ret = -1;
if (geteuid() == 0 && no_xattr == false)
- log_error("Failed: cp --reflink=auto --sparse=auto --preserve=mode,xattr,timestamps -fL %s %s", src,
- dst);
+ log_error
+ ("Failed: cp --reflink=auto --sparse=auto --preserve=mode,xattr,timestamps -fL %s %s",
+ src, dst);
else
- log_error("Failed: cp --reflink=auto --sparse=auto --preserve=mode,timestamps -fL %s %s", src,
- dst);
+ log_error
+ ("Failed: cp --reflink=auto --sparse=auto --preserve=mode,timestamps -fL %s %s",
+ src, dst);
break;
}
}
if (sysrootdirlen) {
if (strncmp(src, sysrootdir, sysrootdirlen) == 0)
fullsrcpath = strdup(src);
- else if (asprintf(&fullsrcpath, "%s/%s", (sysrootdirlen ? sysrootdir : ""), (src[0] == '/' ? src+1 : src)) < 0)
- _exit(EXIT_FAILURE);
+ else if (asprintf
+ (&fullsrcpath, "%s/%s", (sysrootdirlen ? sysrootdir : ""),
+ (src[0] == '/' ? src + 1 : src)) < 0)
+ _exit(EXIT_FAILURE);
} else
fullsrcpath = strdup(src);
log_debug("get_real_file('%s')", fullsrcpath);
if (lstat(fullsrcpath, &sb) < 0)
- return NULL;
+ return NULL;
switch (sb.st_mode & S_IFMT) {
case S_IFDIR:
case S_IFREG:
- return strdup(fullsrcpath);
+ return strdup(fullsrcpath);
case S_IFLNK:
- break;
+ break;
default:
- return NULL;
+ return NULL;
}
linksz = readlink(fullsrcpath, linktarget, sizeof(linktarget));
log_error("Could not append '%s' to logfile '%s': %m", path, logfile);
}
-static bool check_hashmap(Hashmap *hm, const char *item)
+static bool check_hashmap(Hashmap * hm, const char *item)
{
char *existing;
existing = hashmap_get(hm, item);
return false;
}
-static int dracut_mkdir(const char *src) {
+static int dracut_mkdir(const char *src)
+{
_cleanup_free_ char *parent = NULL;
char *path;
struct stat sb;
if (!parent)
return 1;
- path = parent[0] == '/' ? parent+1 : parent;
+ path = parent[0] == '/' ? parent + 1 : parent;
while (path) {
path = strstr(path, "/");
if (path)
src_mode = sb.st_mode;
}
- ret = asprintf(&fulldstpath, "%s/%s", destrootdir, (dst[0]=='/' ? (dst+1) : dst));
+ ret = asprintf(&fulldstpath, "%s/%s", destrootdir, (dst[0] == '/' ? (dst + 1) : dst));
if (ret < 0) {
log_error("Out of memory!");
exit(EXIT_FAILURE);
if (lstat(fulldstpath, &sb) != 0) {
_cleanup_free_ char *absdestpath = NULL;
- ret = asprintf(&absdestpath, "%s/%s", destrootdir, (abspath[0]=='/' ? (abspath+1) : abspath) + sysrootdirlen);
+ ret =
+ asprintf(&absdestpath, "%s/%s", destrootdir,
+ (abspath[0] == '/' ? (abspath + 1) : abspath) + sysrootdirlen);
if (ret < 0) {
log_error("Out of memory!");
exit(EXIT_FAILURE);
static void usage(int status)
{
- /* */
+ /* */
printf("Usage: %s -D DESTROOTDIR [-r SYSROOTDIR] [OPTION]... -a SOURCE...\n"
"or: %s -D DESTROOTDIR [-r SYSROOTDIR] [OPTION]... SOURCE DEST\n"
"or: %s -D DESTROOTDIR [-r SYSROOTDIR] [OPTION]... -m KERNELMODULE [KERNELMODULE …]\n"
" --debug Show debug output\n"
" --version Show package version\n"
" -h --help Show this help\n"
- "\n",
- program_invocation_short_name, program_invocation_short_name,
- program_invocation_short_name);
+ "\n", program_invocation_short_name, program_invocation_short_name, program_invocation_short_name);
exit(status);
}
sysrootdirlen = strlen(sysrootdir);
break;
case 'p':
- if (regcomp(&mod_filter_path, optarg, REG_NOSUB|REG_EXTENDED) != 0) {
+ if (regcomp(&mod_filter_path, optarg, REG_NOSUB | REG_EXTENDED) != 0) {
log_error("Module path filter %s is not a regular expression", optarg);
exit(EXIT_FAILURE);
}
arg_mod_filter_path = true;
break;
case 'P':
- if (regcomp(&mod_filter_nopath, optarg, REG_NOSUB|REG_EXTENDED) != 0) {
+ if (regcomp(&mod_filter_nopath, optarg, REG_NOSUB | REG_EXTENDED) != 0) {
log_error("Module path filter %s is not a regular expression", optarg);
exit(EXIT_FAILURE);
}
arg_mod_filter_nopath = true;
break;
case 's':
- if (regcomp(&mod_filter_symbol, optarg, REG_NOSUB|REG_EXTENDED) != 0) {
+ if (regcomp(&mod_filter_symbol, optarg, REG_NOSUB | REG_EXTENDED) != 0) {
log_error("Module symbol filter %s is not a regular expression", optarg);
exit(EXIT_FAILURE);
}
arg_mod_filter_symbol = true;
break;
case 'S':
- if (regcomp(&mod_filter_nosymbol, optarg, REG_NOSUB|REG_EXTENDED) != 0) {
+ if (regcomp(&mod_filter_nosymbol, optarg, REG_NOSUB | REG_EXTENDED) != 0) {
log_error("Module symbol filter %s is not a regular expression", optarg);
exit(EXIT_FAILURE);
}
arg_mod_filter_nosymbol = true;
break;
case 'N':
- if (regcomp(&mod_filter_noname, optarg, REG_NOSUB|REG_EXTENDED) != 0) {
+ if (regcomp(&mod_filter_noname, optarg, REG_NOSUB | REG_EXTENDED) != 0) {
log_error("Module symbol filter %s is not a regular expression", optarg);
exit(EXIT_FAILURE);
}
for (j = 0; j < globbuf.gl_pathc; j++) {
char *dest = strdup(globbuf.gl_pathv[j] + sysrootdirlen);
- ret |= dracut_install(globbuf.gl_pathv[j] + sysrootdirlen, dest, arg_createdir, arg_resolvedeps, true);
+ ret |=
+ dracut_install(globbuf.gl_pathv[j] + sysrootdirlen, dest,
+ arg_createdir, arg_resolvedeps, true);
free(dest);
}
}
return ret;
}
-static int install_firmware(struct kmod_module *mod) {
+static int install_firmware(struct kmod_module *mod)
+{
struct kmod_list *l;
_cleanup_kmod_module_info_free_list_ struct kmod_list *list = NULL;
int ret;
ret = kmod_module_get_info(mod, &list);
if (ret < 0) {
- log_error("could not get modinfo from '%s': %s\n",
- kmod_module_get_name(mod), strerror(-ret));
+ log_error("could not get modinfo from '%s': %s\n", kmod_module_get_name(mod), strerror(-ret));
return ret;
}
kmod_list_foreach(l, list) {
exit(EXIT_FAILURE);
}
- if ((strstr(value, "*") != 0 || strstr(value, "?") != 0 || strstr(value, "[") != 0) && stat(fwpath, &sb) != 0) {
+ if ((strstr(value, "*") != 0 || strstr(value, "?") != 0 || strstr(value, "[") != 0)
+ && stat(fwpath, &sb) != 0) {
int i;
_cleanup_globfree_ glob_t globbuf;
glob(fwpath, 0, NULL, &globbuf);
for (i = 0; i < globbuf.gl_pathc; i++) {
install_firmware_fullpath(globbuf.gl_pathv[i]);
if (ret != 0) {
- log_info("Possible missing firmware %s for kernel module %s", value, kmod_module_get_name(mod));
+ log_info("Possible missing firmware %s for kernel module %s", value,
+ kmod_module_get_name(mod));
}
}
} else {
install_firmware_fullpath(fwpath);
if (ret != 0) {
- log_info("Possible missing firmware %s for kernel module %s", value, kmod_module_get_name(mod));
+ log_info("Possible missing firmware %s for kernel module %s", value,
+ kmod_module_get_name(mod));
}
}
}
const char *symbol = kmod_module_symbol_get_symbol(itr);
// log_debug("Checking symbol %s", symbol);
if (regexec(&mod_filter_nosymbol, symbol, 0, NULL, 0) == 0) {
- log_debug("Module %s: symbol %s matched exclusion filter", kmod_module_get_name(mod), symbol);
+ log_debug("Module %s: symbol %s matched exclusion filter", kmod_module_get_name(mod),
+ symbol);
return false;
}
}
const char *symbol = kmod_module_dependency_symbol_get_symbol(itr);
// log_debug("Checking symbol %s", symbol);
if (regexec(&mod_filter_symbol, symbol, 0, NULL, 0) == 0) {
- log_debug("Module %s: symbol %s matched inclusion filter", kmod_module_get_name(mod), symbol);
+ log_debug("Module %s: symbol %s matched inclusion filter", kmod_module_get_name(mod),
+ symbol);
return true;
}
}
int ret = 0;
kmod_list_foreach(itr, modlist) {
- _cleanup_kmod_module_unref_ struct kmod_module *mod = NULL;
+ _cleanup_kmod_module_unref_ struct kmod_module *mod = NULL;
mod = kmod_module_get_module(itr);
path = kmod_module_get_path(mod);
if (path == NULL)
continue;
- if (check_hashmap(items_failed, path))
- return -1;
+ if (check_hashmap(items_failed, path))
+ return -1;
- if (check_hashmap(items, path)) {
- continue;
- }
+ if (check_hashmap(items, path)) {
+ continue;
+ }
name = kmod_module_get_name(mod);
ret = dracut_install(path, &path[kerneldirlen], false, false, true);
if (ret == 0) {
- _cleanup_kmod_module_unref_list_ struct kmod_list *modlist = NULL;
- _cleanup_kmod_module_unref_list_ struct kmod_list *modpre = NULL;
- _cleanup_kmod_module_unref_list_ struct kmod_list *modpost = NULL;
+ _cleanup_kmod_module_unref_list_ struct kmod_list *modlist = NULL;
+ _cleanup_kmod_module_unref_list_ struct kmod_list *modpre = NULL;
+ _cleanup_kmod_module_unref_list_ struct kmod_list *modpost = NULL;
log_debug("dracut_install '%s' '%s' OK", path, &path[kerneldirlen]);
install_firmware(mod);
- modlist = kmod_module_get_dependencies(mod);
- ret = install_dependent_modules(modlist);
- if (ret == 0) {
- ret = kmod_module_get_softdeps(mod, &modpre, &modpost);
- if (ret == 0) {
+ modlist = kmod_module_get_dependencies(mod);
+ ret = install_dependent_modules(modlist);
+ if (ret == 0) {
+ ret = kmod_module_get_softdeps(mod, &modpre, &modpost);
+ if (ret == 0) {
int r;
- ret = install_dependent_modules(modpre);
+ ret = install_dependent_modules(modpre);
r = install_dependent_modules(modpost);
ret = ret ? : r;
}
if (ret == 0) {
ret = kmod_module_get_softdeps(mod, &modpre, &modpost);
- if (ret == 0) {
+ if (ret == 0) {
int r;
- ret = install_dependent_modules(modpre);
+ ret = install_dependent_modules(modpre);
r = install_dependent_modules(modpost);
ret = ret ? : r;
}
{
char *paths[] = { "/sys/devices", NULL };
- fts = fts_open(paths, FTS_NOCHDIR|FTS_NOSTAT, NULL);
+ fts = fts_open(paths, FTS_NOCHDIR | FTS_NOSTAT, NULL);
}
- for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
+ for (FTSENT * ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_kmod_module_unref_list_ struct kmod_list *list = NULL;
struct kmod_list *l;
if (!(f = fopen(ftsent->fts_accpath, "r")))
continue;
- if(!fgets(alias, sizeof(alias), f))
+ if (!fgets(alias, sizeof(alias), f))
continue;
len = strlen(alias);
if (len == 0)
continue;
- if (alias[len-1] == '\n')
- alias[len-1] = 0;
+ if (alias[len - 1] == '\n')
+ alias[len - 1] = 0;
err = kmod_module_new_from_lookup(ctx, alias, &list);
if (err < 0)
size_t len;
char *dupname = NULL;
- if(!(fgets(name, sizeof(name), f)))
+ if (!(fgets(name, sizeof(name), f)))
continue;
len = strlen(name);
if (len == 0)
continue;
- if (name[len-1] == '\n')
- name[len-1] = 0;
+ if (name[len - 1] == '\n')
+ name[len - 1] = 0;
log_debug("Adding module '%s' to hostonly module list", name);
dupname = strdup(name);
log_error("ERROR: installing module '%s'", modname);
return -ENOENT;
};
- ret = ( ret == 0 ? 0 : r );
+ ret = (ret == 0 ? 0 : r);
modinst = 1;
}
} else if (argv[i][0] == '=') {
_cleanup_fts_close_ FTS *fts = NULL;
log_debug("Handling =%s", &argv[i][1]);
- /* FIXME and add more paths*/
+ /* FIXME and add more paths */
r = asprintf(&path2, "%s/kernel/%s", kerneldir, &argv[i][1]);
if (r < 0) {
log_error("Out of memory!");
{
char *paths[] = { path1, path2, path3, NULL };
- fts = fts_open(paths, FTS_COMFOLLOW|FTS_NOCHDIR|FTS_NOSTAT|FTS_LOGICAL, NULL);
+ fts = fts_open(paths, FTS_COMFOLLOW | FTS_NOCHDIR | FTS_NOSTAT | FTS_LOGICAL, NULL);
}
- for (FTSENT *ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
+ for (FTSENT * ftsent = fts_read(fts); ftsent != NULL; ftsent = fts_read(fts)) {
_cleanup_kmod_module_unref_list_ struct kmod_list *modlist = NULL;
_cleanup_free_ const char *modname = NULL;
- if((ftsent->fts_info == FTS_D) && !check_module_path(ftsent->fts_accpath)) {
+ if ((ftsent->fts_info == FTS_D) && !check_module_path(ftsent->fts_accpath)) {
fts_set(fts, ftsent, FTS_SKIP);
log_debug("Skipping %s", ftsent->fts_accpath);
continue;
}
- if((ftsent->fts_info != FTS_F) && (ftsent->fts_info != FTS_SL)) {
+ if ((ftsent->fts_info != FTS_F) && (ftsent->fts_info != FTS_SL)) {
log_debug("Ignoring %s", ftsent->fts_accpath);
continue;
}
log_debug("Handling %s", ftsent->fts_accpath);
r = kmod_module_new_from_path(ctx, ftsent->fts_accpath, &mod_o);
if (r < 0) {
- log_debug("Failed to lookup modules path '%s': %m",
- ftsent->fts_accpath);
+ log_debug("Failed to lookup modules path '%s': %m", ftsent->fts_accpath);
if (!arg_optional) {
return -ENOENT;
}
}
if (!modlist) {
- log_error("Failed to find module '%s' %s", modname,
- ftsent->fts_accpath);
+ log_error("Failed to find module '%s' %s", modname, ftsent->fts_accpath);
if (!arg_optional) {
return -ENOENT;
}
log_error("ERROR: installing module '%s'", modname);
return -ENOENT;
};
- ret = ( ret == 0 ? 0 : r );
+ ret = (ret == 0 ? 0 : r);
modinst = 1;
}
}
if (endswith(modname, ".ko")) {
int len = strlen(modname);
- modname[len-3]=0;
+ modname[len - 3] = 0;
}
if (endswith(modname, ".ko.xz") || endswith(modname, ".ko.gz")) {
int len = strlen(modname);
- modname[len-6]=0;
+ modname[len - 6] = 0;
}
r = kmod_module_new_from_lookup(ctx, modname, &modlist);
if (r < 0) {
log_error("ERROR: installing '%s'", argv[i]);
return -ENOENT;
};
- ret = ( ret == 0 ? 0 : r );
+ ret = (ret == 0 ? 0 : r);
modinst = 1;
}
}
#define BY_HASH(h) ((struct hashmap_entry**) ((uint8_t*) (h) + ALIGN(sizeof(Hashmap))))
-unsigned string_hash_func(const void *p) {
+unsigned string_hash_func(const void *p)
+{
unsigned hash = 5381;
const signed char *c;
/* DJB's hash function */
for (c = p; *c; c++)
- hash = (hash << 5) + hash + (unsigned) *c;
+ hash = (hash << 5) + hash + (unsigned)*c;
return hash;
}
-int string_compare_func(const void *a, const void *b) {
+int string_compare_func(const void *a, const void *b)
+{
return strcmp(a, b);
}
-unsigned trivial_hash_func(const void *p) {
+unsigned trivial_hash_func(const void *p)
+{
return PTR_TO_UINT(p);
}
-int trivial_compare_func(const void *a, const void *b) {
+int trivial_compare_func(const void *a, const void *b)
+{
return a < b ? -1 : (a > b ? 1 : 0);
}
-Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) {
+Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func)
+{
Hashmap *h;
size_t size;
- size = ALIGN(sizeof(Hashmap)) + NBUCKETS * sizeof(struct hashmap_entry*);
+ size = ALIGN(sizeof(Hashmap)) + NBUCKETS * sizeof(struct hashmap_entry *);
h = malloc0(size);
return h;
}
-int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func) {
+int hashmap_ensure_allocated(Hashmap ** h, hash_func_t hash_func, compare_func_t compare_func)
+{
assert(h);
if (*h)
return 0;
}
-static void link_entry(Hashmap *h, struct hashmap_entry *e, unsigned hash) {
+static void link_entry(Hashmap * h, struct hashmap_entry *e, unsigned hash)
+{
assert(h);
assert(e);
assert(h->n_entries >= 1);
}
-static void unlink_entry(Hashmap *h, struct hashmap_entry *e, unsigned hash) {
+static void unlink_entry(Hashmap * h, struct hashmap_entry *e, unsigned hash)
+{
assert(h);
assert(e);
h->n_entries--;
}
-static void remove_entry(Hashmap *h, struct hashmap_entry **ep) {
+static void remove_entry(Hashmap * h, struct hashmap_entry **ep)
+{
struct hashmap_entry *e = *ep;
unsigned hash;
*ep = NULL;
}
-void hashmap_free(Hashmap*h) {
+void hashmap_free(Hashmap * h)
+{
if (!h)
return;
free(h);
}
-void hashmap_free_free(Hashmap *h) {
+void hashmap_free_free(Hashmap * h)
+{
void *p;
while ((p = hashmap_steal_first(h)))
hashmap_free(h);
}
-void hashmap_clear(Hashmap *h) {
+void hashmap_clear(Hashmap * h)
+{
if (!h)
return;
}
}
-static struct hashmap_entry *hash_scan(Hashmap *h, unsigned hash, const void *key) {
+static struct hashmap_entry *hash_scan(Hashmap * h, unsigned hash, const void *key)
+{
struct hashmap_entry *e;
assert(h);
assert(hash < NBUCKETS);
return NULL;
}
-int hashmap_put(Hashmap *h, const void *key, void *value) {
+int hashmap_put(Hashmap * h, const void *key, void *value)
+{
struct hashmap_entry *e;
unsigned hash;
return 1;
}
-int hashmap_replace(Hashmap *h, const void *key, void *value) {
+int hashmap_replace(Hashmap * h, const void *key, void *value)
+{
struct hashmap_entry *e;
unsigned hash;
return hashmap_put(h, key, value);
}
-void* hashmap_get(Hashmap *h, const void *key) {
+void *hashmap_get(Hashmap * h, const void *key)
+{
unsigned hash;
struct hashmap_entry *e;
return e->value;
}
-void* hashmap_remove(Hashmap *h, const void *key) {
+void *hashmap_remove(Hashmap * h, const void *key)
+{
struct hashmap_entry *e;
unsigned hash;
void *data;
return data;
}
-int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value) {
+int hashmap_remove_and_put(Hashmap * h, const void *old_key, const void *new_key, void *value)
+{
struct hashmap_entry *e;
unsigned old_hash, new_hash;
return 0;
}
-int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value) {
+int hashmap_remove_and_replace(Hashmap * h, const void *old_key, const void *new_key, void *value)
+{
struct hashmap_entry *e, *k;
unsigned old_hash, new_hash;
return 0;
}
-void* hashmap_remove_value(Hashmap *h, const void *key, void *value) {
+void *hashmap_remove_value(Hashmap * h, const void *key, void *value)
+{
struct hashmap_entry *e;
unsigned hash;
return value;
}
-void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key) {
+void *hashmap_iterate(Hashmap * h, Iterator * i, const void **key)
+{
struct hashmap_entry *e;
assert(i);
if (*i == ITERATOR_FIRST && !h->iterate_list_head)
goto at_end;
- e = *i == ITERATOR_FIRST ? h->iterate_list_head : (struct hashmap_entry*) *i;
+ e = *i == ITERATOR_FIRST ? h->iterate_list_head : (struct hashmap_entry *)*i;
if (e->iterate_next)
*i = (Iterator) e->iterate_next;
return e->value;
-at_end:
+ at_end:
*i = ITERATOR_LAST;
if (key)
return NULL;
}
-void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key) {
+void *hashmap_iterate_backwards(Hashmap * h, Iterator * i, const void **key)
+{
struct hashmap_entry *e;
assert(i);
if (*i == ITERATOR_LAST && !h->iterate_list_tail)
goto at_beginning;
- e = *i == ITERATOR_LAST ? h->iterate_list_tail : (struct hashmap_entry*) *i;
+ e = *i == ITERATOR_LAST ? h->iterate_list_tail : (struct hashmap_entry *)*i;
if (e->iterate_previous)
*i = (Iterator) e->iterate_previous;
return e->value;
-at_beginning:
+ at_beginning:
*i = ITERATOR_FIRST;
if (key)
return NULL;
}
-void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i) {
+void *hashmap_iterate_skip(Hashmap * h, const void *key, Iterator * i)
+{
unsigned hash;
struct hashmap_entry *e;
return e->value;
}
-void* hashmap_first(Hashmap *h) {
+void *hashmap_first(Hashmap * h)
+{
if (!h)
return NULL;
return h->iterate_list_head->value;
}
-void* hashmap_first_key(Hashmap *h) {
+void *hashmap_first_key(Hashmap * h)
+{
if (!h)
return NULL;
if (!h->iterate_list_head)
return NULL;
- return (void*) h->iterate_list_head->key;
+ return (void *)h->iterate_list_head->key;
}
-void* hashmap_last(Hashmap *h) {
+void *hashmap_last(Hashmap * h)
+{
if (!h)
return NULL;
return h->iterate_list_tail->value;
}
-void* hashmap_steal_first(Hashmap *h) {
+void *hashmap_steal_first(Hashmap * h)
+{
struct hashmap_entry *e;
void *data;
return data;
}
-void* hashmap_steal_first_key(Hashmap *h) {
+void *hashmap_steal_first_key(Hashmap * h)
+{
struct hashmap_entry *e;
void *key;
return NULL;
e = h->iterate_list_head;
- key = (void*) e->key;
+ key = (void *)e->key;
remove_entry(h, &e);
return key;
}
-unsigned hashmap_size(Hashmap *h) {
+unsigned hashmap_size(Hashmap * h)
+{
if (!h)
return 0;
return h->n_entries;
}
-bool hashmap_isempty(Hashmap *h) {
+bool hashmap_isempty(Hashmap * h)
+{
if (!h)
return true;
return h->n_entries == 0;
}
-int hashmap_merge(Hashmap *h, Hashmap *other) {
+int hashmap_merge(Hashmap * h, Hashmap * other)
+{
struct hashmap_entry *e;
assert(h);
return 0;
}
-void hashmap_move(Hashmap *h, Hashmap *other) {
+void hashmap_move(Hashmap * h, Hashmap * other)
+{
struct hashmap_entry *e, *n;
assert(h);
}
}
-int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key) {
+int hashmap_move_one(Hashmap * h, Hashmap * other, const void *key)
+{
unsigned h_hash, other_hash;
struct hashmap_entry *e;
return 0;
}
-char **hashmap_get_strv(Hashmap *h) {
+char **hashmap_get_strv(Hashmap * h)
+{
char **sv;
Iterator it;
char *item;
int n;
- sv = new(char*, h->n_entries+1);
+ sv = new(char *, h->n_entries + 1);
if (!sv)
return NULL;
n = 0;
HASHMAP_FOREACH(item, h, it)
- sv[n++] = item;
+ sv[n++] = item;
sv[n] = NULL;
return sv;
typedef struct Hashmap Hashmap;
typedef struct _IteratorStruct _IteratorStruct;
-typedef _IteratorStruct* Iterator;
+typedef _IteratorStruct *Iterator;
#define ITERATOR_FIRST ((Iterator) 0)
#define ITERATOR_LAST ((Iterator) -1)
int trivial_compare_func(const void *a, const void *b);
Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func);
-void hashmap_free(Hashmap *h);
-void hashmap_free_free(Hashmap *h);
-int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func);
-
-int hashmap_put(Hashmap *h, const void *key, void *value);
-int hashmap_replace(Hashmap *h, const void *key, void *value);
-void* hashmap_get(Hashmap *h, const void *key);
-void* hashmap_remove(Hashmap *h, const void *key);
-void* hashmap_remove_value(Hashmap *h, const void *key, void *value);
-int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value);
-int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value);
-
-int hashmap_merge(Hashmap *h, Hashmap *other);
-void hashmap_move(Hashmap *h, Hashmap *other);
-int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key);
-
-unsigned hashmap_size(Hashmap *h);
-bool hashmap_isempty(Hashmap *h);
-
-void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key);
-void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key);
-void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i);
-
-void hashmap_clear(Hashmap *h);
-void *hashmap_steal_first(Hashmap *h);
-void *hashmap_steal_first_key(Hashmap *h);
-void* hashmap_first(Hashmap *h);
-void* hashmap_first_key(Hashmap *h);
-void* hashmap_last(Hashmap *h);
-
-char **hashmap_get_strv(Hashmap *h);
+void hashmap_free(Hashmap * h);
+void hashmap_free_free(Hashmap * h);
+int hashmap_ensure_allocated(Hashmap ** h, hash_func_t hash_func, compare_func_t compare_func);
+
+int hashmap_put(Hashmap * h, const void *key, void *value);
+int hashmap_replace(Hashmap * h, const void *key, void *value);
+void *hashmap_get(Hashmap * h, const void *key);
+void *hashmap_remove(Hashmap * h, const void *key);
+void *hashmap_remove_value(Hashmap * h, const void *key, void *value);
+int hashmap_remove_and_put(Hashmap * h, const void *old_key, const void *new_key, void *value);
+int hashmap_remove_and_replace(Hashmap * h, const void *old_key, const void *new_key, void *value);
+
+int hashmap_merge(Hashmap * h, Hashmap * other);
+void hashmap_move(Hashmap * h, Hashmap * other);
+int hashmap_move_one(Hashmap * h, Hashmap * other, const void *key);
+
+unsigned hashmap_size(Hashmap * h);
+bool hashmap_isempty(Hashmap * h);
+
+void *hashmap_iterate(Hashmap * h, Iterator * i, const void **key);
+void *hashmap_iterate_backwards(Hashmap * h, Iterator * i, const void **key);
+void *hashmap_iterate_skip(Hashmap * h, const void *key, Iterator * i);
+
+void hashmap_clear(Hashmap * h);
+void *hashmap_steal_first(Hashmap * h);
+void *hashmap_steal_first_key(Hashmap * h);
+void *hashmap_first(Hashmap * h);
+void *hashmap_first_key(Hashmap * h);
+void *hashmap_last(Hashmap * h);
+
+char **hashmap_get_strv(Hashmap * h);
#define HASHMAP_FOREACH(e, h, i) \
for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); (e); (e) = hashmap_iterate((h), &(i), NULL))
* use here. */
static char *log_abort_msg = NULL;
-void log_close_console(void) {
+void log_close_console(void)
+{
if (console_fd < 0)
return;
}
}
-static int log_open_console(void) {
+static int log_open_console(void)
+{
if (console_fd >= 0)
return 0;
if (getpid() == 1) {
- console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
+ console_fd = open_terminal("/dev/console", O_WRONLY | O_NOCTTY | O_CLOEXEC);
if (console_fd < 0) {
log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd));
return console_fd;
return 0;
}
-
-int log_open(void) {
+int log_open(void)
+{
return log_open_console();
}
-
-void log_close(void) {
+void log_close(void)
+{
log_close_console();
}
-
-void log_set_max_level(int level) {
+void log_set_max_level(int level)
+{
assert((level & LOG_PRIMASK) == level);
log_max_level = level;
}
-void log_set_facility(int facility) {
+void log_set_facility(int facility)
+{
log_facility = facility;
}
-static int write_to_console(
- int level,
- const char*file,
- unsigned int line,
- const char *func,
- const char *buffer) {
+static int write_to_console(int level, const char *file, unsigned int line, const char *func, const char *buffer)
+{
struct iovec iovec[5];
unsigned n = 0;
return 1;
}
-static int log_dispatch(
- int level,
- const char*file,
- int line,
- const char *func,
- char *buffer) {
+static int log_dispatch(int level, const char *file, int line, const char *func, char *buffer)
+{
int r = 0;
return r;
}
-int log_metav(
- int level,
- const char*file,
- int line,
- const char *func,
- const char *format,
- va_list ap) {
+int log_metav(int level, const char *file, int line, const char *func, const char *format, va_list ap)
+{
char buffer[LINE_MAX];
int saved_errno, r;
return r;
}
-int log_meta(
- int level,
- const char*file,
- int line,
- const char *func,
- const char *format, ...) {
+int log_meta(int level, const char *file, int line, const char *func, const char *format, ...)
+{
int r;
va_list ap;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
-_noreturn_ static void log_assert(const char *text, const char *file, int line, const char *func, const char *format) {
+_noreturn_ static void log_assert(const char *text, const char *file, int line, const char *func, const char *format)
+{
static char buffer[LINE_MAX];
snprintf(buffer, sizeof(buffer), format, text, file, line, func);
log_dispatch(LOG_CRIT, file, line, func, buffer);
abort();
}
+
#pragma GCC diagnostic pop
-_noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func) {
+_noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func)
+{
log_assert(text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
}
-_noreturn_ void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
+_noreturn_ void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func)
+{
log_assert(text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
}
-void log_set_target(LogTarget target) {
+void log_set_target(LogTarget target)
+{
assert(target >= 0);
assert(target < _LOG_TARGET_MAX);
log_target = target;
}
-int log_set_target_from_string(const char *e) {
+int log_set_target_from_string(const char *e)
+{
LogTarget t;
t = log_target_from_string(e);
return 0;
}
-int log_set_max_level_from_string(const char *e) {
+int log_set_max_level_from_string(const char *e)
+{
int t;
t = log_level_from_string(e);
return 0;
}
-void log_parse_environment(void) {
+void log_parse_environment(void)
+{
const char *e;
if ((e = getenv("DRACUT_INSTALL_LOG_TARGET"))) {
}
}
-LogTarget log_get_target(void) {
+LogTarget log_get_target(void)
+{
return log_target;
}
-int log_get_max_level(void) {
+int log_get_max_level(void)
+{
return log_max_level;
}
-
static const char *const log_target_table[] = {
[LOG_TARGET_CONSOLE] = "console",
[LOG_TARGET_AUTO] = "auto",
#include "macro.h"
-typedef enum LogTarget{
+typedef enum LogTarget {
LOG_TARGET_CONSOLE,
LOG_TARGET_KMSG,
LOG_TARGET_JOURNAL,
LOG_TARGET_JOURNAL_OR_KMSG,
LOG_TARGET_SYSLOG,
LOG_TARGET_SYSLOG_OR_KMSG,
- LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */
- LOG_TARGET_SAFE, /* console if stderr is tty, KMSG otherwise */
+ LOG_TARGET_AUTO, /* console if stderr is tty, JOURNAL_OR_KMSG otherwise */
+ LOG_TARGET_SAFE, /* console if stderr is tty, KMSG otherwise */
LOG_TARGET_NULL,
_LOG_TARGET_MAX,
_LOG_TARGET_INVALID = -1
-} LogTarget;
+} LogTarget;
void log_set_target(LogTarget target);
void log_set_max_level(int level);
void log_parse_environment(void);
-int log_meta(
- int level,
- const char*file,
- int line,
- const char *func,
- const char *format, ...) _printf_attr_(5,6);
-
-int log_metav(
- int level,
- const char*file,
- int line,
- const char *func,
- const char *format,
- va_list ap);
+int log_meta(int level, const char *file, int line, const char *func, const char *format, ...) _printf_attr_(5, 6);
+
+int log_metav(int level, const char *file, int line, const char *func, const char *format, va_list ap);
_noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func);
_noreturn_ void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func);
/* This modifies the buffer passed! */
-int log_dump_internal(
- int level,
- const char*file,
- int line,
- const char *func,
- char *buffer);
+int log_dump_internal(int level, const char *file, int line, const char *func, char *buffer);
#define log_full(level, ...) log_meta(level, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define ALIGN4_PTR(p) ((void*) ALIGN4((unsigned long) p))
#define ALIGN8_PTR(p) ((void*) ALIGN8((unsigned long) p))
-static inline size_t ALIGN_TO(size_t l, size_t ali) {
+static inline size_t ALIGN_TO(size_t l, size_t ali)
+{
return ((l + ali - 1) & ~(ali - 1));
}
_i->iov_len = strlen(_s); \
} while(false)
-static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n) {
+static inline size_t IOVEC_TOTAL_SIZE(const struct iovec *i, unsigned n)
+{
unsigned j;
size_t r = 0;
return r;
}
-static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
+static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k)
+{
unsigned j;
for (j = 0; j < n; j++) {
sub = MIN(i[j].iov_len, k);
i[j].iov_len -= sub;
- i[j].iov_base = (uint8_t*) i[j].iov_base + sub;
+ i[j].iov_base = (uint8_t *) i[j].iov_base + sub;
k -= sub;
}
/* Because statfs.t_type can be int on some architecures, we have to cast
* the const magic to the type, otherwise the compiler warns about
* signed/unsigned comparison, because the magic can be 32 bit unsigned.
- */
+ */
#define F_TYPE_CMP(a, b) (a == (typeof(a)) b)
-
/* Returns the number of chars needed to format variables of the
* specified type as a decimal string. Adds in extra space for a
* negative '-' prefix. */
#include "util.h"
#include "strv.h"
-char *strv_find(char **l, const char *name) {
+char *strv_find(char **l, const char *name)
+{
char **i;
assert(name);
STRV_FOREACH(i, l)
- if (streq(*i, name))
- return *i;
+ if (streq(*i, name))
+ return *i;
return NULL;
}
-char *strv_find_prefix(char **l, const char *name) {
+char *strv_find_prefix(char **l, const char *name)
+{
char **i;
assert(name);
STRV_FOREACH(i, l)
- if (startswith(*i, name))
- return *i;
+ if (startswith(*i, name))
+ return *i;
return NULL;
}
-void strv_free(char **l) {
+void strv_free(char **l)
+{
char **k;
if (!l)
free(l);
}
-char **strv_copy(char * const *l) {
+char **strv_copy(char *const *l)
+{
char **r, **k;
- k = r = new(char*, strv_length(l) + 1);
+ k = r = new(char *, strv_length(l) + 1);
if (!r)
return NULL;
return r;
}
-unsigned int strv_length(char * const *l) {
+unsigned int strv_length(char *const *l)
+{
unsigned n = 0;
if (!l)
return n;
}
-char **strv_new_ap(const char *x, va_list ap) {
+char **strv_new_ap(const char *x, va_list ap)
+{
const char *s;
char **a;
unsigned n = 0, i = 0;
* the string list. */
if (x) {
- n = x == (const char*) -1 ? 0 : 1;
+ n = x == (const char *)-1 ? 0 : 1;
va_copy(aq, ap);
- while ((s = va_arg(aq, const char*))) {
- if (s == (const char*) -1)
+ while ((s = va_arg(aq, const char *))) {
+ if (s == (const char *)-1)
continue;
n++;
va_end(aq);
}
- a = new(char*, n+1);
+ a = new(char *, n + 1);
if (!a)
return NULL;
if (x) {
- if (x != (const char*) -1) {
+ if (x != (const char *)-1) {
a[i] = strdup(x);
if (!a[i])
goto fail;
i++;
}
- while ((s = va_arg(ap, const char*))) {
+ while ((s = va_arg(ap, const char *))) {
- if (s == (const char*) -1)
+ if (s == (const char *)-1)
continue;
a[i] = strdup(s);
return a;
-fail:
+ fail:
strv_free(a);
return NULL;
}
-char **strv_new(const char *x, ...) {
+char **strv_new(const char *x, ...)
+{
char **r;
va_list ap;
return r;
}
-char **strv_merge(char **a, char **b) {
+char **strv_merge(char **a, char **b)
+{
char **r, **k;
if (!a)
if (!b)
return strv_copy(a);
- r = new(char*, strv_length(a) + strv_length(b) + 1);
+ r = new(char *, strv_length(a) + strv_length(b) + 1);
if (!r)
return NULL;
*k = NULL;
return r;
-fail:
+ fail:
strv_free(r);
return NULL;
}
-char **strv_merge_concat(char **a, char **b, const char *suffix) {
+char **strv_merge_concat(char **a, char **b, const char *suffix)
+{
char **r, **k;
/* Like strv_merge(), but appends suffix to all strings in b, before adding */
if (!b)
return strv_copy(a);
- r = new(char*, strv_length(a) + strv_length(b) + 1);
+ r = new(char *, strv_length(a) + strv_length(b) + 1);
if (!r)
return NULL;
*k = NULL;
return r;
-fail:
+ fail:
strv_free(r);
return NULL;
}
-char **strv_split(const char *s, const char *separator) {
+char **strv_split(const char *s, const char *separator)
+{
char *state;
char *w;
size_t l;
n = 0;
FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
- n++;
+ n++;
- r = new(char*, n+1);
+ r = new(char *, n + 1);
if (!r)
return NULL;
return r;
}
-char **strv_split_quoted(const char *s) {
+char **strv_split_quoted(const char *s)
+{
char *state;
char *w;
size_t l;
n = 0;
FOREACH_WORD_QUOTED(w, l, s, state)
- n++;
+ n++;
- r = new(char*, n+1);
+ r = new(char *, n + 1);
if (!r)
return NULL;
return r;
}
-char **strv_split_newlines(const char *s) {
+char **strv_split_newlines(const char *s)
+{
char **l;
unsigned int n;
if (n == 0)
return l;
- if (isempty(l[n-1])) {
- free(l[n-1]);
- l[n-1] = NULL;
+ if (isempty(l[n - 1])) {
+ free(l[n - 1]);
+ l[n - 1] = NULL;
}
return l;
}
-char *strv_join(char **l, const char *separator) {
+char *strv_join(char **l, const char *separator)
+{
char *r, *e;
char **s;
size_t n, k;
n += strlen(*s);
}
- r = new(char, n+1);
+ r = new(char, n + 1);
if (!r)
return NULL;
return r;
}
-char **strv_append(char **l, const char *s) {
+char **strv_append(char **l, const char *s)
+{
char **r, **k;
if (!l)
if (!s)
return strv_copy(l);
- r = new(char*, strv_length(l)+2);
+ r = new(char *, strv_length(l) + 2);
if (!r)
return NULL;
k[1] = NULL;
return r;
-fail:
+ fail:
strv_free(r);
return NULL;
}
-int strv_push(char ***l, char *value) {
+int strv_push(char ***l, char *value)
+{
char **c;
unsigned n;
return 0;
n = strv_length(*l);
- c = realloc(*l, sizeof(char*) * (n + 2));
+ c = realloc(*l, sizeof(char *) * (n + 2));
if (!c)
return -ENOMEM;
c[n] = value;
- c[n+1] = NULL;
+ c[n + 1] = NULL;
*l = c;
return 0;
}
-int strv_extend(char ***l, const char *value) {
+int strv_extend(char ***l, const char *value)
+{
char *v;
int r;
return r;
}
-char **strv_uniq(char **l) {
+char **strv_uniq(char **l)
+{
char **i;
/* Drops duplicate entries. The first identical string will be
* kept, the others dropped */
STRV_FOREACH(i, l)
- strv_remove(i+1, *i);
+ strv_remove(i + 1, *i);
return l;
}
-char **strv_remove(char **l, const char *s) {
+char **strv_remove(char **l, const char *s)
+{
char **f, **t;
if (!l)
return l;
}
-char **strv_remove_prefix(char **l, const char *s) {
+char **strv_remove_prefix(char **l, const char *s)
+{
char **f, **t;
if (!l)
return l;
}
-char **strv_parse_nulstr(const char *s, size_t l) {
+char **strv_parse_nulstr(const char *s, size_t l)
+{
const char *p;
unsigned c = 0, i = 0;
char **v;
assert(s || l == 0);
if (l == 0)
- return new0(char*, 1);
+ return new0(char *, 1);
for (p = s; p < s + l; p++)
if (*p == 0)
c++;
- if (s[l-1] != 0)
+ if (s[l - 1] != 0)
c++;
- v = new0(char*, c+1);
+ v = new0(char *, c + 1);
if (!v)
return NULL;
return v;
}
-char **strv_split_nulstr(const char *s) {
+char **strv_split_nulstr(const char *s)
+{
const char *i;
char **r = NULL;
NULSTR_FOREACH(i, s)
- if (strv_extend(&r, i) < 0) {
- strv_free(r);
- return NULL;
- }
+ if (strv_extend(&r, i) < 0) {
+ strv_free(r);
+ return NULL;
+ }
if (!r)
return strv_new(NULL, NULL);
return r;
}
-bool strv_overlap(char **a, char **b) {
+bool strv_overlap(char **a, char **b)
+{
char **i, **j;
STRV_FOREACH(i, a) {
return false;
}
-static int str_compare(const void *_a, const void *_b) {
- const char **a = (const char**) _a, **b = (const char**) _b;
+static int str_compare(const void *_a, const void *_b)
+{
+ const char **a = (const char **)_a, **b = (const char **)_b;
return strcmp(*a, *b);
}
-char **strv_sort(char **l) {
+char **strv_sort(char **l)
+{
if (strv_isempty(l))
return l;
- qsort(l, strv_length(l), sizeof(char*), str_compare);
+ qsort(l, strv_length(l), sizeof(char *), str_compare);
return l;
}
-void strv_print(char **l) {
+void strv_print(char **l)
+{
char **s;
if (!l)
return;
STRV_FOREACH(s, l)
- puts(*s);
+ puts(*s);
}
char *strv_find_prefix(char **l, const char *name) _pure_;
void strv_free(char **l);
-DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
+DEFINE_TRIVIAL_CLEANUP_FUNC(char **, strv_free);
#define _cleanup_strv_free_ _cleanup_(strv_freep)
-char **strv_copy(char * const *l);
-unsigned int strv_length(char * const *l) _pure_;
+char **strv_copy(char *const *l);
+unsigned int strv_length(char *const *l) _pure_;
char **strv_merge(char **a, char **b);
char **strv_merge_concat(char **a, char **b, const char *suffix);
char **strv_new(const char *x, ...) _sentinel_;
char **strv_new_ap(const char *x, va_list ap);
-static inline const char* STRV_IFNOTNULL(const char *x) {
- return x ? x : (const char *) -1;
+static inline const char *STRV_IFNOTNULL(const char *x)
+{
+ return x ? x : (const char *)-1;
}
-static inline bool strv_isempty(char * const *l) {
+static inline bool strv_isempty(char *const *l)
+{
return !l || !*l;
}
#include "util.h"
-static inline pid_t gettid(void) {
+static inline pid_t gettid(void)
+{
return (pid_t) syscall(SYS_gettid);
}
-size_t page_size(void) {
+size_t page_size(void)
+{
static __thread size_t pgsz = 0;
long r;
assert_se((r = sysconf(_SC_PAGESIZE)) > 0);
- pgsz = (size_t) r;
+ pgsz = (size_t)r;
return pgsz;
}
-bool endswith(const char *s, const char *postfix) {
+bool endswith(const char *s, const char *postfix)
+{
size_t sl, pl;
assert(s);
return memcmp(s + sl - pl, postfix, pl) == 0;
}
-int close_nointr(int fd) {
+
+int close_nointr(int fd)
+{
assert(fd >= 0);
for (;;) {
}
}
-void close_nointr_nofail(int fd) {
+void close_nointr_nofail(int fd)
+{
int saved_errno = errno;
/* like close_nointr() but cannot fail, and guarantees errno
errno = saved_errno;
}
-int open_terminal(const char *name, int mode) {
+int open_terminal(const char *name, int mode)
+{
int fd, r;
unsigned c = 0;
return fd;
}
-bool streq_ptr(const char *a, const char *b) {
+bool streq_ptr(const char *a, const char *b)
+{
/* Like streq(), but tries to make sense of NULL pointers */
return false;
}
-bool is_main_thread(void) {
+
+bool is_main_thread(void)
+{
static __thread int cached = 0;
if (_unlikely_(cached == 0))
- cached = getpid() == gettid() ? 1 : -1;
+ cached = getpid() == gettid()? 1 : -1;
return cached > 0;
}
-int safe_atou(const char *s, unsigned *ret_u) {
+int safe_atou(const char *s, unsigned *ret_u)
+{
char *x = NULL;
unsigned long l;
if (!x || *x || errno)
return errno ? -errno : -EINVAL;
- if ((unsigned long) (unsigned) l != l)
+ if ((unsigned long)(unsigned)l != l)
return -ERANGE;
- *ret_u = (unsigned) l;
+ *ret_u = (unsigned)l;
return 0;
}
DEFINE_STRING_TABLE_LOOKUP(log_level, int);
-char *strnappend(const char *s, const char *suffix, size_t b) {
+char *strnappend(const char *s, const char *suffix, size_t b)
+{
size_t a;
char *r;
assert(suffix);
a = strlen(s);
- if (b > ((size_t) -1) - a)
+ if (b > ((size_t)-1) - a)
return NULL;
- r = new(char, a+b+1);
+ r = new(char, a + b + 1);
if (!r)
return NULL;
memcpy(r, s, a);
- memcpy(r+a, suffix, b);
- r[a+b] = 0;
+ memcpy(r + a, suffix, b);
+ r[a + b] = 0;
return r;
}
-char *strappend(const char *s, const char *suffix) {
+char *strappend(const char *s, const char *suffix)
+{
return strnappend(s, suffix, suffix ? strlen(suffix) : 0);
}
-char *strjoin(const char *x, ...) {
+char *strjoin(const char *x, ...)
+{
va_list ap;
size_t l;
char *r;
break;
n = strlen(t);
- if (n > ((size_t) -1) - l) {
+ if (n > ((size_t)-1) - l) {
va_end(ap);
return NULL;
}
va_end(ap);
- r = new(char, l+1);
+ r = new(char, l + 1);
if (!r)
return NULL;
return r;
}
-char *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix) {
+char *cunescape_length_with_prefix(const char *s, size_t length, const char *prefix)
+{
char *r, *t;
const char *f;
size_t pl;
pl = prefix ? strlen(prefix) : 0;
- r = new(char, pl+length+1);
+ r = new(char, pl + length + 1);
if (!r)
return r;
*(t++) = ' ';
break;
- case 'x': {
- /* hexadecimal encoding */
- int a, b;
+ case 'x':{
+ /* hexadecimal encoding */
+ int a, b;
- a = unhexchar(f[1]);
- b = unhexchar(f[2]);
+ a = unhexchar(f[1]);
+ b = unhexchar(f[2]);
- if (a < 0 || b < 0) {
- /* Invalid escape code, let's take it literal then */
- *(t++) = '\\';
- *(t++) = 'x';
- } else {
- *(t++) = (char) ((a << 4) | b);
- f += 2;
- }
+ if (a < 0 || b < 0) {
+ /* Invalid escape code, let's take it literal then */
+ *(t++) = '\\';
+ *(t++) = 'x';
+ } else {
+ *(t++) = (char)((a << 4) | b);
+ f += 2;
+ }
- break;
- }
+ break;
+ }
case '0':
case '1':
case '4':
case '5':
case '6':
- case '7': {
- /* octal encoding */
- int a, b, c;
-
- a = unoctchar(f[0]);
- b = unoctchar(f[1]);
- c = unoctchar(f[2]);
-
- if (a < 0 || b < 0 || c < 0) {
- /* Invalid escape code, let's take it literal then */
- *(t++) = '\\';
- *(t++) = f[0];
- } else {
- *(t++) = (char) ((a << 6) | (b << 3) | c);
- f += 2;
- }
+ case '7':{
+ /* octal encoding */
+ int a, b, c;
+
+ a = unoctchar(f[0]);
+ b = unoctchar(f[1]);
+ c = unoctchar(f[2]);
+
+ if (a < 0 || b < 0 || c < 0) {
+ /* Invalid escape code, let's take it literal then */
+ *(t++) = '\\';
+ *(t++) = f[0];
+ } else {
+ *(t++) = (char)((a << 6) | (b << 3) | c);
+ f += 2;
+ }
- break;
- }
+ break;
+ }
case 0:
- /* premature end of string.*/
+ /* premature end of string. */
*(t++) = '\\';
goto finish;
}
}
-finish:
+ finish:
*t = 0;
return r;
}
-char *cunescape_length(const char *s, size_t length) {
+char *cunescape_length(const char *s, size_t length)
+{
return cunescape_length_with_prefix(s, length, NULL);
}
-
/* Split a string into words, but consider strings enclosed in '' and
* "" as words even if they include spaces. */
-char *split_quoted(const char *c, size_t *l, char **state) {
+char *split_quoted(const char *c, size_t *l, char **state)
+{
const char *current, *e;
bool escaped = false;
return NULL;
else if (*current == '\'') {
- current ++;
+ current++;
for (e = current; *e; e++) {
if (escaped)
break;
}
- *l = e-current;
- *state = (char*) (*e == 0 ? e : e+1);
+ *l = e - current;
+ *state = (char *)(*e == 0 ? e : e + 1);
} else if (*current == '\"') {
- current ++;
+ current++;
for (e = current; *e; e++) {
if (escaped)
break;
}
- *l = e-current;
- *state = (char*) (*e == 0 ? e : e+1);
+ *l = e - current;
+ *state = (char *)(*e == 0 ? e : e + 1);
} else {
for (e = current; *e; e++) {
else if (strchr(WHITESPACE, *e))
break;
}
- *l = e-current;
- *state = (char*) e;
+ *l = e - current;
+ *state = (char *)e;
}
- return (char*) current;
+ return (char *)current;
}
/* Split a string into words. */
-char *split(const char *c, size_t *l, const char *separator, char **state) {
+char *split(const char *c, size_t *l, const char *separator, char **state)
+{
char *current;
- current = *state ? *state : (char*) c;
+ current = *state ? *state : (char *)c;
if (!*current || *c == 0)
return NULL;
current += strspn(current, separator);
*l = strcspn(current, separator);
- *state = current+*l;
+ *state = current + *l;
- return (char*) current;
+ return (char *)current;
}
-int unhexchar(char c) {
+int unhexchar(char c)
+{
if (c >= '0' && c <= '9')
return c - '0';
return -1;
}
-int unoctchar(char c) {
+int unoctchar(char c)
+{
if (c >= '0' && c <= '7')
return c - '0';
usec_t now(clockid_t clock);
-dual_timestamp* dual_timestamp_get(dual_timestamp *ts);
-dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u);
+dual_timestamp *dual_timestamp_get(dual_timestamp * ts);
+dual_timestamp *dual_timestamp_from_realtime(dual_timestamp * ts, usec_t u);
#define dual_timestamp_is_set(ts) ((ts)->realtime > 0)
#define malloc0(n) (calloc((n), 1))
-static inline const char* yes_no(bool b) {
+static inline const char *yes_no(bool b)
+{
return b ? "yes" : "no";
}
-static inline const char* strempty(const char *s) {
+static inline const char *strempty(const char *s)
+{
return s ? s : "";
}
-static inline const char* strnull(const char *s) {
+static inline const char *strnull(const char *s)
+{
return s ? s : "(null)";
}
-static inline const char *strna(const char *s) {
+static inline const char *strna(const char *s)
+{
return s ? s : "n/a";
}
-static inline bool isempty(const char *p) {
+static inline bool isempty(const char *p)
+{
return !p || !p[0];
}
-
-static inline const char *startswith(const char *s, const char *prefix) {
+static inline const char *startswith(const char *s, const char *prefix)
+{
if (strncmp(s, prefix, strlen(prefix)) == 0)
return s + strlen(prefix);
return NULL;
bool endswith(const char *s, const char *postfix);
-
bool startswith_no_case(const char *s, const char *prefix);
bool first_word(const char *s, const char *word);
void close_many(const int fds[], unsigned n_fd);
int parse_boolean(const char *v);
-int parse_usec(const char *t, usec_t *usec);
-int parse_nsec(const char *t, nsec_t *nsec);
-int parse_bytes(const char *t, off_t *bytes);
-int parse_pid(const char *s, pid_t* ret_pid);
-int parse_uid(const char *s, uid_t* ret_uid);
+int parse_usec(const char *t, usec_t * usec);
+int parse_nsec(const char *t, nsec_t * nsec);
+int parse_bytes(const char *t, off_t * bytes);
+int parse_pid(const char *s, pid_t * ret_pid);
+int parse_uid(const char *s, uid_t * ret_uid);
#define parse_gid(s, ret_uid) parse_uid(s, ret_uid)
int safe_atou(const char *s, unsigned *ret_u);
int safe_atolli(const char *s, long long int *ret_i);
#if LONG_MAX == INT_MAX
-static inline int safe_atolu(const char *s, unsigned long *ret_u) {
+static inline int safe_atolu(const char *s, unsigned long *ret_u)
+{
assert_cc(sizeof(unsigned long) == sizeof(unsigned));
- return safe_atou(s, (unsigned*) ret_u);
+ return safe_atou(s, (unsigned *)ret_u);
}
-static inline int safe_atoli(const char *s, long int *ret_u) {
+
+static inline int safe_atoli(const char *s, long int *ret_u)
+{
assert_cc(sizeof(long int) == sizeof(int));
- return safe_atoi(s, (int*) ret_u);
+ return safe_atoi(s, (int *)ret_u);
}
#else
-static inline int safe_atolu(const char *s, unsigned long *ret_u) {
+static inline int safe_atolu(const char *s, unsigned long *ret_u)
+{
assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
- return safe_atollu(s, (unsigned long long*) ret_u);
+ return safe_atollu(s, (unsigned long long *)ret_u);
}
-static inline int safe_atoli(const char *s, long int *ret_u) {
+
+static inline int safe_atoli(const char *s, long int *ret_u)
+{
assert_cc(sizeof(long int) == sizeof(long long int));
- return safe_atolli(s, (long long int*) ret_u);
+ return safe_atolli(s, (long long int *)ret_u);
}
#endif
-static inline int safe_atou32(const char *s, uint32_t *ret_u) {
+static inline int safe_atou32(const char *s, uint32_t * ret_u)
+{
assert_cc(sizeof(uint32_t) == sizeof(unsigned));
- return safe_atou(s, (unsigned*) ret_u);
+ return safe_atou(s, (unsigned *)ret_u);
}
-static inline int safe_atoi32(const char *s, int32_t *ret_i) {
+static inline int safe_atoi32(const char *s, int32_t * ret_i)
+{
assert_cc(sizeof(int32_t) == sizeof(int));
- return safe_atoi(s, (int*) ret_i);
+ return safe_atoi(s, (int *)ret_i);
}
-static inline int safe_atou64(const char *s, uint64_t *ret_u) {
+static inline int safe_atou64(const char *s, uint64_t * ret_u)
+{
assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
- return safe_atollu(s, (unsigned long long*) ret_u);
+ return safe_atollu(s, (unsigned long long *)ret_u);
}
-static inline int safe_atoi64(const char *s, int64_t *ret_i) {
+static inline int safe_atoi64(const char *s, int64_t * ret_i)
+{
assert_cc(sizeof(int64_t) == sizeof(long long int));
- return safe_atolli(s, (long long int*) ret_i);
+ return safe_atolli(s, (long long int *)ret_i);
}
char *split(const char *c, size_t *l, const char *separator, char **state);
#define FOREACH_WORD_QUOTED(word, length, s, state) \
for ((state) = NULL, (word) = split_quoted((s), &(length), &(state)); (word); (word) = split_quoted((s), &(length), &(state)))
-pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
+pid_t get_parent_of_pid(pid_t pid, pid_t * ppid);
int get_starttime_of_pid(pid_t pid, unsigned long long *st);
int write_one_line_file(const char *fn, const char *line);
int get_process_comm(pid_t pid, char **name);
int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
int get_process_exe(pid_t pid, char **name);
-int get_process_uid(pid_t pid, uid_t *uid);
+int get_process_uid(pid_t pid, uid_t * uid);
char hexchar(int x);
int unhexchar(char c);
int chvt(int vt);
-int read_one_char(FILE *f, char *ret, usec_t timeout, bool *need_nl);
+int read_one_char(FILE * f, char *ret, usec_t timeout, bool *need_nl);
int ask(char *ret, const char *replies, const char *text, ...);
int reset_terminal_fd(int fd, bool switch_to_text);
int sigaction_many(const struct sigaction *sa, ...);
int close_pipe(int p[]);
-int fopen_temporary(const char *path, FILE **_f, char **_temp_path);
+int fopen_temporary(const char *path, FILE ** _f, char **_temp_path);
ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll);
ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll);
void rename_process(const char name[8]);
-void sigset_add_many(sigset_t *ss, ...);
+void sigset_add_many(sigset_t * ss, ...);
-char* gethostname_malloc(void);
+char *gethostname_malloc(void);
bool hostname_is_set(void);
-char* getlogname_malloc(void);
+char *getlogname_malloc(void);
int getttyname_malloc(int fd, char **r);
int getttyname_harder(int fd, char **r);
-int get_ctty_devnr(pid_t pid, dev_t *d);
-int get_ctty(pid_t, dev_t *_devnr, char **r);
+int get_ctty_devnr(pid_t pid, dev_t * d);
+int get_ctty(pid_t, dev_t * _devnr, char **r);
int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
int fchmod_and_fchown(int fd, mode_t mode, uid_t uid, gid_t gid);
int pipe_eof(int fd);
-cpu_set_t* cpu_set_malloc(unsigned *ncpus);
+cpu_set_t *cpu_set_malloc(unsigned *ncpus);
void status_vprintf(const char *status, bool ellipse, const char *format, va_list ap);
void status_printf(const char *status, bool ellipse, const char *format, ...);
char *unquote(const char *s, const char *quotes);
char *normalize_env_assignment(const char *s);
-int wait_for_terminate(pid_t pid, siginfo_t *status);
+int wait_for_terminate(pid_t pid, siginfo_t * status);
int wait_for_terminate_and_warn(const char *name, pid_t pid);
_noreturn_ void freeze(void);
DIR *xopendirat(int dirfd, const char *name, int flags);
-void dual_timestamp_serialize(FILE *f, const char *name, dual_timestamp *t);
-void dual_timestamp_deserialize(const char *value, dual_timestamp *t);
+void dual_timestamp_serialize(FILE * f, const char *name, dual_timestamp * t);
+void dual_timestamp_deserialize(const char *value, dual_timestamp * t);
char *fstab_node_to_udev_node(const char *p);
int vtnr_from_tty(const char *tty);
const char *default_term_for_tty(const char *tty);
-void execute_directory(const char *directory, DIR *_d, char *argv[]);
+void execute_directory(const char *directory, DIR * _d, char *argv[]);
int kill_and_sigcont(pid_t pid, int sig);
-bool nulstr_contains(const char*nulstr, const char *needle);
+bool nulstr_contains(const char *nulstr, const char *needle);
bool plymouth_running(void);
void skip_syslog_date(char **buf);
bool hostname_is_valid(const char *s);
-char* hostname_cleanup(char *s);
+char *hostname_cleanup(char *s);
-char* strshorten(char *s, size_t l);
+char *strshorten(char *s, size_t l);
int terminal_vhangup_fd(int fd);
int terminal_vhangup(const char *name);
bool display_is_local(const char *display);
int socket_from_display(const char *display, char **path);
-int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home);
-int get_group_creds(const char **groupname, gid_t *gid);
+int get_user_creds(const char **username, uid_t * uid, gid_t * gid, const char **home);
+int get_group_creds(const char **groupname, gid_t * gid);
int in_group(const char *name);
int glob_exists(const char *path);
-int dirent_ensure_type(DIR *d, struct dirent *de);
+int dirent_ensure_type(DIR * d, struct dirent *de);
int in_search_path(const char *path, char **search);
int get_files_in_directory(const char *path, char ***list);
bool is_main_thread(void);
-bool in_charset(const char *s, const char* charset);
+bool in_charset(const char *s, const char *charset);
-int block_get_whole_disk(dev_t d, dev_t *ret);
+int block_get_whole_disk(dev_t d, dev_t * ret);
int file_is_priv_sticky(const char *p);
int fd_wait_for_event(int fd, int event, usec_t timeout);
-void* memdup(const void *p, size_t l);
+void *memdup(const void *p, size_t l);
int is_kernel_thread(pid_t pid);
-static inline void freep(void *p) {
- free(*(void**) p);
+static inline void freep(void *p)
+{
+ free(*(void **)p);
}
-static inline void fclosep(FILE **f) {
+static inline void fclosep(FILE ** f)
+{
if (*f)
fclose(*f);
}
-static inline void pclosep(FILE **f) {
+static inline void pclosep(FILE ** f)
+{
if (*f)
pclose(*f);
}
-static inline void closep(int *fd) {
+static inline void closep(int *fd)
+{
if (*fd >= 0)
close_nointr_nofail(*fd);
}
-static inline void closedirp(DIR **d) {
+static inline void closedirp(DIR ** d)
+{
if (*d)
closedir(*d);
}
-static inline void umaskp(mode_t *u) {
+static inline void umaskp(mode_t * u)
+{
umask(*u);
}
int fd_inc_sndbuf(int fd, size_t n);
int fd_inc_rcvbuf(int fd, size_t n);
-int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
+int fork_agent(pid_t * pid, const int except[], unsigned n_except, const char *path, ...);
int setrlimit_closest(int resource, const struct rlimit *rlim);
#define BUFLEN 4096
-int
-main(int argc, char *argv[])
+int main(int argc, char *argv[])
{
- int fd;
- int len, slen;
- int ret;
- int timeout;
- char *timeout_env;
- struct pollfd fds[] = {{
- .fd = STDIN_FILENO,
- .events = POLLIN | POLLERR,
- }};
+ int fd;
+ int len, slen;
+ int ret;
+ int timeout;
+ char *timeout_env;
+ struct pollfd fds[] = { {
+ .fd = STDIN_FILENO,
+ .events = POLLIN | POLLERR,
+ }
+ };
- timeout_env = getenv("LOGTEE_TIMEOUT_MS");
- if (timeout_env)
- timeout = atoi(timeout_env);
- else
- timeout = -1;
+ timeout_env = getenv("LOGTEE_TIMEOUT_MS");
+ if (timeout_env)
+ timeout = atoi(timeout_env);
+ else
+ timeout = -1;
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <file>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <file>\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
- fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0644);
- if (fd == -1) {
- perror("open");
- exit(EXIT_FAILURE);
- }
+ fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, 0644);
+ if (fd == -1) {
+ perror("open");
+ exit(EXIT_FAILURE);
+ }
- fprintf(stderr, "Logging to %s: ", argv[1]);
+ fprintf(stderr, "Logging to %s: ", argv[1]);
- slen = 0;
+ slen = 0;
- do {
- ret = poll (fds, sizeof(fds) / sizeof(fds[0]), timeout);
- if (ret == 0) {
- fprintf (stderr, "Timed out after %d milliseconds of no output.\n", timeout);
- exit(EXIT_FAILURE);
- }
- len = splice(STDIN_FILENO, NULL, fd, NULL,
- BUFLEN, SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
+ do {
+ ret = poll(fds, sizeof(fds) / sizeof(fds[0]), timeout);
+ if (ret == 0) {
+ fprintf(stderr, "Timed out after %d milliseconds of no output.\n", timeout);
+ exit(EXIT_FAILURE);
+ }
+ len = splice(STDIN_FILENO, NULL, fd, NULL, BUFLEN, SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
- if (len < 0) {
- if (errno == EAGAIN)
- continue;
- perror("tee");
- exit(EXIT_FAILURE);
- } else
- if (len == 0)
- break;
- slen += len;
- if ((slen/BUFLEN) > 0) {
- fprintf(stderr, ".");
- }
- slen = slen % BUFLEN;
+ if (len < 0) {
+ if (errno == EAGAIN)
+ continue;
+ perror("tee");
+ exit(EXIT_FAILURE);
+ } else if (len == 0)
+ break;
+ slen += len;
+ if ((slen / BUFLEN) > 0) {
+ fprintf(stderr, ".");
+ }
+ slen = slen % BUFLEN;
- } while (1);
- close(fd);
- fprintf(stderr, "\n");
- exit(EXIT_SUCCESS);
+ } while (1);
+ close(fd);
+ fprintf(stderr, "\n");
+ exit(EXIT_SUCCESS);
}