Add option --name for naming the process
authorJosé Bollo <jose.bollo@iot.bzh>
Tue, 29 Aug 2017 09:22:49 +0000 (11:22 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Tue, 29 Aug 2017 09:22:49 +0000 (11:22 +0200)
This option allows to set the command line
and the thread name of the main process.

The value can contain spaces that will be
replaced by nulls.

Change-Id: I895270a24663467b16fb3cd8fc3218b7b003b6bb
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/CMakeLists.txt
src/afb-config.c
src/afb-config.h
src/main.c
src/process-name.c [new file with mode: 0644]
src/process-name.h [new file with mode: 0644]

index fe40c4c..14c5120 100644 (file)
@@ -64,6 +64,7 @@ ADD_LIBRARY(afb-lib STATIC
        afb-xreq.c
        jobs.c
        locale-root.c
+       process-name.c
        sd-fds.c
        sig-monitor.c
        subpath.c
index 25f44cd..d906bcb 100644 (file)
@@ -90,6 +90,7 @@
 #if defined(WITH_MONITORING_OPTION)
 #define SET_MONITORING     'M'
 #endif
+#define SET_NAME           'n'
 #define SET_TCP_PORT       'p'
 #define SET_QUIET          'q'
 #define SET_RNDTOKEN       'r'
 #define SET_WORK_DIR       'w'
 
 const char shortopts[] =
-       "c:D:E:ehp:qrT:t:u:Vvw:"
+       "c:D:E:ehn:p:qrT:t:u:Vvw:"
 #if defined(WITH_MONITORING_OPTION)
        "M"
 #endif
@@ -125,6 +126,8 @@ static AFB_options cliOptions[] = {
        {SET_FORGROUND,     0, "foreground",  "Get all in foreground mode"},
        {SET_BACKGROUND,    0, "daemon",      "Get all in background mode"},
 
+       {SET_NAME,          1, "name",        "Set the visible name"},
+
        {SET_TCP_PORT,      1, "port",        "HTTP listening TCP port  [default 1234]"},
        {SET_ROOT_HTTP,     1, "roothttp",    "HTTP Root Directory [default no root http (files not served but apis still available)]"},
        {SET_ROOT_BASE,     1, "rootbase",    "Angular Base Root URL [default /opa]"},
@@ -512,6 +515,10 @@ static void parse_arguments(int argc, char **argv, struct afb_config *config)
                        config->background = 1;
                        break;
 
+               case SET_NAME:
+                       config->name = argvalstr(optc);
+                       break;
+
                case SET_MODE:
                        config->mode = argvalenum(optc, mode_desc);
                        break;
@@ -674,6 +681,7 @@ void afb_config_dump(struct afb_config *config)
        S(workdir)
        S(uploaddir)
        S(token)
+       S(name)
 
        L(aliases)
        L(dbus_clients)
index 792a6b8..f1a27f7 100644 (file)
@@ -35,6 +35,7 @@ struct afb_config {
        char *workdir;          // where to run the program
        char *uploaddir;        // where to store transient files
        char *token;            // initial authentication token [default NULL no session]
+       char *name;             /* name to set to the daemon */
 
        struct afb_config_list *aliases;
        struct afb_config_list *dbus_clients;
index 3999b77..0405fd1 100644 (file)
@@ -54,6 +54,7 @@
 #include "afb-hook.h"
 #include "sd-fds.h"
 #include "afb-debug.h"
+#include "process-name.h"
 
 /*
    if SELF_PGROUP == 0 the launched command is the group leader
@@ -651,6 +652,10 @@ int main(int argc, char *argv[])
        config = afb_config_parse_arguments(argc, argv);
 
        afb_debug("main-args");
+       if (config->name) {
+               process_name_set_name(config->name);
+               process_name_replace_cmdline(argv, config->name);
+       }
 
        // --------- run -----------
        if (config->background) {
diff --git a/src/process-name.c b/src/process-name.c
new file mode 100644 (file)
index 0000000..e8e19ee
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ *
+ * Licensed 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.
+ */
+
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/prctl.h>
+
+#include "process-name.h"
+
+int process_name_set_name(const char *name)
+{
+       return prctl(PR_SET_NAME, name);
+}
+
+int process_name_replace_cmdline(char **argv, const char *name)
+{
+       char *beg, *end, **av, c;
+       size_t length;
+
+       /* update the command line */
+       av = argv;
+       if (!av) {
+               errno = EINVAL;
+               return -1; /* no command line update required */
+       }
+
+       /* longest prefix */
+       end = beg = *av;
+       while (*av)
+               if (*av++ == end)
+                       while(*end++)
+                               ;
+       if (end == beg) {
+               errno = EINVAL;
+               return -1; /* nothing to change */
+       }
+
+       /* patch the command line */
+       av = &argv[1];
+       end--;
+       while (beg != end && (c = *name++)) {
+               if (c != ' ' || !*av)
+                       *beg++ = c;
+               else {
+                       *beg++ = 0;
+                       *av++ = beg;
+               }
+       }
+       /* terminate last arg */
+       if (beg != end)
+               *beg++ = 0;
+       /* update remaining args (for keeping initial length correct) */
+       while (*av)
+               *av++ = beg;
+       /* fulfill last arg with spaces */
+       while (beg != end)
+               *beg++ = ' ';
+       *beg = 0;
+
+       return 0;
+}
+
diff --git a/src/process-name.h b/src/process-name.h
new file mode 100644 (file)
index 0000000..874753f
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
+ *
+ * Licensed 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.
+ */
+
+#pragma once
+
+extern int process_name_set_name(const char *name);
+extern int process_name_replace_cmdline(char **argv, const char *name);
+