Skip to content

Commit

Permalink
main REFACTOR use fcntl instead of lockf
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvasko committed May 10, 2024
1 parent d0c8d13 commit c14b7bb
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ server_open_pidfile(const char *pidfile)
{
int pidfd, len;
char pid[8];
struct flock fl;

/* make sure we are the only instance - lock the PID file and write the PID */
pidfd = open(pidfile, O_RDWR | O_CREAT, 00644);
Expand All @@ -668,7 +669,11 @@ server_open_pidfile(const char *pidfile)
return -1;
}

if (lockf(pidfd, F_TLOCK, 0) < 0) {
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_start = 5;
if (fcntl(pidfd, F_SETLK, &fl) < 0) {
close(pidfd);
if ((errno == EACCES) || (errno == EAGAIN)) {
ERR("Another instance of the Netopeer2 server is running.");
Expand Down

0 comments on commit c14b7bb

Please sign in to comment.