From 1d246f680f8d0fcec53961391fe9a36e8728a219 Mon Sep 17 00:00:00 2001 From: yingying Date: Tue, 25 Nov 2014 13:21:49 +0800 Subject: idmap: Do not unlink the file if it is locked by another process. The idmap file will be locked if it is used by one process. Then if another process need to access this file, it will get the file locked state. So in this case, should not unlink the file. Otherwise this file will be deleted by this process, and the another process which locked it will fail to write the file. CRs-fixed: 763809 Change-Id: I2bca8b04dd82b5a56d30b103325ba7e22f0072d9 --- cmds/idmap/create.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'cmds') diff --git a/cmds/idmap/create.cpp b/cmds/idmap/create.cpp index 41395f1..929f047 100644 --- a/cmds/idmap/create.cpp +++ b/cmds/idmap/create.cpp @@ -33,6 +33,7 @@ namespace { int open_idmap(const char *path) { int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644)); + bool needUnlink = true; if (fd == -1) { ALOGD("error: open %s: %s\n", path, strerror(errno)); goto fail; @@ -43,6 +44,8 @@ namespace { } if (TEMP_FAILURE_RETRY(flock(fd, LOCK_EX | LOCK_NB)) != 0) { ALOGD("error: flock %s: %s\n", path, strerror(errno)); + // If the file is locked by another process, then we needn't unlink the file. + needUnlink = false; goto fail; } @@ -50,7 +53,7 @@ namespace { fail: if (fd != -1) { close(fd); - unlink(path); + if (needUnlink) unlink(path); } return -1; } -- cgit v1.1