From e63fa9927f218ae26558a00f5de6b674a41a2ef0 Mon Sep 17 00:00:00 2001 From: David Hendricks Date: Wed, 1 Jun 2016 15:51:23 -0700 Subject: 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 Reviewed-on: https://chromium-review.googlesource.com/348850 Reviewed-by: Hung-Te Lin (cherry picked from commit 4b680119cc1de2dda7c0b625c4fea1d1e964189a) Reviewed-on: https://chromium-review.googlesource.com/354780 Reviewed-by: Bernie Thompson Commit-Queue: Bernie Thompson Tested-by: Bernie Thompson --- util/lock/file_lock.c | 43 +++++++++++++++++++++++++++---------------- 1 file 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); -- cgit v1.2.1