[CRIU] [PATCH] sysctl: Use open_proc()

Pavel Emelyanov xemul at virtuozzo.com
Fri Apr 14 09:56:40 PDT 2017


Many of sysctl_op-s we have read (or write) single entry.
In current implementaiton this results in two opens and
two closes for each -- open /proc/sys, then open the rest.

It's better to use open_proc() as the latter already have
fd for /proc cached.

Signed-off-by: Pavel Emelyanov <xemul at virtuozzo.com>
---
 criu/sysctl.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)

diff --git a/criu/sysctl.c b/criu/sysctl.c
index 1f3c9af..76349e0 100644
--- a/criu/sysctl.c
+++ b/criu/sysctl.c
@@ -351,23 +351,15 @@ out:
 
 static int __nonuserns_sysctl_op(struct sysctl_req *req, size_t nr_req, int op)
 {
-	int dir, ret, exit_code = -1;;
-
-	dir = open("/proc/sys", O_RDONLY, O_DIRECTORY);
-	if (dir < 0) {
-		pr_perror("Can't open sysctl dir");
-		return -1;
-	}
+	int ret, exit_code = -1;;
 
 	while (nr_req--) {
-		int fd, flags;
+		int fd;
 
 		if (op == CTL_READ)
-			flags = O_RDONLY;
+			fd = open_proc(PROC_GEN, "sys/%s", req->name);
 		else
-			flags = O_WRONLY;
-
-		fd = openat(dir, req->name, flags);
+			fd = open_proc_rw(PROC_GEN, "sys/%s", req->name);
 		if (fd < 0) {
 			if (errno == ENOENT && (req->flags & CTL_FLAGS_OPTIONAL)) {
 				req++;
@@ -394,7 +386,6 @@ static int __nonuserns_sysctl_op(struct sysctl_req *req, size_t nr_req, int op)
 
 	exit_code = 0;
 out:
-	close(dir);
 	return exit_code;
 }
 
-- 
2.5.5


More information about the CRIU mailing list