Back to index

glibc  2.9
dl-origin.c
Go to the documentation of this file.
00001 /* Find path of executable.
00002    Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <assert.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 #include <sys/param.h>
00026 #include <ldsodefs.h>
00027 #include <sysdep.h>
00028 
00029 #include <dl-dst.h>
00030 
00031 /* On Linux >= 2.1 systems which have the dcache implementation we can get
00032    the path of the application from the /proc/self/exe symlink.  Try this
00033    first and fall back on the generic method if necessary.  */
00034 
00035 const char *
00036 _dl_get_origin (void)
00037 {
00038   char linkval[PATH_MAX];
00039   char *result;
00040   int len;
00041   INTERNAL_SYSCALL_DECL (err);
00042 
00043   len = INTERNAL_SYSCALL (readlink, err, 3, "/proc/self/exe", linkval,
00044                        sizeof (linkval));
00045   if (! INTERNAL_SYSCALL_ERROR_P (len, err) && len > 0 && linkval[0] != '[')
00046     {
00047       /* We can use this value.  */
00048       assert (linkval[0] == '/');
00049       while (len > 1 && linkval[len - 1] != '/')
00050        --len;
00051       result = (char *) malloc (len + 1);
00052       if (result == NULL)
00053        result = (char *) -1;
00054       else if (len == 1)
00055        memcpy (result, "/", 2);
00056       else
00057        *((char *) __mempcpy (result, linkval, len - 1)) = '\0';
00058     }
00059   else
00060     {
00061       result = (char *) -1;
00062       /* We use the environment variable LD_ORIGIN_PATH.  If it is set make
00063         a copy and strip out trailing slashes.  */
00064       if (GLRO(dl_origin_path) != NULL)
00065        {
00066          size_t len = strlen (GLRO(dl_origin_path));
00067          result = (char *) malloc (len + 1);
00068          if (result == NULL)
00069            result = (char *) -1;
00070          else
00071            {
00072              char *cp = __mempcpy (result, GLRO(dl_origin_path), len);
00073              while (cp > result + 1 && cp[-1] == '/')
00074               --cp;
00075              *cp = '\0';
00076            }
00077        }
00078     }
00079 
00080   return result;
00081 }