From: Karel Zak Date: Thu, 15 Jul 2010 13:24:11 +0000 (+0200) Subject: libmount: add mnt_get_writable_mtab_path() X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d1be0c3471d6eb7304542e613da676130fda5194;p=util-linux libmount: add mnt_get_writable_mtab_path() Signed-off-by: Karel Zak --- diff --git a/shlibs/mount/src/mount.h.in b/shlibs/mount/src/mount.h.in index fcb4b4e5..46934a98 100644 --- a/shlibs/mount/src/mount.h.in +++ b/shlibs/mount/src/mount.h.in @@ -114,6 +114,7 @@ extern int mnt_fstype_is_netfs(const char *type); extern int mnt_fstype_is_pseudofs(const char *type); extern int mnt_match_fstype(const char *type, const char *pattern); extern int mnt_match_options(const char *optstr, const char *pattern); +extern const char *mnt_get_writable_mtab_path(void); /* cache.c */ extern mnt_cache *mnt_new_cache(void); @@ -209,10 +210,12 @@ extern char *mnt_optls_create_userspace_optstr(mnt_optls *ls); extern int mnt_optls_print_debug(mnt_optls *ls, FILE *file); /* lock.c */ -extern mnt_lock *mnt_new_lock(const char *lockfile, pid_t id); +extern mnt_lock *mnt_new_lock(const char *lockname, pid_t id); extern void mnt_free_lock(mnt_lock *ml); + extern const char *mnt_lock_get_lockfile(mnt_lock *ml); extern const char *mnt_lock_get_linkfile(mnt_lock *ml); + extern void mnt_unlock_file(mnt_lock *ml); extern int mnt_lock_file(mnt_lock *ml); diff --git a/shlibs/mount/src/mount.sym b/shlibs/mount/src/mount.sym index 14d9fbf4..5596d87c 100644 --- a/shlibs/mount/src/mount.sym +++ b/shlibs/mount/src/mount.sym @@ -51,6 +51,7 @@ global: mnt_fstype_is_pseudofs; mnt_get_builtin_optmap; mnt_get_library_version; + mnt_get_writable_mtab_path; mnt_init_debug; mnt_iter_get_direction; mnt_lock_file; diff --git a/shlibs/mount/src/utils.c b/shlibs/mount/src/utils.c index 9bdd62ad..67b00c31 100644 --- a/shlibs/mount/src/utils.c +++ b/shlibs/mount/src/utils.c @@ -262,6 +262,43 @@ int mnt_has_regular_mtab(void) return 0; } +/** + * mnt_get_writable_mtab_path: + * + * Returns: pointer to the static string with path to the file with userspace + * mount options (classic /etc/mtab or /var/run/mount/mountinfo) + */ +const char *mnt_get_writable_mtab_path(void) +{ + struct stat mst, ist; + int mtab, info; + + mtab = !lstat(_PATH_MOUNTED, &mst); + info = !stat(MNT_PATH_RUNDIR, &ist); + + /* A) mtab is symlink, /var/run/mount is available */ + if (mtab && S_ISLNK(mst.st_mode) && info) { + int fd = open(MNT_PATH_MOUNTINFO, O_RDWR | O_CREAT, 0644); + if (fd >= 0) { + close(fd); + return MNT_PATH_MOUNTINFO; + } + return NULL; /* probably EACCES */ + } + + /* B) classis system with /etc/mtab */ + if (mtab && S_ISREG(mst.st_mode)) { + int fd = open(_PATH_MOUNTED, O_RDWR, 0644); + if (fd >= 0) { + close(fd); + return _PATH_MOUNTED; + } + return NULL; /* probably EACCES */ + } + + return NULL; +} + #ifdef TEST_PROGRAM int test_match_fstype(struct mtest *ts, int argc, char *argv[]) {