Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2964)

Unified Diff: chrome/common/multi_process_lock_linux.cc

Issue 4721001: Add multi_process_lock to chrome/common (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: removed long name test as it tripped a NOTREACHED call. Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..bfb20f4fdb8f967335a2006b32ce4624f23feda5
--- /dev/null
+++ b/chrome/common/multi_process_lock_linux.cc
@@ -0,0 +1,63 @@
+// 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)
+ : MultiProcessLock(name), fd_(-1) { }
+
+ virtual ~MultiProcessLockLinux() { Unlock(); }
+
+ virtual bool TryLock() {
+ struct sockaddr_un address;
+ // +2 because: 1 for terminator, and 1 for \0 at the front that makes
agl 2010/11/11 21:50:53 terminator? These aren't NUL terminated names.
dmac 2010/11/11 23:33:02 agl, as far as I know you either have to NUL termi
+ // this an abstract name port.
+ if (name_.length() + 2 > sizeof(address.sun_path)) {
+ NOTREACHED() << "Socket name too long";
Mark Mentovai 2010/11/11 22:08:27 name_, and thus its length, is fully in the caller
dmac 2010/11/11 23:33:02 Changed, and test added.
+ 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;
+ return true;
+ } else {
+ NOTREACHED() << "Couldn't bind socket to " << address.sun_path;
+ HANDLE_EINTR(close(socket_fd));
+ return false;
+ }
+ }
+
+ virtual void Unlock() {
+ if (fd_ != -1) {
+ HANDLE_EINTR(close(fd_));
+ fd_ = -1;
+ }
+ }
+
+ private:
+ int fd_;
+ DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux);
+};
+
+MultiProcessLock* MultiProcessLock::Create(const std::string &name) {
+ return new MultiProcessLockLinux(name);
+}

Powered by Google App Engine
This is Rietveld 408576698