Use argv0 instead of passing "slock:" to die every time

This commit is contained in:
FRIGN 2016-02-14 02:13:54 +01:00
parent a6dc051e37
commit b02c4d452a

28
slock.c
View file

@ -46,6 +46,7 @@ static Bool failure = False;
static Bool rr; static Bool rr;
static int rrevbase; static int rrevbase;
static int rrerrbase; static int rrerrbase;
static char *argv0;
static void static void
die(const char *errstr, ...) die(const char *errstr, ...)
@ -53,6 +54,7 @@ die(const char *errstr, ...)
va_list ap; va_list ap;
va_start(ap, errstr); va_start(ap, errstr);
fprintf(stderr, "%s: ", argv0);
vfprintf(stderr, errstr, ap); vfprintf(stderr, errstr, ap);
va_end(ap); va_end(ap);
exit(1); exit(1);
@ -88,9 +90,9 @@ getpw(void)
errno = 0; errno = 0;
if (!(pw = getpwuid(getuid()))) { if (!(pw = getpwuid(getuid()))) {
if (errno) if (errno)
die("slock: getpwuid: %s\n", strerror(errno)); die("getpwuid: %s\n", strerror(errno));
else else
die("slock: cannot retrieve password entry\n"); die("cannot retrieve password entry\n");
} }
rval = pw->pw_passwd; rval = pw->pw_passwd;
@ -98,7 +100,7 @@ getpw(void)
if (rval[0] == 'x' && rval[1] == '\0') { if (rval[0] == 'x' && rval[1] == '\0') {
struct spwd *sp; struct spwd *sp;
if (!(sp = getspnam(getenv("USER")))) if (!(sp = getspnam(getenv("USER"))))
die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n"); die("cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
rval = sp->sp_pwdp; rval = sp->sp_pwdp;
} }
#endif #endif
@ -106,7 +108,7 @@ getpw(void)
/* 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("cannot drop privileges\n");
return rval; return rval;
} }
#endif #endif
@ -254,7 +256,7 @@ lockscreen(Display *dpy, int screen)
usleep(1000); usleep(1000);
} }
if (!len) { if (!len) {
fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n", screen); fprintf(stderr, "unable to grab mouse pointer for screen %d\n", screen);
} else { } else {
for (len = 1000; len; len--) { 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) {
@ -264,7 +266,7 @@ lockscreen(Display *dpy, int screen)
} }
usleep(1000); usleep(1000);
} }
fprintf(stderr, "slock: unable to grab keyboard for screen %d\n", screen); fprintf(stderr, "unable to grab keyboard for screen %d\n", screen);
} }
/* grabbing one of the inputs failed */ /* grabbing one of the inputs failed */
running = 0; running = 0;
@ -281,24 +283,26 @@ main(int argc, char **argv)
Display *dpy; Display *dpy;
int screen; int screen;
argv0 = argv[0], argc--, argv++;
#ifdef __linux__ #ifdef __linux__
dontkillme(); dontkillme();
#endif #endif
if (!getpwuid(getuid())) if (!getpwuid(getuid()))
die("slock: no passwd entry for you\n"); die("no passwd entry for you\n");
#ifndef HAVE_BSD_AUTH #ifndef HAVE_BSD_AUTH
pws = getpw(); pws = getpw();
#endif #endif
if (!(dpy = XOpenDisplay(0))) if (!(dpy = XOpenDisplay(0)))
die("slock: cannot open display\n"); die("cannot open display\n");
rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase); rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
/* Get the number of screens in display "dpy" and blank them all. */ /* Get the number of screens in display "dpy" and blank them all. */
nscreens = ScreenCount(dpy); nscreens = ScreenCount(dpy);
if (!(locks = malloc(sizeof(Lock*) * nscreens))) if (!(locks = malloc(sizeof(Lock*) * nscreens)))
die("slock: malloc: %s\n", strerror(errno)); die("Out of memory.\n");
int nlocks = 0; int nlocks = 0;
for (screen = 0; screen < nscreens; screen++) { for (screen = 0; screen < nscreens; screen++) {
if ((locks[screen] = lockscreen(dpy, screen)) != NULL) if ((locks[screen] = lockscreen(dpy, screen)) != NULL)
@ -313,11 +317,11 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (argc >= 2 && fork() == 0) { if (argc >= 1 && fork() == 0) {
if (dpy) if (dpy)
close(ConnectionNumber(dpy)); close(ConnectionNumber(dpy));
execvp(argv[1], argv+1); execvp(argv[0], argv);
die("slock: execvp %s failed: %s\n", argv[1], strerror(errno)); die("execvp %s failed: %s\n", argv[0], strerror(errno));
} }
/* Everything is now blank. Now wait for the correct password. */ /* Everything is now blank. Now wait for the correct password. */