forked from halfgaar/FlashMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneinstancelock.cpp
66 lines (55 loc) · 1.42 KB
/
oneinstancelock.cpp
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
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#include "oneinstancelock.h"
#include <stdexcept>
#include "utils.h"
OneInstanceLock::OneInstanceLock()
{
std::string dir("/tmp");
char *d = getenv("HOME");
if (d != NULL && d[0] == '/')
{
dir = std::string(d);
}
lockFilePath = dir + "/.FlashMQ.lock";
}
OneInstanceLock::~OneInstanceLock()
{
unlock();
}
void OneInstanceLock::lock()
{
fd = open(lockFilePath.c_str(), O_RDWR | O_CREAT, 0600);
if (fd < 0)
throw std::runtime_error(formatString("Can't create '%s': %s", lockFilePath.c_str(), strerror(errno)));
struct flock fl;
fl.l_start = 0;
fl.l_len = 0;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
if (fcntl(fd, F_SETLK, &fl) < 0)
{
throw std::runtime_error("Can't acquire lock: another instance is already running?");
}
}
void OneInstanceLock::unlock()
{
if (fd > 0)
{
close(fd);
fd = 0;
if (!lockFilePath.empty())
{
if (unlink(lockFilePath.c_str()) < 0)
{
logger->logf(LOG_ERR, "Can't delete '%': %s", lockFilePath.c_str(), strerror(errno));
}
lockFilePath.clear();
}
}
}