[CRIU] [PATCH 3/3] sfd: Print readable name of sfd type in errors

Cyrill Gorcunov gorcunov at gmail.com
Fri Mar 29 20:55:24 MSK 2019


Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
 criu/include/servicefd.h |  5 +++--
 criu/servicefd.c         | 34 +++++++++++++++++++++++++++++++---
 2 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/criu/include/servicefd.h b/criu/include/servicefd.h
index 1c4349e4eeb0..8f879cbf20ba 100644
--- a/criu/include/servicefd.h
+++ b/criu/include/servicefd.h
@@ -38,11 +38,12 @@ extern bool sfds_protected;
 											\
 		snprintf(self_path, sizeof(self_path), "/proc/self/fd/%d", _new_fd);	\
 		if (access(self_path, F_OK)) {						\
-			pr_warn("type %d: busy target %d -> %d\n",			\
-				_type, _old_fd, _new_fd);				\
+			pr_warn("%s busy target %d -> %d\n",				\
+				sfd_type_name(_type), _old_fd, _new_fd);		\
 		}									\
 	} while (0)
 
+extern const char *sfd_type_name(enum sfd_type type);
 extern int init_service_fd(void);
 extern int get_service_fd(enum sfd_type type);
 extern bool is_any_service_fd(int fd);
diff --git a/criu/servicefd.c b/criu/servicefd.c
index bb8c26ca466c..9c3c4e890427 100644
--- a/criu/servicefd.c
+++ b/criu/servicefd.c
@@ -38,6 +38,31 @@ static int sfd_arr[SERVICE_FD_MAX];
  */
 bool sfds_protected = false;
 
+const char *sfd_type_name(enum sfd_type type)
+{
+	static const char *names[] = {
+		[SERVICE_FD_MIN]	= __stringify_1(SERVICE_FD_MIN),
+		[LOG_FD_OFF]		= __stringify_1(LOG_FD_OFF),
+		[IMG_FD_OFF]		= __stringify_1(IMG_FD_OFF),
+		[PROC_FD_OFF]		= __stringify_1(PROC_FD_OFF),
+		[PROC_PID_FD_OFF]	= __stringify_1(PROC_PID_FD_OFF),
+		[CR_PROC_FD_OFF]	= __stringify_1(CR_PROC_FD_OFF),
+		[ROOT_FD_OFF]		= __stringify_1(ROOT_FD_OFF),
+		[CGROUP_YARD]		= __stringify_1(CGROUP_YARD),
+		[USERNSD_SK]		= __stringify_1(USERNSD_SK),
+		[NS_FD_OFF]		= __stringify_1(NS_FD_OFF),
+		[TRANSPORT_FD_OFF]	= __stringify_1(TRANSPORT_FD_OFF),
+		[RPC_SK_OFF]		= __stringify_1(RPC_SK_OFF),
+		[FDSTORE_SK_OFF]	= __stringify_1(FDSTORE_SK_OFF),
+		[SERVICE_FD_MAX]	= __stringify_1(SERVICE_FD_MAX),
+	};
+
+	if (type < ARRAY_SIZE(names))
+		return names[type];
+
+	return "UNKNOWN";
+}
+
 int init_service_fd(void)
 {
 	struct rlimit64 rlimit;
@@ -105,7 +130,8 @@ int service_fd_min_fd(struct pstree_item *item)
 
 static void sfds_protection_bug(enum sfd_type type)
 {
-	pr_err("Service fd %u is being modified in protected context\n", type);
+	pr_err("Service fd %s is being modified in protected context\n",
+	       sfd_type_name(type));
 	print_stack_trace(current ? vpid(current) : 0);
 	BUG();
 }
@@ -128,7 +154,8 @@ int install_service_fd(enum sfd_type type, int fd)
 
 	sfd_verify_targtet(type, fd, sfd);
 	if (dup3(fd, sfd, O_CLOEXEC) != sfd) {
-		pr_perror("Dup %d -> %d failed", fd, sfd);
+		pr_perror("%s dup %d -> %d failed",
+			  sfd_type_name(type), fd, sfd);
 		close(fd);
 		return -1;
 	}
@@ -169,7 +196,8 @@ static void move_service_fd(struct pstree_item *me, int type, int new_id, int ne
 	ret = dup2(old, new);
 	if (ret == -1) {
 		if (errno != EBADF)
-			pr_perror("Unable to clone %d->%d", old, new);
+			pr_perror("%s unable to clone %d->%d",
+				  sfd_type_name(type), old, new);
 	} else if (!(rsti(me)->clone_flags & CLONE_FILES))
 		close(old);
 }
-- 
2.20.1



More information about the CRIU mailing list