From: Jonathan Rajotte Date: Wed, 19 Jun 2019 17:27:21 +0000 (-0400) Subject: libs: Do not rely on nftw for detecting the validity of plugin path X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=037cbd1ac687161b52b6e1c5fd49dcd9d79a878f libs: Do not rely on nftw for detecting the validity of plugin path test_plugin.c:test_create_all_from_dir with non existing path fails on cygwin. An error is expected but bt_plugin_find_all_from_dir returns success. The cygwin implementation of nftw is not posix compliant regarding returning ENOENT on non-existent path. [1] [2] The cygwin implementation of nftw does not return ENOENT on non-existent path. Instead, the non-existent path is passed through the fn function (nftw_append_all_from_dir) and the flag is set to FTW_NS. Since we are "discovering" plugins, nftw_append_all_from_dir does not error out on FTW_NS and simply skip the file. The proposed solution is to stat() the provided path and validate that no errors occur before using nftw(). [1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/nftw.html [2] https://cygwin.com/ml/cygwin/2019-06/msg00185.html Change-Id: Ia7c88dbab5b16affbdb8ce1dbe261b03635727cc Signed-off-by: Jonathan Rajotte Reviewed-on: https://review.lttng.org/c/babeltrace/+/1520 CI-Build: Philippe Proulx Tested-by: jenkins Reviewed-by: Philippe Proulx --- diff --git a/src/lib/plugin/plugin.c b/src/lib/plugin/plugin.c index 7caf362e..1cef5123 100644 --- a/src/lib/plugin/plugin.c +++ b/src/lib/plugin/plugin.c @@ -485,10 +485,28 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir( int nftw_flags = FTW_PHYS; int ret; enum bt_plugin_status status; + struct stat sb; BT_ASSERT(plugin_set); BT_ASSERT(path); BT_ASSERT(strlen(path) < PATH_MAX); + + /* + * Make sure that path exists and is accessible. + * This is necessary since Cygwin implementation of nftw() is not POSIX + * compliant. Cygwin nftw() implementation does not fail on non-existent + * path with ENOENT. Instead, it flags the directory as FTW_NS. FTW_NS during + * nftw_append_all_from_dir is not treated as an error since we are + * traversing the tree for plugin discovery. + */ + if (stat(path, &sb)) { + BT_LOGW_ERRNO("Cannot open directory", + ": path=\"%s\", recurse=%d", + path, recurse); + status = BT_PLUGIN_STATUS_ERROR; + goto end; + } + pthread_mutex_lock(&append_all_from_dir_info.lock); append_all_from_dir_info.plugin_set = plugin_set; append_all_from_dir_info.recurse = recurse;