Skip to content

Commit fa9217b

Browse files
committed
libct/nsenter: add and use bailx
We use bail to report fatal errors, and bail always append %m (aka strerror(errno)). In case an error condition did not set errno, the log message will end up with ": Success" or an error from a stale errno value. Either case is confusing for users. Introduce bailx which is the same as bail except it does not append %m, and use it where appropriate. PS we still use bail in a few cases after read or write, even if that read/write did not return an error, because the code does not distinguish between short read/write and error (-1). This will be addressed by the next commit. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 4b6f0eb commit fa9217b

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

libcontainer/nsenter/log.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,15 @@ extern int logfd;
3636
exit(1); \
3737
} while(0)
3838

39+
/* bailx is the same as bail, except it does not add ": %m" (errno). */
40+
#define bailx(fmt, ...) \
41+
do { \
42+
if (logfd < 0) \
43+
fprintf(stderr, "FATAL: " fmt "\n", ##__VA_ARGS__); \
44+
else \
45+
write_log(FATAL, fmt, ##__VA_ARGS__); \
46+
exit(1); \
47+
} while(0)
48+
3949

4050
#endif /* NSENTER_LOG_H */

libcontainer/nsenter/nsexec.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static int try_mapping_tool(const char *app, int pid, char *map, size_t map_len)
208208
* or programming issue.
209209
*/
210210
if (!app)
211-
bail("mapping tool not present");
211+
bailx("mapping tool not present");
212212

213213
child = fork();
214214
if (child < 0)
@@ -274,7 +274,7 @@ static void update_uidmap(const char *path, int pid, char *map, size_t map_len)
274274
bail("failed to update /proc/%d/uid_map", pid);
275275
write_log(DEBUG, "update /proc/%d/uid_map got -EPERM (trying %s)", pid, path);
276276
if (try_mapping_tool(path, pid, map, map_len))
277-
bail("failed to use newuid map on %d", pid);
277+
bailx("failed to use newuid map on %d", pid);
278278
}
279279
}
280280

@@ -289,7 +289,7 @@ static void update_gidmap(const char *path, int pid, char *map, size_t map_len)
289289
bail("failed to update /proc/%d/gid_map", pid);
290290
write_log(DEBUG, "update /proc/%d/gid_map got -EPERM (trying %s)", pid, path);
291291
if (try_mapping_tool(path, pid, map, map_len))
292-
bail("failed to use newgid map on %d", pid);
292+
bailx("failed to use newgid map on %d", pid);
293293
}
294294
}
295295

@@ -340,14 +340,16 @@ static void nl_parse(int fd, struct nlconfig_t *config)
340340

341341
/* Retrieve the netlink header. */
342342
len = read(fd, &hdr, NLMSG_HDRLEN);
343+
if (len < 0)
344+
bail("failed to read netlink header");
343345
if (len != NLMSG_HDRLEN)
344-
bail("invalid netlink header length %zu", len);
346+
bailx("invalid netlink header length %zu", len);
345347

346348
if (hdr.nlmsg_type == NLMSG_ERROR)
347-
bail("failed to read netlink message");
349+
bailx("failed to read netlink message");
348350

349351
if (hdr.nlmsg_type != INIT_MSG)
350-
bail("unexpected msg type %d", hdr.nlmsg_type);
352+
bailx("unexpected msg type %d", hdr.nlmsg_type);
351353

352354
/* Retrieve data. */
353355
size = NLMSG_PAYLOAD(&hdr, 0);
@@ -356,8 +358,10 @@ static void nl_parse(int fd, struct nlconfig_t *config)
356358
bail("failed to allocate %zu bytes of memory for nl_payload", size);
357359

358360
len = read(fd, data, size);
361+
if (len < 0)
362+
bail("failed to read netlink payload");
359363
if (len != size)
360-
bail("failed to read netlink payload, %zu != %zu", len, size);
364+
bailx("failed to read netlink payload, %zu != %zu", len, size);
361365

362366
/* Parse the netlink payload. */
363367
config->data = data;
@@ -456,7 +460,7 @@ static int nstype(char *name)
456460
* without corresponding handling could result in broken behaviour) and
457461
* the rest of runc doesn't allow unknown namespace types anyway.
458462
*/
459-
bail("unknown namespace type %s", name);
463+
bailx("unknown namespace type %s", name);
460464
}
461465

462466
static nsset_t __open_namespaces(char *nsspec, struct namespace_t **ns_list, size_t *ns_len)
@@ -469,7 +473,7 @@ static nsset_t __open_namespaces(char *nsspec, struct namespace_t **ns_list, siz
469473
namespace = strtok_r(nsspec, ",", &saveptr);
470474

471475
if (!namespace || !strlen(namespace) || !strlen(nsspec))
472-
bail("ns paths are empty");
476+
bailx("ns paths are empty");
473477

474478
do {
475479
int fd;
@@ -485,7 +489,7 @@ static nsset_t __open_namespaces(char *nsspec, struct namespace_t **ns_list, siz
485489
/* Split 'ns:path'. */
486490
path = strstr(namespace, ":");
487491
if (!path)
488-
bail("failed to parse %s", namespace);
492+
bailx("failed to parse %s", namespace);
489493
*path++ = '\0';
490494

491495
fd = open(path, O_RDONLY);
@@ -530,7 +534,7 @@ static nsset_t __join_namespaces(nsset_t allow, struct namespace_t *ns_list, siz
530534
/* Skip permission errors. */
531535
if (saved_errno == EPERM)
532536
continue;
533-
bail("failed to setns into %s namespace", ns->type);
537+
bailx("failed to setns into %s namespace: %s", ns->type, strerror(saved_errno));
534538
}
535539
joined |= type;
536540

@@ -597,7 +601,7 @@ static void __close_namespaces(nsset_t to_join, nsset_t joined, struct namespace
597601

598602
/* Make sure we joined the namespaces we planned to. */
599603
if (failed_to_join)
600-
bail("failed to join {%s} namespaces: %s", nsset_to_str(failed_to_join), strerror(EPERM));
604+
bailx("failed to join {%s} namespaces: %s", nsset_to_str(failed_to_join), strerror(EPERM));
601605

602606
free(ns_list);
603607
}
@@ -939,7 +943,7 @@ void nsexec(void)
939943
stage1_complete = true;
940944
break;
941945
default:
942-
bail("unexpected sync value: %u", s);
946+
bailx("unexpected sync value: %u", s);
943947
}
944948
}
945949
write_log(DEBUG, "<- stage-1 synchronisation loop");
@@ -970,7 +974,7 @@ void nsexec(void)
970974
stage2_complete = true;
971975
break;
972976
default:
973-
bail("unexpected sync value: %u", s);
977+
bailx("unexpected sync value: %u", s);
974978
}
975979
}
976980
write_log(DEBUG, "<- stage-2 synchronisation loop");
@@ -1061,7 +1065,7 @@ void nsexec(void)
10611065
if (read(syncfd, &s, sizeof(s)) != sizeof(s))
10621066
bail("failed to sync with parent: read(SYNC_USERMAP_ACK)");
10631067
if (s != SYNC_USERMAP_ACK)
1064-
bail("failed to sync with parent: SYNC_USERMAP_ACK: got %u", s);
1068+
bailx("failed to sync with parent: SYNC_USERMAP_ACK: got %u", s);
10651069

10661070
/* Revert temporary re-dumpable setting. */
10671071
if (config.namespaces) {
@@ -1097,7 +1101,7 @@ void nsexec(void)
10971101
if (read(syncfd, &s, sizeof(s)) != sizeof(s))
10981102
bail("failed to sync with parent: read(SYNC_TIMEOFFSETS_ACK)");
10991103
if (s != SYNC_TIMEOFFSETS_ACK)
1100-
bail("failed to sync with parent: SYNC_TIMEOFFSETS_ACK: got %u", s);
1104+
bailx("failed to sync with parent: SYNC_TIMEOFFSETS_ACK: got %u", s);
11011105
}
11021106

11031107
/*
@@ -1133,7 +1137,7 @@ void nsexec(void)
11331137
}
11341138
if (s != SYNC_RECVPID_ACK) {
11351139
sane_kill(stage2_pid, SIGKILL);
1136-
bail("failed to sync with parent: SYNC_RECVPID_ACK: got %u", s);
1140+
bailx("failed to sync with parent: SYNC_RECVPID_ACK: got %u", s);
11371141
}
11381142

11391143
write_log(DEBUG, "signal completion to stage-0");
@@ -1180,7 +1184,7 @@ void nsexec(void)
11801184
if (read(syncfd, &s, sizeof(s)) != sizeof(s))
11811185
bail("failed to sync with parent: read(SYNC_GRANDCHILD)");
11821186
if (s != SYNC_GRANDCHILD)
1183-
bail("failed to sync with parent: SYNC_GRANDCHILD: got %u", s);
1187+
bailx("failed to sync with parent: SYNC_GRANDCHILD: got %u", s);
11841188

11851189
if (setsid() < 0)
11861190
bail("setsid failed");
@@ -1215,9 +1219,9 @@ void nsexec(void)
12151219
}
12161220
break;
12171221
default:
1218-
bail("unexpected jump value");
1222+
bailx("unexpected jump value");
12191223
}
12201224

12211225
/* Should never be reached. */
1222-
bail("should never be reached");
1226+
bailx("should never be reached");
12231227
}

0 commit comments

Comments
 (0)