Remove the global variable in telnetd_daemon

Change-Id: I264d10c0f141109ada6ac90afb2ba3e8cfad4404
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2020-02-03 16:54:54 +08:00 committed by patacongo
parent 1154735ca6
commit 28891ef27e

View File

@ -43,6 +43,7 @@
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@ -81,31 +82,6 @@ struct telnetd_s
* connection is accepted. */
};
/* This structure is used to passed information to telnet daemon when it
* started. It contains global information visable to all telnet daemons.
*/
struct telnetd_common_s
{
uint8_t ndaemons; /* The total number of daemons running */
sem_t startsem; /* Enforces one-at-a-time startup */
FAR struct telnetd_s *daemon; /* Describes the new daemon */
};
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/* This structure is used to passed information to telnet daemon when it
* started.
*/
static struct telnetd_common_s g_telnetdcommon;
/****************************************************************************
* Public Functions
****************************************************************************/
@ -125,7 +101,7 @@ static struct telnetd_common_s g_telnetdcommon;
*
****************************************************************************/
static int telnetd_daemon(int argc, char *argv[])
static int telnetd_daemon(int argc, FAR char *argv[])
{
FAR struct telnetd_s *daemon;
union
@ -159,9 +135,7 @@ static int telnetd_daemon(int argc, char *argv[])
/* Get daemon startup info */
daemon = g_telnetdcommon.daemon;
g_telnetdcommon.daemon = NULL;
sem_post(&g_telnetdcommon.startsem);
daemon = (FAR struct telnetd_s *)((uintptr_t)strtoul(argv[1], NULL, 0));
DEBUGASSERT(daemon != NULL);
#ifdef CONFIG_SCHED_HAVE_PARENT
@ -412,8 +386,9 @@ errout_with_socket:
int telnetd_start(FAR struct telnetd_config_s *config)
{
FAR struct telnetd_s *daemon;
FAR char *argv[2];
char arg0[16];
pid_t pid;
int ret;
/* Allocate a state structure for the new daemon */
@ -431,18 +406,14 @@ int telnetd_start(FAR struct telnetd_config_s *config)
daemon->stacksize = config->t_stacksize;
daemon->entry = config->t_entry;
/* Initialize the common structure if this is the first daemon */
if (g_telnetdcommon.ndaemons < 1)
{
sem_init(&g_telnetdcommon.startsem, 0, 0);
}
/* Then start the new daemon */
g_telnetdcommon.daemon = daemon;
snprintf(arg0, 16, "0x%" PRIxPTR, (uintptr_t)daemon);
argv[0] = arg0;
argv[1] = NULL;
pid = task_create("Telnet daemon", config->d_priority, config->d_stacksize,
telnetd_daemon, NULL);
telnetd_daemon, argv);
if (pid < 0)
{
int errval = errno;
@ -451,23 +422,6 @@ int telnetd_start(FAR struct telnetd_config_s *config)
return -errval;
}
/* Then wait for the daemon to start and complete the handshake */
do
{
ret = sem_wait(&g_telnetdcommon.startsem);
/* The only expected error condition is for sem_wait to be awakened by
* a receipt of a signal.
*/
if (ret < 0)
{
DEBUGASSERT(errno == EINTR || errno == ECANCELED);
}
}
while (ret < 0);
/* Return success */
return pid;