Skip to contentSkip to conversation

src/log.c: Include libgen.h for basename API

PATCH In review Khem Raj 3 participants 3 messages Jan 2, 2024 – Jan 18, 2024

lore
Share & tools
lore

Khem Raj

author
#0 lore raw

PATCH v2 1/1 src/log.c: Include libgen.h for basename API

Use POSIX version of basename. This comes to front with latest musl which dropped the declaration from string.h [1] it fails to build with clang-17+ because it treats implicit function declaration as error.

Fix it by applying the basename on a copy of string since posix version may modify the input string.

[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7

---
v2: Fix formatting

 src/log.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
src/log.c +4 -2
index 554b046..2df3af7 100644
@@ -24,6 +24,7 @@
 #endif
 
 #include <stdio.h>
+#include <libgen.h>
 #include <unistd.h>
 #include <stdarg.h>
 #include <stdlib.h>
@@ -196,6 +197,7 @@ int __connman_log_init(const char *program, const char *debug,
 		const char *program_name, const char *program_version)
 {
 	static char path[PATH_MAX];
+	char* tmp = strdup(program);
 	int option = LOG_NDELAY | LOG_PID;
 
 	program_exec = program;
@@ -212,8 +214,8 @@ int __connman_log_init(const char *program, const char *debug,
 	if (backtrace)
 		signal_setup(signal_handler);
 
-	openlog(basename(program), option, LOG_DAEMON);
-
+	openlog(basename(tmp), option, LOG_DAEMON);
+	free(tmp);
 	syslog(LOG_INFO, "%s version %s", program_name, program_version);
 
 	return 0;
Signature · 1 line
2.43.0
#1 lore raw

Hi Khem,

Quoted 29 lines

Use POSIX version of basename. This comes to front with latest musl which dropped the declaration from string.h [1] it fails to build with clang-17+ because it treats implicit function declaration as error.

Fix it by applying the basename on a copy of string since posix version may modify the input string.

[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7

---
v2: Fix formatting

src/log.c | 6 ++++--

1 file changed, 4 insertions(+), 2 deletions(-)
src/log.c +1 -0
index 554b046..2df3af7 100644
@@ -24,6 +24,7 @@
 #endif
 
 #include <stdio.h>
+#include <libgen.h>
 #include <unistd.h>
 #include <stdarg.h>
 #include <stdlib.h>

src/log.c int __connman_log_init(const char *program, const char *debug, · line 200

Quoted 4 lines
src/log.c +1 -0
@@ -196,6 +197,7 @@ int __connman_log_init(const char *program, const char *debug,
 const char *program_name, const char *program_version)
 {
 static char path[PATH_MAX];
+ char* tmp = strdup(program);

declaration are char *tmp;

src/log.c int __connman_log_init(const char *program, const char *debug, · line 216

Quoted 9 lines
src/log.c +0 -2
 int option = LOG_NDELAY | LOG_PID;
 
 program_exec = program;
@@ -212,8 +214,8 @@ int __connman_log_init(const char *program, const char *debug,
 if (backtrace)
 signal_setup(signal_handler);
 
- openlog(basename(program), option, LOG_DAEMON);
-

Put the strdup here.

And you need to handle tmp == NULL since then it seems basename returns a constant “.”.

src/log.c int __connman_log_init(const char *program, const char *debug, · line 218

Quoted 2 lines
src/log.c +2 -0
+ openlog(basename(tmp), option, LOG_DAEMON);
+ free(tmp);

Regards

Marcel

#2 lore raw

Hi all,

Quoted 25 lines · Marcel Holtmann

Hi Khem,

Quoted 23 lines

Use POSIX version of basename. This comes to front with latest musl which dropped the declaration from string.h [1] it fails to build with clang-17+ because it treats implicit function declaration as error.

Fix it by applying the basename on a copy of string since posix version may modify the input string.

[1] https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7

---
v2: Fix formatting

src/log.c | 6 ++++--

1 file changed, 4 insertions(+), 2 deletions(-)
src/log.c +0 -0
index 554b046..2df3af7 100644
@@ -24,6 +24,7 @@
 #endif
 
Quoted 12 lines

src/log.c int __connman_log_init(const char *program, const char *debug, · line 200

Quoted 10 lines
src/log.c +2 -0
 #include <stdio.h>
+#include <libgen.h>
 #include <unistd.h>
 #include <stdarg.h>
 #include <stdlib.h>
@@ -196,6 +197,7 @@ int __connman_log_init(const char *program, const char *debug,
 const char *program_name, const char *program_version)
 {
 static char path[PATH_MAX];
+ char* tmp = strdup(program); >

declaration are char *tmp;

And perhaps use g_strdup() instead? coding-style.txt indirectly refers to use of g_ functions when available, as it is done with the rest of the codebase. Also, when small allocations fail the program should crash (M8).

Quoted 7 lines
Quoted 7 lines
src/log.c +0 -0
 int option = LOG_NDELAY | LOG_PID;
 
 program_exec = program;
@@ -212,8 +214,8 @@ int __connman_log_init(const char *program, const char *debug,
 if (backtrace)
 signal_setup(signal_handler);
 
Quoted 10 lines

src/log.c int __connman_log_init(const char *program, const char *debug, · line 216

Quoted 2 lines
src/log.c +0 -2
- openlog(basename(program), option, LOG_DAEMON);
-

Put the strdup here.

And you need to handle tmp == NULL since then it seems basename returns a constant “.”.

Quoted 2 lines
src/log.c +2 -0
+ openlog(basename(tmp), option, LOG_DAEMON);
+ free(tmp);

I was just thinking maybe it would be better to handle the duplication of the program name, argv[0] in main.c (both connmand and vpnd), where it is given as initialization arg to __connman_log_init()?

Cheers,
Jussi