nuttx-apps/system/nxinit/test/test_nxinit.h
wangjianyu3 bc0ed23a5d system/nxinit: add a per-service "console" option
Services started by nxinit (e.g. "sh") do not open a console device on
their own, unlike nsh_main, which explicitly does so via
nsh_consolemain()/nsh_waitusbready() for USB gadget consoles
(CDC-ACM/PL2303). When such a board switches its top-level init from
nsh_main to nxinit, no code path ever registers/connects the USB
console gadget, and a service that just execs a plain "sh" inherits
whatever (invalid, for a USB gadget console not yet opened at the time
the idle task file descriptors are set up) stdio nxinit itself has.

Add a "console [<device>]" service option: a service declared with it
gets the given device (CONFIG_SYSTEM_NXINIT_CONSOLE_DEV, "/dev/console"
by default, if no device is given) opened and dup'd onto its stdin,
stdout and stderr via posix_spawn_file_actions before it is spawned.
This does not depend on nsh being enabled at all.

For a USB gadget console, the device does not exist until the gadget
is actually registered; boards using one are expected to bring it up
themselves before any service using "console" is started (e.g. via an
"exec -- sercon" action in their init.rc, since apps/system/cdcacm
already implements exactly that registration step and does not depend
on nxinit or nsh either).

console_file_actions() runs after the previous commit's time_started
update, so a failure to build the console's file actions is covered by
the same up to date timestamp - no separate clock_gettime() call is
needed on this failure path.

Covered by a new unit test, test_nxinit_service_console_option, that
exercises the option with and without an explicit device, and confirms
services without the option are left untouched.

Assisted-by: Kiro:claude-sonnet-5
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2026-09-10 23:12:06 +08:00

82 lines
3.8 KiB
C

/****************************************************************************
* apps/system/nxinit/test/test_nxinit.h
*
* 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.
*
****************************************************************************/
#ifndef __APPS_SYSTEM_NXINIT_TEST_TEST_NXINIT_H
#define __APPS_SYSTEM_NXINIT_TEST_TEST_NXINIT_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/compiler.h>
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: test_nxinit_group_setup
****************************************************************************/
int test_nxinit_group_setup(FAR void **state);
/****************************************************************************
* Name: test_nxinit_group_teardown
****************************************************************************/
int test_nxinit_group_teardown(FAR void **state);
/****************************************************************************
* Name: test_nxinit_parser_*
****************************************************************************/
void test_nxinit_parser_arguments_spaces(FAR void **state);
void test_nxinit_parser_arguments_quoted(FAR void **state);
void test_nxinit_parser_arguments_dashdash_separator(FAR void **state);
void test_nxinit_parser_arguments_long_option(FAR void **state);
void test_nxinit_parser_arguments_truncate(FAR void **state);
void test_nxinit_parser_config_sections(FAR void **state);
void test_nxinit_parser_config_skip_blank_lines(FAR void **state);
void test_nxinit_parser_config_unknown_section(FAR void **state);
void test_nxinit_parser_config_line_too_long(FAR void **state);
void test_nxinit_parser_config_line_crosses_boundary(FAR void **state);
void test_nxinit_parser_config_buffer_crosses_boundary(FAR void **state);
/****************************************************************************
* Name: test_nxinit_action_*
****************************************************************************/
void test_nxinit_action_event_match_exact(FAR void **state);
void test_nxinit_action_event_match_invert(FAR void **state);
void test_nxinit_action_event_match_fnmatch(FAR void **state);
void test_nxinit_action_event_and_semantics(FAR void **state);
/****************************************************************************
* Name: test_nxinit_service_*
****************************************************************************/
void test_nxinit_service_duplicate_conflict(FAR void **state);
void test_nxinit_service_override_replaces_duplicate(FAR void **state);
void test_nxinit_service_args_max_boundary(FAR void **state);
void test_nxinit_service_console_option(FAR void **state);
#endif /* __APPS_SYSTEM_NXINIT_TEST_TEST_NXINIT_H */