ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
pq2ping.cxx
Go to the documentation of this file.
1 // @(#)root/proof:$Id$
2 // Author: G. Ganis, Mar 2010
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 // ************************************************************************* //
13 // * * //
14 // * p q 2 p i n g * //
15 // * * //
16 // * This file implements the daemon checking functions used by pq2main * //
17 // * * //
18 // ************************************************************************* //
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include "Bytes.h"
24 #include "TSocket.h"
25 #include "TString.h"
26 #include "TSystem.h"
27 #include "TUrl.h"
28 
29 #include "redirguard.h"
30 #include "pq2ping.h"
31 
32 // Auxilliary structures for Xrootd/Xproofd pinging ...
33 // The client request
34 typedef struct {
35  int first;
36  int second;
37  int third;
38  int fourth;
39  int fifth;
40 } clnt_HS_t;
41 // The body received after the first handshake's header
42 typedef struct {
43  int msglen;
44  int protover;
45  int msgval;
46 } srv_HS_t;
47 
48 // Global variables used by other PQ2 components
51 
52 // Global variables defined by other PQ2 components
53 extern Int_t gverbose;
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// Check if something is running at gUrl
57 /// Return
58 /// 0 if OK and data server
59 /// 1 if OK and PROOF server
60 /// -1 if nothing valid is available
61 
62 Int_t checkUrl(const char *url, const char *flog, bool def_proof)
63 {
64  gIsProof = kFALSE;
65  gUrl.SetUrl(url);
66  TString protocol(gUrl.GetProtocol());
67  if (protocol == "root" || protocol == "xroot") {
68  // Check Xrootd
69  if (pingXrootdAt() != 0) {
70  Printf("checkUrl: specified URL does not identifies a running (x)rootd server: %s", url);
71  return -1;
72  }
73  } else if (protocol == "proof") {
74  // Check PROOF
75  if (pingXproofdAt() != 0) {
76  Printf("checkUrl: specified URL does not identifies a running PROOF master: %s", url);
77  return -1;
78  }
79  gIsProof = kTRUE;
80  // Always force a new session (do not attach)
81  gUrl.SetOptions("N");
82  } else {
83  Int_t rc = -1;
84  if (def_proof) {
85  // Check first PROOF
86  { redirguard rog(flog, "a", 0);
87  if ((rc = pingXproofdAt()) == 0)
88  gIsProof = kTRUE;
89  }
90  if (rc != 0) {
91  // Check also a generic data server
92  if (pingServerAt() != 0) {
93  Printf("checkUrl: specified URL does not identifies a valid PROOF or data server: %s", url);
94  return -1;
95  }
96  }
97  } else {
98  // Check first generic data server
99  { redirguard rog(flog, "a", 0);
100  rc = pingServerAt();
101  }
102  if (rc != 0) {
103  // Check also PROOF
104  if (pingXproofdAt() != 0) {
105  Printf("checkUrl: specified URL does not identifies a valid data or PROOF server: %s", url);
106  return -1;
107  }
108  gIsProof = kTRUE;
109  }
110  }
111  }
112  if (gverbose > 0) Printf("checkUrl: %s service", (gIsProof ? "PROOF" : "Data"));
113 
114  // Done (if we are here the test was successful)
115  return ((gIsProof) ? 1 : 0);
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Check if a XrdXrootd service is running on 'port' at 'host'
120 /// Return
121 /// 0 if OK
122 /// -1 if nothing is listening on the port (connection cannot be open)
123 /// 1 if something is listening but not XROOTD
124 
126 {
127  Int_t port = gUrl.GetPort();
128  const char *host = gUrl.GetHost();
129 
130  // Open the connection
131  TSocket s(host, port);
132  if (!(s.IsValid())) {
133  if (gDebug > 0)
134  Printf("pingXrootdAt: could not open connection to %s:%d", host, port);
135  return -1;
136  }
137  // Send the first bytes
138  clnt_HS_t initHS;
139  memset(&initHS, 0, sizeof(initHS));
140  initHS.fourth = host2net((int)4);
141  initHS.fifth = host2net((int)2012);
142  int len = sizeof(initHS);
143  int writeCount = s.SendRaw(&initHS, len);
144  if (writeCount != len) {
145  if (gDebug > 0)
146  Printf("pingXrootdAt: 1st: wrong number of bytes sent: %d (expected: %d)",
147  writeCount, len);
148  return 1;
149  }
150  // Read first server response
151  int type;
152  len = sizeof(type);
153  int readCount = s.RecvRaw(&type, len); // 4(2+2) bytes
154  if (readCount != len) {
155  if (gDebug > 0)
156  Printf("pingXrootdAt: 1st: wrong number of bytes read: %d (expected: %d)",
157  readCount, len);
158  return 1;
159  }
160  // to host byte order
161  type = net2host(type);
162  // Check if the server is the eXtended proofd
163  if (type == 0) {
164  srv_HS_t xbody;
165  len = sizeof(xbody);
166  readCount = s.RecvRaw(&xbody, len); // 12(4+4+4) bytes
167  if (readCount != len) {
168  if (gDebug > 0)
169  Printf("pingXrootdAt: 2nd: wrong number of bytes read: %d (expected: %d)",
170  readCount, len);
171  return 1;
172  }
173 
174  } else if (type == 8) {
175  // Standard proofd
176  if (gDebug > 0)
177  Printf("pingXrootdAt: server is ROOTD");
178  return 1;
179  } else {
180  // We don't know the server type
181  if (gDebug > 0)
182  Printf("pingXrootdAt: unknown server type: %d", type);
183  return 1;
184  }
185  // Done
186  return 0;
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Check if a XrdProofd service is running on 'port' at 'host'
191 /// Return
192 /// 0 if OK
193 /// -1 if nothing is listening on the port (connection cannot be open)
194 /// 1 if something is listening but not XPROOFD
195 
197 {
198  Int_t port = gUrl.GetPort();
199  const char *host = gUrl.GetHost();
200 
201  // Open the connection
202  TSocket s(host, port);
203  if (!(s.IsValid())) {
204  if (gDebug > 0)
205  Printf("pingXproofdAt: could not open connection to %s:%d", host, port);
206  return -1;
207  }
208  // Send the first bytes
209  int writeCount = -1;
210  clnt_HS_t initHS;
211  memset(&initHS, 0, sizeof(initHS));
212  initHS.third = (int)host2net((int)1);
213  int len = sizeof(initHS);
214  writeCount = s.SendRaw(&initHS, len);
215  if (writeCount != len) {
216  if (gDebug > 0)
217  Printf("pingXproofdAt: 1st: wrong number of bytes sent: %d (expected: %d)",
218  writeCount, len);
219  return 1;
220  }
221  // These 8 bytes are need by 'proofd' and discarded by XPD
222  int dum[2];
223  dum[0] = (int)host2net((int)4);
224  dum[1] = (int)host2net((int)2012);
225  writeCount = s.SendRaw(&dum[0], sizeof(dum));
226  if (writeCount != sizeof(dum)) {
227  if (gDebug > 0)
228  Printf("pingXproofdAt: 2nd: wrong number of bytes sent: %d (expected: %d)",
229  writeCount, (int) sizeof(dum));
230  return 1;
231  }
232  // Read first server response
233  int type;
234  len = sizeof(type);
235  int readCount = s.RecvRaw(&type, len); // 4(2+2) bytes
236  if (readCount != len) {
237  if (gDebug > 0)
238  Printf("pingXproofdAt: 1st: wrong number of bytes read: %d (expected: %d)",
239  readCount, len);
240  return 1;
241  }
242  // to host byte order
243  type = net2host(type);
244  // Check if the server is the eXtended proofd
245  if (type == 0) {
246  srv_HS_t xbody;
247  len = sizeof(xbody);
248  readCount = s.RecvRaw(&xbody, len); // 12(4+4+4) bytes
249  if (readCount != len) {
250  if (gDebug > 0)
251  Printf("pingXproofdAt: 2nd: wrong number of bytes read: %d (expected: %d)",
252  readCount, len);
253  return 1;
254  }
255  xbody.protover = net2host(xbody.protover);
256  xbody.msgval = net2host(xbody.msglen);
257  xbody.msglen = net2host(xbody.msgval);
258 
259  } else if (type == 8) {
260  // Standard proofd
261  if (gDebug > 0)
262  Printf("pingXproofdAt: server is PROOFD");
263  return 1;
264  } else {
265  // We don't know the server type
266  if (gDebug > 0)
267  Printf("pingXproofdAt: unknown server type: %d", type);
268  return 1;
269  }
270  // Done
271  return 0;
272 }
273 
274 ////////////////////////////////////////////////////////////////////////////////
275 /// Check if service is running at 'url'
276 /// Return
277 /// 0 if OK
278 /// -1 if nothing is listening at the URL
279 /// 1 if not a directory
280 
282 {
283  Int_t rc = -1;
284  FileStat_t st;
285  if (gSystem->GetPathInfo(gUrl.GetUrl(), st) == 0) {
286  rc = 1;
287  if (R_ISDIR(st.fMode)) rc = 0;
288  }
289 
290  // Done
291  return rc;
292 }
const char * GetHost() const
Definition: TUrl.h:76
virtual Bool_t IsValid() const
Definition: TSocket.h:162
This class represents a WWW compatible URL.
Definition: TUrl.h:41
int GetPathInfo(const char *path, Long_t *id, Long_t *size, Long_t *flags, Long_t *modtime)
Get info about a file: id, size, flags, modification time.
Definition: TSystem.cxx:1311
const char * GetProtocol() const
Definition: TUrl.h:73
void SetUrl(const char *url, Bool_t defaultIsFile=kFALSE)
Parse url character string and split in its different subcomponents.
Definition: TUrl.cxx:108
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Int_t fMode
Definition: TSystem.h:138
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
Definition: TSocket.cxx:620
UShort_t net2host(UShort_t x)
Definition: Bytes.h:579
Int_t gverbose
Definition: pq2main.cxx:40
TString flog
Definition: pq2main.cxx:37
Int_t GetPort() const
Definition: TUrl.h:87
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
bool first
Definition: line3Dfit.C:48
Int_t pingServerAt()
Check if service is running at 'url' Return 0 if OK -1 if nothing is listening at the URL 1 if not a ...
Definition: pq2ping.cxx:281
#define Printf
Definition: TGeoToOCC.h:18
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
Definition: TUrl.cxx:385
int type
Definition: TGX11.cxx:120
Int_t checkUrl(const char *url, const char *flog, bool def_proof)
Check if something is running at gUrl Return 0 if OK and data server 1 if OK and PROOF server -1 if n...
Definition: pq2ping.cxx:62
TUrl gUrl
Definition: pq2ping.cxx:49
Bool_t R_ISDIR(Int_t mode)
Definition: TSystem.h:126
Bool_t gIsProof
Definition: pq2ping.cxx:50
void SetOptions(const char *opt)
Definition: TUrl.h:96
R__EXTERN Int_t gDebug
Definition: Rtypes.h:128
Int_t pingXproofdAt()
Check if a XrdProofd service is running on 'port' at 'host' Return 0 if OK -1 if nothing is listening...
Definition: pq2ping.cxx:196
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition: TSocket.cxx:901
const Bool_t kTRUE
Definition: Rtypes.h:91
UShort_t host2net(UShort_t x)
Definition: Bytes.h:566
Int_t pingXrootdAt()
Check if a XrdXrootd service is running on 'port' at 'host' Return 0 if OK -1 if nothing is listening...
Definition: pq2ping.cxx:125