58 lines
979 B
C++
58 lines
979 B
C++
#include <csignal>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <fcntl.h>
|
|
#include <optional>
|
|
#include <signal.h>
|
|
#include <sys/file.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
#include "App.hpp"
|
|
|
|
bool signal_running();
|
|
|
|
std::optional<App> g_app{};
|
|
|
|
auto main() -> int {
|
|
if (signal_running()) {
|
|
return 0;
|
|
}
|
|
|
|
std::signal(SIGINT, [](int) {
|
|
if (g_app)
|
|
g_app->stop();
|
|
});
|
|
|
|
g_app.emplace();
|
|
g_app->run();
|
|
}
|
|
|
|
bool signal_running() {
|
|
const char *lock_path = "/tmp/waylight.lock";
|
|
int fd = open(lock_path, O_CREAT | O_RDWR, 0666);
|
|
if (fd == -1)
|
|
return false;
|
|
|
|
if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
|
|
FILE *f = fopen(lock_path, "r");
|
|
if (f) {
|
|
pid_t pid;
|
|
if (fscanf(f, "%d", &pid) == 1)
|
|
kill(pid, SIGUSR1);
|
|
fclose(f);
|
|
}
|
|
close(fd);
|
|
return true;
|
|
}
|
|
|
|
if (ftruncate(fd, 0) == -1) {
|
|
close(fd);
|
|
unlink(lock_path);
|
|
return false;
|
|
}
|
|
|
|
dprintf(fd, "%d\n", getpid());
|
|
return false;
|
|
}
|