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

Submitted by Cyrill Gorcunov on March 29, 2019, 5:55 p.m.

Details

Message ID 20190329175524.7145-4-gorcunov@gmail.com
State New
Series "sfd: Move service routines into separate file"
Headers show

Commit Message

Cyrill Gorcunov March 29, 2019, 5:55 p.m.
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
---
 criu/include/servicefd.h |  5 +++--
 criu/servicefd.c         | 34 +++++++++++++++++++++++++++++++---
 2 files changed, 34 insertions(+), 5 deletions(-)

Patch hide | download patch | download mbox

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);
 }