1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-10 01:18:15 +03:00

ctdb-common: Reimplement pidfile_context_create() using pidfile_path_create()

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Volker Lendecke <vl@samba.org>

Autobuild-User(master): Martin Schwenke <martins@samba.org>
Autobuild-Date(master): Wed Aug  2 07:28:44 CEST 2017 on sn-devel-144
This commit is contained in:
Martin Schwenke 2017-07-31 15:26:36 +10:00 committed by Martin Schwenke
parent 09b0398865
commit edf77a112f

View File

@ -23,6 +23,7 @@
#include <talloc.h>
#include "lib/util/blocking.h"
#include "lib/util/pidfile.h"
#include "common/pidfile.h"
@ -38,11 +39,7 @@ int pidfile_context_create(TALLOC_CTX *mem_ctx, const char *pidfile,
struct pidfile_context **result)
{
struct pidfile_context *pid_ctx;
struct flock lck;
char tmp[64];
int fd, ret = 0;
int len;
ssize_t nwritten;
pid_ctx = talloc_zero(mem_ctx, struct pidfile_context);
if (pid_ctx == NULL) {
@ -57,67 +54,18 @@ int pidfile_context_create(TALLOC_CTX *mem_ctx, const char *pidfile,
pid_ctx->pid = getpid();
fd = open(pidfile, O_CREAT|O_WRONLY|O_NONBLOCK, 0644);
if (fd == -1) {
ret = errno;
goto fail;
}
if (! set_close_on_exec(fd)) {
close(fd);
ret = EIO;
goto fail;
ret = pidfile_path_create(pid_ctx->pidfile, &fd);
if (ret != 0) {
return ret;
}
pid_ctx->fd = fd;
lck = (struct flock) {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
};
do {
ret = fcntl(fd, F_SETLK, &lck);
} while ((ret == -1) && (errno == EINTR));
if (ret != 0) {
ret = errno;
goto fail;
}
do {
ret = ftruncate(fd, 0);
} while ((ret == -1) && (errno == EINTR));
if (ret == -1) {
ret = EIO;
goto fail_unlink;
}
len = snprintf(tmp, sizeof(tmp), "%u\n", pid_ctx->pid);
if (len < 0) {
ret = EIO;
goto fail_unlink;
}
do {
nwritten = write(fd, tmp, len);
} while ((nwritten == -1) && (errno == EINTR));
if ((nwritten == -1) || (nwritten != len)) {
ret = EIO;
goto fail_unlink;
}
talloc_set_destructor(pid_ctx, pidfile_context_destructor);
*result = pid_ctx;
return 0;
fail_unlink:
unlink(pidfile);
close(fd);
fail:
talloc_free(pid_ctx);
return ret;
@ -125,27 +73,13 @@ fail:
static int pidfile_context_destructor(struct pidfile_context *pid_ctx)
{
struct flock lck;
int ret;
if (getpid() != pid_ctx->pid) {
return 0;
}
lck = (struct flock) {
.l_type = F_UNLCK,
.l_whence = SEEK_SET,
};
(void) unlink(pid_ctx->pidfile);
do {
ret = fcntl(pid_ctx->fd, F_SETLK, &lck);
} while ((ret == -1) && (errno == EINTR));
do {
ret = close(pid_ctx->fd);
} while ((ret == -1) && (errno == EINTR));
pidfile_fd_close(pid_ctx->fd);
return 0;
}