Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions compat/getprogname.c
Original file line number Diff line number Diff line change
@@ -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
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

void
setprogname(const char *name)
{
progname = name;
}
35 changes: 35 additions & 0 deletions compat/getprogname.h
Original file line number Diff line number Diff line change
@@ -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
9 changes: 1 addition & 8 deletions compat/setproctitle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 0 additions & 14 deletions compat/setproctitle.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@
#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);
Expand Down
37 changes: 37 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 1 addition & 33 deletions src/logerr.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <time.h>
#include <unistd.h>

#include "config.h"
#include "logerr.h"

#ifndef LOGERR_SYSLOG_FACILITY
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -518,10 +490,6 @@ logclose(void)
#endif

closelog();
#if defined(__linux__)
free(_logprog);
_logprog = NULL;
#endif
#ifndef SMALL
if (ctx->log_file == NULL)
return;
Expand Down