summaryrefslogtreecommitdiff
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
parent2f8984613e17f9e5971ec338240b0bb6d2dd1cc6 (diff)
downloads6-linux-utils-4d41f0d5e871b86b8f56339a99cfb1b80fbb420c.tar.xz
Deglobalize s6-ps
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rw-r--r--.gitignore4
-rw-r--r--package/deps.mak24
-rw-r--r--src/include-local/s6ps.h (renamed from src/include-local/s6-ps.h)105
-rw-r--r--src/libps/s6ps_grcache.c67
-rw-r--r--src/libps/s6ps_pwcache.c67
-rw-r--r--src/libs6ps/deps-lib/s6ps (renamed from src/libps/deps-lib/s6ps)1
-rw-r--r--src/libs6ps/s6ps-internal.h17
-rw-r--r--src/libs6ps/s6ps_cache.c34
-rw-r--r--src/libs6ps/s6ps_grcache.c47
-rw-r--r--src/libs6ps/s6ps_otree.c (renamed from src/libps/s6ps_otree.c)2
-rw-r--r--src/libs6ps/s6ps_pfield.c (renamed from src/libps/s6ps_pfield.c)188
-rw-r--r--src/libs6ps/s6ps_pwcache.c47
-rw-r--r--src/libs6ps/s6ps_statparse.c (renamed from src/libps/s6ps_statparse.c)2
-rw-r--r--src/libs6ps/s6ps_ttycache.c (renamed from src/libps/s6ps_ttycache.c)40
-rw-r--r--src/libs6ps/s6ps_wchan.c (renamed from src/libps/s6ps_wchan.c)45
-rw-r--r--src/s6-linux-utils/s6-ps.c58
-rwxr-xr-xtools/gen-deps.sh11
17 files changed, 400 insertions, 359 deletions
diff --git a/.gitignore b/.gitignore
index 4ca416f..b360b5b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,7 @@
*.o
-*.a.xyzzy
+/lib*.a.xyzzy
*.lo
-*.so.xyzzy
+/lib*.so.xyzzy
/config.mak
/src/include/s6-linux-utils/config.h
/rngseed
diff --git a/package/deps.mak b/package/deps.mak
index 0dac63f..fb16475 100644
--- a/package/deps.mak
+++ b/package/deps.mak
@@ -2,13 +2,14 @@
# This file has been generated by tools/gen-deps.sh
#
-src/libps/s6ps_grcache.o src/libps/s6ps_grcache.lo: src/libps/s6ps_grcache.c src/include-local/s6-ps.h
-src/libps/s6ps_otree.o src/libps/s6ps_otree.lo: src/libps/s6ps_otree.c src/include-local/s6-ps.h
-src/libps/s6ps_pfield.o src/libps/s6ps_pfield.lo: src/libps/s6ps_pfield.c src/include-local/s6-ps.h
-src/libps/s6ps_pwcache.o src/libps/s6ps_pwcache.lo: src/libps/s6ps_pwcache.c src/include-local/s6-ps.h
-src/libps/s6ps_statparse.o src/libps/s6ps_statparse.lo: src/libps/s6ps_statparse.c src/include-local/s6-ps.h
-src/libps/s6ps_ttycache.o src/libps/s6ps_ttycache.lo: src/libps/s6ps_ttycache.c src/include-local/s6-ps.h
-src/libps/s6ps_wchan.o src/libps/s6ps_wchan.lo: src/libps/s6ps_wchan.c src/include-local/s6-ps.h
+src/libs6ps/s6ps_cache.o src/libs6ps/s6ps_cache.lo: src/libs6ps/s6ps_cache.c src/libs6ps/s6ps-internal.h src/include-local/s6ps.h
+src/libs6ps/s6ps_grcache.o src/libs6ps/s6ps_grcache.lo: src/libs6ps/s6ps_grcache.c src/libs6ps/s6ps-internal.h src/include-local/s6ps.h
+src/libs6ps/s6ps_otree.o src/libs6ps/s6ps_otree.lo: src/libs6ps/s6ps_otree.c src/include-local/s6ps.h
+src/libs6ps/s6ps_pfield.o src/libs6ps/s6ps_pfield.lo: src/libs6ps/s6ps_pfield.c src/include-local/s6ps.h
+src/libs6ps/s6ps_pwcache.o src/libs6ps/s6ps_pwcache.lo: src/libs6ps/s6ps_pwcache.c src/libs6ps/s6ps-internal.h src/include-local/s6ps.h
+src/libs6ps/s6ps_statparse.o src/libs6ps/s6ps_statparse.lo: src/libs6ps/s6ps_statparse.c src/include-local/s6ps.h
+src/libs6ps/s6ps_ttycache.o src/libs6ps/s6ps_ttycache.lo: src/libs6ps/s6ps_ttycache.c src/libs6ps/s6ps-internal.h src/include-local/s6ps.h
+src/libs6ps/s6ps_wchan.o src/libs6ps/s6ps_wchan.lo: src/libs6ps/s6ps_wchan.c src/include-local/s6ps.h
src/s6-linux-utils/rngseed.o src/s6-linux-utils/rngseed.lo: src/s6-linux-utils/rngseed.c src/include/s6-linux-utils/config.h
src/s6-linux-utils/s6-chroot.o src/s6-linux-utils/s6-chroot.lo: src/s6-linux-utils/s6-chroot.c
src/s6-linux-utils/s6-freeramdisk.o src/s6-linux-utils/s6-freeramdisk.lo: src/s6-linux-utils/s6-freeramdisk.c
@@ -16,18 +17,16 @@ src/s6-linux-utils/s6-hostname.o src/s6-linux-utils/s6-hostname.lo: src/s6-linux
src/s6-linux-utils/s6-logwatch.o src/s6-linux-utils/s6-logwatch.lo: src/s6-linux-utils/s6-logwatch.c
src/s6-linux-utils/s6-mount.o src/s6-linux-utils/s6-mount.lo: src/s6-linux-utils/s6-mount.c src/include-local/mount-constants.h
src/s6-linux-utils/s6-pivotchroot.o src/s6-linux-utils/s6-pivotchroot.lo: src/s6-linux-utils/s6-pivotchroot.c
-src/s6-linux-utils/s6-ps.o src/s6-linux-utils/s6-ps.lo: src/s6-linux-utils/s6-ps.c src/include-local/s6-ps.h
+src/s6-linux-utils/s6-ps.o src/s6-linux-utils/s6-ps.lo: src/s6-linux-utils/s6-ps.c src/include-local/s6ps.h
src/s6-linux-utils/s6-swapoff.o src/s6-linux-utils/s6-swapoff.lo: src/s6-linux-utils/s6-swapoff.c
src/s6-linux-utils/s6-swapon.o src/s6-linux-utils/s6-swapon.lo: src/s6-linux-utils/s6-swapon.c
src/s6-linux-utils/s6-umount.o src/s6-linux-utils/s6-umount.lo: src/s6-linux-utils/s6-umount.c
ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),)
-libs6ps.a.xyzzy: src/libps/s6ps_grcache.o src/libps/s6ps_otree.o src/libps/s6ps_pfield.o src/libps/s6ps_pwcache.o src/libps/s6ps_statparse.o src/libps/s6ps_ttycache.o src/libps/s6ps_wchan.o
+libs6ps.a.xyzzy: src/libs6ps/s6ps_cache.o src/libs6ps/s6ps_grcache.o src/libs6ps/s6ps_otree.o src/libs6ps/s6ps_pfield.o src/libs6ps/s6ps_pwcache.o src/libs6ps/s6ps_statparse.o src/libs6ps/s6ps_ttycache.o src/libs6ps/s6ps_wchan.o
else
-libs6ps.a.xyzzy: src/libps/s6ps_grcache.lo src/libps/s6ps_otree.lo src/libps/s6ps_pfield.lo src/libps/s6ps_pwcache.lo src/libps/s6ps_statparse.lo src/libps/s6ps_ttycache.lo src/libps/s6ps_wchan.lo
+libs6ps.a.xyzzy: src/libs6ps/s6ps_cache.lo src/libs6ps/s6ps_grcache.lo src/libs6ps/s6ps_otree.lo src/libs6ps/s6ps_pfield.lo src/libs6ps/s6ps_pwcache.lo src/libs6ps/s6ps_statparse.lo src/libs6ps/s6ps_ttycache.lo src/libs6ps/s6ps_wchan.lo
endif
-libs6ps.so.xyzzy: EXTRA_LIBS :=
-libs6ps.so.xyzzy: src/libps/s6ps_grcache.lo src/libps/s6ps_otree.lo src/libps/s6ps_pfield.lo src/libps/s6ps_pwcache.lo src/libps/s6ps_statparse.lo src/libps/s6ps_ttycache.lo src/libps/s6ps_wchan.lo
s6-linux-utils: EXTRA_LIBS := -lskarnet ${SYSCLOCK_LIB} ${MAYBEPTHREAD_LIB}
s6-linux-utils: src/multicall/s6-linux-utils.o libs6ps.a.xyzzy ${LIBNSSS}
rngseed: EXTRA_LIBS := -lskarnet ${SYSCLOCK_LIB}
@@ -52,3 +51,4 @@ s6-swapon: EXTRA_LIBS := -lskarnet
s6-swapon: src/s6-linux-utils/s6-swapon.o
s6-umount: EXTRA_LIBS := -lskarnet
s6-umount: src/s6-linux-utils/s6-umount.o
+INTERNAL_LIBS := libs6ps.a.xyzzy
diff --git a/src/include-local/s6-ps.h b/src/include-local/s6ps.h
index 7c963f1..951cc3a 100644
--- a/src/include-local/s6-ps.h
+++ b/src/include-local/s6ps.h
@@ -8,18 +8,11 @@
#include <skalibs/uint64.h>
#include <skalibs/stralloc.h>
+#include <skalibs/genalloc.h>
#include <skalibs/tai.h>
+#include <skalibs/avltree.h>
#include <skalibs/avltreen.h>
-
-typedef struct dius_s dius_t, *dius_t_ref ;
-struct dius_s
-{
- uint32_t left ;
- size_t right ;
-} ;
-#define DIUS_ZERO { .left = 0, .right = 0 }
-
typedef enum pfield_e pfield_t, *pfield_t_ref ;
enum pfield_e
{
@@ -59,9 +52,6 @@ enum pfield_e
PFIELD_PHAIL
} ;
-extern char const *const *s6ps_opttable ;
-extern char const *const *s6ps_fieldheaders ;
-
typedef struct pscan_s pscan_t, *pscan_t_ref ;
struct pscan_s
{
@@ -131,29 +121,82 @@ struct pscan_s
.policy = 0 \
}
+typedef struct s6ps_cache_s s6ps_cache_t, *s6ps_cache_t_ref ;
+struct s6ps_cache_s
+{
+ avltree tree ;
+ genalloc index ;
+} ;
+#define S6PS_CACHE_ZERO { .tree = AVLTREE_ZERO, .index = GENALLOC_ZERO }
+
+typedef struct s6ps_wchan_s s6ps_wchan_t, *s6ps_wchan_t_ref ;
+struct s6ps_wchan_s
+{
+ stralloc sysmap ;
+ genalloc ind ;
+} ;
+#define S6PS_WCHAN_ZERO { .sysmap = STRALLOC_ZERO, .ind = GENALLOC_ZERO }
+
+typedef struct s6ps_auxinfo_s s6ps_auxinfo_t, *s6ps_auxinfo_t_ref ;
+struct s6ps_auxinfo_s
+{
+ tain boottime ;
+ s6ps_cache_t caches[3] ;
+ s6ps_wchan_t wchan ;
+ unsigned int hz ;
+ unsigned int pgsz ;
+ uint64_t totalmem ;
+} ;
+#define S6PS_AUXINFO_ZERO { .boottime = TAIN_EPOCH, .caches = { S6PS_CACHE_ZERO, S6PS_CACHE_ZERO, S6PS_CACHE_ZERO }, .wchan = S6PS_WCHAN_ZERO, .hz = 0, .pgsz = 0, .totalmem = 0 }
+
+typedef int pfieldfmt_func (s6ps_auxinfo_t *, pscan_t *, size_t *, size_t *) ;
+typedef pfieldfmt_func *pfieldfmt_func_ref ;
+
+
+ /* exported by s6ps_pfield.c */
+
+extern char const *const *s6ps_opttable ;
+extern char const *const *s6ps_fieldheaders ;
+extern pfieldfmt_func_ref const *const s6ps_pfield_fmt ;
+extern int s6ps_compute_boottime (s6ps_auxinfo_t *, pscan_t *, unsigned int) ;
+
+
+ /* exported by s6ps_statparse.c */
+
extern int s6ps_statparse (pscan_t *) ;
+
+
+ /* exported by s6ps_otree.c */
+
extern void s6ps_otree (pscan_t *, unsigned int, avltreen *, unsigned int *) ;
-extern int s6ps_compute_boottime (pscan_t *, unsigned int) ;
-typedef int pfieldfmt_func (pscan_t *, size_t *, size_t *) ;
-typedef pfieldfmt_func *pfieldfmt_func_ref ;
+ /* exported by s6ps_cache.c */
+
+extern int s6ps_cache_init (s6ps_cache_t *) ;
+extern void s6ps_cache_finish (s6ps_cache_t *) ;
+extern int s6ps_uint32_cmp (void const *, void const *, void *) ;
+
+
+ /* exported by s6ps_pwcache.c */
+
+extern int s6ps_pwcache_lookup (s6ps_cache_t *, stralloc *, uid_t) ;
+
+
+ /* exported by s6ps_grcache.c */
+
+extern int s6ps_grcache_lookup (s6ps_cache_t *, stralloc *, gid_t) ;
+
+
+ /* exported by s6ps_ttycache.c */
+
+extern int s6ps_ttycache_lookup (s6ps_cache_t *, stralloc *, dev_t) ;
+
+
+ /* exported by s6ps_wchan.c */
-extern pfieldfmt_func_ref *s6ps_pfield_fmt ;
-
-extern void *left_dtok (unsigned int, void *) ;
-extern int uint32_cmp (void const *, void const *, void *) ;
-extern int s6ps_pwcache_init (void) ;
-extern void s6ps_pwcache_finish (void) ;
-extern int s6ps_pwcache_lookup (stralloc *, uid_t) ;
-extern int s6ps_grcache_init (void) ;
-extern void s6ps_grcache_finish (void) ;
-extern int s6ps_grcache_lookup (stralloc *, gid_t) ;
-extern int s6ps_ttycache_init (void) ;
-extern void s6ps_ttycache_finish (void) ;
-extern int s6ps_ttycache_lookup (stralloc *, dev_t) ;
-extern int s6ps_wchan_init (char const *) ;
-extern void s6ps_wchan_finish (void) ;
-extern int s6ps_wchan_lookup (stralloc *, uint64_t) ;
+extern int s6ps_wchan_init (s6ps_wchan_t *, char const *) ;
+extern void s6ps_wchan_finish (s6ps_wchan_t *) ;
+extern int s6ps_wchan_lookup (s6ps_wchan_t const *, stralloc *, uint64_t) ;
#endif
diff --git a/src/libps/s6ps_grcache.c b/src/libps/s6ps_grcache.c
deleted file mode 100644
index 189f5ae..0000000
--- a/src/libps/s6ps_grcache.c
+++ /dev/null
@@ -1,67 +0,0 @@
-/* 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 "s6-ps.h"
-
-static avltree grcache_tree = AVLTREE_ZERO ;
-static genalloc grcache_index = GENALLOC_ZERO ;
-
-int s6ps_grcache_init (void)
-{
- avltree_init(&grcache_tree, 5, 3, 8, &left_dtok, &uint32_cmp, &grcache_index) ;
- return 1 ;
-}
-
-void s6ps_grcache_finish (void)
-{
- avltree_free(&grcache_tree) ;
- genalloc_free(dius_t, &grcache_index) ;
-}
-
-int s6ps_grcache_lookup (stralloc *sa, gid_t gid)
-{
- int wasnull = !satmp.s ;
- dius_t d = { .left = (uint32_t)gid, .right = satmp.len } ;
- uint32_t i ;
- if (!avltree_search(&grcache_tree, &d.left, &i))
- {
- struct group *gr ;
- unsigned int n = genalloc_len(dius_t, &grcache_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, &grcache_index, &d)) goto err ;
- if (!avltree_insert(&grcache_tree, n))
- {
- genalloc_setlen(dius_t, &grcache_index, n) ;
- goto err ;
- }
- i = n ;
- }
- return stralloc_cats(sa, satmp.s + genalloc_s(dius_t, &grcache_index)[i].right) ;
- err:
- {
- int e = errno ;
- if (wasnull) stralloc_free(&satmp) ;
- else satmp.len = d.right ;
- errno = e ;
- }
- return 0 ;
-}
diff --git a/src/libps/s6ps_pwcache.c b/src/libps/s6ps_pwcache.c
deleted file mode 100644
index ce4caa8..0000000
--- a/src/libps/s6ps_pwcache.c
+++ /dev/null
@@ -1,67 +0,0 @@
-/* ISC license. */
-
-#include <sys/types.h>
-#include <stdint.h>
-#include <pwd.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 "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, &uint32_cmp, &pwcache_index) ;
- return 1 ;
-}
-
-void s6ps_pwcache_finish (void)
-{
- avltree_free(&pwcache_tree) ;
- genalloc_free(dius_t, &pwcache_index) ;
-}
-
-int s6ps_pwcache_lookup (stralloc *sa, uid_t uid)
-{
- int wasnull = !satmp.s ;
- dius_t d = { .left = (uint32_t)uid, .right = satmp.len } ;
- uint32_t i ;
- if (!avltree_search(&pwcache_tree, &d.left, &i))
- {
- struct passwd *pw ;
- unsigned int n = genalloc_len(dius_t, &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(dius_t, &pwcache_index, &d)) goto err ;
- if (!avltree_insert(&pwcache_tree, n))
- {
- genalloc_setlen(dius_t, &pwcache_index, n) ;
- goto err ;
- }
- i = n ;
- }
- return stralloc_cats(sa, satmp.s + genalloc_s(dius_t, &pwcache_index)[i].right) ;
- err:
- {
- int e = errno ;
- if (wasnull) stralloc_free(&satmp) ;
- else satmp.len = d.right ;
- errno = e ;
- }
- return 0 ;
-}
diff --git a/src/libps/deps-lib/s6ps b/src/libs6ps/deps-lib/s6ps
index 0f078a6..f3bc04a 100644
--- a/src/libps/deps-lib/s6ps
+++ b/src/libs6ps/deps-lib/s6ps
@@ -1,3 +1,4 @@
+s6ps_cache.o
s6ps_grcache.o
s6ps_otree.o
s6ps_pfield.o
diff --git a/src/libs6ps/s6ps-internal.h b/src/libs6ps/s6ps-internal.h
new file mode 100644
index 0000000..5f205da
--- /dev/null
+++ b/src/libs6ps/s6ps-internal.h
@@ -0,0 +1,17 @@
+/* ISC license. */
+
+#ifndef S6PS_INTERNAL_H
+#define S6PS_INTERNAL_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+typedef struct dius_s dius_t, *dius_t_ref ;
+struct dius_s
+{
+ uint32_t left ;
+ size_t right ;
+} ;
+#define DIUS_ZERO { .left = 0, .right = 0 }
+
+#endif
diff --git a/src/libs6ps/s6ps_cache.c b/src/libs6ps/s6ps_cache.c
new file mode 100644
index 0000000..0ef438a
--- /dev/null
+++ b/src/libs6ps/s6ps_cache.c
@@ -0,0 +1,34 @@
+/* ISC license. */
+
+#include <stdint.h>
+
+#include <skalibs/genalloc.h>
+#include <skalibs/avltree.h>
+
+#include "s6ps.h"
+#include "s6ps-internal.h"
+
+static void *left_dtok (unsigned int d, void *x)
+{
+ return (void *)&genalloc_s(dius_t, (genalloc *)x)[d].left ;
+}
+
+int s6ps_uint32_cmp (void const *a, void const *b, void *x)
+{
+ uint32_t aa = *(uint32_t *)a ;
+ uint32_t bb = *(uint32_t *)b ;
+ (void)x ;
+ return (aa < bb) ? -1 : (aa > bb) ;
+}
+
+int s6ps_cache_init (s6ps_cache_t *cache)
+{
+ avltree_init(&cache->tree, 5, 3, 8, &left_dtok, &s6ps_uint32_cmp, &cache->index) ;
+ return 1 ;
+}
+
+void s6ps_cache_finish (s6ps_cache_t *cache)
+{
+ avltree_free(&cache->tree) ;
+ genalloc_free(dius_t, &cache->index) ;
+}
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 ;
+}
diff --git a/src/libps/s6ps_otree.c b/src/libs6ps/s6ps_otree.c
index 6ba8589..c3bd202 100644
--- a/src/libps/s6ps_otree.c
+++ b/src/libs6ps/s6ps_otree.c
@@ -4,7 +4,7 @@
#include <skalibs/avltreen.h>
-#include "s6-ps.h"
+#include "s6ps.h"
typedef struct ptreeiter_s ptreeiter_t, *ptreeiter_t_ref ;
struct ptreeiter_s
diff --git a/src/libps/s6ps_pfield.c b/src/libs6ps/s6ps_pfield.c
index 9e53e50..6d84ea0 100644
--- a/src/libps/s6ps_pfield.c
+++ b/src/libs6ps/s6ps_pfield.c
@@ -13,7 +13,7 @@
#include <skalibs/djbtime.h>
#include <skalibs/stralloc.h>
-#include "s6-ps.h"
+#include "s6ps.h"
static char const *const fieldheaders[PFIELD_PHAIL] =
{
@@ -93,8 +93,6 @@ static char const *const opttable[PFIELD_PHAIL] =
char const *const *s6ps_opttable = opttable ;
-static tain boottime = TAIN_EPOCH ;
-
static int fmt_64 (pscan_t *p, size_t *pos, size_t *len, uint64_t u)
{
if (!stralloc_readyplus(&p->data, UINT64_FMT)) return 0 ;
@@ -113,20 +111,23 @@ static int fmt_i (pscan_t *p, size_t *pos, size_t *len, int d)
return 1 ;
}
-static int fmt_pid (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_pid (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_64(p, pos, len, p->pid) ;
}
-static int fmt_comm (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_comm (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
*pos = p->statlen ;
*len = p->commlen ;
return 1 ;
}
-static int fmt_s (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_s (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
if (!stralloc_readyplus(&p->data, 4)) return 0 ;
*pos = p->data.len ;
p->data.s[p->data.len++] = p->data.s[p->state] ;
@@ -140,27 +141,30 @@ static int fmt_s (pscan_t *p, size_t *pos, size_t *len)
return 1 ;
}
-static int fmt_ppid (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_ppid (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_64(p, pos, len, p->ppid) ;
}
-static int fmt_pgrp (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_pgrp (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_64(p, pos, len, p->pgrp) ;
}
-static int fmt_session (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_session (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_64(p, pos, len, p->session) ;
}
-static int fmt_ttynr(pscan_t *p, size_t *pos, size_t *len)
+static int fmt_ttynr(s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
if (p->ttynr)
{
size_t tmppos = p->data.len ;
- if (!s6ps_ttycache_lookup(&p->data, p->ttynr)) return 0 ;
+ if (!s6ps_ttycache_lookup(&aux->caches[2], &p->data, p->ttynr)) return 0 ;
*pos = tmppos ;
*len = p->data.len - tmppos ;
}
@@ -173,15 +177,15 @@ static int fmt_ttynr(pscan_t *p, size_t *pos, size_t *len)
return 1 ;
}
-static int fmt_tpgid (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_tpgid (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return p->tpgid < 0 ? fmt_i(p, pos, len, -1) : fmt_64(p, pos, len, p->tpgid) ;
}
-static unsigned int gethz (void)
+static unsigned int gethz (s6ps_auxinfo_t *aux)
{
- static unsigned int hz = 0 ;
- if (!hz)
+ if (!aux->hz)
{
long jiffies = sysconf(_SC_CLK_TCK) ;
if (jiffies < 1)
@@ -189,14 +193,14 @@ static unsigned int gethz (void)
char fmt[ULONG_FMT + 1] ;
fmt[long_fmt(fmt, jiffies)] = 0 ;
strerr_warnw3x("invalid _SC_CLK_TCK value (", fmt, "), using 100") ;
- hz = 100 ;
+ aux->hz = 100 ;
}
- else hz = (unsigned int)jiffies ;
+ else aux->hz = (unsigned int)jiffies ;
}
- return hz ;
+ return aux->hz ;
}
-int s6ps_compute_boottime (pscan_t *p, unsigned int mypos)
+int s6ps_compute_boottime (s6ps_auxinfo_t *aux, pscan_t *p, unsigned int mypos)
{
if (!mypos--)
{
@@ -205,16 +209,16 @@ int s6ps_compute_boottime (pscan_t *p, unsigned int mypos)
}
else
{
- unsigned int hz = gethz() ;
+ unsigned int hz = gethz(aux) ;
tain offset = { .sec = { .x = p[mypos].start / hz }, .nano = (p[mypos].start % hz) * (1000000000 / hz) } ;
- tain_sub(&boottime, &STAMP, &offset) ;
+ tain_sub(&aux->boottime, &STAMP, &offset) ;
return 1 ;
}
}
-static int fmt_jiffies (pscan_t *p, size_t *pos, size_t *len, uint64_t j)
+static int fmt_jiffies (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len, uint64_t j)
{
- unsigned int hz = gethz() ;
+ unsigned int hz = gethz(aux) ;
uint32_t hrs, mins, secs, hfrac ;
if (!stralloc_readyplus(&p->data, UINT64_FMT + 13)) return 0 ;
hfrac = (j % hz) * 100 / hz ;
@@ -253,42 +257,45 @@ static int fmt_jiffies (pscan_t *p, size_t *pos, size_t *len, uint64_t j)
return 1 ;
}
-static int fmt_utime (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_utime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- return fmt_jiffies(p, pos, len, p->utime) ;
+ return fmt_jiffies(aux, p, pos, len, p->utime) ;
}
-static int fmt_stime (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_stime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- return fmt_jiffies(p, pos, len, p->stime) ;
+ return fmt_jiffies(aux, p, pos, len, p->stime) ;
}
-static int fmt_cutime (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_cutime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- return fmt_jiffies(p, pos, len, p->utime + p->cutime) ;
+ return fmt_jiffies(aux, p, pos, len, p->utime + p->cutime) ;
}
-static int fmt_cstime (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_cstime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- return fmt_jiffies(p, pos, len, p->stime + p->cstime) ;
+ return fmt_jiffies(aux, p, pos, len, p->stime + p->cstime) ;
}
-static int fmt_prio (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_prio (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_i(p, pos, len, p->prio) ;
}
-static int fmt_nice (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_nice (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_i(p, pos, len, p->nice) ;
}
-static int fmt_threads (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_threads (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_64(p, pos, len, p->threads) ;
}
-static int fmt_timedate (pscan_t *p, size_t *pos, size_t *len, struct tm const *tm)
+static int fmt_timedate (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len, struct tm const *tm)
{
static struct tm nowtm = { .tm_year = 0 } ;
size_t tmplen ;
@@ -307,20 +314,19 @@ static int fmt_timedate (pscan_t *p, size_t *pos, size_t *len, struct tm const *
return 1 ;
}
-static int fmt_start (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_start (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
struct tm starttm ;
- unsigned int hz = gethz() ;
+ unsigned int hz = gethz(aux) ;
tain blah = { .sec = { .x = p->start / hz }, .nano = (p->start % hz) * (1000000000 / hz) } ;
- tain_add(&blah, &boottime, &blah) ;
+ tain_add(&blah, &aux->boottime, &blah) ;
if (!localtm_from_tai(&starttm, tain_secp(&blah), 1)) return 0 ;
- return fmt_timedate(p, pos, len, &starttm) ;
+ return fmt_timedate(aux, p, pos, len, &starttm) ;
}
-static unsigned int getpgsz (void)
+static unsigned int getpgsz (s6ps_auxinfo_t *aux)
{
- static unsigned int pgsz = 0 ;
- if (!pgsz)
+ if (!aux->pgsz)
{
long sz = sysconf(_SC_PAGESIZE) ;
if (sz < 1)
@@ -328,75 +334,81 @@ static unsigned int getpgsz (void)
char fmt[ULONG_FMT + 1] ;
fmt[long_fmt(fmt, sz)] = 0 ;
strerr_warnw3x("invalid _SC_PAGESIZE value (", fmt, "), using 4096") ;
- pgsz = 4096 ;
+ aux->pgsz = 4096 ;
}
- else pgsz = sz ;
+ else aux->pgsz = sz ;
}
- return pgsz ;
+ return aux->pgsz ;
}
-static int fmt_vsize (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_vsize (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_64(p, pos, len, p->vsize / 1024) ;
}
-static int fmt_rss (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_rss (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- return fmt_64(p, pos, len, p->rss * (getpgsz() / 1024)) ;
+ return fmt_64(p, pos, len, p->rss * (getpgsz(aux) / 1024)) ;
}
-static int fmt_rsslim (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_rsslim (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_64(p, pos, len, p->rsslim / 1024) ;
}
-static int fmt_cpuno (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_cpuno (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_64(p, pos, len, p->cpuno) ;
}
-static int fmt_rtprio (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_rtprio (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
+ (void)aux ;
return fmt_64(p, pos, len, p->rtprio) ;
}
-static int fmt_policy (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_policy (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
static char const *const policies[8] = { "NORMAL", "FIFO", "RR", "BATCH", "ISO", "IDLE", "UNKNOWN", "UNKNOWN" } ;
size_t tmppos = p->data.len ;
if (!stralloc_cats(&p->data, policies[p->policy & 7])) return 0 ;
*pos = tmppos ;
*len = p->data.len - tmppos ;
+ (void)aux ;
return 1 ;
}
-static int fmt_user (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_user (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
size_t tmppos = p->data.len ;
- if (!s6ps_pwcache_lookup(&p->data, p->uid)) return 0 ;
+ if (!s6ps_pwcache_lookup(&aux->caches[0], &p->data, p->uid)) return 0 ;
*pos = tmppos ;
*len = p->data.len - tmppos ;
return 1 ;
}
-static int fmt_group (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_group (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
size_t tmppos = p->data.len ;
- if (!s6ps_grcache_lookup(&p->data, p->gid)) return 0 ;
+ if (!s6ps_grcache_lookup(&aux->caches[1], &p->data, p->gid)) return 0 ;
*pos = tmppos ;
*len = p->data.len - tmppos ;
return 1 ;
}
-static struct sysinfo si = { .totalram = 0, .loads = { 0, 0, 0 } } ;
-
-static uint64_t gettotalmem (void)
+static uint64_t gettotalmem (s6ps_auxinfo_t *aux)
{
- uint64_t totalmem = 0 ;
- if (!si.totalram && (sysinfo(&si) < 0)) return 0 ;
- totalmem = si.totalram ;
- totalmem *= si.mem_unit ;
- return totalmem ;
+ if (!aux->totalmem)
+ {
+ struct sysinfo si = { .totalram = 0, .loads = { 0, 0, 0 } } ;
+ if (sysinfo(&si) < 0) return 0 ;
+ aux->totalmem = si.totalram ;
+ aux->totalmem *= si.mem_unit ;
+ }
+ return aux->totalmem ;
}
static int percent (stralloc *sa, unsigned int n, size_t *pos, size_t *len)
@@ -411,22 +423,22 @@ static int percent (stralloc *sa, unsigned int n, size_t *pos, size_t *len)
return 1 ;
}
-static int fmt_pmem (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_pmem (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- uint64 l = gettotalmem() ;
- return l ? percent(&p->data, p->rss * getpgsz() * 10000 / l, pos, len) : 0 ;
+ uint64_t l = gettotalmem(aux) ;
+ return l ? percent(&p->data, p->rss * getpgsz(aux) * 10000 / l, pos, len) : 0 ;
}
-static int fmt_wchan (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_wchan (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
size_t tmppos = p->data.len ;
- if (!s6ps_wchan_lookup(&p->data, p->wchan)) return 0 ;
+ if (!s6ps_wchan_lookup(&aux->wchan, &p->data, p->wchan)) return 0 ;
*len = p->data.len - tmppos ;
*pos = tmppos ;
return 1 ;
}
-static int fmt_args (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_args (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
if (!stralloc_readyplus(&p->data, (p->height << 2) + (p->cmdlen ? p->cmdlen : p->commlen + (p->data.s[p->state] == 'Z' ? 11 : 3))))
return 0 ;
@@ -459,10 +471,11 @@ static int fmt_args (pscan_t *p, size_t *pos, size_t *len)
else
stralloc_catb(&p->data, p->data.s + uint32_fmt(0, p->pid) + 1, p->commlen+2) ;
*len = p->data.len - *pos ;
+ (void)aux ;
return 1 ;
}
-static int fmt_env (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_env (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
size_t i = 0 ;
if (!p->envlen)
@@ -476,52 +489,53 @@ static int fmt_env (pscan_t *p, size_t *pos, size_t *len)
*len = p->envlen ;
for (; i < *len ; i++)
if (!p->data.s[*pos + i]) p->data.s[*pos + i] = ' ' ;
+ (void)aux ;
return 1 ;
}
-static uint64_t gettotalj (uint64_t j)
+static uint64_t gettotalj (s6ps_auxinfo_t *aux, uint64_t j)
{
tain totaltime ;
- unsigned int hz = gethz() ;
- tain_sub(&totaltime, &STAMP, &boottime) ;
+ unsigned int hz = gethz(aux) ;
+ tain_sub(&totaltime, &STAMP, &aux->boottime) ;
j = totaltime.sec.x * hz + totaltime.nano / (1000000000 / hz) - j ;
if (!j) j = 1 ;
return j ;
}
-static int fmt_pcpu (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_pcpu (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- return percent(&p->data, 10000 * (p->utime + p->stime) / gettotalj(p->start), pos, len) ;
+ return percent(&p->data, 10000 * (p->utime + p->stime) / gettotalj(aux, p->start), pos, len) ;
}
-static int fmt_ttime (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_ttime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- return fmt_jiffies(p, pos, len, p->utime + p->stime) ;
+ return fmt_jiffies(aux, p, pos, len, p->utime + p->stime) ;
}
-static int fmt_cttime (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_cttime (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- return fmt_jiffies(p, pos, len, p->utime + p->stime + p->cutime + p->cstime) ;
+ return fmt_jiffies(aux, p, pos, len, p->utime + p->stime + p->cutime + p->cstime) ;
}
-static int fmt_tstart (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_tstart (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- unsigned int hz = gethz() ;
+ unsigned int hz = gethz(aux) ;
tain blah = { .sec = { .x = p->start / hz }, .nano = (p->start % hz) * (1000000000 / hz) } ;
if (!stralloc_readyplus(&p->data, TIMESTAMP)) return 0 ;
- tain_add(&blah, &boottime, &blah) ;
+ tain_add(&blah, &aux->boottime, &blah) ;
*pos = p->data.len ;
*len = timestamp_fmt(p->data.s + p->data.len, &blah) ;
p->data.len += *len ;
return 1 ;
}
-static int fmt_cpcpu (pscan_t *p, size_t *pos, size_t *len)
+static int fmt_cpcpu (s6ps_auxinfo_t *aux, pscan_t *p, size_t *pos, size_t *len)
{
- return percent(&p->data, 10000 * (p->utime + p->stime + p->cutime + p->cstime) / gettotalj(p->start), pos, len) ;
+ return percent(&p->data, 10000 * (p->utime + p->stime + p->cutime + p->cstime) / gettotalj(aux, p->start), pos, len) ;
}
-static pfieldfmt_func_ref pfieldfmt_table[PFIELD_PHAIL] =
+static pfieldfmt_func_ref const pfieldfmt_table[PFIELD_PHAIL] =
{
&fmt_pid,
&fmt_comm,
@@ -558,4 +572,4 @@ static pfieldfmt_func_ref pfieldfmt_table[PFIELD_PHAIL] =
&fmt_cpcpu
} ;
-pfieldfmt_func_ref *s6ps_pfield_fmt = pfieldfmt_table ;
+pfieldfmt_func_ref const *const s6ps_pfield_fmt = pfieldfmt_table ;
diff --git a/src/libs6ps/s6ps_pwcache.c b/src/libs6ps/s6ps_pwcache.c
new file mode 100644
index 0000000..2c29977
--- /dev/null
+++ b/src/libs6ps/s6ps_pwcache.c
@@ -0,0 +1,47 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <pwd.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_pwcache_lookup (s6ps_cache_t *cache, stralloc *sa, uid_t uid)
+{
+ dius_t d = { .left = (uint32_t)uid, .right = satmp.len } ;
+ uint32_t i ;
+ if (!avltree_search(&cache->tree, &d.left, &i))
+ {
+ struct passwd *pw ;
+ size_t n = genalloc_len(dius_t, &cache->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(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 ;
+}
diff --git a/src/libps/s6ps_statparse.c b/src/libs6ps/s6ps_statparse.c
index ea46d70..8cc5a93 100644
--- a/src/libps/s6ps_statparse.c
+++ b/src/libs6ps/s6ps_statparse.c
@@ -9,7 +9,7 @@
#include <skalibs/stralloc.h>
#include <skalibs/tai.h>
-#include "s6-ps.h"
+#include "s6ps.h"
/*
diff --git a/src/libps/s6ps_ttycache.c b/src/libs6ps/s6ps_ttycache.c
index 27282e7..583ab03 100644
--- a/src/libps/s6ps_ttycache.c
+++ b/src/libs6ps/s6ps_ttycache.c
@@ -15,22 +15,8 @@
#include <skalibs/skamisc.h>
#include <skalibs/avltree.h>
-#include "s6-ps.h"
-
-static avltree ttycache_tree = AVLTREE_ZERO ;
-static genalloc ttycache_index = GENALLOC_ZERO ;
-
-int s6ps_ttycache_init (void)
-{
- avltree_init(&ttycache_tree, 5, 3, 8, &left_dtok, &uint32_cmp, &ttycache_index) ;
- return 1 ;
-}
-
-void s6ps_ttycache_finish (void)
-{
- avltree_free(&ttycache_tree) ;
- genalloc_free(dius_t, &ttycache_index) ;
-}
+#include "s6ps.h"
+#include "s6ps-internal.h"
static int check (char const *s, dev_t ttynr)
{
@@ -109,30 +95,24 @@ static int ttyguess (stralloc *sa, dev_t ttynr)
}
}
-int s6ps_ttycache_lookup (stralloc *sa, dev_t ttynr)
+int s6ps_ttycache_lookup (s6ps_cache_t *cache, stralloc *sa, dev_t ttynr)
{
- int wasnull = !satmp.s ;
dius_t d = { .left = (uint32_t)ttynr, .right = satmp.len } ;
uint32_t i ;
- if (!avltree_search(&ttycache_tree, &d.left, &i))
+ if (!avltree_search(&cache->tree, &d.left, &i))
{
- unsigned int n = genalloc_len(dius_t, &ttycache_index) ;
+ size_t n = genalloc_len(dius_t, &cache->index) ;
if (!ttyguess(&satmp, ttynr)) return 0 ;
- if (!genalloc_append(dius_t, &ttycache_index, &d)) goto err ;
- if (!avltree_insert(&ttycache_tree, n))
+ if (!genalloc_append(dius_t, &cache->index, &d)) goto err ;
+ if (!avltree_insert(&cache->tree, n))
{
- genalloc_setlen(dius_t, &ttycache_index, n) ;
+ genalloc_setlen(dius_t, &cache->index, n) ;
goto err ;
}
i = n ;
}
- return stralloc_cats(sa, satmp.s + genalloc_s(dius_t, &ttycache_index)[i].right) ;
+ return stralloc_cats(sa, satmp.s + genalloc_s(dius_t, &cache->index)[i].right) ;
err:
- {
- int e = errno ;
- if (wasnull) stralloc_free(&satmp) ;
- else satmp.len = d.right ;
- errno = e ;
- }
+ satmp.len = d.right ;
return 0 ;
}
diff --git a/src/libps/s6ps_wchan.c b/src/libs6ps/s6ps_wchan.c
index 9261693..c7c4348 100644
--- a/src/libps/s6ps_wchan.c
+++ b/src/libs6ps/s6ps_wchan.c
@@ -8,16 +8,13 @@
#include <skalibs/genalloc.h>
#include <skalibs/djbunix.h>
-#include "s6-ps.h"
+#include "s6ps.h"
-static stralloc sysmap = STRALLOC_ZERO ;
-static genalloc ind = GENALLOC_ZERO ;
-
-int s6ps_wchan_init (char const *file)
+int s6ps_wchan_init (s6ps_wchan_t *w, char const *file)
{
if (file)
{
- if (!openslurpclose(&sysmap, file)) return 0 ;
+ if (!openslurpclose(&w->sysmap, file)) return 0 ;
}
else
{
@@ -34,39 +31,39 @@ int s6ps_wchan_init (char const *file)
memcpy(buf + 17, uts.release, n + 1) ;
files[1] = buf ;
for (; i < 3 ; i++)
- if (openslurpclose(&sysmap, files[i])) break ;
+ if (openslurpclose(&w->sysmap, files[i])) break ;
if (i >= 3) return 0 ;
}
}
{
size_t i = 0 ;
- if (!genalloc_append(size_t, &ind, &i)) goto err2 ;
- for (i = 1 ; i <= sysmap.len ; i++)
- if (sysmap.s[i-1] == '\n')
- if (!genalloc_append(size_t, &ind, &i)) goto err ;
+ if (!genalloc_append(size_t, &w->ind, &i)) goto err2 ;
+ for (i = 1 ; i <= w->sysmap.len ; i++)
+ if (w->sysmap.s[i-1] == '\n')
+ if (!genalloc_append(size_t, &w->ind, &i)) goto err ;
}
return 1 ;
err:
- genalloc_free(size_t, &ind) ;
+ genalloc_free(size_t, &w->ind) ;
err2:
- stralloc_free(&sysmap) ;
+ stralloc_free(&w->sysmap) ;
return 0 ;
}
-void s6ps_wchan_finish (void)
+void s6ps_wchan_finish (s6ps_wchan_t *w)
{
- genalloc_free(size_t, &ind) ;
- stralloc_free(&sysmap) ;
+ genalloc_free(size_t, &w->ind) ;
+ stralloc_free(&w->sysmap) ;
}
-static inline size_t lookup (uint64_t addr, size_t *i)
+static inline size_t lookup (s6ps_wchan_t const *w, uint64_t addr, size_t *i)
{
- size_t low = 0, mid, high = genalloc_len(size_t, &ind), len ;
+ size_t low = 0, mid, high = genalloc_len(size_t, &w->ind), len ;
for (;;)
{
uint64_t cur ;
mid = (low + high) >> 1 ;
- len = uint64_xscan(sysmap.s + genalloc_s(size_t, &ind)[mid], &cur) ;
+ len = uint64_xscan(w->sysmap.s + genalloc_s(size_t, &w->ind)[mid], &cur) ;
if (!len) return 0 ;
if (cur == addr) break ;
if (mid == low) return 0 ;
@@ -76,17 +73,17 @@ static inline size_t lookup (uint64_t addr, size_t *i)
return len ;
}
-int s6ps_wchan_lookup (stralloc *sa, uint64_t addr)
+int s6ps_wchan_lookup (s6ps_wchan_t const *w, stralloc *sa, uint64_t addr)
{
if (addr == (sizeof(void *) == 8 ? 0xffffffffffffffffULL : 0xffffffffUL))
return stralloc_catb(sa, "*", 1) ;
if (!addr) return stralloc_catb(sa, "-", 1) ;
- if (sysmap.len)
+ if (w->sysmap.len)
{
- size_t i, pos, len = lookup(addr, &i) ;
+ size_t i, pos, len = lookup(w, addr, &i) ;
if (!len) return stralloc_catb(sa, "?", 1) ;
- pos = genalloc_s(size_t, &ind)[i] + len + 3 ;
- return stralloc_catb(sa, sysmap.s + pos, genalloc_s(size_t, &ind)[i+1] - 1 - pos) ;
+ pos = genalloc_s(size_t, &w->ind)[i] + len + 3 ;
+ return stralloc_catb(sa, w->sysmap.s + pos, genalloc_s(size_t, &w->ind)[i+1] - 1 - pos) ;
}
if (!stralloc_readyplus(sa, UINT64_FMT + 3)) return 0 ;
stralloc_catb(sa, "(0x", 3) ;
diff --git a/src/s6-linux-utils/s6-ps.c b/src/s6-linux-utils/s6-ps.c
index fe4b433..255b195 100644
--- a/src/s6-linux-utils/s6-ps.c
+++ b/src/s6-linux-utils/s6-ps.c
@@ -27,7 +27,7 @@
#include <skalibs/unix-transactional.h>
#include <skalibs/avltreen.h>
-#include "s6-ps.h"
+#include "s6ps.h"
#define USAGE "s6-ps [ -H ] [ -w spacing ] [ -W wchanfile ] [ -l | -o field,field... ]"
@@ -49,25 +49,12 @@
(1 << PFIELD_PCPU) | \
((uint64_t)1 << PFIELD_CPCPU))
-void *left_dtok (unsigned int d, void *x)
-{
- return (void *)&genalloc_s(dius_t, (genalloc *)x)[d].left ;
-}
-
-int uint32_cmp (void const *a, void const *b, void *x)
-{
- uint32_t aa = *(uint32_t *)a ;
- uint32_t bb = *(uint32_t *)b ;
- (void)x ;
- return (aa < bb) ? -1 : (aa > bb) ;
-}
-
static void *pid_dtok (unsigned int d, void *x)
{
return &((pscan_t *)x)[d].pid ;
}
-static int fillo_notree (unsigned int i, unsigned int h, void *x)
+static int ps_fillo_notree (unsigned int i, unsigned int h, void *x)
{
static unsigned int j = 0 ;
unsigned int *list = x ;
@@ -76,7 +63,7 @@ static int fillo_notree (unsigned int i, unsigned int h, void *x)
return 1 ;
}
-static inline unsigned int fieldscan (char const *s, pfield_t *list, uint64_t *fbf)
+static inline unsigned int ps_fieldscan (char const *s, pfield_t *list, uint64_t *fbf)
{
uint64_t bits = 0 ;
unsigned int n = 0 ;
@@ -105,7 +92,7 @@ static inline unsigned int fieldscan (char const *s, pfield_t *list, uint64_t *f
return n ;
}
-static int slurpit (unsigned int dirfd, stralloc *data, char const *buf, char const *what, size_t *len)
+static int ps_slurpit (unsigned int dirfd, stralloc *data, char const *buf, char const *what, size_t *len)
{
size_t start = data->len ;
int fd = open_readat(dirfd, what) ;
@@ -165,7 +152,7 @@ int main (int argc, char const *const *argv)
case 'W' : wchanfile = l.arg ; break ;
case 'o' :
{
- nfields = fieldscan(l.arg, fieldlist, &fbf) ;
+ nfields = ps_fieldscan(l.arg, fieldlist, &fbf) ;
break ;
}
default : strerr_dieusage(100, USAGE) ;
@@ -242,22 +229,22 @@ int main (int argc, char const *const *argv)
}
if (needstat)
{
- if (!slurpit(dirfd, &pscan.data, buf, "stat", &pscan.statlen))
+ if (!ps_slurpit(dirfd, &pscan.data, buf, "stat", &pscan.statlen))
goto errindir ;
- if (!slurpit(dirfd, &pscan.data, buf, "comm", &pscan.commlen))
+ if (!ps_slurpit(dirfd, &pscan.data, buf, "comm", &pscan.commlen))
goto errindir ;
if (pscan.commlen) { pscan.commlen-- ; pscan.data.len-- ; }
}
if (fbf & (1 << PFIELD_ARGS))
{
- if (!slurpit(dirfd, &pscan.data, buf, "cmdline", &pscan.cmdlen)) goto errindir ;
+ if (!ps_slurpit(dirfd, &pscan.data, buf, "cmdline", &pscan.cmdlen)) goto errindir ;
while (!pscan.data.s[pscan.data.len-1])
{
pscan.cmdlen-- ;
pscan.data.len-- ;
}
}
- if (fbf & (1 << PFIELD_ENV)) slurpit(dirfd, &pscan.data, buf, "environ", &pscan.envlen) ;
+ if (fbf & (1 << PFIELD_ENV)) ps_slurpit(dirfd, &pscan.data, buf, "environ", &pscan.envlen) ;
fd_close(dirfd) ;
if (!genalloc_append(pscan_t, &pscans, &pscan))
strerr_diefu1sys(111, "genalloc_append") ;
@@ -281,13 +268,14 @@ int main (int argc, char const *const *argv)
n = genalloc_len(pscan_t, &pscans) - 1 ;
{
- unsigned int orderedlist[n+1] ; /* 1st element will be 0, ignored */
+ s6ps_auxinfo_t aux = S6PS_AUXINFO_ZERO ;
unsigned int i = 0 ;
+ unsigned int orderedlist[n+1] ; /* 1st element will be 0, ignored */
/* Order the processes for display */
{
- AVLTREEN_DECLARE_AND_INIT(pidtree, n+1, &pid_dtok, &uint32_cmp, p) ;
+ AVLTREEN_DECLARE_AND_INIT(pidtree, n+1, &pid_dtok, &s6ps_uint32_cmp, p) ;
for (i = 0 ; i < n ; i++)
{
if (needstat && !s6ps_statparse(p+i))
@@ -299,7 +287,7 @@ int main (int argc, char const *const *argv)
strerr_diefu1sys(111, "avltreen_insert") ;
if (flagtree) s6ps_otree(p, n+1, &pidtree, orderedlist) ;
- else avltreen_iter_nocancel(&pidtree, avltreen_totalsize(&pidtree), &fillo_notree, orderedlist) ;
+ else avltreen_iter_nocancel(&pidtree, avltreen_totalsize(&pidtree), &ps_fillo_notree, orderedlist) ;
}
@@ -308,15 +296,15 @@ int main (int argc, char const *const *argv)
if (fbf & ((1 << PFIELD_START) | ((uint64_t)1 << PFIELD_TSTART) | (1 << PFIELD_PCPU) | ((uint64_t)1 << PFIELD_CPCPU)))
{
tain_wallclock_read_g() ;
- s6ps_compute_boottime(p, mypos) ;
+ s6ps_compute_boottime(&aux, p, mypos) ;
}
- if (fbf & (1 << PFIELD_USER) && !s6ps_pwcache_init())
+ if (fbf & (1 << PFIELD_USER) && !s6ps_cache_init(&aux.caches[0]))
strerr_diefu1sys(111, "init user name cache") ;
- if (fbf & (1 << PFIELD_GROUP) && !s6ps_grcache_init())
+ if (fbf & (1 << PFIELD_GROUP) && !s6ps_cache_init(&aux.caches[1]))
strerr_diefu1sys(111, "init group name cache") ;
- if (fbf & (1 << PFIELD_TTY) && !s6ps_ttycache_init())
+ if (fbf & (1 << PFIELD_TTY) && !s6ps_cache_init(&aux.caches[2]))
strerr_diefu1sys(111, "init tty name cache") ;
- if (fbf & (1 << PFIELD_WCHAN) && !s6ps_wchan_init(wchanfile))
+ if (fbf & (1 << PFIELD_WCHAN) && !s6ps_wchan_init(&aux.wchan, wchanfile))
{
if (wchanfile) strerr_warnwu2sys("init wchan file ", wchanfile) ;
else strerr_warnwu1sys("init wchan") ;
@@ -333,7 +321,7 @@ int main (int argc, char const *const *argv)
unsigned int j = 0 ;
for (; j < nfields ; j++)
{
- if (!(*s6ps_pfield_fmt[fieldlist[j]])(p+i, &fmtpos[i][j], &fmtlen[i][j]))
+ if (!(*s6ps_pfield_fmt[fieldlist[j]])(&aux, p+i, &fmtpos[i][j], &fmtlen[i][j]))
strerr_diefu1sys(111, "format fields") ;
if (fmtlen[i][j] > maxlen[j]) maxlen[j] = fmtlen[i][j] ;
}
@@ -341,10 +329,10 @@ int main (int argc, char const *const *argv)
for (i = 0 ; i < nfields ; i++)
if (maxlen[i] > maxspaces) maxspaces = maxlen[i] ;
maxspaces += spacing ;
- if (fbf & (1 << PFIELD_USER)) s6ps_pwcache_finish() ;
- if (fbf & (1 << PFIELD_GROUP)) s6ps_grcache_finish() ;
- if (fbf & (1 << PFIELD_TTY)) s6ps_ttycache_finish() ;
- if (fbf & (1 << PFIELD_WCHAN)) s6ps_wchan_finish() ;
+ if (fbf & (1 << PFIELD_USER)) s6ps_cache_finish(&aux.caches[0]) ;
+ if (fbf & (1 << PFIELD_GROUP)) s6ps_cache_finish(&aux.caches[1]) ;
+ if (fbf & (1 << PFIELD_TTY)) s6ps_cache_finish(&aux.caches[2]) ;
+ if (fbf & (1 << PFIELD_WCHAN)) s6ps_wchan_finish(&aux.wchan) ;
stralloc_free(&satmp) ;
{
char spaces[maxspaces] ;
diff --git a/tools/gen-deps.sh b/tools/gen-deps.sh
index 27e5b3e..befe021 100755
--- a/tools/gen-deps.sh
+++ b/tools/gen-deps.sh
@@ -7,6 +7,8 @@ echo '# This file has been generated by tools/gen-deps.sh'
echo '#'
echo
+internal_libs=
+
for dir in src/include/${package} src/* ; do
for file in $(ls -1 $dir | grep -- \\.h$) ; do
{
@@ -70,8 +72,12 @@ for dir in $(ls -1 src | grep -v ^include) ; do
echo else
echo "lib${file}.a.xyzzy:$(echo "$deps" | sed 's/\.o/.lo/g')"
echo endif
- echo "lib${file}.so.xyzzy: EXTRA_LIBS :=$libs"
- echo "lib${file}.so.xyzzy:$(echo "$deps" | sed 's/\.o/.lo/g')"
+ if grep -E "^LIB_DEFS [+:]=" package/targets.mak | grep -qF "$file" ; then
+ echo "lib${file}.so.xyzzy: EXTRA_LIBS :=$libs"
+ echo "lib${file}.so.xyzzy:$(echo "$deps" | sed 's/\.o/.lo/g')"
+ else
+ internal_libs="$internal_libs lib${file}.a.xyzzy"
+ fi
done
for file in $(ls -1 src/$dir/deps-exe) ; do
@@ -91,3 +97,4 @@ for dir in $(ls -1 src | grep -v ^include) ; do
echo "$file: src/$dir/$file.o$deps"
done
done
+echo "INTERNAL_LIBS :=$internal_libs"