FreeBSD/libexec/rtld-elf/rtld.c
/*
 * Finds the library with the given name using the directory descriptors
 * listed in the LD_LIBRARY_PATH_FDS environment variable.
 *
 * Returns a freshly-opened close-on-exec file descriptor for the library,
 * or -1 if the library cannot be found.
 */
Finds the library with the given name using the directory descriptors
listed in the LD_LIBRARY_PATH_FDS environment variable.

Returns a freshly-opened close-on-exec file descriptor for the library,
or -1 if the library cannot be found.
Finds the library with the given name using the directory descriptors listed in the LD_LIBRARY_PATH_FDS environment variable . Returns a freshly - opened close - on - exec file descriptor for the library or - 1 if the library cannot be found .
static char *
search_library_pathfds(const char *name, const char *path, int *fdp)
{
	char *envcopy, *fdstr, *found, *last_token;
	size_t len;
	int dirfd, fd;
	dbg("%s('%s', '%s', fdp)", __func__, name, path);
	if (!trust)
		return (NULL);
	if (path == NULL)
		return (NULL);
	if (name[0] == '/') {
		dbg("Absolute path (%s) passed to %s", name, __func__);
		return (NULL);
	}
	found = NULL;
	envcopy = xstrdup(path);
	for (fdstr = strtok_r(envcopy, ":", &last_token); fdstr != NULL;
	    fdstr = strtok_r(NULL, ":", &last_token)) {
		dirfd = parse_libdir(fdstr);
		if (dirfd < 0)
			break;
		fd = __sys_openat(dirfd, name, O_RDONLY | O_CLOEXEC | O_VERIFY);
		if (fd >= 0) {
			*fdp = fd;
			len = strlen(fdstr) + strlen(name) + 3;
			found = xmalloc(len);
			if (rtld_snprintf(found, len, "#%d/%s", dirfd, name) < 0) {
				_rtld_error("error generating '%d/%s'",
				    dirfd, name);
				rtld_die();
			}
			dbg("open('%s') => %d", found, fd);
			break;
		}
	}
	free(envcopy);
	return (found);
}
static char * search_library_pathfds ( const char * name , const char * path , int * fdp ) { char * envcopy , * fdstr , * found , * last_token ; size_t len ; int dirfd , fd ; dbg ( " " , __func__ , name , path ) ; if ( ! trust ) return ( NULL ) ; if ( path == NULL ) return ( NULL ) ; if ( name [ 0 ] == ' ' ) { dbg ( " " , name , __func__ ) ; return ( NULL ) ; } found = NULL ; envcopy = xstrdup ( path ) ; for ( fdstr = strtok_r ( envcopy , " " , & last_token ) ; fdstr != NULL ; fdstr = strtok_r ( NULL , " " , & last_token ) ) { dirfd = parse_libdir ( fdstr ) ; if ( dirfd < 0 ) break ; fd = __sys_openat ( dirfd , name , O_RDONLY | O_CLOEXEC | O_VERIFY ) ; if ( fd >= 0 ) { * fdp = fd ; len = strlen ( fdstr ) + strlen ( name ) + 3 ; found = xmalloc ( len ) ; if ( rtld_snprintf ( found , len , " " , dirfd , name ) < 0 ) { _rtld_error ( " " , dirfd , name ) ; rtld_die ( ) ; } dbg ( " " , found , fd ) ; break ; } } free ( envcopy ) ; return ( found ) ; }
Finds the library with the given name using the directory descriptors
 listed in the LD_LIBRARY_PATH_FDS environment variable.
Finds the library with the given name using the directory descriptors listed in the LD_LIBRARY_PATH_FDS environment variable .
/* Don't load from user-specified libdirs into setuid binaries. */ /* We can't do anything if LD_LIBRARY_PATH_FDS isn't set. */ /* LD_LIBRARY_PATH_FDS only works with relative paths. */ /*
	 * Use strtok_r() to walk the FD:FD:FD list.  This requires a local
	 * copy of the path, as strtok_r rewrites separator tokens
	 * with '\0'.
	 */