[CRIU] [PATCH] Use *open_proc* where possible

Kir Kolyshkin kir at openvz.org
Tue Mar 28 18:10:34 PDT 2017


Using open_proc/fopen_proc/__open_proc is better since
 - it uses openat
 - it comes with nice error reporting

Let's use it in places where we can. Even if it does not give any
improvements (such as in cr-check.c), error message unification
is good enough reason to do so.

Requested-by: Pavel Emelyanov <xemul at virtuozzo.com>
Signed-off-by: Kir Kolyshkin <kir at openvz.org>
---
 criu/autofs.c     |  6 ++----
 criu/cr-check.c   | 12 ++++--------
 criu/kerndat.c    |  9 +++------
 criu/namespaces.c | 14 ++++----------
 criu/net.c        |  6 ++----
 5 files changed, 15 insertions(+), 32 deletions(-)

diff --git a/criu/autofs.c b/criu/autofs.c
index a436e9f..15b0fc4 100644
--- a/criu/autofs.c
+++ b/criu/autofs.c
@@ -359,11 +359,9 @@ static int access_autofs_mount(struct mount_info *pm)
 	if (new_pid_ns < 0)
 		return -1;
 
-	old_pid_ns = open("/proc/self/ns/pid", O_RDONLY);
-	if (old_pid_ns < 0) {
-		pr_perror("Can't open /proc/self/ns/pid");
+	old_pid_ns = open_proc(PROC_SELF, "ns/pid");
+	if (old_pid_ns < 0)
 		goto close_new_pid_ns;
-	}
 
 	if (switch_ns(pm->nsid->ns_pid, &mnt_ns_desc, &old_mnt_ns)) {
 		pr_err("failed to switch to mount namespace\n");
diff --git a/criu/cr-check.c b/criu/cr-check.c
index 176eafb..28e8db1 100644
--- a/criu/cr-check.c
+++ b/criu/cr-check.c
@@ -245,11 +245,9 @@ static int check_fcntl(void)
 	u32 v[2];
 	int fd;
 
-	fd = open("/proc/self/comm", O_RDONLY);
-	if (fd < 0) {
-		pr_perror("Can't open self comm file");
+	fd = open_proc(PROC_SELF, "comm");
+	if (fd < 0)
 		return -1;
-	}
 
 	if (fcntl(fd, F_GETOWNER_UIDS, (long)v)) {
 		pr_perror("Can'r fetch file owner UIDs");
@@ -727,11 +725,9 @@ static unsigned long get_ring_len(unsigned long addr)
 	FILE *maps;
 	char buf[256];
 
-	maps = fopen("/proc/self/maps", "r");
-	if (!maps) {
-		pr_perror("No maps proc file");
+	maps = fopen_proc(PROC_SELF, "maps");
+	if (!maps)
 		return 0;
-	}
 
 	while (fgets(buf, sizeof(buf), maps)) {
 		unsigned long start, end;
diff --git a/criu/kerndat.c b/criu/kerndat.c
index 354c2a5..22dd1e8 100644
--- a/criu/kerndat.c
+++ b/criu/kerndat.c
@@ -297,9 +297,8 @@ int kerndat_get_dirty_track(void)
 		goto no_dt;
 
 	ret = -1;
-	pm2 = open("/proc/self/pagemap", O_RDONLY);
+	pm2 = open_proc(PROC_SELF, "pagemap");
 	if (pm2 < 0) {
-		pr_perror("Can't open pagemap file");
 		munmap(map, PAGE_SIZE);
 		return ret;
 	}
@@ -400,11 +399,9 @@ int kerndat_fdinfo_has_lock()
 	int fd, pfd = -1, exit_code = -1, len;
 	char buf[PAGE_SIZE];
 
-	fd = open("/proc/locks", O_RDONLY);
-	if (fd < 0) {
-		pr_perror("Unable to open /proc/locks");
+	fd = open_proc(PROC_GEN, "locks");
+	if (fd < 0)
 		return -1;
-	}
 
 	if (flock(fd, LOCK_SH)) {
 		pr_perror("Can't take a lock");
diff --git a/criu/namespaces.c b/criu/namespaces.c
index c1bee49..02959e7 100644
--- a/criu/namespaces.c
+++ b/criu/namespaces.c
@@ -241,16 +241,12 @@ int switch_ns(int pid, struct ns_desc *nd, int *rst)
 
 int switch_ns_by_fd(int nsfd, struct ns_desc *nd, int *rst)
 {
-	char buf[32];
 	int ret = -1;
 
 	if (rst) {
-		snprintf(buf, sizeof(buf), "/proc/self/ns/%s", nd->str);
-		*rst = open(buf, O_RDONLY);
-		if (*rst < 0) {
-			pr_perror("Can't open ns file");
+		*rst = open_proc(PROC_SELF, "ns/%s", nd->str);
+		if (*rst < 0)
 			goto err_ns;
-		}
 	}
 
 	ret = setns(nsfd, nd->cflag);
@@ -2194,11 +2190,9 @@ static int create_user_ns_hierarhy_fn(void *in_arg)
 		/* Set self pid to allow parent restore user_ns maps */
 		p_arg->pid = get_self_real_pid();
 		futex_set_and_wake(p_futex, NS__CREATED);
-		fd = open("/proc/self/ns/user", O_RDONLY);
-		if (fd < 0) {
-			pr_perror("Can't get self user ns");
+		fd = open_proc(PROC_SELF, "ns/user");
+		if (fd < 0)
 			goto out;
-		}
 		me->user.nsfd_id = fdstore_add(fd);
 		close(fd);
 		if (me->user.nsfd_id < 0) {
diff --git a/criu/net.c b/criu/net.c
index e06d32b..b423000 100644
--- a/criu/net.c
+++ b/criu/net.c
@@ -2210,11 +2210,9 @@ int netns_keep_nsfd(void)
 	 * that before we leave the existing namespaces.
 	 */
 
-	ns_fd = open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC);
-	if (ns_fd < 0) {
-		pr_perror("Can't cache net fd");
+	ns_fd = __open_proc(PROC_SELF, 0, O_RDONLY | O_CLOEXEC, "ns/net");
+	if (ns_fd < 0)
 		return -1;
-	}
 
 	ret = install_service_fd(NS_FD_OFF, ns_fd);
 	if (ret < 0)
-- 
2.9.3



More information about the CRIU mailing list