summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2017-08-29 11:22:49 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2017-08-29 11:22:49 +0200
commit9bc48026a93e8330a2bd2795e78d5951c16b7d33 (patch)
treee4a182f2ed5c43ebc2007f18fd813e21323e7184
parent33c28e126df60d8f7c416e762314d9da79f53448 (diff)
Add option --name for naming the process
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>
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/afb-config.c10
-rw-r--r--src/afb-config.h1
-rw-r--r--src/main.c5
-rw-r--r--src/process-name.c76
-rw-r--r--src/process-name.h21
6 files changed, 113 insertions, 1 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index fe40c4c6..14c5120d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -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
diff --git a/src/afb-config.c b/src/afb-config.c
index 25f44cd8..d906bcb7 100644
--- a/src/afb-config.c
+++ b/src/afb-config.c
@@ -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'
@@ -102,7 +103,7 @@
#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)
diff --git a/src/afb-config.h b/src/afb-config.h
index 792a6b8f..f1a27f7a 100644
--- a/src/afb-config.h
+++ b/src/afb-config.h
@@ -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;
diff --git a/src/main.c b/src/main.c
index 3999b77a..0405fd1f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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
index 00000000..e8e19eea
--- /dev/null
+++ b/src/process-name.c
@@ -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
index 00000000..874753f6
--- /dev/null
+++ b/src/process-name.h
@@ -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);
+