33 lines
872 B
C
33 lines
872 B
C
//
|
|
// Created by xajhuang on 2023/3/2.
|
|
//
|
|
#include <fcntl.h>
|
|
#include <sys/file.h>
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include "user_errno.h"
|
|
#include "misc.h"
|
|
static int g_lockfd = -1;
|
|
static char g_pidPath[MAX_PATH] = {0};
|
|
|
|
int process_lock_pidfile(char *pFilePath) {
|
|
if (pFilePath == NULL || strlen(pFilePath) == 0) {
|
|
return ERR_SUCCESS;
|
|
}
|
|
|
|
strcpy(g_pidPath, pFilePath);
|
|
|
|
g_lockfd = open(pFilePath, O_CREAT | O_RDWR, 0666); // open file
|
|
int rc = flock(g_lockfd, LOCK_EX | LOCK_NB); // lock access to open file
|
|
if (rc && EWOULDBLOCK == errno) { // check lock success
|
|
return -ERR_FILE_LOCKED; // another instance is running
|
|
}
|
|
|
|
return ERR_SUCCESS;
|
|
}
|
|
|
|
void process_unlock_pidfile() {
|
|
close(g_lockfd);
|
|
unlink(g_pidPath);
|
|
} |