fix: use getuser() syscall for determining username rather than session.toml hack

This commit is contained in:
2026-04-01 11:44:13 +02:00
parent 8e63b75fe7
commit 6276f8b162
8 changed files with 40 additions and 67 deletions
+8 -13
View File
@@ -297,10 +297,10 @@ namespace user {
return false; return false;
} }
// ---- Session (current logged-in user) ---- // ---- Session state and current user helpers ----
// Write the current session after successful login. // Persist login session state after successful login.
// Stores username in 0:/config/session.toml so any app can query it. // Current processes should use getuser() for their active username.
inline void set_session(const char* username) { inline void set_session(const char* username) {
montauk::toml::Doc doc; montauk::toml::Doc doc;
doc.init(); doc.init();
@@ -314,19 +314,14 @@ namespace user {
montauk::fdelete("0:/config/session.toml"); montauk::fdelete("0:/config/session.toml");
} }
// Read the current username into buf. Returns true if a session exists. // Read the current process username into buf. Returns true if one is set.
inline bool get_session_username(char* buf, int bufSz) { inline bool get_session_username(char* buf, int bufSz) {
auto doc = config::load("session"); if (buf == nullptr || bufSz <= 0) return false;
const char* name = doc.get_string("session.username", ""); int len = montauk::getuser(buf, (uint64_t)bufSz);
bool found = (name[0] != '\0'); return len > 0 && buf[0] != '\0';
if (found) {
montauk::strncpy(buf, name, bufSz);
}
doc.destroy();
return found;
} }
// Get the current user's home directory. Returns true if a session exists. // Get the current process user's home directory. Returns true if a user is set.
inline bool get_home_dir(char* buf, int bufSz) { inline bool get_home_dir(char* buf, int bufSz) {
char username[32]; char username[32];
if (!get_session_username(username, sizeof(username))) return false; if (!get_session_username(username, sizeof(username))) return false;
+1 -1
View File
@@ -509,7 +509,7 @@ static bool try_login(LoginState* ls) {
return false; return false;
} }
// Write session so any app can query the current user // Persist login session state for the active desktop session.
montauk::user::set_session(ls->username); montauk::user::set_session(ls->username);
return true; return true;
} }
+5 -5
View File
@@ -7,7 +7,6 @@
#include <montauk/syscall.h> #include <montauk/syscall.h>
#include <montauk/string.h> #include <montauk/string.h>
#include <montauk/heap.h> #include <montauk/heap.h>
#include <montauk/config.h>
// ==== ANSI color codes ==== // ==== ANSI color codes ====
@@ -81,8 +80,11 @@ extern "C" void _start() {
Montauk::SysInfo sysinfo; Montauk::SysInfo sysinfo;
montauk::get_info(&sysinfo); montauk::get_info(&sysinfo);
auto doc = montauk::config::load("session"); const char* username = "user";
const char* username = doc.get_string("session.username", "user"); char username_buf[32] = {};
if (montauk::getuser(username_buf, sizeof(username_buf)) > 0 && username_buf[0]) {
username = username_buf;
}
uint64_t ms = montauk::get_milliseconds(); uint64_t ms = montauk::get_milliseconds();
uint64_t total_secs = ms / 1000; uint64_t total_secs = ms / 1000;
@@ -260,8 +262,6 @@ extern "C" void _start() {
p = app(p, RST); p = app(p, RST);
} }
doc.destroy();
// ==== Print output ==== // ==== Print output ====
montauk::putchar('\n'); montauk::putchar('\n');
+4 -10
View File
@@ -1500,20 +1500,14 @@ extern "C" void _start() {
music_visualizer::reset(g.visualizer); music_visualizer::reset(g.visualizer);
// Parse arguments: accept a directory path or a file path // Parse arguments: accept a directory path or a file path
// Helper: set dir_path to current user's home via session config // Helper: set dir_path to current user's home directory
auto set_user_home = [&]() { auto set_user_home = [&]() {
auto doc = montauk::config::load("session"); char username[32] = {};
const char* name = doc.get_string("session.username", ""); if (montauk::getuser(username, sizeof(username)) > 0 && username[0]) {
if (name[0]) { snprintf(g.dir_path, sizeof(g.dir_path), "0:/users/%s", username);
int p = 0;
const char* pfx = "0:/users/";
while (*pfx && p < (int)sizeof(g.dir_path) - 1) g.dir_path[p++] = *pfx++;
while (*name && p < (int)sizeof(g.dir_path) - 1) g.dir_path[p++] = *name++;
g.dir_path[p] = '\0';
} else { } else {
memcpy(g.dir_path, "0:/home", 8); memcpy(g.dir_path, "0:/home", 8);
} }
doc.destroy();
}; };
char arg_file[128] = {}; char arg_file[128] = {};
+1 -9
View File
@@ -8,7 +8,6 @@
#include <montauk/syscall.h> #include <montauk/syscall.h>
#include <montauk/string.h> #include <montauk/string.h>
#include <montauk/heap.h> #include <montauk/heap.h>
#include <montauk/config.h>
extern "C" { extern "C" {
#include <string.h> #include <string.h>
@@ -114,18 +113,11 @@ extern "C" void _start() {
// Build output path: 0:/users/<username>/Pictures/screenshot_YYYYMMDD_HHMMSS.jpg // Build output path: 0:/users/<username>/Pictures/screenshot_YYYYMMDD_HHMMSS.jpg
char username[32] = {}; char username[32] = {};
{ if (montauk::getuser(username, sizeof(username)) <= 0 || username[0] == '\0') {
auto doc = montauk::config::load("session");
const char* name = doc.get_string("session.username", "");
if (name[0] == '\0') {
doc.destroy();
montauk::mfree(jpeg.data); montauk::mfree(jpeg.data);
montauk::print("screenshot: no user session\n"); montauk::print("screenshot: no user session\n");
montauk::exit(1); montauk::exit(1);
} }
montauk::strncpy(username, name, sizeof(username) - 1);
doc.destroy();
}
char home[128]; char home[128];
snprintf(home, sizeof(home), "0:/users/%s", username); snprintf(home, sizeof(home), "0:/users/%s", username);
+4 -7
View File
@@ -7,7 +7,6 @@
#include <montauk/syscall.h> #include <montauk/syscall.h>
#include <montauk/string.h> #include <montauk/string.h>
#include <montauk/heap.h> #include <montauk/heap.h>
#include <montauk/config.h>
#include <gui/gui.hpp> #include <gui/gui.hpp>
#include <gui/standalone.hpp> #include <gui/standalone.hpp>
#include <gui/truetype.hpp> #include <gui/truetype.hpp>
@@ -1255,19 +1254,17 @@ static bool handle_key(const Montauk::KeyEvent& key, bool& quit) {
} }
static void set_default_dir() { static void set_default_dir() {
auto doc = montauk::config::load("session"); char username[32] = {};
const char* name = doc.get_string("session.username", ""); if (montauk::getuser(username, sizeof(username)) > 0 && username[0]) {
if (name[0]) { snprintf(g.dir_path, sizeof(g.dir_path), "0:/users/%s/Videos", username);
snprintf(g.dir_path, sizeof(g.dir_path), "0:/users/%s/Videos", name);
const char* probe[1]; const char* probe[1];
int ok = montauk::readdir(g.dir_path, probe, 1); int ok = montauk::readdir(g.dir_path, probe, 1);
if (ok < 0) { if (ok < 0) {
snprintf(g.dir_path, sizeof(g.dir_path), "0:/users/%s", name); snprintf(g.dir_path, sizeof(g.dir_path), "0:/users/%s", username);
} }
} else { } else {
montauk::strncpy(g.dir_path, "0:/home", (int)sizeof(g.dir_path)); montauk::strncpy(g.dir_path, "0:/home", (int)sizeof(g.dir_path));
} }
doc.destroy();
} }
static void parse_args(char* auto_file, int auto_file_cap) { static void parse_args(char* auto_file, int auto_file_cap) {
+6 -6
View File
@@ -519,11 +519,11 @@ bool config::unset(toml::Doc* doc, const char* key);
### Example ### Example
```cpp ```cpp
auto doc = montauk::config::load("session"); auto doc = montauk::config::load("desktop");
const char* user = doc.get_string("session.username", "guest"); const char* theme = doc.get_string("appearance.theme", "light");
montauk::config::set_string(&doc, "session.theme", "dark"); montauk::config::set_string(&doc, "appearance.theme", "dark");
montauk::config::save("session", &doc); montauk::config::save("desktop", &doc);
``` ```
--- ---
@@ -543,8 +543,8 @@ bool user::change_password(const char* username, const char* new_password);
void user::set_session(const char* username); // Log in void user::set_session(const char* username); // Log in
void user::clear_session(); // Log out void user::clear_session(); // Log out
bool user::get_session_username(char* buf, int sz); // Get current user bool user::get_session_username(char* buf, int sz); // Get current process user
bool user::get_home_dir(char* buf, int sz); // Get user's home directory bool user::get_home_dir(char* buf, int sz); // Get current process home directory
``` ```
--- ---
+8 -13
View File
@@ -297,10 +297,10 @@ namespace user {
return false; return false;
} }
// ---- Session (current logged-in user) ---- // ---- Session state and current user helpers ----
// Write the current session after successful login. // Persist login session state after successful login.
// Stores username in 0:/config/session.toml so any app can query it. // Current processes should use getuser() for their active username.
inline void set_session(const char* username) { inline void set_session(const char* username) {
montauk::toml::Doc doc; montauk::toml::Doc doc;
doc.init(); doc.init();
@@ -314,19 +314,14 @@ namespace user {
montauk::fdelete("0:/config/session.toml"); montauk::fdelete("0:/config/session.toml");
} }
// Read the current username into buf. Returns true if a session exists. // Read the current process username into buf. Returns true if one is set.
inline bool get_session_username(char* buf, int bufSz) { inline bool get_session_username(char* buf, int bufSz) {
auto doc = config::load("session"); if (buf == nullptr || bufSz <= 0) return false;
const char* name = doc.get_string("session.username", ""); int len = montauk::getuser(buf, (uint64_t)bufSz);
bool found = (name[0] != '\0'); return len > 0 && buf[0] != '\0';
if (found) {
montauk::strncpy(buf, name, bufSz);
}
doc.destroy();
return found;
} }
// Get the current user's home directory. Returns true if a session exists. // Get the current process user's home directory. Returns true if a user is set.
inline bool get_home_dir(char* buf, int bufSz) { inline bool get_home_dir(char* buf, int bufSz) {
char username[32]; char username[32];
if (!get_session_username(username, sizeof(username))) return false; if (!get_session_username(username, sizeof(username))) return false;