summaryrefslogtreecommitdiff
path: root/shlibs
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2010-09-21 21:44:19 +0200
committerKarel Zak <kzak@redhat.com>2011-01-03 12:28:42 +0100
commita1e8af753cfe312d22c1b497243ef1f98d97052c (patch)
tree150d1ec15d2c6bb404ab5c3517d09e0466d0fe57 /shlibs
parentc68408c2fed27f5fd7a69b6ec0d649f67483226f (diff)
downloadutil-linux-old-a1e8af753cfe312d22c1b497243ef1f98d97052c.tar.gz
libmount: add utils for work with uid/gid
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'shlibs')
-rw-r--r--shlibs/mount/src/mountP.h3
-rw-r--r--shlibs/mount/src/utils.c43
2 files changed, 46 insertions, 0 deletions
diff --git a/shlibs/mount/src/mountP.h b/shlibs/mount/src/mountP.h
index f5958957..230f51cb 100644
--- a/shlibs/mount/src/mountP.h
+++ b/shlibs/mount/src/mountP.h
@@ -107,6 +107,9 @@ extern int endswith(const char *s, const char *sx);
extern int startswith(const char *s, const char *sx);
extern char *mnt_get_username(const uid_t uid);
+extern int mnt_get_uid(const char *username, uid_t *uid);
+extern int mnt_get_gid(const char *groupname, gid_t *gid);
+
extern int mnt_has_regular_mtab(void);
extern char *mnt_get_mountpoint(const char *path);
diff --git a/shlibs/mount/src/utils.c b/shlibs/mount/src/utils.c
index f48e533d..108bb2eb 100644
--- a/shlibs/mount/src/utils.c
+++ b/shlibs/mount/src/utils.c
@@ -27,6 +27,7 @@
#include <sys/types.h>
#include <fcntl.h>
#include <pwd.h>
+#include <grp.h>
#include "strutils.h"
#include "pathnames.h"
@@ -316,6 +317,48 @@ char *mnt_get_username(const uid_t uid)
return username;
}
+int mnt_get_uid(const char *username, uid_t *uid)
+{
+ struct passwd pwd;
+ struct passwd *pw;
+ size_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
+ char *buf;
+
+ if (sz <= 0)
+ sz = 16384; /* Should be more than enough */
+
+ buf = malloc(sz);
+ if (!buf)
+ return -ENOMEM;
+
+ if (!getpwnam_r(username, &pwd, buf, sz, &pw) && pw)
+ *uid= pw->pw_uid;
+
+ free(buf);
+ return 0;
+}
+
+int mnt_get_gid(const char *groupname, gid_t *gid)
+{
+ struct group grp;
+ struct group *gr;
+ size_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
+ char *buf;
+
+ if (sz <= 0)
+ sz = 16384; /* Should be more than enough */
+
+ buf = malloc(sz);
+ if (!buf)
+ return -ENOMEM;
+
+ if (!getgrnam_r(groupname, &grp, buf, sz, &gr) && gr)
+ *gid= gr->gr_gid;
+
+ free(buf);
+ return 0;
+}
+
/*
* Returns 1 if /etc/mtab is a reqular file.
*/