Initialize Tizen 2.3
[external/chromium.git] / ipc / ipc_channel_posix.h
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.
4
5 #ifndef IPC_IPC_CHANNEL_POSIX_H_
6 #define IPC_IPC_CHANNEL_POSIX_H_
7 #pragma once
8
9 #include "ipc/ipc_channel.h"
10
11 #include <sys/socket.h>  // for CMSG macros
12
13 #include <queue>
14 #include <string>
15 #include <vector>
16
17 #include "base/message_loop.h"
18 #include "ipc/file_descriptor_set_posix.h"
19
20 #if !defined(OS_MACOSX)
21 // On Linux, the seccomp sandbox makes it very expensive to call
22 // recvmsg() and sendmsg(). The restriction on calling read() and write(), which
23 // are cheap, is that we can't pass file descriptors over them.
24 //
25 // As we cannot anticipate when the sender will provide us with file
26 // descriptors, we have to make the decision about whether we call read() or
27 // recvmsg() before we actually make the call. The easiest option is to
28 // create a dedicated socketpair() for exchanging file descriptors. Any file
29 // descriptors are split out of a message, with the non-file-descriptor payload
30 // going over the normal connection, and the file descriptors being sent
31 // separately over the other channel. When read()ing from a channel, we'll
32 // notice if the message was supposed to have come with file descriptors and
33 // use recvmsg on the other socketpair to retrieve them and combine them
34 // back with the rest of the message.
35 //
36 // Mac can also run in IPC_USES_READWRITE mode if necessary, but at this time
37 // doesn't take a performance hit from recvmsg and sendmsg, so it doesn't
38 // make sense to waste resources on having the separate dedicated socketpair.
39 // It is however useful for debugging between Linux and Mac to be able to turn
40 // this switch 'on' on the Mac as well.
41 //
42 // The HELLO message from the client to the server is always sent using
43 // sendmsg because it will contain the file descriptor that the server
44 // needs to send file descriptors in later messages.
45 #define IPC_USES_READWRITE 1
46 #endif
47
48 namespace IPC {
49
50 class Channel::ChannelImpl : public MessageLoopForIO::Watcher {
51  public:
52   // Mirror methods of Channel, see ipc_channel.h for description.
53   ChannelImpl(const IPC::ChannelHandle& channel_handle, Mode mode,
54               Listener* listener);
55   virtual ~ChannelImpl();
56   bool Connect();
57   void Close();
58   void set_listener(Listener* listener) { listener_ = listener; }
59   bool Send(Message* message);
60   int GetClientFileDescriptor() const;
61   bool AcceptsConnections() const;
62   bool HasAcceptedConnection() const;
63   bool GetClientEuid(uid_t* client_euid) const;
64   void ResetToAcceptingConnectionState();
65   static bool IsNamedServerInitialized(const std::string& channel_id);
66
67  private:
68   bool CreatePipe(const IPC::ChannelHandle& channel_handle);
69
70   bool ProcessIncomingMessages();
71   bool ProcessOutgoingMessages();
72
73   bool AcceptConnection();
74   void ClosePipeOnError();
75   void QueueHelloMessage();
76   bool IsHelloMessage(const Message* m) const;
77
78   // MessageLoopForIO::Watcher implementation.
79   virtual void OnFileCanReadWithoutBlocking(int fd);
80   virtual void OnFileCanWriteWithoutBlocking(int fd);
81
82   Mode mode_;
83
84   // After accepting one client connection on our server socket we want to
85   // stop listening.
86   MessageLoopForIO::FileDescriptorWatcher server_listen_connection_watcher_;
87   MessageLoopForIO::FileDescriptorWatcher read_watcher_;
88   MessageLoopForIO::FileDescriptorWatcher write_watcher_;
89
90   // Indicates whether we're currently blocked waiting for a write to complete.
91   bool is_blocked_on_write_;
92   bool waiting_connect_;
93
94   // If sending a message blocks then we use this variable
95   // to keep track of where we are.
96   size_t message_send_bytes_written_;
97
98   // File descriptor we're listening on for new connections if we listen
99   // for connections.
100   int server_listen_pipe_;
101
102   // The pipe used for communication.
103   int pipe_;
104
105   // For a server, the client end of our socketpair() -- the other end of our
106   // pipe_ that is passed to the client.
107   int client_pipe_;
108
109 #if defined(IPC_USES_READWRITE)
110   // Linux/BSD use a dedicated socketpair() for passing file descriptors.
111   int fd_pipe_;
112   int remote_fd_pipe_;
113 #endif
114
115   // The "name" of our pipe.  On Windows this is the global identifier for
116   // the pipe.  On POSIX it's used as a key in a local map of file descriptors.
117   std::string pipe_name_;
118
119   Listener* listener_;
120
121   // Messages to be sent are queued here.
122   std::queue<Message*> output_queue_;
123
124   // We read from the pipe into this buffer
125   char input_buf_[Channel::kReadBufferSize];
126
127   enum {
128     // We assume a worst case: kReadBufferSize bytes of messages, where each
129     // message has no payload and a full complement of descriptors.
130     MAX_READ_FDS = (Channel::kReadBufferSize / sizeof(IPC::Message::Header)) *
131                    FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE,
132   };
133
134   // This is a control message buffer large enough to hold kMaxReadFDs
135 #if defined(OS_MACOSX)
136   // TODO(agl): OSX appears to have non-constant CMSG macros!
137   char input_cmsg_buf_[1024];
138 #else
139   char input_cmsg_buf_[CMSG_SPACE(sizeof(int) * MAX_READ_FDS)];
140 #endif
141
142   // Large messages that span multiple pipe buffers, get built-up using
143   // this buffer.
144   std::string input_overflow_buf_;
145   std::vector<int> input_overflow_fds_;
146
147   // True if we are responsible for unlinking the unix domain socket file.
148   bool must_unlink_;
149
150   DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelImpl);
151 };
152
153 // The maximum length of the name of a pipe for MODE_NAMED_SERVER or
154 // MODE_NAMED_CLIENT if you want to pass in your own socket.
155 // The standard size on linux is 108, mac is 104. To maintain consistency
156 // across platforms we standardize on the smaller value.
157 static const size_t kMaxPipeNameLength = 104;
158
159 }  // namespace IPC
160
161 #endif  // IPC_IPC_CHANNEL_POSIX_H_