summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Hendricks <dhendrix@chromium.org>2016-06-01 15:51:23 -0700
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2016-06-07 22:51:05 +0000
commit5b125be7660c78b3235ccf50ffe9b8b080ec97de (patch)
treea249e42450859927c84723e1bff62b8a37188728
parent5219d2f86b151ca942673e88ad58d2bbacae1c46 (diff)
downloadchrome-ec-stabilize-8350.68.B.tar.gz
file_lock: Add fallback directorystabilize-8350.68.Brelease-R52-8350.B
This adds a fallback directory in case SYSTEM_LOCKFILE_DIR is unavailable. Since this is a band-aid meant to help older systems auto-update, the fallback path is hardcoded to "/tmp" as to avoid polluting the overall lockfile API. BUG=chromium:616620 BRANCH=none TEST=Tested on veyron_jaq by removing /run/lock and seeing mosys, flashrom, and ectool run successfully with firmware_utility_lock in /tmp. Change-Id: Idbe63a574474ec35a5c3b6fe2b0fb3b672941492 Signed-off-by: David Hendricks <dhendrix@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/348850 Reviewed-by: Hung-Te Lin <hungte@chromium.org> (cherry picked from commit 4b680119cc1de2dda7c0b625c4fea1d1e964189a) Reviewed-on: https://chromium-review.googlesource.com/350202
-rw-r--r--util/lock/file_lock.c43
1 files changed, 27 insertions, 16 deletions
diff --git a/util/lock/file_lock.c b/util/lock/file_lock.c
index 94f5991172..36b420d287 100644
--- a/util/lock/file_lock.c
+++ b/util/lock/file_lock.c
@@ -72,9 +72,25 @@ static int lock_is_held(struct ipc_lock *lock)
return lock->is_held;
}
-static int file_lock_open_or_create(struct ipc_lock *lock)
+static int test_dir(const char *path)
{
struct stat s;
+
+ if (lstat(path, &s) < 0) {
+ fprintf(stderr, "Cannot stat %s.\n", path);
+ return -1;
+ }
+
+ if (!S_ISDIR(s.st_mode)) {
+ fprintf(stderr, "%s is not a directory.\n", path);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int file_lock_open_or_create(struct ipc_lock *lock)
+{
char path[PATH_MAX];
if (in_android()) {
@@ -89,25 +105,20 @@ static int file_lock_open_or_create(struct ipc_lock *lock)
return -1;
}
} else {
- if (snprintf(path, sizeof(path), "%s", SYSTEM_LOCKFILE_DIR) < 0)
- return -1;
-
- if (lstat(path, &s) < 0) {
- fprintf(stderr, "Cannot stat %s", path);
- return -1;
+ const char *dir = SYSTEM_LOCKFILE_DIR;
+ const char fallback[] = "/tmp";
+
+ if (test_dir(dir)) {
+ dir = fallback;
+ fprintf(stderr, "Trying fallback directory: %s\n", dir);
+ if (test_dir(dir))
+ return -1;
}
- if (!S_ISDIR(s.st_mode)) {
- fprintf(stderr, "%s is not a directory.\n", path);
+ if (snprintf(path, sizeof(path),
+ "%s/%s", dir, lock->filename) < 0)
return -1;
- }
- if (strlen(path) + strlen(lock->filename) + 2 > PATH_MAX) {
- fprintf(stderr, "Lockfile path too long.\n");
- return -1;
- }
- strcat(path, "/");
- strcat(path, lock->filename);
}
lock->fd = open(path, O_RDWR | O_CREAT, 0600);