summary refs log tree commit diff
path: root/src/libs6ps/s6ps_grcache.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-02-12 01:36:13 +0000
committerLaurent Bercot <ska@appnovation.com>2023-02-12 01:36:13 +0000
commit4d41f0d5e871b86b8f56339a99cfb1b80fbb420c (patch)
tree0446c51aeb07d8ef817fe3cdc53955dc7fe3a09d /src/libs6ps/s6ps_grcache.c
parent2f8984613e17f9e5971ec338240b0bb6d2dd1cc6 (diff)
downloads6-linux-utils-4d41f0d5e871b86b8f56339a99cfb1b80fbb420c.tar.gz
s6-linux-utils-4d41f0d5e871b86b8f56339a99cfb1b80fbb420c.tar.xz
s6-linux-utils-4d41f0d5e871b86b8f56339a99cfb1b80fbb420c.zip
Deglobalize s6-ps
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libs6ps/s6ps_grcache.c')
-rw-r--r--src/libs6ps/s6ps_grcache.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/libs6ps/s6ps_grcache.c b/src/libs6ps/s6ps_grcache.c
new file mode 100644
index 0000000..49d84e5
--- /dev/null
+++ b/src/libs6ps/s6ps_grcache.c
@@ -0,0 +1,47 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <grp.h>
+#include <errno.h>
+
+#include <skalibs/types.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/genalloc.h>
+#include <skalibs/skamisc.h>
+#include <skalibs/avltree.h>
+
+#include "s6ps.h"
+#include "s6ps-internal.h"
+
+int s6ps_grcache_lookup (s6ps_cache_t *cache, stralloc *sa, gid_t gid)
+{
+  dius_t d = { .left = (uint32_t)gid, .right = satmp.len } ;
+  uint32_t i ;
+  if (!avltree_search(&cache->tree, &d.left, &i))
+  {
+    struct group *gr ;
+    size_t n = genalloc_len(dius_t, &cache->index) ;
+    errno = 0 ;
+    gr = getgrgid(gid) ;
+    if (!gr)
+    {
+      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, gid) ;
+      stralloc_catb(&satmp, ")", 2) ;
+    }
+    else if (!stralloc_cats(&satmp, gr->gr_name) || !stralloc_0(&satmp)) return 0 ;
+    if (!genalloc_append(dius_t, &cache->index, &d)) goto err ;
+    if (!avltree_insert(&cache->tree, n))
+    {
+      genalloc_setlen(dius_t, &cache->index, n) ;
+      goto err ;
+    }
+    i = n ;
+  }
+  return stralloc_cats(sa, satmp.s + genalloc_s(dius_t, &cache->index)[i].right) ;
+ err:
+  satmp.len = d.right ;
+  return 0 ;
+}