XRootD
XrdLinkXeq.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d L i n k X e q . c c */
4 /* */
5 /* (c) 2018 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* Produced by Andrew Hanushevsky for Stanford University under contract */
7 /* DE-AC02-76-SFO0515 with the Department of Energy */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19 /* License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24 /* */
25 /* The copyright holder's institutional names and contributor's names may not */
26 /* be used to endorse or promote products derived from this software without */
27 /* specific prior written permission of the institution or contributor. */
28 /******************************************************************************/
29 
30 #include <limits.h>
31 #include <poll.h>
32 #include <signal.h>
33 #include <cstdio>
34 #include <cstring>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/uio.h>
38 
39 #if defined(__linux__) || defined(__GNU__)
40 #include <netinet/tcp.h>
41 #if !defined(TCP_CORK)
42 #undef HAVE_SENDFILE
43 #endif
44 #endif
45 
46 #ifdef HAVE_SENDFILE
47 
48 #if defined(__solaris__) || defined(__linux__) || defined(__GNU__)
49 #include <sys/sendfile.h>
50 #endif
51 
52 #endif
53 
54 #include "XrdSys/XrdSysAtomics.hh"
55 #include "XrdSys/XrdSysError.hh"
56 #include "XrdSys/XrdSysFD.hh"
57 #include "XrdSys/XrdSysPlatform.hh"
58 
59 #include "Xrd/XrdBuffer.hh"
60 #include "Xrd/XrdLink.hh"
61 #include "Xrd/XrdLinkCtl.hh"
62 #include "Xrd/XrdLinkXeq.hh"
63 #include "Xrd/XrdPoll.hh"
64 #include "Xrd/XrdScheduler.hh"
65 #include "Xrd/XrdSendQ.hh"
66 #include "Xrd/XrdTcpMonPin.hh"
67 
68 #define TRACE_IDENT ID
69 #include "Xrd/XrdTrace.hh"
70 
71 /******************************************************************************/
72 /* G l o b a l s */
73 /******************************************************************************/
74 
75 namespace XrdGlobal
76 {
77 extern XrdSysError Log;
78 extern XrdScheduler Sched;
79 extern XrdTlsContext *tlsCtx;
81 extern int devNull;
82  const int maxIOV = XrdSys::getIovMax();
83 };
84 
85 using namespace XrdGlobal;
86 
87 /******************************************************************************/
88 /* S t a t i c s */
89 /******************************************************************************/
90 
91  const char *XrdLinkXeq::TraceID = "LinkXeq";
92 
93  long long XrdLinkXeq::LinkBytesIn = 0;
94  long long XrdLinkXeq::LinkBytesOut = 0;
95  long long XrdLinkXeq::LinkConTime = 0;
96  long long XrdLinkXeq::LinkCountTot = 0;
97  int XrdLinkXeq::LinkCount = 0;
100  int XrdLinkXeq::LinkStalls = 0;
101  int XrdLinkXeq::LinkSfIntr = 0;
103 
104 /******************************************************************************/
105 /* C o n s t r u c t o r */
106 /******************************************************************************/
107 
108 XrdLinkXeq::XrdLinkXeq() : XrdLink(*this), PollInfo((XrdLink &)*this)
109 {
111 }
112 
114 {
115  memcpy(Uname+sizeof(Uname)-7, "anon.0@", 7);
116  strcpy(Lname, "somewhere");
117  ID = &Uname[sizeof(Uname)-5];
118  Comment = ID;
119  sendQ = 0;
120  stallCnt = stallCntTot = 0;
121  tardyCnt = tardyCntTot = 0;
122  SfIntr = 0;
123  isIdle = 0;
125  LockReads= false;
126  KeepFD = false;
127  Protocol = 0;
128  ProtoAlt = 0;
129  CloseRequestCb = 0;
130 
131  LinkInfo.Reset();
132  PollInfo.Zorch();
133  ResetLink();
134 }
135 
136 /******************************************************************************/
137 /* B a c k l o g */
138 /******************************************************************************/
139 
141 {
143 
144 // Return backlog information
145 //
146  return (sendQ ? sendQ->Backlog() : 0);
147 }
148 
149 /******************************************************************************/
150 /* C l i e n t */
151 /******************************************************************************/
152 
153 int XrdLinkXeq::Client(char *nbuf, int nbsz)
154 {
155  int ulen;
156 
157 // Generate full client name
158 //
159  if (nbsz <= 0) return 0;
160  ulen = (Lname - ID);
161  if ((ulen + HNlen) >= nbsz) ulen = 0;
162  else {strncpy(nbuf, ID, ulen);
163  strcpy(nbuf+ulen, HostName);
164  ulen += HNlen;
165  }
166  return ulen;
167 }
168 
169 /******************************************************************************/
170 /* C l o s e */
171 /******************************************************************************/
172 
173 int XrdLinkXeq::Close(bool defer)
175  int csec, fd, rc = 0;
176 
177 // If a defer close is requested, we can close the descriptor but we must
178 // keep the slot number to prevent a new client getting the same fd number.
179 // Linux is peculiar in that any in-progress operations will remain in that
180 // state even after the FD is closed unless there is some activity either on
181 // the connection or an event occurs that causes an operation restart. We
182 // portably solve this problem by issuing a shutdown() on the socket prior
183 // closing it. On most platforms, this informs readers that the connection is
184 // gone (though not on old (i.e. <= 2.3) versions of Linux, sigh). Also, if
185 // nonblocking mode is enabled, we need to do this in a separate thread as
186 // a shutdown may block for a pretty long time if lots\ of messages are queued.
187 // We will ask the SendQ object to schedule the shutdown for us before it
188 // commits suicide.
189 // Note that we can hold the opMutex while we also get the wrMutex.
190 //
191  if (defer)
192  {if (!sendQ) Shutdown(false);
193  else {TRACEI(DEBUG, "Shutdown FD " <<LinkInfo.FD<<" only via SendQ");
194  LinkInfo.InUse++;
195  LinkInfo.FD = -LinkInfo.FD; // Leave poll version untouched!
196  wrMutex.Lock();
197  sendQ->Terminate(this);
198  sendQ = 0;
199  wrMutex.UnLock();
200  }
201  return 0;
202  }
203 
204 // If we got here then this is not a deferred close so we just need to check
205 // if there is a sendq appendage we need to get rid of.
206 //
207  if (sendQ)
208  {wrMutex.Lock();
209  sendQ->Terminate();
210  sendQ = 0;
211  wrMutex.UnLock();
212  }
213 
214 // Multiple protocols may be bound to this link. If it is in use, defer the
215 // actual close until the use count drops to one.
216 //
217  while(LinkInfo.InUse > 1)
218  {opHelper.UnLock();
219  TRACEI(DEBUG, "Close FD "<<LinkInfo.FD <<" deferred, use count="
220  <<LinkInfo.InUse);
221  Serialize();
222  opHelper.Lock(&LinkInfo.opMutex);
223  }
224  LinkInfo.InUse--;
225  Instance = 0;
226 
227 // Add up the statistic for this link
228 //
229  syncStats(&csec);
230 
231 // Cleanup TLS if it is active
232 //
233  if (isTLS) tlsIO.Shutdown();
234 
235 // Clean this link up
236 //
237  if (Protocol) {Protocol->Recycle(this, csec, LinkInfo.Etext); Protocol = 0;}
238  if (ProtoAlt) {ProtoAlt->Recycle(this, csec, LinkInfo.Etext); ProtoAlt = 0;}
239  if (LinkInfo.Etext) {free(LinkInfo.Etext); LinkInfo.Etext = 0;}
240  LinkInfo.InUse = 0;
241 
242 // At this point we can have no lock conflicts, so if someone is waiting for
243 // us to terminate let them know about it. Note that we will get the condvar
244 // mutex while we hold the opMutex. This is the required order! We will also
245 // zero out the pointer to the condvar while holding the opmutex.
246 //
247  if (LinkInfo.KillcvP)
248  {LinkInfo.KillcvP->Lock();
251  LinkInfo.KillcvP = 0;
252  }
253 
254 // Remove ourselves from the poll table and then from the Link table. We may
255 // not hold on to the opMutex when we acquire the LTMutex. However, the link
256 // table needs to be cleaned up prior to actually closing the socket. So, we
257 // do some fancy footwork to prevent multiple closes of this link.
258 //
259  fd = abs(LinkInfo.FD);
260  if (PollInfo.FD > 0)
262  PollInfo.FD = -1;
263  opHelper.UnLock();
264  XrdLinkCtl::Unhook(fd);
265  } else opHelper.UnLock();
266 
267 // Invoke the TCP monitor if it was loaded.
268 //
269  if (TcpMonPin && fd > 2)
270  {XrdTcpMonPin::LinkInfo lnkInfo;
271  lnkInfo.tident = ID;
272  lnkInfo.fd = fd;
273  lnkInfo.consec = csec;
274  lnkInfo.bytesIn = BytesInTot;
275  lnkInfo.bytesOut = BytesOutTot;
276  TcpMonPin->Monitor(Addr, lnkInfo, sizeof(lnkInfo));
277  }
278 
279 // Close the file descriptor if it isn't being shared. Do it as the last
280 // thing because closes and accepts and not interlocked.
281 //
282  if (fd >= 2) {if (KeepFD) rc = 0;
283  else rc = (close(fd) < 0 ? errno : 0);
284  }
285  if (rc) Log.Emsg("Link", rc, "close", ID);
286  return rc;
287 }
288 
289 /******************************************************************************/
290 /* D o I t */
291 /******************************************************************************/
292 
294 {
295  int rc;
296 
297 // The Process() return code tells us what to do:
298 // < 0 -> Stop getting requests,
299 // -EINPROGRESS leave link disabled but otherwise all is well
300 // -n Error, disable and close the link
301 // = 0 -> OK, get next request, if allowed, o/w enable the link
302 // > 0 -> Slow link, stop getting requests and enable the link
303 //
304  if (Protocol)
305  do {rc = Protocol->Process(this);} while (!rc && Sched.canStick());
306  else {Log.Emsg("Link", "Dispatch on closed link", ID);
307  return;
308  }
309 
310 // Either re-enable the link and cycle back waiting for a new request, leave
311 // disabled, or terminate the connection.
312 //
313  bool doCl = false;
314  if (rc >= 0)
315  {if (PollInfo.Poller && !PollInfo.Poller->Enable(PollInfo)) doCl = true;}
316  else if (rc != -EINPROGRESS) doCl = true;
317 
318  if (doCl)
319  {if (CloseRequestCb)
320  {const bool res = CloseRequestCb(CloseRequestCbArg);
321  if (!res) return;
322  }
323  Close();
324  }
325 }
326 
327 /******************************************************************************/
328 /* g e t P e e r C e r t s */
329 /******************************************************************************/
330 
332 {
333  return (isTLS ? tlsIO.getCerts(true) : 0);
334 }
335 
336 /******************************************************************************/
337 /* P e e k */
338 /******************************************************************************/
339 
340 int XrdLinkXeq::Peek(char *Buff, int Blen, int timeout)
341 {
342  XrdSysMutexHelper theMutex;
343  struct pollfd polltab = {PollInfo.FD, POLLIN|POLLRDNORM, 0};
344  ssize_t mlen;
345  int retc;
346 
347 // Lock the read mutex if we need to, the helper will unlock it upon exit
348 //
349  if (LockReads) theMutex.Lock(&rdMutex);
350 
351 // Wait until we can actually read something
352 //
353  isIdle = 0;
354  do {retc = poll(&polltab, 1, timeout);} while(retc < 0 && errno == EINTR);
355  if (retc != 1)
356  {if (retc == 0) return 0;
357  return Log.Emsg("Link", -errno, "poll", ID);
358  }
359 
360 // Verify it is safe to read now
361 //
362  if (!(polltab.revents & (POLLIN|POLLRDNORM)))
363  {Log.Emsg("Link", XrdPoll::Poll2Text(polltab.revents), "polling", ID);
364  return -1;
365  }
366 
367 // Do the peek.
368 //
369  do {mlen = recv(LinkInfo.FD, Buff, Blen, MSG_PEEK);}
370  while(mlen < 0 && errno == EINTR);
371 
372 // Return the result
373 //
374  if (mlen >= 0) return int(mlen);
375  Log.Emsg("Link", errno, "peek on", ID);
376  return -1;
377 }
378 
379 /******************************************************************************/
380 /* R e c v */
381 /******************************************************************************/
382 
383 int XrdLinkXeq::Recv(char *Buff, int Blen)
384 {
385  ssize_t rlen;
386 
387 // Note that we will read only as much as is queued. Use Recv() with a
388 // timeout to receive as much data as possible.
389 //
390  if (LockReads) rdMutex.Lock();
391  isIdle = 0;
392  do {rlen = read(LinkInfo.FD, Buff, Blen);} while(rlen < 0 && errno == EINTR);
393  if (rlen > 0) AtomicAdd(BytesIn, rlen);
394  if (LockReads) rdMutex.UnLock();
395 
396  if (rlen >= 0) return int(rlen);
397  if (LinkInfo.FD >= 0) Log.Emsg("Link", errno, "receive from", ID);
398  return -1;
399 }
400 
401 /******************************************************************************/
402 
403 int XrdLinkXeq::Recv(char *Buff, int Blen, int timeout)
404 {
405  XrdSysMutexHelper theMutex;
406  struct pollfd polltab = {PollInfo.FD, POLLIN|POLLRDNORM, 0};
407  ssize_t rlen, totlen = 0;
408  int retc;
409 
410 // Lock the read mutex if we need to, the helper will unlock it upon exit
411 //
412  if (LockReads) theMutex.Lock(&rdMutex);
413 
414 // Wait up to timeout milliseconds for data to arrive
415 //
416  isIdle = 0;
417  while(Blen > 0)
418  {do {retc = poll(&polltab,1,timeout);} while(retc < 0 && errno == EINTR);
419  if (retc != 1)
420  {if (retc == 0)
421  {tardyCnt++;
422  if (totlen)
423  {if ((++stallCnt & 0xff) == 1) TRACEI(DEBUG,"read timed out");
424  AtomicAdd(BytesIn, totlen);
425  }
426  return int(totlen);
427  }
428  return (LinkInfo.FD >= 0 ? Log.Emsg("Link",-errno,"poll",ID) : -1);
429  }
430 
431  // Verify it is safe to read now
432  //
433  if (!(polltab.revents & (POLLIN|POLLRDNORM)))
434  {Log.Emsg("Link", XrdPoll::Poll2Text(polltab.revents),
435  "polling", ID);
436  return -1;
437  }
438 
439  // Read as much data as you can. Note that we will force an error
440  // if we get a zero-length read after poll said it was OK.
441  //
442  do {rlen = recv(LinkInfo.FD, Buff, Blen, 0);}
443  while(rlen < 0 && errno == EINTR);
444  if (rlen <= 0)
445  {if (!rlen) return -ENOMSG;
446  if (LinkInfo.FD > 0) Log.Emsg("Link", -errno, "receive from", ID);
447  return -1;
448  }
449  totlen += rlen; Blen -= rlen; Buff += rlen;
450  }
451 
452  AtomicAdd(BytesIn, totlen);
453  return int(totlen);
454 }
455 
456 /******************************************************************************/
457 
458 int XrdLinkXeq::Recv(const struct iovec *iov, int iocnt, int timeout)
459 {
460  XrdSysMutexHelper theMutex;
461  struct pollfd polltab = {PollInfo.FD, POLLIN|POLLRDNORM, 0};
462  int retc, rlen;
463 
464 // Lock the read mutex if we need to, the helper will unlock it upon exit
465 //
466  if (LockReads) theMutex.Lock(&rdMutex);
467 
468 // Wait up to timeout milliseconds for data to arrive
469 //
470  isIdle = 0;
471  do {retc = poll(&polltab,1,timeout);} while(retc < 0 && errno == EINTR);
472  if (retc != 1)
473  {if (retc == 0)
474  {tardyCnt++;
475  return 0;
476  }
477  return (LinkInfo.FD >= 0 ? Log.Emsg("Link",-errno,"poll",ID) : -1);
478  }
479 
480 // Verify it is safe to read now
481 //
482  if (!(polltab.revents & (POLLIN|POLLRDNORM)))
483  {Log.Emsg("Link", XrdPoll::Poll2Text(polltab.revents), "polling", ID);
484  return -1;
485  }
486 
487 // If the iocnt is within limits then just go ahead and read once.
488 //
489  if (iocnt <= maxIOV)
490  {rlen = RecvIOV(iov, iocnt);
491  if (rlen > 0) {AtomicAdd(BytesIn, rlen);}
492  return rlen;
493  }
494 
495 // We will have to break this up into allowable segments and we need to add up
496 // the bytes in each segment so that we know when to stop reading.
497 //
498  int seglen, segcnt = maxIOV, totlen = 0;
499  do {seglen = 0;
500  for (int i = 0; i < segcnt; i++) seglen += iov[i].iov_len;
501  if ((rlen = RecvIOV(iov, segcnt)) < 0) return rlen;
502  totlen += rlen;
503  if (rlen < seglen) break;
504  iov += segcnt;
505  iocnt -= segcnt;
506  if (iocnt <= maxIOV) segcnt = iocnt;
507  } while(iocnt > 0);
508 
509 // All done
510 //
511  AtomicAdd(BytesIn, totlen);
512  return totlen;
513 }
514 
515 /******************************************************************************/
516 /* R e c v A l l */
517 /******************************************************************************/
518 
519 int XrdLinkXeq::RecvAll(char *Buff, int Blen, int timeout)
520 {
521  struct pollfd polltab = {PollInfo.FD, POLLIN|POLLRDNORM, 0};
522  ssize_t rlen;
523  int retc;
524 
525 // Check if timeout specified. Notice that the timeout is the max we will
526 // for some data. We will wait forever for all the data. Yeah, it's weird.
527 //
528  if (timeout >= 0)
529  {do {retc = poll(&polltab,1,timeout);} while(retc < 0 && errno == EINTR);
530  if (retc != 1)
531  {if (!retc) return -ETIMEDOUT;
532  Log.Emsg("Link",errno,"poll",ID);
533  return -1;
534  }
535  if (!(polltab.revents & (POLLIN|POLLRDNORM)))
536  {Log.Emsg("Link",XrdPoll::Poll2Text(polltab.revents),"polling",ID);
537  return -1;
538  }
539  }
540 
541 // Note that we will block until we receive all he bytes.
542 //
543  if (LockReads) rdMutex.Lock();
544  isIdle = 0;
545  do {rlen = recv(LinkInfo.FD, Buff, Blen, MSG_WAITALL);}
546  while(rlen < 0 && errno == EINTR);
547  if (rlen > 0) AtomicAdd(BytesIn, rlen);
548  if (LockReads) rdMutex.UnLock();
549 
550  if (int(rlen) == Blen) return Blen;
551  if (!rlen) {TRACEI(DEBUG, "No RecvAll() data; errno=" <<errno);}
552  else if (rlen > 0) Log.Emsg("RecvAll", "Premature end from", ID);
553  else if (LinkInfo.FD >= 0) Log.Emsg("Link", errno, "receive from", ID);
554  return -1;
555 }
556 
557 /******************************************************************************/
558 /* Protected: R e c v I O V */
559 /******************************************************************************/
560 
561 int XrdLinkXeq::RecvIOV(const struct iovec *iov, int iocnt)
562 {
563  ssize_t retc = 0;
564 
565 // Read the data in. On some version of Unix (e.g., Linux) a readv() may
566 // end at any time without reading all the bytes when directed to a socket.
567 // We always return the number bytes read (or an error). The caller needs to
568 // restart the read at the appropriate place in the iovec when more data arrives.
569 //
570  do {retc = readv(LinkInfo.FD, iov, iocnt);}
571  while(retc < 0 && errno == EINTR);
572 
573 // Check how we completed
574 //
575  if (retc < 0) Log.Emsg("Link", errno, "receive from", ID);
576  return retc;
577 }
578 
579 /******************************************************************************/
580 /* R e g i s t e r */
581 /******************************************************************************/
582 
583 bool XrdLinkXeq::Register(const char *hName)
584 {
585 
586 // Make appropriate changes here
587 //
588  if (HostName) free(HostName);
589  HostName = strdup(hName);
590  strlcpy(Lname, hName, sizeof(Lname));
591  return true;
592 }
593 
594 /******************************************************************************/
595 /* S e n d */
596 /******************************************************************************/
597 
598 int XrdLinkXeq::Send(const char *Buff, int Blen)
599 {
600  ssize_t retc = 0, bytesleft = Blen;
601 
602 // Get a lock
603 //
604  wrMutex.Lock();
605  isIdle = 0;
606  AtomicAdd(BytesOut, Blen);
607 
608 // Do non-blocking writes if we are setup to do so.
609 //
610  if (sendQ)
611  {retc = sendQ->Send(Buff, Blen);
612  wrMutex.UnLock();
613  return retc;
614  }
615 
616 // Write the data out
617 //
618  while(bytesleft)
619  {if ((retc = write(LinkInfo.FD, Buff, bytesleft)) < 0)
620  {if (errno == EINTR) continue;
621  else break;
622  }
623  bytesleft -= retc; Buff += retc;
624  }
625 
626 // All done
627 //
628  wrMutex.UnLock();
629  if (retc >= 0) return Blen;
630  Log.Emsg("Link", errno, "send to", ID);
631  return -1;
632 }
633 
634 /******************************************************************************/
635 
636 int XrdLinkXeq::Send(const struct iovec *iov, int iocnt, int bytes)
637 {
638  int retc;
639 
640 // Get a lock and assume we will be successful (statistically we are)
641 //
642  wrMutex.Lock();
643  isIdle = 0;
644  AtomicAdd(BytesOut, bytes);
645 
646 // Do non-blocking writes if we are setup to do so.
647 //
648  if (sendQ)
649  {retc = sendQ->Send(iov, iocnt, bytes);
650  wrMutex.UnLock();
651  return retc;
652  }
653 
654 // If the iocnt is within limits then just go ahead and write this out
655 //
656  if (iocnt <= maxIOV)
657  {retc = SendIOV(iov, iocnt, bytes);
658  wrMutex.UnLock();
659  return retc;
660  }
661 
662 // We will have to break this up into allowable segments
663 //
664  int seglen, segcnt = maxIOV, iolen = 0;
665  do {seglen = 0;
666  for (int i = 0; i < segcnt; i++) seglen += iov[i].iov_len;
667  if ((retc = SendIOV(iov, segcnt, seglen)) < 0)
668  {wrMutex.UnLock();
669  return retc;
670  }
671  iolen += retc;
672  iov += segcnt;
673  iocnt -= segcnt;
674  if (iocnt <= maxIOV) segcnt = iocnt;
675  } while(iocnt > 0);
676 
677 // All done
678 //
679  wrMutex.UnLock();
680  return iolen;
681 }
682 
683 /******************************************************************************/
684 
685 int XrdLinkXeq::Send(const sfVec *sfP, int sfN)
686 {
687 #if !defined(HAVE_SENDFILE)
688 
689  return -1;
690 
691 #elif defined(__solaris__)
692 
693  sendfilevec_t vecSF[XrdOucSFVec::sfMax], *vecSFP = vecSF;
694  size_t xframt, totamt, bytes = 0;
695  ssize_t retc;
696  int i = 0;
697 
698 // Construct the sendfilev() vector
699 //
700  for (i = 0; i < sfN; sfP++, i++)
701  {if (sfP->fdnum < 0)
702  {vecSF[i].sfv_fd = SFV_FD_SELF;
703  vecSF[i].sfv_off = (off_t)sfP->buffer;
704  } else {
705  vecSF[i].sfv_fd = sfP->fdnum;
706  vecSF[i].sfv_off = sfP->offset;
707  }
708  vecSF[i].sfv_flag = 0;
709  vecSF[i].sfv_len = sfP->sendsz;
710  bytes += sfP->sendsz;
711  }
712  totamt = bytes;
713 
714 // Lock the link, issue sendfilev(), and unlock the link. The documentation
715 // is very spotty and inconsistent. We can only retry this operation under
716 // very limited conditions.
717 //
718  wrMutex.Lock();
719  isIdle = 0;
720 do{retc = sendfilev(LinkInfo.FD, vecSFP, sfN, &xframt);
721 
722 // Check if all went well and return if so (usual case)
723 //
724  if (xframt == bytes)
725  {AtomicAdd(BytesOut, bytes);
726  wrMutex.UnLock();
727  return totamt;
728  }
729 
730 // The only one we will recover from is EINTR. We cannot legally get EAGAIN.
731 //
732  if (retc < 0 && errno != EINTR) break;
733 
734 // Try to resume the transfer
735 //
736  if (xframt > 0)
737  {AtomicAdd(BytesOut, xframt); bytes -= xframt; SfIntr++;
738  while(xframt > 0 && sfN)
739  {if ((ssize_t)xframt < (ssize_t)vecSFP->sfv_len)
740  {vecSFP->sfv_off += xframt; vecSFP->sfv_len -= xframt; break;}
741  xframt -= vecSFP->sfv_len; vecSFP++; sfN--;
742  }
743  }
744  } while(sfN > 0);
745 
746 // See if we can recover without destroying the connection
747 //
748  retc = (retc < 0 ? errno : ECANCELED);
749  wrMutex.UnLock();
750  Log.Emsg("Link", retc, "send file to", ID);
751  return -1;
752 
753 #elif defined(__linux__) || defined(__GNU__)
754 
755  static const int setON = 1, setOFF = 0;
756  ssize_t retc = 0, bytesleft;
757  off_t myOffset;
758  int i, xfrbytes = 0, uncork = 1, xIntr = 0;
759 
760 // lock the link
761 //
762  wrMutex.Lock();
763  isIdle = 0;
764 
765 // In linux we need to cork the socket. On permanent errors we do not uncork
766 // the socket because it will be closed in short order.
767 //
768  if (setsockopt(PollInfo.FD, SOL_TCP, TCP_CORK, &setON, sizeof(setON)) < 0)
769  {Log.Emsg("Link", errno, "cork socket for", ID);
770  uncork = 0; sfOK = 0;
771  }
772 
773 // Send the header first
774 //
775  for (i = 0; i < sfN; sfP++, i++)
776  {if (sfP->fdnum < 0) retc = sendData(sfP->buffer, sfP->sendsz);
777  else {myOffset = sfP->offset; bytesleft = sfP->sendsz;
778  while(bytesleft
779  && (retc=sendfile(LinkInfo.FD,sfP->fdnum,&myOffset,bytesleft)) > 0)
780  {bytesleft -= retc; xIntr++;}
781  }
782  if (retc < 0 && errno == EINTR) continue;
783  if (retc <= 0) break;
784  xfrbytes += sfP->sendsz;
785  }
786 
787 // Diagnose any sendfile errors
788 //
789  if (retc <= 0)
790  {if (retc == 0) errno = ECANCELED;
791  wrMutex.UnLock();
792  Log.Emsg("Link", errno, "send file to", ID);
793  return -1;
794  }
795 
796 // Now uncork the socket
797 //
798  if (uncork
799  && setsockopt(PollInfo.FD, SOL_TCP, TCP_CORK, &setOFF, sizeof(setOFF)) < 0)
800  Log.Emsg("Link", errno, "uncork socket for", ID);
801 
802 // All done
803 //
804  if (xIntr > sfN) SfIntr += (xIntr - sfN);
805  AtomicAdd(BytesOut, xfrbytes);
806  wrMutex.UnLock();
807  return xfrbytes;
808 
809 #else
810 
811  return -1;
812 
813 #endif
814 }
815 
816 /******************************************************************************/
817 /* Protected: s e n d D a t a */
818 /******************************************************************************/
819 
820 int XrdLinkXeq::sendData(const char *Buff, int Blen)
821 {
822  ssize_t retc = 0, bytesleft = Blen;
823 
824 // Write the data out
825 //
826  while(bytesleft)
827  {if ((retc = write(LinkInfo.FD, Buff, bytesleft)) < 0)
828  {if (errno == EINTR) continue;
829  else break;
830  }
831  bytesleft -= retc; Buff += retc;
832  }
833 
834 // All done
835 //
836  return retc;
837 }
838 
839 /******************************************************************************/
840 /* Protected: S e n d I O V */
841 /******************************************************************************/
842 
843 int XrdLinkXeq::SendIOV(const struct iovec *iov, int iocnt, int bytes)
844 {
845  ssize_t bytesleft, n, retc = 0;
846  const char *Buff;
847 
848 // Write the data out. On some version of Unix (e.g., Linux) a writev() may
849 // end at any time without writing all the bytes when directed to a socket.
850 // So, we attempt to resume the writev() using a combination of write() and
851 // a writev() continuation. This approach slowly converts a writev() to a
852 // series of writes if need be. We must do this inline because we must hold
853 // the lock until all the bytes are written or an error occurs.
854 //
855  bytesleft = static_cast<ssize_t>(bytes);
856  while(bytesleft)
857  {do {retc = writev(LinkInfo.FD, iov, iocnt);}
858  while(retc < 0 && errno == EINTR);
859  if (retc >= bytesleft || retc < 0) break;
860  bytesleft -= retc;
861  while(retc >= (n = static_cast<ssize_t>(iov->iov_len)))
862  {retc -= n; iov++; iocnt--;}
863  Buff = (const char *)iov->iov_base + retc; n -= retc; iov++; iocnt--;
864  while(n) {if ((retc = write(LinkInfo.FD, Buff, n)) < 0)
865  {if (errno == EINTR) continue;
866  else break;
867  }
868  n -= retc; Buff += retc; bytesleft -= retc;
869  }
870  if (retc < 0 || iocnt < 1) break;
871  }
872 
873 // All done
874 //
875  if (retc >= 0) return bytes;
876  Log.Emsg("Link", errno, "send to", ID);
877  return -1;
878 }
879 
880 /******************************************************************************/
881 /* s e t I D */
882 /******************************************************************************/
883 
884 void XrdLinkXeq::setID(const char *userid, int procid)
885 {
886  char buff[sizeof(Uname)], *bp, *sp;
887  int ulen;
888 
889  snprintf(buff, sizeof(buff), "%s.%d:%d", userid, procid, PollInfo.FD);
890  ulen = strlen(buff);
891  sp = buff + ulen - 1;
892  bp = &Uname[sizeof(Uname)-1];
893  if (ulen > (int)sizeof(Uname)) ulen = sizeof(Uname);
894  *bp = '@'; bp--;
895  while(ulen--) {*bp = *sp; bp--; sp--;}
896  ID = bp+1;
897  Comment = (const char *)ID;
898 
899 // Update the ID in the TLS socket if enabled
900 //
901  if (isTLS) tlsIO.SetTraceID(ID);
902 }
903 
904 /******************************************************************************/
905 /* s e t N B */
906 /******************************************************************************/
907 
909 {
910 // We don't support non-blocking output except for Linux at the moment
911 //
912 #if !defined(__linux__)
913  return false;
914 #else
915 // Trace this request
916 //
917  TRACEI(DEBUG,"enabling non-blocking output");
918 
919 // If we don't already have a sendQ object get one. This is a one-time call
920 // so to optimize checking if this object exists we also get the opMutex.'
921 //
923  if (!sendQ)
924  {wrMutex.Lock();
925  sendQ = new XrdSendQ(*this, wrMutex);
926  wrMutex.UnLock();
927  }
929  return true;
930 #endif
931 }
932 
933 /******************************************************************************/
934 /* s e t P r o t o c o l */
935 /******************************************************************************/
936 
938 {
939 
940 // Set new protocol.
941 //
943  XrdProtocol *op = Protocol;
944  if (push) ProtoAlt = Protocol;
945  Protocol = pp;
947  return op;
948 }
949 
950 /******************************************************************************/
951 /* s e t P r o t N a m e */
952 /******************************************************************************/
953 
954 void XrdLinkXeq::setProtName(const char *name)
955 {
956 
957 // Set the protocol name.
958 //
960  Addr.SetDialect(name);
962 }
963 
964 /******************************************************************************/
965 /* s e t T L S */
966 /******************************************************************************/
967 
968 bool XrdLinkXeq::setTLS(bool enable, XrdTlsContext *ctx)
969 { //???
970 // static const XrdTlsConnection::RW_Mode rwMode=XrdTlsConnection::TLS_RNB_WBL;
973  const char *eNote;
974  XrdTls::RC rc;
975 
976 // If we are already in a compatible mode, we are done
977 //
978 
979  if (isTLS == enable) return true;
980 
981 // If this is a shutdown, then do it now.
982 //
983  if (!enable)
984  {tlsIO.Shutdown();
985  isTLS = enable;
986  Addr.SetTLS(enable);
987  return true;
988  }
989 // We want to initialize TLS, do so now.
990 //
991  if (!ctx) ctx = tlsCtx;
992  eNote = tlsIO.Init(*ctx, PollInfo.FD, rwMode, hsMode, false, false, ID);
993 
994 // Check for errors
995 //
996  if (eNote)
997  {char buff[1024];
998  snprintf(buff, sizeof(buff), "Unable to enable tls for %s;", ID);
999  Log.Emsg("LinkXeq", buff, eNote);
1000  return false;
1001  }
1002 
1003 // Now we need to accept this TLS connection
1004 //
1005  std::string eMsg;
1006  rc = tlsIO.Accept(&eMsg);
1007 
1008 // Diagnose return state
1009 //
1010  if (rc != XrdTls::TLS_AOK) Log.Emsg("LinkXeq", eMsg.c_str());
1011  else {isTLS = enable;
1012  Addr.SetTLS(enable);
1013  Log.Emsg("LinkXeq", ID, "connection upgraded to", verTLS());
1014  }
1015  return rc == XrdTls::TLS_AOK;
1016 }
1017 
1018 /******************************************************************************/
1019 /* S F E r r o r */
1020 /******************************************************************************/
1021 
1023 {
1024  Log.Emsg("TLS", rc, "send file to", ID);
1025  return -1;
1026 }
1027 
1028 /******************************************************************************/
1029 /* S h u t d o w n */
1030 /******************************************************************************/
1031 
1032 void XrdLinkXeq::Shutdown(bool getLock)
1033 {
1034  int temp;
1035 
1036 // Trace the entry
1037 //
1038  TRACEI(DEBUG, (getLock ? "Async" : "Sync") <<" link shutdown in progress");
1039 
1040 // Get the lock if we need too (external entry via another thread)
1041 //
1042  if (getLock) LinkInfo.opMutex.Lock();
1043 
1044 // If there is something to do, do it now
1045 //
1046  temp = Instance; Instance = 0;
1047  if (!KeepFD)
1048  {shutdown(PollInfo.FD, SHUT_RDWR);
1049  if (dup2(devNull, PollInfo.FD) < 0)
1050  {Instance = temp;
1051  Log.Emsg("Link", errno, "shutdown FD for", ID);
1052  }
1053  }
1054 
1055 // All done
1056 //
1057  if (getLock) LinkInfo.opMutex.UnLock();
1058 }
1059 
1060 /******************************************************************************/
1061 /* S t a t s */
1062 /******************************************************************************/
1063 
1064 int XrdLinkXeq::Stats(char *buff, int blen, bool do_sync)
1065 {
1066  static const char statfmt[] = "<stats id=\"link\"><num>%d</num>"
1067  "<maxn>%d</maxn><tot>%lld</tot><in>%lld</in><out>%lld</out>"
1068  "<ctime>%lld</ctime><tmo>%d</tmo><stall>%d</stall>"
1069  "<sfps>%d</sfps></stats>";
1070  int i;
1071 
1072 // Check if actual length wanted
1073 //
1074  if (!buff) return sizeof(statfmt)+17*6;
1075 
1076 // We must synchronize the statistical counters
1077 //
1078  if (do_sync) XrdLinkCtl::SyncAll();
1079 
1080 // Obtain lock on the stats area and format it
1081 //
1083  i = snprintf(buff, blen, statfmt, AtomicGet(LinkCount),
1093  return i;
1094 }
1095 
1096 /******************************************************************************/
1097 /* s y n c S t a t s */
1098 /******************************************************************************/
1099 
1100 void XrdLinkXeq::syncStats(int *ctime)
1101 {
1102  long long tmpLL;
1103  int tmpI4;
1104 
1105 // If this is dynamic, get the opMutex lock
1106 //
1107  if (!ctime) LinkInfo.opMutex.Lock();
1108 
1109 // Either the caller has the opMutex or this is called out of close. In either
1110 // case, we need to get the read and write mutexes; each followed by the stats
1111 // mutex. This order is important because we should not hold the stats mutex
1112 // for very long and the r/w mutexes may take a long time to acquire. If we
1113 // must maintain the link count we need to actually acquire the stats mutex as
1114 // we will be doing compound operations. Atomics are still used to keep other
1115 // threads from seeing partial results.
1116 //
1117  AtomicBeg(rdMutex);
1118 
1119  if (ctime)
1120  {*ctime = time(0) - LinkInfo.conTime;
1121  AtomicAdd(LinkConTime, *ctime);
1122  statsMutex.Lock();
1123  if (LinkCount > 0) AtomicDec(LinkCount);
1124  statsMutex.UnLock();
1125  }
1126 
1128 
1129  tmpLL = AtomicFAZ(BytesIn);
1130  AtomicAdd(LinkBytesIn, tmpLL); AtomicAdd(BytesInTot, tmpLL);
1131  tmpI4 = AtomicFAZ(tardyCnt);
1132  AtomicAdd(LinkTimeOuts, tmpI4); AtomicAdd(tardyCntTot, tmpI4);
1133  tmpI4 = AtomicFAZ(stallCnt);
1134  AtomicAdd(LinkStalls, tmpI4); AtomicAdd(stallCntTot, tmpI4);
1136 
1138  tmpLL = AtomicFAZ(BytesOut);
1139  AtomicAdd(LinkBytesOut, tmpLL); AtomicAdd(BytesOutTot, tmpLL);
1140  tmpI4 = AtomicFAZ(SfIntr);
1141  AtomicAdd(LinkSfIntr, tmpI4);
1143 
1144 // Make sure the protocol updates it's statistics as well
1145 //
1146  if (Protocol) Protocol->Stats(0, 0, 1);
1147 
1148 // All done
1149 //
1150  if (!ctime) LinkInfo.opMutex.UnLock();
1151 }
1152 
1153 /******************************************************************************/
1154 /* Protected: T L S _ E r r o r */
1155 /******************************************************************************/
1156 
1157 int XrdLinkXeq::TLS_Error(const char *act, XrdTls::RC rc)
1158 {
1159  std::string reason = XrdTls::RC2Text(rc);
1160  char msg[512];
1161 
1162  snprintf(msg, sizeof(msg), "Unable to %s %s;", act, ID);
1163  Log.Emsg("TLS", msg, reason.c_str());
1164  return -1;
1165 }
1166 
1167 /******************************************************************************/
1168 /* T L S _ P e e k */
1169 /******************************************************************************/
1170 
1171 int XrdLinkXeq::TLS_Peek(char *Buff, int Blen, int timeout)
1172 {
1173  XrdSysMutexHelper theMutex;
1174  XrdTls::RC retc;
1175  int rc, rlen;
1176 
1177 // Lock the read mutex if we need to, the helper will unlock it upon exit
1178 //
1179  if (LockReads) theMutex.Lock(&rdMutex);
1180 
1181 // Wait until we can actually read something
1182 //
1183  isIdle = 0;
1184  if (timeout)
1185  {rc = Wait4Data(timeout);
1186  if (rc < 1) return rc;
1187  }
1188 
1189 // Do the peek and if sucessful, the number of bytes available.
1190 //
1191  retc = tlsIO.Peek(Buff, Blen, rlen);
1192  if (retc == XrdTls::TLS_AOK) return rlen;
1193 
1194 // Dianose the TLS error and return failure
1195 //
1196  return TLS_Error("peek on", retc);
1197 }
1198 
1199 /******************************************************************************/
1200 /* T L S _ R e c v */
1201 /******************************************************************************/
1202 
1203 int XrdLinkXeq::TLS_Recv(char *Buff, int Blen)
1204 {
1205  XrdSysMutexHelper theMutex;
1206  XrdTls::RC retc;
1207  int rlen;
1208 
1209 // Lock the read mutex if we need to, the helper will unlock it upon exit
1210 //
1211  if (LockReads) theMutex.Lock(&rdMutex);
1212 
1213 // Note that we will read only as much as is queued. Use Recv() with a
1214 // timeout to receive as much data as possible.
1215 //
1216  isIdle = 0;
1217  retc = tlsIO.Read(Buff, Blen, rlen);
1218  if (retc != XrdTls::TLS_AOK) return TLS_Error("receive from", retc);
1219  if (rlen > 0) AtomicAdd(BytesIn, rlen);
1220  return rlen;
1221 }
1222 
1223 /******************************************************************************/
1224 
1225 int XrdLinkXeq::TLS_Recv(char *Buff, int Blen, int timeout, bool havelock)
1226 {
1227  XrdSysMutexHelper theMutex;
1228  XrdTls::RC retc;
1229  int pend, rlen, totlen = 0;
1230 
1231 // Lock the read mutex if we need to, the helper will unlock it upon exit
1232 //
1233  if (LockReads && !havelock) theMutex.Lock(&rdMutex);
1234 
1235 // Wait up to timeout milliseconds for data to arrive
1236 //
1237  isIdle = 0;
1238  while(Blen > 0)
1239  {pend = tlsIO.Pending(true);
1240  if (!pend) pend = Wait4Data(timeout);
1241  if (pend < 1)
1242  {if (pend < 0) return -1;
1243  tardyCnt++;
1244  if (totlen)
1245  {if ((++stallCnt & 0xff) == 1) TRACEI(DEBUG,"read timed out");
1246  AtomicAdd(BytesIn, totlen);
1247  }
1248  return totlen;
1249  }
1250 
1251  // Read as much data as you can. Note that we will force an error
1252  // if we get a zero-length read after poll said it was OK. However,
1253  // if we never read anything, then we simply return -ENOMSG to avoid
1254  // generating a "read link error" as clearly there was a hangup.
1255  //
1256  retc = tlsIO.Read(Buff, Blen, rlen);
1257  if (retc != XrdTls::TLS_AOK)
1258  {if (!totlen) return -ENOMSG;
1259  AtomicAdd(BytesIn, totlen);
1260  return TLS_Error("receive from", retc);
1261  }
1262  if (rlen <= 0) break;
1263  totlen += rlen; Blen -= rlen; Buff += rlen;
1264  }
1265 
1266  AtomicAdd(BytesIn, totlen);
1267  return totlen;
1268 }
1269 
1270 /******************************************************************************/
1271 
1272 int XrdLinkXeq::TLS_Recv(const struct iovec *iov, int iocnt, int timeout)
1273 {
1274  XrdSysMutexHelper theMutex;
1275  char *Buff;
1276  int Blen, rlen, totlen = 0;
1277 
1278 // Lock the read mutex if we need to, the helper will unlock it upon exit
1279 //
1280  if (LockReads) theMutex.Lock(&rdMutex);
1281 
1282 // Individually process each element until we can't read any more
1283 //
1284  isIdle = 0;
1285  for (int i = 0; i < iocnt; i++)
1286  {Buff = (char *)iov[i].iov_base;
1287  Blen = iov[i].iov_len;
1288  rlen = TLS_Recv(Buff, Blen, timeout, true);
1289  if (rlen <= 0) break;
1290  totlen += rlen;
1291  if (rlen < Blen) break;
1292  }
1293 
1294  if (totlen) {AtomicAdd(BytesIn, totlen);}
1295  return totlen;
1296 }
1297 
1298 /******************************************************************************/
1299 /* T L S _ R e c v A l l */
1300 /******************************************************************************/
1301 
1302 int XrdLinkXeq::TLS_RecvAll(char *Buff, int Blen, int timeout)
1303 {
1304  int retc;
1305 
1306 // Check if timeout specified. Notice that the timeout is the max we will
1307 // wait for some data. We will wait forever for all the data. Yeah, it's weird.
1308 //
1309  if (timeout >= 0)
1310  {retc = tlsIO.Pending(true);
1311  if (!retc) retc = Wait4Data(timeout);
1312  if (retc < 1) return (retc ? -1 : -ETIMEDOUT);
1313  }
1314 
1315 // Note that we will block until we receive all the bytes.
1316 //
1317  return TLS_Recv(Buff, Blen, -1);
1318 }
1319 
1320 /******************************************************************************/
1321 /* T L S _ S e n d */
1322 /******************************************************************************/
1323 
1324 int XrdLinkXeq::TLS_Send(const char *Buff, int Blen)
1325 {
1327  ssize_t bytesleft = Blen;
1328  XrdTls::RC retc;
1329  int byteswritten;
1330 
1331 // Prepare to send
1332 //
1333  isIdle = 0;
1334  AtomicAdd(BytesOut, Blen);
1335 
1336 // Do non-blocking writes if we are setup to do so.
1337 //
1338  if (sendQ) return sendQ->Send(Buff, Blen);
1339 
1340 // Write the data out
1341 //
1342  while(bytesleft)
1343  {retc = tlsIO.Write(Buff, bytesleft, byteswritten);
1344  if (retc != XrdTls::TLS_AOK) return TLS_Error("send to", retc);
1345  bytesleft -= byteswritten; Buff += byteswritten;
1346  }
1347 
1348 // All done
1349 //
1350  return Blen;
1351 }
1352 
1353 /******************************************************************************/
1354 
1355 int XrdLinkXeq::TLS_Send(const struct iovec *iov, int iocnt, int bytes)
1356 {
1358  XrdTls::RC retc;
1359  int byteswritten;
1360 
1361 // Get a lock and assume we will be successful (statistically we are). Note
1362 // that the calling interface gauranteed bytes are not zero.
1363 //
1364  isIdle = 0;
1365  AtomicAdd(BytesOut, bytes);
1366 
1367 // Do non-blocking writes if we are setup to do so.
1368 //
1369  if (sendQ) return sendQ->Send(iov, iocnt, bytes);
1370 
1371 // Write the data out.
1372 //
1373  for (int i = 0; i < iocnt; i++)
1374  {ssize_t bytesleft = iov[i].iov_len;
1375  char *Buff = (char *)iov[i].iov_base;
1376  while(bytesleft)
1377  {retc = tlsIO.Write(Buff, bytesleft, byteswritten);
1378  if (retc != XrdTls::TLS_AOK) return TLS_Error("send to", retc);
1379  bytesleft -= byteswritten; Buff += byteswritten;
1380  }
1381  }
1382 
1383 // All done
1384 //
1385  return bytes;
1386 }
1387 
1388 /******************************************************************************/
1389 
1390 int XrdLinkXeq::TLS_Send(const sfVec *sfP, int sfN)
1391 {
1393  int bytes, buffsz, fileFD, retc;
1394  off_t offset;
1395  ssize_t totamt = 0;
1396  char myBuff[65536];
1397 
1398 // Convert the sendfile to a regular send. The conversion is not particularly
1399 // fast and caller are advised to avoid using sendfile on TLS connections.
1400 //
1401  isIdle = 0;
1402  for (int i = 0; i < sfN; sfP++, i++)
1403  {if (!(bytes = sfP->sendsz)) continue;
1404  totamt += bytes;
1405  if (sfP->fdnum < 0)
1406  {if (!TLS_Write(sfP->buffer, bytes)) return -1;
1407  continue;
1408  }
1409  offset = sfP->offset;
1410  fileFD = sfP->fdnum;
1411  buffsz = (bytes < (int)sizeof(myBuff) ? bytes : sizeof(myBuff));
1412  do {do {retc = pread(fileFD, myBuff, buffsz, offset);}
1413  while(retc < 0 && errno == EINTR);
1414  if (retc < 0) return SFError(errno);
1415  if (!retc) break;
1416  if (!TLS_Write(myBuff, buffsz)) return -1;
1417  offset += buffsz; bytes -= buffsz; totamt += retc;
1418  } while(bytes > 0);
1419  }
1420 
1421 // We are done
1422 //
1423  AtomicAdd(BytesOut, totamt);
1424  return totamt;
1425 }
1426 
1427 /******************************************************************************/
1428 /* Protected: T L S _ W r i t e */
1429 /******************************************************************************/
1430 
1431 bool XrdLinkXeq::TLS_Write(const char *Buff, int Blen)
1432 {
1433  XrdTls::RC retc;
1434  int byteswritten;
1435 
1436 // Write the data out
1437 //
1438  while(Blen)
1439  {retc = tlsIO.Write(Buff, Blen, byteswritten);
1440  if (retc != XrdTls::TLS_AOK)
1441  {TLS_Error("write to", retc);
1442  return false;
1443  }
1444  Blen -= byteswritten; Buff += byteswritten;
1445  }
1446 
1447 // All done
1448 //
1449  return true;
1450 }
1451 
1452 /******************************************************************************/
1453 /* v e r T L S */
1454 /******************************************************************************/
1455 
1456 const char *XrdLinkXeq::verTLS()
1457 {
1458  return tlsIO.Version();
1459 }
1460 
1461 /******************************************************************************/
1462 /* R e g i s t e r C l o s e R e q u e s t C b */
1463 /******************************************************************************/
1464 
1466  void* cbarg)
1467 {
1468  if (pp != Protocol) return false;
1469 
1470  CloseRequestCb = cb;
1471  CloseRequestCbArg = cbarg;
1472  return true;
1473 }
#define DEBUG(x)
Definition: XrdBwmTrace.hh:54
ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
ssize_t write(int fildes, const void *buf, size_t nbyte)
ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
ssize_t read(int fildes, void *buf, size_t nbyte)
#define close(a)
Definition: XrdPosix.hh:48
#define pread(a, b, c, d)
Definition: XrdPosix.hh:80
#define eMsg(x)
#define AtomicFAZ(x)
#define AtomicBeg(Mtx)
#define AtomicDec(x)
#define AtomicGet(x)
#define AtomicEnd(Mtx)
#define AtomicAdd(x, y)
size_t strlcpy(char *dst, const char *src, size_t sz)
#define TRACEI(act, x)
Definition: XrdTrace.hh:66
const char * Comment
Definition: XrdJob.hh:47
static void SyncAll()
Synchronize statustics for ll links.
Definition: XrdLinkCtl.cc:374
static void Unhook(int fd)
Unhook a link from the active table of links.
Definition: XrdLinkCtl.cc:392
time_t conTime
Definition: XrdLinkInfo.hh:44
void Reset()
Definition: XrdLinkInfo.hh:52
char * Etext
Definition: XrdLinkInfo.hh:45
XrdSysRecMutex opMutex
Definition: XrdLinkInfo.hh:46
XrdSysCondVar * KillcvP
Definition: XrdLinkInfo.hh:42
static const char * TraceID
Definition: XrdLinkXeq.hh:160
bool(* CloseRequestCb)(void *)
Definition: XrdLinkXeq.hh:188
int TLS_Send(const char *Buff, int Blen)
Definition: XrdLinkXeq.cc:1324
long long BytesOut
Definition: XrdLinkXeq.hh:175
int TLS_Error(const char *act, XrdTls::RC rc)
Definition: XrdLinkXeq.cc:1157
int TLS_Peek(char *Buff, int Blen, int timeout)
Definition: XrdLinkXeq.cc:1171
int stallCntTot
Definition: XrdLinkXeq.hh:178
int Client(char *buff, int blen)
Definition: XrdLinkXeq.cc:153
char Uname[24]
Definition: XrdLinkXeq.hh:205
XrdTlsPeerCerts * getPeerCerts()
Definition: XrdLinkXeq.cc:331
static int LinkCountMax
Definition: XrdLinkXeq.hh:169
XrdLinkInfo LinkInfo
Definition: XrdLinkXeq.hh:147
XrdProtocol * ProtoAlt
Definition: XrdLinkXeq.hh:187
int Close(bool defer=false)
Definition: XrdLinkXeq.cc:173
XrdNetAddr Addr
Definition: XrdLinkXeq.hh:197
int TLS_Recv(char *Buff, int Blen)
Definition: XrdLinkXeq.cc:1203
int sendData(const char *Buff, int Blen)
Definition: XrdLinkXeq.cc:820
long long BytesInTot
Definition: XrdLinkXeq.hh:174
bool TLS_Write(const char *Buff, int Blen)
Definition: XrdLinkXeq.cc:1431
int SendIOV(const struct iovec *iov, int iocnt, int bytes)
Definition: XrdLinkXeq.cc:843
XrdProtocol * setProtocol(XrdProtocol *pp, bool push)
Definition: XrdLinkXeq.cc:937
static long long LinkCountTot
Definition: XrdLinkXeq.hh:167
long long BytesOutTot
Definition: XrdLinkXeq.hh:176
void Shutdown(bool getLock)
Definition: XrdLinkXeq.cc:1032
int Peek(char *buff, int blen, int timeout=-1)
Definition: XrdLinkXeq.cc:340
static int LinkCount
Definition: XrdLinkXeq.hh:168
void Reset()
Definition: XrdLinkXeq.cc:113
int Backlog()
Definition: XrdLinkXeq.cc:140
XrdSysMutex wrMutex
Definition: XrdLinkXeq.hh:199
static int Stats(char *buff, int blen, bool do_sync=false)
Definition: XrdLinkXeq.cc:1064
XrdSendQ * sendQ
Definition: XrdLinkXeq.hh:200
XrdPollInfo PollInfo
Definition: XrdLinkXeq.hh:148
void setID(const char *userid, int procid)
Definition: XrdLinkXeq.cc:884
bool LockReads
Definition: XrdLinkXeq.hh:202
int Recv(char *buff, int blen)
Definition: XrdLinkXeq.cc:383
static long long LinkBytesIn
Definition: XrdLinkXeq.hh:164
void * CloseRequestCbArg
Definition: XrdLinkXeq.hh:189
int TLS_RecvAll(char *Buff, int Blen, int timeout)
Definition: XrdLinkXeq.cc:1302
int SFError(int rc)
Definition: XrdLinkXeq.cc:1022
long long BytesIn
Definition: XrdLinkXeq.hh:173
int tardyCntTot
Definition: XrdLinkXeq.hh:180
int Send(const char *buff, int blen)
Definition: XrdLinkXeq.cc:598
XrdSysMutex rdMutex
Definition: XrdLinkXeq.hh:198
const char * verTLS()
Definition: XrdLinkXeq.cc:1456
bool setNB()
Definition: XrdLinkXeq.cc:908
int RecvIOV(const struct iovec *iov, int iocnt)
Definition: XrdLinkXeq.cc:561
char Lname[256]
Definition: XrdLinkXeq.hh:206
static long long LinkConTime
Definition: XrdLinkXeq.hh:166
static int LinkSfIntr
Definition: XrdLinkXeq.hh:172
XrdTlsSocket tlsIO
Definition: XrdLinkXeq.hh:193
void DoIt()
Definition: XrdLinkXeq.cc:293
int RecvAll(char *buff, int blen, int timeout=-1)
Definition: XrdLinkXeq.cc:519
XrdProtocol * Protocol
Definition: XrdLinkXeq.hh:186
bool Register(const char *hName)
Definition: XrdLinkXeq.cc:583
static XrdSysMutex statsMutex
Definition: XrdLinkXeq.hh:182
void setProtName(const char *name)
Definition: XrdLinkXeq.cc:954
static int LinkStalls
Definition: XrdLinkXeq.hh:171
static long long LinkBytesOut
Definition: XrdLinkXeq.hh:165
void syncStats(int *ctime=0)
Definition: XrdLinkXeq.cc:1100
bool setTLS(bool enable, XrdTlsContext *ctx=0)
Definition: XrdLinkXeq.cc:968
static int LinkTimeOuts
Definition: XrdLinkXeq.hh:170
bool RegisterCloseRequestCb(XrdProtocol *pp, bool(*cb)(void *), void *cbarg)
Definition: XrdLinkXeq.cc:1465
void SetDialect(const char *dP)
Definition: XrdNetAddr.hh:205
void SetTLS(bool val)
Definition: XrdNetAddr.cc:590
void Zorch()
Definition: XrdPollInfo.hh:49
XrdPoll * Poller
Definition: XrdPollInfo.hh:43
virtual int Enable(XrdPollInfo &pInfo)=0
static char * Poll2Text(short events)
Definition: XrdPoll.cc:272
static void Detach(XrdPollInfo &pInfo)
Definition: XrdPoll.cc:177
virtual void Recycle(XrdLink *lp=0, int consec=0, const char *reason=0)=0
virtual int Stats(char *buff, int blen, int do_sync=0)=0
virtual int Process(XrdLink *lp)=0
void Terminate(XrdLink *lP=0)
Definition: XrdSendQ.cc:396
int Send(const char *buff, int blen)
Definition: XrdSendQ.cc:230
unsigned int Backlog()
Definition: XrdSendQ.hh:46
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
void Lock(XrdSysMutex *Mutex)
int fd
Socket file descriptor.
Definition: XrdTcpMonPin.hh:61
long long bytesOut
Bytes written to the socket.
Definition: XrdTcpMonPin.hh:64
int consec
Seconds connected.
Definition: XrdTcpMonPin.hh:62
virtual void Monitor(XrdNetAddrInfo &netInfo, LinkInfo &lnkInfo, int liLen)=0
long long bytesIn
Bytes read from the socket.
Definition: XrdTcpMonPin.hh:63
const char * tident
Pointer to the client's trace identifier.
Definition: XrdTcpMonPin.hh:60
@ TLS_HS_BLOCK
Always block during handshake.
Definition: XrdTlsSocket.hh:53
XrdTls::RC Accept(std::string *eMsg=0)
void Shutdown(SDType=sdImmed)
@ TLS_RBL_WBL
blocking read blocking write
Definition: XrdTlsSocket.hh:48
XrdTls::RC Write(const char *buffer, size_t size, int &bytesOut)
const char * Version()
XrdTls::RC Read(char *buffer, size_t size, int &bytesRead)
Read from the TLS connection. If necessary, a handshake will be done.
const char * Init(XrdTlsContext &ctx, int sfd, RW_Mode rwm, HS_Mode hsm, bool isClient, bool serial=true, const char *tid="")
void SetTraceID(const char *tid)
int Pending(bool any=true)
XrdTls::RC Peek(char *buffer, size_t size, int &bytesPeek)
XrdTlsPeerCerts * getCerts(bool ver=true)
static std::string RC2Text(XrdTls::RC rc, bool dbg=false)
Definition: XrdTls.cc:127
@ TLS_AOK
All went well, will always be zero.
Definition: XrdTls.hh:40
XrdTlsContext * tlsCtx
Definition: XrdGlobals.cc:52
XrdTcpMonPin * TcpMonPin
Definition: XrdLinkXeq.cc:80
const int maxIOV
Definition: XrdLinkXeq.cc:82
XrdSysError Log
Definition: XrdConfig.cc:113
XrdScheduler Sched
Definition: XrdLinkCtl.cc:54
int devNull
Definition: XrdGlobals.cc:55
int getIovMax()
int fdnum
File descriptor for data.
Definition: XrdOucSFVec.hh:47
int sendsz
Length of data at offset.
Definition: XrdOucSFVec.hh:46