1 |
From a61ea6379ae9dbb63fbf022d1456733520db6be7 Mon Sep 17 00:00:00 2001 |
2 |
From: Masami Hiramatsu <mhiramat@kernel.org> |
3 |
Date: Thu, 19 Nov 2020 14:53:22 +0900 |
4 |
Subject: [PATCH 101/150] tools/bootconfig: Fix errno reference after printf() |
5 |
|
6 |
Fix not to refer the errno variable as the result of previous libc |
7 |
functions after printf() because printf() can change the errno. |
8 |
|
9 |
Link: https://lkml.kernel.org/r/160576520243.320071.51093664672431249.stgit@devnote2 |
10 |
|
11 |
Fixes: 85c46b78da58 ("bootconfig: Add bootconfig magic word for indicating bootconfig explicitly") |
12 |
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> |
13 |
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> |
14 |
--- |
15 |
tools/bootconfig/main.c | 52 +++++++++++++++++++++++------------------ |
16 |
1 file changed, 29 insertions(+), 23 deletions(-) |
17 |
|
18 |
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c |
19 |
index eb92027817a7..52eb2bbe8966 100644 |
20 |
--- a/tools/bootconfig/main.c |
21 |
+++ b/tools/bootconfig/main.c |
22 |
@@ -147,6 +147,12 @@ static int load_xbc_file(const char *path, char **buf) |
23 |
return ret; |
24 |
} |
25 |
|
26 |
+static int pr_errno(const char *msg, int err) |
27 |
+{ |
28 |
+ pr_err("%s: %d\n", msg, err); |
29 |
+ return err; |
30 |
+} |
31 |
+ |
32 |
static int load_xbc_from_initrd(int fd, char **buf) |
33 |
{ |
34 |
struct stat stat; |
35 |
@@ -162,26 +168,24 @@ static int load_xbc_from_initrd(int fd, char **buf) |
36 |
if (stat.st_size < 8 + BOOTCONFIG_MAGIC_LEN) |
37 |
return 0; |
38 |
|
39 |
- if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) { |
40 |
- pr_err("Failed to lseek: %d\n", -errno); |
41 |
- return -errno; |
42 |
- } |
43 |
+ if (lseek(fd, -BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0) |
44 |
+ return pr_errno("Failed to lseek for magic", -errno); |
45 |
+ |
46 |
if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0) |
47 |
- return -errno; |
48 |
+ return pr_errno("Failed to read", -errno); |
49 |
+ |
50 |
/* Check the bootconfig magic bytes */ |
51 |
if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0) |
52 |
return 0; |
53 |
|
54 |
- if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) { |
55 |
- pr_err("Failed to lseek: %d\n", -errno); |
56 |
- return -errno; |
57 |
- } |
58 |
+ if (lseek(fd, -(8 + BOOTCONFIG_MAGIC_LEN), SEEK_END) < 0) |
59 |
+ return pr_errno("Failed to lseek for size", -errno); |
60 |
|
61 |
if (read(fd, &size, sizeof(u32)) < 0) |
62 |
- return -errno; |
63 |
+ return pr_errno("Failed to read size", -errno); |
64 |
|
65 |
if (read(fd, &csum, sizeof(u32)) < 0) |
66 |
- return -errno; |
67 |
+ return pr_errno("Failed to read checksum", -errno); |
68 |
|
69 |
/* Wrong size error */ |
70 |
if (stat.st_size < size + 8 + BOOTCONFIG_MAGIC_LEN) { |
71 |
@@ -190,10 +194,8 @@ static int load_xbc_from_initrd(int fd, char **buf) |
72 |
} |
73 |
|
74 |
if (lseek(fd, stat.st_size - (size + 8 + BOOTCONFIG_MAGIC_LEN), |
75 |
- SEEK_SET) < 0) { |
76 |
- pr_err("Failed to lseek: %d\n", -errno); |
77 |
- return -errno; |
78 |
- } |
79 |
+ SEEK_SET) < 0) |
80 |
+ return pr_errno("Failed to lseek", -errno); |
81 |
|
82 |
ret = load_xbc_fd(fd, buf, size); |
83 |
if (ret < 0) |
84 |
@@ -262,14 +264,16 @@ static int show_xbc(const char *path, bool list) |
85 |
|
86 |
ret = stat(path, &st); |
87 |
if (ret < 0) { |
88 |
- pr_err("Failed to stat %s: %d\n", path, -errno); |
89 |
- return -errno; |
90 |
+ ret = -errno; |
91 |
+ pr_err("Failed to stat %s: %d\n", path, ret); |
92 |
+ return ret; |
93 |
} |
94 |
|
95 |
fd = open(path, O_RDONLY); |
96 |
if (fd < 0) { |
97 |
- pr_err("Failed to open initrd %s: %d\n", path, fd); |
98 |
- return -errno; |
99 |
+ ret = -errno; |
100 |
+ pr_err("Failed to open initrd %s: %d\n", path, ret); |
101 |
+ return ret; |
102 |
} |
103 |
|
104 |
ret = load_xbc_from_initrd(fd, &buf); |
105 |
@@ -307,8 +311,9 @@ static int delete_xbc(const char *path) |
106 |
|
107 |
fd = open(path, O_RDWR); |
108 |
if (fd < 0) { |
109 |
- pr_err("Failed to open initrd %s: %d\n", path, fd); |
110 |
- return -errno; |
111 |
+ ret = -errno; |
112 |
+ pr_err("Failed to open initrd %s: %d\n", path, ret); |
113 |
+ return ret; |
114 |
} |
115 |
|
116 |
size = load_xbc_from_initrd(fd, &buf); |
117 |
@@ -383,9 +388,10 @@ static int apply_xbc(const char *path, const char *xbc_path) |
118 |
/* Apply new one */ |
119 |
fd = open(path, O_RDWR | O_APPEND); |
120 |
if (fd < 0) { |
121 |
- pr_err("Failed to open %s: %d\n", path, fd); |
122 |
+ ret = -errno; |
123 |
+ pr_err("Failed to open %s: %d\n", path, ret); |
124 |
free(data); |
125 |
- return fd; |
126 |
+ return ret; |
127 |
} |
128 |
/* TODO: Ensure the @path is initramfs/initrd image */ |
129 |
ret = write(fd, data, size + 8); |
130 |
-- |
131 |
2.29.2 |
132 |
|