1From cece3ffd5be2f8641eb694513f2b73e5eb97ffd3 Mon Sep 17 00:00:00 2001 2From: Natanael Copa <ncopa@alpinelinux.org> 3Date: Fri, 28 Jan 2022 12:13:30 +0100 4Subject: [PATCH] efisecdb: fix build with musl libc 5 6Refactor code to use POSIX atexit(3) instead of the GNU specific 7on_exit(3). 8 9Resolves: #197 10Resolves: #202 11Signed-off-by: Natanael Copa <ncopa@alpinelinux.org> 12 13Upstream-Status: Backport 14https://github.com/rhboot/efivar/commit/cece3ffd5be2f8641eb694513f2b73e5eb97ffd3 15 16Signed-off-by: Davide Gardenal <davide.gardenal@huawei.com> 17--- 18 src/compiler.h | 2 -- 19 src/efisecdb.c | 68 +++++++++++++++++++------------------------------- 20 2 files changed, 26 insertions(+), 44 deletions(-) 21 22diff --git a/src/compiler.h b/src/compiler.h 23index e2f18f0b..d95fb014 100644 24--- a/src/compiler.h 25+++ b/src/compiler.h 26@@ -7,8 +7,6 @@ 27 #ifndef COMPILER_H_ 28 #define COMPILER_H_ 29 30-#include <sys/cdefs.h> 31- 32 /* GCC version checking borrowed from glibc. */ 33 #if defined(__GNUC__) && defined(__GNUC_MINOR__) 34 # define GNUC_PREREQ(maj,min) \ 35diff --git a/src/efisecdb.c b/src/efisecdb.c 36index f8823737..6bd5ad90 100644 37--- a/src/efisecdb.c 38+++ b/src/efisecdb.c 39@@ -25,6 +25,10 @@ 40 extern char *optarg; 41 extern int optind, opterr, optopt; 42 43+static efi_secdb_t *secdb = NULL; 44+static list_t infiles; 45+static list_t actions; 46+ 47 struct hash_param { 48 char *name; 49 efi_secdb_type_t algorithm; 50@@ -187,12 +191,11 @@ add_action(list_t *list, action_type_t action_type, const efi_guid_t *owner, 51 } 52 53 static void 54-free_actions(int status UNUSED, void *actionsp) 55+free_actions(void) 56 { 57- list_t *actions = (list_t *)actionsp; 58 list_t *pos, *tmp; 59 60- for_each_action_safe(pos, tmp, actions) { 61+ for_each_action_safe(pos, tmp, &actions) { 62 action_t *action = list_entry(pos, action_t, list); 63 64 list_del(&action->list); 65@@ -202,12 +205,11 @@ free_actions(int status UNUSED, void *actionsp) 66 } 67 68 static void 69-free_infiles(int status UNUSED, void *infilesp) 70+free_infiles(void) 71 { 72- list_t *infiles = (list_t *)infilesp; 73 list_t *pos, *tmp; 74 75- for_each_ptr_safe(pos, tmp, infiles) { 76+ for_each_ptr_safe(pos, tmp, &infiles) { 77 ptrlist_t *entry = list_entry(pos, ptrlist_t, list); 78 79 list_del(&entry->list); 80@@ -216,27 +218,12 @@ free_infiles(int status UNUSED, void *infilesp) 81 } 82 83 static void 84-maybe_free_secdb(int status UNUSED, void *voidp) 85+maybe_free_secdb(void) 86 { 87- efi_secdb_t **secdbp = (efi_secdb_t **)voidp; 88- 89- if (secdbp == NULL || *secdbp == NULL) 90+ if (secdb == NULL) 91 return; 92 93- efi_secdb_free(*secdbp); 94-} 95- 96-static void 97-maybe_do_unlink(int status, void *filep) 98-{ 99- char **file = (char **)filep; 100- 101- if (status == 0) 102- return; 103- if (file == NULL || *file == NULL) 104- return; 105- 106- unlink(*file); 107+ efi_secdb_free(secdb); 108 } 109 110 static void 111@@ -323,15 +310,6 @@ parse_input_files(list_t *infiles, char **outfile, efi_secdb_t **secdb, 112 return status; 113 } 114 115-/* 116- * These need to be static globals so that they're not on main's stack when 117- * on_exit() fires. 118- */ 119-static efi_secdb_t *secdb = NULL; 120-static list_t infiles; 121-static list_t actions; 122-static char *outfile = NULL; 123- 124 int 125 main(int argc, char *argv[]) 126 { 127@@ -351,6 +329,7 @@ main(int argc, char *argv[]) 128 bool do_sort_data = false; 129 bool sort_descending = false; 130 int status = 0; 131+ char *outfile = NULL; 132 133 const char sopts[] = ":aAc:dfg:h:i:Lo:rs:t:v?"; 134 const struct option lopts[] = { 135@@ -376,10 +355,9 @@ main(int argc, char *argv[]) 136 INIT_LIST_HEAD(&infiles); 137 INIT_LIST_HEAD(&actions); 138 139- on_exit(free_actions, &actions); 140- on_exit(free_infiles, &infiles); 141- on_exit(maybe_free_secdb, &secdb); 142- on_exit(maybe_do_unlink, &outfile); 143+ atexit(free_actions); 144+ atexit(free_infiles); 145+ atexit(maybe_free_secdb); 146 147 /* 148 * parse the command line. 149@@ -587,24 +565,30 @@ main(int argc, char *argv[]) 150 outfd = open(outfile, flags, 0600); 151 if (outfd < 0) { 152 char *tmpoutfile = outfile; 153- if (errno == EEXIST) 154- outfile = NULL; 155+ if (errno != EEXIST) 156+ unlink(outfile); 157 err(1, "could not open \"%s\"", tmpoutfile); 158 } 159 160 rc = ftruncate(outfd, 0); 161- if (rc < 0) 162+ if (rc < 0) { 163+ unlink(outfile); 164 err(1, "could not truncate output file \"%s\"", outfile); 165+ } 166 167 void *output; 168 size_t size = 0; 169 rc = efi_secdb_realize(secdb, &output, &size); 170- if (rc < 0) 171+ if (rc < 0) { 172+ unlink(outfile); 173 secdb_err(1, "could not realize signature list"); 174+ } 175 176 rc = write(outfd, output, size); 177- if (rc < 0) 178+ if (rc < 0) { 179+ unlink(outfile); 180 err(1, "could not write signature list"); 181+ } 182 183 close(outfd); 184 xfree(output); 185