All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] change thread-unsafe readdir to thread-safe readdir_r calls
@ 2010-07-07 18:37 Steven Dake
       [not found] ` <1278527821-14804-1-git-send-email-sdake-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
  0 siblings, 1 reply; 14+ messages in thread
From: Steven Dake @ 2010-07-07 18:37 UTC (permalink / raw)
  To: linux-rdma-u79uwXL29TY76Z2rM5mHXA; +Cc: Steven Dake, Steven Dake

From: Steven Dake <sdake-AqqL/iNfm4CbU/LNMs03Rw@public.gmane.org>

The readdir POSIX api is not thread safe.  This presents problems in
multithreaded programs that use the libibverbs APIs.  This patch has been
tested with a libibverbs application (http://www.corosync.org) on
Mellanox MT26428 cards.

Signed-off-by: Steven Dake <sdake-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
---
 src/init.c |   31 +++++++++++++++++++++++++------
 1 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/src/init.c b/src/init.c
index 4f0130e..ac3efdc 100644
--- a/src/init.c
+++ b/src/init.c
@@ -46,10 +46,12 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <dirent.h>
+#include <stddef.h>
 #include <errno.h>
 
 #include "ibverbs.h"
 
+
 HIDDEN int abi_ver;
 
 struct ibv_sysfs_dev {
@@ -85,6 +87,7 @@ static int find_sysfs_devs(void)
 	struct ibv_sysfs_dev *sysfs_dev = NULL;
 	char value[8];
 	int ret = 0;
+	struct dirent *buf;
 
 	snprintf(class_path, sizeof class_path, "%s/class/infiniband_verbs",
 		 ibv_get_sysfs_path());
@@ -93,7 +96,12 @@ static int find_sysfs_devs(void)
 	if (!class_dir)
 		return ENOSYS;
 
-	while ((dent = readdir(class_dir))) {
+	buf = (struct dirent *)malloc(offsetof(struct dirent, d_name) + NAME_MAX + 1);
+	if (buf == NULL) {
+		goto out_closedir;
+		ret = ENOMEM;
+	}
+	while (readdir_r(class_dir, buf, &dent) == 0 && dent) {
 		struct stat buf;
 
 		if (dent->d_name[0] == '.')
@@ -146,9 +154,11 @@ static int find_sysfs_devs(void)
 	}
 
  out:
+	free (buf);
 	if (sysfs_dev)
 		free(sysfs_dev);
 
+out_closedir:
 	closedir(class_dir);
 	return ret;
 }
@@ -290,26 +300,31 @@ static void read_config_file(const char *path)
 	fclose(conf);
 }
 
-static void read_config(void)
+static int read_config(void)
 {
 	DIR *conf_dir;
 	struct dirent *dent;
+	struct dirent *buf;
 	char *path;
 
 	conf_dir = opendir(IBV_CONFIG_DIR);
 	if (!conf_dir) {
 		fprintf(stderr, PFX "Warning: couldn't open config directory '%s'.\n",
 			IBV_CONFIG_DIR);
-		return;
+		return 0;
 	}
 
-	while ((dent = readdir(conf_dir))) {
+	buf = (struct dirent *)malloc(offsetof(struct dirent, d_name) + NAME_MAX + 1);
+	if (buf == NULL)
+		return ENOMEM;
+
+	while (readdir_r(conf_dir, buf, &dent) == 0 && dent) {
 		struct stat buf;
 
 		if (asprintf(&path, "%s/%s", IBV_CONFIG_DIR, dent->d_name) < 0) {
 			fprintf(stderr, PFX "Warning: couldn't read config file %s/%s.\n",
 				IBV_CONFIG_DIR, dent->d_name);
-			return;
+			return 0;
 		}
 
 		if (stat(path, &buf)) {
@@ -326,7 +341,9 @@ next:
 		free(path);
 	}
 
+	free(buf);
 	closedir(conf_dir);
+	return 0;
 }
 
 static struct ibv_device *try_driver(struct ibv_driver *driver,
@@ -471,7 +488,9 @@ HIDDEN int ibverbs_init(struct ibv_device ***list)
 
 	check_memlock_limit();
 
-	read_config();
+	ret = read_config();
+	if (ret)
+		return -ret;
 
 	ret = find_sysfs_devs();
 	if (ret)
-- 
1.6.6.1

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 14+ messages in thread
* [PATCH] change thread-unsafe readdir to thread-safe readdir_r calls
@ 2010-07-07 22:14 Steven Dake
       [not found] ` <1278540873-3857-1-git-send-email-sdake-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
  0 siblings, 1 reply; 14+ messages in thread
From: Steven Dake @ 2010-07-07 22:14 UTC (permalink / raw)
  To: linux-rdma-u79uwXL29TY76Z2rM5mHXA; +Cc: Steven Dake, Steven Dake

From: Steven Dake <sdake-AqqL/iNfm4CbU/LNMs03Rw@public.gmane.org>

The readdir POSIX api is not thread safe.  This presents problems in
multithreaded programs that use the libibverbs APIs.  This patch has been
tested with a libibverbs application (http://www.corosync.org) on
Mellanox MT26428 cards.

This updated version uses alloca instead of malloc.

Signed-off-by: Steven Dake <sdake-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
---
 src/init.c |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/init.c b/src/init.c
index 4f0130e..11b8415 100644
--- a/src/init.c
+++ b/src/init.c
@@ -35,6 +35,9 @@
 #  include <config.h>
 #endif /* HAVE_CONFIG_H */
 
+#if HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
 #include <stdlib.h>
 #include <string.h>
 #include <glob.h>
@@ -46,6 +49,7 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <dirent.h>
+#include <stddef.h>
 #include <errno.h>
 
 #include "ibverbs.h"
@@ -85,6 +89,7 @@ static int find_sysfs_devs(void)
 	struct ibv_sysfs_dev *sysfs_dev = NULL;
 	char value[8];
 	int ret = 0;
+	struct dirent *buf;
 
 	snprintf(class_path, sizeof class_path, "%s/class/infiniband_verbs",
 		 ibv_get_sysfs_path());
@@ -93,7 +98,8 @@ static int find_sysfs_devs(void)
 	if (!class_dir)
 		return ENOSYS;
 
-	while ((dent = readdir(class_dir))) {
+	buf = alloca(offsetof(struct dirent, d_name) + NAME_MAX + 1);
+	while (readdir_r(class_dir, buf, &dent) == 0 && dent) {
 		struct stat buf;
 
 		if (dent->d_name[0] == '.')
@@ -294,6 +300,7 @@ static void read_config(void)
 {
 	DIR *conf_dir;
 	struct dirent *dent;
+	struct dirent *buf;
 	char *path;
 
 	conf_dir = opendir(IBV_CONFIG_DIR);
@@ -303,7 +310,8 @@ static void read_config(void)
 		return;
 	}
 
-	while ((dent = readdir(conf_dir))) {
+	buf = alloca(offsetof(struct dirent, d_name) + NAME_MAX + 1);
+	while (readdir_r(conf_dir, buf, &dent) == 0 && dent) {
 		struct stat buf;
 
 		if (asprintf(&path, "%s/%s", IBV_CONFIG_DIR, dent->d_name) < 0) {
-- 
1.6.6.1

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2010-07-21 18:33 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-07 18:37 [PATCH] change thread-unsafe readdir to thread-safe readdir_r calls Steven Dake
     [not found] ` <1278527821-14804-1-git-send-email-sdake-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2010-07-07 18:52   ` Jason Gunthorpe
     [not found]     ` <20100707185257.GJ4630-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2010-07-07 19:14       ` Steven Dake
     [not found]         ` <4C34D208.1000705-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2010-07-07 20:47           ` Jason Gunthorpe
     [not found]             ` <20100707204712.GK4630-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2010-07-07 21:24               ` Steven Dake
     [not found]                 ` <4C34F09D.6080908-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2010-07-07 21:49                   ` Jason Gunthorpe
     [not found]                     ` <20100707214920.GN4630-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2010-07-07 22:17                       ` Steven Dake
2010-07-07 18:54   ` Roland Dreier
     [not found]     ` <adatyobyvg7.fsf-BjVyx320WGW9gfZ95n9DRSW4+XlvGpQz@public.gmane.org>
2010-07-07 19:17       ` Steven Dake
  -- strict thread matches above, loose matches on Subject: below --
2010-07-07 22:14 Steven Dake
     [not found] ` <1278540873-3857-1-git-send-email-sdake-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2010-07-08 18:27   ` Roland Dreier
     [not found]     ` <adalj9lzv4r.fsf-BjVyx320WGW9gfZ95n9DRSW4+XlvGpQz@public.gmane.org>
2010-07-08 18:47       ` Jason Gunthorpe
2010-07-21 18:06   ` Roland Dreier
     [not found]     ` <adak4ooogjj.fsf-BjVyx320WGW9gfZ95n9DRSW4+XlvGpQz@public.gmane.org>
2010-07-21 18:33       ` Steven Dake

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.