mirror of
https://github.com/neocturne/fastd.git
synced 2025-05-14 04:15:08 +02:00
status: unlink stale status sockets, add lockfile
By using the lock status instead of the existence of the status socket, restarting fastd after a crash will no longer fail.
This commit is contained in:
parent
c137199077
commit
20c51eacf0
1 changed files with 31 additions and 1 deletions
32
src/status.c
32
src/status.c
|
@ -21,6 +21,7 @@
|
|||
|
||||
#include <json-c/json.h>
|
||||
#include <net/if.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
|
||||
|
@ -195,7 +196,29 @@ static void unlink_status_socket(void) {
|
|||
return;
|
||||
|
||||
if (unlink(conf.status_socket))
|
||||
pr_warn_errno("fastd_status_cleanup: unlink");
|
||||
pr_warn_errno("unlink_status_socket: unlink");
|
||||
}
|
||||
|
||||
static void status_socket_lock(void) {
|
||||
const char *lock_format = "%s.lock";
|
||||
|
||||
size_t lockname_len = strlen(lock_format) + strlen(conf.status_socket) + 1;
|
||||
char lockname[lockname_len];
|
||||
snprintf(lockname, lockname_len, lock_format, conf.status_socket);
|
||||
|
||||
int lock_fd = open(lockname, O_RDONLY | O_CREAT, 0600);
|
||||
if (lock_fd < 0)
|
||||
exit_errno("unable to open status socket lock file");
|
||||
|
||||
if (flock(lock_fd, LOCK_EX | LOCK_NB)) {
|
||||
switch (errno) {
|
||||
case EWOULDBLOCK:
|
||||
exit_error("status socket already in use");
|
||||
|
||||
default:
|
||||
exit_error("unable to set status socket lock");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Initialized the status socket */
|
||||
|
@ -217,6 +240,13 @@ void fastd_status_init(void) {
|
|||
}
|
||||
#endif
|
||||
|
||||
status_socket_lock();
|
||||
|
||||
if (unlink(conf.status_socket) == 0)
|
||||
pr_info("removing old status socket");
|
||||
else if (errno != ENOENT)
|
||||
pr_warn_errno("unable to remove old status socket");
|
||||
|
||||
ctx.status_fd = FASTD_POLL_FD(POLL_TYPE_STATUS, socket(AF_UNIX, SOCK_STREAM, 0));
|
||||
if (ctx.status_fd.fd < 0)
|
||||
exit_errno("fastd_status_init: socket");
|
||||
|
|
Loading…
Add table
Reference in a new issue