Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

about zlog_rotater_trylock function doubt in version 1.2.17 #257

Open
haiyang-lhy opened this issue Jun 17, 2024 · 2 comments
Open

about zlog_rotater_trylock function doubt in version 1.2.17 #257

haiyang-lhy opened this issue Jun 17, 2024 · 2 comments

Comments

@haiyang-lhy
Copy link

haiyang-lhy commented Jun 17, 2024

Hi
I found the zlog_rotater_trylock has been reimplemented in version 1.2.17, and the file lock funtion for process protection has been replaced by lock_file function
static int zlog_rotater_trylock(zlog_rotater_t *a_rotater)
{
int rc;

rc = pthread_mutex_trylock(&(a_rotater->lock_mutex));
if (rc == EBUSY) {
	zc_warn("pthread_mutex_trylock fail, as lock_mutex is locked by other threads");
	return -1;
} else if (rc != 0) {
	zc_error("pthread_mutex_trylock fail, rc[%d]", rc);
	return -1;
}

a_rotater->lock_fd = lock_file(a_rotater->lock_file);
if (a_rotater->lock_fd == INVALID_LOCK_FD) {
	return -1;
}

return 0;

}

But I found the lock_file function only call open in linux, How it implements the multi process file lock protectioin function? please help explain it。 thanks very much

LOCK_FD lock_file(char* path) {
if (!path || strlen(path) <= 0) {
return INVALID_LOCK_FD;
}
#ifdef _WIN32
LOCK_FD fd = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (fd == INVALID_LOCK_FD) {
DWORD err = GetLastError();
zc_error("lock file error : %d ", err);
}
#else
LOCK_FD fd = open(path, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
if (fd == INVALID_LOCK_FD) {
zc_error("lock file error : %s ", strerror(errno));
}
#endif
return fd;
}

@deemar
Copy link
Collaborator

deemar commented Jun 17, 2024

It is a mechanism called file locking in Linux.
You can google it.

@haiyang-lhy
Copy link
Author

haiyang-lhy commented Jun 17, 2024

@deemar Hi , I know linux use fcntl API for file lock, but I can't find any code to call fcntl function in version 1.2.17.

the lock_file function only call open function.
zlog_rotater_trylock--->lock_file--->open
LOCK_FD fd = open(path, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants