summaryrefslogtreecommitdiffstats
path: root/shlibs/mount/src/utils.c
diff options
context:
space:
mode:
authorKarel Zak2010-09-21 21:44:19 +0200
committerKarel Zak2011-01-03 12:28:42 +0100
commita1e8af753cfe312d22c1b497243ef1f98d97052c (patch)
tree150d1ec15d2c6bb404ab5c3517d09e0466d0fe57 /shlibs/mount/src/utils.c
parentlibmount: add first part of high-level API (diff)
downloadkernel-qcow2-util-linux-a1e8af753cfe312d22c1b497243ef1f98d97052c.tar.gz
kernel-qcow2-util-linux-a1e8af753cfe312d22c1b497243ef1f98d97052c.tar.xz
kernel-qcow2-util-linux-a1e8af753cfe312d22c1b497243ef1f98d97052c.zip
libmount: add utils for work with uid/gid
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'shlibs/mount/src/utils.c')
-rw-r--r--shlibs/mount/src/utils.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/shlibs/mount/src/utils.c b/shlibs/mount/src/utils.c
index f48e533d7..108bb2eb6 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.
*/