[CRIU] [PATCH 3/3] restore: Add support to flock&posix file locks' restore

Gu Zheng cengku.gu at huawei.com
Wed Nov 7 22:10:30 EST 2012


Add a preliminary support to file locks restore to match the file
locks dump option,it only supports the flock and posix file locks,
and should be only used for container indeed.

Signed-off-by: Zheng Gu <cengku.gu at huawei.com>
---
 cr-restore.c        |    6 ++++
 file-lock.c         |   77 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/file-lock.h |    2 +
 3 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/cr-restore.c b/cr-restore.c
index 2ebf3f8..e989cf0 100644
--- a/cr-restore.c
+++ b/cr-restore.c
@@ -53,6 +53,7 @@
 #include "pstree.h"
 #include "net.h"
 #include "tty.h"
+#include "file-lock.h"
 
 #include "protobuf.h"
 #include "protobuf/sa.pb-c.h"
@@ -345,6 +346,11 @@ static int restore_one_alive_task(int pid, CoreEntry *core)
 	if (prepare_fs(pid))
 		return -1;
 
+	if (opts.handle_file_locks) {
+		pr_info("restore file locks\n");
+		if (restore_file_locks(pid))
+			return -1;
+	}
 	if (prepare_sigactions(pid))
 		return -1;
 
diff --git a/file-lock.c b/file-lock.c
index 32d2765..78a5151 100644
--- a/file-lock.c
+++ b/file-lock.c
@@ -1,5 +1,8 @@
 #include <stdlib.h>
 #include <unistd.h>
+#include <sys/file.h>
+#include <fcntl.h>
+#include <string.h>
 
 #include "crtools.h"
 #include "file-lock.h"
@@ -13,3 +16,77 @@ int dump_one_file_lock(FileLockEntry *fe, const struct cr_fdset *fdset)
 	return pb_write_one(fdset_fd(fdset, CR_FD_FILE_LOCKS),
 			fe, PB_FILE_LOCK);
 }
+
+static int restore_file_lock(FileLockEntry *fe)
+{
+	int ret = -1;
+	unsigned int cmd;
+
+	if (fe->flag & FL_FLOCK) {
+		if (fe->type & LOCK_MAND) {
+			cmd = fe->type;
+		} else if (fe->type & F_RDLCK) {
+			cmd |= LOCK_SH;
+		} else if (fe->type & F_WRLCK) {
+			cmd |= LOCK_EX;
+		} else if (fe->type & F_UNLCK) {
+			cmd |= LOCK_UN;
+		} else {
+			pr_err("Unknow flock type!\n");
+			goto err;
+		}
+
+		ret = flock(fe->fd, cmd);
+		if (ret < 0) {
+			pr_err("Can not set flock!\n");
+			goto err;
+		}
+	} else if (fe->flag & FL_POSIX) {
+		struct flock flk;
+		memset(&flk, 0, sizeof(flk));
+
+		flk.l_whence = SEEK_SET;
+		flk.l_start = fe->start;
+		flk.l_len = fe->len;
+		flk.l_pid = fe->pid;
+		flk.l_type = fe->type;
+
+		ret = fcntl(fe->fd, F_SETLKW, &flk);
+		if (ret < 0) {
+			pr_err("Can not set posix lock!\n");
+			goto err;
+		}
+	} else {
+		pr_err("Unknow file lock style!\n");
+	}
+err:
+	return ret;
+}
+
+int restore_file_locks(int pid)
+{
+	int fd, ret = -1;
+	FileLockEntry *fe;
+
+	fd = open_image_ro(CR_FD_FILE_LOCKS, pid);
+	if (fd < 0) {
+		if (errno == ENOENT)
+			return 0;
+		else
+			return -1;
+	}
+
+	while (1) {
+		ret = pb_read_one_eof(fd, &fe, PB_FILE_LOCK);
+		if (ret <= 0)
+			break;
+
+		ret = restore_file_lock(fe);
+		file_lock_entry__free_unpacked(fe, NULL);
+		if (ret < 0)
+			break;
+	}
+
+	close(fd);
+	return ret;
+}
diff --git a/include/file-lock.h b/include/file-lock.h
index 491f637..706b6ea 100644
--- a/include/file-lock.h
+++ b/include/file-lock.h
@@ -28,3 +28,5 @@
 #define LOCK_RW		192	/* which allows concurrent read & write ops */
 
 extern int dump_one_file_lock(FileLockEntry *fe, const struct cr_fdset *fdset);
+
+extern int restore_file_locks(int pid);
-- 
1.7.1




More information about the CRIU mailing list