applied Dimitris' style patch from Dec'14, with some minor modifications

This commit is contained in:
Anselm R Garbe 2015-01-27 22:16:52 +01:00
parent 66e31556db
commit a31b919111
1 changed files with 60 additions and 52 deletions

48
slock.c
View File

@ -1,4 +1,3 @@
/* See LICENSE file for license details. */ /* See LICENSE file for license details. */
#define _XOPEN_SOURCE 500 #define _XOPEN_SOURCE 500
#if HAVE_SHADOW_H #if HAVE_SHADOW_H
@ -37,20 +36,22 @@ static int nscreens;
static Bool running = True; static Bool running = True;
static void static void
die(const char *errstr, ...) { die(const char *errstr, ...)
{
va_list ap; va_list ap;
va_start(ap, errstr); va_start(ap, errstr);
vfprintf(stderr, errstr, ap); vfprintf(stderr, errstr, ap);
va_end(ap); va_end(ap);
exit(EXIT_FAILURE); exit(1);
} }
#ifdef __linux__ #ifdef __linux__
#include <fcntl.h> #include <fcntl.h>
static void static void
dontkillme(void) { dontkillme(void)
{
int fd; int fd;
fd = open("/proc/self/oom_score_adj", O_WRONLY); fd = open("/proc/self/oom_score_adj", O_WRONLY);
@ -62,8 +63,10 @@ dontkillme(void) {
#endif #endif
#ifndef HAVE_BSD_AUTH #ifndef HAVE_BSD_AUTH
/* only run as root */
static const char * static const char *
getpw(void) { /* only run as root */ getpw(void)
{
const char *rval; const char *rval;
struct passwd *pw; struct passwd *pw;
@ -73,7 +76,7 @@ getpw(void) { /* only run as root */
if (errno) if (errno)
die("slock: getpwuid: %s\n", strerror(errno)); die("slock: getpwuid: %s\n", strerror(errno));
else else
die("slock: cannot retrieve password entry (make sure to suid or sgid slock)\n"); die("slock: cannot retrieve password entry\n");
} }
rval = pw->pw_passwd; rval = pw->pw_passwd;
@ -88,8 +91,8 @@ getpw(void) { /* only run as root */
#endif #endif
/* drop privileges */ /* drop privileges */
if (geteuid() == 0 if (geteuid() == 0 &&
&& ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0)) ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0))
die("slock: cannot drop privileges\n"); die("slock: cannot drop privileges\n");
return rval; return rval;
} }
@ -125,9 +128,11 @@ readpw(Display *dpy, const char *pws)
else if (ksym >= XK_KP_0 && ksym <= XK_KP_9) else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
ksym = (ksym - XK_KP_0) + XK_0; ksym = (ksym - XK_KP_0) + XK_0;
} }
if(IsFunctionKey(ksym) || IsKeypadKey(ksym) if (IsFunctionKey(ksym) ||
|| IsMiscFunctionKey(ksym) || IsPFKey(ksym) IsKeypadKey(ksym) ||
|| IsPrivateKeypadKey(ksym)) IsMiscFunctionKey(ksym) ||
IsPFKey(ksym) ||
IsPrivateKeypadKey(ksym))
continue; continue;
switch (ksym) { switch (ksym) {
case XK_Return: case XK_Return:
@ -174,7 +179,8 @@ readpw(Display *dpy, const char *pws)
} }
static void static void
unlockscreen(Display *dpy, Lock *lock) { unlockscreen(Display *dpy, Lock *lock)
{
if(dpy == NULL || lock == NULL) if(dpy == NULL || lock == NULL)
return; return;
@ -187,7 +193,8 @@ unlockscreen(Display *dpy, Lock *lock) {
} }
static Lock * static Lock *
lockscreen(Display *dpy, int screen) { lockscreen(Display *dpy, int screen)
{
char curs[] = {0, 0, 0, 0, 0, 0, 0, 0}; char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned int len; unsigned int len;
Lock *lock; Lock *lock;
@ -228,8 +235,7 @@ lockscreen(Display *dpy, int screen) {
} }
if (running && (len > 0)) { if (running && (len > 0)) {
for (len = 1000; len; len--) { for (len = 1000; len; len--) {
if(XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
== GrabSuccess)
break; break;
usleep(1000); usleep(1000);
} }
@ -240,16 +246,18 @@ lockscreen(Display *dpy, int screen) {
unlockscreen(dpy, lock); unlockscreen(dpy, lock);
lock = NULL; lock = NULL;
} }
else else {
XSelectInput(dpy, lock->root, SubstructureNotifyMask); XSelectInput(dpy, lock->root, SubstructureNotifyMask);
}
return lock; return lock;
} }
static void static void
usage(void) { usage(void)
{
fprintf(stderr, "usage: slock [-v]\n"); fprintf(stderr, "usage: slock [-v]\n");
exit(EXIT_FAILURE); exit(1);
} }
int int
@ -261,7 +269,7 @@ main(int argc, char **argv) {
int screen; int screen;
if ((argc == 2) && !strcmp("-v", argv[1])) if ((argc == 2) && !strcmp("-v", argv[1]))
die("slock-%s, © 2006-2014 slock engineers\n", VERSION); die("slock-%s, © 2006-2015 slock engineers\n", VERSION);
else if (argc != 1) else if (argc != 1)
usage(); usage();
@ -291,7 +299,7 @@ main(int argc, char **argv) {
XSync(dpy, False); XSync(dpy, False);
/* Did we actually manage to lock something? */ /* Did we actually manage to lock something? */
if (nlocks == 0) { // nothing to protect if (nlocks == 0) { /* nothing to protect */
free(locks); free(locks);
XCloseDisplay(dpy); XCloseDisplay(dpy);
return 1; return 1;