]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/Coordinator.cc
4 * DEBUG: section 54 Interprocess Communication
10 #include "base/Subscription.h"
12 #include "comm/Connection.h"
13 #include "ipc/Coordinator.h"
14 #include "ipc/FdNotes.h"
15 #include "ipc/SharedListen.h"
18 CBDATA_NAMESPACED_CLASS_INIT(Ipc
, Coordinator
);
19 Ipc::Coordinator
* Ipc::Coordinator::TheInstance
= NULL
;
22 Ipc::Coordinator::Coordinator():
27 void Ipc::Coordinator::start()
32 Ipc::StrandCoord
* Ipc::Coordinator::findStrand(int kidId
)
34 typedef Strands::iterator SI
;
35 for (SI iter
= strands
.begin(); iter
!= strands
.end(); ++iter
) {
36 if (iter
->kidId
== kidId
)
42 void Ipc::Coordinator::registerStrand(const StrandCoord
& strand
)
44 if (StrandCoord
* found
= findStrand(strand
.kidId
))
47 strands
.push_back(strand
);
50 void Ipc::Coordinator::receive(const TypedMsgHdr
& message
)
52 switch (message
.type()) {
54 debugs(54, 6, HERE
<< "Registration request");
55 handleRegistrationRequest(StrandCoord(message
));
58 case mtSharedListenRequest
:
59 debugs(54, 6, HERE
<< "Shared listen request");
60 handleSharedListenRequest(SharedListenRequest(message
));
64 debugs(54, 1, HERE
<< "Unhandled message type: " << message
.type());
69 void Ipc::Coordinator::handleRegistrationRequest(const StrandCoord
& strand
)
71 registerStrand(strand
);
73 // send back an acknowledgement; TODO: remove as not needed?
76 SendMessage(MakeAddr(strandAddrPfx
, strand
.kidId
), message
);
80 Ipc::Coordinator::handleSharedListenRequest(const SharedListenRequest
& request
)
82 debugs(54, 4, HERE
<< "kid" << request
.requestorId
<<
83 " needs shared listen FD for " << request
.params
.addr
);
84 Listeners::const_iterator i
= listeners
.find(request
.params
);
86 const Comm::ConnectionPointer c
= (i
!= listeners
.end()) ?
87 i
->second
: openListenSocket(request
, errNo
);
89 debugs(54, 3, HERE
<< "sending shared listen " << c
<< " for " <<
90 request
.params
.addr
<< " to kid" << request
.requestorId
<<
91 " mapId=" << request
.mapId
);
93 SharedListenResponse
response(c
, errNo
, request
.mapId
);
95 response
.pack(message
);
96 SendMessage(MakeAddr(strandAddrPfx
, request
.requestorId
), message
);
99 Comm::ConnectionPointer
100 Ipc::Coordinator::openListenSocket(const SharedListenRequest
& request
,
103 const OpenListenerParams
&p
= request
.params
;
105 debugs(54, 6, HERE
<< "opening listen FD at " << p
.addr
<< " for kid" <<
106 request
.requestorId
);
108 Comm::ConnectionPointer conn
= new Comm::Connection
;
109 conn
->local
= p
.addr
; // comm_open_listener may modify it
110 conn
->flags
= p
.flags
;
113 comm_open_listener(p
.sock_type
, p
.proto
, conn
, FdNote(p
.fdNote
));
114 errNo
= Comm::IsConnOpen(conn
) ? 0 : errno
;
117 debugs(54, 6, HERE
<< "tried listening on " << conn
<< " for kid" <<
118 request
.requestorId
);
120 // cache positive results
121 if (Comm::IsConnOpen(conn
))
122 listeners
[request
.params
] = conn
;
127 void Ipc::Coordinator::broadcastSignal(int sig
) const
129 typedef Strands::const_iterator SCI
;
130 for (SCI iter
= strands
.begin(); iter
!= strands
.end(); ++iter
) {
131 debugs(54, 5, HERE
<< "signal " << sig
<< " to kid" << iter
->kidId
<<
132 ", PID=" << iter
->pid
);
133 kill(iter
->pid
, sig
);
137 Ipc::Coordinator
* Ipc::Coordinator::Instance()
140 TheInstance
= new Coordinator
;
141 // XXX: if the Coordinator job quits, this pointer will become invalid
142 // we could make Coordinator death fatal, except during exit, but since
143 // Strands do not re-register, even process death would be pointless.