Skip to content

Commit 3a752c7

Browse files
lib/: Simplify error handling
Print simpler messages that don't need translation. Check libc errors with ==-1 and ==NULL instead of <0 (or !=0) and !p. Signed-off-by: Alejandro Colomar <alx@kernel.org>
1 parent 71e5184 commit 3a752c7

16 files changed

Lines changed: 113 additions & 132 deletions

lib/commonio.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ static int check_link_count (const char *file, bool log)
7272
{
7373
struct stat sb;
7474

75-
if (stat (file, &sb) != 0) {
75+
if (stat(file, &sb) == -1) {
7676
if (log) {
77-
fprinte(log_get_logfd(), "%s: %s file stat error",
77+
fprinte(log_get_logfd(), "%s: stat(\"%s\")",
7878
log_get_progname(), file);
7979
}
8080
return 0;
@@ -104,7 +104,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
104104
fd = open (file, O_CREAT | O_TRUNC | O_WRONLY, 0600);
105105
if (-1 == fd) {
106106
if (log) {
107-
fprinte(log_get_logfd(), "%s: %s", log_get_progname(), file);
107+
fprinte(log_get_logfd(), "%s: open(\"%s\")", log_get_progname(), file);
108108
}
109109
return 0;
110110
}
@@ -114,7 +114,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
114114
len = (ssize_t) strlen (buf) + 1;
115115
if (write_full(fd, buf, len) == -1) {
116116
if (log) {
117-
fprinte(log_get_logfd(), "%s: %s file write error",
117+
fprinte(log_get_logfd(), "%s: write(\"%s\")",
118118
log_get_progname(), file);
119119
}
120120
(void) close (fd);
@@ -123,7 +123,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
123123
}
124124
if (fdatasync (fd) == -1) {
125125
if (log) {
126-
fprinte(log_get_logfd(), "%s: %s file sync error",
126+
fprinte(log_get_logfd(), "%s: fdatasync(\"%s\")",
127127
log_get_progname(), file);
128128
}
129129
(void) close (fd);
@@ -141,7 +141,7 @@ static int do_lock_file (const char *file, const char *lock, bool log)
141141
fd = open (lock, O_RDWR);
142142
if (-1 == fd) {
143143
if (log) {
144-
fprinte(log_get_logfd(), "%s: %s", log_get_progname(), lock);
144+
fprinte(log_get_logfd(), "%s: open(\"%s\")", log_get_progname(), lock);
145145
}
146146
unlink (file);
147147
errno = EINVAL;

lib/copydir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ error_acl(MAYBE_UNUSED struct error_context *_1, const char *fmt, ...)
106106
}
107107

108108
va_start (ap, fmt);
109-
(void) fprintf (log_get_logfd(), _("%s: "), log_get_progname());
109+
fprintf(log_get_logfd(), "%s: ", log_get_progname());
110110
vfprintec(log_get_logfd(), e, fmt, ap);
111111
va_end (ap);
112112

lib/find_new_gid.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ int find_new_gid (bool sys_group,
240240
/* Create an array to hold all of the discovered GIDs */
241241
used_gids = calloc_T(gid_max + 1, bool);
242242
if (NULL == used_gids) {
243-
fprinte(log_get_logfd(), _("%s: failed to allocate memory"),
244-
log_get_progname());
243+
fprinte(log_get_logfd(), "%s: calloc", log_get_progname());
245244
return -1;
246245
}
247246

lib/find_new_uid.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ int find_new_uid(bool sys_user,
239239
/* Create an array to hold all of the discovered UIDs */
240240
used_uids = calloc_T(uid_max + 1, bool);
241241
if (NULL == used_uids) {
242-
fprinte(log_get_logfd(), _("%s: failed to allocate memory"),
243-
log_get_progname());
242+
fprinte(log_get_logfd(), "%s: calloc", log_get_progname());
244243
return -1;
245244
}
246245

lib/get_pid.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ int open_pidfd(const char *pidstr)
6060
return -ENOENT;
6161

6262
if (stprintf_a(proc_dir_name, "/proc/%d/", target) == -1) {
63-
eprinte("snprintf of proc path failed for %d", target);
63+
eprinte("snprintf");
6464
return -EINVAL;
6565
}
6666

6767
proc_dir_fd = open(proc_dir_name, O_DIRECTORY);
68-
if (proc_dir_fd < 0) {
69-
eprinte(_("Could not open proc directory for target %d"), target);
68+
if (proc_dir_fd == -1) {
69+
eprinte("open(\"%s\")", proc_dir_name);
7070
return -EINVAL;
7171
}
7272
return proc_dir_fd;

lib/gettime.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ gettime(void)
3838
return fallback;
3939

4040
if (a2i(time_t, &epoch, source_date_epoch, NULL, 10, 0, fallback) == -1) {
41-
fprinte(log_get_logfd(),
42-
_("Environment variable $SOURCE_DATE_EPOCH: a2i(\"%s\")"),
41+
fprinte(log_get_logfd(), "a2i(SOURCE_DATE_EPOCH=\"%s\")",
4342
source_date_epoch);
4443
return fallback;
4544
}

lib/idmapping.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ get_map_ranges(int ranges, int argc, char **argv)
4949

5050
mappings = calloc_T(ranges, struct map_range);
5151
if (!mappings) {
52-
fprintf(log_get_logfd(), _( "%s: Memory allocation failure\n"),
53-
log_get_progname());
52+
fprinte(log_get_logfd(), "%s: calloc:", log_get_progname());
5453
return NULL;
5554
}
5655

@@ -150,12 +149,12 @@ void write_mapping(int proc_dir_fd, int ranges, const struct map_range *mappings
150149
/* Align setuid- and fscaps-based new{g,u}idmap behavior. */
151150
if (geteuid() == 0 && geteuid() != ruid) {
152151
if (prctl(PR_SET_KEEPCAPS, 1L) == -1) {
153-
fprintf(log_get_logfd(), _("%s: Could not prctl(PR_SET_KEEPCAPS)\n"), log_get_progname());
152+
fprinte(log_get_logfd(), "%s: prctl(PR_SET_KEEPCAPS)", log_get_progname());
154153
exit(EXIT_FAILURE);
155154
}
156155

157-
if (seteuid(ruid) < 0) {
158-
fprintf(log_get_logfd(), _("%s: Could not seteuid to %d\n"), log_get_progname(), ruid);
156+
if (seteuid(ruid) == -1) {
157+
fprinte(log_get_logfd(), "%s: seteuid(%d)", log_get_progname(), ruid);
159158
exit(EXIT_FAILURE);
160159
}
161160
}
@@ -170,8 +169,8 @@ void write_mapping(int proc_dir_fd, int ranges, const struct map_range *mappings
170169
if (maps_lower_root(cap, ranges, mappings))
171170
data[0].effective |= CAP_TO_MASK(CAP_SETFCAP);
172171
data[0].permitted = data[0].effective;
173-
if (capset(&hdr, data) < 0) {
174-
fprintf(log_get_logfd(), _("%s: Could not set caps\n"), log_get_progname());
172+
if (capset(&hdr, data) == -1) {
173+
fprinte(log_get_logfd(), "%s: capset", log_get_progname());
175174
exit(EXIT_FAILURE);
176175
}
177176
#endif
@@ -190,24 +189,24 @@ void write_mapping(int proc_dir_fd, int ranges, const struct map_range *mappings
190189
mapping->count);
191190
}
192191
if (pos == end || pos == NULL) {
193-
fprintf(log_get_logfd(), _("%s: seprintf failed!\n"), log_get_progname());
192+
fprinte(log_get_logfd(), "%s: seprintf", log_get_progname());
194193
exit(EXIT_FAILURE);
195194
}
196195

197196
/* Write the mapping to the mapping file */
198197
fd = openat(proc_dir_fd, map_file, O_WRONLY);
199-
if (fd < 0) {
200-
fprinte(log_get_logfd(), _("%s: open of %s failed"),
198+
if (fd == -1) {
199+
fprinte(log_get_logfd(), "%s: openat(%s)",
201200
log_get_progname(), map_file);
202201
exit(EXIT_FAILURE);
203202
}
204203
if (write_full(fd, buf, pos - buf) == -1) {
205-
fprinte(log_get_logfd(), _("%s: write to %s failed"),
204+
fprinte(log_get_logfd(), "%s: write(\"%s\")",
206205
log_get_progname(), map_file);
207206
exit(EXIT_FAILURE);
208207
}
209-
if (close(fd) != 0 && errno != EINTR) {
210-
fprinte(log_get_logfd(), _("%s: closing %s failed"),
208+
if (close(fd) == -1 && errno != EINTR) {
209+
fprinte(log_get_logfd(), "%s: close(\"%s\")",
211210
log_get_progname(), map_file);
212211
exit(EXIT_FAILURE);
213212
}

lib/nss.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include <stdatomic.h>
1111

1212
#include "alloc/malloc.h"
13+
#include "io/fprintf/fprinte.h"
1314
#include "prototypes.h"
14-
#include "../libsubid/subid.h"
1515
#include "shadowlog.h"
1616
#include "string/sprintf/stprintf.h"
1717
#include "string/strcmp/strcaseprefix.h"
@@ -20,6 +20,8 @@
2020
#include "string/strspn/stpspn.h"
2121
#include "string/strtok/stpsep.h"
2222

23+
#include "../libsubid/subid.h"
24+
2325

2426
#define NSSWITCH "/etc/nsswitch.conf"
2527

@@ -70,9 +72,9 @@ nss_init(const char *nsswitch_path) {
7072
// read nsswitch.conf to check for a line like:
7173
// subid: files
7274
nssfp = fopen(nsswitch_path, "r");
73-
if (!nssfp) {
75+
if (nssfp == NULL) {
7476
if (errno != ENOENT)
75-
fprintf(log_get_logfd(), "Failed opening %s: %m\n", nsswitch_path);
77+
fprinte(log_get_logfd(), "fopen(\"%s\")", nsswitch_path);
7678

7779
atomic_store(&nss_init_completed, true);
7880
return;

lib/prefix_flag.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
8989

9090
if (prefix != NULL) {
9191
/* Drop privileges */
92-
if ( (setregid (getgid (), getgid ()) != 0)
93-
|| (setreuid (getuid (), getuid ()) != 0)) {
94-
fprinte(log_get_logfd(), _("%s: failed to drop privileges"),
95-
log_get_progname());
92+
if (setregid(getgid(), getgid()) == -1) {
93+
fprinte(log_get_logfd(), "%s: setregid", log_get_progname());
94+
exit (EXIT_FAILURE);
95+
}
96+
if (setreuid(getuid(), getuid()) == -1) {
97+
fprinte(log_get_logfd(), "%s: setreuid", log_get_progname());
9698
exit (EXIT_FAILURE);
9799
}
98100

lib/root_flag.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ extern void process_root_flag (const char* short_opt, int argc, char **argv)
7373
static void change_root (const char* newroot)
7474
{
7575
/* Drop privileges */
76-
if ( (setregid (getgid (), getgid ()) != 0)
77-
|| (setreuid (getuid (), getuid ()) != 0)) {
78-
fprinte(log_get_logfd(), _("%s: failed to drop privileges"),
79-
log_get_progname());
76+
if (setregid(getgid(), getgid()) == -1) {
77+
fprinte(log_get_logfd(), "%s: setregid", log_get_progname());
78+
exit (EXIT_FAILURE);
79+
}
80+
if (setreuid(getuid(), getuid()) == -1) {
81+
fprinte(log_get_logfd(), "%s: setreuid", log_get_progname());
8082
exit (EXIT_FAILURE);
8183
}
8284

@@ -87,20 +89,20 @@ static void change_root (const char* newroot)
8789
exit (E_BAD_ARG);
8890
}
8991

90-
if (access (newroot, F_OK) != 0) {
91-
fprinte(log_get_logfd(), _("%s: cannot access chroot directory %s"),
92+
if (access(newroot, F_OK) == -1) {
93+
fprinte(log_get_logfd(), "%s: access(\"%s\", F_OK)",
9294
log_get_progname(), newroot);
9395
exit (E_BAD_ARG);
9496
}
9597

96-
if (chroot (newroot) != 0) {
97-
fprinte(log_get_logfd(), _("%s: unable to chroot to directory %s"),
98+
if (chroot(newroot) == -1) {
99+
fprinte(log_get_logfd(), "%s: chroot(\"%s\")",
98100
log_get_progname(), newroot);
99101
exit (E_BAD_ARG);
100102
}
101103

102-
if (chdir ("/") != 0) {
103-
fprinte(log_get_logfd(), _("%s: cannot chdir in chroot directory %s"),
104+
if (chdir("/") == -1) {
105+
fprinte(log_get_logfd(), "%s: chdir(\"%s\")",
104106
log_get_progname(), newroot);
105107
exit (E_BAD_ARG);
106108
}

0 commit comments

Comments
 (0)