diff options
-rw-r--r-- | ChangeLog.txt | 9 | ||||
-rwxr-xr-x | makewrappers | 4 | ||||
-rw-r--r-- | ports/darwin/portdefs.h | 3 | ||||
-rw-r--r-- | ports/linux/portdefs.h | 8 | ||||
-rw-r--r-- | ports/unix/guts/link.c | 32 | ||||
-rw-r--r-- | ports/unix/guts/linkat.c | 84 | ||||
-rw-r--r-- | ports/unix/wrapfuncs.in | 1 | ||||
-rw-r--r-- | pseudo.h | 8 |
8 files changed, 123 insertions, 26 deletions
diff --git a/ChangeLog.txt b/ChangeLog.txt index 906d5cc..3216c96 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,3 +1,12 @@ +2012-12-12: + * (seebs) Remove extra tab from the path alloc code in + makewrappers. (Which has no effect since I changed my mind about + the implementation which would have made it show up.) + * (seebs) linkat() implementation. as a side effect, clear up + the documentation and behavior of link(2) to reflect host + OS semantics: Linux will hardlink symlinks, Darwin will only + hardlink their targets. + 2012-08-09: * (seebs) base_stat should be real_stat64, not stat64 * (seebs) add stat64/lstat64/fstat64 wrappers to Linux (not diff --git a/makewrappers b/makewrappers index 18dbbc0..168fd81 100755 --- a/makewrappers +++ b/makewrappers @@ -335,7 +335,7 @@ class Function: alloc_paths.append( "%s = pseudo_root_path(__func__, __LINE__, %s%s, %s, %s);" % (path, prefix, self.dirfd, path, self.flags)) - return "\n\t\t\t".join(alloc_paths) + return "\n\t\t".join(alloc_paths) def free_paths(self): """free any allocated paths""" @@ -345,7 +345,7 @@ class Function: # has been overwritten. for path in self.paths_to_munge: free_paths.append("free((void *) %s);" % path) - return "\n\t\t\t".join(free_paths) + return "\n\t\t".join(free_paths) def real_predecl(self): if self.real_func: diff --git a/ports/darwin/portdefs.h b/ports/darwin/portdefs.h index f27e28d..900d98e 100644 --- a/ports/darwin/portdefs.h +++ b/ports/darwin/portdefs.h @@ -8,3 +8,6 @@ extern int pseudo_host_etc_passwd_fd; extern int pseudo_host_etc_group_fd; extern FILE *pseudo_host_etc_passwd_file; extern FILE *pseudo_host_etc_group_file; +/* Darwin ALWAYS follows symlinks for link(2) */ +#undef PSEUDO_LINK_SYMLINK_BEHAVIOR +#define PSEUDO_LINK_SYMLINK_BEHAVIOR AT_SYMLINK_FOLLOW diff --git a/ports/linux/portdefs.h b/ports/linux/portdefs.h index 7e6c2aa..20ad529 100644 --- a/ports/linux/portdefs.h +++ b/ports/linux/portdefs.h @@ -3,3 +3,11 @@ #define PSEUDO_STATBUF_64 1 #define PSEUDO_STATBUF struct stat64 #define PSEUDO_LINKPATH_SEPARATOR " " +/* Linux NEVER follows symlinks for link(2)... except on old kernels + * I don't care about. + */ +#undef PSEUDO_LINK_SYMLINK_BEHAVIOR +/* Note: 0, here, really means AT_SYMLINK_NOFOLLOW, but specifying that + * causes errors; you have to leave it empty or specify AT_SYMLINK_FOLLOW. + */ +#define PSEUDO_LINK_SYMLINK_BEHAVIOR 0 diff --git a/ports/unix/guts/link.c b/ports/unix/guts/link.c index 09551ac..cabcdf4 100644 --- a/ports/unix/guts/link.c +++ b/ports/unix/guts/link.c @@ -6,30 +6,14 @@ * wrap_link(const char *oldpath, const char *newpath) { * int rc = -1; */ - pseudo_msg_t *msg; - PSEUDO_STATBUF buf; - - rc = real_link(oldpath, newpath); - if (rc == 0) { - /* link(2) will not overwrite; if it succeeded, we know - * that there was no previous file with this name, so we - * shove it into the database. - */ - /* On linux, link(2) links to symlinks, not to the - * files they link to. This is contraPOSIX, but - * it's apparently useful. - */ - base_lstat(oldpath, &buf); - /* a link should copy the existing database entry, if - * there is one. OP_LINK is also used to insert unseen - * files, though, so it can't be implicit. - */ - msg = pseudo_client_op(OP_STAT, 0, -1, -1, oldpath, &buf); - if (msg) { - pseudo_stat_msg(&buf, msg); - } - pseudo_client_op(OP_LINK, 0, -1, -1, newpath, &buf); - } + /* since 2.6.18 or so, linkat supports AT_SYMLINK_FOLLOW, which + * provides the behavior link() has on most non-Linux systems, + * but the default is not to follow symlinks. Better yet, it + * does NOT support AT_SYMLINK_NOFOLLOW! So define this in + * your port's portdefs.h or hope the default works for you. + */ + rc = wrap_linkat(AT_FDCWD, oldpath, AT_FDCWD, newpath, + PSEUDO_LINK_SYMLINK_BEHAVIOR); /* return rc; * } diff --git a/ports/unix/guts/linkat.c b/ports/unix/guts/linkat.c new file mode 100644 index 0000000..3d0f99a --- /dev/null +++ b/ports/unix/guts/linkat.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2012 Wind River Systems; see + * guts/COPYRIGHT for information. + * + * int linkat(int olddirfd, const char *oldname, int newdirfd, const char *newname, int flags) + * int rc = -1; + */ + + int rc2, rflags, save_errno; + pseudo_msg_t *msg; + char *oldpath = NULL, *newpath = NULL; + PSEUDO_STATBUF buf; + + /* This is gratuitously complicated. On Linux 2.6.18 and later, + * flags may contain AT_SYMLINK_FOLLOW, which implies following + * symlinks; otherwise, linkat() will *not* follow symlinks. FreeBSD + * appears to use the same semantics. + * + * So on Darwin, always pass AT_SYMLINK_FOLLOW, because the + * alternative doesn't work. And never pass AT_SYMLINK_NOFOLLOW + * because that's not a valid flag to linkat(). + * + * So we need a flag for path resolution which is AT_SYMLINK_NOFOLLOW + * unless AT_SYMLINK_FOLLOW was specified, in which case it's 0. + */ + + rflags = (flags & AT_SYMLINK_FOLLOW) ? 0 : AT_SYMLINK_NOFOLLOW; + +#ifdef PSEUDO_NO_REAL_AT_FUNCTIONS + if (olddirfd != AT_FDCWD || newdirfd != AT_FDCWD) { + errno = ENOSYS; + return -1; + } + oldpath = pseudo_root_path(__func__, __LINE__, olddirfd, oldname, rflags); + newpath = pseudo_root_path(__func__, __LINE__, newdirfd, newname, AT_SYMLINK_NOFOLLOW); + rc = real_link(oldpath, newpath); + if (rc == -1) { + save_errno = errno; + free(oldpath); + free(newpath); + errno = save_errno; + return rc; + } +#else + rc = real_linkat(olddirfd, oldname, newdirfd, newname, flags); + if (rc == -1) { + return rc; + } + oldpath = pseudo_root_path(__func__, __LINE__, olddirfd, oldname, rflags); + newpath = pseudo_root_path(__func__, __LINE__, newdirfd, newname, AT_SYMLINK_NOFOLLOW); +#endif + + /* if we got this far, the link succeeded, and oldpath and newpath + * are the newly-allocated canonical paths. If OS, filesystem, or + * the flags value prevent hard linking to symlinks, the resolved + * path should be the target's path anyway, so lstat is safe here. + */ + /* find the target: */ + rc2 = base_lstat(oldpath, &buf); + if (rc2 == -1) { + pseudo_diag("Fatal: Tried to stat '%s' after linking it, but failed: %s.\n", + oldpath, strerror(errno)); + free(oldpath); + free(newpath); + errno = ENOENT; + return rc; + } + msg = pseudo_client_op(OP_STAT, 0, -1, -1, oldpath, &buf); + if (msg) { + pseudo_stat_msg(&buf, msg); + } + /* Long story short: I am pretty sure we still want OP_LINK even + * if the thing linked is a symlink. + */ + pseudo_client_op(OP_LINK, 0, -1, -1, newpath, &buf); + + save_errno = errno; + free(oldpath); + free(newpath); + errno = save_errno; + +/* return rc; + * } + */ diff --git a/ports/unix/wrapfuncs.in b/ports/unix/wrapfuncs.in index 775f38a..f6a2cd1 100644 --- a/ports/unix/wrapfuncs.in +++ b/ports/unix/wrapfuncs.in @@ -38,6 +38,7 @@ int chown(const char *path, uid_t owner, gid_t group); int fchmodat(int dirfd, const char *path, mode_t mode, int flags); int fchownat(int dirfd, const char *path, uid_t owner, gid_t group, int flags); int link(const char *oldpath, const char *newpath); /* flags=AT_SYMLINK_NOFOLLOW */ +int linkat(int olddirfd, const char *oldname, int newdirfd, const char *newname, int flags); int mkdir(const char *path, mode_t mode); /* flags=AT_SYMLINK_NOFOLLOW */ int mkdirat(int dirfd, const char *path, mode_t mode); /* flags=AT_SYMLINK_NOFOLLOW */ int mkfifo(const char *path, mode_t mode); /* flags=AT_SYMLINK_NOFOLLOW */ @@ -113,4 +113,12 @@ extern char *pseudo_version; #define O_LARGEFILE 0 #endif +/* Does link(2) let you create hard links to symlinks? Of course not. Who + * would ever do that? Well, Linux did, and possibly as a result, linkat() + * does by default too; if you are on a host with the historical Unix + * behavior of following symlinks to find the link target, you will want + * to set this to AT_SYMLINK_FOLLOW. Darwin does. + */ +#define PSEUDO_LINK_SYMLINK_BEHAVIOR 0 + #include "pseudo_ports.h" |