SharedListen.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 54 Interprocess Communication */
10 
11 #include "squid.h"
12 #include "base/TextException.h"
13 #include "comm.h"
14 #include "comm/Connection.h"
15 #include "globals.h"
16 #include "ipc/Kids.h"
17 #include "ipc/Messages.h"
18 #include "ipc/Port.h"
19 #include "ipc/SharedListen.h"
20 #include "ipc/StartListening.h"
21 #include "ipc/TypedMsgHdr.h"
22 #include "tools.h"
23 
24 #include <list>
25 #include <map>
26 
29 {
30 public:
32  AsyncCall::Pointer callback; // who to notify
33 };
34 
36 typedef std::map<int, PendingOpenRequest> SharedListenRequestMap;
38 
40 typedef std::list<PendingOpenRequest> DelayedSharedListenRequests;
42 
43 static int
45 {
46  // find unused ID using linear seach; there should not be many entries
47  for (int id = 0; true; ++id) {
49  TheSharedListenRequestMap[id] = por;
50  return id;
51  }
52  }
53  assert(false); // not reached
54  return -1;
55 }
56 
58 {
59  memset(this, 0, sizeof(*this));
60 }
61 
62 bool
64 {
65  if (sock_type != p.sock_type)
66  return sock_type < p.sock_type;
67 
68  if (proto != p.proto)
69  return proto < p.proto;
70 
71  // ignore flags and fdNote differences because they do not affect binding
72 
73  return addr.compareWhole(p.addr) < 0;
74 }
75 
76 Ipc::SharedListenRequest::SharedListenRequest(): requestorId(-1), mapId(-1)
77 {
78  // caller will then set public data members
79 }
80 
82 {
84  hdrMsg.getPod(*this);
85 }
86 
88 {
90  hdrMsg.putPod(*this);
91 }
92 
93 Ipc::SharedListenResponse::SharedListenResponse(int aFd, int anErrNo, int aMapId):
94  fd(aFd), errNo(anErrNo), mapId(aMapId)
95 {
96 }
97 
99  fd(-1), errNo(0), mapId(-1)
100 {
102  hdrMsg.getPod(*this);
103  fd = hdrMsg.getFd();
104  // other conn details are passed in OpenListenerParams and filled out by SharedListenJoin()
105 }
106 
108 {
110  hdrMsg.putPod(*this);
111  hdrMsg.putFd(fd);
112 }
113 
114 static void
116 {
118  request.requestorId = KidIdentifier;
119  request.params = por.params;
120  request.mapId = AddToMap(por);
121 
122  debugs(54, 3, "getting listening FD for " << request.params.addr <<
123  " mapId=" << request.mapId);
124 
125  Ipc::TypedMsgHdr message;
126  request.pack(message);
128 }
129 
130 static void
132 {
133  if (TheDelayedRequests.empty())
134  return; // no pending requests to resume
135 
136  debugs(54, 3, "resuming with " << TheSharedListenRequestMap.size() <<
137  " active + " << TheDelayedRequests.size() << " delayed requests");
138 
140  TheDelayedRequests.pop_front();
141 }
142 
143 void
145 {
146  PendingOpenRequest por;
147  por.params = params;
148  por.callback = cb;
149 
150  const DelayedSharedListenRequests::size_type concurrencyLimit = 1;
151  if (TheSharedListenRequestMap.size() >= concurrencyLimit) {
152  debugs(54, 3, "waiting for " << TheSharedListenRequestMap.size() <<
153  " active + " << TheDelayedRequests.size() << " delayed requests");
154  TheDelayedRequests.push_back(por);
155  } else {
157  }
158 }
159 
161 {
162  // Dont debugs c fully since only FD is filled right now.
163  debugs(54, 3, "got listening FD " << response.fd << " errNo=" <<
164  response.errNo << " mapId=" << response.mapId << " with " <<
165  TheSharedListenRequestMap.size() << " active + " <<
166  TheDelayedRequests.size() << " delayed requests");
167 
170  Must(por.callback != NULL);
171  TheSharedListenRequestMap.erase(response.mapId);
172 
173  StartListeningCb *cbd = dynamic_cast<StartListeningCb*>(por.callback->getDialer());
174  assert(cbd && cbd->conn != NULL);
175  Must(cbd && cbd->conn != NULL);
176  cbd->conn->fd = response.fd;
177 
178  if (Comm::IsConnOpen(cbd->conn)) {
179  OpenListenerParams &p = por.params;
180  cbd->conn->local = p.addr;
181  cbd->conn->flags = p.flags;
182  // XXX: leave the comm AI stuff to comm_import_opened()?
183  struct addrinfo *AI = NULL;
184  p.addr.getAddrInfo(AI);
185  AI->ai_socktype = p.sock_type;
186  AI->ai_protocol = p.proto;
187  comm_import_opened(cbd->conn, FdNote(p.fdNote), AI);
189  }
190 
191  cbd->errNo = response.errNo;
192  cbd->handlerSubscription = por.params.handlerSubscription;
193  ScheduleCallHere(por.callback);
194 
196 }
197 
const char * FdNote(int fdNodeId)
converts FdNoteId into a string
Definition: FdNotes.cc:16
void pack(TypedMsgHdr &hdrMsg) const
prepare for sendmsg()
Definition: SharedListen.cc:87
static void kickDelayedRequest()
#define assert(EX)
Definition: assert.h:17
int mapId
to map future response to the requestor's callback
Definition: SharedListen.h:73
static void FreeAddr(struct addrinfo *&ai)
Definition: Address.cc:694
void comm_import_opened(const Comm::ConnectionPointer &conn, const char *note, struct addrinfo *AI)
update Comm state after getting a comm_open() FD from another process
Definition: comm.cc:499
SharedListenRequest()
from OpenSharedListen() which then sets public data
Definition: SharedListen.cc:76
void getPod(Pod &pod) const
load POD
Definition: TypedMsgHdr.h:50
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
std::map< int, PendingOpenRequest > SharedListenRequestMap
maps ID assigned at request time to the response callback
Definition: SharedListen.cc:36
int requestorId
kidId of the requestor
Definition: SharedListen.h:55
void putPod(const Pod &pod)
store POD
Definition: TypedMsgHdr.h:52
Subscription::Pointer handlerSubscription
The subscription we will pass on to the ConnAcceptor.
char * p
Definition: membanger.c:43
Ip::Address addr
will be memset and memcopied
Definition: SharedListen.h:37
void SharedListenJoined(const SharedListenResponse &response)
process Coordinator response to SharedListenRequest
OpenListenerParams params
actual comm_open_sharedListen() parameters
Definition: SharedListen.h:57
static int AddToMap(const PendingOpenRequest &por)
Definition: SharedListen.cc:44
a response to SharedListenRequest
Definition: SharedListen.h:63
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:24
common API for all StartListening() callbacks
std::list< PendingOpenRequest > DelayedSharedListenRequests
accumulates delayed requests until they are ready to be sent, in FIFO order
Definition: SharedListen.cc:40
static void SendSharedListenRequest(const PendingOpenRequest &por)
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
static DelayedSharedListenRequests TheDelayedRequests
Definition: SharedListen.cc:41
Comm::ConnectionPointer conn
opened listening socket
Ipc::OpenListenerParams params
actual comm_open_sharedListen() parameters
Definition: SharedListen.cc:31
void * addr
Definition: membanger.c:46
a request for a listen socket with given parameters
Definition: SharedListen.h:47
void SendMessage(const String &toAddress, const TypedMsgHdr &message)
Definition: UdsOp.cc:188
#define Must(cond)
Definition: TextException.h:89
Ip::Address local
Definition: Connection.h:135
int errNo
errno value from the comm_open_listener() call
int fdNote
index into fd_note() comment strings
Definition: SharedListen.h:34
void JoinSharedListen(const OpenListenerParams &, AsyncCall::Pointer &)
prepare and send SharedListenRequest to Coordinator
int KidIdentifier
void putFd(int aFd)
stores descriptor
Definition: TypedMsgHdr.cc:186
#define ScheduleCallHere(call)
Definition: AsyncCall.h:166
SharedListenResponse(int fd, int errNo, int mapId)
Definition: SharedListen.cc:93
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:31
int errNo
errno value from comm_open_sharedListen() call
Definition: SharedListen.h:72
void checkType(int aType) const
throws if stored type is not aType
Definition: TypedMsgHdr.cc:84
void pack(TypedMsgHdr &hdrMsg) const
prepare for sendmsg()
int getFd() const
returns stored descriptor
Definition: TypedMsgHdr.cc:207
void setType(int aType)
sets message type; use MessageType enum
Definition: TypedMsgHdr.cc:90
int fd
opened listening socket or -1
Definition: SharedListen.h:71
AsyncCall::Pointer callback
Definition: SharedListen.cc:32
"shared listen" is when concurrent processes are listening on the same fd
Definition: SharedListen.h:24
static String CoordinatorAddr()
get the IPC message address for coordinator process
Definition: Port.cc:64
#define NULL
Definition: types.h:166
bool operator<(const OpenListenerParams &p) const
useful for map<>
Definition: SharedListen.cc:63
holds information necessary to handle JoinListen response
Definition: SharedListen.cc:28
void getAddrInfo(struct addrinfo *&ai, int force=AF_UNSPEC) const
Definition: Address.cc:607
static SharedListenRequestMap TheSharedListenRequestMap
Definition: SharedListen.cc:37
int mapId
to map future response to the requestor's callback
Definition: SharedListen.h:59

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors