From: Roy Marples Date: Tue, 15 Oct 2019 13:03:27 +0000 (+0100) Subject: compat: Go back to linux specific setproctitle X-Git-Tag: v8.1.1~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=09b22668cf8d6478be633f21d3ee4d6ae22e763a;p=thirdparty%2Fdhcpcd.git compat: Go back to linux specific setproctitle Solaris does not work with argv stamping and this is much cleaner anyway. --- 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 #include +#endif +#include #include #include #include @@ -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 diff --git a/src/dhcpcd.c b/src/dhcpcd.c index 193414ed..6ab4f8e2 100644 --- a/src/dhcpcd.c +++ b/src/dhcpcd.c @@ -2153,6 +2153,9 @@ exit1: loginfox(PACKAGE " exited"); logclose(); free(ctx.logfile); +#ifdef SETPROCTITLE_H + setproctitle_free(); +#endif #ifdef USE_SIGNALS if (ctx.options & DHCPCD_FORKED) _exit(i); /* so atexit won't remove our pidfile */