commit:     84ca9ab04c49503ff2ef4346c45da63597ac204a
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Tue Jun 10 01:44:33 2014 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Tue Jun 10 01:49:08 2014 +0000
URL:        
http://git.overlays.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=84ca9ab0

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.

---
 misc/install-xattr/install-xattr.c | 17 +++++++++--------
 1 file 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:

Reply via email to