From 87c48f0ca3c10561413f770e14858be1a09f9335 Mon Sep 17 00:00:00 2001 From: Ralph Castain Date: Wed, 18 Jan 2017 12:05:47 -0800 Subject: [PATCH 1/3] Cleanup the os_dirpath logic so it doesn't error out if the directory actually gets created (regardless of what mkdir returns), and pretty-prints the error if it does error out. Signed-off-by: Ralph Castain (cherry picked from commit b257c32d2c264f78897e24c6952ea5297709e44c) (cherry picked from commit c722fb954ee2ac6aa67683585837537e6e397c60) --- opal/util/help-opal-util.txt | 21 ++++++++++++++++ opal/util/os_dirpath.c | 46 ++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/opal/util/help-opal-util.txt b/opal/util/help-opal-util.txt index 7206b72dae5..07b49fd128a 100644 --- a/opal/util/help-opal-util.txt +++ b/opal/util/help-opal-util.txt @@ -1,6 +1,7 @@ # -*- text -*- # # Copyright (c) 2009 Cisco Systems, Inc. All rights reserved. +# Copyright (c) 2017 Intel, Inc. All rights reserved. # $COPYRIGHT$ # # Additional copyrights may follow @@ -93,3 +94,23 @@ resource: Limit: %s Please correct the request and try again. +# +[dir-mode] +While working through a directory tree, we were unable to set +a directory to the desired mode: + + Directory: %s + Mode: %0x + Error: %s + +Please check to ensure you have adequate permissions to perform +the desired operation. +# +[mkdir-failed] +A call to mkdir was unable to create the desired directory: + + Directory: %s + Error: %s + +Please check to ensure you have adequate permissions to perform +the desired operation. diff --git a/opal/util/os_dirpath.c b/opal/util/os_dirpath.c index 881eb424c8b..7973b48ae99 100644 --- a/opal/util/os_dirpath.c +++ b/opal/util/os_dirpath.c @@ -11,6 +11,7 @@ * All rights reserved. * Copyright (c) 2015 Research Organization for Information Science * and Technology (RIST). All rights reserved. + * Copyright (c) 2016-2017 Intel, Inc. All rights reserved. * $COPYRIGHT$ * * Additional copyrights may follow @@ -39,6 +40,7 @@ #include "opal/util/output.h" #include "opal/util/os_dirpath.h" +#include "opal/util/show_help.h" #include "opal/util/argv.h" #include "opal/util/os_path.h" #include "opal/constants.h" @@ -63,11 +65,9 @@ int opal_os_dirpath_create(const char *path, const mode_t mode) if (0 == (ret = chmod(path, (buf.st_mode | mode)))) { /* successfully change mode */ return(OPAL_SUCCESS); } - opal_output(0, - "opal_os_dirpath_create: " - "Error: Unable to create directory (%s), unable to set the correct mode [%d]\n", - path, ret); - return(OPAL_ERROR); /* can't set correct mode */ + opal_show_help("help-opal-util.txt", "dir-mode", true, + path, mode, strerror(errno)); + return(OPAL_ERR_PERM); /* can't set correct mode */ } /* quick -- try to make directory */ @@ -112,14 +112,11 @@ int opal_os_dirpath_create(const char *path, const mode_t mode) strcat(tmp, parts[i]); } - /* Now that we finally have the name to check, check it. - Create it if it doesn't exist. */ + /* Now that we have the name, try to create it */ ret = mkdir(tmp, mode); - if ((0 > ret && EEXIST != errno) || 0 != stat(tmp, &buf)) { - opal_output(0, - "opal_os_dirpath_create: " - "Error: Unable to create the sub-directory (%s) of (%s), mkdir failed [%d]\n", - tmp, path, ret); + if (0 != stat(tmp, &buf)) { + opal_show_help("help-opal-util.txt", "mkdir-failed", true, + tmp, strerror(errno)); opal_argv_free(parts); free(tmp); return OPAL_ERROR; @@ -263,19 +260,19 @@ bool opal_os_dirpath_is_empty(const char *path ) { struct dirent *ep; if (NULL != path) { /* protect against error */ - dp = opendir(path); - if (NULL != dp) { - while ((ep = readdir(dp))) { - if ((0 != strcmp(ep->d_name, ".")) && - (0 != strcmp(ep->d_name, ".."))) { + dp = opendir(path); + if (NULL != dp) { + while ((ep = readdir(dp))) { + if ((0 != strcmp(ep->d_name, ".")) && + (0 != strcmp(ep->d_name, ".."))) { closedir(dp); - return false; - } - } - closedir(dp); - return true; - } - return false; + return false; + } + } + closedir(dp); + return true; + } + return false; } return true; @@ -306,4 +303,3 @@ int opal_os_dirpath_access(const char *path, const mode_t in_mode ) { return( OPAL_ERR_NOT_FOUND ); } } - From 6ce7c08a41cbd975d54bb9637fc36634bb41d891 Mon Sep 17 00:00:00 2001 From: Ralph Castain Date: Wed, 18 Jan 2017 15:42:31 -0800 Subject: [PATCH 2/3] Quick fix: save the errno from the mkdir call as the call to stat will likely overwrite it Signed-off-by: Ralph Castain (cherry picked from commit 6da4dbbb333b03b978188b7ff0b45f39f3f9db21) (cherry picked from commit 867c841eb757d8663fa87afa5246075b8271a1d9) --- opal/util/os_dirpath.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/opal/util/os_dirpath.c b/opal/util/os_dirpath.c index 7973b48ae99..319639094bf 100644 --- a/opal/util/os_dirpath.c +++ b/opal/util/os_dirpath.c @@ -113,10 +113,11 @@ int opal_os_dirpath_create(const char *path, const mode_t mode) } /* Now that we have the name, try to create it */ - ret = mkdir(tmp, mode); + mkdir(tmp, mode); + ret = errno; // save the errno for an error msg, if needed if (0 != stat(tmp, &buf)) { opal_show_help("help-opal-util.txt", "mkdir-failed", true, - tmp, strerror(errno)); + tmp, strerror(ret)); opal_argv_free(parts); free(tmp); return OPAL_ERROR; From af522319f51e4e2cbeee5145f9420a2283e03429 Mon Sep 17 00:00:00 2001 From: Gilles Gouaillardet Date: Thu, 19 Jan 2017 14:02:47 +0900 Subject: [PATCH 3/3] opal/util: fix a race condition in opal_os_dirpath_create() always check the permissions of the created directory, in case some one else created the very same directory but with incompatible permissions Signed-off-by: Gilles Gouaillardet (cherry picked from commit dffaad9de294667c66fab79d7f67717054cc8584) (cherry picked from commit de539e0232147cd2d266866fd5156928ac12770e) --- opal/util/os_dirpath.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/opal/util/os_dirpath.c b/opal/util/os_dirpath.c index 319639094bf..6909ec41d38 100644 --- a/opal/util/os_dirpath.c +++ b/opal/util/os_dirpath.c @@ -9,7 +9,7 @@ * University of Stuttgart. All rights reserved. * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. - * Copyright (c) 2015 Research Organization for Information Science + * Copyright (c) 2015-2017 Research Organization for Information Science * and Technology (RIST). All rights reserved. * Copyright (c) 2016-2017 Intel, Inc. All rights reserved. * $COPYRIGHT$ @@ -121,6 +121,12 @@ int opal_os_dirpath_create(const char *path, const mode_t mode) opal_argv_free(parts); free(tmp); return OPAL_ERROR; + } else if (i == (len-1) && (mode != (mode & buf.st_mode)) && (0 > chmod(tmp, (buf.st_mode | mode)))) { + opal_show_help("help-opal-util.txt", "dir-mode", true, + tmp, mode, strerror(errno)); + opal_argv_free(parts); + free(tmp); + return(OPAL_ERR_PERM); /* can't set correct mode */ } }