Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / base / posix / unix_domain_socket_linux.h
blob94da4b4f9113e6b8305520257cc626ee8dd30619
1 // Copyright (c) 2011 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.
5 #ifndef BASE_POSIX_UNIX_DOMAIN_SOCKET_LINUX_H_
6 #define BASE_POSIX_UNIX_DOMAIN_SOCKET_LINUX_H_
8 #include <stdint.h>
9 #include <sys/types.h>
10 #include <vector>
12 #include "base/base_export.h"
13 #include "base/files/scoped_file.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/process/process_handle.h"
17 namespace base {
19 class Pickle;
21 class BASE_EXPORT UnixDomainSocket {
22 public:
23 // Maximum number of file descriptors that can be read by RecvMsg().
24 static const size_t kMaxFileDescriptors;
26 #if !defined(OS_NACL_NONSFI)
27 // Use to enable receiving process IDs in RecvMsgWithPid. Should be called on
28 // the receiving socket (i.e., the socket passed to RecvMsgWithPid). Returns
29 // true if successful.
30 static bool EnableReceiveProcessId(int fd);
31 #endif // !defined(OS_NACL_NONSFI)
33 // Use sendmsg to write the given msg and include a vector of file
34 // descriptors. Returns true if successful.
35 static bool SendMsg(int fd,
36 const void* msg,
37 size_t length,
38 const std::vector<int>& fds);
40 // Use recvmsg to read a message and an array of file descriptors. Returns
41 // -1 on failure. Note: will read, at most, |kMaxFileDescriptors| descriptors.
42 static ssize_t RecvMsg(int fd,
43 void* msg,
44 size_t length,
45 ScopedVector<ScopedFD>* fds);
47 // Same as RecvMsg above, but also returns the sender's process ID (as seen
48 // from the caller's namespace). However, before using this function to
49 // receive process IDs, EnableReceiveProcessId() should be called on the
50 // receiving socket.
51 static ssize_t RecvMsgWithPid(int fd,
52 void* msg,
53 size_t length,
54 ScopedVector<ScopedFD>* fds,
55 ProcessId* pid);
57 #if !defined(OS_NACL_NONSFI)
58 // Perform a sendmsg/recvmsg pair.
59 // 1. This process creates a UNIX SEQPACKET socketpair. Using
60 // connection-oriented sockets (SEQPACKET or STREAM) is critical here,
61 // because if one of the ends closes the other one must be notified.
62 // 2. This process writes a request to |fd| with an SCM_RIGHTS control
63 // message containing on end of the fresh socket pair.
64 // 3. This process blocks reading from the other end of the fresh
65 // socketpair.
66 // 4. The target process receives the request, processes it and writes the
67 // reply to the end of the socketpair contained in the request.
68 // 5. This process wakes up and continues.
70 // fd: descriptor to send the request on
71 // reply: buffer for the reply
72 // reply_len: size of |reply|
73 // result_fd: (may be NULL) the file descriptor returned in the reply
74 // (if any)
75 // request: the bytes to send in the request
76 static ssize_t SendRecvMsg(int fd,
77 uint8_t* reply,
78 unsigned reply_len,
79 int* result_fd,
80 const Pickle& request);
82 // Similar to SendRecvMsg(), but |recvmsg_flags| allows to control the flags
83 // of the recvmsg(2) call.
84 static ssize_t SendRecvMsgWithFlags(int fd,
85 uint8_t* reply,
86 unsigned reply_len,
87 int recvmsg_flags,
88 int* result_fd,
89 const Pickle& request);
90 #endif // !defined(OS_NACL_NONSFI)
91 private:
92 // Similar to RecvMsg, but allows to specify |flags| for recvmsg(2).
93 static ssize_t RecvMsgWithFlags(int fd,
94 void* msg,
95 size_t length,
96 int flags,
97 ScopedVector<ScopedFD>* fds,
98 ProcessId* pid);
101 } // namespace base
103 #endif // BASE_POSIX_UNIX_DOMAIN_SOCKET_LINUX_H_