| |
| |
| |
| |
| |
| |
| |
| #include <fcntl.h> |
| #include <stdlib.h> |
| #include <limits.h> |
| #include <errno.h> |
| #include <sys/stat.h> |
| #include <slibtool/slibtool.h> |
| |
| #include "slibtool_readlink_impl.h" |
| |
| #ifdef _MIDIPIX_ABI |
| #include <sys/fs.h> |
| #endif |
| |
| #ifndef ENOTSUP |
| #define ENOTSUP EOPNOTSUPP |
| #endif |
| |
| int slbt_realpath( |
| int fdat, |
| const char * path, |
| int options, |
| char * buf, |
| size_t buflen) |
| { |
| int ret; |
| int fd; |
| int fdproc; |
| struct stat st; |
| struct stat stproc; |
| char procfspath[36]; |
| |
| |
| if (!buf || (options & O_CREAT)) { |
| errno = EINVAL; |
| return -1; |
| } |
| |
| |
| #ifdef _MIDIPIX_ABI |
| return __fs_rpath(fdat,path,options,buf,buflen); |
| #endif |
| |
| |
| if (buflen < PATH_MAX) { |
| errno = ENOBUFS; |
| return -1; |
| } |
| |
| |
| if (fdat == AT_FDCWD) { |
| return realpath(path,buf) ? 0 : -1; |
| } |
| |
| |
| if ((fd = openat(fdat,path,options,0)) < 0) |
| return -1; |
| |
| sprintf(procfspath,"/proc/self/fd/%d",fd); |
| |
| if (slbt_readlink(procfspath,buf,buflen)) { |
| close(fd); |
| return -1; |
| } |
| |
| if ((fdproc = openat(AT_FDCWD,buf,options|O_NOFOLLOW,0)) < 0) { |
| close(fd); |
| errno = ELOOP; |
| return -1; |
| } |
| |
| ret = fstat(fd,&st) || fstat(fdproc,&stproc); |
| |
| close(fd); |
| close(fdproc); |
| |
| if (ret || (st.st_dev != stproc.st_dev) || (st.st_ino != stproc.st_ino)) { |
| errno = ENOTSUP; |
| return -1; |
| } |
| |
| return 0; |
| } |