From c4da7817b4c8dd71b2a07d4d1c46b486ec57eeb4 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 5 Mar 2018 17:27:04 -0800 Subject: fixes #265 nngcat should support persistent ZT nodes fixes #267 zerotier transport should lock ZT_HOME --- src/platform/posix/posix_file.c | 27 +++++++++++++++++++++++++++ src/platform/posix/posix_impl.h | 4 ++++ 2 files changed, 31 insertions(+) (limited to 'src/platform/posix') diff --git a/src/platform/posix/posix_file.c b/src/platform/posix/posix_file.c index 7863fdee..83d045fa 100644 --- a/src/platform/posix/posix_file.c +++ b/src/platform/posix/posix_file.c @@ -260,6 +260,33 @@ nni_plat_file_basename(const char *path) return (path); } +int +nni_plat_file_lock(const char *path, nni_plat_flock *lk) +{ + int fd; + if ((fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) { + return (nni_plat_errno(errno)); + } + if (lockf(fd, F_TLOCK, 0) < 0) { + int rv = errno; + close(fd); + if (rv == EAGAIN) { + return (NNG_EBUSY); + } + return (nni_plat_errno(rv)); + } + lk->fd = fd; + return (0); +} + +void +nni_plat_file_unlock(nni_plat_flock *lk) +{ + int fd = lk->fd; + lk->fd = -1; + (void) close(fd); +} + char * nni_plat_temp_dir(void) { diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 0b2a09f0..3616c69b 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -76,6 +76,10 @@ struct nni_plat_thr { void *arg; }; +struct nni_plat_flock { + int fd; +}; + #define NNG_PLATFORM_DIR_SEP "/" #endif -- cgit v1.2.3-70-g09d2