+int
+evutil_open_closeonexec(const char *pathname, int flags, unsigned mode)
+{
+ int fd;
+
+#ifdef O_CLOEXEC
+ flags |= O_CLOEXEC;
+#endif
+
+ if (flags & O_CREAT)
+ fd = open(pathname, flags, (mode_t)mode);
+ else
+ fd = open(pathname, flags);
+ if (fd < 0)
+ return -1;
+
+#if !defined(O_CLOEXEC) && defined(FD_CLOEXEC)
+ if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
+ return -1;
+#endif
+
+ return fd;
+}
+