javar/jdk8-analysis/src/launcher/java_md_solinux.c
/*
 * Compute the name of the executable
 *
 * In order to re-exec securely we need the absolute path of the
 * executable. On Solaris getexecname(3c) may not return an absolute
 * path so we use dladdr to get the filename of the executable and
 * then use realpath to derive an absolute path. From Solaris 9
 * onwards the filename returned in DL_info structure from dladdr is
 * an absolute pathname so technically realpath isn't required.
 * On Linux we read the executable name from /proc/self/exe.
 * As a fallback, and for platforms other than Solaris and Linux,
 * we use FindExecName to compute the executable name.
 */
Compute the name of the executable
In order to re-exec securely we need the absolute path of the
executable. On Solaris getexecname(3c) may not return an absolute
path so we use dladdr to get the filename of the executable and
then use realpath to derive an absolute path. From Solaris 9
onwards the filename returned in DL_info structure from dladdr is
an absolute pathname so technically realpath isn't required.
On Linux we read the executable name from /proc/self/exe.
As a fallback, and for platforms other than Solaris and Linux,
we use FindExecName to compute the executable name.
Compute the name of the executable In order to re - exec securely we need the absolute path of the executable . On Solaris getexecname ( 3c ) may not return an absolute path so we use dladdr to get the filename of the executable and then use realpath to derive an absolute path . From Solaris 9 onwards the filename returned in DL_info structure from dladdr is an absolute pathname so technically realpath isn ' t required . On Linux we read the executable name from / proc / self / exe . As a fallback and for platforms other than Solaris and Linux we use FindExecName to compute the executable name .
const char*
SetExecname(char **argv)
{
    char* exec_path = NULL;
#if defined(__solaris__)
    {
        Dl_info dlinfo;
        int (*fptr)();
        fptr = (int (*)())dlsym(RTLD_DEFAULT, "main");
        if (fptr == NULL) {
            JLI_ReportErrorMessage(DLL_ERROR3, dlerror());
            return JNI_FALSE;
        }
        if (dladdr((void*)fptr, &dlinfo)) {
            char *resolved = (char*)JLI_MemAlloc(PATH_MAX+1);
            if (resolved != NULL) {
                exec_path = realpath(dlinfo.dli_fname, resolved);
                if (exec_path == NULL) {
                    JLI_MemFree(resolved);
                }
            }
        }
    }
#elif defined(__linux__)
    {
        const char* self = "/proc/self/exe";
        char buf[PATH_MAX+1];
        int len = readlink(self, buf, PATH_MAX);
        if (len >= 0) {
            buf[len] = '\0';            
            exec_path = JLI_StringDup(buf);
        }
    }
#else 
    {
    }
#endif
    if (exec_path == NULL) {
        exec_path = FindExecName(argv[0]);
    }
    execname = exec_path;
    return exec_path;
}
const char * SetExecname ( char * * argv ) { char * exec_path = NULL ; #if defined ( __solaris__ ) { Dl_info dlinfo ; int ( * fptr ) ( ) ; fptr = ( int ( * ) ( ) ) dlsym ( RTLD_DEFAULT , " " ) ; if ( fptr == NULL ) { JLI_ReportErrorMessage ( DLL_ERROR3 , dlerror ( ) ) ; return JNI_FALSE ; } if ( dladdr ( ( void * ) fptr , & dlinfo ) ) { char * resolved = ( char * ) JLI_MemAlloc ( PATH_MAX + 1 ) ; if ( resolved != NULL ) { exec_path = realpath ( dlinfo . dli_fname , resolved ) ; if ( exec_path == NULL ) { JLI_MemFree ( resolved ) ; } } } } #elif defined ( __linux__ ) { const char * self = " " ; char buf [ PATH_MAX + 1 ] ; int len = readlink ( self , buf , PATH_MAX ) ; if ( len >= 0 ) { buf [ len ] = ' \0 ' ; exec_path = JLI_StringDup ( buf ) ; } } #else { } #endif if ( exec_path == NULL ) { exec_path = FindExecName ( argv [ 0 ] ) ; } execname = exec_path ; return exec_path ; }
Compute the name of the executable
 In order to re-exec securely we need the absolute path of the
 executable.
Compute the name of the executable In order to re - exec securely we need the absolute path of the executable .
/* readlink(2) doesn't NUL terminate */ /* !__solaris__ && !__linux__ */ /* Not implemented */