[rtems commit] networking: socket to/from file descriptor

Sebastian Huber sebh at rtems.org
Tue Apr 3 14:20:41 UTC 2012


Module:    rtems
Branch:    master
Commit:    58f665583e8023f1245910e953eafff05121e782
Changeset: http://git.rtems.org/rtems/commit/?id=58f665583e8023f1245910e953eafff05121e782

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Apr  3 16:15:34 2012 +0200

networking: socket to/from file descriptor

 o Move rtems_bsdnet_fdToSocket() and rtems_bsdnet_makeFdForSocket() to
   "cpukit/libnetworking/rtems/rtems_syscall.c".
 o The rtems_bsdnet_makeFdForSocket() function is now static.
 o Check in rtems_bsdnet_fdToSocket() function that the file descriptor
   uses the socket handlers, otherwise an error status will be returned
   and errno set to ENOTSOCK.
 o New test libtests/syscall01.

---

 cpukit/libcsupport/Makefile.am                     |    4 -
 cpukit/libcsupport/src/libio_sockets.c             |   76 -------------
 cpukit/libnetworking/rtems/rtems_bsdnet_internal.h |    2 -
 cpukit/libnetworking/rtems/rtems_syscall.c         |   61 ++++++++++-
 testsuites/libtests/Makefile.am                    |    1 +
 testsuites/libtests/configure.ac                   |    1 +
 testsuites/libtests/syscall01/Makefile.am          |   19 ++++
 testsuites/libtests/syscall01/init.c               |  115 ++++++++++++++++++++
 testsuites/libtests/syscall01/syscall01.doc        |   11 ++
 testsuites/libtests/syscall01/syscall01.scn        |    2 +
 10 files changed, 207 insertions(+), 85 deletions(-)

diff --git a/cpukit/libcsupport/Makefile.am b/cpukit/libcsupport/Makefile.am
index 321eb29..ce2a745 100644
--- a/cpukit/libcsupport/Makefile.am
+++ b/cpukit/libcsupport/Makefile.am
@@ -60,10 +60,6 @@ BASE_FS_C_FILES = src/base_fs.c src/mount.c src/unmount.c src/libio.c \
     src/privateenv.c \
     src/open_dev_console.c src/__usrenv.c src/rtems_mkdir.c
 
-if LIBNETWORKING
-BASE_FS_C_FILES += src/libio_sockets.c
-endif
-
 TERMIOS_C_FILES = src/cfgetispeed.c src/cfgetospeed.c src/cfsetispeed.c \
     src/cfsetospeed.c src/tcgetattr.c src/tcsetattr.c src/tcdrain.c \
     src/tcflow.c src/tcflush.c src/tcgetpgrp.c src/tcsendbreak.c \
diff --git a/cpukit/libcsupport/src/libio_sockets.c b/cpukit/libcsupport/src/libio_sockets.c
deleted file mode 100644
index ff02e0f..0000000
--- a/cpukit/libcsupport/src/libio_sockets.c
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- *  This file contains the support infrastructure used to manage the
- *  table of integer style file descriptors used by the socket calls.
- *
- *  COPYRIGHT (c) 1989-1999.
- *  On-Line Applications Research Corporation (OAR).
- *
- *  The license and distribution terms for this file may be
- *  found in the file LICENSE in this distribution or at
- *  http://www.rtems.com/license/LICENSE.
- *
- *  $Id$
- */
-
-#if HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <rtems/libio_.h>
-#include <rtems/seterr.h>
-#include <rtems/rtems_bsdnet_internal.h>
-
-/*
- * Convert an RTEMS file descriptor to a BSD socket pointer.
- */
-
-struct socket *rtems_bsdnet_fdToSocket(
-  int fd
-)
-{
-  rtems_libio_t *iop;
-
-  /* same as rtems_libio_check_fd(_fd) but different return */
-  if ((uint32_t)fd >= rtems_libio_number_iops) {
-    errno = EBADF;
-    return NULL;
-  }
-  iop = &rtems_libio_iops[fd];
-
-  /* same as rtems_libio_check_is_open(iop) but different return */
-  if ((iop->flags & LIBIO_FLAGS_OPEN) == 0) {
-    errno = EBADF;
-    return NULL;
-  }
-
-  if (iop->data1 == NULL)
-    errno = EBADF;
-  return iop->data1;
-}
-
-/*
- * Create an RTEMS file descriptor for a socket
- */
-
-int rtems_bsdnet_makeFdForSocket(
-  void *so,
-  const rtems_filesystem_file_handlers_r *h
-)
-{
-  rtems_libio_t *iop;
-  int fd;
-
-  iop = rtems_libio_allocate();
-  if (iop == 0)
-      rtems_set_errno_and_return_minus_one( ENFILE );
-
-  fd = iop - rtems_libio_iops;
-  iop->flags |= LIBIO_FLAGS_WRITE | LIBIO_FLAGS_READ;
-  iop->data0 = fd;
-  iop->data1 = so;
-  iop->pathinfo.handlers = h;
-  iop->pathinfo.ops = &rtems_filesystem_operations_default;
-  iop->pathinfo.mt_entry = &rtems_filesystem_null_mt_entry;
-  rtems_filesystem_location_add_to_mt_entry(&iop->pathinfo);
-  return fd;
-}
diff --git a/cpukit/libnetworking/rtems/rtems_bsdnet_internal.h b/cpukit/libnetworking/rtems/rtems_bsdnet_internal.h
index 3e1ce72..8c43701 100644
--- a/cpukit/libnetworking/rtems/rtems_bsdnet_internal.h
+++ b/cpukit/libnetworking/rtems/rtems_bsdnet_internal.h
@@ -199,8 +199,6 @@ int ioctl (int, ioctl_command_t, ...);
 # error "Network event conflict"
 #endif
 
-int rtems_bsdnet_makeFdForSocket(
-    void *so, const rtems_filesystem_file_handlers_r *h);
 struct socket *rtems_bsdnet_fdToSocket(int fd);
 
 #ifdef __cplusplus
diff --git a/cpukit/libnetworking/rtems/rtems_syscall.c b/cpukit/libnetworking/rtems/rtems_syscall.c
index 9935a69..0a4ae91 100644
--- a/cpukit/libnetworking/rtems/rtems_syscall.c
+++ b/cpukit/libnetworking/rtems/rtems_syscall.c
@@ -13,7 +13,7 @@
 #include <errno.h>
 
 #include <rtems.h>
-#include <rtems/libio.h>
+#include <rtems/libio_.h>
 #include <rtems/error.h>
 #include <rtems/rtems_bsdnet.h>
 
@@ -44,6 +44,61 @@ ssize_t	recv(int, void *, size_t, int);
 static const rtems_filesystem_file_handlers_r socket_handlers;
 
 /*
+ * Convert an RTEMS file descriptor to a BSD socket pointer.
+ */
+struct socket *
+rtems_bsdnet_fdToSocket (int fd)
+{
+  rtems_libio_t *iop;
+
+  /* same as rtems_libio_check_fd(_fd) but different return */
+  if ((uint32_t)fd >= rtems_libio_number_iops) {
+    errno = EBADF;
+    return NULL;
+  }
+  iop = &rtems_libio_iops[fd];
+
+  /* same as rtems_libio_check_is_open(iop) but different return */
+  if ((iop->flags & LIBIO_FLAGS_OPEN) == 0) {
+    errno = EBADF;
+    return NULL;
+  }
+
+  if (iop->pathinfo.handlers != &socket_handlers) {
+    errno = ENOTSOCK;
+    return NULL;
+  }
+
+  if (iop->data1 == NULL)
+    errno = EBADF;
+  return iop->data1;
+}
+
+/*
+ * Create an RTEMS file descriptor for a socket
+ */
+static int
+rtems_bsdnet_makeFdForSocket (void *so)
+{
+  rtems_libio_t *iop;
+  int fd;
+
+  iop = rtems_libio_allocate();
+  if (iop == 0)
+      rtems_set_errno_and_return_minus_one( ENFILE );
+
+  fd = iop - rtems_libio_iops;
+  iop->flags |= LIBIO_FLAGS_WRITE | LIBIO_FLAGS_READ;
+  iop->data0 = fd;
+  iop->data1 = so;
+  iop->pathinfo.handlers = &socket_handlers;
+  iop->pathinfo.ops = &rtems_filesystem_operations_default;
+  iop->pathinfo.mt_entry = &rtems_filesystem_null_mt_entry;
+  rtems_filesystem_location_add_to_mt_entry(&iop->pathinfo);
+  return fd;
+}
+
+/*
  * Package system call argument into mbuf.
  */
 static int
@@ -82,7 +137,7 @@ socket (int domain, int type, int protocol)
 	rtems_bsdnet_semaphore_obtain ();
 	error = socreate(domain, &so, type, protocol, NULL);
 	if (error == 0) {
-		fd = rtems_bsdnet_makeFdForSocket (so, &socket_handlers);
+		fd = rtems_bsdnet_makeFdForSocket (so);
 		if (fd < 0)
 			soclose (so);
 	}
@@ -233,7 +288,7 @@ accept (int s, struct sockaddr *name, int *namelen)
 	TAILQ_REMOVE(&head->so_comp, so, so_list);
 	head->so_qlen--;
 
-	fd = rtems_bsdnet_makeFdForSocket (so, &socket_handlers);
+	fd = rtems_bsdnet_makeFdForSocket (so);
 	if (fd < 0) {
 		TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
 		head->so_qlen++;
diff --git a/testsuites/libtests/Makefile.am b/testsuites/libtests/Makefile.am
index 3bcd8c2..9521825 100644
--- a/testsuites/libtests/Makefile.am
+++ b/testsuites/libtests/Makefile.am
@@ -21,6 +21,7 @@ SUBDIRS += bspcmdline01 cpuuse devfs01 devfs02 devfs03 devfs04 \
 
 if NETTESTS
 SUBDIRS += ftp01
+SUBDIRS += syscall01
 endif
 
 include $(top_srcdir)/../automake/subdirs.am
diff --git a/testsuites/libtests/configure.ac b/testsuites/libtests/configure.ac
index ec022bc..d1ce374 100644
--- a/testsuites/libtests/configure.ac
+++ b/testsuites/libtests/configure.ac
@@ -43,6 +43,7 @@ AM_CONDITIONAL(NETTESTS,test "$rtems_cv_RTEMS_NETWORKING" = "yes")
 
 # Explicitly list all Makefiles here
 AC_CONFIG_FILES([Makefile
+syscall01/Makefile
 flashdisk01/Makefile
 block01/Makefile
 block02/Makefile
diff --git a/testsuites/libtests/syscall01/Makefile.am b/testsuites/libtests/syscall01/Makefile.am
new file mode 100644
index 0000000..126cd30
--- /dev/null
+++ b/testsuites/libtests/syscall01/Makefile.am
@@ -0,0 +1,19 @@
+rtems_tests_PROGRAMS = syscall01
+syscall01_SOURCES = init.c
+
+dist_rtems_tests_DATA = syscall01.scn syscall01.doc
+
+include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP at .cfg
+include $(top_srcdir)/../automake/compile.am
+include $(top_srcdir)/../automake/leaf.am
+
+AM_CPPFLAGS += -I$(top_srcdir)/../support/include
+
+LINK_OBJS = $(syscall01_OBJECTS)
+LINK_LIBS = $(syscall01_LDLIBS)
+
+syscall01$(EXEEXT): $(syscall01_OBJECTS) $(syscall01_DEPENDENCIES)
+	@rm -f syscall01$(EXEEXT)
+	$(make-exe)
+
+include $(top_srcdir)/../automake/local.am
diff --git a/testsuites/libtests/syscall01/init.c b/testsuites/libtests/syscall01/init.c
new file mode 100644
index 0000000..b438a93
--- /dev/null
+++ b/testsuites/libtests/syscall01/init.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
+ *
+ *  embedded brains GmbH
+ *  Obere Lagerstr. 30
+ *  82178 Puchheim
+ *  Germany
+ *  <rtems at embedded-brains.de>
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ */
+
+#ifdef HAVE_CONFIG_H
+  #include "config.h"
+#endif
+
+#include "tmacros.h"
+
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <rtems/libio.h>
+#include <rtems/rtems_bsdnet.h>
+
+static const char open_driver_path [] = "/dev/open_driver";
+
+struct rtems_bsdnet_config rtems_bsdnet_config;
+
+static void test(void)
+{
+  int rv;
+  char buf [1];
+  ssize_t n;
+  int fd = open(open_driver_path, O_RDWR);
+  rtems_test_assert(fd >= 0);
+
+  n = send(fd, buf, sizeof(buf), 0);
+  rtems_test_assert(n == -1);
+  rtems_test_assert(errno == ENOTSOCK);
+
+  n = recv(fd, buf, sizeof(buf), 0);
+  rtems_test_assert(n == -1);
+  rtems_test_assert(errno == ENOTSOCK);
+
+  rv = close(fd);
+  rtems_test_assert(rv == 0);
+}
+
+static void Init(rtems_task_argument arg)
+{
+  int rv;
+
+  puts("\n\n*** TEST SYSCALL 1 ***");
+
+  rv = rtems_bsdnet_initialize_network();
+  rtems_test_assert(rv == 0);
+
+  test();
+
+  puts("*** END OF TEST SYSCALL 1 ***");
+
+  rtems_test_exit(0);
+}
+
+static rtems_device_driver open_driver_initialize(
+  rtems_device_major_number major,
+  rtems_device_minor_number minor,
+  void *arg
+)
+{
+  rtems_status_code sc = rtems_io_register_name(open_driver_path, major, 0);
+  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
+
+  return RTEMS_SUCCESSFUL;
+}
+
+static rtems_device_driver open_driver_open(
+  rtems_device_major_number major,
+  rtems_device_minor_number minor,
+  void *arg
+)
+{
+  rtems_libio_open_close_args_t *oc = arg;
+
+  oc->iop->data0 = 1;
+  oc->iop->data1 = (void *) 1;
+
+  return RTEMS_SUCCESSFUL;
+}
+
+#define OPEN_DRIVER { \
+  .initialization_entry = open_driver_initialize, \
+  .open_entry = open_driver_open \
+}
+
+#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
+#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
+#define CONFIGURE_APPLICATION_EXTRA_DRIVERS OPEN_DRIVER
+
+#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
+
+#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 4
+
+#define CONFIGURE_MAXIMUM_TASKS 2
+
+#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
+
+#define CONFIGURE_INIT
+
+#include <rtems/confdefs.h>
diff --git a/testsuites/libtests/syscall01/syscall01.doc b/testsuites/libtests/syscall01/syscall01.doc
new file mode 100644
index 0000000..6ae2ec3
--- /dev/null
+++ b/testsuites/libtests/syscall01/syscall01.doc
@@ -0,0 +1,11 @@
+This file describes the directives and concepts tested by this test set.
+
+test set name: syscall01
+
+directives:
+
+  TBD
+
+concepts:
+
+  TBD
diff --git a/testsuites/libtests/syscall01/syscall01.scn b/testsuites/libtests/syscall01/syscall01.scn
new file mode 100644
index 0000000..598a0f4
--- /dev/null
+++ b/testsuites/libtests/syscall01/syscall01.scn
@@ -0,0 +1,2 @@
+*** TEST SYSCALL 1 ***
+*** END OF TEST SYSCALL 1 ***




More information about the vc mailing list