From 37f01c869de14e0f0e6d609e2b8f3448167c11db Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Wed, 30 Sep 2015 00:31:17 +0200 Subject: [PATCH] messages_dgm: Add messaging_dgm_get_unique To be able to read, we need to open the lockfile O_RDWR instead of O_WRONLY Signed-off-by: Volker Lendecke Reviewed-by: Jeremy Allison --- source3/lib/messages_dgm.c | 62 +++++++++++++++++++++++++++++++++++++++++++++- source3/lib/messages_dgm.h | 1 + 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/source3/lib/messages_dgm.c b/source3/lib/messages_dgm.c index 944602ac59f..1acd2d738d7 100644 --- a/source3/lib/messages_dgm.c +++ b/source3/lib/messages_dgm.c @@ -84,7 +84,7 @@ static int messaging_dgm_lockfile_create(struct messaging_dgm_context *ctx, /* no O_EXCL, existence check is via the fcntl lock */ - lockfile_fd = open(lockfile_name.buf, O_NONBLOCK|O_CREAT|O_WRONLY, + lockfile_fd = open(lockfile_name.buf, O_NONBLOCK|O_CREAT|O_RDWR, 0644); if (lockfile_fd == -1) { ret = errno; @@ -303,6 +303,66 @@ static void messaging_dgm_recv(struct unix_msg_ctx *ctx, dgm_ctx->recv_cb_private_data); } +static int messaging_dgm_read_unique(int fd, uint64_t *punique) +{ + char buf[25]; + ssize_t rw_ret; + unsigned long long unique; + char *endptr; + + rw_ret = pread(fd, buf, sizeof(buf)-1, 0); + if (rw_ret == -1) { + return errno; + } + buf[rw_ret] = '\0'; + + unique = strtoull(buf, &endptr, 10); + if ((unique == 0) && (errno == EINVAL)) { + return EINVAL; + } + if ((unique == ULLONG_MAX) && (errno == ERANGE)) { + return ERANGE; + } + if (endptr[0] != '\n') { + return EINVAL; + } + *punique = unique; + return 0; +} + +int messaging_dgm_get_unique(pid_t pid, uint64_t *unique) +{ + struct messaging_dgm_context *ctx = global_dgm_context; + struct sun_path_buf lockfile_name; + int ret, fd; + + if (ctx == NULL) { + return EBADF; + } + + if (pid == getpid()) { + /* + * Protect against losing our own lock + */ + return messaging_dgm_read_unique(ctx->lockfile_fd, unique); + } + + ret = snprintf(lockfile_name.buf, sizeof(lockfile_name.buf), + "%s/%u", ctx->lockfile_dir.buf, (int)pid); + if (ret >= sizeof(lockfile_name.buf)) { + return ENAMETOOLONG; + } + + fd = open(lockfile_name.buf, O_NONBLOCK|O_RDONLY, 0); + if (fd == -1) { + return errno; + } + + ret = messaging_dgm_read_unique(fd, unique); + close(fd); + return ret; +} + int messaging_dgm_cleanup(pid_t pid) { struct messaging_dgm_context *ctx = global_dgm_context; diff --git a/source3/lib/messages_dgm.h b/source3/lib/messages_dgm.h index c9c9c611a8b..d38cfcc65df 100644 --- a/source3/lib/messages_dgm.h +++ b/source3/lib/messages_dgm.h @@ -35,6 +35,7 @@ int messaging_dgm_init(struct tevent_context *ev, void *private_data), void *recv_cb_private_data); void messaging_dgm_destroy(void); +int messaging_dgm_get_unique(pid_t pid, uint64_t *unique); int messaging_dgm_send(pid_t pid, const struct iovec *iov, int iovlen, const int *fds, size_t num_fds); -- 2.11.4.GIT