[sheepdog] [PATCH v2 4/4] sheep: let config use atomic_create_and_write()

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Wed Apr 17 09:58:51 CEST 2013


Because config of sheep cannot use the journaling mechanism, it should
use atomic_create_and_write() for safe update of its data.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
v2:
 - remove unnecessary access(2) in check_tmp_config()

 sheep/config.c |   32 ++++++++++++++++++++------------
 1 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/sheep/config.c b/sheep/config.c
index 71e8a7a..f5c9794 100644
--- a/sheep/config.c
+++ b/sheep/config.c
@@ -35,29 +35,37 @@ char *config_path;
 
 static int write_config(void)
 {
-	int fd, ret;
+	int ret;
 
-	fd = open(config_path, O_RDWR | O_CREAT | O_DSYNC, sd_def_fmode);
-	if (fd < 0) {
-		sd_eprintf("failed to open config file, %m");
+	ret = atomic_create_and_write(config_path, (char *)&config,
+				sizeof(config));
+	if (ret < 0) {
+		sd_eprintf("atomic_create_and_write() failed");
 		return SD_RES_EIO;
 	}
 
-	ret = xwrite(fd, &config, sizeof(config));
-	if (ret != sizeof(config)) {
-		sd_eprintf("failed to write config data, %m");
-		ret = SD_RES_EIO;
-	} else
-		ret = SD_RES_SUCCESS;
-	close(fd);
+	return SD_RES_SUCCESS;
+}
 
-	return ret;
+static void check_tmp_config(void)
+{
+	char tmp_config_path[PATH_MAX];
+
+	snprintf(tmp_config_path, PATH_MAX, "%s.tmp", config_path);
+
+	if (!access(tmp_config_path, F_OK))
+		return;
+
+	sd_iprintf("remove temporal config file");
+	unlink(tmp_config_path);
 }
 
 int init_config_file(void)
 {
 	int fd, ret;
 
+	check_tmp_config();
+
 	fd = open(config_path, O_RDONLY);
 	if (fd < 0) {
 		if (errno != ENOENT) {
-- 
1.7.2.5




More information about the sheepdog mailing list