StartListening.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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/AsyncCallbacks.h"
13 #include "base/TextException.h"
14 #include "comm.h"
15 #include "comm/Connection.h"
16 #include "ipc/SharedListen.h"
17 #include "ipc/StartListening.h"
18 #include "tools.h"
19 
20 #include <cerrno>
21 
22 std::ostream &
23 Ipc::operator <<(std::ostream &os, const StartListeningAnswer &answer)
24 {
25  os << answer.conn;
26  if (answer.errNo)
27  os << ", err=" << answer.errNo;
28  return os;
29 }
30 
31 void
32 Ipc::StartListening(int sock_type, int proto, const Comm::ConnectionPointer &listenConn,
33  const FdNoteId fdNote, StartListeningCallback &callback)
34 {
35  auto &answer = callback.answer();
36  answer.conn = listenConn;
37 
38  const auto giveEachWorkerItsOwnQueue = listenConn->flags & COMM_REUSEPORT;
39  if (!giveEachWorkerItsOwnQueue && UsingSmp()) {
40  // Ask Coordinator for a listening socket.
41  // All askers share one listening queue.
43  p.sock_type = sock_type;
44  p.proto = proto;
45  p.addr = listenConn->local;
46  p.flags = listenConn->flags;
47  p.fdNote = fdNote;
48  JoinSharedListen(p, callback);
49  return; // wait for the call back
50  }
51 
52  enter_suid();
53  comm_open_listener(sock_type, proto, answer.conn, FdNote(fdNote));
54  const auto savedErrno = errno;
55  leave_suid();
56 
57  answer.errNo = Comm::IsConnOpen(answer.conn) ? 0 : savedErrno;
58 
59  debugs(54, 3, "opened listen " << answer);
60  ScheduleCallHere(callback.release());
61 }
62 
#define ScheduleCallHere(call)
Definition: AsyncCall.h:166
int fdNote
index into fd_note() comment strings
Definition: SharedListen.h:36
int errNo
errno value from the comm_open_listener() call
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:27
const char * FdNote(int fdNodeId)
converts FdNoteId into a string
Definition: FdNotes.cc:16
"shared listen" is when concurrent processes are listening on the same fd
Definition: SharedListen.h:28
void comm_open_listener(int sock_type, int proto, Comm::ConnectionPointer &conn, const char *note)
Definition: comm.cc:257
StartListening() result.
void leave_suid(void)
Definition: tools.cc:559
void StartListening(int sock_type, int proto, const Comm::ConnectionPointer &listenConn, FdNoteId, StartListeningCallback &)
Comm::ConnectionPointer conn
opened listening socket
void JoinSharedListen(const OpenListenerParams &, StartListeningCallback &)
prepare and send SharedListenRequest to Coordinator
Answer & answer()
Ip::Address local
Definition: Connection.h:146
FdNoteId
We cannot send char* FD notes to other processes. Pass int IDs and convert.
Definition: FdNotes.h:20
AsyncCall::Pointer release()
#define COMM_REUSEPORT
Definition: Connection.h:52
Ip::Address addr
will be memset and memcopied
Definition: SharedListen.h:39
void enter_suid(void)
Definition: tools.cc:623
std::ostream & operator<<(std::ostream &os, const QuestionerId &qid)
Definition: QuestionerId.h:63
bool UsingSmp()
Whether there should be more than one worker process running.
Definition: tools.cc:696
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192

 

Introduction

Documentation

Support

Miscellaneous