diff options
author | Anthony G. Basile <blueness@gentoo.org> | 2014-06-09 21:44:33 -0400 |
---|---|---|
committer | Anthony G. Basile <blueness@gentoo.org> | 2014-06-09 21:49:08 -0400 |
commit | 84ca9ab04c49503ff2ef4346c45da63597ac204a (patch) | |
tree | be46ea2eedcb6bf9536df9a3cec01b91f681286c | |
parent | misc/install-xattr: refine the behavior of which() (diff) | |
download | elfix-84ca9ab04c49503ff2ef4346c45da63597ac204a.tar.gz elfix-84ca9ab04c49503ff2ef4346c45da63597ac204a.tar.bz2 elfix-84ca9ab04c49503ff2ef4346c45da63597ac204a.zip |
misc/install-xattr: /proc/self/exe for canonical path to self
Evaluating realpath(argv[0], NULL) when argv[0] is in the $PATH just
returns argv[0] and not the full canonical path. Using /proc/self/exe
is more reliable.
-rw-r--r-- | misc/install-xattr/install-xattr.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/misc/install-xattr/install-xattr.c b/misc/install-xattr/install-xattr.c index 7dc248b..7b73cc3 100644 --- a/misc/install-xattr/install-xattr.c +++ b/misc/install-xattr/install-xattr.c @@ -162,9 +162,8 @@ copyxattr(const char *source, const char *target) static char * -which(const char *myfile) +which(char *mypath) { - char *mypath = realpath(myfile, NULL); /* argv[0]'s canonical path */ char *path, *env_path = getenv("PATH"); /* full $PATH string */ char *portage_bin_path = getenv("PORTAGE_BIN_PATH"); /* PORTAGE BIN $PATHs to skip */ @@ -206,7 +205,6 @@ which(const char *myfile) */ if (stat(canpath, &s) == 0) if (S_ISREG(s.st_mode)) { - free(mypath); free(path); return canpath; } @@ -238,11 +236,13 @@ main(int argc, char* argv[]) int first, last; /* argv indices of the first file/directory and last */ char *target; /* the target file or directory */ char *path; /* path to the target file */ - char *install; /* path to the system install */ + + char *mypath = realpath("/proc/self/exe", NULL); /* path to argv[0] */ + char *install; /* path to the system install */ struct stat s; /* test if a file is a regular file or a directory */ - char *portage_xattr_exclude; /* strings of excluded xattr names from $PORTAGE_XATTR_EXCLUDE */ + char *portage_xattr_exclude; /* strings of excluded xattr names from $PORTAGE_XATTR_EXCLUDE */ portage_xattr_exclude = getenv("PORTAGE_XATTR_EXCLUDE"); if (portage_xattr_exclude == NULL) @@ -316,9 +316,10 @@ main(int argc, char* argv[]) err(1, "fork() failed"); case 0: - install = which(argv[0]); - argv[0] = install; /* so coreutils' lib/program.c behaves */ - execv(install, argv); /* The kernel will free(install). */ + install = which(mypath); /* find system install avoiding mypath! */ + free(mypath); + argv[0] = install; /* so coreutils' lib/program.c behaves */ + execv(install, argv); /* The kernel will free(install). */ err(1, "execv() failed"); default: |