1 /* See LICENSE file for license details. */
2 #define _XOPEN_SOURCE 500
15 #include <sys/types.h>
16 #include <X11/extensions/Xrandr.h>
17 #include <X11/keysym.h>
19 #include <X11/Xutil.h>
22 #include <login_cap.h>
39 unsigned long colors[NUMCOLS];
44 static Bool running = True;
45 static Bool failure = False;
51 die(const char *errstr, ...)
56 vfprintf(stderr, errstr, ap);
63 #include <linux/oom.h>
72 fd = open("/proc/self/oom_score_adj", O_WRONLY);
73 if (fd < 0 && errno == ENOENT)
76 /* convert OOM_SCORE_ADJ_MIN to string for writing */
77 length = snprintf(value, sizeof(value), "%d\n", OOM_SCORE_ADJ_MIN);
79 /* bail on truncation */
80 if (length >= sizeof(value))
81 die("buffer too small\n");
83 if (fd < 0 || write(fd, value, length) != length || close(fd) != 0)
84 die("cannot disable the out-of-memory killer for this process\n");
89 /* only run as root */
97 pw = getpwuid(getuid());
100 die("slock: getpwuid: %s\n", strerror(errno));
102 die("slock: cannot retrieve password entry\n");
104 rval = pw->pw_passwd;
107 if (rval[0] == 'x' && rval[1] == '\0') {
109 sp = getspnam(getenv("USER"));
111 die("slock: cannot retrieve shadow entry (make sure to suid or sgid slock)\n");
116 /* drop privileges */
117 if (geteuid() == 0 &&
118 ((getegid() != pw->pw_gid && setgid(pw->pw_gid) < 0) || setuid(pw->pw_uid) < 0))
119 die("slock: cannot drop privileges\n");
128 readpw(Display *dpy, const char *pws)
131 char buf[32], passwd[256];
133 unsigned int len, color;
136 static int oldc = INIT;
141 /* As "slock" stands for "Simple X display locker", the DPMS settings
142 * had been removed and you can set it with "xset" or some other
143 * utility. This way the user can easily set a customized DPMS
145 while (running && !XNextEvent(dpy, &ev)) {
146 if (ev.type == KeyPress) {
148 num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
149 if (IsKeypadKey(ksym)) {
150 if (ksym == XK_KP_Enter)
152 else if (ksym >= XK_KP_0 && ksym <= XK_KP_9)
153 ksym = (ksym - XK_KP_0) + XK_0;
155 if (IsFunctionKey(ksym) ||
157 IsMiscFunctionKey(ksym) ||
159 IsPrivateKeypadKey(ksym))
165 running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
167 running = !!strcmp(crypt(passwd, pws), pws);
183 if (num && !iscntrl((int) buf[0]) && (len + num < sizeof(passwd))) {
184 memcpy(passwd + len, buf, num);
189 color = len ? INPUT : (failure || failonclear ? FAILED : INIT);
190 if (running && oldc != color) {
191 for (screen = 0; screen < nscreens; screen++) {
192 XSetWindowBackground(dpy, locks[screen]->win, locks[screen]->colors[color]);
193 XClearWindow(dpy, locks[screen]->win);
197 } else if (rr && ev.type == rrevbase + RRScreenChangeNotify) {
198 XRRScreenChangeNotifyEvent *rre = (XRRScreenChangeNotifyEvent*)&ev;
199 for (screen = 0; screen < nscreens; screen++) {
200 if (locks[screen]->win == rre->window) {
201 XResizeWindow(dpy, locks[screen]->win, rre->width, rre->height);
202 XClearWindow(dpy, locks[screen]->win);
205 } else for (screen = 0; screen < nscreens; screen++)
206 XRaiseWindow(dpy, locks[screen]->win);
211 unlockscreen(Display *dpy, Lock *lock)
213 if(dpy == NULL || lock == NULL)
216 XUngrabPointer(dpy, CurrentTime);
217 XFreeColors(dpy, DefaultColormap(dpy, lock->screen), lock->colors, NUMCOLS, 0);
218 XFreePixmap(dpy, lock->pmap);
219 XDestroyWindow(dpy, lock->win);
225 lockscreen(Display *dpy, int screen)
227 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
232 XSetWindowAttributes wa;
235 if (dpy == NULL || screen < 0)
238 lock = malloc(sizeof(Lock));
242 lock->screen = screen;
244 lock->root = RootWindow(dpy, lock->screen);
246 for (i = 0; i < NUMCOLS; i++) {
247 XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen), colorname[i], &color, &dummy);
248 lock->colors[i] = color.pixel;
252 wa.override_redirect = 1;
253 wa.background_pixel = lock->colors[INIT];
254 lock->win = XCreateWindow(dpy, lock->root, 0, 0, DisplayWidth(dpy, lock->screen), DisplayHeight(dpy, lock->screen),
255 0, DefaultDepth(dpy, lock->screen), CopyFromParent,
256 DefaultVisual(dpy, lock->screen), CWOverrideRedirect | CWBackPixel, &wa);
257 lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
258 invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap, &color, &color, 0, 0);
259 XDefineCursor(dpy, lock->win, invisible);
260 XMapRaised(dpy, lock->win);
262 XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
263 for (len = 1000; len; len--) {
264 if (XGrabPointer(dpy, lock->root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
265 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
269 if (running && (len > 0)) {
270 for (len = 1000; len; len--) {
271 if (XGrabKeyboard(dpy, lock->root, True, GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
277 running &= (len > 0);
279 unlockscreen(dpy, lock);
283 XSelectInput(dpy, lock->root, SubstructureNotifyMask);
292 fprintf(stderr, "usage: slock [-v]\n");
297 main(int argc, char **argv) {
298 #ifndef HAVE_BSD_AUTH
304 if ((argc == 2) && !strcmp("-v", argv[1]))
305 die("slock-%s, © 2006-2015 slock engineers\n", VERSION);
313 if (!getpwuid(getuid()))
314 die("slock: no passwd entry for you\n");
316 #ifndef HAVE_BSD_AUTH
320 if (!(dpy = XOpenDisplay(0)))
321 die("slock: cannot open display\n");
322 rr = XRRQueryExtension(dpy, &rrevbase, &rrerrbase);
323 /* Get the number of screens in display "dpy" and blank them all. */
324 nscreens = ScreenCount(dpy);
325 locks = malloc(sizeof(Lock *) * nscreens);
327 die("slock: malloc: %s\n", strerror(errno));
329 for (screen = 0; screen < nscreens; screen++) {
330 if ( (locks[screen] = lockscreen(dpy, screen)) != NULL)
335 /* Did we actually manage to lock something? */
336 if (nlocks == 0) { /* nothing to protect */
342 /* Everything is now blank. Now wait for the correct password. */
349 /* Password ok, unlock everything and quit. */
350 for (screen = 0; screen < nscreens; screen++)
351 unlockscreen(dpy, locks[screen]);