Squid Web Cache master
Loading...
Searching...
No Matches
Port.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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 "comm.h"
13#include "comm/Connection.h"
14#include "comm/Read.h"
15#include "CommCalls.h"
16#include "Instance.h"
17#include "ipc/Port.h"
18#include "sbuf/Stream.h"
19#include "tools.h"
20#include "util.h"
21
22static const char channelPathPfx[] = DEFAULT_STATEDIR "/";
23static const char coordinatorAddrLabel[] = "-coordinator";
24const char Ipc::strandAddrLabel[] = "-kid";
25
26Ipc::Port::Port(const String& aListenAddr):
27 UdsOp(aListenAddr)
28{
30}
31
33{
35 doListen();
36}
37
39{
40 debugs(54, 6, MYNAME);
41 buf.prepForReading();
43 AsyncCall::Pointer readHandler = JobCallback(54, 6,
44 Dialer, this, Port::noteRead);
45 comm_read(conn(), buf.raw(), buf.size(), readHandler);
46}
47
49{
50 return false; // listen forever
51}
52
53String Ipc::Port::MakeAddr(const char* processLabel, int id)
54{
55 assert(id >= 0);
56 String addr;
57 addr.append(Instance::NamePrefix(channelPathPfx, processLabel));
58 addr.append('-');
59 addr.append(xitoa(id));
60 addr.append(".ipc");
61 return addr;
62}
63
66{
67 static String coordinatorAddr;
68 if (!coordinatorAddr.size()) {
70 coordinatorAddr.append(".ipc");
71 }
72 return coordinatorAddr;
73}
74
75void
77{
78 throw TextException(ToSBuf("bad IPC message type: ", message.rawType()), Here());
79}
80
82void
84{
85 try {
86 receive(message);
87 } catch (...) {
88 debugs(54, DBG_IMPORTANT, "WARNING: Ignoring IPC message" <<
89 Debug::Extra << "message type: " << message.rawType() <<
90 Debug::Extra << "problem: " << CurrentException);
91 }
92}
93
95{
96 debugs(54, 6, params.conn << " flag " << params.flag <<
97 " [" << this << ']');
98 if (params.flag == Comm::OK) {
99 assert(params.buf == buf.raw());
100 debugs(54, 6, "message type: " << buf.rawType());
101 receiveOrIgnore(buf);
102 }
103 // TODO: if there was a fatal error on our socket, close the socket before
104 // trying to listen again and print a level-1 error message.
105
106 doListen();
107}
108
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
#define COMM_DOBIND
Definition Connection.h:49
#define COMM_NONBLOCKING
Definition Connection.h:46
#define Here()
source code location of the caller
Definition Here.h:15
static const char channelPathPfx[]
Definition Port.cc:22
static const char coordinatorAddrLabel[]
Definition Port.cc:23
void comm_read(const Comm::ConnectionPointer &conn, char *buf, int len, AsyncCall::Pointer &callback)
Definition Read.h:59
std::ostream & CurrentException(std::ostream &os)
prints active (i.e., thrown but not yet handled) exception
#define assert(EX)
Definition assert.h:17
virtual void start()
called by AsyncStart; do not call directly
Definition AsyncJob.cc:59
Comm::Flag flag
comm layer result status.
Definition CommCalls.h:82
Comm::ConnectionPointer conn
Definition CommCalls.h:80
static std::ostream & Extra(std::ostream &)
Definition debug.cc:1316
void noteRead(const CommIoCbParams &params)
Definition Port.cc:94
Port(const String &aListenAddr)
Definition Port.cc:26
bool doneAll() const override
whether positive goal has been reached
Definition Port.cc:48
void doListen()
read the next incoming message
Definition Port.cc:38
void start() override=0
called by AsyncStart; do not call directly
Definition Port.cc:32
void receiveOrIgnore(const TypedMsgHdr &)
receive() but ignore any errors
Definition Port.cc:83
virtual void receive(const TypedMsgHdr &)=0
Definition Port.cc:76
static String CoordinatorAddr()
get the IPC message address for coordinator process
Definition Port.cc:65
static String MakeAddr(const char *proccessLabel, int id)
calculates IPC message address for strand id of processLabel type
Definition Port.cc:53
struct msghdr with a known type, fixed-size I/O and control buffers
Definition TypedMsgHdr.h:35
int rawType() const
Definition TypedMsgHdr.h:51
void setOptions(int newOptions)
changes socket options
Definition UdsOp.cc:36
void append(char const *buf, int len)
Definition String.cc:131
size_type size() const
Definition SquidString.h:74
an std::runtime_error with thrower location info
#define MYNAME
Definition Stream.h:219
#define DBG_IMPORTANT
Definition Stream.h:38
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
@ OK
Definition Flag.h:16
SBuf NamePrefix(const char *head, const char *tail=nullptr)
Definition Instance.cc:253
const char strandAddrLabel[]
strand's listening address unique label
Definition Port.cc:24
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition Stream.h:63
const char * xitoa(int num)
Definition util.cc:60