diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/lock/file_lock.c | 43 |
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); |