+
+static int
+verify_perms(const char *dirpath)
+{
+ struct stat st;
+
+ /* if not root, check down one level first */
+ if (strcmp(dirpath, "/") != 0) {
+ static __thread char last_dir_checked[PATH_MAX];
+ char copy[PATH_MAX];
+ const char *dir;
+
+ strlcpy(copy, dirpath, PATH_MAX);
+ dir = dirname(copy);
+ if (strncmp(dir, last_dir_checked, PATH_MAX) != 0) {
+ if (verify_perms(dir) != 0)
+ return -1;
+ strlcpy(last_dir_checked, dir, PATH_MAX);
+ }
+ }
+
+ /* call stat to check for permissions and ensure not world writable */
+ if (stat(dirpath, &st) != 0) {
+ RTE_LOG(ERR, EAL, "Error with stat on %s, %s\n",
+ dirpath, strerror(errno));
+ return -1;
+ }
+ if (st.st_mode & S_IWOTH) {
+ RTE_LOG(ERR, EAL,
+ "Error, directory path %s is world-writable and insecure\n",
+ dirpath);
+ return -1;
+ }
+
+ return 0;
+}
+
+static void *
+eal_dlopen(const char *pathname)
+{
+ void *retval = NULL;
+ char *realp = realpath(pathname, NULL);
+
+ if (realp == NULL && errno == ENOENT) {
+ /* not a full or relative path, try a load from system dirs */
+ retval = dlopen(pathname, RTLD_NOW);
+ if (retval == NULL)
+ RTE_LOG(ERR, EAL, "%s\n", dlerror());
+ return retval;
+ }
+ if (realp == NULL) {
+ RTE_LOG(ERR, EAL, "Error with realpath for %s, %s\n",
+ pathname, strerror(errno));
+ goto out;
+ }
+ if (strnlen(realp, PATH_MAX) == PATH_MAX) {
+ RTE_LOG(ERR, EAL, "Error, driver path greater than PATH_MAX\n");
+ goto out;
+ }
+
+ /* do permissions checks */
+ if (verify_perms(realp) != 0)
+ goto out;
+
+ retval = dlopen(realp, RTLD_NOW);
+ if (retval == NULL)
+ RTE_LOG(ERR, EAL, "%s\n", dlerror());
+out:
+ free(realp);
+ return retval;
+}
+
+static int
+is_shared_build(void)
+{
+#define EAL_SO "librte_eal.so"
+ char soname[32];
+ size_t len, minlen = strlen(EAL_SO);
+
+ len = strlcpy(soname, EAL_SO"."ABI_VERSION, sizeof(soname));
+ if (len > sizeof(soname)) {
+ RTE_LOG(ERR, EAL, "Shared lib name too long in shared build check\n");
+ len = sizeof(soname) - 1;
+ }
+
+ while (len >= minlen) {
+ /* check if we have this .so loaded, if so - shared build */
+ RTE_LOG(DEBUG, EAL, "Checking presence of .so '%s'\n", soname);
+ if (dlopen(soname, RTLD_LAZY | RTLD_NOLOAD) != NULL) {
+ RTE_LOG(INFO, EAL, "Detected shared linkage of DPDK\n");
+ return 1;
+ }
+
+ /* remove any version numbers off the end to retry */
+ while (len-- > 0)
+ if (soname[len] == '.') {
+ soname[len] = '\0';
+ break;
+ }
+ }
+
+ RTE_LOG(INFO, EAL, "Detected static linkage of DPDK\n");
+ return 0;
+}