Fix targets in test/automated/Makefile.in
[emacs.git] / lib / openat-proc.c
blob1712340a00274ccb976a3c3a996475207be9124c
1 /* Create /proc/self/fd-related names for subfiles of open directories.
3 Copyright (C) 2006, 2009-2016 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
20 #include <config.h>
22 #include "openat-priv.h"
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #ifdef __KLIBC__
34 # include <InnoTekLIBC/backend.h>
35 #endif
37 #include "intprops.h"
39 /* Set BUF to the name of the subfile of the directory identified by
40 FD, where the subfile is named FILE. If successful, return BUF if
41 the result fits in BUF, dynamically allocated memory otherwise.
42 Return NULL (setting errno) on error. */
43 char *
44 openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file)
46 char *result = buf;
47 int dirlen;
49 /* Make sure the caller gets ENOENT when appropriate. */
50 if (!*file)
52 buf[0] = '\0';
53 return buf;
56 #ifndef __KLIBC__
57 # define PROC_SELF_FD_FORMAT "/proc/self/fd/%d/"
59 enum {
60 PROC_SELF_FD_DIR_SIZE_BOUND
61 = (sizeof PROC_SELF_FD_FORMAT - (sizeof "%d" - 1)
62 + INT_STRLEN_BOUND (int))
65 static int proc_status = 0;
66 if (! proc_status)
68 /* Set PROC_STATUS to a positive value if /proc/self/fd is
69 reliable, and a negative value otherwise. Solaris 10
70 /proc/self/fd mishandles "..", and any file name might expand
71 to ".." after symbolic link expansion, so avoid /proc/self/fd
72 if it mishandles "..". Solaris 10 has openat, but this
73 problem is exhibited on code that built on Solaris 8 and
74 running on Solaris 10. */
76 int proc_self_fd = open ("/proc/self/fd",
77 O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK);
78 if (proc_self_fd < 0)
79 proc_status = -1;
80 else
82 /* Detect whether /proc/self/fd/%i/../fd exists, where %i is the
83 number of a file descriptor open on /proc/self/fd. On Linux,
84 that name resolves to /proc/self/fd, which was opened above.
85 However, on Solaris, it may resolve to /proc/self/fd/fd, which
86 cannot exist, since all names in /proc/self/fd are numeric. */
87 char dotdot_buf[PROC_SELF_FD_DIR_SIZE_BOUND + sizeof "../fd" - 1];
88 sprintf (dotdot_buf, PROC_SELF_FD_FORMAT "../fd", proc_self_fd);
89 proc_status = access (dotdot_buf, F_OK) ? -1 : 1;
90 close (proc_self_fd);
94 if (proc_status < 0)
95 return NULL;
96 else
98 size_t bufsize = PROC_SELF_FD_DIR_SIZE_BOUND + strlen (file);
99 if (OPENAT_BUFFER_SIZE < bufsize)
101 result = malloc (bufsize);
102 if (! result)
103 return NULL;
106 dirlen = sprintf (result, PROC_SELF_FD_FORMAT, fd);
109 #else
110 /* OS/2 kLIBC provides a function to retrieve a path from a fd. */
112 char dir[_MAX_PATH];
113 size_t bufsize;
115 if (__libc_Back_ioFHToPath (fd, dir, sizeof dir))
116 return NULL;
118 dirlen = strlen (dir);
119 bufsize = dirlen + 1 + strlen (file) + 1; /* 1 for '/', 1 for null */
120 if (OPENAT_BUFFER_SIZE < bufsize)
122 result = malloc (bufsize);
123 if (! result)
124 return NULL;
127 strcpy (result, dir);
128 result[dirlen++] = '/';
130 #endif
132 strcpy (result + dirlen, file);
133 return result;