1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <fcntl.h> 4 #include <unistd.h> 5 #include <sys/stat.h> 6 7 #include "lockfile.h" 8 #include "sbuf.h" 9 #include "banned.h" 10 11 #define LOCKFILE "~/.jgmenu-lockfile" 12 static struct sbuf lockfile; 13 lockfile_unlink(void)14void lockfile_unlink(void) 15 { 16 unlink(lockfile.buf); 17 } 18 lockfile_init(void)19void lockfile_init(void) 20 { 21 int fd; 22 struct stat st; 23 24 sbuf_init(&lockfile); 25 sbuf_cpy(&lockfile, LOCKFILE); 26 sbuf_expand_tilde(&lockfile); 27 if (!stat(lockfile.buf, &st)) { 28 die("Lockfile '%s' exists. This is probably because another\n" 29 " instance of jgmenu is running. If this is not" 30 " the case, manually delete\n the lockfile.", 31 LOCKFILE); 32 } 33 fd = open(lockfile.buf, O_RDWR | O_CREAT, 0600); 34 if (fd < 0) 35 die("lockfile '%s'", lockfile.buf); 36 if (close(fd) < 0) 37 warn("[%s] error closing file descriptor", __FILE__); 38 atexit(lockfile_unlink); 39 } 40