fixes: improve login environment handling (#114)

* chore(organization): allow nested header and c files

* feat: add shell login "middleware" & etc:
  * lidm now calls `bash` (or other shells, depends on PACKAGE cfg) in
  login mode as a session wrapper to source most env (can be disabled)
  * this fixes a lot of env problems with all `/etc/profile` and more

Extra:
* implemented a musl compatible version of `execvpe` and now lidm should
  search for PATH everywhere it needs to
* `search_path` now also checks if the found binary is properly
  executable
* lidm now uses `confstr` for a decent PATH default if none is found
* logs are unbuffered for cases where debug logs appear empty (exit
  without handlers moment)

* chore: one-time evaluate certain makefile vars

---------

Co-authored-by: grialion <48643945+grialion@users.noreply.github.com>
This commit is contained in:
2026-02-07 17:12:03 +01:00
committed by GitHub
parent c009c6c31c
commit 0b2d0bdf03
34 changed files with 555 additions and 424 deletions

View File

@@ -1,10 +1,11 @@
// TODO: handle `fork() == -1`// TODO: handle `fork() == -1`s
// TODO: handle `fork() == -1`
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <security/pam_misc.h>
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -21,7 +22,8 @@
#include "pam.h"
#include "sessions.h"
#include "ui.h"
#include "util.h"
#include "util/path.h"
#include "util/vec.h"
#define XORG_MESSAGE_LENGTH 16
@@ -80,16 +82,6 @@ static void source_paths(struct Vector* NNULLABLE vec_envlist,
}
}
/*char *buf;*/
/*size_t bsize = snprintf(NULL, 0, "/run/user/%d", pw->pw_uid) + 1;*/
/*buf = malloc(bsize);*/
/*snprintf(buf, bsize, "/run/user/%d", pw->pw_uid);*/
/*setenv("XDG_RUNTIME_DIR", buf, true);*/
/*setenv("XDG_SESSION_CLASS", "user", true);*/
/*setenv("XDG_SESSION_ID", "1", true);*/
/*setenv("XDG_SESSION_DESKTOP", , true);*/
/*setenv("XDG_SEAT", "seat0", true);*/
struct child_msg {
char* msg;
int _errno;
@@ -176,7 +168,8 @@ static void start_xorg_server(struct passwd* pw, char** NNULLABLE envlist,
// TODO: add error msgs
/// returns on completion
static void launch_with_xorg_server(struct session_exec* NNULLABLE exec,
static void launch_with_xorg_server(struct config* config,
struct session_exec* NNULLABLE exec,
struct passwd* pw,
char** NNULLABLE envlist) {
int xorg_pipefd[2];
@@ -212,10 +205,14 @@ static void launch_with_xorg_server(struct session_exec* NNULLABLE exec,
(void)fflush(NULL);
pid_t xorg_session_pid = fork();
if (xorg_session_pid == 0) {
int exit = session_exec_exec(exec, envlist);
perror("exec error");
if (config->behavior.bypass_shell_login)
session_exec_exec(exec, envlist);
else
session_exec_login_through_shell(exec, envlist);
perror("session exec error");
(void)fputs("failure calling session\n", stderr);
_exit(exit);
_exit(EXIT_FAILURE);
}
// looks weird, waiting on -1 should wait on any child and then just check if
@@ -254,8 +251,8 @@ static void launch_with_xorg_server(struct session_exec* NNULLABLE exec,
char _dummy; \
read(pipefd[0], &_dummy, sizeof(_dummy)); \
}
inline static void forked(int pipefd[2], struct passwd* pw,
char* NNULLABLE user,
inline static void forked(struct config* config, int pipefd[2],
struct passwd* pw, char* NNULLABLE user,
struct session* NNULLABLE session,
char** NNULLABLE envlist) {
if (chdir(pw->pw_dir) == -1) SEND_ERR("chdir");
@@ -267,14 +264,26 @@ inline static void forked(int pipefd[2], struct passwd* pw,
DUMMY_READ();
close(pipefd[0]);
if (!getenv("PATH")) {
size_t path_size = confstr(_CS_PATH, NULL, 0);
char* path_env = malloc(path_size);
confstr(_CS_PATH, path_env, path_size);
setenv("PATH", path_env, 1);
free(path_env);
}
log_printf(" [I] using shell login?: %s",
config->behavior.bypass_shell_login ? "true" : "false");
if (session->type == XORG) {
launch_with_xorg_server(&session->exec, pw, envlist);
launch_with_xorg_server(config, &session->exec, pw, envlist);
_exit(EXIT_SUCCESS);
} else {
int exit = session_exec_exec(&session->exec, envlist);
perror("exec error");
(void)fputs("failure calling session\n", stderr);
_exit(exit);
if (config->behavior.bypass_shell_login)
session_exec_exec(&session->exec, envlist);
else
session_exec_login_through_shell(&session->exec, envlist);
perror("session exec error");
_exit(EXIT_FAILURE);
}
}
#undef SEND_MSG
@@ -325,7 +334,7 @@ bool launch(char* user, char* passwd, struct session session, void (*cb)(void),
uint pid = fork();
if (pid == 0)
forked(pipefd, pw, user, &session, envlist);
forked(config, pipefd, pw, user, &session, envlist);
else {
struct child_msg msg;
read(pipefd[0], &msg, sizeof(struct child_msg));