Commit 97d187d4 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Renovate our pid-file handling.

The FreeBSD origin has moved into territory where we will not go
(CAPSICUM) so we can now varnish-ify this without cost.
parent bf55b2fc
...@@ -839,8 +839,9 @@ main(int argc, char * const *argv) ...@@ -839,8 +839,9 @@ main(int argc, char * const *argv)
S_arg = make_secret(dirname); S_arg = make_secret(dirname);
AN(S_arg); AN(S_arg);
assert(!VPF_Write(pfh1)); VPF_Write(pfh1);
assert(pfh2 == NULL || !VPF_Write(pfh2)); if (pfh2 != NULL)
VPF_Write(pfh2);
MGT_Complain(C_DEBUG, "Version: %s", VCS_version); MGT_Complain(C_DEBUG, "Version: %s", VCS_version);
MGT_Complain(C_DEBUG, "Platform: %s", VSB_data(vident) + 1); MGT_Complain(C_DEBUG, "Platform: %s", VSB_data(vident) + 1);
...@@ -922,8 +923,8 @@ main(int argc, char * const *argv) ...@@ -922,8 +923,8 @@ main(int argc, char * const *argv)
MGT_Complain(C_INFO, "manager dies"); MGT_Complain(C_INFO, "manager dies");
mgt_cli_close_all(); mgt_cli_close_all();
VEV_Destroy(&mgt_evb); VEV_Destroy(&mgt_evb);
(void)VPF_Remove(pfh1); VPF_Remove(pfh1);
if (pfh2 != NULL) if (pfh2 != NULL)
(void)VPF_Remove(pfh2); VPF_Remove(pfh2);
exit(exit_status); exit(exit_status);
} }
...@@ -14,8 +14,4 @@ delay 1 ...@@ -14,8 +14,4 @@ delay 1
shell "rm -f ${tmpdir}/ncsa.pid" shell "rm -f ${tmpdir}/ncsa.pid"
process p1 -expect-exit 1 -stop -wait process p1 -stop -wait
shell -expect "Cannot remove pid file ${tmpdir}/ncsa.pid" {
cat ${tmpdir}/p1/stderr
}
...@@ -417,7 +417,7 @@ haproxy_wait_pidfile(struct haproxy *h) ...@@ -417,7 +417,7 @@ haproxy_wait_pidfile(struct haproxy *h)
if (vtc_error) if (vtc_error)
return; return;
if (VPF_read(h->pid_fn, &pid) != 0) { if (VPF_Read(h->pid_fn, &pid) != 0) {
bprintf(buf_err, bprintf(buf_err,
"Could not read PID file '%s'", h->pid_fn); "Could not read PID file '%s'", h->pid_fn);
usleep(usleep_time); usleep(usleep_time);
......
...@@ -33,9 +33,8 @@ ...@@ -33,9 +33,8 @@
struct vpf_fh; struct vpf_fh;
struct vpf_fh *VPF_Open(const char *path, mode_t mode, pid_t *pidptr); struct vpf_fh *VPF_Open(const char *path, mode_t mode, pid_t *pidptr);
int VPF_Write(struct vpf_fh *pfh); int VPF_Read(const char *path, pid_t *);
int VPF_Close(struct vpf_fh *pfh); void VPF_Write(const struct vpf_fh *pfh);
int VPF_Remove(struct vpf_fh *pfh); void VPF_Remove(struct vpf_fh *pfh);
int VPF_read(const char *path, pid_t *);
#endif #endif
...@@ -47,13 +47,11 @@ ...@@ -47,13 +47,11 @@
struct vpf_fh { struct vpf_fh {
int pf_fd; int pf_fd;
char pf_path[MAXPATHLEN + 1]; char *pf_path;
dev_t pf_dev; dev_t pf_dev;
ino_t pf_ino; ino_t pf_ino;
}; };
static int _VPF_Remove(struct vpf_fh *pfh, int freeit);
static int static int
vpf_verify(const struct vpf_fh *pfh) vpf_verify(const struct vpf_fh *pfh)
{ {
...@@ -72,7 +70,7 @@ vpf_verify(const struct vpf_fh *pfh) ...@@ -72,7 +70,7 @@ vpf_verify(const struct vpf_fh *pfh)
} }
int int
VPF_read(const char *path, pid_t *pidptr) VPF_Read(const char *path, pid_t *pidptr)
{ {
char buf[16], *endptr; char buf[16], *endptr;
int error, fd, i; int error, fd, i;
...@@ -82,8 +80,8 @@ VPF_read(const char *path, pid_t *pidptr) ...@@ -82,8 +80,8 @@ VPF_read(const char *path, pid_t *pidptr)
return (errno); return (errno);
i = read(fd, buf, sizeof(buf) - 1); i = read(fd, buf, sizeof(buf) - 1);
error = errno; /* Remember errno in case close() wants to change it. */ error = errno;
(void)close(fd); closefd(&fd);
if (i == -1) if (i == -1)
return (error); return (error);
else if (i == 0) else if (i == 0)
...@@ -104,21 +102,7 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr) ...@@ -104,21 +102,7 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr)
{ {
struct vpf_fh *pfh; struct vpf_fh *pfh;
struct stat sb; struct stat sb;
int error, fd, len; int fd;
pfh = malloc(sizeof(*pfh));
if (pfh == NULL)
return (NULL);
assert(path != NULL);
len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
"%s", path);
if (len >= (int)sizeof(pfh->pf_path)) {
free(pfh);
errno = ENAMETOOLONG;
return (NULL);
}
/* /*
* Open the PID file and obtain exclusive lock. * Open the PID file and obtain exclusive lock.
...@@ -126,15 +110,14 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr) ...@@ -126,15 +110,14 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr)
* PID file will be truncated again in VPF_Write(), so * PID file will be truncated again in VPF_Write(), so
* VPF_Write() can be called multiple times. * VPF_Write() can be called multiple times.
*/ */
fd = VFL_Open(pfh->pf_path, fd = VFL_Open(path,
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode); O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode);
if (fd == -1) { if (fd == -1) {
if (errno == EWOULDBLOCK && pidptr != NULL) { if (errno == EWOULDBLOCK && pidptr != NULL) {
errno = VPF_read(pfh->pf_path, pidptr); errno = VPF_Read(path, pidptr);
if (errno == 0) if (errno == 0)
errno = EEXIST; errno = EEXIST;
} }
free(pfh);
return (NULL); return (NULL);
} }
...@@ -142,14 +125,12 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr) ...@@ -142,14 +125,12 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr)
* Remember file information, so in VPF_Write() we are sure we write * Remember file information, so in VPF_Write() we are sure we write
* to the proper descriptor. * to the proper descriptor.
*/ */
if (fstat(fd, &sb) == -1) { AZ(fstat(fd, &sb));
error = errno;
(void)unlink(pfh->pf_path); pfh = malloc(sizeof(*pfh));
(void)close(fd); AN(pfh);
free(pfh); pfh->pf_path = strdup(path);
errno = error; AN(pfh->pf_path);
return (NULL);
}
pfh->pf_fd = fd; pfh->pf_fd = fd;
pfh->pf_dev = sb.st_dev; pfh->pf_dev = sb.st_dev;
...@@ -158,99 +139,35 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr) ...@@ -158,99 +139,35 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr)
return (pfh); return (pfh);
} }
int void
VPF_Write(struct vpf_fh *pfh) VPF_Write(const struct vpf_fh *pfh)
{ {
char pidstr[16]; char pidstr[16];
int error, fd;
/* /*
* Check remembered descriptor, so we don't overwrite some other * Check remembered descriptor, so we don't overwrite some other
* file if pidfile was closed and descriptor reused. * file if pidfile was closed and descriptor reused.
*/ */
errno = vpf_verify(pfh); if (vpf_verify(pfh) != 0)
if (errno != 0) { return;
/*
* Don't close descriptor, because we are not sure if it's ours.
*/
return (-1);
}
fd = pfh->pf_fd;
/* /*
* Truncate PID file, so multiple calls of VPF_Write() are allowed. * Truncate PID file, so multiple calls of VPF_Write() are allowed.
*/ */
if (ftruncate(fd, 0) == -1) { AZ(ftruncate(pfh->pf_fd, 0));
error = errno;
(void)_VPF_Remove(pfh, 0);
errno = error;
return (-1);
}
error = snprintf(pidstr, sizeof(pidstr), "%jd", (intmax_t)getpid()); bprintf(pidstr, "%jd", (intmax_t)getpid());
assert(error < sizeof pidstr); assert(pwrite(pfh->pf_fd, pidstr, strlen(pidstr), 0) ==
if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) { (ssize_t)strlen(pidstr));
error = errno;
(void)_VPF_Remove(pfh, 0);
errno = error;
return (-1);
}
return (0);
} }
int void
VPF_Close(struct vpf_fh *pfh) VPF_Remove(struct vpf_fh *pfh)
{ {
int error; if (vpf_verify(pfh) == 0) {
(void)unlink(pfh->pf_path);
error = vpf_verify(pfh); closefd(&pfh->pf_fd);
if (error != 0) {
errno = error;
return (-1);
} }
free(pfh->pf_path);
if (close(pfh->pf_fd) == -1)
error = errno;
free(pfh); free(pfh);
if (error != 0) {
errno = error;
return (-1);
}
return (0);
}
static int
_VPF_Remove(struct vpf_fh *pfh, int freeit)
{
int error;
error = vpf_verify(pfh);
if (error != 0) {
errno = error;
return (-1);
}
if (unlink(pfh->pf_path) == -1)
error = errno;
if (close(pfh->pf_fd) == -1) {
if (error == 0)
error = errno;
}
if (freeit)
free(pfh);
else
pfh->pf_fd = -1;
if (error != 0) {
errno = error;
return (-1);
}
return (0);
}
int
VPF_Remove(struct vpf_fh *pfh)
{
return (_VPF_Remove(pfh, 1));
} }
...@@ -81,10 +81,7 @@ vut_vpf_remove(void) ...@@ -81,10 +81,7 @@ vut_vpf_remove(void)
AN(pfh); AN(pfh);
AN(pfh_vut.P_arg); AN(pfh_vut.P_arg);
if (VPF_Remove(pfh) != 0) VPF_Remove(pfh);
VUT_Error(&pfh_vut, 1, "Cannot remove pid file %s: %s",
pfh_vut.P_arg, strerror(errno));
free(pfh_vut.P_arg); free(pfh_vut.P_arg);
ZERO_OBJ(&pfh_vut, sizeof pfh_vut); ZERO_OBJ(&pfh_vut, sizeof pfh_vut);
pfh = NULL; pfh = NULL;
...@@ -312,7 +309,7 @@ VUT_Setup(struct VUT *vut) ...@@ -312,7 +309,7 @@ VUT_Setup(struct VUT *vut)
/* Write PID and setup exit handler */ /* Write PID and setup exit handler */
if (vut->P_arg) { if (vut->P_arg) {
AN(pfh); AN(pfh);
AZ(VPF_Write(pfh)); VPF_Write(pfh);
/* NB: move ownership to a global pseudo-VUT. */ /* NB: move ownership to a global pseudo-VUT. */
INIT_OBJ(&pfh_vut, VUT_MAGIC); INIT_OBJ(&pfh_vut, VUT_MAGIC);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment