portability: implement pipe2 and mknodat

cute-signatures
Ariadne Conill 2021-12-27 14:44:29 -06:00 committed by Timo Teräs
parent fade8b1ef2
commit e6b1b29028
5 changed files with 64 additions and 0 deletions

View File

@ -7,6 +7,8 @@ libportability_src = []
check_functions = [
['memrchr', 'memrchr.c', 'NEED_MEMRCHR', 'string.h'],
['strlcpy', 'strlcpy.c', 'NEED_STRLCPY', 'string.h'],
['pipe2', 'pipe2.c', 'NEED_PIPE2', 'unistd.h'],
['mknodat', 'mknodat.c', 'NEED_MKNODAT', 'sys/stat.h'],
]

30
portability/mknodat.c Normal file
View File

@ -0,0 +1,30 @@
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev)
{
int ret = 0;
int curdir_fd = open(".", O_DIRECTORY | O_CLOEXEC);
if (curdir_fd < 0)
return -1;
if (fchdir(dirfd) < 0) {
ret = -1;
goto cleanup;
}
/* if mknod fails, fall through and restore the original dirfd */
if (mknod(pathname, mode, dev) < 0) {
ret = -1;
}
if (fchdir(curdir_fd) < 0) {
ret = -1;
goto cleanup;
}
cleanup:
close(curdir_fd);
return ret;
}

22
portability/pipe2.c Normal file
View File

@ -0,0 +1,22 @@
#include <fcntl.h>
#include <unistd.h>
int pipe2(int pipefd[2], int flags)
{
int r;
if ((r = pipe(pipefd)) < 0)
return r;
if (flags & O_CLOEXEC) {
(void) fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
(void) fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
}
if (flags & O_NONBLOCK) {
(void) fcntl(pipefd[0], F_SETFL, O_NONBLOCK);
(void) fcntl(pipefd[1], F_SETFL, O_NONBLOCK);
}
return 0;
}

5
portability/sys/stat.h Normal file
View File

@ -0,0 +1,5 @@
#include_next <sys/stat.h>
#ifdef NEED_MKNODAT
int mknodat(int dirfd, const char *pathname, mode_t mode, dev_t dev);
#endif

5
portability/unistd.h Normal file
View File

@ -0,0 +1,5 @@
#include_next <unistd.h>
#ifdef NEED_PIPE2
int pipe2(int pipefd[2], int flags);
#endif