nuttx-apps/netutils/rexecd/rexecd.c
wangjianyu3 c503843e38 netutils/rexecd: add -t option to serve connections without a thread
By default rexecd spawns a detached worker thread per accepted connection
to allow concurrent sessions. Add a "-t" runtime option to instead handle
each connection inline in the main task, avoiding the per-connection
worker stack (CONFIG_NETUTILS_REXECD_STACKSIZE) allocation from the heap.
This matters on low-memory targets with limited free heap.

With "-t", connections are served strictly one at a time: a long-running
or interactive command blocks the accept loop until it finishes. Without
it, the existing thread-per-connection behaviour is unchanged.

Assisted-by: GitHubCopilot:claude-4.8-opus
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2026-06-30 13:46:37 +08:00

314 lines
7.5 KiB
C

/****************************************************************************
* apps/netutils/rexecd/rexecd.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <pthread.h>
#include <netpacket/rpmsg.h>
#include <sys/socket.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <poll.h>
#include <syslog.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define REXECD_PORT 512
#define REXECD_BUFSIZE 512
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static FAR void *doit(pthread_addr_t pvarg);
static int getstr(int fd, FAR char *buf);
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
static int getstr(int fd, FAR char *buf)
{
FAR char *end = buf;
int ret;
do
{
ret = read(fd, end, 1);
if (ret <= 0)
{
return ret;
}
end += ret;
}
while (*(end - 1));
return ret;
}
static FAR void *doit(pthread_addr_t pvarg)
{
char buf[REXECD_BUFSIZE];
struct pollfd fds[2];
pid_t pid;
int sock = (long)pvarg;
int ret;
/* we need to read err_sock, user and passwd, but ignore them */
getstr(sock, buf);
getstr(sock, buf);
getstr(sock, buf);
/* we need to read command */
getstr(sock, buf);
memset(fds, 0, sizeof(fds));
fds[0].events = POLLIN;
fds[1].fd = sock;
fds[1].events = POLLIN;
fds[0].fd = dpopen(buf, O_RDWR, &pid);
if (fds[0].fd < 0)
{
goto errout;
}
while (1)
{
ret = poll(fds, 2, -1);
if (ret <= 0)
{
continue;
}
if (fds[0].revents & POLLIN)
{
ret = read(fds[0].fd, buf, REXECD_BUFSIZE);
if (ret <= 0)
{
break;
}
ret = write(sock, buf, ret);
if (ret < 0)
{
break;
}
}
if (fds[1].revents & POLLIN)
{
ret = read(sock, buf, REXECD_BUFSIZE);
if (ret <= 0)
{
break;
}
ret = write(fds[0].fd, buf, ret);
if (ret < 0)
{
break;
}
}
if (((fds[0].revents | fds[1].revents) & POLLHUP) &&
((fds[0].revents | fds[1].revents) & POLLIN) == 0)
{
break;
}
}
dpclose(fds[0].fd, pid);
errout:
close(sock);
return NULL;
}
static void usage(FAR const char *progname)
{
fprintf(stderr, "Usage: %s [-4|-6|-r] [-t]\n", progname);
fprintf(stderr, "Remote Execution Daemon:\n"
" -4, Specify address family to AF_INET(default)\n"
" -6, Specify address family to AF_INET6\n"
" -r, Specify address family to AF_RPMSG\n"
" -t, Serve each connection inline without spawning a"
" per-connection thread (saves heap, no concurrency)\n");
exit(EXIT_FAILURE);
}
int main(int argc, FAR char **argv)
{
struct sockaddr_storage addr;
pthread_attr_t attr;
pthread_t tid;
bool threadless = false;
int family;
int option;
int serv;
int sock;
int ret;
family = AF_INET;
while ((option = getopt(argc, argv, "46rt")) != ERROR)
{
switch (option)
{
case '4':
family = AF_INET;
break;
case '6':
family = AF_INET6;
break;
case 'r':
family = AF_RPMSG;
break;
case 't':
threadless = true;
break;
default:
usage(argv[0]);
}
}
serv = socket(family, SOCK_STREAM, 0);
if (serv < 0)
{
return serv;
}
memset(&addr, 0, sizeof(addr));
switch (family)
{
default:
case AF_INET:
((FAR struct sockaddr_in *)&addr)->sin_family = AF_INET;
((FAR struct sockaddr_in *)&addr)->sin_port = htons(REXECD_PORT);
ret = sizeof(struct sockaddr_in);
break;
case AF_INET6:
((FAR struct sockaddr_in6 *)&addr)->sin6_family = AF_INET6;
((FAR struct sockaddr_in6 *)&addr)->sin6_port = htons(REXECD_PORT);
ret = sizeof(struct sockaddr_in6);
break;
case AF_RPMSG:
((FAR struct sockaddr_rpmsg *)&addr)->rp_family = AF_RPMSG;
snprintf(((FAR struct sockaddr_rpmsg *)&addr)->rp_name,
RPMSG_SOCKET_NAME_SIZE, "%d", htons(REXECD_PORT));
ret = sizeof(struct sockaddr_rpmsg);
}
ret = bind(serv, (FAR struct sockaddr *)&addr, ret);
if (ret < 0)
{
goto err_out;
}
ret = listen(serv, 5);
if (ret < 0)
{
goto err_out;
}
if (!threadless)
{
ret = pthread_attr_init(&attr);
if (ret != 0)
{
goto err_out;
}
ret = pthread_attr_setstacksize(&attr,
CONFIG_NETUTILS_REXECD_STACKSIZE);
if (ret != 0)
{
goto attr_out;
}
ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (ret != 0)
{
goto attr_out;
}
}
while (1)
{
sock = accept4(serv, NULL, 0, SOCK_CLOEXEC);
if (sock < 0)
{
if (errno == EINTR)
{
continue;
}
else
{
ret = sock;
goto attr_out;
}
}
if (threadless)
{
doit((pthread_addr_t)(long)sock);
}
else
{
ret = pthread_create(&tid, &attr, doit,
(pthread_addr_t)(long)sock);
if (ret < 0)
{
close(sock);
}
}
}
attr_out:
if (!threadless)
{
pthread_attr_destroy(&attr);
}
err_out:
syslog(LOG_ERR, "rexecd failed ret:%d errno:%d\n", ret, errno);
close(serv);
return ret;
}