summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorDavid Hendricks <dhendrix@chromium.org>2016-06-01 15:51:23 -0700
committerchrome-bot <chrome-bot@chromium.org>2016-06-02 17:02:51 -0700
commit4b680119cc1de2dda7c0b625c4fea1d1e964189a (patch)
treeddb4bfd2e3643ae12ac8419c9b178dfa9d241225 /util
parent54261224660e9deb980c8d39b74f144f01edf68a (diff)
downloadchrome-ec-4b680119cc1de2dda7c0b625c4fea1d1e964189a.tar.gz
file_lock: Add fallback directory
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>
Diffstat (limited to 'util')
-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);