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

48
slock.c
View file

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