]>
Commit | Line | Data |
---|---|---|
10cefb7b | 1 | /* |
2 | * $Id$ | |
3 | * | |
4 | * DEBUG: section 54 Interprocess Communication | |
5 | * | |
6 | */ | |
7 | ||
8 | ||
9 | #include "config.h" | |
10 | #include "comm.h" | |
11 | #include "ipc/UdsOp.h" | |
12 | ||
10cefb7b | 13 | |
ba568924 | 14 | Ipc::Message::Message() |
10cefb7b | 15 | { |
ba568924 AR |
16 | data.messageType = mtNone; |
17 | data.strand.kidId = -1; | |
10cefb7b | 18 | } |
19 | ||
20 | Ipc::Message::Message(MessageType messageType, int kidId, pid_t pid) | |
21 | { | |
22 | data.messageType = messageType; | |
23 | data.strand.kidId = kidId; | |
24 | data.strand.pid = pid; | |
25 | } | |
26 | ||
ba568924 | 27 | const Ipc::StrandData &Ipc::Message::strand() const |
10cefb7b | 28 | { |
ba568924 AR |
29 | Must(data.messageType == mtRegistration); |
30 | return data.strand; | |
10cefb7b | 31 | } |
32 | ||
ba568924 | 33 | Ipc::UdsOp::UdsOp(const String& pathAddr): |
10cefb7b | 34 | AsyncJob("Ipc::UdsOp"), |
35 | addr(setAddr(pathAddr)), | |
36 | options(COMM_NONBLOCKING), | |
37 | fd_(-1) | |
38 | { | |
ba568924 | 39 | debugs(54, 5, HERE << '[' << this << "] pathAddr=" << pathAddr); |
10cefb7b | 40 | } |
41 | ||
42 | Ipc::UdsOp::~UdsOp() | |
43 | { | |
44 | debugs(54, 5, HERE << '[' << this << ']'); | |
ba568924 | 45 | if (fd_ >= 0) |
10cefb7b | 46 | comm_close(fd_); |
47 | } | |
48 | ||
ba568924 | 49 | void Ipc::UdsOp::setOptions(int newOptions) |
10cefb7b | 50 | { |
ba568924 | 51 | options = newOptions; |
10cefb7b | 52 | } |
53 | ||
54 | int Ipc::UdsOp::fd() | |
55 | { | |
56 | if (fd_ < 0) { | |
ba568924 AR |
57 | if (options & COMM_DOBIND) |
58 | unlink(addr.sun_path); | |
10cefb7b | 59 | fd_ = comm_open_uds(SOCK_DGRAM, 0, &addr, options); |
ba568924 | 60 | Must(fd_ >= 0); |
10cefb7b | 61 | } |
62 | return fd_; | |
63 | } | |
64 | ||
65 | struct sockaddr_un Ipc::UdsOp::setAddr(const String& pathAddr) | |
66 | { | |
67 | assert(pathAddr.size() != 0); | |
68 | struct sockaddr_un unixAddr; | |
69 | memset(&unixAddr, 0, sizeof(unixAddr)); | |
70 | unixAddr.sun_family = AF_LOCAL; | |
71 | xstrncpy(unixAddr.sun_path, pathAddr.termedBuf(), sizeof(unixAddr.sun_path)); | |
72 | return unixAddr; | |
73 | } | |
74 | ||
ba568924 | 75 | void Ipc::UdsOp::setTimeout(int seconds, const char *handlerName) |
10cefb7b | 76 | { |
ba568924 AR |
77 | AsyncCall::Pointer handler = asyncCall(54,5, handlerName, |
78 | CommCbMemFunT<UdsOp, CommTimeoutCbParams>(this, | |
79 | &UdsOp::noteTimeout)); | |
80 | commSetTimeout(fd(), seconds, handler); | |
81 | } | |
82 | ||
83 | void Ipc::UdsOp::clearTimeout() | |
84 | { | |
85 | commSetTimeout(fd(), -1, NULL, NULL); // TODO: add Comm::ClearTimeout(fd) | |
86 | } | |
87 | ||
88 | void Ipc::UdsOp::noteTimeout(const CommTimeoutCbParams &) | |
89 | { | |
90 | timedout(); // our kid handles communication timeout | |
10cefb7b | 91 | } |
92 | ||
93 | ||
94 | CBDATA_NAMESPACED_CLASS_INIT(Ipc, UdsSender); | |
95 | ||
96 | Ipc::UdsSender::UdsSender(const String& pathAddr, const Message& aMessage): | |
ba568924 | 97 | UdsOp(pathAddr), |
10cefb7b | 98 | message(aMessage), |
ba568924 AR |
99 | retries(4), // TODO: make configurable? |
100 | timeout(5), // TODO: make configurable? | |
101 | writing(false) | |
10cefb7b | 102 | { |
10cefb7b | 103 | } |
104 | ||
105 | void Ipc::UdsSender::start() | |
106 | { | |
ba568924 | 107 | UdsOp::start(); |
10cefb7b | 108 | write(); |
109 | if (timeout > 0) | |
ba568924 | 110 | setTimeout(timeout, "Ipc::UdsSender::noteTimeout"); |
10cefb7b | 111 | } |
112 | ||
ba568924 | 113 | bool Ipc::UdsSender::doneAll() const |
10cefb7b | 114 | { |
ba568924 | 115 | return !writing && UdsOp::doneAll(); |
10cefb7b | 116 | } |
117 | ||
118 | void Ipc::UdsSender::write() | |
119 | { | |
120 | debugs(54, 5, HERE); | |
ba568924 AR |
121 | AsyncCall::Pointer writeHandler = asyncCall(54, 5, "Ipc::UdsSender::wrote", |
122 | CommCbMemFunT<UdsSender, CommIoCbParams>(this, &UdsSender::wrote)); | |
123 | comm_write(fd(), message.raw(), message.size(), writeHandler); | |
124 | writing = true; | |
10cefb7b | 125 | } |
126 | ||
ba568924 | 127 | void Ipc::UdsSender::wrote(const CommIoCbParams& params) |
10cefb7b | 128 | { |
129 | debugs(54, 5, HERE << "FD " << params.fd << " flag " << params.flag << " [" << this << ']'); | |
ba568924 AR |
130 | writing = false; |
131 | if (params.flag != COMM_OK && retries-- > 0) | |
132 | write(); // XXX: should we close on error so that fd() reopens? | |
10cefb7b | 133 | } |
134 | ||
ba568924 | 135 | void Ipc::UdsSender::timedout() |
10cefb7b | 136 | { |
137 | debugs(54, 5, HERE); | |
ba568924 | 138 | mustStop("timedout"); |
10cefb7b | 139 | } |
140 | ||
141 | ||
142 | void Ipc::SendMessage(const String& toAddress, const Message& message) | |
143 | { | |
144 | AsyncJob::AsyncStart(new UdsSender(toAddress, message)); | |
145 | } |