mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-12 10:22:12 +02:00
Users (Linux): adds systemd fallback for user detection on Linux
Fixes #2064
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
#include "common/io/io.h"
|
||||
#include "common/properties.h"
|
||||
#include "fastfetch.h"
|
||||
#include "users.h"
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#if FF_HAVE_UTMPX
|
||||
#include <utmpx.h>
|
||||
#else
|
||||
@@ -15,7 +19,111 @@
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
const char* ffDetectUsers(FFUsersOptions* options, FFlist* users)
|
||||
#if __linux__
|
||||
bool detectUserBySystemd(const FFstrbuf* pathUsers, FFlist* users)
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY state = ffStrbufCreate();
|
||||
FF_STRBUF_AUTO_DESTROY userName = ffStrbufCreate();
|
||||
FF_STRBUF_AUTO_DESTROY loginTime = ffStrbufCreate();
|
||||
FF_STRBUF_AUTO_DESTROY sessions = ffStrbufCreate();
|
||||
|
||||
// WARNING: This is private data. Do not parse
|
||||
if (!ffParsePropFileValues(pathUsers->chars, 4, (FFpropquery[]) {
|
||||
{"NAME=", &userName},
|
||||
{"STATE=", &state},
|
||||
{"REALTIME=", &loginTime},
|
||||
{"ONLINE_SESSIONS=", &sessions},
|
||||
}) || !ffStrbufEqualS(&state, "active"))
|
||||
return false;
|
||||
|
||||
FFUserResult* user = FF_LIST_ADD(FFUserResult, *users);
|
||||
ffStrbufInitMove(&user->name, &userName);
|
||||
ffStrbufInit(&user->hostName);
|
||||
ffStrbufInit(&user->sessionName);
|
||||
ffStrbufInit(&user->clientIp);
|
||||
ffStrbufSubstrBefore(&loginTime, loginTime.length - 3); // converts us to ms
|
||||
user->loginTime = ffStrbufToUInt(&loginTime, 0);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY pathSessions = ffStrbufCreateS("/run/systemd/sessions/");
|
||||
const uint32_t pathSessionsBaseLen = pathSessions.length;
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY tty = ffStrbufCreate();
|
||||
FF_STRBUF_AUTO_DESTROY remoteHost = ffStrbufCreate();
|
||||
FF_STRBUF_AUTO_DESTROY service = ffStrbufCreate();
|
||||
|
||||
char* token = NULL;
|
||||
size_t n = 0;
|
||||
while (ffStrbufGetdelim(&token, &n, ' ', &sessions))
|
||||
{
|
||||
ffStrbufSubstrBefore(&pathSessions, pathSessionsBaseLen);
|
||||
ffStrbufAppendS(&pathSessions, token);
|
||||
|
||||
ffStrbufClear(&remoteHost);
|
||||
ffStrbufClear(&service);
|
||||
ffStrbufClear(&tty);
|
||||
ffStrbufClear(&loginTime);
|
||||
|
||||
// WARNING: This is private data. Do not parse
|
||||
if (ffParsePropFileValues(pathSessions.chars, 4, (FFpropquery[]) {
|
||||
{"REMOTE_HOST=", &remoteHost},
|
||||
{"TTY=", &tty},
|
||||
{"SERVICE=", &service},
|
||||
{"REALTIME=", &loginTime},
|
||||
}) && !ffStrbufEqualS(&service, "systemd-user"))
|
||||
{
|
||||
if (remoteHost.length)
|
||||
{
|
||||
ffStrbufTrimRight(&remoteHost, ']');
|
||||
ffStrbufTrimLeft(&remoteHost, '[');
|
||||
ffStrbufInitMove(&user->hostName, &remoteHost);
|
||||
}
|
||||
else
|
||||
ffStrbufSetStatic(&user->hostName, "localhost");
|
||||
ffStrbufInitMove(&user->sessionName, tty.length ? &tty : &service);
|
||||
if (loginTime.length)
|
||||
{
|
||||
ffStrbufSubstrBefore(&loginTime, loginTime.length - 3); // converts us to ms
|
||||
user->loginTime = ffStrbufToUInt(&loginTime, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* detectBySystemd(FFUsersOptions* options, FFlist* users)
|
||||
{
|
||||
// For some reason, debian/ubuntu no longer updates `/var/run/utmp` (#2064)
|
||||
// Query systemd instead
|
||||
FF_STRBUF_AUTO_DESTROY pathUsers = ffStrbufCreateS("/run/systemd/users/");
|
||||
|
||||
if (options->myselfOnly)
|
||||
{
|
||||
ffStrbufAppendUInt(&pathUsers, getuid());
|
||||
detectUserBySystemd(&pathUsers, users);
|
||||
}
|
||||
else
|
||||
{
|
||||
const uint32_t pathUsersBaseLen = pathUsers.length;
|
||||
FF_AUTO_CLOSE_DIR DIR* dirp = opendir(pathUsers.chars);
|
||||
if (!dirp) return "opendir(\"/run/systemd/users/\") failed";
|
||||
|
||||
struct dirent* entry;
|
||||
while ((entry = readdir(dirp)))
|
||||
{
|
||||
if (entry->d_type != DT_REG) continue;
|
||||
|
||||
ffStrbufAppendS(&pathUsers, entry->d_name);
|
||||
detectUserBySystemd(&pathUsers, users);
|
||||
ffStrbufSubstrBefore(&pathUsers, pathUsersBaseLen);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
const char* detectByUtmp(FFUsersOptions* options, FFlist* users)
|
||||
{
|
||||
struct utmpx* n = NULL;
|
||||
setutxent();
|
||||
@@ -35,7 +143,7 @@ next:
|
||||
goto next;
|
||||
}
|
||||
|
||||
FFUserResult* user = (FFUserResult*) ffListAdd(users);
|
||||
FFUserResult* user = FF_LIST_ADD(FFUserResult, *users);
|
||||
ffStrbufInitS(&user->name, n->ut_user);
|
||||
ffStrbufInitS(&user->hostName, n->ut_host);
|
||||
ffStrbufInitS(&user->sessionName, n->ut_line);
|
||||
@@ -68,3 +176,15 @@ next:
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* ffDetectUsers(FFUsersOptions* options, FFlist* users)
|
||||
{
|
||||
const char* err = detectByUtmp(options, users);
|
||||
if (err) return err;
|
||||
|
||||
#if __linux__
|
||||
if (users->length == 0) detectBySystemd(options, users);
|
||||
#endif
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user