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_
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"
19 class BASE_EXPORT UnixDomainSocket
{
21 // Maximum number of file descriptors that can be read by RecvMsg().
22 static const size_t kMaxFileDescriptors
;
24 // Use to enable receiving process IDs in RecvMsgWithPid. Should be called on
25 // the receiving socket (i.e., the socket passed to RecvMsgWithPid). Returns
26 // true if successful.
27 static bool EnableReceiveProcessId(int fd
);
29 // Use sendmsg to write the given msg and include a vector of file
30 // descriptors. Returns true if successful.
31 static bool SendMsg(int fd
,
34 const std::vector
<int>& fds
);
36 // Use recvmsg to read a message and an array of file descriptors. Returns
37 // -1 on failure. Note: will read, at most, |kMaxFileDescriptors| descriptors.
38 static ssize_t
RecvMsg(int fd
,
41 ScopedVector
<base::ScopedFD
>* fds
);
43 // Same as RecvMsg above, but also returns the sender's process ID (as seen
44 // from the caller's namespace). However, before using this function to
45 // receive process IDs, EnableReceiveProcessId() should be called on the
47 static ssize_t
RecvMsgWithPid(int fd
,
50 ScopedVector
<base::ScopedFD
>* fds
,
51 base::ProcessId
* pid
);
53 // Perform a sendmsg/recvmsg pair.
54 // 1. This process creates a UNIX SEQPACKET socketpair. Using
55 // connection-oriented sockets (SEQPACKET or STREAM) is critical here,
56 // because if one of the ends closes the other one must be notified.
57 // 2. This process writes a request to |fd| with an SCM_RIGHTS control
58 // message containing on end of the fresh socket pair.
59 // 3. This process blocks reading from the other end of the fresh
61 // 4. The target process receives the request, processes it and writes the
62 // reply to the end of the socketpair contained in the request.
63 // 5. This process wakes up and continues.
65 // fd: descriptor to send the request on
66 // reply: buffer for the reply
67 // reply_len: size of |reply|
68 // result_fd: (may be NULL) the file descriptor returned in the reply
70 // request: the bytes to send in the request
71 static ssize_t
SendRecvMsg(int fd
,
75 const Pickle
& request
);
77 // Similar to SendRecvMsg(), but |recvmsg_flags| allows to control the flags
78 // of the recvmsg(2) call.
79 static ssize_t
SendRecvMsgWithFlags(int fd
,
84 const Pickle
& request
);
86 // Similar to RecvMsg, but allows to specify |flags| for recvmsg(2).
87 static ssize_t
RecvMsgWithFlags(int fd
,
91 ScopedVector
<base::ScopedFD
>* fds
,
92 base::ProcessId
* pid
);
95 #endif // BASE_POSIX_UNIX_DOMAIN_SOCKET_LINUX_H_