summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--trunk/ChangeLog.cross15
-rw-r--r--trunk/src/cache.c8
-rw-r--r--trunk/src/canonicalize.c90
-rw-r--r--trunk/src/gather.c4
-rw-r--r--trunk/src/ld-libs.c2
-rw-r--r--trunk/src/prelink.h3
-rw-r--r--trunk/src/wrap-file.c278
7 files changed, 118 insertions, 282 deletions
diff --git a/trunk/ChangeLog.cross b/trunk/ChangeLog.cross
index 89d5d88..4dca3d0 100644
--- a/trunk/ChangeLog.cross
+++ b/trunk/ChangeLog.cross
@@ -2,6 +2,21 @@
* Integrate prelinker/cross-prelinking patches
+ 2007-07-09 Daniel Jacobowitz <dan@codesourcery.com>
+
+ Issue #1816
+ * src/wrap-file.c (chroot_canon): Delete.
+ (sysroot_file_name): Use canon_filename.
+ (unsysroot_file_name): Make global.
+ (wrap_canonicalize_file_name): Delete.
+ * src/ld-libs.c (load_dsos): Use prelink_canonicalize.
+ * src/canonicalize.c (canon_filename): Make global. Add chroot,
+ allow_last_link, allow_missing arguments. Handle links inside
+ a chroot. Handle canonicalizing the current directory. Remove
+ debugging output.
+ (prelink_canonicalize): Update. Use unsysroot_file_name.
+ * src/prelink.h (wrap_canonicalize_file_name): Delete prototype.
+
2007-03-02 Sandra Loosemore <sandra@codesourcery.com>
* doc/prelink.8: Copy edit. Add entry for --root option. Update
diff --git a/trunk/src/cache.c b/trunk/src/cache.c
index 2a72267..311dfa3 100644
--- a/trunk/src/cache.c
+++ b/trunk/src/cache.c
@@ -112,7 +112,7 @@ prelink_find_entry (const char *filename, const struct stat64 *stp,
if (! stp)
{
- canon_filename = wrap_prelink_canonicalize (filename, &st);
+ canon_filename = prelink_canonicalize (filename, &st);
if (canon_filename == NULL && wrap_stat64 (filename, &st) < 0)
{
error (0, errno, "Could not stat %s", filename);
@@ -142,7 +142,7 @@ prelink_find_entry (const char *filename, const struct stat64 *stp,
{
ent = (struct prelink_entry *) *devino_slot;
if (canon_filename == NULL)
- canon_filename = wrap_prelink_canonicalize (filename, NULL);
+ canon_filename = prelink_canonicalize (filename, NULL);
if (canon_filename == NULL)
{
error (0, 0, "Could not canonicalize filename %s", filename);
@@ -189,7 +189,7 @@ prelink_find_entry (const char *filename, const struct stat64 *stp,
if (canon_filename != NULL)
ent->canon_filename = canon_filename;
else
- ent->canon_filename = wrap_prelink_canonicalize (filename, NULL);
+ ent->canon_filename = prelink_canonicalize (filename, NULL);
if (ent->canon_filename == NULL)
{
error (0, 0, "Could not canonicalize filename %s", filename);
@@ -248,7 +248,7 @@ prelink_load_entry (const char *filename)
if (*filename_slot != NULL)
return (struct prelink_entry *) *filename_slot;
- canon_filename = wrap_prelink_canonicalize (filename, &st);
+ canon_filename = prelink_canonicalize (filename, &st);
if (canon_filename == NULL)
goto error_out2;
if (strcmp (canon_filename, filename) != 0)
diff --git a/trunk/src/canonicalize.c b/trunk/src/canonicalize.c
index c51b277..717e991 100644
--- a/trunk/src/canonicalize.c
+++ b/trunk/src/canonicalize.c
@@ -73,14 +73,17 @@ dirname_eq (const void *p, const void *q)
separators ('/') or symlinks. All path components must exist.
The result is malloc'd. */
-static char *
-canon_filename (const char *name, int nested, struct stat64 *stp)
+char *
+canon_filename (const char *name, int nested, struct stat64 *stp,
+ const char *chroot, int allow_last_link,
+ int allow_missing)
{
- char *rpath, *dest, *extra_buf = NULL;
+ char *rpath, *dest, *extra_buf = NULL, *rpath_root;
const char *start, *end, *rpath_limit;
long int path_max;
int num_links = 0;
int stp_initialized = 0;
+ int chroot_len;
if (name == NULL)
{
@@ -93,6 +96,9 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
errno = ENOENT;
return NULL;
}
+ chroot_len = strlen (chroot);
+ if (chroot_len > 0 && chroot[chroot_len - 1] == '/')
+ chroot_len--;
#ifdef PATH_MAX
path_max = PATH_MAX;
@@ -102,7 +108,7 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
path_max = 1024;
#endif
- rpath = malloc (path_max);
+ rpath = malloc (path_max + chroot_len + 1);
if (rpath == NULL)
return NULL;
rpath_limit = rpath + path_max;
@@ -114,12 +120,32 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
rpath[0] = '\0';
goto error;
}
- dest = strchr (rpath, '\0');
+ if (chroot_len > 0)
+ {
+ struct stat64 st;
+ char *cwd = canon_filename (rpath, 1, &st, chroot, 0, 0);
+ if (cwd == NULL)
+ goto error;
+ if (memcmp (cwd, chroot, chroot_len) != 0)
+ goto error;
+ strcpy (rpath, cwd);
+ free (cwd);
+ rpath_root = rpath + chroot_len;
+ }
+ else
+ rpath_root = rpath;
+
+ dest = strchr (rpath_root, '\0');
}
else
{
- rpath[0] = '/';
- dest = rpath + 1;
+ if (chroot_len > 0)
+ rpath_root = (char *) mempcpy (rpath, chroot, chroot_len);
+ else
+ rpath_root = rpath;
+
+ rpath_root[0] = '/';
+ dest = rpath_root + 1;
if (!nested)
{
@@ -152,7 +178,8 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
ep->dirname_len = e.dirname_len;
memcpy (dirname, name, ep->dirname_len);
dirname[ep->dirname_len] = '\0';
- ep->canon_dirname = canon_filename (ep->dirname, 1, &st);
+ ep->canon_dirname = canon_filename (ep->dirname, 1, &st,
+ chroot, 0, 0);
if (ep->canon_dirname == NULL || !S_ISDIR (st.st_mode))
free (ep);
else
@@ -169,10 +196,11 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
if (rpath + ep->canon_dirname_len + 1 >= rpath_limit)
{
- size_t new_size;
+ size_t new_size, root_size;
char *new_rpath;
new_size = rpath_limit - rpath;
+ root_size = rpath_root - rpath;
if (ep->canon_dirname_len + 1 > path_max)
new_size += ep->canon_dirname_len + 1;
else
@@ -182,6 +210,7 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
goto error;
rpath = new_rpath;
rpath_limit = rpath + new_size;
+ rpath_root = rpath + root_size;
}
dest = mempcpy (rpath, ep->canon_dirname, ep->canon_dirname_len);
*dest = '\0';
@@ -209,7 +238,7 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
else if (end - start == 2 && start[0] == '.' && start[1] == '.')
{
/* Back up to previous component, ignore if at root already. */
- if (dest > rpath + 1)
+ if (dest > rpath_root + 1)
while ((--dest)[-1] != '/');
stp_initialized = 0;
}
@@ -223,6 +252,7 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
if (dest + (end - start) >= rpath_limit)
{
ptrdiff_t dest_offset = dest - rpath;
+ size_t root_size = rpath_root - rpath;
char *new_rpath;
new_size = rpath_limit - rpath;
@@ -235,6 +265,7 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
goto error;
rpath = new_rpath;
rpath_limit = rpath + new_size;
+ rpath_root = rpath + root_size;
dest = rpath + dest_offset;
}
@@ -242,8 +273,15 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
dest = mempcpy (dest, start, end - start);
*dest = '\0';
- if (wrap_lstat64 (rpath, stp) < 0)
- goto error;
+ if (allow_last_link && *end == '\0')
+ break;
+
+ if (lstat64 (rpath, stp) < 0)
+ {
+ if (allow_missing && *end == '\0')
+ break;
+ goto error;
+ }
stp_initialized = 1;
@@ -258,9 +296,13 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
goto error;
}
- n = wrap_readlink (rpath, buf, path_max);
+ n = readlink (rpath, buf, path_max);
if (n < 0)
- goto error;
+ {
+ if (allow_missing && *end == '\0')
+ break;
+ goto error;
+ }
buf[n] = '\0';
if (!extra_buf)
@@ -278,10 +320,10 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
name = end = memcpy (extra_buf, buf, n);
if (buf[0] == '/')
- dest = rpath + 1; /* It's an absolute symlink */
+ dest = rpath_root + 1; /* It's an absolute symlink */
else
/* Back up to previous component, ignore if at root already: */
- if (dest > rpath + 1)
+ if (dest > rpath_root + 1)
while ((--dest)[-1] != '/');
}
else if (!S_ISDIR (stp->st_mode) && *end != '\0')
@@ -295,7 +337,8 @@ canon_filename (const char *name, int nested, struct stat64 *stp)
--dest;
*dest = '\0';
- if (!stp_initialized && wrap_lstat64 (rpath, stp) < 0)
+ if (!stp_initialized && !allow_missing && !allow_last_link
+ && lstat64 (rpath, stp) < 0)
goto error;
if (dest + 1 - rpath <= (rpath_limit - rpath) / 2)
@@ -312,9 +355,20 @@ error:
return NULL;
}
+char *unsysroot_file_name (const char *name);
+
char *
prelink_canonicalize (const char *name, struct stat64 *stp)
{
struct stat64 st;
- return canon_filename (name, 0, stp ? stp : &st);
+ char *canon, *final;
+
+ canon = canon_filename (name, 0, stp ? stp : &st,
+ sysroot ? sysroot : "", 0, 0);
+ if (canon == NULL)
+ return NULL;
+ final = unsysroot_file_name (canon);
+ if (final != canon)
+ free (canon);
+ return final;
}
diff --git a/trunk/src/gather.c b/trunk/src/gather.c
index d31f472..a8caefc 100644
--- a/trunk/src/gather.c
+++ b/trunk/src/gather.c
@@ -626,7 +626,7 @@ add_dir_to_dirlist (const char *name, dev_t dev, int flags)
struct prelink_dir *dir;
size_t len;
- canon_name = wrap_prelink_canonicalize (name, NULL);
+ canon_name = prelink_canonicalize (name, NULL);
if (canon_name == NULL)
{
if (! all && implicit)
@@ -1294,7 +1294,7 @@ add_to_blacklist (const char *name, int deref, int onefs)
return 0;
}
- canon_name = wrap_prelink_canonicalize (name, NULL);
+ canon_name = prelink_canonicalize (name, NULL);
if (canon_name == NULL)
{
if (implicit)
diff --git a/trunk/src/ld-libs.c b/trunk/src/ld-libs.c
index b81f4ee..df2bfc6 100644
--- a/trunk/src/ld-libs.c
+++ b/trunk/src/ld-libs.c
@@ -583,7 +583,7 @@ load_dsos (DSO *dso, int host_paths)
/* See if the filename we found has already been
opened (possibly under a different SONAME via
some symlink). */
- new_canon_name = wrap_prelink_canonicalize (new_name, NULL);
+ new_canon_name = prelink_canonicalize (new_name, NULL);
if (new_canon_name == NULL)
new_canon_name = strdup (new_name);
new_dso_ent = in_dso_list (dso_list, soname, new_canon_name);
diff --git a/trunk/src/prelink.h b/trunk/src/prelink.h
index 4392bf7..e491c8f 100644
--- a/trunk/src/prelink.h
+++ b/trunk/src/prelink.h
@@ -509,7 +509,6 @@ extern GElf_Addr mmap_reg_start, mmap_reg_end;
extern const char *sysroot;
-char *wrap_prelink_canonicalize (const char *name, struct stat64 *stp);
int wrap_readlink (const char *path, char *buf, int len);
int wrap_lstat64 (const char *file, struct stat64 *buf);
int wrap_stat64 (const char *file, struct stat64 *buf);
@@ -522,7 +521,7 @@ int wrap_nftw64 (const char *dir, __nftw64_func_t func,
int wrap_utime (const char *file, struct utimbuf *file_times);
int wrap_mkstemp (char *filename);
int wrap_unlink (const char *filename);
-char *sysroot_file_name (const char *name, int allow_last_link, struct stat64 *stp);
+char *sysroot_file_name (const char *name, int allow_last_link);
extern const char *prelink_rtld;
diff --git a/trunk/src/wrap-file.c b/trunk/src/wrap-file.c
index db2290f..20f4a13 100644
--- a/trunk/src/wrap-file.c
+++ b/trunk/src/wrap-file.c
@@ -1,11 +1,6 @@
/* Copyright (C) 2003 MontaVista Software, Inc.
Written by Daniel Jacobowitz <drow@mvista.com>, 2003.
- The chroot_canon function is copied from the GNU C Library,
- elf/chroot-canon.c, also licensed under the GPL:
- Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
- [and then further modified.]
-
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
@@ -44,231 +39,33 @@
#define MAXSYMLINKS 20
#endif
-/* Return the canonical absolute name of file NAME as if chroot(CHROOT) was
- done first. A canonical name does not contain any `.', `..' components
- nor any repeated path separators ('/') or symlinks. All path components
- must exist and NAME must be absolute filename. The result is malloc'd.
- The returned name includes the CHROOT prefix.
-
- If ALLOW_LAST_LINK, then symlinks in the last component won't be
- resolved. */
-
-static char *
-chroot_canon_filename (const char * chroot, const char *name, int allow_last_link, struct stat64 *stp)
-{
- char *rpath, *dest, *extra_buf = NULL;
- char *rpath_root;
- const char *start, *end, *rpath_limit;
- long int path_max;
- int num_links = 0;
- int stp_initialized = 0;
- size_t chroot_len = 0;
-
- if (name == NULL)
- {
- errno = EINVAL;
- return NULL;
- }
-
- if (name[0] == '\0')
- {
- errno = ENOENT;
- return NULL;
- }
-
- if (chroot == NULL)
- {
- errno = EINVAL;
- return NULL;
- }
-
- chroot_len = strlen (chroot);
-
-#ifdef PATH_MAX
- path_max = PATH_MAX;
-#else
- path_max = pathconf (name, _PC_PATH_MAX);
- if (path_max <= 0)
- path_max = 1024;
-#endif
-
- rpath = malloc (chroot_len + path_max);
- if (rpath == NULL)
- return NULL;
- rpath_limit = rpath + chroot_len + path_max;
-
- rpath_root = (char *) mempcpy (rpath, chroot, chroot_len) - 1;
- if (*rpath_root != '/')
- *++rpath_root = '/';
- dest = rpath_root + 1;
-
- for (start = end = name; *start; start = end)
- {
- int n;
-
- /* Skip sequence of multiple path-separators. */
- while (*start == '/')
- ++start;
-
- /* Find end of path component. */
- for (end = start; *end && *end != '/'; ++end)
- /* Nothing. */;
-
- if (end - start == 0)
- break;
- else if (end - start == 1 && start[0] == '.')
- /* nothing */;
- else if (end - start == 2 && start[0] == '.' && start[1] == '.')
- {
- /* Back up to previous component, ignore if at root already. */
- if (dest > rpath_root + 1)
- while ((--dest)[-1] != '/');
- stp_initialized = 0;
- }
- else
- {
- size_t new_size;
-
- if (dest[-1] != '/')
- *dest++ = '/';
-
- if (dest + (end - start) >= rpath_limit)
- {
- ptrdiff_t dest_offset = dest - rpath;
- char *new_rpath;
-
- new_size = rpath_limit - rpath;
- if (end - start + 1 > path_max)
- new_size += end - start + 1;
- else
- new_size += path_max;
- new_rpath = (char *) realloc (rpath, new_size);
- if (new_rpath == NULL)
- goto error;
- rpath = new_rpath;
- rpath_limit = rpath + new_size;
-
- dest = rpath + dest_offset;
- }
-
- dest = mempcpy (dest, start, end - start);
- *dest = '\0';
-
- if (lstat64 (rpath, stp) < 0)
- goto error;
-
- stp_initialized = 1;
-
- if (allow_last_link && *end == '\0')
- goto done;
-
- if (S_ISLNK (stp->st_mode))
- {
- char *buf = alloca (path_max);
- size_t len;
-
- if (++num_links > MAXSYMLINKS)
- {
- errno = ELOOP;
- goto error;
- }
-
- n = readlink (rpath, buf, path_max);
- if (n < 0)
- goto error;
- buf[n] = '\0';
-
- if (!extra_buf)
- extra_buf = alloca (path_max);
-
- len = strlen (end);
- if ((long int) (n + len) >= path_max)
- {
- errno = ENAMETOOLONG;
- goto error;
- }
-
- /* Careful here, end may be a pointer into extra_buf... */
- memmove (&extra_buf[n], end, len + 1);
- name = end = memcpy (extra_buf, buf, n);
-
- if (buf[0] == '/')
- dest = rpath_root + 1; /* It's an absolute symlink */
- else
- /* Back up to previous component, ignore if at root already: */
- if (dest > rpath_root + 1)
- while ((--dest)[-1] != '/');
- }
- else if (!S_ISDIR (stp->st_mode) && *end != '\0')
- {
- errno = ENOTDIR;
- goto error;
- }
- }
- }
-done:
- if (dest > rpath_root + 1 && dest[-1] == '/')
- --dest;
- *dest = '\0';
-
- if (!stp_initialized && lstat64 (rpath, stp) < 0)
- goto error;
-
- if (dest + 1 - rpath <= (rpath_limit - rpath) / 2)
- {
- char *new_rpath = realloc (rpath, dest + 1 - rpath);
-
- if (new_rpath != NULL)
- return new_rpath;
- }
- return rpath;
-
-error:
- free (rpath);
- return NULL;
-}
-
+extern char *canon_filename (const char *name, int nested, struct stat64 *stp,
+ const char *chroot, int allow_last_link,
+ int allow_missing);
const char *sysroot;
char *
-sysroot_file_name (const char *name, int allow_last_link, struct stat64 *stp)
+sysroot_file_name (const char *name, int allow_last_link)
{
- char *ret;
struct stat64 st;
+ char *ret;
- if (sysroot == NULL || name == NULL)
+ if (sysroot == NULL)
return (char *) name;
- if (name[0] != '/')
- {
- char *tmpname = malloc (strlen (name) + 2);
- strcpy (tmpname, "/");
- strcat (tmpname, name);
- ret = chroot_canon_filename (sysroot, tmpname, allow_last_link, stp ? stp : &st);
- free (tmpname);
- }
- else
- ret = chroot_canon_filename (sysroot, name, allow_last_link, stp ? stp : &st);
+ ret = canon_filename (name, 0, &st, sysroot, allow_last_link, 1);
if (ret == NULL)
- {
- char *ret_root;
-
- ret = malloc(strlen(sysroot) + strlen(name) + 1);
- ret_root = mempcpy(ret, sysroot, strlen(sysroot));
- ret_root = mempcpy(ret_root, name, strlen(name));
- *ret_root='\0';
- }
+ /* That will have set errno. */
+ return NULL;
+
return ret;
}
-static char *
+char *
unsysroot_file_name (const char *name)
{
- if (name == NULL)
- return (char *)name;
-
if (sysroot)
{
int sysroot_len = strlen (sysroot);
@@ -283,35 +80,6 @@ unsysroot_file_name (const char *name)
return (char *)name;
}
-char *
-wrap_prelink_canonicalize (const char *name, struct stat64 *stp)
-{
- if (sysroot)
- {
- struct stat64 st;
- char *tmpname;
- char *ret;
-
- /* Use chroot_canon_filename because we want a NULL return if it doesn't exist! */
- tmpname = chroot_canon_filename (sysroot, name, 0, stp ? stp : &st);
-
- if (tmpname == NULL)
- return NULL;
-
- ret = unsysroot_file_name (tmpname);
-
- if (ret == tmpname)
- ret = strdup (ret);
-
- if (tmpname != name)
- free (tmpname);
-
- return ret;
- }
- else
- return prelink_canonicalize(name, stp);
-}
-
static int
wrap_stat_body (const char *file, struct stat64 *buf, int lstat)
{
@@ -320,7 +88,7 @@ wrap_stat_body (const char *file, struct stat64 *buf, int lstat)
int ret;
int len;
- tmpname = sysroot_file_name (file, lstat, NULL);
+ tmpname = sysroot_file_name (file, lstat);
if (tmpname == NULL)
return -1;
@@ -359,14 +127,14 @@ wrap_stat64 (const char *file, struct stat64 *buf)
int
wrap_rename (const char *old, const char *new)
{
- char *tmpold = sysroot_file_name (old, 1, NULL);
+ char *tmpold = sysroot_file_name (old, 1);
char *tmpnew;
int ret;
if (tmpold == NULL)
return -1;
- tmpnew = sysroot_file_name (new, 1, NULL);
+ tmpnew = sysroot_file_name (new, 1);
if (tmpnew == NULL)
return -1;
@@ -382,7 +150,7 @@ wrap_rename (const char *old, const char *new)
int
wrap_open (const char *name, int mode, ...)
{
- char *tmpname = sysroot_file_name (name, 0, NULL);
+ char *tmpname = sysroot_file_name (name, 0);
int ret;
if (tmpname == NULL)
@@ -408,7 +176,7 @@ wrap_open (const char *name, int mode, ...)
int
wrap_access (const char *name, int mode)
{
- char *tmpname = sysroot_file_name (name, 0, NULL);
+ char *tmpname = sysroot_file_name (name, 0);
int ret;
if (tmpname == NULL)
@@ -424,14 +192,14 @@ wrap_access (const char *name, int mode)
int
wrap_link (const char *old, const char *new)
{
- char *tmpold = sysroot_file_name (old, 1, NULL);
+ char *tmpold = sysroot_file_name (old, 1);
char *tmpnew;
int ret;
if (tmpold == NULL)
return -1;
- tmpnew = sysroot_file_name (new, 1, NULL);
+ tmpnew = sysroot_file_name (new, 1);
if (tmpnew == NULL)
return -1;
@@ -466,7 +234,7 @@ int
wrap_nftw64 (const char *dir, __nftw64_func_t func,
int descriptors, int flag)
{
- char *tmpdir = sysroot_file_name (dir, 1, NULL);
+ char *tmpdir = sysroot_file_name (dir, 1);
int ret;
if (tmpdir == NULL)
@@ -483,7 +251,7 @@ wrap_nftw64 (const char *dir, __nftw64_func_t func,
int
wrap_utime (const char *file, struct utimbuf *file_times)
{
- char *tmpname = sysroot_file_name (file, 0, NULL);
+ char *tmpname = sysroot_file_name (file, 0);
int ret;
if (tmpname == NULL)
@@ -499,7 +267,7 @@ wrap_utime (const char *file, struct utimbuf *file_times)
int
wrap_mkstemp (char *filename)
{
- char *tmpname = sysroot_file_name (filename, 1, NULL);
+ char *tmpname = sysroot_file_name (filename, 1);
int ret;
if (tmpname == NULL)
@@ -518,7 +286,7 @@ wrap_mkstemp (char *filename)
int
wrap_unlink (const char *filename)
{
- char *tmpname = sysroot_file_name (filename, 1, NULL);
+ char *tmpname = sysroot_file_name (filename, 1);
int ret;
if (tmpname == NULL)
@@ -534,7 +302,7 @@ wrap_unlink (const char *filename)
int
wrap_readlink (const char *path, char *buf, int len)
{
- char *tmpname = sysroot_file_name (path, 1, NULL);
+ char *tmpname = sysroot_file_name (path, 1);
int ret;
if (tmpname == NULL)