summaryrefslogtreecommitdiff
path: root/src/minutils/s6ps_pwcache.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-09-19 15:01:58 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-09-19 15:01:58 +0000
commit54b6467013bfbdb3ee606961c02fbff1271ca582 (patch)
treee3969743e61542979f12fca01017e67c4480eca8 /src/minutils/s6ps_pwcache.c
downloads6-linux-utils-54b6467013bfbdb3ee606961c02fbff1271ca582.tar.xz
initial commit
Diffstat (limited to 'src/minutils/s6ps_pwcache.c')
-rw-r--r--src/minutils/s6ps_pwcache.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/minutils/s6ps_pwcache.c b/src/minutils/s6ps_pwcache.c
new file mode 100644
index 0000000..4c78460
--- /dev/null
+++ b/src/minutils/s6ps_pwcache.c
@@ -0,0 +1,66 @@
+/* ISC license. */
+
+#include <sys/types.h>
+#include <pwd.h>
+#include <errno.h>
+#include <skalibs/uint.h>
+#include <skalibs/diuint.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/genalloc.h>
+#include <skalibs/skamisc.h>
+#include <skalibs/avltree.h>
+#include "s6-ps.h"
+
+static avltree pwcache_tree = AVLTREE_ZERO ;
+static genalloc pwcache_index = GENALLOC_ZERO ;
+
+int s6ps_pwcache_init (void)
+{
+ avltree_init(&pwcache_tree, 5, 3, 8, &left_dtok, &uint_cmp, &pwcache_index) ;
+ return 1 ;
+}
+
+void s6ps_pwcache_finish (void)
+{
+ avltree_free(&pwcache_tree) ;
+ genalloc_free(diuint, &pwcache_index) ;
+}
+
+int s6ps_pwcache_lookup (stralloc *sa, unsigned int uid)
+{
+ int wasnull = !satmp.s ;
+ diuint d = { .left = uid, .right = satmp.len } ;
+ unsigned int i ;
+ if (!avltree_search(&pwcache_tree, &d.left, &i))
+ {
+ struct passwd *pw ;
+ unsigned int n = genalloc_len(diuint, &pwcache_index) ;
+ errno = 0 ;
+ pw = getpwuid(uid) ;
+ if (!pw)
+ {
+ if (errno) return 0 ;
+ if (!stralloc_readyplus(&satmp, UINT_FMT + 2)) return 0 ;
+ stralloc_catb(&satmp, "(", 1) ;
+ satmp.len += uint_fmt(satmp.s + satmp.len, uid) ;
+ stralloc_catb(&satmp, ")", 2) ;
+ }
+ else if (!stralloc_cats(&satmp, pw->pw_name) || !stralloc_0(&satmp)) return 0 ;
+ if (!genalloc_append(diuint, &pwcache_index, &d)) goto err ;
+ if (!avltree_insert(&pwcache_tree, n))
+ {
+ genalloc_setlen(diuint, &pwcache_index, n) ;
+ goto err ;
+ }
+ i = n ;
+ }
+ return stralloc_cats(sa, satmp.s + genalloc_s(diuint, &pwcache_index)[i].right) ;
+ err:
+ {
+ register int e = errno ;
+ if (wasnull) stralloc_free(&satmp) ;
+ else satmp.len = d.right ;
+ errno = e ;
+ }
+ return 0 ;
+}