acassis commented on code in PR #3192:
URL: https://github.com/apache/nuttx-apps/pull/3192#discussion_r2466449095


##########
system/nxinit/service.c:
##########
@@ -0,0 +1,682 @@
+/****************************************************************************
+ * apps/system/nxinit/service.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 <nuttx/clock.h>
+#include <sys/boardctl.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+#include <signal.h>
+#include <spawn.h>
+#include <sys/param.h>
+
+#include "init.h"
+#include "parser.h"
+#include "service.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define SYSTEM_NXINIT_SERVICE_GENTLE_KILL_TIMEOUT 200
+
+#define check_flags(s, f) ((s)->flags & (f))
+
+#ifdef CONFIG_SYSTEM_NXINIT_DEBUG
+#  define dump_flags(fmt, flags) \
+          do \
+            { \
+              size_t _i; \
+              for (_i = 0; _i < nitems(g_flag_str); _i++) \
+                { \
+                  if (g_flag_str[_i].flag & (flags)) \
+                    { \
+                      init_debug(fmt, g_flag_str[_i].str); \
+                    } \
+                } \
+            } \
+          while(0)
+#else
+#  define dump_flags(...)
+#endif
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct cmd_map_s
+{
+  FAR const char *cmd;
+  uint8_t minargs;
+  uint8_t maxargs;
+  int (*func)(FAR struct service_manager_s *, int, FAR char **);
+};
+
+#ifdef CONFIG_SYSTEM_NXINIT_DEBUG
+struct flag_str_s
+{
+  uint32_t flag;
+  FAR const char *str;
+};
+#endif
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static int option_class(FAR struct service_manager_s *sm,
+                        int argc, FAR char **argv);
+static int option_gentle_kill(FAR struct service_manager_s *sm,
+                              int argc, FAR char **argv);
+static int option_restart_period(FAR struct service_manager_s *sm,
+                                 int argc, FAR char **argv);
+static int option_override(FAR struct service_manager_s *sm,
+                           int argc, FAR char **argv);
+static int option_oneshot(FAR struct service_manager_s *sm,
+                          int argc, FAR char **argv);
+#ifdef CONFIG_BOARDCTL_RESET
+static int option_reboot_on_failure(FAR struct service_manager_s *sm,
+                                    int argc, FAR char **argv);
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct cmd_map_s g_option[] =
+{
+  {"class", 2, 99, option_class},
+  {"gentle_kill", 1, 1, option_gentle_kill},
+  {"restart_period", 2, 2, option_restart_period},
+  {"override", 1, 1, option_override},
+  {"oneshot", 1, 1, option_oneshot},
+#ifdef CONFIG_BOARDCTL_RESET
+  {"reboot_on_failure", 2, 2, option_reboot_on_failure},
+#endif
+};
+
+#ifdef CONFIG_SYSTEM_NXINIT_DEBUG
+static const struct flag_str_s g_flag_str[] =
+{
+  {SVC_DISABLED, "disabled"},
+  {SVC_ONESHOT, "oneshot"},
+  {SVC_RUNNING, "running"},
+  {SVC_RESTARTING, "restarting"},
+  {SVC_GENTLE_KILL, "gentle_kill"},
+  {SVC_REMOVE, "remove"},
+  {SVC_SIGKILL, "sigkill"},
+  {SVC_OVERRIDE, "override"},
+};
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void add_flags(FAR struct service_s *service, uint32_t flags)
+{
+  init_debug("Service '%s' flag 0x%" PRIx32 " add 0x%" PRIx32 "",
+             service->argv[1], service->flags, flags);
+  dump_flags("  +flag '%s'", flags);
+
+  service->flags |= flags;
+}
+
+static void remove_flags(FAR struct service_s *service, uint32_t flags)
+{
+  init_debug("Service '%s' flag 0x%" PRIx32 " add 0x%" PRIx32 "",
+             service->argv[1], service->flags, flags);
+  dump_flags("  -flag '%s'", flags);
+
+  service->flags &= ~flags;
+}
+
+static int kill_service(FAR struct service_s *service, int signo)
+{
+  int ret;
+
+  ret = kill(service->pid, signo);
+  if (ret < 0)
+    {
+      ret = -errno;
+      if (ret == -ESRCH)
+        {
+          init_service_reap(service, 0);
+        }
+    }
+
+  init_log(ret < 0 ? LOG_ERR : LOG_WARNING,
+           "sent signal %d to service '%s' pid %d %d",
+           signo, service->argv[1], service->pid, ret);
+
+  return ret;
+}
+
+static void remove_service(FAR struct service_s *service)
+{
+  FAR struct service_class_s *class;
+  FAR struct service_class_s *tmp;
+  int i;
+
+  init_warn("Removing service '%s' ...", service->argv[1]);
+  list_for_every_entry_safe(&service->classes, class, tmp,
+                            struct service_class_s, node)
+    {
+      list_delete(&class->node);
+      free(class);
+    }
+
+  for (i = 0; i < service->argc; i++)
+    {
+      free(service->argv[i]);
+    }
+
+  list_delete(&service->node);
+  free(service);
+}
+
+static int option_class(FAR struct service_manager_s *sm,
+                        int argc, FAR char **argv)
+{
+  FAR struct service_s *s = list_last_entry(&sm->services, struct service_s,
+                                            node);
+  size_t len = strlen(argv[1]) + 1;
+  FAR struct service_class_s *c;
+
+  list_for_every_entry(&s->classes, c, struct service_class_s, node)
+    {
+      if (!strcmp(c->name, argv[1]))
+        {
+          return 0;
+        }
+    }
+
+  c = malloc(sizeof(*c) + len);
+  if (!c)

Review Comment:
   It is more readable and also was compilers are become more restrictive, soon 
or later they can start raising warning about this kind of test



-- 
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]

Reply via email to