POSIX Shared memory support

Philippe Ney philippe.ney at tizoo.com
Mon Jul 30 10:16:46 UTC 2007


> Is there any work being done to bring POSIX shared memory support to  
> uclibc? (shm_open and shm_unlink)
> 
> Regards
> 	Arnar Mar Sigurðsson - Valka ehf

Hi,

As said in the post Carlo pointed out, I used this patch.
In my project, I needed shm support to use Xenomai.
Finally, due to some other missings related to timer, I had
to use the glibc with buildroot instead of uClibc.

Philippe


diff -Naur a/librt/Makefile b/librt/Makefile
--- a/librt/Makefile	2005-08-18 00:49:48.000000000 +0200
+++ b/librt/Makefile	2006-11-30 09:33:25.000000000 +0100
@@ -14,7 +14,8 @@
 CSRC=mq_open.c mq_close.c mq_unlink.c mq_getsetattr.c \
      mq_send.c mq_receive.c mq_notify.c \
      timer_create.c timer_delete.c \
-     timer_settime.c timer_gettime.c timer_getoverr.c
+     timer_settime.c timer_gettime.c timer_getoverr.c \
+     shm_open.c shm_unlink.c
 OBJS=$(patsubst %.c,%.o, $(CSRC))
 
 all: $(LIBRT)
diff -Naur a/librt/shm_open.c b/librt/shm_open.c
--- a/librt/shm_open.c	1970-01-01 01:00:00.000000000 +0100
+++ b/librt/shm_open.c	2006-11-30 08:41:35.000000000 +0100
@@ -0,0 +1,48 @@
+/* shm_open - open a shared memory file */
+
+/* Copyright 2002, Red Hat Inc. */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <limits.h>
+
+int
+shm_open (const char *name, int oflag, mode_t mode)
+{
+  int fd;
+  char shm_name[PATH_MAX+20] = "/dev/shm/";
+
+  /* skip opening slash */
+  if (*name == '/')
+    ++name;
+
+  /* create special shared memory file name and leave enough space to
+     cause a path/name error if name is too long */
+  strlcpy (shm_name + 9, name, PATH_MAX + 10);
+
+  fd = open (shm_name, oflag, mode);
+
+  if (fd != -1)
+    {
+      /* once open we must add FD_CLOEXEC flag to file descriptor */
+      int flags = fcntl (fd, F_GETFD, 0);
+
+      if (flags >= 0)
+        {
+          flags |= FD_CLOEXEC;
+          flags = fcntl (fd, F_SETFD, flags);
+        }
+
+      /* on failure, just close file and give up */
+      if (flags == -1)
+        {
+          close (fd);
+          fd = -1;
+        }
+    }
+
+  return fd;
+}
diff -Naur a/librt/shm_unlink.c b/librt/shm_unlink.c
--- a/librt/shm_unlink.c	1970-01-01 01:00:00.000000000 +0100
+++ b/librt/shm_unlink.c	2006-11-30 08:41:35.000000000 +0100
@@ -0,0 +1,28 @@
+/* shm_unlink - remove a shared memory file */
+
+/* Copyright 2002, Red Hat Inc. */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <string.h>
+#include <limits.h>
+
+int
+shm_unlink (const char *name)
+{
+  int rc;
+  char shm_name[PATH_MAX+20] = "/dev/shm/";
+
+  /* skip opening slash */
+  if (*name == '/')
+    ++name;
+
+  /* create special shared memory file name and leave enough space to
+     cause a path/name error if name is too long */
+  strlcpy (shm_name + 9, name, PATH_MAX + 10);
+
+  rc = unlink (shm_name);
+
+  return rc;
+}
 



More information about the uClibc mailing list