mirror of
https://gitlab.freedesktop.org/libbsd/libbsd.git
synced 2025-10-20 22:31:34 +02:00
Update pidfile module from FreeBSD
Use EINVAL instead of EDOOFUS. Add a missing synopsis for pidfile_fileno() in the man page. Move the definition of struct pidfh from libutil.h into pidfile.c following upstream change.
This commit is contained in:
@@ -29,20 +29,27 @@
|
||||
#include <sys/file.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <libutil.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct pidfh {
|
||||
int pf_fd;
|
||||
char *pf_path;
|
||||
dev_t pf_dev;
|
||||
ino_t pf_ino;
|
||||
};
|
||||
|
||||
static int _pidfile_remove(struct pidfh *pfh, int freeit);
|
||||
|
||||
static int
|
||||
pidfile_verify(struct pidfh *pfh)
|
||||
pidfile_verify(const struct pidfh *pfh)
|
||||
{
|
||||
struct stat sb;
|
||||
|
||||
@@ -114,25 +121,33 @@ pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
|
||||
fd = flopen(pfh->pf_path,
|
||||
O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
|
||||
if (fd == -1) {
|
||||
count = 0;
|
||||
rqtp.tv_sec = 0;
|
||||
rqtp.tv_nsec = 5000000;
|
||||
if (errno == EWOULDBLOCK && pidptr != NULL) {
|
||||
again:
|
||||
errno = pidfile_read(pfh->pf_path, pidptr);
|
||||
if (errno == 0)
|
||||
if (errno == EWOULDBLOCK) {
|
||||
if (pidptr == NULL) {
|
||||
errno = EEXIST;
|
||||
else if (errno == EAGAIN) {
|
||||
if (++count <= 3) {
|
||||
} else {
|
||||
count = 20;
|
||||
rqtp.tv_sec = 0;
|
||||
rqtp.tv_nsec = 5000000;
|
||||
for (;;) {
|
||||
errno = pidfile_read(pfh->pf_path,
|
||||
pidptr);
|
||||
if (errno != EAGAIN || --count == 0)
|
||||
break;
|
||||
nanosleep(&rqtp, 0);
|
||||
goto again;
|
||||
}
|
||||
if (errno == EAGAIN)
|
||||
*pidptr = -1;
|
||||
if (errno == 0 || errno == EAGAIN)
|
||||
errno = EEXIST;
|
||||
}
|
||||
}
|
||||
error = errno;
|
||||
free(pfh->pf_path);
|
||||
free(pfh);
|
||||
errno = error;
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Remember file information, so in pidfile_write() we are sure we write
|
||||
* to the proper descriptor.
|
||||
@@ -251,3 +266,14 @@ pidfile_remove(struct pidfh *pfh)
|
||||
|
||||
return (_pidfile_remove(pfh, 1));
|
||||
}
|
||||
|
||||
int
|
||||
pidfile_fileno(const struct pidfh *pfh)
|
||||
{
|
||||
|
||||
if (pfh == NULL || pfh->pf_fd == -1) {
|
||||
errno = EINVAL;
|
||||
return (-1);
|
||||
}
|
||||
return (pfh->pf_fd);
|
||||
}
|
||||
|
Reference in New Issue
Block a user