2004-01-09 11:03:27 +03:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
2007-07-01 21:59:46 +04:00
|
|
|
* Copyright (c) 2004-2007 The University of Tennessee and The University
|
2005-11-05 22:57:48 +03:00
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2012-01-31 20:45:50 +04:00
|
|
|
* Copyright (c) 2009-2012 Cisco Systems, Inc. All rights reserved.
|
2010-08-30 21:46:47 +04:00
|
|
|
* Copyright (c) 2010 IBM Corporation. All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-01-09 11:03:27 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2004-01-09 11:03:27 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-02-11 02:18:29 +03:00
|
|
|
#include <errno.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-01-09 11:03:27 +03:00
|
|
|
#include <unistd.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2007-07-25 07:49:31 +04:00
|
|
|
#ifdef HAVE_SHLWAPI_H
|
2007-07-02 22:11:14 +04:00
|
|
|
#include <shlwapi.h>
|
2007-07-25 07:49:31 +04:00
|
|
|
#endif
|
2010-02-11 02:18:29 +03:00
|
|
|
#ifdef HAVE_SYS_MOUNT_H
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
2008-08-20 19:30:25 +04:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2010-02-11 02:18:29 +03:00
|
|
|
#ifdef HAVE_SYS_VFS_H
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STATFS_H
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_MOUNT_H
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif
|
2004-01-09 11:03:27 +03:00
|
|
|
|
2010-02-11 02:18:29 +03:00
|
|
|
#include "opal/util/output.h"
|
2005-07-04 05:59:52 +04:00
|
|
|
#include "opal/util/path.h"
|
2006-08-23 03:25:13 +04:00
|
|
|
#include "opal/util/os_path.h"
|
2005-07-04 04:13:44 +04:00
|
|
|
#include "opal/util/argv.h"
|
2004-01-09 11:03:27 +03:00
|
|
|
|
2005-02-09 20:13:54 +03:00
|
|
|
static void path_env_load(char *path, int *pargc, char ***pargv);
|
|
|
|
static char *list_env_get(char *var, char **list);
|
|
|
|
|
2007-01-25 03:17:02 +03:00
|
|
|
bool opal_path_is_absolute( const char *path )
|
|
|
|
{
|
|
|
|
#if defined(__WINDOWS__)
|
2007-07-01 21:53:19 +04:00
|
|
|
/* On Windows an absolute path always start with [a-z]:\ or with \\ */
|
|
|
|
if( (isalpha(path[0]) && (':' == path[1])) ||
|
2007-07-01 21:59:46 +04:00
|
|
|
('\\' == path[0]) && ('\\' == path[1]) ) return true;
|
2007-01-25 03:17:02 +03:00
|
|
|
#else
|
|
|
|
if( OPAL_PATH_SEP[0] == *path ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif /* defined(__WINDOWS__) */
|
2007-07-01 21:53:19 +04:00
|
|
|
return false;
|
2007-01-25 03:17:02 +03:00
|
|
|
}
|
|
|
|
|
2004-01-09 11:03:27 +03:00
|
|
|
/**
|
|
|
|
* Locates a file with certain permissions
|
|
|
|
*/
|
2005-07-04 05:59:52 +04:00
|
|
|
char *opal_path_find(char *fname, char **pathv, int mode, char **envv)
|
2004-01-09 11:03:27 +03:00
|
|
|
{
|
2005-02-09 20:13:54 +03:00
|
|
|
char *fullpath;
|
|
|
|
char *delimit;
|
|
|
|
char *env;
|
|
|
|
char *pfix;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* If absolute path is given, return it without searching. */
|
2007-01-25 03:17:02 +03:00
|
|
|
if( opal_path_is_absolute(fname) ) {
|
2007-03-01 16:39:20 +03:00
|
|
|
return opal_path_access(fname, "", mode);
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
2005-02-09 20:13:54 +03:00
|
|
|
|
|
|
|
/* Initialize. */
|
|
|
|
|
2004-01-09 11:03:27 +03:00
|
|
|
fullpath = NULL;
|
|
|
|
i = 0;
|
2005-02-09 20:13:54 +03:00
|
|
|
|
|
|
|
/* Consider each directory until the file is found. Thus, the
|
|
|
|
order of directories is important. */
|
|
|
|
|
|
|
|
while (pathv[i] && NULL == fullpath) {
|
|
|
|
|
|
|
|
/* Replace environment variable at the head of the string. */
|
2004-01-09 11:03:27 +03:00
|
|
|
if ('$' == *pathv[i]) {
|
2006-08-23 03:25:13 +04:00
|
|
|
delimit = strchr(pathv[i], OPAL_PATH_SEP[0]);
|
2004-01-09 11:03:27 +03:00
|
|
|
if (delimit) {
|
|
|
|
*delimit = '\0';
|
|
|
|
}
|
|
|
|
env = list_env_get(pathv[i]+1, envv);
|
|
|
|
if (delimit) {
|
2006-08-23 03:25:13 +04:00
|
|
|
*delimit = OPAL_PATH_SEP[0];
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
2005-02-09 20:13:54 +03:00
|
|
|
if (NULL != env) {
|
2004-01-09 11:03:27 +03:00
|
|
|
if (!delimit) {
|
2007-03-01 16:39:20 +03:00
|
|
|
fullpath = opal_path_access(fname, env, mode);
|
2004-01-09 11:03:27 +03:00
|
|
|
} else {
|
2004-10-18 19:38:19 +04:00
|
|
|
pfix = (char*) malloc(strlen(env) + strlen(delimit) + 1);
|
2005-02-09 20:13:54 +03:00
|
|
|
if (NULL == pfix) {
|
|
|
|
return NULL;
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
|
|
|
strcpy(pfix, env);
|
|
|
|
strcat(pfix, delimit);
|
2007-03-01 16:39:20 +03:00
|
|
|
fullpath = opal_path_access(fname, pfix, mode);
|
2004-02-10 03:09:36 +03:00
|
|
|
free(pfix);
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-03-01 16:39:20 +03:00
|
|
|
fullpath = opal_path_access(fname, pathv[i], mode);
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2006-08-23 03:25:13 +04:00
|
|
|
return opal_make_filename_os_friendly(fullpath);
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
|
|
|
|
2005-02-09 20:13:54 +03:00
|
|
|
/*
|
|
|
|
* Locates a file with certain permissions from a list of search paths
|
2004-01-09 11:03:27 +03:00
|
|
|
*/
|
2005-07-04 05:59:52 +04:00
|
|
|
char *opal_path_findv(char *fname, int mode, char **envv, char *wrkdir)
|
2004-01-09 11:03:27 +03:00
|
|
|
{
|
2005-02-09 20:13:54 +03:00
|
|
|
char **dirv;
|
|
|
|
char *fullpath;
|
|
|
|
char *path;
|
|
|
|
int dirc;
|
|
|
|
int i;
|
|
|
|
bool found_dot = false;
|
|
|
|
|
|
|
|
/* Set the local search paths. */
|
|
|
|
|
2004-01-09 11:03:27 +03:00
|
|
|
dirc = 0;
|
|
|
|
dirv = NULL;
|
|
|
|
|
2005-02-09 20:13:54 +03:00
|
|
|
if (NULL != (path = list_env_get("PATH", envv))) {
|
2004-01-09 11:03:27 +03:00
|
|
|
path_env_load(path, &dirc, &dirv);
|
|
|
|
}
|
2005-02-09 20:13:54 +03:00
|
|
|
|
|
|
|
/* Replace the "." path by the working directory. */
|
|
|
|
|
|
|
|
if (NULL != wrkdir) {
|
|
|
|
for (i = 0; i < dirc; ++i) {
|
|
|
|
if (0 == strcmp(dirv[i], ".")) {
|
|
|
|
found_dot = true;
|
|
|
|
free(dirv[i]);
|
|
|
|
dirv[i] = strdup(wrkdir);
|
|
|
|
if (NULL == dirv[i]){
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-02-09 20:13:54 +03:00
|
|
|
|
|
|
|
/* If we didn't find "." in the path and we have a wrkdir, append
|
|
|
|
the wrkdir to the end of the path */
|
|
|
|
|
|
|
|
if (!found_dot && NULL != wrkdir) {
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(&dirc, &dirv, wrkdir);
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
|
|
|
|
2005-03-18 06:43:59 +03:00
|
|
|
if(NULL == dirv)
|
|
|
|
return NULL;
|
2005-07-04 05:59:52 +04:00
|
|
|
fullpath = opal_path_find(fname, dirv, mode, envv);
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_free(dirv);
|
2005-02-09 20:13:54 +03:00
|
|
|
return fullpath;
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2005-02-09 20:13:54 +03:00
|
|
|
* Forms a complete pathname and checks it for existance and
|
|
|
|
* permissions
|
2004-01-09 11:03:27 +03:00
|
|
|
*
|
2004-01-09 21:47:52 +03:00
|
|
|
* Accepts:
|
|
|
|
* -fname File name
|
|
|
|
* -path Path prefix
|
|
|
|
* -mode Target permissions which must be satisfied
|
2004-01-09 11:03:27 +03:00
|
|
|
*
|
2004-01-09 21:47:52 +03:00
|
|
|
* Returns:
|
|
|
|
* -Full pathname of located file Success
|
|
|
|
* -NULL Failure
|
2004-01-09 11:03:27 +03:00
|
|
|
*/
|
2007-03-01 16:39:20 +03:00
|
|
|
char *opal_path_access(char *fname, char *path, int mode)
|
2004-01-09 11:03:27 +03:00
|
|
|
{
|
2007-03-01 16:39:20 +03:00
|
|
|
char *fullpath = NULL;
|
2008-08-20 19:30:25 +04:00
|
|
|
struct stat buf;
|
|
|
|
|
2005-02-09 20:13:54 +03:00
|
|
|
/* Allocate space for the full pathname. */
|
2008-08-20 19:30:25 +04:00
|
|
|
if (NULL == path) {
|
|
|
|
fullpath = opal_os_path(false, fname, NULL);
|
2007-07-01 21:53:19 +04:00
|
|
|
} else {
|
2008-08-20 19:30:25 +04:00
|
|
|
fullpath = opal_os_path(false, path, fname, NULL);
|
2007-03-01 16:39:20 +03:00
|
|
|
}
|
2008-08-20 19:30:25 +04:00
|
|
|
if (NULL == fullpath)
|
2005-02-09 20:13:54 +03:00
|
|
|
return NULL;
|
2008-08-20 19:30:25 +04:00
|
|
|
|
|
|
|
/* first check to see - is this a file or a directory? We
|
|
|
|
* only want files
|
|
|
|
*/
|
|
|
|
if (0 != stat(fullpath, &buf)) {
|
|
|
|
/* couldn't stat the path - obviously, this also meets the
|
|
|
|
* existence check, if that was requested
|
|
|
|
*/
|
2004-02-10 03:09:36 +03:00
|
|
|
free(fullpath);
|
2008-08-20 19:30:25 +04:00
|
|
|
return NULL;
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
2008-08-20 19:30:25 +04:00
|
|
|
|
|
|
|
if (!(S_IFREG & buf.st_mode) &&
|
|
|
|
!(S_IFLNK & buf.st_mode)) {
|
|
|
|
/* this isn't a regular file or a symbolic link, so
|
|
|
|
* ignore it
|
|
|
|
*/
|
|
|
|
free(fullpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check the permissions */
|
|
|
|
if ((X_OK & mode) && !(S_IXUSR & buf.st_mode)) {
|
|
|
|
/* if they asked us to check executable permission,
|
|
|
|
* and that isn't set, then return NULL
|
|
|
|
*/
|
|
|
|
free(fullpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((R_OK & mode) && !(S_IRUSR & buf.st_mode)) {
|
|
|
|
/* if they asked us to check read permission,
|
|
|
|
* and that isn't set, then return NULL
|
|
|
|
*/
|
|
|
|
free(fullpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((W_OK & mode) && !(S_IWUSR & buf.st_mode)) {
|
|
|
|
/* if they asked us to check write permission,
|
|
|
|
* and that isn't set, then return NULL
|
|
|
|
*/
|
|
|
|
free(fullpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* must have met all criteria! */
|
2005-02-09 20:13:54 +03:00
|
|
|
return fullpath;
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
|
|
|
|
2005-02-09 20:13:54 +03:00
|
|
|
|
2004-01-09 11:03:27 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Loads argument array with $PATH env var.
|
|
|
|
*
|
2004-01-09 21:47:52 +03:00
|
|
|
* Accepts
|
|
|
|
* -path String contiaing the $PATH
|
|
|
|
* -argc Pointer to argc
|
|
|
|
* -argv Pointer to list of argv
|
2004-01-09 11:03:27 +03:00
|
|
|
*/
|
2005-02-09 20:13:54 +03:00
|
|
|
static void path_env_load(char *path, int *pargc, char ***pargv)
|
2004-01-09 11:03:27 +03:00
|
|
|
{
|
2005-02-09 20:13:54 +03:00
|
|
|
char *p;
|
|
|
|
char saved;
|
2004-01-09 11:03:27 +03:00
|
|
|
|
|
|
|
if (NULL == path) {
|
2005-02-09 20:13:54 +03:00
|
|
|
*pargc = 0;
|
|
|
|
return;
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
2005-02-09 20:13:54 +03:00
|
|
|
|
|
|
|
/* Loop through the paths (delimited by PATHENVSEP), adding each
|
|
|
|
one to argv. */
|
|
|
|
|
|
|
|
while ('\0' != *path) {
|
|
|
|
|
2007-07-01 21:53:19 +04:00
|
|
|
/* Locate the delimiter. */
|
2005-02-09 20:13:54 +03:00
|
|
|
|
2006-08-23 03:25:13 +04:00
|
|
|
for (p = path; *p && (*p != OPAL_ENV_SEP); ++p) {
|
2005-02-09 20:13:54 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the path. */
|
|
|
|
|
2004-01-09 11:03:27 +03:00
|
|
|
if (p != path) {
|
|
|
|
saved = *p;
|
|
|
|
*p = '\0';
|
2005-07-04 04:13:44 +04:00
|
|
|
opal_argv_append(pargc, pargv, path);
|
2004-01-09 11:03:27 +03:00
|
|
|
*p = saved;
|
|
|
|
path = p;
|
|
|
|
}
|
2005-02-09 20:13:54 +03:00
|
|
|
|
|
|
|
/* Skip past the delimiter, if present. */
|
|
|
|
|
2004-01-09 11:03:27 +03:00
|
|
|
if (*path) {
|
|
|
|
++path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-09 20:13:54 +03:00
|
|
|
|
2004-01-09 11:03:27 +03:00
|
|
|
/**
|
|
|
|
* Gets value of variable in list or environment. Looks in the list first
|
|
|
|
*
|
2004-01-09 21:47:52 +03:00
|
|
|
* Accepts:
|
|
|
|
* -var String variable
|
|
|
|
* -list Pointer to environment list
|
2004-01-09 11:03:27 +03:00
|
|
|
*
|
2004-01-09 21:47:52 +03:00
|
|
|
* Returns:
|
|
|
|
* -List Pointer to environment list Success
|
|
|
|
* -NULL Failure
|
2004-01-09 11:03:27 +03:00
|
|
|
*/
|
2005-02-09 20:13:54 +03:00
|
|
|
static char *list_env_get(char *var, char **list)
|
2004-01-09 11:03:27 +03:00
|
|
|
{
|
2006-08-23 03:25:13 +04:00
|
|
|
size_t n;
|
2004-01-09 11:03:27 +03:00
|
|
|
|
2005-02-09 20:13:54 +03:00
|
|
|
if (NULL != list) {
|
2004-01-09 11:03:27 +03:00
|
|
|
n = strlen(var);
|
|
|
|
|
2005-02-09 20:13:54 +03:00
|
|
|
while (NULL != *list) {
|
2004-01-09 11:03:27 +03:00
|
|
|
if ((0 == strncmp(var, *list, n)) && ('=' == (*list)[n])) {
|
2005-02-09 20:13:54 +03:00
|
|
|
return (*list + n + 1);
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
2005-02-09 20:13:54 +03:00
|
|
|
++list;
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
|
|
|
}
|
2005-02-09 20:13:54 +03:00
|
|
|
return getenv(var);
|
2004-01-09 11:03:27 +03:00
|
|
|
}
|
|
|
|
|
2007-07-01 21:53:19 +04:00
|
|
|
/**
|
|
|
|
* Try to figure out the absolute path based on the application name
|
|
|
|
* (usually argv[0]). If the path is already absolute return a copy, if
|
|
|
|
* it start with . look into the current directory, if not dig into
|
|
|
|
* the $PATH.
|
|
|
|
* In case of error or if executable was not found (as an example if
|
|
|
|
* the application did a cwd between the start and this call), the
|
|
|
|
* function will return NULL. Otherwise, an newly allocated string
|
|
|
|
* will be returned.
|
|
|
|
*/
|
|
|
|
char* opal_find_absolute_path( char* app_name )
|
|
|
|
{
|
|
|
|
char* abs_app_name;
|
2009-10-20 08:05:16 +04:00
|
|
|
char cwd[OPAL_PATH_MAX], *pcwd;
|
2007-07-01 21:53:19 +04:00
|
|
|
|
|
|
|
if( opal_path_is_absolute(app_name) ) { /* already absolute path */
|
|
|
|
abs_app_name = app_name;
|
2009-10-20 08:05:16 +04:00
|
|
|
} else if ( '.' == app_name[0] ||
|
|
|
|
NULL != strchr(app_name, OPAL_PATH_SEP[0])) {
|
|
|
|
/* the app is in the current directory or below it */
|
2009-05-07 00:11:28 +04:00
|
|
|
pcwd = getcwd( cwd, OPAL_PATH_MAX );
|
2007-07-01 21:53:19 +04:00
|
|
|
if( NULL == pcwd ) {
|
|
|
|
/* too bad there is no way we can get the app absolute name */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
abs_app_name = opal_os_path( false, pcwd, app_name, NULL );
|
|
|
|
} else {
|
|
|
|
/* Otherwise try to search for the application in the PATH ... */
|
|
|
|
abs_app_name = opal_path_findv( app_name, X_OK, NULL, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( NULL != abs_app_name ) {
|
2009-05-07 00:11:28 +04:00
|
|
|
char* resolved_path = (char*)malloc(OPAL_PATH_MAX);
|
2007-07-02 18:55:10 +04:00
|
|
|
#if !defined(__WINDOWS__)
|
2007-07-01 21:53:19 +04:00
|
|
|
realpath( abs_app_name, resolved_path );
|
2007-07-02 18:55:10 +04:00
|
|
|
#else
|
2007-07-25 07:49:31 +04:00
|
|
|
#ifdef HAVE_SHLWAPI_H
|
2007-07-02 22:11:14 +04:00
|
|
|
PathCanonicalize(resolved_path, abs_app_name);
|
2007-07-25 07:49:31 +04:00
|
|
|
#endif
|
2007-07-02 18:55:10 +04:00
|
|
|
#endif /* !defined(__WINDOWS__) */
|
2007-07-01 21:53:19 +04:00
|
|
|
if( abs_app_name != app_name ) free(abs_app_name);
|
2007-09-14 06:09:38 +04:00
|
|
|
return resolved_path;
|
2007-07-01 21:53:19 +04:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-02-11 02:18:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Figure out, whether fname is on network file system
|
|
|
|
*
|
|
|
|
* Try to figure out, whether the file name specified through fname is
|
2010-02-12 17:15:39 +03:00
|
|
|
* on any network file system (currently NFS, Lustre, Panasas and GPFS).
|
2010-02-11 02:18:29 +03:00
|
|
|
*
|
|
|
|
* If the file is not created, the parent directory is checked.
|
|
|
|
* This allows checking for NFS prior to opening the file.
|
|
|
|
*
|
|
|
|
* @param[in] fname File name to check
|
|
|
|
*
|
2010-02-12 17:15:39 +03:00
|
|
|
* @retval true If fname is on NFS, Lustre, Panasas or GPFS
|
2010-02-11 02:18:29 +03:00
|
|
|
* @retval false otherwise
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Linux:
|
|
|
|
* statfs(const char *path, struct statfs *buf);
|
|
|
|
* with fsid_t f_fsid; (in kernel struct{ int val[2] };)
|
|
|
|
* return 0 success, -1 on failure with errno set.
|
|
|
|
* statvfs (const char *path, struct statvfs *buf);
|
|
|
|
* with unsigned long f_fsid; -- returns wrong info
|
|
|
|
* return 0 success, -1 on failure with errno set.
|
|
|
|
* Solaris:
|
|
|
|
* statvfs (const char *path, struct statvfs *buf);
|
|
|
|
* with f_basetype, contains a string of length FSTYPSZ
|
|
|
|
* return 0 success, -1 on failure with errno set.
|
|
|
|
* FreeBSD:
|
|
|
|
* statfs(const char *path, struct statfs *buf);
|
|
|
|
* with f_fstypename, contains a string of length MFSNAMELEN
|
|
|
|
* return 0 success, -1 on failure with errno set.
|
|
|
|
* compliant with: 4.4BSD.
|
|
|
|
* Mac OSX (10.6.2):
|
|
|
|
* statvfs(const char * restrict path, struct statvfs * restrict buf);
|
|
|
|
* with fsid Not meaningful in this implementation.
|
|
|
|
* is just a wrapper around statfs()
|
|
|
|
* statfs(const char *path, struct statfs *buf);
|
|
|
|
* with f_fstypename, contains a string of length MFSTYPENAMELEN
|
|
|
|
* return 0 success, -1 on failure with errno set.
|
|
|
|
* Windows (interix):
|
|
|
|
* statvfs(const char *path, struct statvfs *buf);
|
|
|
|
* with unsigned long f_fsid
|
|
|
|
* return 0 success, -1 on failure with errno set.
|
|
|
|
*/
|
|
|
|
#ifndef LL_SUPER_MAGIC
|
|
|
|
#define LL_SUPER_MAGIC 0x0BD00BD0 /* Lustre magic number */
|
|
|
|
#endif
|
|
|
|
#ifndef NFS_SUPER_MAGIC
|
|
|
|
#define NFS_SUPER_MAGIC 0x6969
|
|
|
|
#endif
|
|
|
|
#ifndef PAN_KERNEL_FS_CLIENT_SUPER_MAGIC
|
|
|
|
#define PAN_KERNEL_FS_CLIENT_SUPER_MAGIC 0xAAD7AAEA /* Panasas FS */
|
|
|
|
#endif
|
2010-02-12 17:15:39 +03:00
|
|
|
#ifndef GPFS_SUPER_MAGIC
|
|
|
|
#define GPFS_SUPER_MAGIC 0x47504653 /* Thats GPFS in ASCII */
|
|
|
|
#endif
|
2010-02-11 02:18:29 +03:00
|
|
|
|
|
|
|
#define MASK2 0xffff
|
|
|
|
#define MASK4 0xffffffff
|
|
|
|
|
|
|
|
bool opal_path_nfs(char *fname)
|
|
|
|
{
|
|
|
|
#if !defined(__WINDOWS__)
|
|
|
|
int i;
|
|
|
|
int rc;
|
|
|
|
int trials;
|
|
|
|
char * file = strdup (fname);
|
2010-02-23 18:58:49 +03:00
|
|
|
#if defined(__SVR4) && defined(__sun)
|
2010-02-11 02:18:29 +03:00
|
|
|
struct statvfs buf;
|
2012-02-01 00:27:33 +04:00
|
|
|
#elif defined(__linux__) || defined (__BSD) || (defined(__APPLE__) && defined(__MACH__))
|
2010-02-11 02:18:29 +03:00
|
|
|
struct statfs buf;
|
|
|
|
#endif
|
2010-08-26 14:10:16 +04:00
|
|
|
/*
|
|
|
|
* Be sure to update the test (test/util/opal_path_nfs.c)
|
|
|
|
* while adding a new Network/Cluster Filesystem here
|
|
|
|
*/
|
2010-02-11 02:18:29 +03:00
|
|
|
static struct fs_types_t {
|
|
|
|
unsigned long long f_fsid;
|
|
|
|
unsigned long long f_mask;
|
|
|
|
const char * f_fsname;
|
|
|
|
} fs_types[] = {
|
|
|
|
{LL_SUPER_MAGIC, MASK4, "lustre"},
|
|
|
|
{NFS_SUPER_MAGIC, MASK2, "nfs"},
|
|
|
|
{PAN_KERNEL_FS_CLIENT_SUPER_MAGIC, MASK4, "panfs"},
|
2010-02-12 17:15:39 +03:00
|
|
|
{GPFS_SUPER_MAGIC, MASK4, "gpfs"}
|
2010-02-11 02:18:29 +03:00
|
|
|
};
|
|
|
|
#define FS_TYPES_NUM (int)(sizeof (fs_types)/sizeof (fs_types[0]))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First, get the OS-dependent struct stat(v)fs buf
|
|
|
|
* This may return the ESTALE error on NFS, if the underlying file/path has changed
|
|
|
|
*/
|
|
|
|
again:
|
|
|
|
trials = 5;
|
|
|
|
do {
|
2010-02-23 18:58:49 +03:00
|
|
|
#if defined(__SVR4) && defined(__sun)
|
2010-02-11 02:18:29 +03:00
|
|
|
rc = statvfs (file, &buf);
|
2012-02-01 00:27:33 +04:00
|
|
|
#elif defined(__linux__) || defined (__BSD) || (defined(__APPLE__) && defined(__MACH__))
|
2010-02-11 02:18:29 +03:00
|
|
|
rc = statfs (file, &buf);
|
|
|
|
#endif
|
|
|
|
} while (-1 == rc && ESTALE == errno && (0 < --trials));
|
|
|
|
|
|
|
|
/* In case some error with the current filename, try the directory */
|
|
|
|
if (-1 == rc) {
|
|
|
|
char * last_sep;
|
|
|
|
|
|
|
|
OPAL_OUTPUT_VERBOSE((10, 0, "opal_path_nfs: stat(v)fs on file:%s failed errno:%d directory:%s\n",
|
|
|
|
fname, errno, file));
|
2010-02-18 02:40:08 +03:00
|
|
|
|
|
|
|
last_sep = strrchr(file, OPAL_PATH_SEP[0]);
|
2010-02-11 02:18:29 +03:00
|
|
|
/* Stop the search, when we have searched past root '/' */
|
2010-02-23 18:58:49 +03:00
|
|
|
if (NULL == last_sep || (1 == strlen(last_sep) &&
|
2010-02-24 22:05:19 +03:00
|
|
|
OPAL_PATH_SEP[0] == *last_sep)) {
|
2010-02-11 02:18:29 +03:00
|
|
|
free (file);
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-18 02:40:08 +03:00
|
|
|
*last_sep = '\0';
|
2010-02-11 02:18:29 +03:00
|
|
|
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next, extract the magic value */
|
2010-02-23 18:58:49 +03:00
|
|
|
#if defined(__SVR4) && defined(__sun)
|
|
|
|
for (i = 0; i < FS_TYPES_NUM; i++)
|
2010-02-11 02:18:29 +03:00
|
|
|
if (0 == strncasecmp (fs_types[i].f_fsname, buf.f_basetype, FSTYPSZ))
|
|
|
|
goto found;
|
2010-02-27 11:15:49 +03:00
|
|
|
#elif (defined(__APPLE__) && defined(__MACH__))
|
2010-02-11 02:18:29 +03:00
|
|
|
for (i = 0; i < FS_TYPES_NUM; i++)
|
|
|
|
if (0 == strncasecmp (fs_types[i].f_fsname, buf.f_fstypename, MFSTYPENAMELEN))
|
|
|
|
goto found;
|
|
|
|
#elif defined(__BSD)
|
|
|
|
for (i = 0; i < FS_TYPES_NUM; i++)
|
|
|
|
if (0 == strncasecmp (fs_types[i].f_fsname, buf.f_fstypename, MFSNAMELEN))
|
|
|
|
goto found;
|
2012-02-01 00:27:33 +04:00
|
|
|
#elif defined(__linux__)
|
2010-02-11 02:18:29 +03:00
|
|
|
for (i = 0; i < FS_TYPES_NUM; i++)
|
|
|
|
if (fs_types[i].f_fsid == (buf.f_type & fs_types[i].f_mask))
|
|
|
|
goto found;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
free (file);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
found:
|
|
|
|
OPAL_OUTPUT_VERBOSE((10, 0, "opal_path_nfs: file:%s on fs:%s\n",
|
|
|
|
fname, fs_types[i].f_fsname));
|
|
|
|
free (file);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
#undef FS_TYPES_NUM
|
|
|
|
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif /* __WINDOWS__ */
|
|
|
|
}
|
|
|
|
|