CoreIPCServer.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma once
  2. #include <LibCore/CEvent.h>
  3. #include <LibCore/CEventLoop.h>
  4. #include <LibCore/CIODevice.h>
  5. #include <LibCore/CLocalSocket.h>
  6. #include <LibCore/CNotifier.h>
  7. #include <LibCore/CObject.h>
  8. #include <errno.h>
  9. #include <stdio.h>
  10. #include <sys/socket.h>
  11. #include <sys/types.h>
  12. #include <sys/uio.h>
  13. #include <unistd.h>
  14. //#define CIPC_DEBUG
  15. namespace IPC {
  16. namespace Server {
  17. class Event : public CEvent {
  18. public:
  19. enum Type {
  20. Invalid = 2000,
  21. Disconnected,
  22. };
  23. Event() {}
  24. explicit Event(Type type)
  25. : CEvent(type)
  26. {
  27. }
  28. };
  29. class DisconnectedEvent : public Event {
  30. public:
  31. explicit DisconnectedEvent(int client_id)
  32. : Event(Disconnected)
  33. , m_client_id(client_id)
  34. {
  35. }
  36. int client_id() const { return m_client_id; }
  37. private:
  38. int m_client_id { 0 };
  39. };
  40. template<typename T, class... Args>
  41. T* new_connection_for_client(Args&&... args)
  42. {
  43. auto conn = new T(AK::forward<Args>(args)...) /* arghs */;
  44. conn->send_greeting();
  45. return conn;
  46. };
  47. template<typename ServerMessage, typename ClientMessage>
  48. class Connection : public CObject {
  49. C_OBJECT(Connection)
  50. public:
  51. Connection(CLocalSocket& socket, int client_id)
  52. : m_socket(socket)
  53. , m_client_id(client_id)
  54. {
  55. add_child(socket);
  56. m_socket.on_ready_to_read = [this] { drain_client(); };
  57. #if defined(CIPC_DEBUG)
  58. dbg() << "S: Created new Connection " << fd << client_id << " and said hello";
  59. #endif
  60. }
  61. ~Connection()
  62. {
  63. #if defined(CIPC_DEBUG)
  64. dbg() << "S: Destroyed Connection " << m_socket.fd() << client_id();
  65. #endif
  66. }
  67. void post_message(const ServerMessage& message, const ByteBuffer& extra_data = {})
  68. {
  69. #if defined(CIPC_DEBUG)
  70. dbg() << "S: -> C " << int(message.type) << " extra " << extra_data.size();
  71. #endif
  72. if (!extra_data.is_empty())
  73. const_cast<ServerMessage&>(message).extra_size = extra_data.size();
  74. struct iovec iov[2];
  75. int iov_count = 1;
  76. iov[0].iov_base = const_cast<ServerMessage*>(&message);
  77. iov[0].iov_len = sizeof(message);
  78. if (!extra_data.is_empty()) {
  79. iov[1].iov_base = const_cast<u8*>(extra_data.data());
  80. iov[1].iov_len = extra_data.size();
  81. ++iov_count;
  82. }
  83. int nwritten = writev(m_socket.fd(), iov, iov_count);
  84. if (nwritten < 0) {
  85. switch (errno) {
  86. case EPIPE:
  87. dbgprintf("Connection::post_message: Disconnected from peer.\n");
  88. delete_later();
  89. return;
  90. break;
  91. case EAGAIN:
  92. dbgprintf("Connection::post_message: Client buffer overflowed.\n");
  93. did_misbehave();
  94. return;
  95. break;
  96. default:
  97. perror("Connection::post_message writev");
  98. ASSERT_NOT_REACHED();
  99. }
  100. }
  101. ASSERT(nwritten == (int)(sizeof(message) + extra_data.size()));
  102. }
  103. void drain_client()
  104. {
  105. unsigned messages_received = 0;
  106. for (;;) {
  107. ClientMessage message;
  108. // FIXME: Don't go one message at a time, that's so much context switching, oof.
  109. ssize_t nread = recv(m_socket.fd(), &message, sizeof(ClientMessage), MSG_DONTWAIT);
  110. if (nread == 0 || (nread == -1 && errno == EAGAIN)) {
  111. if (!messages_received) {
  112. // TODO: is delete_later() sufficient?
  113. CEventLoop::current().post_event(*this, make<DisconnectedEvent>(client_id()));
  114. }
  115. break;
  116. }
  117. if (nread < 0) {
  118. perror("recv");
  119. ASSERT_NOT_REACHED();
  120. }
  121. ByteBuffer extra_data;
  122. if (message.extra_size) {
  123. if (message.extra_size >= 32768) {
  124. dbgprintf("message.extra_size is way too large\n");
  125. return did_misbehave();
  126. }
  127. extra_data = ByteBuffer::create_uninitialized(message.extra_size);
  128. // FIXME: We should allow this to time out. Maybe use a socket timeout?
  129. int extra_nread = read(m_socket.fd(), extra_data.data(), extra_data.size());
  130. if (extra_nread != (int)message.extra_size) {
  131. dbgprintf("extra_nread(%d) != extra_size(%d)\n", extra_nread, extra_data.size());
  132. if (extra_nread < 0)
  133. perror("read");
  134. return did_misbehave();
  135. }
  136. }
  137. #if defined(CIPC_DEBUG)
  138. dbg() << "S: <- C " << int(message.type) << " extra " << extra_data.size();
  139. #endif
  140. if (!handle_message(message, move(extra_data)))
  141. return;
  142. ++messages_received;
  143. }
  144. }
  145. void did_misbehave()
  146. {
  147. dbgprintf("Connection{%p} (id=%d, pid=%d) misbehaved, disconnecting.\n", this, client_id(), m_pid);
  148. m_socket.close();
  149. delete_later();
  150. }
  151. int client_id() const { return m_client_id; }
  152. pid_t client_pid() const { return m_pid; }
  153. void set_client_pid(pid_t pid) { m_pid = pid; }
  154. // ### having this public is sad
  155. virtual void send_greeting() = 0;
  156. protected:
  157. void event(CEvent& event)
  158. {
  159. if (event.type() == Event::Disconnected) {
  160. int client_id = static_cast<const DisconnectedEvent&>(event).client_id();
  161. dbgprintf("Connection: Client disconnected: %d\n", client_id);
  162. delete this;
  163. return;
  164. }
  165. CObject::event(event);
  166. }
  167. virtual bool handle_message(const ClientMessage&, const ByteBuffer&& = {}) = 0;
  168. private:
  169. CLocalSocket& m_socket;
  170. int m_client_id;
  171. int m_pid;
  172. };
  173. } // Server
  174. } // IPC