[v3,06/10] files: Refactor clone_service_fd()

Submitted by Kirill Tkhai on Dec. 29, 2017, 9:36 a.m.

Details

Message ID 151454016182.19480.16863195557230182390.stgit@localhost.localdomain
State New
Series "Introduce custom per-task service fds placement"
Headers show

Commit Message

Kirill Tkhai Dec. 29, 2017, 9:36 a.m.
This patch just moves part of clone_service_fd()
to separate function, that change readability of the code.

There are no functional changes, only refactoring.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
---
 criu/util.c |   36 ++++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/util.c b/criu/util.c
index a1ce72f8b..ec16cffee 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -524,30 +524,34 @@  int close_service_fd(enum sfd_type type)
 	return 0;
 }
 
+static void move_service_fd(struct pstree_item *me, int type, int new_id, int new_base)
+{
+	int old = get_service_fd(type);
+	int new = __get_service_fd(type, new_id);
+	int ret;
+
+	if (old < 0)
+		return;
+	ret = dup2(old, new);
+	if (ret == -1) {
+		if (errno != EBADF)
+			pr_perror("Unable to clone %d->%d", old, new);
+	} else if (!(rsti(me)->clone_flags & CLONE_FILES))
+		close(old);
+}
+
 int clone_service_fd(struct pstree_item *me)
 {
-	int id, i, ret = -1;
+	int id, new_base, i, ret = -1;
 
+	new_base = service_fd_base;
 	id = rsti(me)->service_fd_id;
 
 	if (service_fd_id == id)
 		return 0;
 
-	for (i = SERVICE_FD_MIN + 1; i < SERVICE_FD_MAX; i++) {
-		int old = get_service_fd(i);
-		int new = __get_service_fd(i, id);
-
-		if (old < 0)
-			continue;
-		ret = dup2(old, new);
-		if (ret == -1) {
-			if (errno == EBADF)
-				continue;
-			pr_perror("Unable to clone %d->%d", old, new);
-		}
-		if (ret >= 0 && !(rsti(me)->clone_flags & CLONE_FILES))
-			close(old);
-	}
+	for (i = SERVICE_FD_MIN + 1; i < SERVICE_FD_MAX; i++)
+		move_service_fd(me, i, id, new_base);
 
 	service_fd_id = id;
 	ret = 0;