Skip to content

Commit

Permalink
log: remove all uses of %m specifier in pr_* functions
Browse files Browse the repository at this point in the history
As our pr_* functions are complex and can call different system calls
inside before actual printing (e.g. gettimeofday for timestamps) actual
errno at the time of printing may be changed.

Let's just use %s + strerror(errno) instead of %m with pr_* functions to
be explicit that errno to string transformation happens before calling
anything else.

Note: tcp_repair_off is called from pie with no pr_perror defined due to
CR_NOGLIBC set and if I use errno variable there I get "Unexpected
undefined symbol: `__errno_location'. External symbol in PIE?", so it
seems there is no way to print errno there, so let's just skip it.

Signed-off-by: Pavel Tikhomirov <[email protected]>
  • Loading branch information
Snorch authored and avagin committed Jan 25, 2023
1 parent f684c74 commit ac78cbe
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
5 changes: 4 additions & 1 deletion compel/include/log.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef COMPEL_LOG_H__
#define COMPEL_LOG_H__

#include <errno.h>
#include <string.h>

#include "uapi/compel/log.h"

#ifndef LOG_PREFIX
Expand Down Expand Up @@ -45,6 +48,6 @@ extern void compel_print_on_level(unsigned int loglevel, const char *format, ...

#define pr_debug(fmt, ...) compel_print_on_level(COMPEL_LOG_DEBUG, LOG_PREFIX fmt, ##__VA_ARGS__)

#define pr_perror(fmt, ...) pr_err(fmt ": %m\n", ##__VA_ARGS__)
#define pr_perror(fmt, ...) pr_err(fmt ": %s\n", ##__VA_ARGS__, strerror(errno))

#endif /* COMPEL_LOG_H__ */
2 changes: 1 addition & 1 deletion criu/fsnotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static char *alloc_openable(unsigned int s_dev, unsigned long i_ino, FhEntry *f_
return path;
}
} else
pr_debug("\t\t\tnot openable as %s (%m)\n", __path);
pr_debug("\t\t\tnot openable as %s (%s)\n", __path, strerror(errno));
}

err:
Expand Down
2 changes: 1 addition & 1 deletion criu/include/sk-inet.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static inline void tcp_repair_off(int fd)

ret = setsockopt(fd, SOL_TCP, TCP_REPAIR, &aux, sizeof(aux));
if (ret < 0)
pr_err("Failed to turn off repair mode on socket: %m\n");
pr_err("Failed to turn off repair mode on socket\n");
}

extern void tcp_locked_conn_add(struct inet_sk_info *);
Expand Down
4 changes: 2 additions & 2 deletions criu/kerndat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1055,9 +1055,9 @@ static int kerndat_has_move_mount_set_group(void)
exit_code = 0;
out:
if (umount2(tmpdir, MNT_DETACH))
pr_warn("Fail to umount2 %s: %m\n", tmpdir);
pr_warn("Fail to umount2 %s: %s\n", tmpdir, strerror(errno));
if (rmdir(tmpdir))
pr_warn("Fail to rmdir %s: %m\n", tmpdir);
pr_warn("Fail to rmdir %s: %s\n", tmpdir, strerror(errno));
return exit_code;
}

Expand Down
7 changes: 4 additions & 3 deletions criu/sk-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ int kerndat_socket_unix_file(void)
}
fd = ioctl(sk, SIOCUNIXFILE);
if (fd < 0 && errno != ENOENT) {
pr_warn("Unable to open a socket file: %m\n");
pr_warn("Unable to open a socket file: %s\n", strerror(errno));
kdat.sk_unix_file = false;
close(sk);
return 0;
Expand Down Expand Up @@ -620,7 +620,8 @@ static int unix_resolve_name_old(int lfd, uint32_t id, struct unix_sk_desc *d, U
snprintf(rpath, sizeof(rpath), ".%s", name);
if (fstatat(mntns_root, rpath, &st, 0)) {
if (errno != ENOENT) {
pr_warn("Can't stat socket %#" PRIx32 "(%s), skipping: %m (err %d)\n", id, rpath, errno);
pr_warn("Can't stat socket %#" PRIx32 "(%s), skipping: %s (err %d)\n", id, rpath,
strerror(errno), errno);
goto skip;
}

Expand Down Expand Up @@ -669,7 +670,7 @@ static int unix_resolve_name(int lfd, uint32_t id, struct unix_sk_desc *d, UnixS

fd = ioctl(lfd, SIOCUNIXFILE);
if (fd < 0) {
pr_warn("Unable to get a socket file descriptor with SIOCUNIXFILE ioctl: %m\n");
pr_warn("Unable to get a socket file descriptor with SIOCUNIXFILE ioctl: %s\n", strerror(errno));
goto fallback;
}

Expand Down

0 comments on commit ac78cbe

Please sign in to comment.