Hello all

In case anyone is interested: I've added logging capabilities to
Slock. The patch is attached. It will enable logging of all locks,
unlocks and failed unlock attempts to ~/.slock.log, if compiled with
the ENABLE_LOGGING flag.

--Danilo
diff -up /tmp/slock-0.9/config.mk slock-0.9/config.mk
--- /tmp/slock-0.9/config.mk	2008-07-29 20:22:46.000000000 +0200
+++ slock-0.9/config.mk	2010-11-11 16:55:19.164582000 +0100
@@ -14,7 +14,7 @@ INCS = -I. -I/usr/include -I${X11INC}
 LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext
 
 # flags
-CPPFLAGS = -DVERSION=\"${VERSION}\" -DHAVE_SHADOW_H
+CPPFLAGS = -DVERSION=\"${VERSION}\" -DHAVE_SHADOW_H -DENABLE_LOGGING
 CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
 LDFLAGS = -s ${LIBS}
 
Only in slock-0.9: slock
diff -up /tmp/slock-0.9/slock.c slock-0.9/slock.c
--- /tmp/slock-0.9/slock.c	2008-07-29 20:22:46.000000000 +0200
+++ slock-0.9/slock.c	2010-11-11 16:42:10.181248668 +0100
@@ -22,6 +22,10 @@
 #include <bsd_auth.h>
 #endif
 
+#ifdef ENABLE_LOGGING
+#include <time.h>
+#endif
+
 static void
 die(const char *errstr, ...) {
 	va_list ap;
@@ -123,6 +127,18 @@ main(int argc, char **argv) {
 	len = 0;
 	XSync(dpy, False);
 
+#ifdef ENABLE_LOGGING
+	FILE *logfile;
+	char logfile_path[1024];
+	if (snprintf(logfile_path, sizeof logfile_path, "%s/%s", getenv("HOME"), ".slock.log")
+		>= sizeof logfile_path)
+		die("slock: log file path too long");
+	time_t rawtime;
+	time(&rawtime);
+	logfile = fopen(logfile_path, "a+");
+	fprintf(logfile, "Locked: %s", ctime(&rawtime));
+#endif
+
 	/* main event loop */
 	while(running && !XNextEvent(dpy, &ev)) {
 		if(len == 0 && DPMSCapable(dpy)) {
@@ -150,8 +166,13 @@ main(int argc, char **argv) {
 #else
 				running = strcmp(crypt(passwd, pws), pws);
 #endif
-				if (running != 0)
+				if (running != 0) {
 					XBell(dpy, 100);
+#ifdef ENABLE_LOGGING
+					time(&rawtime);
+					fprintf(logfile, "Failed unlock attempt: %s", ctime(&rawtime));
+#endif
+				}
 				len = 0;
 				break;
 			case XK_Escape:
@@ -174,5 +195,12 @@ main(int argc, char **argv) {
 	XFreePixmap(dpy, pmap);
 	XDestroyWindow(dpy, w);
 	XCloseDisplay(dpy);
+
+#ifdef ENABLE_LOGGING
+	time(&rawtime);
+	fprintf(logfile, "Unlocked: %s", strcat(ctime(&rawtime), "\n"));
+	fclose(logfile);
+#endif
+
 	return 0;
 }
Only in slock-0.9: slock.o

Reply via email to