| Index: chrome/common/multi_process_lock_linux.cc
|
| diff --git a/chrome/common/multi_process_lock_linux.cc b/chrome/common/multi_process_lock_linux.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..22d0546e8d51dbef8a1e588306a51298ced95bcf
|
| --- /dev/null
|
| +++ b/chrome/common/multi_process_lock_linux.cc
|
| @@ -0,0 +1,80 @@
|
| +// Copyright (c) 2010 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/common/multi_process_lock.h"
|
| +
|
| +#include <sys/socket.h>
|
| +#include <sys/un.h>
|
| +#include <unistd.h>
|
| +
|
| +#include "base/eintr_wrapper.h"
|
| +#include "base/logging.h"
|
| +
|
| +class MultiProcessLockLinux : public MultiProcessLock {
|
| + public:
|
| + explicit MultiProcessLockLinux(const std::string& name)
|
| + : name_(name), fd_(-1), count_(0) { }
|
| +
|
| + virtual ~MultiProcessLockLinux() {
|
| + if (count_ > 0) {
|
| + Unlock();
|
| + }
|
| + }
|
| +
|
| + virtual bool TryLock() {
|
| + if (count_ > 0) {
|
| + count_ += 1;
|
| + return true;
|
| + }
|
| +
|
| + struct sockaddr_un address;
|
| + // +2 because: 1 for terminator, and 1 for \0 at the front that makes
|
| + // this an abstract name port.
|
| + if (name_.length() + 2 > sizeof(address.sun_path)) {
|
| + DLOG(ERROR) << "Socket name too long " << name_;
|
| + return false;
|
| + }
|
| + memset(&address, 0, sizeof(address));
|
| + memcpy(&address.sun_path[1], name_.data(), name_.length());
|
| + address.sun_family = AF_LOCAL;
|
| + int socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
|
| + if (socket_fd < 0) {
|
| + PLOG(ERROR) << "Couldn't create socket";
|
| + return false;
|
| + }
|
| + if (bind(socket_fd,
|
| + reinterpret_cast<sockaddr *>(&address),
|
| + sizeof(address)) == 0) {
|
| + fd_ = socket_fd;
|
| + count_ = 1;
|
| + return true;
|
| + } else {
|
| + NOTREACHED() << "Couldn't bind socket to " << address.sun_path;
|
| + HANDLE_EINTR(close(socket_fd));
|
| + return false;
|
| + }
|
| + }
|
| +
|
| + virtual void Unlock() {
|
| + if (count_ == 0) {
|
| + DLOG(ERROR) << "Over unlocked MultiProcessLock " << name_;
|
| + return;
|
| + }
|
| + count_ -= 1;
|
| + if (count_ == 0) {
|
| + HANDLE_EINTR(close(fd_));
|
| + fd_ = -1;
|
| + }
|
| + }
|
| +
|
| + private:
|
| + std::string name_;
|
| + int fd_;
|
| + int count_;
|
| + DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux);
|
| +};
|
| +
|
| +MultiProcessLock* MultiProcessLock::Create(const std::string &name) {
|
| + return new MultiProcessLockLinux(name);
|
| +}
|
|
|