summaryrefslogtreecommitdiffstats
path: root/compat
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2019-10-15 14:03:27 +0100
committerRoy Marples <roy@marples.name>2019-10-15 14:03:27 +0100
commit56af89f86b1d1ac033017af247ff9bc413e7192b (patch)
treea67f21ee9d026bdfa33eeb76ac21601426ed12dd /compat
parentb6f15b302ca72858f93406b41a4886cbad42b6ee (diff)
downloaddhcpcd-56af89f86b1d1ac033017af247ff9bc413e7192b.tar.xz
compat: Go back to linux specific setproctitle
Solaris does not work with argv stamping and this is much cleaner anyway.
Diffstat (limited to 'compat')
-rw-r--r--compat/setproctitle.c59
-rw-r--r--compat/setproctitle.h1
2 files changed, 42 insertions, 18 deletions
diff --git a/compat/setproctitle.c b/compat/setproctitle.c
index f5ff0ac0..f3a42f79 100644
--- a/compat/setproctitle.c
+++ b/compat/setproctitle.c
@@ -24,9 +24,12 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
+#ifdef __linux__
#include <sys/prctl.h>
#include <sys/syscall.h>
+#endif
+#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
@@ -38,34 +41,41 @@
#define prctl_arg(x) ((unsigned long)x)
-/*
- * Sets the process title to the specified title. Note that this may fail if
- * the kernel doesn't support PR_SET_MM_MAP (kernels <3.18).
- */
+static char *setproctitle_argv;
+
int setproctitle(const char *fmt, ...)
{
- char title[1024], *tp, *progname;
+ const char *progname;
+ char title[1024], *tp;
+ size_t tl, n;
va_list args;
- int fd, i;
- char *buf_ptr, *tmp_proctitle;
- char buf[BUFSIZ];
- int ret = 0;
- ssize_t bytes_read = 0;
- size_t len;
- static char *proctitle = NULL;
+ int ret;
#if 0
progname = getprogname();
#else
progname = "dhcpcd";
#endif
- tp = title;
- tp += snprintf(title, sizeof(title), "%s: ", progname);
+ tp = title;
+ tl = sizeof(title);
+ n = strlcpy(tp, progname, tl);
+ tp += n;
+ tl -= n;
+ n = strlcpy(tp, ": ", tl);
+ tp += n;
+ tl -= n;
va_start(args, fmt);
- vsnprintf(tp, sizeof(title) - strlen(progname), fmt, args);
+ vsnprintf(tp, tl, fmt, args);
va_end(args);
+#ifdef __linux__
+ int fd, i;
+ char *buf_ptr, *tmp_proctitle;
+ char buf[BUFSIZ];
+ ssize_t bytes_read;
+ size_t len;
+
/*
* We don't really need to know all of this stuff, but unfortunately
* PR_SET_MM_MAP requires us to set it all at once, so we have to
@@ -120,13 +130,13 @@ int setproctitle(const char *fmt, ...)
* want to have room for it. */
len = strlen(title) + 1;
- tmp_proctitle = realloc(proctitle, len);
+ tmp_proctitle = realloc(setproctitle_argv, len);
if (!tmp_proctitle)
return -1;
- proctitle = tmp_proctitle;
+ setproctitle_argv = tmp_proctitle;
- arg_start = (unsigned long)proctitle;
+ arg_start = (unsigned long)setproctitle_argv;
arg_end = arg_start + len;
brk_val = syscall(__NR_brk, 0);
@@ -152,5 +162,18 @@ int setproctitle(const char *fmt, ...)
prctl_arg(sizeof(prctl_map)), prctl_arg(0));
if (ret == 0)
(void)strlcpy((char *)arg_start, title, len);
+#else
+ /* Solaris doesn't work with the ARGV stamping approach.
+ * Is there any other way? */
+ ret = -1;
+ errno = ENOTSUP;
+#endif
return ret;
}
+
+void
+setproctitle_free(void)
+{
+
+ free(setproctitle_argv);
+}
diff --git a/compat/setproctitle.h b/compat/setproctitle.h
index 2fe685f1..1d8e84e7 100644
--- a/compat/setproctitle.h
+++ b/compat/setproctitle.h
@@ -33,4 +33,5 @@
#endif /* !__printflike */
__printflike(1, 2) int setproctitle(const char *, ...);
+void setproctitle_free(void);
#endif