From 01ddd6285284716325bd4b1c9bc5a8a0b9590f66 Mon Sep 17 00:00:00 2001 From: javalsai Date: Fri, 18 Oct 2024 21:54:39 +0200 Subject: [PATCH 01/11] chore: make specific vector (heap stack) impl --- include/util.h | 16 ++++++++++++++ src/util.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/include/util.h b/include/util.h index fcfca0c..cd191d9 100644 --- a/include/util.h +++ b/include/util.h @@ -3,6 +3,7 @@ #include #include +#include #include enum keys find_keyname(char *); @@ -10,4 +11,19 @@ enum keys find_ansi(char *); void read_press(u_char *, char *); void strcln(char **dest, const char *source); +struct Vector { + uint32_t length; + uint32_t alloc_len; + uint16_t alloc_size; + void** pages; +}; + +struct Vector vec_new(); +int vec_push(struct Vector*, void* item); +void vec_free(struct Vector*); +void vec_clear(struct Vector*); +void vec_reset(struct Vector*); +void* vec_pop(struct Vector*); // wont free it, nor shrink vec list space +void* vec_get(struct Vector*, uint32_t index); + #endif diff --git a/src/util.c b/src/util.c index a15ce80..1b44127 100644 --- a/src/util.c +++ b/src/util.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -66,3 +67,59 @@ static int selret_magic() { timeout.tv_usec = 0; return select(1, &set, NULL, NULL, &timeout); } + + +// Vector shii +struct Vector vec_new() { + struct Vector vec; + vec_clear(&vec); + return vec; +} + +int vec_push(struct Vector* vec, void* item) { + if (vec->length >= vec->alloc_len) { + uint32_t new_size = vec->alloc_len + vec->alloc_size; + void **new_location = realloc(vec->pages, vec->alloc_size); + if (new_location != NULL) { + vec->alloc_size = new_size; + vec->pages = new_location; + } else { + return -1; + } + } + + vec->pages[vec->length] = item; + vec->length++; + return 0; +} + +void vec_free(struct Vector* vec) { + for(; vec->length > 0; vec->length--) + free(vec->pages[--vec->length]); + + vec_clear(vec); +} + +void vec_clear(struct Vector* vec) { + free(vec->pages); + vec_reset(vec); +} + +void vec_reset(struct Vector* vec) { + vec->length = 0; + vec->alloc_len = 0; + vec->alloc_size = 4096; // 4KiB page size? + vec->pages = NULL; +} + +void* vec_pop(struct Vector* vec) { + if (vec->length == 0) + return NULL; + + vec->length--; + return vec->pages[vec->length]; +} + +void* vec_get(struct Vector* vec, uint32_t index) { + return vec->pages[index]; +} From a0b68491ba9553995adc56f97b02990da80a176b Mon Sep 17 00:00:00 2001 From: javalsai Date: Sat, 19 Oct 2024 17:48:20 +0200 Subject: [PATCH 02/11] chore: implement vectors also seems to address several of these: https://github.com/javalsai/lidm/issues/3#issuecomment-2423933131 not sure about the issue itself, might be permission stuff --- include/sessions.h | 9 +++----- include/ui.h | 3 ++- include/users.h | 9 +++----- src/main.c | 7 +++--- src/sessions.c | 54 ++++++++++++---------------------------------- src/ui.c | 10 ++++----- src/users.c | 38 ++++++-------------------------- 7 files changed, 38 insertions(+), 92 deletions(-) diff --git a/include/sessions.h b/include/sessions.h index e5e9a1e..1019410 100644 --- a/include/sessions.h +++ b/include/sessions.h @@ -3,6 +3,8 @@ #include +#include + enum session_type { XORG, WAYLAND, @@ -16,11 +18,6 @@ struct session { enum session_type type; }; -struct sessions_list { - u_int16_t length; - struct session *sessions; -}; - -struct sessions_list *get_avaliable_sessions(); +struct Vector get_avaliable_sessions(); #endif diff --git a/include/ui.h b/include/ui.h index fdb08b1..c6bb884 100644 --- a/include/ui.h +++ b/include/ui.h @@ -2,9 +2,10 @@ #define _UIH_ #include +#include void setup(struct config); -int load(struct users_list *, struct sessions_list *); +int load(struct Vector * users, struct Vector * sessions); void print_err(const char *); void print_errno(const char *); diff --git a/include/users.h b/include/users.h index d00354d..60fac69 100644 --- a/include/users.h +++ b/include/users.h @@ -3,17 +3,14 @@ #include +#include + struct user { char *shell; char *username; char *display_name; }; -struct users_list { - u_int16_t length; - struct user *users; -}; - -struct users_list *get_human_users(); +struct Vector get_human_users(); #endif diff --git a/src/main.c b/src/main.c index c09ceba..a5f0425 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ #include #include #include +#include int main(int argc, char *argv[]) { if (argc == 2) @@ -23,10 +24,10 @@ int main(int argc, char *argv[]) { } setup(*config); - struct users_list *users = get_human_users(); - struct sessions_list *sessions = get_avaliable_sessions(); + struct Vector users = get_human_users(); + struct Vector sessions = get_avaliable_sessions(); - int ret = load(users, sessions); + int ret = load(&users, &sessions); if (ret == 0) execl(argv[0], argv[0], NULL); } diff --git a/src/sessions.c b/src/sessions.c index 4086ff1..8ea36b9 100644 --- a/src/sessions.c +++ b/src/sessions.c @@ -18,7 +18,6 @@ static const struct source_dir sources[] = { {XORG, "/usr/share/xsessions"}, {WAYLAND, "/usr/share/wayland-sessions"}, }; -static const size_t sources_size = sizeof(sources) / sizeof(sources[0]); static struct session __new_session(enum session_type type, char *name, const char *exec, const char *tryexec) { @@ -31,24 +30,12 @@ static struct session __new_session(enum session_type type, char *name, return __session; } -static const u_int8_t bs = 16; -static const u_int8_t unit_size = sizeof(struct session); - -static u_int16_t alloc_size = bs; -static u_int16_t used_size = 0; - -static struct session *sessions = NULL; -static struct sessions_list *__sessions_list = NULL; +static struct Vector *cb_sessions = NULL; // NOTE: commented printf's here would be nice to have debug logs if I ever // implement it static enum session_type session_type; static int fn(const char *fpath, const struct stat *sb, int typeflag) { - // practically impossible to reach this - // but will prevent break - if (used_size == 0xffff) - return 0; - if (sb == NULL || !S_ISREG(sb->st_mode)) return 0; @@ -56,7 +43,7 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) { FILE *fd = fopen(fpath, "r"); if (fd == NULL) { perror("fopen"); - fprintf(stderr, "error opening file (r) %s\n", fpath); + fprintf(stderr, "error opening file (r) '%s'\n", fpath); return 0; } @@ -66,8 +53,8 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) { char *name_buf = NULL; char *exec_buf = NULL; char *tryexec_buf = NULL; + // This should be made a specific function while (true) { - /*printf(".");*/ char *buf = malloc(sb->st_blksize); ssize_t read_size = getline(&buf, &alloc_size, fd); if (read_size == -1) { @@ -102,17 +89,10 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) { // just add this to the list if (name_buf != NULL && exec_buf != NULL) { - /*printf("gonna add to session list\n");*/ - if (used_size >= alloc_size) { - alloc_size += bs; - sessions = realloc(sessions, alloc_size * unit_size); - } - - /*printf("n %s\ne %s\nte %s\n", name_buf, exec_buf, tryexec_buf);*/ - sessions[used_size] = __new_session(session_type, name_buf, exec_buf, - tryexec_buf == NULL ? "" : tryexec_buf); - - used_size++; + struct session *session_i = malloc(sizeof (struct session)); + *session_i = __new_session(session_type, name_buf, exec_buf, + tryexec_buf == NULL ? "" : tryexec_buf); + vec_push(cb_sessions, session_i); } if (name_buf != NULL) @@ -125,23 +105,17 @@ static int fn(const char *fpath, const struct stat *sb, int typeflag) { return 0; } -static struct sessions_list __list; // This code is designed to be run purely single threaded -struct sessions_list *get_avaliable_sessions() { - if (sessions != NULL) - return __sessions_list; - else - sessions = malloc(alloc_size * unit_size); +struct Vector get_avaliable_sessions() { + struct Vector sessions = vec_new(); - for (uint i = 0; i < sources_size; i++) { - session_type = sources[i].type; + cb_sessions = &sessions; + for (uint i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) { /*printf("recurring into %s\n", sources[i].dir);*/ + session_type = sources[i].type; ftw(sources[i].dir, &fn, 1); } + cb_sessions = NULL; - sessions = realloc(sessions, used_size * unit_size); - - __list.length = used_size; - __list.sessions = sessions; - return __sessions_list = &__list; + return sessions; } diff --git a/src/ui.c b/src/ui.c index ac1e50f..f6bcdff 100644 --- a/src/ui.c +++ b/src/ui.c @@ -179,8 +179,8 @@ struct opt_field of_session; struct opt_field of_user; struct opt_field of_passwd; -struct users_list *gusers; -struct sessions_list *gsessions; +struct Vector *gusers; +struct Vector *gsessions; // not *that* OF tho struct opt_field *get_of(enum input from) { @@ -217,7 +217,7 @@ void ffield_cursor_focus() { struct user get_current_user() { if (of_user.current_opt != 0) - return gusers->users[of_user.current_opt - 1]; + return *(struct user*)vec_get(gusers, of_user.current_opt - 1); else { struct user custom_user; custom_user.shell = "/usr/bin/bash"; @@ -237,7 +237,7 @@ struct session get_current_session() { shell_session.exec = shell_session.name = get_current_user().shell; return shell_session; } else - return gsessions->sessions[of_session.current_opt - 1]; + return *(struct session*)vec_get(gsessions, of_session.current_opt - 1); } else { struct session custom_session; custom_session.type = SHELL; @@ -320,7 +320,7 @@ void ffield_type(char *text) { print_ffield(); } -int load(struct users_list *users, struct sessions_list *sessions) { +int load(struct Vector *users, struct Vector *sessions) { /// SETUP gusers = users; gsessions = sessions; diff --git a/src/users.c b/src/users.c index 810fd00..e86bff5 100644 --- a/src/users.c +++ b/src/users.c @@ -20,43 +20,19 @@ static struct user __new_user(struct passwd *p) { 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 single threaded -struct users_list *get_human_users() { - if (users != NULL) - return __users_list; - else - users = malloc(alloc_size * unit_size); +struct Vector get_human_users() { + struct Vector users = vec_new(); 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)) + if (!(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++; + struct user *user_i = malloc(sizeof(struct user)); + *user_i = __new_user(pwd); + vec_push(&users, user_i); } - users = realloc(users, used_size * unit_size); - - __list.length = used_size; - __list.users = users; - return __users_list = &__list; + return users; } From d30d5a8884022a2d50c3f24777db73d619a7e8ea Mon Sep 17 00:00:00 2001 From: javalsai Date: Sat, 19 Oct 2024 17:52:33 +0200 Subject: [PATCH 03/11] =?UTF-8?q?grammar=20=F0=9F=91=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/util.h b/include/util.h index cd191d9..be9bdce 100644 --- a/include/util.h +++ b/include/util.h @@ -23,7 +23,7 @@ int vec_push(struct Vector*, void* item); void vec_free(struct Vector*); void vec_clear(struct Vector*); void vec_reset(struct Vector*); -void* vec_pop(struct Vector*); // wont free it, nor shrink vec list space +void* vec_pop(struct Vector*); // won't free it, nor shrink vec list space void* vec_get(struct Vector*, uint32_t index); #endif From 2c606cda2ce161b97ec444054dbb31ac6943a832 Mon Sep 17 00:00:00 2001 From: javalsai Date: Sat, 19 Oct 2024 18:12:49 +0200 Subject: [PATCH 04/11] =?UTF-8?q?fix:=20ci=20builds=20(hopefully=20?= =?UTF-8?q?=F0=9F=A4=9E)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/uraimo/run-on-arch-action/issues/152#issuecomment-2401981347 --- .github/workflows/check-and-build.yml | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/.github/workflows/check-and-build.yml b/.github/workflows/check-and-build.yml index f79e81b..35c9f22 100644 --- a/.github/workflows/check-and-build.yml +++ b/.github/workflows/check-and-build.yml @@ -136,8 +136,9 @@ jobs: - uses: uraimo/run-on-arch-action@v2 with: - arch: aarch64 - distro: ubuntu22.04 + arch: none + distro: none + base_image: '--platform=linux/aarch64 aarch64/ubuntu22.04' githubToken: ${{ github.token }} install: | apt-get update && \ @@ -180,8 +181,9 @@ jobs: - uses: uraimo/run-on-arch-action@v2 with: - arch: armv7 - distro: ubuntu22.04 + arch: none + distro: none + base_image: '--platform=linux/armv7 armv7/ubuntu22.04' githubToken: ${{ github.token }} install: | apt-get update && \ @@ -224,8 +226,9 @@ jobs: - uses: uraimo/run-on-arch-action@v2 with: - arch: riscv64 - distro: ubuntu22.04 + arch: none + distro: none + base_imgae: '--platform=linux/riscv64 riscv64/ubuntu22.04' githubToken: ${{ github.token }} install: | apt-get update && \ From 43447ca415a429737c60abccd3452be80bc109c6 Mon Sep 17 00:00:00 2001 From: javalsai Date: Sat, 19 Oct 2024 18:15:55 +0200 Subject: [PATCH 05/11] =?UTF-8?q?grammar=20=F0=9F=91=8D=20(again)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/check-and-build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check-and-build.yml b/.github/workflows/check-and-build.yml index 35c9f22..89c493b 100644 --- a/.github/workflows/check-and-build.yml +++ b/.github/workflows/check-and-build.yml @@ -228,7 +228,7 @@ jobs: with: arch: none distro: none - base_imgae: '--platform=linux/riscv64 riscv64/ubuntu22.04' + base_image: '--platform=linux/riscv64 riscv64/ubuntu22.04' githubToken: ${{ github.token }} install: | apt-get update && \ From ab6703416f587f59c41fad0e1021a9501f4cb774 Mon Sep 17 00:00:00 2001 From: javalsai Date: Sat, 19 Oct 2024 18:16:40 +0200 Subject: [PATCH 06/11] fix: still fails (version?) --- .github/workflows/check-and-build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/check-and-build.yml b/.github/workflows/check-and-build.yml index 89c493b..8056961 100644 --- a/.github/workflows/check-and-build.yml +++ b/.github/workflows/check-and-build.yml @@ -138,7 +138,7 @@ jobs: with: arch: none distro: none - base_image: '--platform=linux/aarch64 aarch64/ubuntu22.04' + base_image: '--platform=linux/aarch64 aarch64/ubuntu:22.04' githubToken: ${{ github.token }} install: | apt-get update && \ @@ -183,7 +183,7 @@ jobs: with: arch: none distro: none - base_image: '--platform=linux/armv7 armv7/ubuntu22.04' + base_image: '--platform=linux/armv7 armv7/ubuntu:22.04' githubToken: ${{ github.token }} install: | apt-get update && \ @@ -228,7 +228,7 @@ jobs: with: arch: none distro: none - base_image: '--platform=linux/riscv64 riscv64/ubuntu22.04' + base_image: '--platform=linux/riscv64 riscv64/ubuntu:22.04' githubToken: ${{ github.token }} install: | apt-get update && \ From 6489df2e4175a7ffe7ece45b113da4dba26ef0dc Mon Sep 17 00:00:00 2001 From: javalsai Date: Sat, 19 Oct 2024 18:31:52 +0200 Subject: [PATCH 07/11] fix: idk lol, versions again? --- .github/workflows/check-and-build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check-and-build.yml b/.github/workflows/check-and-build.yml index 8056961..8b432d0 100644 --- a/.github/workflows/check-and-build.yml +++ b/.github/workflows/check-and-build.yml @@ -138,7 +138,7 @@ jobs: with: arch: none distro: none - base_image: '--platform=linux/aarch64 aarch64/ubuntu:22.04' + base_image: '--platform=linux/aarch64 aarch64/ubuntu:latest' githubToken: ${{ github.token }} install: | apt-get update && \ @@ -183,7 +183,7 @@ jobs: with: arch: none distro: none - base_image: '--platform=linux/armv7 armv7/ubuntu:22.04' + base_image: '--platform=linux/armv7 armv7/ubuntu:latest' githubToken: ${{ github.token }} install: | apt-get update && \ From d7bfa79284463531a76503a5dda3cb394090c41a Mon Sep 17 00:00:00 2001 From: javalsai Date: Sat, 19 Oct 2024 18:49:26 +0200 Subject: [PATCH 08/11] finally fix? --- .github/workflows/check-and-build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check-and-build.yml b/.github/workflows/check-and-build.yml index 8b432d0..53a8d6f 100644 --- a/.github/workflows/check-and-build.yml +++ b/.github/workflows/check-and-build.yml @@ -138,7 +138,7 @@ jobs: with: arch: none distro: none - base_image: '--platform=linux/aarch64 aarch64/ubuntu:latest' + base_image: '--platform=linux/aarch64 ubuntu:22.04' githubToken: ${{ github.token }} install: | apt-get update && \ @@ -183,7 +183,7 @@ jobs: with: arch: none distro: none - base_image: '--platform=linux/armv7 armv7/ubuntu:latest' + base_image: '--platform=linux/arm/v7 ubuntu:22.04' githubToken: ${{ github.token }} install: | apt-get update && \ From 49e3ad528f24e3e28464bccefe630c3e8a2916fe Mon Sep 17 00:00:00 2001 From: javalsai Date: Fri, 1 Nov 2024 13:11:01 +0100 Subject: [PATCH 09/11] add: safety bound checks for `vec_get` --- src/util.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/util.c b/src/util.c index 1b44127..17cd66d 100644 --- a/src/util.c +++ b/src/util.c @@ -121,5 +121,8 @@ void* vec_pop(struct Vector* vec) { } void* vec_get(struct Vector* vec, uint32_t index) { + if (index >= vec->length) + return NULL; + return vec->pages[index]; } From 06fa419a6c1c8e3fab1ee7540162c1de3b7d3fac Mon Sep 17 00:00:00 2001 From: javalsai Date: Fri, 1 Nov 2024 13:25:38 +0100 Subject: [PATCH 10/11] =?UTF-8?q?fix:=20logic=20=F0=9F=91=8D=20(tf=20was?= =?UTF-8?q?=20i=20thinking=20here=3F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.c b/src/util.c index 17cd66d..cf542ca 100644 --- a/src/util.c +++ b/src/util.c @@ -94,7 +94,7 @@ int vec_push(struct Vector* vec, void* item) { } void vec_free(struct Vector* vec) { - for(; vec->length > 0; vec->length--) + while(vec->length > 0) free(vec->pages[--vec->length]); vec_clear(vec); From b8caf5b7d7eca9bc3bc20d51aa32ec8a220b3d97 Mon Sep 17 00:00:00 2001 From: javalsai Date: Fri, 1 Nov 2024 13:28:32 +0100 Subject: [PATCH 11/11] chore: readable vec_pop --- src/util.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/util.c b/src/util.c index cf542ca..877ddd7 100644 --- a/src/util.c +++ b/src/util.c @@ -116,8 +116,7 @@ void* vec_pop(struct Vector* vec) { if (vec->length == 0) return NULL; - vec->length--; - return vec->pages[vec->length]; + return vec->pages[--vec->length]; } void* vec_get(struct Vector* vec, uint32_t index) {