]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ipc/UdsOp.cc
Removed CVS $ markers
[thirdparty/squid.git] / src / ipc / UdsOp.cc
CommitLineData
10cefb7b 1/*
10cefb7b 2 * DEBUG: section 54 Interprocess Communication
3 *
4 */
5
f7f3304a 6#include "squid.h"
10cefb7b 7#include "comm.h"
1bac0258 8#include "CommCalls.h"
8942ceb3 9#include "comm/Connection.h"
ec41b64c 10#include "comm/Write.h"
a67d2b2e 11#include "base/TextException.h"
10cefb7b 12#include "ipc/UdsOp.h"
13
ba568924 14Ipc::UdsOp::UdsOp(const String& pathAddr):
5667a628
AR
15 AsyncJob("Ipc::UdsOp"),
16 address(PathToAddress(pathAddr)),
e0d28505 17 options(COMM_NONBLOCKING)
10cefb7b 18{
ba568924 19 debugs(54, 5, HERE << '[' << this << "] pathAddr=" << pathAddr);
10cefb7b 20}
21
22Ipc::UdsOp::~UdsOp()
23{
24 debugs(54, 5, HERE << '[' << this << ']');
e0d28505
AJ
25 if (Comm::IsConnOpen(conn_))
26 conn_->close();
27 conn_ = NULL;
10cefb7b 28}
29
ba568924 30void Ipc::UdsOp::setOptions(int newOptions)
10cefb7b 31{
ba568924 32 options = newOptions;
10cefb7b 33}
34
e0d28505
AJ
35Comm::ConnectionPointer &
36Ipc::UdsOp::conn()
10cefb7b 37{
e0d28505 38 if (!Comm::IsConnOpen(conn_)) {
ba568924 39 if (options & COMM_DOBIND)
1bac0258 40 unlink(address.sun_path);
e0d28505
AJ
41 if (conn_ == NULL)
42 conn_ = new Comm::Connection;
43 conn_->fd = comm_open_uds(SOCK_DGRAM, 0, &address, options);
44 Must(Comm::IsConnOpen(conn_));
10cefb7b 45 }
e0d28505 46 return conn_;
10cefb7b 47}
48
ba568924 49void Ipc::UdsOp::setTimeout(int seconds, const char *handlerName)
10cefb7b 50{
4299f876 51 typedef CommCbMemFunT<UdsOp, CommTimeoutCbParams> Dialer;
ba568924 52 AsyncCall::Pointer handler = asyncCall(54,5, handlerName,
4299f876 53 Dialer(CbcPointer<UdsOp>(this), &UdsOp::noteTimeout));
8d77a37c 54 commSetConnTimeout(conn(), seconds, handler);
ba568924
AR
55}
56
57void Ipc::UdsOp::clearTimeout()
58{
8d77a37c 59 commUnsetConnTimeout(conn());
ba568924
AR
60}
61
62void Ipc::UdsOp::noteTimeout(const CommTimeoutCbParams &)
63{
64 timedout(); // our kid handles communication timeout
10cefb7b 65}
66
1bac0258 67struct sockaddr_un
5667a628 68Ipc::PathToAddress(const String& pathAddr) {
1bac0258
AR
69 assert(pathAddr.size() != 0);
70 struct sockaddr_un unixAddr;
71 memset(&unixAddr, 0, sizeof(unixAddr));
72 unixAddr.sun_family = AF_LOCAL;
73 xstrncpy(unixAddr.sun_path, pathAddr.termedBuf(), sizeof(unixAddr.sun_path));
74 return unixAddr;
75}
76
10cefb7b 77CBDATA_NAMESPACED_CLASS_INIT(Ipc, UdsSender);
78
1bac0258 79Ipc::UdsSender::UdsSender(const String& pathAddr, const TypedMsgHdr& aMessage):
5667a628
AR
80 UdsOp(pathAddr),
81 message(aMessage),
82 retries(10), // TODO: make configurable?
83 timeout(10), // TODO: make configurable?
84 writing(false)
10cefb7b 85{
1bac0258 86 message.address(address);
10cefb7b 87}
88
89void Ipc::UdsSender::start()
90{
ba568924 91 UdsOp::start();
10cefb7b 92 write();
93 if (timeout > 0)
ba568924 94 setTimeout(timeout, "Ipc::UdsSender::noteTimeout");
10cefb7b 95}
96
ba568924 97bool Ipc::UdsSender::doneAll() const
10cefb7b 98{
ba568924 99 return !writing && UdsOp::doneAll();
10cefb7b 100}
101
102void Ipc::UdsSender::write()
103{
104 debugs(54, 5, HERE);
4299f876
AR
105 typedef CommCbMemFunT<UdsSender, CommIoCbParams> Dialer;
106 AsyncCall::Pointer writeHandler = JobCallback(54, 5,
4cb2536f 107 Dialer, this, UdsSender::wrote);
b0388924 108 Comm::Write(conn(), message.raw(), message.size(), writeHandler, NULL);
ba568924 109 writing = true;
10cefb7b 110}
111
ba568924 112void Ipc::UdsSender::wrote(const CommIoCbParams& params)
10cefb7b 113{
8bbb16e3 114 debugs(54, 5, HERE << params.conn << " flag " << params.flag << " retries " << retries << " [" << this << ']');
ba568924 115 writing = false;
1bac0258
AR
116 if (params.flag != COMM_OK && retries-- > 0) {
117 sleep(1); // do not spend all tries at once; XXX: use an async timed event instead of blocking here; store the time when we started writing so that we do not sleep if not needed?
e0d28505 118 write(); // XXX: should we close on error so that conn() reopens?
1bac0258 119 }
10cefb7b 120}
121
ba568924 122void Ipc::UdsSender::timedout()
10cefb7b 123{
124 debugs(54, 5, HERE);
ba568924 125 mustStop("timedout");
10cefb7b 126}
127
1bac0258 128void Ipc::SendMessage(const String& toAddress, const TypedMsgHdr &message)
10cefb7b 129{
4299f876 130 AsyncJob::Start(new UdsSender(toAddress, message));
10cefb7b 131}
51ea0904 132
1b76e6c1
AJ
133const Comm::ConnectionPointer &
134Ipc::ImportFdIntoComm(const Comm::ConnectionPointer &conn, int socktype, int protocol, Ipc::FdNoteId noteId)
51ea0904
CT
135{
136 struct sockaddr_in addr;
137 socklen_t len = sizeof(addr);
1b76e6c1
AJ
138 if (getsockname(conn->fd, reinterpret_cast<sockaddr*>(&addr), &len) == 0) {
139 conn->remote = addr;
51ea0904 140 struct addrinfo* addr_info = NULL;
1b76e6c1 141 conn->remote.GetAddrInfo(addr_info);
51ea0904
CT
142 addr_info->ai_socktype = socktype;
143 addr_info->ai_protocol = protocol;
1b76e6c1
AJ
144 comm_import_opened(conn, Ipc::FdNote(noteId), addr_info);
145 conn->remote.FreeAddrInfo(addr_info);
51ea0904 146 } else {
1b76e6c1
AJ
147 debugs(54, DBG_CRITICAL, "ERROR: Ipc::ImportFdIntoComm: " << conn << ' ' << xstrerror());
148 conn->close();
51ea0904 149 }
1b76e6c1 150 return conn;
51ea0904 151}