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

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Wed Apr 17 11:04:44 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>
---
v3:
 - remove unnecessary access(2) in check_tmp_config(), take 2

v2:
 - remove unnecessary access(2) in check_tmp_config()

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

diff --git a/sheep/config.c b/sheep/config.c
index 71e8a7a..aa84b96 100644
--- a/sheep/config.c
+++ b/sheep/config.c
@@ -35,29 +35,38 @@ 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)
+{
+	int ret;
+	char tmp_config_path[PATH_MAX];
+
+	snprintf(tmp_config_path, PATH_MAX, "%s.tmp", config_path);
+
+	ret = unlink(tmp_config_path);
+	if (!ret || ret != ENOENT)
+		return;
+
+	sd_iprintf("removed temporal config file");
 }
 
 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