Skip to contentSkip to conversation

eal: remove unnecessary argv[0] handling

PATCH Superseded Stephen Hemminger 2 participants 9 messages Feb 2, 2022 – Feb 10, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal: remove unnecessary argv[0] handling

The rte_eal_init function looks at argv[0] to determine the program name to pass to the log init function. This is both unnecessary and in a corner case a problem.

Parsing argv[0] is unnecessary because the function openlog() already determines the log identifier from program name if NULL is passed, see openlog man page:
The string pointed to by ident is prepended to every message, and is typically set to the program name. If ident is NULL, the program name is used. (POSIX.1-2008 does not specify the behavior when ident is NULL.)

It is possible to run a program with argc=0 and argv[0]=NULL. This would cause rte_eal_init() to dereference a null pointer. Not a really useful feature, but better for DPDK get a SEGV if it doesn't need to.

---
 lib/eal/linux/eal.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
lib/eal/linux/eal.c +1 -5
index 60b49248388e..6bc7cdf440b8 100644
@@ -966,8 +966,6 @@ rte_eal_init(int argc, char **argv)
 	pthread_t thread_id;
 	static uint32_t run_once;
 	uint32_t has_run = 0;
-	const char *p;
-	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 	bool phys_addrs;
@@ -989,8 +987,6 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	p = strrchr(argv[0], '/');
-	strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(internal_conf);
@@ -1165,7 +1161,7 @@ rte_eal_init(int argc, char **argv)
 #endif
 	}
 
-	if (eal_log_init(logid, internal_conf->syslog_facility) < 0) {
+	if (eal_log_init(NULL, internal_conf->syslog_facility) < 0) {
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
Signature · 1 line
2.34.1
#1 lore raw

02/02/2022 20:47, Stephen Hemminger:

Quoted 11 lines

The rte_eal_init function looks at argv[0] to determine the program name to pass to the log init function. This is both unnecessary and in a corner case a problem.

Parsing argv[0] is unnecessary because the function openlog() already determines the log identifier from program name if NULL is passed, see openlog man page:
The string pointed to by ident is prepended to every message, and is typically set to the program name. If ident is NULL, the program name is used. (POSIX.1-2008 does not specify the behavior when ident is NULL.)

What about POSIX warning?
Did you test it? Which libc? What about musl?

[...]

lib/eal/linux/eal.c rte_eal_init(int argc, char **argv) · line 1164

Quoted 2 lines
lib/eal/linux/eal.c +1 -1
-	if (eal_log_init(logid, internal_conf->syslog_facility) < 0) {
+	if (eal_log_init(NULL, internal_conf->syslog_facility) < 0) {

We could completely remove the logid parameter.

#2 lore raw
Quoted 13 lines · On Wed, 09 Feb 2022 15:58:26 +0100 Thomas Monjalon <[email protected]> wrote:

02/02/2022 20:47, Stephen Hemminger:

Quoted 11 lines

The rte_eal_init function looks at argv[0] to determine the program name to pass to the log init function. This is both unnecessary and in a corner case a problem.

Parsing argv[0] is unnecessary because the function openlog() already determines the log identifier from program name if NULL is passed, see openlog man page:
The string pointed to by ident is prepended to every message, and is typically set to the program name. If ident is NULL, the program name is used. (POSIX.1-2008 does not specify the behavior when ident is NULL.)

Quoted 1 line

What about POSIX warning?

There are many cases where it is practical and useful to do non-POSIX things (see _GNU_SOURCE). Look at pthread_setaffinity_np() etc.

Quoted 1 line

Did you test it? Which libc? What about musl?

No, I don't have or use alternative libc versions.

Quoted 6 lines

[...]

lib/eal/linux/eal.c rte_eal_init(int argc, char **argv) · line 1164

Quoted 2 lines
lib/eal/linux/eal.c +1 -1
-	if (eal_log_init(logid, internal_conf->syslog_facility) < 0) {
+	if (eal_log_init(NULL, internal_conf->syslog_facility) < 0) {  

We could completely remove the logid parameter.

Yes, it could go away.

#3 lore raw
In reply to Thomas Monjalon
Quoted 13 lines · On Wed, 09 Feb 2022 15:58:26 +0100 Thomas Monjalon <[email protected]> wrote:

02/02/2022 20:47, Stephen Hemminger:

Quoted 11 lines

The rte_eal_init function looks at argv[0] to determine the program name to pass to the log init function. This is both unnecessary and in a corner case a problem.

Parsing argv[0] is unnecessary because the function openlog() already determines the log identifier from program name if NULL is passed, see openlog man page:
The string pointed to by ident is prepended to every message, and is typically set to the program name. If ident is NULL, the program name is used. (POSIX.1-2008 does not specify the behavior when ident is NULL.)

Quoted 2 lines

What about POSIX warning?
Did you test it? Which libc? What about musl?

Source for musl shows that openlog accepts NULL as ident. It then generates log messages with ident in the message.

#4 lore raw

09/02/2022 16:45, Stephen Hemminger:

Quoted 16 lines
Quoted 13 lines · On Wed, 09 Feb 2022 15:58:26 +0100 Thomas Monjalon <[email protected]> wrote:

02/02/2022 20:47, Stephen Hemminger:

Quoted 11 lines

The rte_eal_init function looks at argv[0] to determine the program name to pass to the log init function. This is both unnecessary and in a corner case a problem.

Parsing argv[0] is unnecessary because the function openlog() already determines the log identifier from program name if NULL is passed, see openlog man page:
The string pointed to by ident is prepended to every message, and is typically set to the program name. If ident is NULL, the program name is used. (POSIX.1-2008 does not specify the behavior when ident is NULL.)

Quoted 5 lines
Quoted 2 lines

What about POSIX warning?
Did you test it? Which libc? What about musl?

Source for musl shows that openlog accepts NULL as ident. It then generates log messages with ident in the message.

In this case, it will be empty string.
It seems this solution does not work with musl.

#5 lore raw
Quoted 17 lines · On Wed, 09 Feb 2022 19:51:15 +0100 Thomas Monjalon <[email protected]> wrote:

09/02/2022 16:45, Stephen Hemminger:

Quoted 16 lines
Quoted 13 lines · On Wed, 09 Feb 2022 15:58:26 +0100 Thomas Monjalon <[email protected]> wrote:

02/02/2022 20:47, Stephen Hemminger:

Quoted 11 lines

The rte_eal_init function looks at argv[0] to determine the program name to pass to the log init function. This is both unnecessary and in a corner case a problem.

Parsing argv[0] is unnecessary because the function openlog() already determines the log identifier from program name if NULL is passed, see openlog man page:
The string pointed to by ident is prepended to every message, and is typically set to the program name. If ident is NULL, the program name is used. (POSIX.1-2008 does not specify the behavior when ident is NULL.)

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

What about POSIX warning?
Did you test it? Which libc? What about musl?

Source for musl shows that openlog accepts NULL as ident. It then generates log messages with ident in the message.

In this case, it will be empty string.
It seems this solution does not work with musl.

Good news: musl is available as debian package
Bad news: DPDK build is broken with musl

Library IPSec_MB found: YES

drivers/crypto/ipsec_mb/meson.build:19:4: ERROR: Could not get define 'IMB_VERSION_STR'

#6 lore raw

09/02/2022 20:29, Stephen Hemminger:

Quoted 29 lines
Quoted 17 lines · On Wed, 09 Feb 2022 19:51:15 +0100 Thomas Monjalon <[email protected]> wrote:

09/02/2022 16:45, Stephen Hemminger:

Quoted 16 lines
Quoted 13 lines · On Wed, 09 Feb 2022 15:58:26 +0100 Thomas Monjalon <[email protected]> wrote:

02/02/2022 20:47, Stephen Hemminger:

Quoted 11 lines

The rte_eal_init function looks at argv[0] to determine the program name to pass to the log init function. This is both unnecessary and in a corner case a problem.

Parsing argv[0] is unnecessary because the function openlog() already determines the log identifier from program name if NULL is passed, see openlog man page:
The string pointed to by ident is prepended to every message, and is typically set to the program name. If ident is NULL, the program name is used. (POSIX.1-2008 does not specify the behavior when ident is NULL.)

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

What about POSIX warning?
Did you test it? Which libc? What about musl?

Source for musl shows that openlog accepts NULL as ident. It then generates log messages with ident in the message.

In this case, it will be empty string.
It seems this solution does not work with musl.

Quoted 7 lines

Good news: musl is available as debian package
Bad news: DPDK build is broken with musl

Library IPSec_MB found: YES

drivers/crypto/ipsec_mb/meson.build:19:4: ERROR: Could not get define 'IMB_VERSION_STR'

This error is not related to musl.

Compilation on Alpine (with musl) is tested regularly, it works for me.

#7 lore raw

PATCH v2 1/1 Subject: eal: remove unnecessary argv[0] handling

In reply to Stephen Hemminger

The rte_eal_init function only evalutes argv[0] to determine the program name to pass to the log init function. This is both unnecessary and in a corner case a problem.

It is possible to run a program with argc=0 and argv[0]=NULL. This would cause rte_eal_init() to dereference a null pointer. Not a really useful feature, but better for DPDK get a SEGV if it doesn't need to.

Parsing argv[0] is unnecessary because the function openlog() already determines the log identifier from program name if NULL is passed.

openlog man page:
The string pointed to by ident is prepended to every message, and is typically set to the program name. If ident is NULL, the program name is used. (POSIX.1-2008 does not specify the behavior when ident is NULL.)

Since eal_log_init() is internal just drop the unused argument.

---
 lib/eal/common/eal_log.h  | 2 +-
 lib/eal/linux/eal.c       | 6 +-----
 lib/eal/linux/eal_log.c   | 4 ++--
 lib/eal/windows/eal.c     | 2 +-
 lib/eal/windows/eal_log.c | 2 +-
 5 files changed, 6 insertions(+), 10 deletions(-)
lib/eal/common/eal_log.h +1 -1
index c784fa604389..d0282b3258da 100644
@@ -11,7 +11,7 @@
 /*
  * Initialize the default log stream.
  */
-int eal_log_init(const char *id, int facility);
+int eal_log_init(int facility);
 
 /*
  * Determine where log data is written when no call to rte_openlog_stream.
lib/eal/linux/eal.c +1 -5
index 9c8395ab14d0..a6e8c5793334 100644
@@ -966,8 +966,6 @@ rte_eal_init(int argc, char **argv)
 	pthread_t thread_id;
 	static uint32_t run_once;
 	uint32_t has_run = 0;
-	const char *p;
-	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 	bool phys_addrs;
@@ -989,8 +987,6 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	p = strrchr(argv[0], '/');
-	strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(internal_conf);
@@ -1165,7 +1161,7 @@ rte_eal_init(int argc, char **argv)
 #endif
 	}
 
-	if (eal_log_init(logid, internal_conf->syslog_facility) < 0) {
+	if (eal_log_init(internal_conf->syslog_facility) < 0) {
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
 		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
lib/eal/linux/eal_log.c +2 -2
index c0aa1007c4df..e5cc10737b37 100644
@@ -46,7 +46,7 @@ static cookie_io_functions_t console_log_func = {
  * once memzones are available.
  */
 int
-eal_log_init(const char *id, int facility)
+eal_log_init(int facility)
 {
 	FILE *log_stream;
 
@@ -54,7 +54,7 @@ eal_log_init(const char *id, int facility)
 	if (log_stream == NULL)
 		return -1;
 
-	openlog(id, LOG_NDELAY | LOG_PID, facility);
+	openlog(NULL, LOG_NDELAY | LOG_PID, facility);
 
 	eal_log_set_default(log_stream);
 
lib/eal/windows/eal.c +1 -1
index ca3c41aaa7f1..3133a934dd3c 100644
@@ -280,7 +280,7 @@ rte_eal_init(int argc, char **argv)
 	enum rte_iova_mode iova_mode;
 	int ret;
 
-	eal_log_init(NULL, 0);
+	eal_log_init(0);
 
 	eal_log_level_parse(argc, argv);
 
lib/eal/windows/eal_log.c +1 -1
index d4ea47f1c824..df995c337998 100644
@@ -8,7 +8,7 @@
 
 /* set the log to default function, called during eal init process. */
 int
-eal_log_init(__rte_unused const char *id, __rte_unused int facility)
+eal_log_init(__rte_unused int facility)
 {
 	rte_openlog_stream(stderr);
 
Signature · 1 line
2.34.1
#8 lore raw

PATCH v3 1/1 eal: simplify argv[0] handling

In reply to Stephen Hemminger

The rte_eal_init function looks at argv[0] to determine the program name to pass to the log init function. But in corner cases argv[0] maybe NULL leading to a SEGV.

The code here is just using argv[0] to generate logid which does not have to be in a static string, openlog() will handle a const char pointer.

Simple workaround for argv[0] being NULL is to pass NULL to openlog() and let it handle it. Both glibc, and musl handle this case.

---

v3 - redo to make this limited to just the null argv[0] bug

 lib/eal/linux/eal.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
lib/eal/linux/eal.c +7 -4
index 9c8395ab14d0..c0ff325c4ce9 100644
@@ -966,8 +966,7 @@ rte_eal_init(int argc, char **argv)
 	pthread_t thread_id;
 	static uint32_t run_once;
 	uint32_t has_run = 0;
-	const char *p;
-	static char logid[PATH_MAX];
+	const char *logid = NULL;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 	bool phys_addrs;
@@ -989,8 +988,12 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	p = strrchr(argv[0], '/');
-	strlcpy(logid, p ? p + 1 : argv[0], sizeof(logid));
+	if (argv && argv[0]) {
+		const char *p = strrchr(argv[0], '/');
+
+		logid = p ? p + 1 : argv[0];
+	}
+
 	thread_id = pthread_self();
 
 	eal_reset_internal_config(internal_conf);
Signature · 1 line
2.34.1