blob: 59b20762119b15ae36c9dbe441e189f755598eb6 [file] [log] [blame]
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A simple program that checks if a file (given on the command line) is locked.
// It is a helper for It's necessary because a
// program can't really check if it is holding a lock on a file itself
// (
// It exits with 0 if the file is locked, 1 if the file is not locked, and
// a negative exit value if the file can't be found or there are other errors.
#include <stdlib.h>
#include <base/files/file.h>
#include <base/logging.h>
#include <brillo/syslog_logging.h>
int main(int argc, char* argv[]) {
brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderr);
if (argc != 2) {
<< "Usage: lock_file_tester file_name.\n"
<< "Tests to see if file_name is locked by another process. Exit\n"
<< "status 0 if locked, 1 if not locked.";
base::FilePath lock_file_path(argv[1]);
base::File lock_file(lock_file_path, base::File::FLAG_OPEN |
base::File::FLAG_READ |
if (!lock_file.IsValid()) {
LOG(ERROR) << "Error opening " << lock_file_path.value() << ": "
<< base::File::ErrorToString(lock_file.error_details());
if (lock_file.Lock(base::File::LockMode::kExclusive) == base::File::FILE_OK) {
// We could lock the file, therefore no one else had locked it.
// There is no way in the base::File API to distinguish "file is locked by
// another process" vs "A different error occurred", so we treat all failures
// as the former.