Skip to contentSkip to conversation

livepatch-tools: remove usage of error.h

PATCH Superseded Roger Pau Monne 2 participants 4 messages Apr 6, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 livepatch-tools: remove usage of error.h

It's a GNU libc specific header which prevents building on musl for example. Instead open code an equivalent replacement for the usage of ERROR() and DIFF_FATAL() macros.

---
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Ross Lagerwall <[email protected]>

---
 common.h             | 10 ++++++----
 create-diff-object.c |  1 -
 lookup.c             |  7 +++++--
 prelink.c            |  1 -
 4 files changed, 11 insertions(+), 8 deletions(-)
common.h +6 -4
index 9a9da79..ec2ea33 100644
@@ -1,18 +1,20 @@
 #ifndef _COMMON_H_
 #define _COMMON_H_
 
-#include <error.h>
-
 extern char *childobj;
 
 #define ERROR(format, ...) \
-	error(1, 0, "ERROR: %s: %s: %d: " format, childobj, __FUNCTION__, __LINE__, ##__VA_ARGS__)
+({ \
+	fflush(stdout); \
+	fprintf(stderr, "ERROR: %s: %s: %d: " format "\n", childobj, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
+	exit(1); \
+})
 
 #define DIFF_FATAL(format, ...) \
 ({ \
 	fflush(stdout); \
 	fprintf(stderr, "ERROR: %s: " format "\n", childobj, ##__VA_ARGS__); \
-	error(2, 0, "unreconcilable difference"); \
+	exit(2); \
 })
 
 #define log_debug(format, ...) log(DEBUG, format, ##__VA_ARGS__)
create-diff-object.c +0 -1
index 780e6c8..d8a0032 100644
@@ -45,7 +45,6 @@
 #include <string.h>
 #include <libgen.h>
 #include <argp.h>
-#include <error.h>
 #include <unistd.h>
 #include <time.h>
 #include <gelf.h>
lookup.c +5 -2
index 39125c6..b440102 100644
@@ -28,14 +28,17 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <error.h>
 #include <gelf.h>
 #include <unistd.h>
 
 #include "lookup.h"
 
 #define ERROR(format, ...) \
-	error(1, 0, "%s: %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__)
+({ \
+	fflush(stdout); \
+	fprintf(stderr, "%s: %d: " format, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
+	exit(1); \
+})
 
 struct symbol {
 	unsigned long value;
prelink.c +0 -1
index 2039e5b..18c5159 100644
@@ -27,7 +27,6 @@
 #include <string.h>
 #include <libgen.h>
 #include <argp.h>
-#include <error.h>
 #include <unistd.h>
 #include <gelf.h>
 
Signature · 1 line
2.40.0
#1 lore raw
Quoted 32 lines · Roger Pau Monne

It's a GNU libc specific header which prevents building on musl for example. Instead open code an equivalent replacement for the usage of ERROR() and DIFF_FATAL() macros.

---
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Ross Lagerwall <[email protected]>

---
 common.h             | 10 ++++++----
 create-diff-object.c |  1 -
 lookup.c             |  7 +++++--
 prelink.c            |  1 -
 4 files changed, 11 insertions(+), 8 deletions(-)
common.h +3 -3
index 9a9da79..ec2ea33 100644
@@ -1,18 +1,20 @@
 #ifndef _COMMON_H_
 #define _COMMON_H_
 
-#include <error.h>
-
 extern char *childobj;
 
 #define ERROR(format, ...) \
-	error(1, 0, "ERROR: %s: %s: %d: " format, childobj, __FUNCTION__, __LINE__, ##__VA_ARGS__)
+({ \
+	fflush(stdout); \
+	fprintf(stderr, "ERROR: %s: %s: %d: " format "\n", childobj, __FUNCTION__, __LINE__, ##__VA_ARGS__); \

common.h line 18

Quoted 10 lines
common.h +3 -1
+	exit(1); \
+})
 
 #define DIFF_FATAL(format, ...) \
 ({ \
 	fflush(stdout); \
 	fprintf(stderr, "ERROR: %s: " format "\n", childobj, ##__VA_ARGS__); \
-	error(2, 0, "unreconcilable difference"); \
+	exit(2); \
 })

Looking at the usage, can't we just use err() instead?

Also, I suspect you don't intend to delete the error message in DIFF_FATAL() ?

~Andrew

#2 lore raw
Quoted 36 lines · Andrew Cooper
Quoted 35 lines · Roger Pau Monne

It's a GNU libc specific header which prevents building on musl for example. Instead open code an equivalent replacement for the usage of ERROR() and DIFF_FATAL() macros.

---
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Ross Lagerwall <[email protected]>

---
 common.h             | 10 ++++++----
 create-diff-object.c |  1 -
 lookup.c             |  7 +++++--
 prelink.c            |  1 -
 4 files changed, 11 insertions(+), 8 deletions(-)
common.h +5 -3
index 9a9da79..ec2ea33 100644
@@ -1,18 +1,20 @@
 #ifndef _COMMON_H_
 #define _COMMON_H_
 
-#include <error.h>
-
 extern char *childobj;
 
 #define ERROR(format, ...) \
-	error(1, 0, "ERROR: %s: %s: %d: " format, childobj, __FUNCTION__, __LINE__, ##__VA_ARGS__)
+({ \
+	fflush(stdout); \
+	fprintf(stderr, "ERROR: %s: %s: %d: " format "\n", childobj, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
+	exit(1); \
+})
 
Quoted 9 lines

common.h line 18

Quoted 7 lines
common.h +1 -1
 #define DIFF_FATAL(format, ...) \
 ({ \
 	fflush(stdout); \
 	fprintf(stderr, "ERROR: %s: " format "\n", childobj, ##__VA_ARGS__); \
-	error(2, 0, "unreconcilable difference"); \
+	exit(2); \
 })

Looking at the usage, can't we just use err() instead?

Hm, err() will unconditionaly use errno, which doesn't seem wanted here, as in both cases errnum is passed as 0, effectively preventing printing it.

I could use errx(), as that doesn't append an error message, I think that's available on musl.

Let me know if you agree with that substitution.

Quoted 2 lines

Also, I suspect you don't intend to delete the error message in DIFF_FATAL() ?

I didn't think it was that helpful, but I could keep it.

Thanks, Roger.

#3 lore raw
Quoted 45 lines · Roger Pau Monné
Quoted 36 lines · Andrew Cooper
Quoted 35 lines · Roger Pau Monne

It's a GNU libc specific header which prevents building on musl for example. Instead open code an equivalent replacement for the usage of ERROR() and DIFF_FATAL() macros.

---
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Ross Lagerwall <[email protected]>

---
 common.h             | 10 ++++++----
 create-diff-object.c |  1 -
 lookup.c             |  7 +++++--
 prelink.c            |  1 -
 4 files changed, 11 insertions(+), 8 deletions(-)
common.h +5 -3
index 9a9da79..ec2ea33 100644
@@ -1,18 +1,20 @@
 #ifndef _COMMON_H_
 #define _COMMON_H_
 
-#include <error.h>
-
 extern char *childobj;
 
 #define ERROR(format, ...) \
-	error(1, 0, "ERROR: %s: %s: %d: " format, childobj, __FUNCTION__, __LINE__, ##__VA_ARGS__)
+({ \
+	fflush(stdout); \
+	fprintf(stderr, "ERROR: %s: %s: %d: " format "\n", childobj, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
+	exit(1); \
+})
 
Quoted 8 lines

common.h line 18

Quoted 7 lines
common.h +1 -1
 #define DIFF_FATAL(format, ...) \
 ({ \
 	fflush(stdout); \
 	fprintf(stderr, "ERROR: %s: " format "\n", childobj, ##__VA_ARGS__); \
-	error(2, 0, "unreconcilable difference"); \
+	exit(2); \
 })

Looking at the usage, can't we just use err() instead?

Quoted 8 lines

Hm, err() will unconditionaly use errno, which doesn't seem wanted here, as in both cases errnum is passed as 0, effectively preventing printing it.

I could use errx(), as that doesn't append an error message, I think that's available on musl.

Let me know if you agree with that substitution.

Yeah, anything in err.h ought to be fine.

Quoted 4 lines
Quoted 3 lines

Also, I suspect you don't intend to delete the error message in DIFF_FATAL() ?

I didn't think it was that helpful, but I could keep it.

I'd be hesitant to drop it, considering how much shell parsing there is of these tools.

But ultimately it's up to Konrad/Ross.

~Andrew