Skip to content

Commit 4ff5356

Browse files
system/nxinit: add fallback service option
Add an SVC_FALLBACK flag and a "fallback" service option, the semantic opposite of "override", to resolve same-name service conflicts: - override: the new definition replaces the old one. - fallback: the definition marked fallback yields to the other one, so a board-level init.rc can provide a default service that is silently dropped when another init.rc defines a service with the same name (and vice versa). If neither flag is set, duplicate service names still produce -EEXIST as before. init_service_check() now resolves the conflict by, in order: override wins, otherwise the fallback side is disabled, otherwise error. Assisted-by: opencode-agent/claude-opus-4-8 Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
1 parent 63ff083 commit 4ff5356

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

system/nxinit/service.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ static int option_gentle_kill(FAR struct service_manager_s *sm,
9696
int argc, FAR char **argv);
9797
static int option_restart_period(FAR struct service_manager_s *sm,
9898
int argc, FAR char **argv);
99+
static int option_fallback(FAR struct service_manager_s *sm,
100+
int argc, FAR char **argv);
99101
static int option_override(FAR struct service_manager_s *sm,
100102
int argc, FAR char **argv);
101103
static int option_oneshot(FAR struct service_manager_s *sm,
@@ -114,6 +116,7 @@ static const struct cmd_map_s g_option[] =
114116
{"class", 2, NXINIT_ACTION_CMD_ARGS_MAX, option_class},
115117
{"gentle_kill", 1, 1, option_gentle_kill},
116118
{"restart_period", 2, 2, option_restart_period},
119+
{"fallback", 1, 1, option_fallback},
117120
{"override", 1, 1, option_override},
118121
{"oneshot", 1, 1, option_oneshot},
119122
#ifdef CONFIG_BOARDCTL_RESET
@@ -131,6 +134,7 @@ static const struct flag_str_s g_flag_str[] =
131134
{SVC_GENTLE_KILL, "gentle_kill"},
132135
{SVC_REMOVE, "remove"},
133136
{SVC_SIGKILL, "sigkill"},
137+
{SVC_FALLBACK, "fallback"},
134138
{SVC_OVERRIDE, "override"},
135139
};
136140
#endif
@@ -253,6 +257,16 @@ static int option_restart_period(FAR struct service_manager_s *sm,
253257
return 0;
254258
}
255259

260+
static int option_fallback(FAR struct service_manager_s *sm,
261+
int argc, FAR char **argv)
262+
{
263+
FAR struct service_s *s = list_last_entry(&sm->services, struct service_s,
264+
node);
265+
266+
add_flags(s, SVC_FALLBACK);
267+
return 0;
268+
}
269+
256270
static int option_override(FAR struct service_manager_s *sm,
257271
int argc, FAR char **argv)
258272
{
@@ -649,17 +663,31 @@ int init_service_check(FAR const struct parser_s *parser)
649663
{
650664
if (!strcmp(s->argv[1], tmp->argv[1]))
651665
{
652-
if (!check_flags(tmp, SVC_OVERRIDE))
666+
if (check_flags(tmp, SVC_OVERRIDE))
667+
{
668+
init_info("override: remove old service '%s'",
669+
s->argv[1]);
670+
add_flags(s, SVC_DISABLED | SVC_REMOVE);
671+
}
672+
else if (check_flags(tmp, SVC_FALLBACK))
673+
{
674+
init_info("fallback: ignore new service '%s'",
675+
tmp->argv[1]);
676+
add_flags(tmp, SVC_DISABLED | SVC_REMOVE);
677+
}
678+
else if (check_flags(s, SVC_FALLBACK))
679+
{
680+
init_info("fallback: replace old service '%s'",
681+
s->argv[1]);
682+
add_flags(s, SVC_DISABLED | SVC_REMOVE);
683+
}
684+
else
653685
{
654686
init_err("Redefined service '%s'", tmp->argv[1]);
655687
init_dump_service(s);
656688
init_dump_service(tmp);
657689
return -EEXIST;
658690
}
659-
660-
init_info("Remove duplicate definition of service '%s'",
661-
tmp->argv[1]);
662-
add_flags(s, SVC_DISABLED | SVC_REMOVE);
663691
}
664692
}
665693
}

system/nxinit/service.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
/* Flags below are new added.
5555
*/
5656

57+
/* Fallback: silently ignored if a service with the same name exists */
58+
59+
#define SVC_FALLBACK (1 << 28)
60+
5761
/* Override the previous definition for a service with the same name */
5862

5963
#define SVC_OVERRIDE (1 << 29)

0 commit comments

Comments
 (0)