Logo ROOT   6.14/05
Reference Guide
XrdProofdAux.h
Go to the documentation of this file.
1 // @(#)root/proofd:$Id$
2 // Author: G. Ganis June 2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2005, 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 #ifndef ROOT_XrdProofdAux
13 #define ROOT_XrdProofdAux
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // XrdProofdAux //
18 // //
19 // Authors: G. Ganis, CERN, 2007 //
20 // //
21 // Small auxiliary classes used in XrdProof //
22 // //
23 //////////////////////////////////////////////////////////////////////////
24 #include <list>
25 #include <map>
26 #include <stdarg.h>
27 
28 #include "XpdSysSemWait.h"
29 
30 #include "Xrd/XrdProtocol.hh"
31 #include "XProofProtocol.h"
32 #include "XrdOuc/XrdOucHash.hh"
33 #include "XrdOuc/XrdOucString.hh"
34 
35 //
36 // User Info class
37 //
38 class XrdProofUI {
39 public:
40  XrdOucString fUser; // User name
41  XrdOucString fGroup; // PROOF group name
42  XrdOucString fHomeDir; // Unix home
43  int fUid; // Unix user ID
44  int fGid; // Unix group ID
45 
46  XrdProofUI() { fUid = -1; fGid = -1; }
47  XrdProofUI(const XrdProofUI &ui) { fUser = ui.fUser;
48  fGroup = ui.fGroup;
49  fHomeDir = ui.fHomeDir;
50  fUid = ui.fUid; fGid = ui.fGid; }
52 
53  void Reset() { fUser = ""; fHomeDir = ""; fGroup = ""; fUid = -1; fGid = -1; }
54 };
55 
56 //
57 // Group Info class
58 //
59 class XrdProofGI {
60 public:
61  XrdOucString fGroup;
62  int fGid;
63 
64  XrdProofGI() { fGid = -1; }
65  XrdProofGI(const XrdProofGI &gi) { fGroup = gi.fGroup; fGid = gi.fGid; }
67 
68  void Reset() { fGroup = ""; fGid = -1; }
69 };
70 
71 //
72 // File container (e.g. for config files)
73 //
75 public:
76  XrdOucString fName; // File name
77  time_t fMtime; // File mofification time last time we accessed it
78  XrdProofdFile(const char *fn = 0, time_t mtime = 0) : fName(fn), fMtime(mtime) { }
79 };
80 
81 //
82 // User priority
83 //
85 public:
86  XrdOucString fUser; // User to who this applies (wild cards accepted)
87  int fDeltaPriority; // Priority change
88  XrdProofdPriority(const char *usr, int dp) : fUser(usr), fDeltaPriority(dp) { }
89 };
90 
91 //
92 // Small class to describe a process
93 //
95 public:
96  int pid;
97  XrdOucString pname;
98  XrdProofdPInfo(int i, const char *n) : pid(i) { pname = n; }
99 };
100 
101 //
102 // Class to handle configuration directives
103 //
104 class XrdProofdDirective;
105 class XrdOucStream;
106 typedef int (*XrdFunDirective_t)(XrdProofdDirective *, char *,
107  XrdOucStream *cfg, bool reconfig);
109 public:
110  void *fVal;
111  XrdOucString fName;
113  bool fRcf;
114  const char *fHost; // needed to support old 'if' construct
115 
116  XrdProofdDirective(const char *n, void *v, XrdFunDirective_t f, bool rcf = 1) :
117  fVal(v), fName(n), fFun(f), fRcf(rcf), fHost(0) { }
118 
119  int DoDirective(char *val, XrdOucStream *cfg, bool reconfig)
120  { return (*fFun)(this, val, cfg, reconfig); }
121 };
122 // Function of general interest
123 int DoDirectiveClass(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
124 int DoDirectiveInt(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
125 int DoDirectiveString(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
126 // To set the host field in a loop over the hash list
127 int SetHostInDirectives(const char *, XrdProofdDirective *d, void *h);
128 
129 //
130 // Class to handle condensed multi-string specification, e.g <head>[01-25]<tail>
131 //
133 private:
134  long fIa;
135  long fIb;
136  XrdOucString fA;
137  XrdOucString fB;
138  int fType;
139  int fN; // Number of combinations
140 
141  void Init(const char *s);
142 public:
143  enum ETokenType { kUndef, kSimple, kLetter, kDigit, kDigits };
144 
145  XrdProofdMultiStrToken(const char *s = 0) { Init(s); }
147 
148  XrdOucString Export(int &next);
149  bool IsValid() const { return (fType == kUndef) ? 0 : 1; }
150  bool Matches(const char *s);
151  int N() const { return fN; }
152 };
153 
155 private:
156  XrdOucString fHead;
157  XrdOucString fTail;
158  std::list<XrdProofdMultiStrToken> fTokens;
159  int fN; // Number of combinations
160 
161  void Init(const char *s);
162 public:
163  XrdProofdMultiStr(const char *s) { Init(s); }
164  virtual ~XrdProofdMultiStr() { }
165 
166  XrdOucString Get(int i);
167  bool IsValid() const { return (fTokens.size() > 0 ? 1 : 0); }
168  bool Matches(const char *s);
169  int N() const { return fN; }
170 
171  XrdOucString Export();
172 };
173 
174 //
175 // Class to handle message buffers received via a pipe
176 //
177 class XpdMsg {
178  int fType;
179  XrdOucString fBuf;
180  int fFrom;
181 public:
182  XpdMsg(const char *buf = 0) { Init(buf); }
183  virtual ~XpdMsg() { }
184 
185  const char *Buf() const {return fBuf.c_str(); }
186 
187  int Init(const char *buf);
188  void Reset() { fFrom = 0; }
189 
190  int Get(int &i);
191  int Get(XrdOucString &s);
192  int Get(void **p);
193 
194  int Type() const { return fType; }
195 };
196 
197 //
198 // Class describing a pipe
199 //
201  XrdSysRecMutex fRdMtx; // Mutex for read operations
202  XrdSysRecMutex fWrMtx; // Mutex for write operations
203  int fPipe[2]; // pipe descriptors
204 public:
205  XrdProofdPipe();
206  virtual ~XrdProofdPipe();
207 
208  void Close();
209  bool IsValid() const { return (fPipe[0] > 0 && fPipe[1] > 0) ? 1 : 0; }
210 
211  int Poll(int to = -1);
212 
213  int Post(int type, const char *msg);
214  int Recv(XpdMsg &msg);
215 };
216 
217 //
218 // Container for DS information
219 //
221 public:
222  XrdOucString fType; // Backend type
223  XrdOucString fUrl; // URL from where to take the information
224  bool fLocal; // TRUE if on the local file system
225  bool fRW; // TRUE if users can modify their area
226  XrdOucString fOpts; // Options for this source
227  XrdOucString fObscure; // Obscure options to pass through
228  XrdProofdDSInfo(const char *t, const char *u, bool local, bool rw,
229  const char *o = "Ar:Av:", const char *obscure = "") :
230  fType(t), fUrl(u), fLocal(local), fRW(rw), fOpts(o),
231  fObscure(obscure) { }
232  XrdOucString ToString() const {
233  return "Type=" + fType + "; URL=" + fUrl +
234  "; Local=" + (fLocal ? "Yes" : "No") +
235  "; RW=" + (fRW ? "Yes" : "No") + "; Options=" + fOpts +
236  "; Obscure=" + fObscure;
237  }
238 };
239 
240 //
241 // Static methods
242 //
243 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
244 typedef struct kinfo_proc kinfo_proc;
245 #endif
246 class XrdOucStream;
249 public:
251 
252  static const char *AdminMsgType(int type);
253  static int AssertBaseDir(const char *path, XrdProofUI ui);
254  static int AssertDir(const char *path, XrdProofUI ui, bool changeown);
255  static int ChangeMod(const char *path, unsigned int mode);
256  static int ChangeOwn(const char *path, XrdProofUI ui);
257  static int ChangeToDir(const char *dir, XrdProofUI ui, bool changeown);
258  static int CheckIf(XrdOucStream *s, const char *h);
259  static char *Expand(char *p);
260  static void Expand(XrdOucString &path);
261  // String form functions
262  static void Form(XrdOucString &s, const char *fmt, int ns, const char *ss[5], int ni, int ii[6],
263  int np, void *pp[5], int nu = 0, unsigned int ui = 0);
264  static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1 = 0,
265  const char *s2 = 0, const char *s3 = 0, const char *s4 = 0);
266  static void Form(XrdOucString &s, const char *fmt, int i0, int i1 = 0, int i2 = 0,
267  int i3 = 0, int i4 = 0, int i5 = 0);
268  static void Form(XrdOucString &s, const char *fmt, void *p0, void *p1 = 0, void *p2 = 0,
269  void *p3 = 0, void *p4 = 0);
270  static void Form(XrdOucString &s, const char *fmt, int i0, const char *s0,
271  const char *s1 = 0, const char *s2 = 0, const char *s3 = 0);
272  static void Form(XrdOucString &s, const char *fmt, const char *s0,
273  int i0, int i1 = 0, int i2 = 0, int i3 = 0);
274  static void Form(XrdOucString &s, const char *fmt, const char *s0,
275  int i0, int i1, unsigned int u1);
276  static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1,
277  int i0, int i1, int i2);
278  static void Form(XrdOucString &s, const char *fmt, int i0, int i1,
279  const char *s0, const char *s1, const char *s2);
280  static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1,
281  const char *s2, int i0, int i1 = 0,
282  const char *s3 = 0, const char *s4 = 0);
283  static void Form(XrdOucString &s, const char *fmt, const char *s0, int i0, int i1,
284  const char *s1, const char *s2,
285  const char *s3);
286  static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1,
287  const char *s2, int i0, unsigned int u1);
288  static void Form(XrdOucString &s, const char *fmt, int i0, int i1, int i2,
289  const char *s0, const char *s1);
290 
291  static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1, const char *s2,
292  const char *s3, int i1);
293  static void Form(XrdOucString &s, const char *fmt, int i0, int i1, int i2, int i3, const char *s0);
294 
295  static void Form(XrdOucString &s, const char *fmt, int i0, int i1, void *p0);
296  static void Form(XrdOucString &s, const char *fmt, int i0, int i1, int i2, void *p0);
297  static void Form(XrdOucString &s, const char *fmt, int i0, int i1, int i2, int i3, void *p0);
298  static void Form(XrdOucString &s, const char *fmt, int i0, int i1, void *p0, int i2, int i3 = 0);
299  static void Form(XrdOucString &s, const char *fmt, void *p0, int i0, int i1);
300  static void Form(XrdOucString &s, const char *fmt, const char *s0, void *p0, int i0, int i1);
301  static void Form(XrdOucString &s, const char *fmt, void *p0, const char *s0, int i0);
302  static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1, void *p0);
303  static void Form(XrdOucString &s, const char *fmt, int i0, const char *s0, const char *s1,
304  int i1, int i2 = 0);
305  static void Form(XrdOucString &s, const char *fmt, int i0, const char *s0, int i1, int i2 = 0);
306 
307  static int GetIDFromPath(const char *path, XrdOucString &emsg);
308  static long int GetLong(char *str);
309 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
310  static int GetMacProcList(kinfo_proc **plist, int &nproc);
311 #endif
312  static int GetNumCPUs();
313  static int GetGroupInfo(const char *grp, XrdProofGI &gi);
314  static int GetGroupInfo(int gid, XrdProofGI &gi);
315  static int GetProcesses(const char *pn, std::map<int,XrdOucString> *plist);
316  static int GetUserInfo(const char *usr, XrdProofUI &ui);
317  static int GetUserInfo(int uid, XrdProofUI &ui);
318  static bool HasToken(const char *s, const char *tokens);
319  static int KillProcess(int pid, bool forcekill, XrdProofUI ui, bool changeown);
320  static void LogEmsgToFile(const char *flog, const char *emsg, const char *pfx = 0);
321  static int MvDir(const char *oldpath, const char *newpath);
322  static int ParsePidPath(const char *path, XrdOucString &before, XrdOucString &after);
323  static int ParseUsrGrp(const char *path, XrdOucString &usr, XrdOucString &grp);
324  static const char *ProofRequestTypes(int type);
325  static int ReadMsg(int fd, XrdOucString &msg);
326  static int RmDir(const char *path);
327  static int SymLink(const char *path, const char *link);
328  static int Touch(const char *path, int opt = 0);
329  static int VerifyProcessByID(int pid, const char *pname = "proofserv");
330  static int Write(int fd, const void *buf, size_t nb);
331 };
332 
333 // Useful definitions
334 #ifndef SafeDel
335 #define SafeDel(x) { if (x) { delete x; x = 0; } }
336 #endif
337 #ifndef SafeDelArray
338 #define SafeDelArray(x) { if (x) { delete[] x; x = 0; } }
339 #endif
340 #ifndef SafeFree
341 #define SafeFree(x) { if (x) free(x); x = 0; }
342 #endif
343 
344 #ifndef INRANGE
345 #define INRANGE(x,y) ((x >= 0) && (x < (int)y->size()))
346 #endif
347 
348 #ifndef DIGIT
349 #define DIGIT(x) (x >= 48 && x <= 57)
350 #endif
351 
352 #ifndef LETTOIDX
353 #define LETTOIDX(x, ilet) \
354  if (x >= 97 && x <= 122) ilet = x - 96; \
355  if (x >= 65 && x <= 90) ilet = x - 38;
356 #endif
357 #ifndef IDXTOLET
358 #define IDXTOLET(ilet, x) \
359  if ((ilet) >= 1 && (ilet) <= 26) x = (ilet) + 96; \
360  if ((ilet) >= 27 && (ilet) <= 52) x = (ilet) + 38;
361 #endif
362 
363 #ifndef XPDSWAP
364 #define XPDSWAP(a,b,t) { t = a ; a = b; b = t; }
365 #endif
366 
367 #ifndef XpdBadPGuard
368 #define XpdBadPGuard(g,u) (!(g.Valid()) && (geteuid() != (uid_t)u))
369 #endif
370 
371 #undef MHEAD
372 #define MHEAD "--- Proofd: "
373 
374 #undef TRACELINK
375 #define TRACELINK fLink
376 
377 #undef RESPONSE
378 #define RESPONSE fResponse
379 
380 #ifndef XPDFORM
381 #define XPDFORM XrdProofdAux::Form
382 #endif
383 
384 #endif
XrdProofdDirective(const char *n, void *v, XrdFunDirective_t f, bool rcf=1)
Definition: XrdProofdAux.h:116
XrdOucString fName
Definition: XrdProofdAux.h:76
int fType
Definition: XrdProofdAux.h:178
bool IsValid() const
Definition: XrdProofdAux.h:209
static double p3(double t, double a, double b, double c, double d)
XrdOucString fUrl
Definition: XrdProofdAux.h:223
virtual ~XrdProofdMultiStr()
Definition: XrdProofdAux.h:164
#define s0(x)
Definition: RSha256.hxx:90
XrdSysRecMutex fWrMtx
Definition: XrdProofdAux.h:202
XrdProofdPriority(const char *usr, int dp)
Definition: XrdProofdAux.h:88
int DoDirectiveClass(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf)
Generic class directive processor.
const char * Buf() const
Definition: XrdProofdAux.h:185
XrdOucString ToString() const
Definition: XrdProofdAux.h:232
XrdOucString fBuf
Definition: XrdProofdAux.h:179
#define XrdSysRecMutex
Definition: XrdSysToOuc.h:18
int(* XrdFunDirective_t)(XrdProofdDirective *, char *, XrdOucStream *cfg, bool reconfig)
Definition: XrdProofdAux.h:106
XrdProofUI(const XrdProofUI &ui)
Definition: XrdProofdAux.h:47
int Type() const
Definition: XrdProofdAux.h:194
#define f(i)
Definition: RSha256.hxx:104
int DoDirectiveInt(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf)
Process directive for an integer.
void Reset()
Definition: XrdProofdAux.h:53
XrdProofGI(const XrdProofGI &gi)
Definition: XrdProofdAux.h:65
XrdOucString fTail
Definition: XrdProofdAux.h:157
void Init(TClassEdit::TInterpreterLookupHelper *helper)
Definition: TClassEdit.cxx:121
static double p2(double t, double a, double b, double c)
XrdOucString fHead
Definition: XrdProofdAux.h:156
TString flog
Definition: pq2main.cxx:37
XpdMsg(const char *buf=0)
Definition: XrdProofdAux.h:182
XrdOucString fOpts
Definition: XrdProofdAux.h:226
XrdOucString fUser
Definition: XrdProofdAux.h:40
XrdOucString fObscure
Definition: XrdProofdAux.h:227
int SetHostInDirectives(const char *, XrdProofdDirective *d, void *h)
Set host field for directive &#39;d&#39; to (const char *h)
SVector< double, 2 > v
Definition: Dict.h:5
int fFrom
Definition: XrdProofdAux.h:180
XrdProofdPInfo(int i, const char *n)
Definition: XrdProofdAux.h:98
int changeown(const std::string &path, uid_t u, gid_t g)
Change the ownership of &#39;path&#39; to the entity described by {u,g}.
Definition: proofexecv.cxx:804
XrdOucString fGroup
Definition: XrdProofdAux.h:41
char * Form(const char *fmt,...)
XrdOucString fGroup
Definition: XrdProofdAux.h:61
XrdProofdMultiStrToken(const char *s=0)
Definition: XrdProofdAux.h:145
#define s1(x)
Definition: RSha256.hxx:91
XrdFunDirective_t fFun
Definition: XrdProofdAux.h:112
static double p1(double t, double a, double b)
virtual ~XrdProofdMultiStrToken()
Definition: XrdProofdAux.h:146
void Reset()
Definition: XrdProofdAux.h:188
XrdOucString pname
Definition: XrdProofdAux.h:97
#define h(i)
Definition: RSha256.hxx:106
PyObject * fType
#define d(i)
Definition: RSha256.hxx:102
int DoDirectiveString(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf)
Process directive for a string.
void Reset()
Definition: XrdProofdAux.h:68
int type
Definition: TGX11.cxx:120
XrdProofdFile(const char *fn=0, time_t mtime=0)
Definition: XrdProofdAux.h:78
static constexpr double s
const char * fHost
Definition: XrdProofdAux.h:114
bool IsValid() const
Definition: XrdProofdAux.h:167
XrdOucString fName
Definition: XrdProofdAux.h:111
std::list< XrdProofdMultiStrToken > fTokens
Definition: XrdProofdAux.h:158
XrdProofdMultiStr(const char *s)
Definition: XrdProofdAux.h:163
int DoDirective(char *val, XrdOucStream *cfg, bool reconfig)
Definition: XrdProofdAux.h:119
XrdOucString fHomeDir
Definition: XrdProofdAux.h:42
virtual ~XpdMsg()
Definition: XrdProofdAux.h:183
XrdOucString fType
Definition: XrdProofdAux.h:222
XrdSysRecMutex fRdMtx
Definition: XrdProofdAux.h:201
XrdProofdDSInfo(const char *t, const char *u, bool local, bool rw, const char *o="Ar:Av:", const char *obscure="")
Definition: XrdProofdAux.h:228
static constexpr double ns
const Int_t n
Definition: legend1.C:16
XrdOucString fUser
Definition: XrdProofdAux.h:86
static XrdSysRecMutex fgFormMutex
Definition: XrdProofdAux.h:248