]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
compat: Add support for getprogname(3)
authorRoy Marples <roy@marples.name>
Tue, 2 Jun 2026 12:26:28 +0000 (13:26 +0100)
committerGitHub <noreply@github.com>
Tue, 2 Jun 2026 12:26:28 +0000 (13:26 +0100)
Removes all inlined variations into a common single one.
Only supports Linux (which is all we did anyway), but now adds detection for program_invocation_short_name in libc and will use that OR package name.
All Linux libc should support this.

compat/getprogname.c [new file with mode: 0644]
compat/getprogname.h [new file with mode: 0644]
compat/setproctitle.c
compat/setproctitle.h
configure
src/logerr.c

diff --git a/compat/getprogname.c b/compat/getprogname.c
new file mode 100644 (file)
index 0000000..afded20
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * getprogname: compat
+ * SPDX-License-Identifier: BSD-2-Clause
+ * Copyright (c) 2006-2025 Roy Marples <roy@marples.name>
+ * All rights reserved
+
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <errno.h>
+#include <stddef.h>
+
+#include "config.h"
+#include "defs.h"
+#include "getprogname.h"
+
+static const char *progname;
+
+const char *
+getprogname(void)
+{
+#if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
+       if (progname == NULL)
+               progname = program_invocation_short_name;
+       return progname;
+#else
+#warning "no OS support for getprogname(3)"
+       if (progname == NULL)
+               progname = PACKAGE;
+       return progname;
+#endif
+}
+
+void
+setprogname(const char *name)
+{
+       progname = name;
+}
diff --git a/compat/getprogname.h b/compat/getprogname.h
new file mode 100644 (file)
index 0000000..bf52eee
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * getprogname: compat
+ * SPDX-License-Identifier: BSD-2-Clause
+ * Copyright (c) 2006-2025 Roy Marples <roy@marples.name>
+ * All rights reserved
+
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef GETPROGNAME_H
+#define GETPROGNAME_H
+
+const char *getprogname(void);
+void setprogname(const char *);
+
+#endif
index 322b13fa2d3a0060bd7c6c22b1d0632230543980..dc0e96be81ab13be60e124ef32099cd551c852da 100644 (file)
@@ -165,7 +165,7 @@ spt_copyargs(int argc, char *argv[])
 void
 setproctitle_init(int argc, char *argv[], char *envp[])
 {
-       char *base, *end, *nul, *tmp;
+       char *base, *end, *nul;
        int i, envc, error;
 
        /* Try to make sure we got called with main() arguments. */
@@ -200,13 +200,6 @@ setproctitle_init(int argc, char *argv[], char *envp[])
                return;
        }
 
-       tmp = strdup(getprogname());
-       if (tmp == NULL) {
-               SPT.error = errno;
-               return;
-       }
-       setprogname(tmp);
-
        error = spt_copyenv(envc, envp);
        if (error) {
                SPT.error = error;
index 173e88a4209a5f689aa1f551ad289e07a04f6ec7..fb32118e274feefa6e0ed16a70b84e0f3168cac4 100644 (file)
 #endif
 #endif /* !__printflike */
 
-/* WEXITSTATUS is defined in stdlib.h which defines free() */
-#ifdef WEXITSTATUS
-static inline const char *
-getprogname(void)
-{
-       return "dhcpcd";
-}
-static inline void
-setprogname(char *name)
-{
-       free(name);
-}
-#endif
-
 void setproctitle_init(int, char *[], char *[]);
 __printflike(1, 2) void setproctitle(const char *, ...);
 void setproctitle_fini(void);
index b041bfbb285ed052f87e0c615edf51d1a1cd9505..e9b8b850d256cf1d721efe0529229c98e36d7693 100755 (executable)
--- a/configure
+++ b/configure
@@ -1075,6 +1075,43 @@ else
        fi
 fi
 
+if [ -z "$GETPROGNAME" ]; then
+       printf "Testing for getprogname ... "
+       cat << EOF >_getprogname.c
+#include <stdlib.h>
+int main(void) {
+       return getprogname() ? 0 : 1;
+}
+EOF
+       if $XCC _getprogname.c -o _getprogname 2>&3; then
+               GETPROGNAME=yes
+       else
+               GETPROGNAME=no
+       fi
+       echo "$GETPROGNAME"
+       rm -rf _getprogname.* _getprogname
+fi
+if [ "$GETPROGNAME" = no ]; then
+       echo "COMPAT_SRCS+=     compat/getprogname.c" >>$CONFIG_MK
+       echo "#include                  \"compat/getprogname.h\"" >>$CONFIG_H
+
+       printf "Testing for program_invocation_short_name ... "
+       cat << EOF >_getprognameshort.c
+#include <errno.h>
+int main(void) {
+       return program_invocation_short_name ? 0 : 1;
+}
+EOF
+       if $XCC _getprognameshort.c -o _getprognameshort 2>&3; then
+               GETPROGNAMESHORT=yes
+               echo "#define   HAVE_PROGRAM_INVOCATION_SHORT_NAME" >>$CONFIG_H
+       else
+               GETPROGNAMESHORT=no
+       fi
+       echo "$GETPROGNAMESHORT"
+       rm -rf _getprognameshort.* _getprognameshort
+fi
+
 if [ -z "$SETPROCTITLE" ]; then
        printf "Testing for setproctitle ... "
        cat << EOF >_setproctitle.c
index c2d318f9ad5c87c50b4c06d028acbba77de7727e..f7e4bfd7a919ec9212850e8374c1737d8ea3bb17 100644 (file)
@@ -39,6 +39,7 @@
 #include <time.h>
 #include <unistd.h>
 
+#include "config.h"
 #include "logerr.h"
 
 #ifndef LOGERR_SYSLOG_FACILITY
@@ -74,35 +75,6 @@ static struct logctx _logctx = {
        .log_pid = 0,
 };
 
-#if defined(__linux__)
-/* Poor man's getprogname(3). */
-static char *_logprog;
-static const char *
-getprogname(void)
-{
-       const char *p;
-
-       /* Use PATH_MAX + 1 to avoid truncation. */
-       if (_logprog == NULL) {
-               /* readlink(2) does not append a NULL byte,
-                * so zero the buffer. */
-               if ((_logprog = calloc(1, PATH_MAX + 1)) == NULL)
-                       return NULL;
-               if (readlink("/proc/self/exe", _logprog, PATH_MAX + 1) == -1) {
-                       free(_logprog);
-                       _logprog = NULL;
-                       return NULL;
-               }
-       }
-       if (_logprog[0] == '[')
-               return NULL;
-       p = strrchr(_logprog, '/');
-       if (p == NULL)
-               return _logprog;
-       return p + 1;
-}
-#endif
-
 #ifndef SMALL
 /* Write the time, syslog style. month day time - */
 static int
@@ -518,10 +490,6 @@ logclose(void)
 #endif
 
        closelog();
-#if defined(__linux__)
-       free(_logprog);
-       _logprog = NULL;
-#endif
 #ifndef SMALL
        if (ctx->log_file == NULL)
                return;