[PATCH 8/9] telnetd: Always build telnet daemon

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Apr 30 09:48:16 UTC 2018


Add support for libbsd initialization.

Update #3419.
---
 cpukit/include/rtems/telnetd.h |  5 +++
 cpukit/telnetd/Makefile.am     |  5 +--
 cpukit/telnetd/telnetd-init.c  | 18 ++++++++++
 cpukit/telnetd/telnetd.c       | 79 +++++++++++++++++++++++++++---------------
 4 files changed, 76 insertions(+), 31 deletions(-)
 create mode 100644 cpukit/telnetd/telnetd-init.c

diff --git a/cpukit/include/rtems/telnetd.h b/cpukit/include/rtems/telnetd.h
index a5c8a187e3..19512fbe9c 100644
--- a/cpukit/include/rtems/telnetd.h
+++ b/cpukit/include/rtems/telnetd.h
@@ -88,6 +88,11 @@ typedef struct {
 } rtems_telnetd_config_table;
 
 /**
+ * @brief Start the Telnet subsystem with the provided configuration.
+ */
+rtems_status_code rtems_telnetd_start(const rtems_telnetd_config_table *config);
+
+/**
  * @brief Telnet configuration.
  *
  * The application must provide this configuration table.  It is used by
diff --git a/cpukit/telnetd/Makefile.am b/cpukit/telnetd/Makefile.am
index cc0ad1d9ba..7051b368c3 100644
--- a/cpukit/telnetd/Makefile.am
+++ b/cpukit/telnetd/Makefile.am
@@ -1,6 +1,5 @@
 include $(top_srcdir)/automake/compile.am
 
-if LIBNETWORKING
 if LIBSHELL
 project_lib_LIBRARIES = libtelnetd.a
 
@@ -8,10 +7,8 @@ $(PROJECT_LIB)/libtelnetd.a: libtelnetd.a
 	$(INSTALL_DATA) $< $(PROJECT_LIB)/libtelnetd.a
 TMPINSTALL_FILES = $(PROJECT_LIB)/libtelnetd.a
 
-libtelnetd_a_SOURCES = check_passwd.c des.c pty.c telnetd.c
+libtelnetd_a_SOURCES = check_passwd.c des.c pty.c telnetd.c telnetd-init.c
 libtelnetd_a_CPPFLAGS = $(AM_CPPFLAGS)
 endif
-endif
-
 
 include $(top_srcdir)/automake/local.am
diff --git a/cpukit/telnetd/telnetd-init.c b/cpukit/telnetd/telnetd-init.c
new file mode 100644
index 0000000000..760db41fcc
--- /dev/null
+++ b/cpukit/telnetd/telnetd-init.c
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2018 embedded brains GmbH.  All rights reserved.
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.org/license/LICENSE.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems/telnetd.h>
+
+int rtems_initialize_telnetd( void )
+{
+  return rtems_telnetd_start( &rtems_telnetd_config );
+}
diff --git a/cpukit/telnetd/telnetd.c b/cpukit/telnetd/telnetd.c
index 195ec979c3..43e00365b5 100644
--- a/cpukit/telnetd/telnetd.c
+++ b/cpukit/telnetd/telnetd.c
@@ -57,7 +57,10 @@
 
 #include <rtems/userenv.h>
 #include <rtems/error.h>
+
+#ifdef RTEMS_NETWORKING
 #include <rtems/rtems_bsdnet.h>
+#endif
 
 #define PARANOIA
 
@@ -87,9 +90,16 @@ rtems_id telnetd_dflt_spawn(
 );
 
 /***********************************************************/
-static rtems_id telnetd_task_id = RTEMS_ID_NONE;
+static rtems_telnetd_config_table *telnetd_config;
+static rtems_id                    telnetd_task_id;
 
-rtems_id (*telnetd_spawn_task)(
+/*
+ * chrisj: this variable was global and with no declared interface in a header
+ *         file and with no means to set it so I have stopped it being global;
+ *         if this breaks any user they will have be to provide a formal
+ *         interface to get this change reverted.
+ */
+static const rtems_id (*telnetd_spawn_task)(
   const char *,
   unsigned,
   unsigned,
@@ -207,24 +217,24 @@ rtems_task_telnetd(void *task_argument)
   };
 
   /* we don't redirect stdio as this probably
-   * was started from the console anyways..
+   * was started from the console anyway ..
    */
   do {
-    if (rtems_telnetd_config.keep_stdio) {
+    if (telnetd_config->keep_stdio) {
       bool start = true;
       char device_name [32];
 
       ttyname_r( 1, device_name, sizeof( device_name));
-      if (rtems_telnetd_config.login_check != NULL) {
+      if (telnetd_config->login_check != NULL) {
         start = rtems_shell_login_prompt(
           stdin,
           stderr,
           device_name,
-          rtems_telnetd_config.login_check
+          telnetd_config->login_check
         );
       }
       if (start) {
-        rtems_telnetd_config.command( device_name, arg->arg);
+        telnetd_config->command( device_name, arg->arg);
       } else {
         syslog(
           LOG_AUTHPRIV | LOG_WARNING,
@@ -244,13 +254,13 @@ rtems_task_telnetd(void *task_argument)
       arg = malloc( sizeof(*arg) );
 
       arg->devname = devname;
-      arg->arg = rtems_telnetd_config.arg;
+      arg->arg = telnetd_config->arg;
       strncpy(arg->peername, peername, sizeof(arg->peername));
 
       telnetd_task_id = telnetd_spawn_task(
         devname,
-        rtems_telnetd_config.priority,
-        rtems_telnetd_config.stack_size,
+        telnetd_config->priority,
+        telnetd_config->stack_size,
         spawned_shell,
         arg
       );
@@ -287,55 +297,70 @@ rtems_task_telnetd(void *task_argument)
   telnetd_task_id = RTEMS_ID_NONE;
 }
 
-rtems_status_code rtems_telnetd_initialize( void)
+rtems_status_code rtems_telnetd_start(const rtems_telnetd_config_table* config)
 {
-  if (telnetd_task_id != RTEMS_ID_NONE) {
+  if (telnetd_config != NULL) {
     fprintf(stderr, "telnetd already started\n");
     return RTEMS_RESOURCE_IN_USE;
   }
 
-  if (rtems_telnetd_config.command == NULL) {
+  if (config->command == NULL) {
     fprintf(stderr, "telnetd setup with invalid command\n");
     return RTEMS_IO_ERROR;
   }
 
+  telnetd_config = calloc(1, sizeof(*telnetd_config));
+  if (telnetd_config == NULL) {
+    fprintf(stderr, "telnetd cannot alloc telnetd config table\n");
+    return RTEMS_NO_MEMORY;
+  }
+
+
   if ( !telnet_pty_initialize() ) {
     fprintf(stderr, "telnetd cannot initialize PTY driver\n");
+    free(telnetd_config);
+    telnetd_config = NULL;
     return RTEMS_IO_ERROR;
   }
 
+  *telnetd_config = *config;
+
   /* Check priority */
-  if (rtems_telnetd_config.priority <= 0) {
-    rtems_telnetd_config.priority = rtems_bsdnet_config.network_task_priority;
+#ifdef RTEMS_NETWORKING
+  if (telnetd_config->priority <= 0) {
+    telnetd_config->priority = rtems_bsdnet_config.network_task_priority;
   }
-  if (rtems_telnetd_config.priority < 2) {
-    rtems_telnetd_config.priority = 100;
+#endif
+  if (telnetd_config->priority < 2) {
+    telnetd_config->priority = 100;
   }
 
   /* Check stack size */
-  if (rtems_telnetd_config.stack_size <= 0) {
-    rtems_telnetd_config.stack_size = (size_t)32 * 1024;
+  if (telnetd_config->stack_size <= 0) {
+    telnetd_config->stack_size = (size_t)32 * 1024;
   }
 
   /* Spawn task */
   telnetd_task_id = telnetd_spawn_task(
     "TNTD",
-    rtems_telnetd_config.priority,
-    rtems_telnetd_config.stack_size,
+    telnetd_config->priority,
+    telnetd_config->stack_size,
     rtems_task_telnetd,
     0
   );
   if (telnetd_task_id == RTEMS_ID_NONE) {
+    free(telnetd_config);
+    telnetd_config = NULL;
     return RTEMS_IO_ERROR;
   }
 
   /* Print status */
-  if (!rtems_telnetd_config.keep_stdio) {
+  if (!telnetd_config->keep_stdio) {
     fprintf(
       stderr,
       "telnetd started with stacksize = %u and priority = %d\n",
-      (unsigned) rtems_telnetd_config.stack_size,
-      (unsigned) rtems_telnetd_config.priority
+      (unsigned) telnetd_config->stack_size,
+      (unsigned) telnetd_config->priority
     );
   }
 
@@ -389,17 +414,17 @@ spawned_shell(void *targ)
   #endif
 
   /* call their routine */
-  if (rtems_telnetd_config.login_check != NULL) {
+  if (telnetd_config->login_check != NULL) {
     start = rtems_shell_login_prompt(
       stdin,
       stderr,
       arg->devname,
-      rtems_telnetd_config.login_check
+      telnetd_config->login_check
     );
     login_failed = !start;
   }
   if (start) {
-    rtems_telnetd_config.command( arg->devname, arg->arg);
+    telnetd_config->command( arg->devname, arg->arg);
   }
 
   stdin  = ostd[0];
-- 
2.12.3




More information about the devel mailing list