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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/common/multi_process_lock.h"
6
7 #include <sys/socket.h>
8 #include <sys/un.h>
9 #include <unistd.h>
10
11 #include "base/eintr_wrapper.h"
12 #include "base/logging.h"
13
14 class MultiProcessLockLinux : public MultiProcessLock {
15 public:
16 explicit MultiProcessLockLinux(const std::string& name)
17 : MultiProcessLock(name), fd_(-1) { }
18
19 virtual ~MultiProcessLockLinux() { Unlock(); }
20
21 virtual bool TryLock() {
22 struct sockaddr_un address;
23 // +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
24 // this an abstract name port.
25 if (name_.length() + 2 > sizeof(address.sun_path)) {
26 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.
27 return false;
28 }
29 memset(&address, 0, sizeof(address));
30 memcpy(&address.sun_path[1], name_.data(), name_.length());
31 address.sun_family = AF_LOCAL;
32 int socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
33 if (socket_fd < 0) {
34 PLOG(ERROR) << "Couldn't create socket";
35 return false;
36 }
37 if (bind(socket_fd,
38 reinterpret_cast<sockaddr *>(&address),
39 sizeof(address)) == 0) {
40 fd_ = socket_fd;
41 return true;
42 } else {
43 NOTREACHED() << "Couldn't bind socket to " << address.sun_path;
44 HANDLE_EINTR(close(socket_fd));
45 return false;
46 }
47 }
48
49 virtual void Unlock() {
50 if (fd_ != -1) {
51 HANDLE_EINTR(close(fd_));
52 fd_ = -1;
53 }
54 }
55
56 private:
57 int fd_;
58 DISALLOW_COPY_AND_ASSIGN(MultiProcessLockLinux);
59 };
60
61 MultiProcessLock* MultiProcessLock::Create(const std::string &name) {
62 return new MultiProcessLockLinux(name);
63 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698