diff options
author | FRIGN <dev@frign.de> | 2016-02-14 02:13:54 +0100 |
---|---|---|
committer | FRIGN <dev@frign.de> | 2016-02-14 02:13:54 +0100 |
commit | b02c4d452a7942d4be3c69e6f98dafd35a2e4e78 (patch) | |
tree | ace56cfb75af6f134205639a876cf0c466a336eb | |
parent | a6dc051e3744ce5b14c54d2d246d3e8258207e76 (diff) |
Use argv0 instead of passing "slock:" to die every time
-rw-r--r-- | slock.c | 28 |
1 files changed, 16 insertions, 12 deletions
@@ -46,6 +46,7 @@ static Bool failure = False; | |||
46 | static Bool rr; | 46 | static Bool rr; |
47 | static int rrevbase; | 47 | static int rrevbase; |
48 | static int rrerrbase; | 48 | static int rrerrbase; |
49 | static char *argv0; | ||
49 | 50 | ||
50 | static void | 51 | static void |
51 | die(const char *errstr, ...) | 52 | die(const char *errstr, ...) |
@@ -53,6 +54,7 @@ die(const char *errstr, ...) | |||
53 | va_list ap; | 54 | va_list ap; |
54 | 55 | ||
55 | va_start(ap, errstr); | 56 | va_start(ap, errstr); |
57 | fprintf(stderr, "%s: ", argv0); | ||
56 | vfprintf(stderr, errstr, ap); | 58 | vfprintf(stderr, errstr, ap); |
57 | va_end(ap); | 59 | va_end(ap); |
58 | exit(1); | 60 | exit(1); |
@@ -88,9 +90,9 @@ getpw(void) | |||
88 | errno = 0; | 90 | errno = 0; |
89 | if (!(pw = getpwuid(getuid()))) { | 91 | if (!(pw = getpwuid(getuid()))) { |
90 | if (errno) | 92 | if (errno) |
91 | die("slock: getpwuid: %s\n", strerror(errno)); | 93 | die("getpwuid: %s\n", strerror(errno)); |
92 | else | 94 | else |
93 | die("slock: cannot retrieve password entry\n"); | 95 | die("cannot retrieve password entry\n"); |
94 | } | 96 | } |
95 | rval = pw->pw_passwd; | 97 | rval = pw->pw_passwd; |
96 | 98 | ||
@@ -98,7 +100,7 @@ getpw(void) | |||
98 | if (rval[0] == 'x' && rval[1] == '\0') { | 100 | if (rval[0] == 'x' && rval[1] == '\0') { |
99 | struct spwd *sp; | 101 | struct spwd *sp; |
100 | if (!(sp = getspnam(getenv("USER")))) | 102 | if (!(sp = getspnam(getenv("USER")))) |
101 | die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n"); | 103 | die("cannot retrieve shadow entry (make sure to suid or sgid slock)\n"); |
102 | rval = sp->sp_pwdp; | 104 | rval = sp->sp_pwdp; |
103 | } | 105 | } |
104 | #endif | 106 | #endif |
@@ -106,7 +108,7 @@ getpw(void) | |||
106 | /* drop privileges */ | 108 | /* drop privileges */ |
107 | if (geteuid() == 0 && | 109 | if (geteuid() == 0 && |
108 | ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0)) | 110 | ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0)) |
109 | die("slock: cannot drop privileges\n"); | 111 | die("cannot drop privileges\n"); |
110 | return rval; | 112 | return rval; |
111 | } | 113 | } |
112 | #endif | 114 | #endif |
@@ -254,7 +256,7 @@ lockscreen(Display *dpy, int screen) | |||
254 | usleep(1000); | 256 | usleep(1000); |
255 | } | 257 | } |
256 | if (!len) { | 258 | if (!len) { |
257 | fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n", screen); | 259 | fprintf(stderr, "unable to grab mouse pointer for screen %d\n", screen); |
258 | } else { | 260 | } else { |
259 | for (len = 1000; len; len--) { | 261 | for (len = 1000; len; len--) { |
260 | if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) { | 262 | if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess) { |
@@ -264,7 +266,7 @@ lockscreen(Display *dpy, int screen) | |||
264 | } | 266 | } |
265 | usleep(1000); | 267 | usleep(1000); |
266 | } | 268 | } |
267 | fprintf(stderr, "slock: unable to grab keyboard for screen %d\n", screen); | 269 | fprintf(stderr, "unable to grab keyboard for screen %d\n", screen); |
268 | } | 270 | } |
269 | /* grabbing one of the inputs failed */ | 271 | /* grabbing one of the inputs failed */ |
270 | running = 0; | 272 | running = 0; |
@@ -281,24 +283,26 @@ main(int argc, char **argv) | |||
281 | Display *dpy; | 283 | Display *dpy; |
282 | int screen; | 284 | int screen; |
283 | 285 | ||
286 | argv0 = argv[0], argc--, argv++; | ||
287 | |||
284 | #ifdef __linux__ | 288 | #ifdef __linux__ |
285 | dontkillme(); | 289 | dontkillme(); |
286 | #endif | 290 | #endif |
287 | 291 | ||
288 | if (!getpwuid(getuid())) | 292 | if (!getpwuid(getuid())) |
289 | die("slock: no passwd entry for you\n"); | 293 | die("no passwd entry for you\n"); |
290 | 294 | ||
291 | #ifndef HAVE_BSD_AUTH | 295 | #ifndef HAVE_BSD_AUTH |
292 | pws = getpw(); | 296 | pws = getpw(); |
293 | #endif | 297 | #endif |
294 | 298 | ||
295 | if (!(dpy = XOpenDisplay(0))) | 299 | if (!(dpy = XOpenDisplay(0))) |
296 | die("slock: cannot open display\n"); | 300 | die("cannot open display\n"); |
297 | rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase); | 301 | rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase); |
298 | /* Get the number of screens in display "dpy" and blank them all. */ | 302 | /* Get the number of screens in display "dpy" and blank them all. */ |
299 | nscreens = ScreenCount(dpy); | 303 | nscreens = ScreenCount(dpy); |
300 | if (!(locks = malloc(sizeof(Lock*) * nscreens))) | 304 | if (!(locks = malloc(sizeof(Lock*) * nscreens))) |
301 | die("slock: malloc: %s\n", strerror(errno)); | 305 | die("Out of memory.\n"); |
302 | int nlocks = 0; | 306 | int nlocks = 0; |
303 | for (screen = 0; screen < nscreens; screen++) { | 307 | for (screen = 0; screen < nscreens; screen++) { |
304 | if ((locks[screen] = lockscreen(dpy, screen)) != NULL) | 308 | if ((locks[screen] = lockscreen(dpy, screen)) != NULL) |
@@ -313,11 +317,11 @@ main(int argc, char **argv) | |||
313 | return 1; | 317 | return 1; |
314 | } | 318 | } |
315 | 319 | ||
316 | if (argc >= 2 && fork() == 0) { | 320 | if (argc >= 1 && fork() == 0) { |
317 | if (dpy) | 321 | if (dpy) |
318 | close(ConnectionNumber(dpy)); | 322 | close(ConnectionNumber(dpy)); |
319 | execvp(argv[1], argv+1); | 323 | execvp(argv[0], argv); |
320 | die("slock: execvp %s failed: %s\n", argv[1], strerror(errno)); | 324 | die("execvp %s failed: %s\n", argv[0], strerror(errno)); |
321 | } | 325 | } |
322 | 326 | ||
323 | /* Everything is now blank. Now wait for the correct password. */ | 327 | /* Everything is now blank. Now wait for the correct password. */ |