*/
mnt_cache *mnt_new_cache(void)
{
- return calloc(1, sizeof(struct _mnt_cache));
+ mnt_cache *cache = calloc(1, sizeof(struct _mnt_cache));
+ if (!cache)
+ return NULL;
+ DBG(CACHE, mnt_debug_h(cache, "alloc"));
+ return cache;
}
/**
if (!cache)
return;
+
+ DBG(CACHE, mnt_debug_h(cache, "free"));
+
for (i = 0; i < cache->nents; i++) {
struct mnt_cache_entry *e = &cache->ents[i];
if (e->real != e->native)
e->flag = flag;
cache->nents++;
- DBG(DEBUG_CACHE,
- printf("cache: add entry[%2zd] (%s): %s: %s\n",
+ DBG(CACHE, mnt_debug_h(cache, "add entry [%2zd] (%s): %s: %s",
cache->nents,
(flag & MNT_CACHE_ISPATH) ? "path" : "tag",
real, native));
if (!cache || !devname)
return -EINVAL;
- DBG(DEBUG_CACHE, printf("cache: tags for %s requested\n", devname));
+ DBG(CACHE, mnt_debug_h(cache, "tags for %s requested", devname));
/* check is device is already cached */
for (i = 0; i < cache->nents; i++) {
return 0;
}
- DBG(DEBUG_CACHE,
- printf("cache: reading tags for: %s\n", devname));
+ DBG(CACHE, mnt_debug_h(cache, "reading tags for: %s", devname));
pr = blkid_new_probe_from_filename(devname);
if (!pr)
*/
#include <stdlib.h>
+#include <stdarg.h>
#include "mountP.h"
*/
void mnt_init_debug(int mask)
{
- if (libmount_debug_mask & DEBUG_INIT)
+ if (libmount_debug_mask & MNT_DEBUG_INIT)
return;
if (!mask) {
char *str = mnt_getenv_safe("LIBMOUNT_DEBUG");
if (libmount_debug_mask)
printf("libmount: debug mask set to 0x%04x.\n",
libmount_debug_mask);
- libmount_debug_mask |= DEBUG_INIT;
+ libmount_debug_mask |= MNT_DEBUG_INIT;
}
ml->lockfile_fd = -1;
ml->linkfile = ln;
ml->lockfile = lo;
+
+ DBG(LOCKS, mnt_debug_h(ml, "alloc"));
return ml;
err:
free(lo);
{
if (!ml)
return;
+ DBG(LOCKS, mnt_debug_h(ml, "free"));
free(ml->lockfile);
free(ml->linkfile);
free(ml);
sigaction(SIGALRM, &sa, &osa);
- DBG(DEBUG_LOCKS, fprintf(stderr,
- "LOCK: (%d) waiting for F_SETLKW.\n", getpid()));
+ DBG(LOCKS, mnt_debug_h(ml, "(%d) waiting for F_SETLKW", getpid()));
alarm(maxtime - now.tv_sec);
if (fcntl(ml->lockfile_fd, F_SETLKW, fl) == -1)
/* restore old sigaction */
sigaction(SIGALRM, &osa, NULL);
- DBG(DEBUG_LOCKS, fprintf(stderr,
- "LOCK: (%d) leaving mnt_wait_setlkw(), rc=%d.\n", getpid(), ret));
+ DBG(LOCKS, mnt_debug_h(ml, "(%d) leaving mnt_wait_setlkw(), rc=%d",
+ getpid(), ret));
return ret;
}
if (!ml)
return;
- DBG(DEBUG_LOCKS, fprintf(stderr,
- "LOCK: (%d) unlocking/cleaning.\n", getpid()));
+ DBG(LOCKS, mnt_debug_h(ml, "(%d) unlocking/cleaning", getpid()));
if (ml->locked == 0 && ml->lockfile && ml->linkfile)
{
if (ml->locked) {
/* We made the link. Now claim the lock. */
if (fcntl (ml->lockfile_fd, F_SETLK, &flock) == -1) {
- DBG(DEBUG_LOCKS, fprintf(stderr,
+ DBG(LOCKS, mnt_debug_h(ml,
"%s: can't F_SETLK lockfile, errno=%d\n",
lockfile, errno));
/* proceed, since it was us who created the lockfile anyway */
int err = mnt_wait_lock(ml, &flock, maxtime.tv_sec);
if (err == 1) {
- DBG(DEBUG_LOCKS, fprintf(stderr,
+ DBG(LOCKS, mnt_debug_h(ml,
"%s: can't create link: time out (perhaps "
"there is a stale lock file?)", lockfile));
rc = -ETIMEDOUT;
ml->lockfile_fd = -1;
}
}
- DBG(DEBUG_LOCKS, fprintf(stderr,
+ DBG(LOCKS, mnt_debug_h(ml,
"LOCK: %s: (%d) successfully locked\n",
lockfile, getpid()));
unlink(linkfile);
#define CONFIG_LIBMOUNT_DEBUG
#endif
-#define DEBUG_INIT (1 << 1)
-#define DEBUG_CACHE (1 << 2)
-#define DEBUG_OPTIONS (1 << 3)
-#define DEBUG_LOCKS (1 << 4)
-#define DEBUG_TAB (1 << 5)
-#define DEBUG_MTAB (1 << 6)
-#define DEBUG_UTILS (1 << 7)
-#define DEBUG_ALL 0xFFFF
+#define MNT_DEBUG_INIT (1 << 1)
+#define MNT_DEBUG_CACHE (1 << 2)
+#define MNT_DEBUG_OPTIONS (1 << 3)
+#define MNT_DEBUG_LOCKS (1 << 4)
+#define MNT_DEBUG_TAB (1 << 5)
+#define MNT_DEBUG_MTAB (1 << 6)
+#define MNT_DEBUG_UTILS (1 << 7)
+#define MNT_DEBUG_CXT (1 << 8)
+#define MNT_DEBUG_ALL 0xFFFF
#ifdef CONFIG_LIBMOUNT_DEBUG
-#include <stdio.h>
+# include <stdio.h>
+# include <stdarg.h>
+
+# define DBG(m,x) do { \
+ if ((MNT_DEBUG_ ## m) & libmount_debug_mask) {\
+ fprintf(stderr, "libmount: %s: ", # m); \
+ x; \
+ } \
+ } while(0)
+
extern int libmount_debug_mask;
-#define DBG(m,x) if ((m) & libmount_debug_mask) x
-#else
-#define DBG(m,x)
+
+static inline void mnt_debug(const char *mesg, ...)
+{
+ va_list ap;
+ va_start(ap, mesg);
+ vfprintf(stderr, mesg, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+}
+
+static inline void mnt_debug_h(void *handler, const char *mesg, ...)
+{
+ va_list ap;
+
+ fprintf(stderr, "[%p]: ", handler);
+ va_start(ap, mesg);
+ vfprintf(stderr, mesg, ap);
+ va_end(ap);
+ fputc('\n', stderr);
+}
+
+#else /* !CONFIG_LIBMOUNT_DEBUG */
+# define DBG(m,x)
#endif
/* extension for files in the /etc/fstab.d directory */
return 1; /* end of optstr */
error:
- DBG(DEBUG_OPTIONS, fprintf(stderr,
- "libmount: parse error: \"%s\"\n", optstr0));
+ DBG(OPTIONS, mnt_debug("parse error: \"%s\"", optstr0));
return -EINVAL;
}
}
} while(1);
- DBG(DEBUG_OPTIONS, fprintf(stderr,
- "libmount: can't found '%s' option\n", name));
+ DBG(OPTIONS, mnt_debug("can't found '%s' option", name));
return rc;
}
}
}
- DBG(DEBUG_OPTIONS, fprintf(stderr,
- "libmount: optstr '%s': mountflags 0x%08lx\n", optstr, *flags));
+ DBG(OPTIONS, mnt_debug("%s: mountflags 0x%08lx", optstr, *flags));
return 0;
}
if (!tb)
return NULL;
- DBG(DEBUG_TAB, fprintf(stderr, "libmount: new tab %p\n", tb));
+ DBG(TAB, mnt_debug_h(tb, "alloc"));
INIT_LIST_HEAD(&tb->ents);
return tb;
if (!tb)
return;
+ DBG(TAB, mnt_debug_h(tb, "free"));
+
while (!list_empty(&tb->ents)) {
mnt_fs *fs = list_entry(tb->ents.next, mnt_fs, ents);
mnt_free_fs(fs);
list_add_tail(&fs->ents, &tb->ents);
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: add entry: %s %s\n",
- tb, mnt_fs_get_source(fs),
- mnt_fs_get_target(fs)));
-
+ DBG(TAB, mnt_debug_h(tb, "add entry: %s %s",
+ mnt_fs_get_source(fs), mnt_fs_get_target(fs)));
tb->nents++;
return 0;
}
if (!tb || !root)
return -EINVAL;
- DBG(DEBUG_TAB, fprintf(stderr, "libmount: tab %p: lookup root fs\n", tb));
+ DBG(TAB, mnt_debug_h(tb, "lookup root fs", tb));
mnt_reset_iter(&itr, MNT_ITER_FORWARD);
while(mnt_tab_next_fs(tb, &itr, &fs) == 0) {
if (!tb || !itr || !parent)
return -EINVAL;
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: lookup next child of %s\n",
- tb, mnt_fs_get_target(parent)));
+ DBG(TAB, mnt_debug_h(tb, "lookup next child of %s",
+ mnt_fs_get_target(parent)));
parent_id = mnt_fs_get_id(parent);
if (!parent_id)
if (!tb || !itr || !fs || !match_func)
return -EINVAL;
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: lookup next fs\n", tb));
+ DBG(TAB, mnt_debug_h(tb, "lookup next fs", tb));
if (!itr->head)
MNT_ITER_INIT(itr, &tb->ents);
if (!tb || !path)
return NULL;
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: lookup target: %s\n", tb, path));
+ DBG(TAB, mnt_debug_h(tb, "lookup target: %s", path));
/* native @target */
mnt_reset_iter(&itr, direction);
assert(tb);
assert(path);
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: lookup srcpath: %s\n", tb, path));
+ DBG(TAB, mnt_debug_h(tb, "lookup srcpath: %s", path));
/* native paths */
mnt_reset_iter(&itr, direction);
if (!tb || !tag || !val)
return NULL;
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: lookup by TAG: %s %s\n", tb, tag, val));
+ DBG(TAB, mnt_debug_h(tb, "lookup by TAG: %s %s", tag, val));
/* look up by TAG */
mnt_reset_iter(&itr, direction);
if (!tb || !source)
return NULL;
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: lookup SOURCE: %s\n", tb, source));
+ DBG(TAB, mnt_debug_h(tb, "lookup SOURCE: %s", source));
if (strchr(source, '=')) {
char *tag, *val;
/* Missing final newline? Otherwise extremely */
/* long line - assume file was corrupted */
if (feof(f)) {
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: WARNING: no final newline at the end of %s\n",
- filename));
+ DBG(TAB, mnt_debug_h(tb,
+ "%s: no final newline", filename));
s = index (buf, '\0');
} else {
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: %s: %d: missing newline at line\n",
+ DBG(TAB, mnt_debug_h(tb,
+ "%s:%d: missing newline at line",
filename, *nlines));
goto err;
}
s = skip_spaces(buf);
} while (*s == '\0' || *s == '#');
- DBG(DEBUG_TAB, fprintf(stderr, "libmount: %s:%d: %s\n",
- filename, *nlines, s));
+ DBG(TAB, mnt_debug_h(tb, "%s:%d: %s", filename, *nlines, s));
if (!tb->fmt)
tb->fmt = detect_fmt(s);
return -ENOMEM;
}
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: %s:%d: SOURCE:%s, MNTPOINT:%s, TYPE:%s, "
- "OPTS:%s, FREQ:%d, PASSNO:%d\n",
- tb, filename, *nlines,
+ DBG(TAB, mnt_debug_h(tb, "%s:%d: SOURCE:%s, MNTPOINT:%s, TYPE:%s, "
+ "OPTS:%s, FREQ:%d, PASSNO:%d",
+ filename, *nlines,
fs->source, fs->target, fs->fstype,
fs->optstr, fs->freq, fs->passno));
return 0;
err:
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: %s:%d: parse error\n", tb, filename, *nlines));
+ DBG(TAB, mnt_debug_h(tb, "%s:%d: parse error", tb, filename, *nlines));
rc = 1; /* recoverable error */
assert(f);
assert(filename);
- DBG(DEBUG_TAB,
- fprintf(stderr, "libmount: tab %p: start parsing %s\n", tb, filename));
+ DBG(TAB, mnt_debug_h(tb, "%s: start parsing", filename));
while (!feof(f)) {
mnt_fs *fs = mnt_new_fs();
}
}
- DBG(DEBUG_TAB,
- fprintf(stderr, "libmount: tab %p: stop parsing %s\n", tb, filename));
+ DBG(TAB, mnt_debug_h(tb, "%s: stop parsing", filename));
return 0;
err:
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: error parsing %s (rc=%d)\n", tb, filename, rc));
+ DBG(TAB, mnt_debug_h(tb, "%s: parse error (rc=%d)", filename, rc));
return rc;
}
num = mnt_tab_get_nents(tb) - num;
- DBG(DEBUG_TAB, fprintf(stderr,
- "libmount: tab %p: fstab contains %d records\n", tb, num));
+ DBG(TAB, mnt_debug_h(tb, "fstab contains %d records", num));
return num > 0 ? 0 : -1;
}
if (!upd)
return NULL;
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: allocate\n", upd));
+ DBG(MTAB, mnt_debug_h(upd, "allocate"));
if (action)
mnt_update_set_action(upd, action);
if (!upd)
return;
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: deallacate\n", upd));
+ DBG(MTAB, mnt_debug_h(upd, "free"));
mnt_free_lock(upd->lc);
free(upd->filename);
if (!tb)
goto error;
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %s: update from tab %p\n", filename, tb));
+ DBG(MTAB, mnt_debug("%s: update from tab %p", filename, tb));
if (snprintf(tmpname, sizeof(tmpname), "%s.tmp", filename)
>= sizeof(tmpname))
return 0;
error:
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %s: update from tab %p failed\n", filename, tb));
+ DBG(MTAB, mnt_debug("%s: update from tab %p failed", filename, tb));
if (f)
fclose(f);
return -1;
if (!upd || !upd->fs)
return -1;
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: prepare update (target %s, source %s, optstr %s)\n",
- upd, mnt_fs_get_target(upd->fs), mnt_fs_get_source(upd->fs),
+ DBG(MTAB, mnt_debug_h(upd,
+ "prepare update (target %s, source %s, optstr %s)",
+ mnt_fs_get_target(upd->fs),
+ mnt_fs_get_source(upd->fs),
mnt_fs_get_optstr(upd->fs)));
if (!upd->filename) {
goto err;
}
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: prepare update: success\n", upd));
+ DBG(MTAB, mnt_debug_h(upd, "prepare update: success"));
free(u);
return 0;
err:
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: prepare update: failed\n", upd));
+ DBG(MTAB, mnt_debug_h(upd, "prepare update: failed"));
free(u);
return -1;
nothing:
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: prepare update: unnecessary\n", upd));
+ DBG(MTAB, mnt_debug_h(upd, "prepare update: unnecessary"));
free(u);
return 1;
}
assert(upd);
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: add entry\n", upd));
+ DBG(MTAB, mnt_debug_h(upd, "add entry"));
+
if (upd->lc)
mnt_lock_file(upd->lc);
f = fopen(upd->filename, "a+");
target = mnt_fs_get_target(upd->fs);
assert(target);
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: remove entry (target %s)\n", upd, target));
+ DBG(MTAB, mnt_debug_h(upd, "remove entry (target %s)", target));
if (upd->lc)
mnt_lock_file(upd->lc);
if (!upd->old_target)
return -1;
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: modify target (%s->%s)\n", upd,
+ DBG(MTAB, mnt_debug_h(upd, "modify target (%s->%s)",
upd->old_target, mnt_fs_get_target(upd->fs)));
if (upd->lc)
assert(target);
assert(upd->filename);
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: modify options (target %s)\n", upd, target));
+ DBG(MTAB, mnt_debug_h(upd, "modify options (target %s)", target));
if (upd->lc)
mnt_lock_file(upd->lc);
if (!upd || !upd->fs)
return -1;
- DBG(DEBUG_MTAB, fprintf(stderr,
- "libmount: update %p: update (target %s)\n", upd,
+ DBG(MTAB, mnt_debug_h(upd, "update (target %s)",
mnt_fs_get_target(upd->fs)));
/*
* umount
memcpy(mnt, "/", 2);
done:
- DBG(DEBUG_UTILS, fprintf(stderr,
- "libmount: utils: fs-root for %s is %s\n", path, mnt));
+ DBG(UTILS, mnt_debug("fs-root for %s is %s", path, mnt));
return mnt;
err:
free(mnt);