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