From de11c4a45ae70ad15a74ce44d02cb632fb1549f6 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 17 Jan 2015 18:54:05 +0900 Subject: [PATCH] openat_proc_name: port to OS/2 kLIBC OS/2 kLIBC provides a function to retrive a path from a fd. Use it instead of /proc/self/fd. * lib/openat-proc.c (openat_proc_name): Don't assume file name length is less than INT_MAX. Port to OS/2 kLIBC with __libc_Back_ioFHToPath(). --- ChangeLog | 80 ++++++++++++++++++++++++++ lib/openat-proc.c | 144 ++++++++++++++++++++++++++++------------------ 2 files changed, 168 insertions(+), 56 deletions(-) diff --git a/ChangeLog b/ChangeLog index bf678c25c6..8a5aed6979 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,83 @@ +2016-01-15 Paul Eggert + KO Myung-Hun + + openat_proc_name: port to OS/2 kLIBC + OS/2 kLIBC provides a function to retrive a path from a fd. Use it + instead of /proc/self/fd. + * lib/openat-proc.c (openat_proc_name): + Don't assume file name length is less than INT_MAX. + Port to OS/2 kLIBC with __libc_Back_ioFHToPath(). + +2016-01-14 KO Myung-Hun + + stdint: check _INTPTR_T_DECLARED for intptr_t etc. + OS/2 kLIBC's stdint.h defines _INTPTR_T_DECLARED and needs its own + definitions of intptr_t and uintptr_t (which use int and unsigned) + to avoid clashes with declarations of system functions like sbrk. + * lib/stdint.in.h (intptr_t, uintptr_t): Check + _INTPTR_T_DECLARED before defining them. + + opendir, closedir, dirfd, fdopendir: port to OS/2 kLIBC + * lib/closedir.c (closedir): Unregister fd if closedir() succeeds. + * lib/dirent.in.h (_gl_register_dirp_fd, _gl_unregister_dirp_fd): + Declare on kLIBC. + * lib/dirfd.c (struct dirp_fd_list): New. Structures to keep track of + fd associated with dirp. + (_gl_register_dirp_fd): New. Register fd associated with dirp to + dirp_fd_list. + (_gl_unregister_dirp_fd): New. Unregister fd with closing it. + (dirfd): Implemented for kLIBC. + * lib/fdopendir.c (fdopendir): Implemented for kLIBC. + * lib/opendir.c (opendir): New. Register fd and dirp pair if open() + succeeds. + * m4/closedir.m4 (gl_FUNC_CLOSEDIR): Replace if OS/2. + * m4/dirfd.m4 (gl_FUNC_DIRFD): Likewise. + (REPLACE_DIRFD): Define to 1 if replaced. + * m4/opendir.m4 (gl_FUNC_OPENDIR): Likewise. + * modules/closedir (Depends-on): Add dirfd. + * modules/dirfd (Depends-on): Add 'test $REPLACE_DIRFD = 1' to errno + condition. + (configure.ac): Add dirfd to LIBOBJS if $REPLACE_DIRFD = 1 as well. + * modules/opendir (Depends-on): Add dirfd. + + dup, dup2, fcntl: support a directory fd on OS/2 kLIBC + On OS/2 kLIBC, dup(), dup2() and fcntl() do not work on a directory fd. + * lib/dup.c (dup_nothrow): New. + * lib/dup2.c (klibc_dup2dirfd): New. dup2() for a directory fd. + (klibc_dup2): New. + * lib/fcntl.c (klibc_fcntl): New. + * m4/dup.m4 (gl_FUNC_DUP): Check if dup() works on a directory fd. + * m4/dup2.m4 (gl_FUNC_DUP2): Check if dup2() works on a directory fd. + * m4/fcntl.m4 (gl_FUNC_FCNTL): Check if F_DUPFD works on a directory + fd. + + pipe_filter_ii_execute: port to OS/2 kLIBC + Pipes on kLIBC do not support O_NONBLOCK like Win32. + * lib/pipe-filter-ii.c (start_wrapper, _beginthreadex, CloseHandle, + WaiForSingleObject, WaitForMultipleObjects): New on OS/2 kLIBC. + Reuse Win32 code on OS/2 kLIBC. + * lib/spawn-pipe.c: Reuse Win32 code on OS/2 kLIBC. + * lib/w32spawn.h: Do not include windows.h on OS/2 kLIBC. + + wchar: fix "conflicting types" error for __wcwidth on OS/2 kLIBC + On OS/2 kLIBC, wcwidth is a macro that expands to the name of a + static inline function. The implementation of wcwidth in wcwidth.c + causes a "conflicting types" error. + * lib/wchar.in.h: Undefine wcwidth on OS/2 kLIBC. + + w32spawn: clear SHELL_SPECIAL_CHARS and SHELL_SPACE_CHAR on OS/2 kLIBC + spawn() on OS/2 kLIBC is not silly like one on Windows + * libc/w32spawn.h (SHELL_SPECIAL_CHARS, SHELL_SPACE_CHAR): Set both to + empty string on OS/2 kLIBC. + + pipe-filter-aux: undefine HAVE_SELECT on KLIBC + On OS/2 kLIBC, select() works only on sockets. + * lib/pipe-filter-aux.h (HAVE_SELECT): Undefine on OS/2 kLIBC. + + binary-io: don't put fd in binary mode if it is a console on EMX + * lib/binary-io.h (SET_BINARY): Don't put fd in binary mode if it is + a console on EMX. + 2016-01-15 Pádraig Brady doc: mention unfixed issues with unsupported localtime() values diff --git a/lib/openat-proc.c b/lib/openat-proc.c index 15a8c799c8..6fba6c2bbf 100644 --- a/lib/openat-proc.c +++ b/lib/openat-proc.c @@ -30,24 +30,21 @@ #include #include -#include "intprops.h" - -#define PROC_SELF_FD_FORMAT "/proc/self/fd/%d/%s" - -#define PROC_SELF_FD_NAME_SIZE_BOUND(len) \ - (sizeof PROC_SELF_FD_FORMAT - sizeof "%d%s" \ - + INT_STRLEN_BOUND (int) + (len) + 1) +#ifdef __KLIBC__ +# include +#endif +#include "intprops.h" -/* Set BUF to the expansion of PROC_SELF_FD_FORMAT, using FD and FILE - respectively for %d and %s. If successful, return BUF if the - result fits in BUF, dynamically allocated memory otherwise. But - return NULL if /proc is not reliable, either because the operating - system support is lacking or because memory is low. */ +/* Set BUF to the name of the subfile of the directory identified by + FD, where the subfile is named FILE. If successful, return BUF if + the result fits in BUF, dynamically allocated memory otherwise. + Return NULL (setting errno) on error. */ char * openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file) { - static int proc_status = 0; + char *result = buf; + int dirlen; /* Make sure the caller gets ENOENT when appropriate. */ if (!*file) @@ -56,47 +53,82 @@ openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file) return buf; } - if (! proc_status) - { - /* Set PROC_STATUS to a positive value if /proc/self/fd is - reliable, and a negative value otherwise. Solaris 10 - /proc/self/fd mishandles "..", and any file name might expand - to ".." after symbolic link expansion, so avoid /proc/self/fd - if it mishandles "..". Solaris 10 has openat, but this - problem is exhibited on code that built on Solaris 8 and - running on Solaris 10. */ - - int proc_self_fd = open ("/proc/self/fd", - O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK); - if (proc_self_fd < 0) - proc_status = -1; - else - { - /* Detect whether /proc/self/fd/%i/../fd exists, where %i is the - number of a file descriptor open on /proc/self/fd. On Linux, - that name resolves to /proc/self/fd, which was opened above. - However, on Solaris, it may resolve to /proc/self/fd/fd, which - cannot exist, since all names in /proc/self/fd are numeric. */ - char dotdot_buf[PROC_SELF_FD_NAME_SIZE_BOUND (sizeof "../fd" - 1)]; - sprintf (dotdot_buf, PROC_SELF_FD_FORMAT, proc_self_fd, "../fd"); - proc_status = access (dotdot_buf, F_OK) ? -1 : 1; - close (proc_self_fd); - } - } - - if (proc_status < 0) - return NULL; - else - { - size_t bufsize = PROC_SELF_FD_NAME_SIZE_BOUND (strlen (file)); - char *result = buf; - if (OPENAT_BUFFER_SIZE < bufsize) - { - result = malloc (bufsize); - if (! result) - return NULL; - } - sprintf (result, PROC_SELF_FD_FORMAT, fd, file); - return result; - } +#ifndef __KLIBC__ +# define PROC_SELF_FD_FORMAT "/proc/self/fd/%d/" + { + enum { + PROC_SELF_FD_DIR_SIZE_BOUND + = (sizeof PROC_SELF_FD_FORMAT - (sizeof "%d" - 1) + + INT_STRLEN_BOUND (int)) + }; + + static int proc_status = 0; + if (! proc_status) + { + /* Set PROC_STATUS to a positive value if /proc/self/fd is + reliable, and a negative value otherwise. Solaris 10 + /proc/self/fd mishandles "..", and any file name might expand + to ".." after symbolic link expansion, so avoid /proc/self/fd + if it mishandles "..". Solaris 10 has openat, but this + problem is exhibited on code that built on Solaris 8 and + running on Solaris 10. */ + + int proc_self_fd = open ("/proc/self/fd", + O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK); + if (proc_self_fd < 0) + proc_status = -1; + else + { + /* Detect whether /proc/self/fd/%i/../fd exists, where %i is the + number of a file descriptor open on /proc/self/fd. On Linux, + that name resolves to /proc/self/fd, which was opened above. + However, on Solaris, it may resolve to /proc/self/fd/fd, which + cannot exist, since all names in /proc/self/fd are numeric. */ + char dotdot_buf[PROC_SELF_FD_DIR_SIZE_BOUND + sizeof "../fd" - 1]; + sprintf (dotdot_buf, PROC_SELF_FD_FORMAT "../fd", proc_self_fd); + proc_status = access (dotdot_buf, F_OK) ? -1 : 1; + close (proc_self_fd); + } + } + + if (proc_status < 0) + return NULL; + else + { + size_t bufsize = PROC_SELF_FD_DIR_SIZE_BOUND + strlen (file); + if (OPENAT_BUFFER_SIZE < bufsize) + { + result = malloc (bufsize); + if (! result) + return NULL; + } + + dirlen = sprintf (result, PROC_SELF_FD_FORMAT, fd); + } + } +#else + /* OS/2 kLIBC provides a function to retrieve a path from a fd. */ + { + char dir[_MAX_PATH]; + size_t bufsize; + + if (__libc_Back_ioFHToPath (fd, dir, sizeof dir)) + return NULL; + + dirlen = strlen (dir); + bufsize = dirlen + 1 + strlen (file) + 1; /* 1 for '/', 1 for null */ + if (OPENAT_BUFFER_SIZE < bufsize) + { + result = malloc (bufsize); + if (! result) + return NULL; + } + + strcpy (result, dir); + result[dirlen] = '/'; + } +#endif + + strcpy (result + dirlen, file); + return result; } -- 2.39.5