summaryrefslogtreecommitdiff
path: root/src/lock.c
blob: 133d3275f33da1bcce59602f065eebfdbd1c1380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include "log.h"
#include "xmalloc.h"

static const char *default_cache_dir = ".cache/";
static const char *lock_filename = "tofi.lock";

[[nodiscard("memory leaked")]]
static char *get_lock_path() {
	char *lock_name = NULL;
	const char *runtime_path = getenv("XDG_RUNTIME_DIR");
	if (runtime_path == NULL) {
		runtime_path = getenv("XDG_CACHE_HOME");
	}
	if (runtime_path == NULL) {
		const char *home = getenv("HOME");
		if (home == NULL) {
			log_error("Couldn't retrieve HOME from environment.\n");
			return NULL;
		}
		size_t len = strlen(home) + 1
			+ strlen(default_cache_dir) + 1
			+ strlen(lock_filename) + 1;
		lock_name = xmalloc(len);
		snprintf(
			lock_name,
			len,
			"%s/%s/%s",
			home,
			default_cache_dir,
			lock_filename);
	} else {
		size_t len = strlen(runtime_path) + 1
			+ strlen(lock_filename) + 1;
		lock_name = xmalloc(len);
		snprintf(
			lock_name,
			len,
			"%s/%s",
			runtime_path,
			lock_filename);
	}
	return lock_name;
}

bool lock_check(void)
{
	bool ret = false;
	char *filename = get_lock_path();
	errno = 0;
	int fd = open(filename, O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR);
	if (fd == -1) {
		log_error("Failed to open lock file %s: %s.\n", filename, strerror(errno));
	} else if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
		if (errno == EWOULDBLOCK) {
			/*
			 * We can't lock the file because another tofi process
			 * already has.
			 */
			ret = true;
		}
        }

	free(filename);
	return ret;
}