[PATCH 1/2] dhcpcd: Add rtems_dhcpcd_start()

Sebastian Huber sebastian.huber at embedded-brains.de
Wed May 2 07:47:15 UTC 2018


---
 dhcpcd/dhcpcd.c                                    | 46 +++++++++++++++
 dhcpcd/namespace.h                                 |  4 ++
 rtemsbsd/include/rtems/dhcpcd.h                    | 66 ++++++++++++++++++++++
 .../include/rtems/bsd/test/default-network-init.h  | 37 ++----------
 4 files changed, 121 insertions(+), 32 deletions(-)
 create mode 100644 rtemsbsd/include/rtems/dhcpcd.h

diff --git a/dhcpcd/dhcpcd.c b/dhcpcd/dhcpcd.c
index 8074f3fc3..7cdd3c066 100644
--- a/dhcpcd/dhcpcd.c
+++ b/dhcpcd/dhcpcd.c
@@ -1110,8 +1110,54 @@ signal_init(void (*func)(int, siginfo_t *, void *), sigset_t *oldset)
 #endif
 
 #ifdef __rtems__
+#include <rtems/dhcpcd.h>
 #include <rtems/libio.h>
 
+#include <assert.h>
+
+rtems_recursive_mutex dhcpcd_mutex =
+    RTEMS_RECURSIVE_MUTEX_INITIALIZER("dhcpcd");
+
+static bool dhcpcd_initialized;
+
+static void
+dhcpcd_task(rtems_task_argument arg)
+{
+	char *dhcpcd[] = { "dhcpcd", NULL };
+
+	(void)arg;
+
+	rtems_bsd_command_dhcpcd(RTEMS_BSD_ARGC(dhcpcd), dhcpcd);
+	assert(0);
+}
+
+rtems_status_code
+rtems_dhcpcd_start(rtems_task_priority priority)
+{
+	rtems_status_code sc;
+
+	rtems_recursive_mutex_lock(&dhcpcd_mutex);
+
+	if (!dhcpcd_initialized) {
+		rtems_id id;
+
+		sc = rtems_task_create(rtems_build_name('D', 'H', 'C', 'P'), priority,
+		    2 * RTEMS_MINIMUM_STACK_SIZE, RTEMS_DEFAULT_MODES,
+		    RTEMS_FLOATING_POINT, &id);
+		if (sc == RTEMS_SUCCESSFUL) {
+			dhcpcd_initialized = true;
+
+			sc = rtems_task_start(id, dhcpcd_task, 0);
+			assert(sc == RTEMS_SUCCESSFUL);
+		}
+	} else {
+		sc = RTEMS_INCORRECT_STATE;
+	}
+
+	rtems_recursive_mutex_unlock(&dhcpcd_mutex);
+	return sc;
+}
+
 struct getopt_data dhcpcd_getopt_data;
 
 static int
diff --git a/dhcpcd/namespace.h b/dhcpcd/namespace.h
index 7299c7d0b..efff909ad 100644
--- a/dhcpcd/namespace.h
+++ b/dhcpcd/namespace.h
@@ -1,3 +1,7 @@
+#include <rtems/thread.h>
+
+extern rtems_recursive_mutex dhcpcd_mutex;
+
 #define add_options dhcpcd_add_options
 #define arp_announce dhcpcd_arp_announce
 #define arp_probe dhcpcd_arp_probe
diff --git a/rtemsbsd/include/rtems/dhcpcd.h b/rtemsbsd/include/rtems/dhcpcd.h
new file mode 100644
index 000000000..6bc5e023d
--- /dev/null
+++ b/rtemsbsd/include/rtems/dhcpcd.h
@@ -0,0 +1,66 @@
+/**
+ * @file
+ *
+ * @ingroup rtems_bsd
+ *
+ * @brief TODO.
+ */
+
+/*
+ * Copyright (c) 2018 embedded brains GmbH.  All rights reserved.
+ *
+ *  embedded brains GmbH
+ *  Dornierstr. 4
+ *  82178 Puchheim
+ *  Germany
+ *  <rtems at embedded-brains.de>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _RTEMS_DHCPCD_H_
+#define _RTEMS_DHCPCD_H_
+
+#include <rtems.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/**
+ * @brief Starts the DHCP client (dhcpcd).
+ *
+ * @retval RTEMS_SUCCESSFUL Successful operation.
+ * @retval RTEMS_INCORRECT_STATE The DHCP client runs already.
+ * @retval RTEMS_TOO_MANY No task control block available.
+ * @retval RTEMS_UNSATISFIED Not enough resources to create task.
+ * @retval RTEMS_INVALID_PRIORITY Invalid task priority.
+ */
+rtems_status_code rtems_dhcpcd_start(rtems_task_priority priority);
+
+/** @} */
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* _RTEMS_DHCPCD_H_ */
diff --git a/testsuite/include/rtems/bsd/test/default-network-init.h b/testsuite/include/rtems/bsd/test/default-network-init.h
index d26df03cd..89274fce0 100644
--- a/testsuite/include/rtems/bsd/test/default-network-init.h
+++ b/testsuite/include/rtems/bsd/test/default-network-init.h
@@ -47,6 +47,7 @@
 #include <rtems/printer.h>
 #include <rtems/stackchk.h>
 #include <rtems/bsd/bsd.h>
+#include <rtems/dhcpcd.h>
 
 #if defined(DEFAULT_NETWORK_DHCPCD_ENABLE) && \
     !defined(DEFAULT_NETWORK_NO_STATIC_IFCONFIG)
@@ -125,22 +126,16 @@ default_network_route_hwif0(char *ifname)
 }
 #endif
 
-#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
 static void
-default_network_dhcpcd_task(rtems_task_argument arg)
+default_network_dhcpcd(void)
 {
+#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
 	static const char default_cfg[] = "clientid libbsd test client\n";
-	int exit_code;
-	char *dhcpcd[] = {
-		"dhcpcd",
-		NULL
-	};
+	rtems_status_code sc;
 	int fd;
 	int rv;
 	ssize_t n;
 
-	(void)arg;
-
 	fd = open("/etc/dhcpcd.conf", O_CREAT | O_WRONLY,
 	    S_IRWXU | S_IRWXG | S_IRWXO);
 	assert(fd >= 0);
@@ -158,29 +153,7 @@ default_network_dhcpcd_task(rtems_task_argument arg)
 	rv = close(fd);
 	assert(rv == 0);
 
-	exit_code = rtems_bsd_command_dhcpcd(RTEMS_BSD_ARGC(dhcpcd), dhcpcd);
-	assert(exit_code == EXIT_SUCCESS);
-}
-#endif
-
-static void
-default_network_dhcpcd(void)
-{
-#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
-	rtems_status_code sc;
-	rtems_id id;
-
-	sc = rtems_task_create(
-		rtems_build_name('D', 'H', 'C', 'P'),
-		RTEMS_MAXIMUM_PRIORITY - 1,
-		2 * RTEMS_MINIMUM_STACK_SIZE,
-		RTEMS_DEFAULT_MODES,
-		RTEMS_FLOATING_POINT,
-		&id
-	);
-	assert(sc == RTEMS_SUCCESSFUL);
-
-	sc = rtems_task_start(id, default_network_dhcpcd_task, 0);
+	sc = rtems_dhcpcd_start(RTEMS_MAXIMUM_PRIORITY - 1);
 	assert(sc == RTEMS_SUCCESSFUL);
 #endif
 }
-- 
2.12.3



More information about the devel mailing list