hartmannathan commented on code in PR #3192: URL: https://github.com/apache/nuttx-apps/pull/3192#discussion_r2449726164
########## system/init/action.c: ########## @@ -0,0 +1,346 @@ +/**************************************************************************** + * apps/system/init/action.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 <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sys/types.h> +#include <sys/param.h> +#include <unistd.h> + +#include "action.h" +#include "builtin.h" +#include "init.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Maximum number of parameters for the action trigger. + * Format: `on <trigger>` + */ + +#define ACTION_ARGUMENTS_MAX 2 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action); +#else +# define init_dump_action(a) +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action) +{ + FAR struct action_cmd_s *cmd; + + init_debug("action %p", action); Review Comment: Similar to @acassis suggestion elsewhere, starting new messages with capital letters makes it easier to see in the logs. ```suggestion init_debug("Action %p", action); ``` ########## system/init/action.c: ########## @@ -0,0 +1,346 @@ +/**************************************************************************** + * apps/system/init/action.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 <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sys/types.h> +#include <sys/param.h> +#include <unistd.h> + +#include "action.h" +#include "builtin.h" +#include "init.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Maximum number of parameters for the action trigger. + * Format: `on <trigger>` + */ + +#define ACTION_ARGUMENTS_MAX 2 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action); +#else +# define init_dump_action(a) +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action) +{ + FAR struct action_cmd_s *cmd; + + init_debug("action %p", action); + init_debug(" event trigger: '%s'", action->event ? action->event : ""); + list_for_every_entry(&action->cmds, cmd, struct action_cmd_s, node) + { + init_dump_args(cmd->argc, cmd->argv); + } +} +#endif + +static void add_ready(FAR struct action_manager_s *am, + FAR struct action_s *a) +{ + FAR struct action_s *ready; +#ifdef CONFIG_SYSTEM_INIT_DEBUG + FAR struct action_cmd_s *cmd; + + list_for_every_entry(&a->cmds, cmd, struct action_cmd_s, node) + { + init_debug("add ready %p %s", cmd, cmd->argv[0]); + } +#endif + + list_for_every_entry(&am->ready_actions, ready, struct action_s, + ready_node) + { + if (ready == a) + { + init_debug("action %p(%s) already on the queue", a, Review Comment: Ditto ```suggestion init_debug("Action %p(%s) already on the queue", a, ``` ########## system/init/action.c: ########## @@ -0,0 +1,346 @@ +/**************************************************************************** + * apps/system/init/action.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 <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sys/types.h> +#include <sys/param.h> +#include <unistd.h> + +#include "action.h" +#include "builtin.h" +#include "init.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Maximum number of parameters for the action trigger. + * Format: `on <trigger>` + */ + +#define ACTION_ARGUMENTS_MAX 2 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action); +#else +# define init_dump_action(a) +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action) +{ + FAR struct action_cmd_s *cmd; + + init_debug("action %p", action); + init_debug(" event trigger: '%s'", action->event ? action->event : ""); + list_for_every_entry(&action->cmds, cmd, struct action_cmd_s, node) + { + init_dump_args(cmd->argc, cmd->argv); + } +} +#endif + +static void add_ready(FAR struct action_manager_s *am, + FAR struct action_s *a) +{ + FAR struct action_s *ready; +#ifdef CONFIG_SYSTEM_INIT_DEBUG + FAR struct action_cmd_s *cmd; + + list_for_every_entry(&a->cmds, cmd, struct action_cmd_s, node) + { + init_debug("add ready %p %s", cmd, cmd->argv[0]); + } +#endif + + list_for_every_entry(&am->ready_actions, ready, struct action_s, + ready_node) + { + if (ready == a) + { + init_debug("action %p(%s) already on the queue", a, + a->event ? a->event : ""); + init_dump_action(a); + return; + } + } + + list_add_tail(&am->ready_actions, &a->ready_node); +} + +static void update_ready(FAR struct action_manager_s *am) +{ + size_t i; + + /* Actions with event trigger */ + + for (i = 0; i < nitems(am->events); i++) + { + if (!am->events[i]) + { + continue; + } + + am->current = list_prepare_entry(am->current, &am->actions, + struct action_s, node); + list_for_every_entry_continue(am->current, &am->actions, + struct action_s, node) + { + if (am->current->event && !strcmp(am->current->event, + am->events[i])) + { + add_ready(am, am->current); + return; + } + } + + init_debug("remove event [%zu] '%s'", i, am->events[i]); Review Comment: Ditto ```suggestion init_debug("Remove event [%zu] '%s'", i, am->events[i]); ``` ########## system/init/action.c: ########## @@ -0,0 +1,346 @@ +/**************************************************************************** + * apps/system/init/action.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 <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sys/types.h> +#include <sys/param.h> +#include <unistd.h> + +#include "action.h" +#include "builtin.h" +#include "init.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Maximum number of parameters for the action trigger. + * Format: `on <trigger>` + */ + +#define ACTION_ARGUMENTS_MAX 2 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action); +#else +# define init_dump_action(a) +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action) +{ + FAR struct action_cmd_s *cmd; + + init_debug("action %p", action); + init_debug(" event trigger: '%s'", action->event ? action->event : ""); + list_for_every_entry(&action->cmds, cmd, struct action_cmd_s, node) + { + init_dump_args(cmd->argc, cmd->argv); + } +} +#endif + +static void add_ready(FAR struct action_manager_s *am, + FAR struct action_s *a) +{ + FAR struct action_s *ready; +#ifdef CONFIG_SYSTEM_INIT_DEBUG + FAR struct action_cmd_s *cmd; + + list_for_every_entry(&a->cmds, cmd, struct action_cmd_s, node) + { + init_debug("add ready %p %s", cmd, cmd->argv[0]); + } +#endif + + list_for_every_entry(&am->ready_actions, ready, struct action_s, + ready_node) + { + if (ready == a) + { + init_debug("action %p(%s) already on the queue", a, + a->event ? a->event : ""); + init_dump_action(a); + return; + } + } + + list_add_tail(&am->ready_actions, &a->ready_node); +} + +static void update_ready(FAR struct action_manager_s *am) +{ + size_t i; + + /* Actions with event trigger */ + + for (i = 0; i < nitems(am->events); i++) + { + if (!am->events[i]) + { + continue; + } + + am->current = list_prepare_entry(am->current, &am->actions, + struct action_s, node); + list_for_every_entry_continue(am->current, &am->actions, + struct action_s, node) + { + if (am->current->event && !strcmp(am->current->event, + am->events[i])) + { + add_ready(am, am->current); + return; + } + } + + init_debug("remove event [%zu] '%s'", i, am->events[i]); + am->current = NULL; + free(am->events[i]); + am->events[i] = NULL; + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int init_action_add_event(FAR struct action_manager_s *am, + FAR const char *event) +{ + int ret = -ENOBUFS; + size_t i; + + for (i = 0; i < nitems(am->events); i++) + { + if (am->events[i]) + { + continue; + } + + am->events[i] = strdup(event); + if (am->events[i]) + { + init_debug("add event [%zu] '%s'", i, am->events[i]); + return 0; + } + + ret = -errno; + break; + } + + init_warn("drop event '%s' %d", event, ret); + return ret; +} + +/**************************************************************************** + * Name: init_action_run_command + * + * Description: + * Execute the ready commands in the action. + * + * Input Parameters: + * am - Instance of Action Manager + * + * Returned Value: + * The expected next call interval (in milliseconds), where INT_MAX + * indicates blocking. + ****************************************************************************/ + +int init_action_run_command(FAR struct action_manager_s *am) +{ + FAR struct action_s *ready; + int ret; + + if (am->pid_running != -1) + { + init_debug("waiting '%s' pid %d", am->running->argv[0], Review Comment: Ditto ```suggestion init_debug("Waiting '%s' pid %d", am->running->argv[0], ``` ########## system/init/builtin.c: ########## @@ -0,0 +1,237 @@ +/**************************************************************************** + * apps/system/init/builtin.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 <stdlib.h> +#include <string.h> +#include <spawn.h> +#include <sys/param.h> +#include <sys/wait.h> + +#include "builtin.h" +#include "init.h" +#include "service.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct cmd_map_s +{ + FAR const char *cmd; + uint8_t minargs; + uint8_t maxargs; + CODE int (*func)(FAR struct action_manager_s *, int, FAR char **); +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int cmd_trigger(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct cmd_map_s g_builtin[] = +{ + {"class_start", 2, 2, cmd_class_start}, + {"class_stop", 2, 2, cmd_class_stop}, + {"exec", 3, 99, cmd_exec}, + {"exec_start", 2, 2, cmd_exec_start}, + {"start", 2, 2, cmd_start}, + {"stop", 2, 2, cmd_stop}, + {"trigger", 2, 2, cmd_trigger}, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_start_by_class(am->sm, argv[1]); +} + +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_stop_by_class(am->sm, argv[1]); +} + +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); + return -EINVAL; + } + + return init_service_start(service); +} + +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + int ret; + + ret = cmd_exec_start(am, argc, argv); + return ret < 0 ? ret : 0; +} + +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); Review Comment: Ditto ```suggestion init_err("No such service '%s'", argv[1]); ``` ########## system/init/builtin.c: ########## @@ -0,0 +1,237 @@ +/**************************************************************************** + * apps/system/init/builtin.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 <stdlib.h> +#include <string.h> +#include <spawn.h> +#include <sys/param.h> +#include <sys/wait.h> + +#include "builtin.h" +#include "init.h" +#include "service.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct cmd_map_s +{ + FAR const char *cmd; + uint8_t minargs; + uint8_t maxargs; + CODE int (*func)(FAR struct action_manager_s *, int, FAR char **); +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int cmd_trigger(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct cmd_map_s g_builtin[] = +{ + {"class_start", 2, 2, cmd_class_start}, + {"class_stop", 2, 2, cmd_class_stop}, + {"exec", 3, 99, cmd_exec}, + {"exec_start", 2, 2, cmd_exec_start}, + {"start", 2, 2, cmd_start}, + {"stop", 2, 2, cmd_stop}, + {"trigger", 2, 2, cmd_trigger}, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_start_by_class(am->sm, argv[1]); +} + +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_stop_by_class(am->sm, argv[1]); +} + +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); + return -EINVAL; + } + + return init_service_start(service); +} + +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + int ret; + + ret = cmd_exec_start(am, argc, argv); + return ret < 0 ? ret : 0; +} + +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); + return -EINVAL; + } + + return init_service_stop(service); +} + +static int cmd_trigger(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_action_add_event(am, argv[1]); +} + +static int cmd_exec(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + int i; + + for (i = 0; i < argc; i++) + { + if (!strcmp(argv[i], "--")) + { + i++; + return init_builtin_run(am, argc - i, &argv[i]); + } + } + + return -EINVAL; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: init_builtin_run + * + * Description: + * Execute the builtin command of Init, builtin App, or NSH builtin. + * + * Input Parameters: + * am - Instance of Action Manager. + * argc - Argument count. + * argv - A pointer to an array of string arguments. The end of the array + * is indicated with a NULL entry. + * + * Returned Value: + * Returns 0 (for Init builtin) or a PID(for builtin App or NSH builtin) on Review Comment: ```suggestion * Returns 0 (for Init builtin) or a PID (for builtin App or NSH builtin) on ``` ########## system/init/import.c: ########## @@ -0,0 +1,60 @@ +/**************************************************************************** + * apps/system/init/import.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 <sys/param.h> + +#include "import.h" +#include "init.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int init_import_parse(FAR const struct parser_s *parser, + bool create, FAR char *buf) +{ + FAR char *argv[2]; + int ret; + + if (create) + { + init_parse_arguments(buf, false, nitems(argv), argv); + ret = init_parse_config_file(parser, argv[1]); + if (ret < 0) + { + init_err("import %s", argv[1]); Review Comment: Should the error say that this is a **parsing** error? ```suggestion init_err("Import %s", argv[1]); ``` ########## system/init/builtin.c: ########## @@ -0,0 +1,237 @@ +/**************************************************************************** + * apps/system/init/builtin.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 <stdlib.h> +#include <string.h> +#include <spawn.h> +#include <sys/param.h> +#include <sys/wait.h> + +#include "builtin.h" +#include "init.h" +#include "service.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct cmd_map_s +{ + FAR const char *cmd; + uint8_t minargs; + uint8_t maxargs; + CODE int (*func)(FAR struct action_manager_s *, int, FAR char **); +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int cmd_trigger(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct cmd_map_s g_builtin[] = +{ + {"class_start", 2, 2, cmd_class_start}, + {"class_stop", 2, 2, cmd_class_stop}, + {"exec", 3, 99, cmd_exec}, + {"exec_start", 2, 2, cmd_exec_start}, + {"start", 2, 2, cmd_start}, + {"stop", 2, 2, cmd_stop}, + {"trigger", 2, 2, cmd_trigger}, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_start_by_class(am->sm, argv[1]); +} + +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_stop_by_class(am->sm, argv[1]); +} + +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); + return -EINVAL; + } + + return init_service_start(service); +} + +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + int ret; + + ret = cmd_exec_start(am, argc, argv); + return ret < 0 ? ret : 0; +} + +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); + return -EINVAL; + } + + return init_service_stop(service); +} + +static int cmd_trigger(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_action_add_event(am, argv[1]); +} + +static int cmd_exec(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + int i; + + for (i = 0; i < argc; i++) + { + if (!strcmp(argv[i], "--")) + { + i++; + return init_builtin_run(am, argc - i, &argv[i]); + } + } + + return -EINVAL; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: init_builtin_run + * + * Description: + * Execute the builtin command of Init, builtin App, or NSH builtin. + * + * Input Parameters: + * am - Instance of Action Manager. + * argc - Argument count. + * argv - A pointer to an array of string arguments. The end of the array + * is indicated with a NULL entry. + * + * Returned Value: + * Returns 0 (for Init builtin) or a PID(for builtin App or NSH builtin) on + * success; Returns the negative value of errno on failure. + ****************************************************************************/ + +int init_builtin_run(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + pid_t pid; + size_t i; + int ret; + + for (i = 0; i < nitems(g_builtin); i++) + { + if (!strcmp(g_builtin[i].cmd, argv[0])) + { + if (argc < g_builtin[i].minargs || argc > g_builtin[i].maxargs) + { + init_err("executing command '%s': invalid argument", argv[0]); + init_dump_args(argc, argv); + return -EINVAL; + } + + init_info("executing command '%s'", argv[0]); + return g_builtin[i].func(am, argc, argv); + } + } + + ret = posix_spawnp(&pid, argv[0], NULL, NULL, argv, NULL); + if (ret != 0) + { +#ifdef CONFIG_SYSTEM_SYSTEM + char cmd[CONFIG_SYSTEM_INIT_RC_LINE_MAX]; + + for (i = 0, cmd[i] = '\0'; i < argc; i++) + { + strlcat(cmd, argv[i], sizeof(cmd)); + if (i < argc - 1) + { + strcat(cmd, " "); + } + } + + init_debug("executing NSH command '%s'", cmd); + ret = system(cmd); + if (WIFEXITED(ret)) + { + init_debug("NSH command '%s' exited %d", cmd, WEXITSTATUS(ret)); + return -WEXITSTATUS(ret); + } +#endif + + init_err("executing command '%s': %d", argv[0], ret); Review Comment: ```suggestion init_err("Executing command '%s': %d", argv[0], ret); ``` ########## system/init/import.c: ########## @@ -0,0 +1,60 @@ +/**************************************************************************** + * apps/system/init/import.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 <sys/param.h> + +#include "import.h" +#include "init.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int init_import_parse(FAR const struct parser_s *parser, + bool create, FAR char *buf) +{ + FAR char *argv[2]; + int ret; + + if (create) + { + init_parse_arguments(buf, false, nitems(argv), argv); + ret = init_parse_config_file(parser, argv[1]); + if (ret < 0) + { + init_err("import %s", argv[1]); + return ret; + } + } + else + { + init_err("parse import"); Review Comment: Needs more descriptive error message here. What went wrong? ```suggestion init_err("Parse import"); ``` ########## system/init/action.c: ########## @@ -0,0 +1,346 @@ +/**************************************************************************** + * apps/system/init/action.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 <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sys/types.h> +#include <sys/param.h> +#include <unistd.h> + +#include "action.h" +#include "builtin.h" +#include "init.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Maximum number of parameters for the action trigger. + * Format: `on <trigger>` + */ + +#define ACTION_ARGUMENTS_MAX 2 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action); +#else +# define init_dump_action(a) +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action) +{ + FAR struct action_cmd_s *cmd; + + init_debug("action %p", action); + init_debug(" event trigger: '%s'", action->event ? action->event : ""); + list_for_every_entry(&action->cmds, cmd, struct action_cmd_s, node) + { + init_dump_args(cmd->argc, cmd->argv); + } +} +#endif + +static void add_ready(FAR struct action_manager_s *am, + FAR struct action_s *a) +{ + FAR struct action_s *ready; +#ifdef CONFIG_SYSTEM_INIT_DEBUG + FAR struct action_cmd_s *cmd; + + list_for_every_entry(&a->cmds, cmd, struct action_cmd_s, node) + { + init_debug("add ready %p %s", cmd, cmd->argv[0]); Review Comment: Ditto ```suggestion init_debug("Add ready %p %s", cmd, cmd->argv[0]); ``` ########## system/init/action.c: ########## @@ -0,0 +1,346 @@ +/**************************************************************************** + * apps/system/init/action.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 <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sys/types.h> +#include <sys/param.h> +#include <unistd.h> + +#include "action.h" +#include "builtin.h" +#include "init.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Maximum number of parameters for the action trigger. + * Format: `on <trigger>` + */ + +#define ACTION_ARGUMENTS_MAX 2 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action); +#else +# define init_dump_action(a) +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action) +{ + FAR struct action_cmd_s *cmd; + + init_debug("action %p", action); + init_debug(" event trigger: '%s'", action->event ? action->event : ""); + list_for_every_entry(&action->cmds, cmd, struct action_cmd_s, node) + { + init_dump_args(cmd->argc, cmd->argv); + } +} +#endif + +static void add_ready(FAR struct action_manager_s *am, + FAR struct action_s *a) +{ + FAR struct action_s *ready; +#ifdef CONFIG_SYSTEM_INIT_DEBUG + FAR struct action_cmd_s *cmd; + + list_for_every_entry(&a->cmds, cmd, struct action_cmd_s, node) + { + init_debug("add ready %p %s", cmd, cmd->argv[0]); + } +#endif + + list_for_every_entry(&am->ready_actions, ready, struct action_s, + ready_node) + { + if (ready == a) + { + init_debug("action %p(%s) already on the queue", a, + a->event ? a->event : ""); + init_dump_action(a); + return; + } + } + + list_add_tail(&am->ready_actions, &a->ready_node); +} + +static void update_ready(FAR struct action_manager_s *am) +{ + size_t i; + + /* Actions with event trigger */ + + for (i = 0; i < nitems(am->events); i++) + { + if (!am->events[i]) + { + continue; + } + + am->current = list_prepare_entry(am->current, &am->actions, + struct action_s, node); + list_for_every_entry_continue(am->current, &am->actions, + struct action_s, node) + { + if (am->current->event && !strcmp(am->current->event, + am->events[i])) + { + add_ready(am, am->current); + return; + } + } + + init_debug("remove event [%zu] '%s'", i, am->events[i]); + am->current = NULL; + free(am->events[i]); + am->events[i] = NULL; + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int init_action_add_event(FAR struct action_manager_s *am, + FAR const char *event) +{ + int ret = -ENOBUFS; + size_t i; + + for (i = 0; i < nitems(am->events); i++) + { + if (am->events[i]) + { + continue; + } + + am->events[i] = strdup(event); + if (am->events[i]) + { + init_debug("add event [%zu] '%s'", i, am->events[i]); + return 0; + } + + ret = -errno; + break; + } + + init_warn("drop event '%s' %d", event, ret); + return ret; +} + +/**************************************************************************** + * Name: init_action_run_command + * + * Description: + * Execute the ready commands in the action. + * + * Input Parameters: + * am - Instance of Action Manager + * + * Returned Value: + * The expected next call interval (in milliseconds), where INT_MAX + * indicates blocking. + ****************************************************************************/ + +int init_action_run_command(FAR struct action_manager_s *am) +{ + FAR struct action_s *ready; + int ret; + + if (am->pid_running != -1) + { + init_debug("waiting '%s' pid %d", am->running->argv[0], + am->pid_running); + return INT_MAX; + } + + update_ready(am); + if (list_is_empty(&am->ready_actions)) + { + return INT_MAX; + } + + ready = list_peek_head_type(&am->ready_actions, struct action_s, + ready_node); + + if (!am->running) + { + am->running = list_peek_head_type(&ready->cmds, + struct action_cmd_s, node); + } + +#if defined(CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW) && \ + CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW > 0 + clock_gettime(CLOCK_MONOTONIC, &am->time_run); +#endif + ret = init_builtin_run(am, am->running->argc, am->running->argv); + if (ret > 0) + { + am->pid_running = ret; + } + else + { + init_action_reap_command(am); + } + + return 0; +} + +void init_action_reap_command(FAR struct action_manager_s *am) +{ + FAR struct action_s *ready = list_peek_head_type(&am->ready_actions, + struct action_s, + ready_node); + +#if defined(CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW) && \ + CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW > 0 + struct timespec time; + int ms; + + clock_gettime(CLOCK_MONOTONIC, &time); + clock_timespec_subtract(&time, &am->time_run, &time); + ms = TIMESPEC2MS(time); + if (ms > CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW) + { + if (am->pid_running <= 0) + { + init_warn("command '%s' took %d ms", am->running->argv[0], ms); Review Comment: Ditto ```suggestion init_warn("Command '%s' took %d ms", am->running->argv[0], ms); ``` ########## system/init/builtin.c: ########## @@ -0,0 +1,237 @@ +/**************************************************************************** + * apps/system/init/builtin.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 <stdlib.h> +#include <string.h> +#include <spawn.h> +#include <sys/param.h> +#include <sys/wait.h> + +#include "builtin.h" +#include "init.h" +#include "service.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct cmd_map_s +{ + FAR const char *cmd; + uint8_t minargs; + uint8_t maxargs; + CODE int (*func)(FAR struct action_manager_s *, int, FAR char **); +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int cmd_trigger(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct cmd_map_s g_builtin[] = +{ + {"class_start", 2, 2, cmd_class_start}, + {"class_stop", 2, 2, cmd_class_stop}, + {"exec", 3, 99, cmd_exec}, + {"exec_start", 2, 2, cmd_exec_start}, + {"start", 2, 2, cmd_start}, + {"stop", 2, 2, cmd_stop}, + {"trigger", 2, 2, cmd_trigger}, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_start_by_class(am->sm, argv[1]); +} + +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_stop_by_class(am->sm, argv[1]); +} + +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); Review Comment: Ditto ```suggestion init_err("No such service '%s'", argv[1]); ``` ########## system/init/action.c: ########## @@ -0,0 +1,346 @@ +/**************************************************************************** + * apps/system/init/action.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 <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sys/types.h> +#include <sys/param.h> +#include <unistd.h> + +#include "action.h" +#include "builtin.h" +#include "init.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Maximum number of parameters for the action trigger. + * Format: `on <trigger>` + */ + +#define ACTION_ARGUMENTS_MAX 2 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action); +#else +# define init_dump_action(a) +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action) +{ + FAR struct action_cmd_s *cmd; + + init_debug("action %p", action); + init_debug(" event trigger: '%s'", action->event ? action->event : ""); + list_for_every_entry(&action->cmds, cmd, struct action_cmd_s, node) + { + init_dump_args(cmd->argc, cmd->argv); + } +} +#endif + +static void add_ready(FAR struct action_manager_s *am, + FAR struct action_s *a) +{ + FAR struct action_s *ready; +#ifdef CONFIG_SYSTEM_INIT_DEBUG + FAR struct action_cmd_s *cmd; + + list_for_every_entry(&a->cmds, cmd, struct action_cmd_s, node) + { + init_debug("add ready %p %s", cmd, cmd->argv[0]); + } +#endif + + list_for_every_entry(&am->ready_actions, ready, struct action_s, + ready_node) + { + if (ready == a) + { + init_debug("action %p(%s) already on the queue", a, + a->event ? a->event : ""); + init_dump_action(a); + return; + } + } + + list_add_tail(&am->ready_actions, &a->ready_node); +} + +static void update_ready(FAR struct action_manager_s *am) +{ + size_t i; + + /* Actions with event trigger */ + + for (i = 0; i < nitems(am->events); i++) + { + if (!am->events[i]) + { + continue; + } + + am->current = list_prepare_entry(am->current, &am->actions, + struct action_s, node); + list_for_every_entry_continue(am->current, &am->actions, + struct action_s, node) + { + if (am->current->event && !strcmp(am->current->event, + am->events[i])) + { + add_ready(am, am->current); + return; + } + } + + init_debug("remove event [%zu] '%s'", i, am->events[i]); + am->current = NULL; + free(am->events[i]); + am->events[i] = NULL; + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int init_action_add_event(FAR struct action_manager_s *am, + FAR const char *event) +{ + int ret = -ENOBUFS; + size_t i; + + for (i = 0; i < nitems(am->events); i++) + { + if (am->events[i]) + { + continue; + } + + am->events[i] = strdup(event); + if (am->events[i]) + { + init_debug("add event [%zu] '%s'", i, am->events[i]); + return 0; + } + + ret = -errno; + break; + } + + init_warn("drop event '%s' %d", event, ret); Review Comment: Ditto. ```suggestion init_warn("Drop event '%s' %d", event, ret); ``` ########## system/init/action.c: ########## @@ -0,0 +1,346 @@ +/**************************************************************************** + * apps/system/init/action.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 <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sys/types.h> +#include <sys/param.h> +#include <unistd.h> + +#include "action.h" +#include "builtin.h" +#include "init.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Maximum number of parameters for the action trigger. + * Format: `on <trigger>` + */ + +#define ACTION_ARGUMENTS_MAX 2 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action); +#else +# define init_dump_action(a) +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action) +{ + FAR struct action_cmd_s *cmd; + + init_debug("action %p", action); + init_debug(" event trigger: '%s'", action->event ? action->event : ""); + list_for_every_entry(&action->cmds, cmd, struct action_cmd_s, node) + { + init_dump_args(cmd->argc, cmd->argv); + } +} +#endif + +static void add_ready(FAR struct action_manager_s *am, + FAR struct action_s *a) +{ + FAR struct action_s *ready; +#ifdef CONFIG_SYSTEM_INIT_DEBUG + FAR struct action_cmd_s *cmd; + + list_for_every_entry(&a->cmds, cmd, struct action_cmd_s, node) + { + init_debug("add ready %p %s", cmd, cmd->argv[0]); + } +#endif + + list_for_every_entry(&am->ready_actions, ready, struct action_s, + ready_node) + { + if (ready == a) + { + init_debug("action %p(%s) already on the queue", a, + a->event ? a->event : ""); + init_dump_action(a); + return; + } + } + + list_add_tail(&am->ready_actions, &a->ready_node); +} + +static void update_ready(FAR struct action_manager_s *am) +{ + size_t i; + + /* Actions with event trigger */ + + for (i = 0; i < nitems(am->events); i++) + { + if (!am->events[i]) + { + continue; + } + + am->current = list_prepare_entry(am->current, &am->actions, + struct action_s, node); + list_for_every_entry_continue(am->current, &am->actions, + struct action_s, node) + { + if (am->current->event && !strcmp(am->current->event, + am->events[i])) + { + add_ready(am, am->current); + return; + } + } + + init_debug("remove event [%zu] '%s'", i, am->events[i]); + am->current = NULL; + free(am->events[i]); + am->events[i] = NULL; + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int init_action_add_event(FAR struct action_manager_s *am, + FAR const char *event) +{ + int ret = -ENOBUFS; + size_t i; + + for (i = 0; i < nitems(am->events); i++) + { + if (am->events[i]) + { + continue; + } + + am->events[i] = strdup(event); + if (am->events[i]) + { + init_debug("add event [%zu] '%s'", i, am->events[i]); Review Comment: Ditto ```suggestion init_debug("Add event [%zu] '%s'", i, am->events[i]); ``` ########## system/init/builtin.c: ########## @@ -0,0 +1,237 @@ +/**************************************************************************** + * apps/system/init/builtin.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 <stdlib.h> +#include <string.h> +#include <spawn.h> +#include <sys/param.h> +#include <sys/wait.h> + +#include "builtin.h" +#include "init.h" +#include "service.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct cmd_map_s +{ + FAR const char *cmd; + uint8_t minargs; + uint8_t maxargs; + CODE int (*func)(FAR struct action_manager_s *, int, FAR char **); +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int cmd_trigger(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct cmd_map_s g_builtin[] = +{ + {"class_start", 2, 2, cmd_class_start}, + {"class_stop", 2, 2, cmd_class_stop}, + {"exec", 3, 99, cmd_exec}, + {"exec_start", 2, 2, cmd_exec_start}, + {"start", 2, 2, cmd_start}, + {"stop", 2, 2, cmd_stop}, + {"trigger", 2, 2, cmd_trigger}, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_start_by_class(am->sm, argv[1]); +} + +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_stop_by_class(am->sm, argv[1]); +} + +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); + return -EINVAL; + } + + return init_service_start(service); +} + +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + int ret; + + ret = cmd_exec_start(am, argc, argv); + return ret < 0 ? ret : 0; +} + +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); + return -EINVAL; + } + + return init_service_stop(service); +} + +static int cmd_trigger(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_action_add_event(am, argv[1]); +} + +static int cmd_exec(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + int i; + + for (i = 0; i < argc; i++) + { + if (!strcmp(argv[i], "--")) + { + i++; + return init_builtin_run(am, argc - i, &argv[i]); + } + } + + return -EINVAL; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: init_builtin_run + * + * Description: + * Execute the builtin command of Init, builtin App, or NSH builtin. + * + * Input Parameters: + * am - Instance of Action Manager. + * argc - Argument count. + * argv - A pointer to an array of string arguments. The end of the array + * is indicated with a NULL entry. + * + * Returned Value: + * Returns 0 (for Init builtin) or a PID(for builtin App or NSH builtin) on + * success; Returns the negative value of errno on failure. + ****************************************************************************/ + +int init_builtin_run(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + pid_t pid; + size_t i; + int ret; + + for (i = 0; i < nitems(g_builtin); i++) + { + if (!strcmp(g_builtin[i].cmd, argv[0])) + { + if (argc < g_builtin[i].minargs || argc > g_builtin[i].maxargs) + { + init_err("executing command '%s': invalid argument", argv[0]); + init_dump_args(argc, argv); + return -EINVAL; + } + + init_info("executing command '%s'", argv[0]); + return g_builtin[i].func(am, argc, argv); + } + } + + ret = posix_spawnp(&pid, argv[0], NULL, NULL, argv, NULL); + if (ret != 0) + { +#ifdef CONFIG_SYSTEM_SYSTEM + char cmd[CONFIG_SYSTEM_INIT_RC_LINE_MAX]; + + for (i = 0, cmd[i] = '\0'; i < argc; i++) + { + strlcat(cmd, argv[i], sizeof(cmd)); + if (i < argc - 1) + { + strcat(cmd, " "); + } + } + + init_debug("executing NSH command '%s'", cmd); Review Comment: ```suggestion init_debug("Executing NSH command '%s'", cmd); ``` ########## system/init/builtin.c: ########## @@ -0,0 +1,237 @@ +/**************************************************************************** + * apps/system/init/builtin.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 <stdlib.h> +#include <string.h> +#include <spawn.h> +#include <sys/param.h> +#include <sys/wait.h> + +#include "builtin.h" +#include "init.h" +#include "service.h" + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct cmd_map_s +{ + FAR const char *cmd; + uint8_t minargs; + uint8_t maxargs; + CODE int (*func)(FAR struct action_manager_s *, int, FAR char **); +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int cmd_trigger(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_exec(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv); +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct cmd_map_s g_builtin[] = +{ + {"class_start", 2, 2, cmd_class_start}, + {"class_stop", 2, 2, cmd_class_stop}, + {"exec", 3, 99, cmd_exec}, + {"exec_start", 2, 2, cmd_exec_start}, + {"start", 2, 2, cmd_start}, + {"stop", 2, 2, cmd_stop}, + {"trigger", 2, 2, cmd_trigger}, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int cmd_class_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_start_by_class(am->sm, argv[1]); +} + +static int cmd_class_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_service_stop_by_class(am->sm, argv[1]); +} + +static int cmd_exec_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); + return -EINVAL; + } + + return init_service_start(service); +} + +static int cmd_start(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + int ret; + + ret = cmd_exec_start(am, argc, argv); + return ret < 0 ? ret : 0; +} + +static int cmd_stop(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + FAR struct service_s *service; + + service = init_service_find_by_name(am->sm, argv[1]); + if (!service) + { + init_err("no such service '%s'", argv[1]); + return -EINVAL; + } + + return init_service_stop(service); +} + +static int cmd_trigger(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + return init_action_add_event(am, argv[1]); +} + +static int cmd_exec(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + int i; + + for (i = 0; i < argc; i++) + { + if (!strcmp(argv[i], "--")) + { + i++; + return init_builtin_run(am, argc - i, &argv[i]); + } + } + + return -EINVAL; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: init_builtin_run + * + * Description: + * Execute the builtin command of Init, builtin App, or NSH builtin. + * + * Input Parameters: + * am - Instance of Action Manager. + * argc - Argument count. + * argv - A pointer to an array of string arguments. The end of the array + * is indicated with a NULL entry. + * + * Returned Value: + * Returns 0 (for Init builtin) or a PID(for builtin App or NSH builtin) on + * success; Returns the negative value of errno on failure. + ****************************************************************************/ + +int init_builtin_run(FAR struct action_manager_s *am, + int argc, FAR char **argv) +{ + pid_t pid; + size_t i; + int ret; + + for (i = 0; i < nitems(g_builtin); i++) + { + if (!strcmp(g_builtin[i].cmd, argv[0])) + { + if (argc < g_builtin[i].minargs || argc > g_builtin[i].maxargs) + { + init_err("executing command '%s': invalid argument", argv[0]); + init_dump_args(argc, argv); + return -EINVAL; + } + + init_info("executing command '%s'", argv[0]); + return g_builtin[i].func(am, argc, argv); + } + } + + ret = posix_spawnp(&pid, argv[0], NULL, NULL, argv, NULL); + if (ret != 0) + { +#ifdef CONFIG_SYSTEM_SYSTEM + char cmd[CONFIG_SYSTEM_INIT_RC_LINE_MAX]; + + for (i = 0, cmd[i] = '\0'; i < argc; i++) + { + strlcat(cmd, argv[i], sizeof(cmd)); + if (i < argc - 1) + { + strcat(cmd, " "); + } + } + + init_debug("executing NSH command '%s'", cmd); + ret = system(cmd); + if (WIFEXITED(ret)) + { + init_debug("NSH command '%s' exited %d", cmd, WEXITSTATUS(ret)); + return -WEXITSTATUS(ret); + } +#endif + + init_err("executing command '%s': %d", argv[0], ret); + init_dump_args(argc, argv); + return -ret; + } + + init_debug("executed command '%s' pid %d", argv[0], pid); Review Comment: ```suggestion init_debug("Executed command '%s' pid %d", argv[0], pid); ``` ########## system/init/action.c: ########## @@ -0,0 +1,346 @@ +/**************************************************************************** + * apps/system/init/action.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 <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sys/types.h> +#include <sys/param.h> +#include <unistd.h> + +#include "action.h" +#include "builtin.h" +#include "init.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Maximum number of parameters for the action trigger. + * Format: `on <trigger>` + */ + +#define ACTION_ARGUMENTS_MAX 2 + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action); +#else +# define init_dump_action(a) +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_SYSTEM_INIT_DEBUG +static void init_dump_action(FAR struct action_s *action) +{ + FAR struct action_cmd_s *cmd; + + init_debug("action %p", action); + init_debug(" event trigger: '%s'", action->event ? action->event : ""); + list_for_every_entry(&action->cmds, cmd, struct action_cmd_s, node) + { + init_dump_args(cmd->argc, cmd->argv); + } +} +#endif + +static void add_ready(FAR struct action_manager_s *am, + FAR struct action_s *a) +{ + FAR struct action_s *ready; +#ifdef CONFIG_SYSTEM_INIT_DEBUG + FAR struct action_cmd_s *cmd; + + list_for_every_entry(&a->cmds, cmd, struct action_cmd_s, node) + { + init_debug("add ready %p %s", cmd, cmd->argv[0]); + } +#endif + + list_for_every_entry(&am->ready_actions, ready, struct action_s, + ready_node) + { + if (ready == a) + { + init_debug("action %p(%s) already on the queue", a, + a->event ? a->event : ""); + init_dump_action(a); + return; + } + } + + list_add_tail(&am->ready_actions, &a->ready_node); +} + +static void update_ready(FAR struct action_manager_s *am) +{ + size_t i; + + /* Actions with event trigger */ + + for (i = 0; i < nitems(am->events); i++) + { + if (!am->events[i]) + { + continue; + } + + am->current = list_prepare_entry(am->current, &am->actions, + struct action_s, node); + list_for_every_entry_continue(am->current, &am->actions, + struct action_s, node) + { + if (am->current->event && !strcmp(am->current->event, + am->events[i])) + { + add_ready(am, am->current); + return; + } + } + + init_debug("remove event [%zu] '%s'", i, am->events[i]); + am->current = NULL; + free(am->events[i]); + am->events[i] = NULL; + } +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int init_action_add_event(FAR struct action_manager_s *am, + FAR const char *event) +{ + int ret = -ENOBUFS; + size_t i; + + for (i = 0; i < nitems(am->events); i++) + { + if (am->events[i]) + { + continue; + } + + am->events[i] = strdup(event); + if (am->events[i]) + { + init_debug("add event [%zu] '%s'", i, am->events[i]); + return 0; + } + + ret = -errno; + break; + } + + init_warn("drop event '%s' %d", event, ret); + return ret; +} + +/**************************************************************************** + * Name: init_action_run_command + * + * Description: + * Execute the ready commands in the action. + * + * Input Parameters: + * am - Instance of Action Manager + * + * Returned Value: + * The expected next call interval (in milliseconds), where INT_MAX + * indicates blocking. + ****************************************************************************/ + +int init_action_run_command(FAR struct action_manager_s *am) +{ + FAR struct action_s *ready; + int ret; + + if (am->pid_running != -1) + { + init_debug("waiting '%s' pid %d", am->running->argv[0], + am->pid_running); + return INT_MAX; + } + + update_ready(am); + if (list_is_empty(&am->ready_actions)) + { + return INT_MAX; + } + + ready = list_peek_head_type(&am->ready_actions, struct action_s, + ready_node); + + if (!am->running) + { + am->running = list_peek_head_type(&ready->cmds, + struct action_cmd_s, node); + } + +#if defined(CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW) && \ + CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW > 0 + clock_gettime(CLOCK_MONOTONIC, &am->time_run); +#endif + ret = init_builtin_run(am, am->running->argc, am->running->argv); + if (ret > 0) + { + am->pid_running = ret; + } + else + { + init_action_reap_command(am); + } + + return 0; +} + +void init_action_reap_command(FAR struct action_manager_s *am) +{ + FAR struct action_s *ready = list_peek_head_type(&am->ready_actions, + struct action_s, + ready_node); + +#if defined(CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW) && \ + CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW > 0 + struct timespec time; + int ms; + + clock_gettime(CLOCK_MONOTONIC, &time); + clock_timespec_subtract(&time, &am->time_run, &time); + ms = TIMESPEC2MS(time); + if (ms > CONFIG_SYSTEM_INIT_ACTION_WARN_SLOW) + { + if (am->pid_running <= 0) + { + init_warn("command '%s' took %d ms", am->running->argv[0], ms); + } + else + { + init_warn("command '%s' pid %d took %d ms", am->running->argv[0], + am->pid_running, ms); + } + } +#endif + + am->pid_running = -1; + if (list_is_tail(&ready->cmds, &am->running->node)) + { + am->running = NULL; + list_delete(&ready->ready_node); + } + else + { + am->running = list_next_entry(am->running, struct action_cmd_s, node); + } +} + +int init_action_parse(FAR const struct parser_s *parser, + bool create, FAR char *buf) +{ + FAR struct action_manager_s *am = parser->priv; + FAR char *argv[ACTION_ARGUMENTS_MAX]; + FAR struct action_cmd_s *cmd; + FAR struct action_s *a; + int ret; + + if (create) + { + ret = init_parse_arguments(buf, false, nitems(argv), argv); + if (ret < 2) + { + init_err("invalid argument: %s", buf); + return -EINVAL; + } + + a = calloc(1, sizeof(*a)); + if (a == NULL) + { + init_err("alloc"); Review Comment: Maybe say which alloc action failed? Might be other subsystems competing for the memory :-) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
