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.
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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
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. */
return;
}
- tmp = strdup(getprogname());
- if (tmp == NULL) {
- SPT.error = errno;
- return;
- }
- setprogname(tmp);
-
error = spt_copyenv(envc, envp);
if (error) {
SPT.error = error;
#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);
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
#include <time.h>
#include <unistd.h>
+#include "config.h"
#include "logerr.h"
#ifndef LOGERR_SYSLOG_FACILITY
.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
#endif
closelog();
-#if defined(__linux__)
- free(_logprog);
- _logprog = NULL;
-#endif
#ifndef SMALL
if (ctx->log_file == NULL)
return;