From 3f9a2b9e15e0fa42ec1ac7858786f57b9f036841 Mon Sep 17 00:00:00 2001 From: Igor Kolokolnikov Date: Wed, 2 Sep 2026 18:06:44 -0700 Subject: [PATCH] Automatically determine user and group. --- config.def.h | 7 ++++--- slock.c | 20 ++++++++++++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/config.def.h b/config.def.h index ebaba70..38560f0 100644 --- a/config.def.h +++ b/config.def.h @@ -1,6 +1,7 @@ /* user and group to drop privileges to */ -static const char *user = "igor"; -static const char *group = "igor"; +/* NOTE: Leave these empty if you want them to be determined automatically. */ +static const char *user = ""; +static const char *group = ""; static const char *colorname[NUMCOLS] = { [INIT] = "black", /* after initialization */ @@ -24,7 +25,7 @@ static const Passthrough passthroughs[] = { { 0, XF86XK_MonBrightnessDown }, }; -static const char *dspoffcmd[] = { "xset", "dpms", "force", "off", NULL }; +/*static const char *dspoffcmd[] = { "xset", "dpms", "force", "off", NULL };*/ static const Key keys[] = { /* modifier key function argument */ diff --git a/slock.c b/slock.c index 7c396eb..abc256e 100644 --- a/slock.c +++ b/slock.c @@ -409,6 +409,8 @@ main(int argc, char **argv) { int s, nlocks, nscreens; int event_base, error_base; + char *username, *groupname; + ARGBEGIN { case 'v': puts("slock-"VERSION); @@ -417,15 +419,25 @@ main(int argc, char **argv) { usage(); } ARGEND + if(strlen(user) && strlen(group)) { + username = (char *)user; + groupname = (char *)group; + } else { + username = getenv("USER"); + groupname = username; + } + + printf("%s:%s\n", username, groupname); + /* validate drop-user and -group */ errno = 0; - if (!(pwd = getpwnam(user))) - die("slock: getpwnam %s: %s\n", user, + if (!(pwd = getpwnam(username))) + die("slock: getpwnam %s: %s\n", username, errno ? strerror(errno) : "user entry not found"); duid = pwd->pw_uid; errno = 0; - if (!(grp = getgrnam(group))) - die("slock: getgrnam %s: %s\n", group, + if (!(grp = getgrnam(groupname))) + die("slock: getgrnam %s: %s\n", groupname, errno ? strerror(errno) : "group entry not found"); dgid = grp->gr_gid;