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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors