]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/Coordinator.cc
d7f341949680a85164f5f784faf0b9cb3bba6b74
4 * DEBUG: section 54 Interprocess Communication
10 #include "base/TextException.h"
11 #include "CacheManager.h"
13 #include "ipc/Coordinator.h"
14 #include "ipc/FdNotes.h"
15 #include "ipc/SharedListen.h"
16 #include "mgr/Inquirer.h"
17 #include "mgr/Request.h"
18 #include "mgr/Response.h"
19 #include "mgr/StoreToCommWriter.h"
22 CBDATA_NAMESPACED_CLASS_INIT(Ipc
, Coordinator
);
23 Ipc::Coordinator
* Ipc::Coordinator::TheInstance
= NULL
;
26 Ipc::Coordinator::Coordinator():
31 void Ipc::Coordinator::start()
36 Ipc::StrandCoord
* Ipc::Coordinator::findStrand(int kidId
)
38 typedef StrandCoords::iterator SI
;
39 for (SI iter
= strands_
.begin(); iter
!= strands_
.end(); ++iter
) {
40 if (iter
->kidId
== kidId
)
46 void Ipc::Coordinator::registerStrand(const StrandCoord
& strand
)
48 if (StrandCoord
* found
= findStrand(strand
.kidId
))
51 strands_
.push_back(strand
);
54 void Ipc::Coordinator::receive(const TypedMsgHdr
& message
)
56 switch (message
.type()) {
58 debugs(54, 6, HERE
<< "Registration request");
59 handleRegistrationRequest(StrandCoord(message
));
62 case mtSharedListenRequest
:
63 debugs(54, 6, HERE
<< "Shared listen request");
64 handleSharedListenRequest(SharedListenRequest(message
));
67 case mtCacheMgrRequest
:
68 debugs(54, 6, HERE
<< "Cache manager request");
69 handleCacheMgrRequest(Mgr::Request(message
));
72 case mtCacheMgrResponse
:
73 debugs(54, 6, HERE
<< "Cache manager response");
74 handleCacheMgrResponse(Mgr::Response(message
));
78 debugs(54, 1, HERE
<< "Unhandled message type: " << message
.type());
83 void Ipc::Coordinator::handleRegistrationRequest(const StrandCoord
& strand
)
85 registerStrand(strand
);
87 // send back an acknowledgement; TODO: remove as not needed?
90 SendMessage(MakeAddr(strandAddrPfx
, strand
.kidId
), message
);
94 Ipc::Coordinator::handleSharedListenRequest(const SharedListenRequest
& request
)
96 debugs(54, 4, HERE
<< "kid" << request
.requestorId
<<
97 " needs shared listen FD for " << request
.params
.addr
);
98 Listeners::const_iterator i
= listeners
.find(request
.params
);
100 const int sock
= (i
!= listeners
.end()) ?
101 i
->second
: openListenSocket(request
, errNo
);
103 debugs(54, 3, HERE
<< "sending shared listen FD " << sock
<< " for " <<
104 request
.params
.addr
<< " to kid" << request
.requestorId
<<
105 " mapId=" << request
.mapId
);
107 SharedListenResponse
response(sock
, errNo
, request
.mapId
);
109 response
.pack(message
);
110 SendMessage(MakeAddr(strandAddrPfx
, request
.requestorId
), message
);
114 Ipc::Coordinator::handleCacheMgrRequest(const Mgr::Request
& request
)
118 // Let the strand know that we are now responsible for handling the request
119 Mgr::Response
response(request
.requestId
);
121 response
.pack(message
);
122 SendMessage(MakeAddr(strandAddrPfx
, request
.requestorId
), message
);
124 Mgr::Action::Pointer action
=
125 CacheManager::GetInstance()->createRequestedAction(request
.params
);
126 AsyncJob::Start(new Mgr::Inquirer(action
,
127 Mgr::ImportHttpFdIntoComm(request
.fd
), request
, strands_
));
131 Ipc::Coordinator::handleCacheMgrResponse(const Mgr::Response
& response
)
133 Mgr::Inquirer::HandleRemoteAck(response
);
137 Ipc::Coordinator::openListenSocket(const SharedListenRequest
& request
,
140 const OpenListenerParams
&p
= request
.params
;
142 debugs(54, 6, HERE
<< "opening listen FD at " << p
.addr
<< " for kid" <<
143 request
.requestorId
);
145 Ip::Address addr
= p
.addr
; // comm_open_listener may modify it
148 const int sock
= comm_open_listener(p
.sock_type
, p
.proto
, addr
, p
.flags
,
150 errNo
= (sock
>= 0) ? 0 : errno
;
153 // cache positive results
155 listeners
[request
.params
] = sock
;
160 void Ipc::Coordinator::broadcastSignal(int sig
) const
162 typedef StrandCoords::const_iterator SCI
;
163 for (SCI iter
= strands_
.begin(); iter
!= strands_
.end(); ++iter
) {
164 debugs(54, 5, HERE
<< "signal " << sig
<< " to kid" << iter
->kidId
<<
165 ", PID=" << iter
->pid
);
166 kill(iter
->pid
, sig
);
170 Ipc::Coordinator
* Ipc::Coordinator::Instance()
173 TheInstance
= new Coordinator
;
174 // XXX: if the Coordinator job quits, this pointer will become invalid
175 // we could make Coordinator death fatal, except during exit, but since
176 // Strands do not re-register, even process death would be pointless.
180 const Ipc::StrandCoords
&
181 Ipc::Coordinator::strands() const