flock mechanic added
This commit is contained in:
24
src/utils.c
24
src/utils.c
@@ -1,15 +1,39 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <glob.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/file.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
int get_instance_lock(void) {
|
||||
const char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
|
||||
char tmp_dir[256];
|
||||
snprintf(tmp_dir, sizeof(tmp_dir), "%s/linuxSmartie.lock", xdg_runtime_dir);
|
||||
|
||||
const int file = open(tmp_dir, O_CREAT | O_RDWR | O_CLOEXEC, 0600);
|
||||
if (file < 0) {
|
||||
applog(LOG_ERR, "Couldn't create a lock file at %s", tmp_dir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (flock(file, LOCK_EX | LOCK_NB) < 0) {
|
||||
if (errno == EWOULDBLOCK)
|
||||
applog(LOG_ERR, "another instance already running, this one will exit.");
|
||||
else
|
||||
applog(LOG_ERR, "flock failed: %s", strerror(errno));
|
||||
close(file);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline void timespec_add_ms(struct timespec *ts, const uint16_t ms) {
|
||||
ts->tv_nsec += ms * 1000000L;
|
||||
if (ts->tv_nsec >= 1000000000L) {
|
||||
|
||||
Reference in New Issue
Block a user