aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix
diff options
context:
space:
mode:
Diffstat (limited to 'src/platform/posix')
-rw-r--r--src/platform/posix/posix_file.c19
-rw-r--r--src/platform/posix/posix_pollq_epoll.c9
2 files changed, 27 insertions, 1 deletions
diff --git a/src/platform/posix/posix_file.c b/src/platform/posix/posix_file.c
index 83d045fa..b1da3cd5 100644
--- a/src/platform/posix/posix_file.c
+++ b/src/platform/posix/posix_file.c
@@ -22,6 +22,11 @@
#include <sys/types.h>
#include <unistd.h>
+// Some systems -- Android -- have BSD flock but not POSIX lockf.
+#if defined(NNG_HAVE_FLOCK) && !defined(NNG_HAVE_LOCKF)
+#include <sys/file.h>
+#endif
+
// File support.
static int
@@ -264,10 +269,22 @@ int
nni_plat_file_lock(const char *path, nni_plat_flock *lk)
{
int fd;
+ int rv;
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) {
+#ifdef NNG_HAVE_LOCKF
+ rv = lockf(fd, F_TLOCK, 0);
+#elif defined NNG_HAVE_FLOCK
+ rv = flock(fd, LOCK_EX | LOCK_NB);
+#else
+ // We don't have locking support. This means you live dangerously.
+ // For example, ZeroTier cannot be sure that nothing else is using
+ // the same configuration file. If you're here, its probably an
+ // embedded scenario, and we can live with it.
+ rv = 0;
+#endif
+ if (rv < 0) {
int rv = errno;
close(fd);
if (rv == EAGAIN) {
diff --git a/src/platform/posix/posix_pollq_epoll.c b/src/platform/posix/posix_pollq_epoll.c
index 9c1ae682..e4f9ddb6 100644
--- a/src/platform/posix/posix_pollq_epoll.c
+++ b/src/platform/posix/posix_pollq_epoll.c
@@ -348,9 +348,18 @@ nni_posix_pollq_create(nni_posix_pollq *pq)
{
int rv;
+#if NNG_HAVE_EPOLL_CREATE1
if ((pq->epfd = epoll_create1(EPOLL_CLOEXEC)) < 0) {
return (nni_plat_errno(errno));
}
+#else
+ // Old Linux. Size is a "hint" about number of descriptors.
+ // Hopefully not a hard limit, and not used in modern Linux.
+ if ((pq->epfd = epoll_create(16)) < 0) {
+ return (nni_plat_errno(errno));
+ }
+ (void) fcntl(pq->epfd, F_SETFD, FD_CLOEXEC);
+#endif
pq->close = false;