feat: add users and sessions discovery

- also add clangd multi-file intellisense
This commit is contained in:
2024-07-10 23:24:51 +02:00
parent 4d8faa5b5f
commit 07d2842be0
11 changed files with 272 additions and 3 deletions

61
src/users.c Normal file
View File

@@ -0,0 +1,61 @@
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <users.h>
#include <util.h>
static struct user __new_user(struct passwd *p) {
struct user __user;
strcln(&__user.username, p->pw_name);
if (p->pw_gecos[0] == '\0')
__user.display_name = __user.username;
else
strcln(&__user.display_name, p->pw_gecos);
return __user;
}
static const u_int8_t bs = 16;
static const u_int8_t unit_size = sizeof(struct user);
static u_int16_t alloc_size = bs;
static u_int16_t used_size = 0;
static struct user *users = NULL;
static struct users_list *__users_list = NULL;
struct users_list __list;
// This code is designed to be run purely sinlge threaded
struct users_list *get_human_users() {
if (users != NULL)
return __users_list;
else
users = malloc(alloc_size * unit_size);
struct passwd *pwd;
while ((pwd = getpwent()) != NULL) {
// practically impossible to reach this (== 0xffff)
// but will prevent break
if (used_size == 0xffff ||
!(pwd->pw_dir && strncmp(pwd->pw_dir, "/home/", 6) == 0))
continue;
if (used_size >= alloc_size) {
alloc_size += bs;
users = realloc(users, alloc_size * unit_size);
}
users[used_size] = __new_user(pwd);
used_size++;
}
users = realloc(users, used_size * unit_size);
__list.length = used_size;
__list.users = users;
return __users_list = &__list;
}