1 // Copyright 2013 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 #include "remoting/host/ipc_util.h"
8 #include <sys/socket.h>
12 #include "base/files/file.h"
13 #include "base/logging.h"
14 #include "base/posix/eintr_wrapper.h"
15 #include "base/single_thread_task_runner.h"
16 #include "ipc/ipc_channel.h"
17 #include "ipc/ipc_channel_proxy.h"
21 bool CreateConnectedIpcChannel(
22 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
23 IPC::Listener
* listener
,
24 base::File
* client_out
,
25 scoped_ptr
<IPC::ChannelProxy
>* server_out
) {
26 // Create a socket pair.
28 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipe_fds
) != 0) {
29 PLOG(ERROR
) << "socketpair()";
33 // Set both ends to be non-blocking.
34 if (fcntl(pipe_fds
[0], F_SETFL
, O_NONBLOCK
) == -1 ||
35 fcntl(pipe_fds
[1], F_SETFL
, O_NONBLOCK
) == -1) {
36 PLOG(ERROR
) << "fcntl(O_NONBLOCK)";
37 if (IGNORE_EINTR(close(pipe_fds
[0])) < 0)
38 PLOG(ERROR
) << "close()";
39 if (IGNORE_EINTR(close(pipe_fds
[1])) < 0)
40 PLOG(ERROR
) << "close()";
44 std::string socket_name
= "Chromoting socket";
46 // Wrap the pipe into an IPC channel.
47 base::FileDescriptor
fd(pipe_fds
[0], false);
48 IPC::ChannelHandle
handle(socket_name
, fd
);
49 *server_out
= IPC::ChannelProxy::Create(IPC::ChannelHandle(socket_name
, fd
),
50 IPC::Channel::MODE_SERVER
,
55 *client_out
= base::File(pipe_fds
[1]);
59 } // namespace remoting