* sysdeps/m68k/dl-machine.h (_dl_start_user): Pass correct
[glibc.git] / elf / chroot_canon.c
blob82cb8c4bf043ca5081864c6941c6c6960f6b14f5
1 /* Return the canonical absolute name of a given file inside chroot.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <limits.h>
24 #include <sys/param.h>
25 #include <sys/stat.h>
26 #include <errno.h>
27 #include <stddef.h>
28 #include "ldconfig.h"
30 #ifndef PATH_MAX
31 #define PATH_MAX 1024
32 #endif
34 /* Return the canonical absolute name of file NAME as if chroot(CHROOT) was
35 done first. A canonical name does not contain any `.', `..' components
36 nor any repeated path separators ('/') or symlinks. All path components
37 must exist and NAME must be absolute filename. The result is malloc'd.
38 The returned name includes the CHROOT prefix. */
40 char *
41 chroot_canon (const char *chroot, const char *name)
43 char *rpath, *dest, *extra_buf = NULL, *rpath_root;
44 const char *start, *end, *rpath_limit;
45 int num_links = 0;
46 size_t chroot_len = strlen (chroot);
48 if (chroot_len < 1)
50 __set_errno (EINVAL);
51 return NULL;
54 rpath = malloc (chroot_len + PATH_MAX);
55 rpath_limit = rpath + chroot_len + PATH_MAX;
57 rpath_root = (char *) mempcpy (rpath, chroot, chroot_len) - 1;
58 if (*rpath_root != '/')
59 *++rpath_root = '/';
60 dest = rpath_root + 1;
62 for (start = end = name; *start; start = end)
64 struct stat64 st;
65 int n;
67 /* Skip sequence of multiple path-separators. */
68 while (*start == '/')
69 ++start;
71 /* Find end of path component. */
72 for (end = start; *end && *end != '/'; ++end)
73 /* Nothing. */;
75 if (end - start == 0)
76 break;
77 else if (end - start == 1 && start[0] == '.')
78 /* nothing */;
79 else if (end - start == 2 && start[0] == '.' && start[1] == '.')
81 /* Back up to previous component, ignore if at root already. */
82 if (dest > rpath_root + 1)
83 while ((--dest)[-1] != '/');
85 else
87 size_t new_size;
89 if (dest[-1] != '/')
90 *dest++ = '/';
92 if (dest + (end - start) >= rpath_limit)
94 ptrdiff_t dest_offset = dest - rpath;
96 new_size = rpath_limit - rpath;
97 if (end - start + 1 > PATH_MAX)
98 new_size += end - start + 1;
99 else
100 new_size += PATH_MAX;
101 rpath = realloc (rpath, new_size);
102 rpath_limit = rpath + new_size;
103 if (rpath == NULL)
104 return NULL;
106 dest = rpath + dest_offset;
109 dest = mempcpy (dest, start, end - start);
110 *dest = '\0';
112 if (lstat64 (rpath, &st) < 0)
114 if (*end == '\0')
115 goto done;
116 goto error;
119 if (S_ISLNK (st.st_mode))
121 char *buf = alloca (PATH_MAX);
122 size_t len;
124 if (++num_links > MAXSYMLINKS)
126 __set_errno (ELOOP);
127 goto error;
130 n = readlink (rpath, buf, PATH_MAX);
131 if (n < 0)
133 if (*end == '\0')
134 goto done;
135 goto error;
137 buf[n] = '\0';
139 if (!extra_buf)
140 extra_buf = alloca (PATH_MAX);
142 len = strlen (end);
143 if ((long int) (n + len) >= PATH_MAX)
145 __set_errno (ENAMETOOLONG);
146 goto error;
149 /* Careful here, end may be a pointer into extra_buf... */
150 memmove (&extra_buf[n], end, len + 1);
151 name = end = memcpy (extra_buf, buf, n);
153 if (buf[0] == '/')
154 dest = rpath_root + 1; /* It's an absolute symlink */
155 else
156 /* Back up to previous component, ignore if at root already: */
157 if (dest > rpath_root + 1)
158 while ((--dest)[-1] != '/');
162 done:
163 if (dest > rpath_root + 1 && dest[-1] == '/')
164 --dest;
165 *dest = '\0';
167 return rpath;
169 error:
170 free (rpath);
171 return NULL;