Loading [MathJax]/extensions/tex2jax.js
Logo ROOT   6.16/01
Reference Guide
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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//
39public:
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//
60public:
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//
75public:
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//
85public:
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//
95public:
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//
105class XrdOucStream;
106typedef int (*XrdFunDirective_t)(XrdProofdDirective *, char *,
107 XrdOucStream *cfg, bool reconfig);
109public:
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
123int DoDirectiveClass(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
124int DoDirectiveInt(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
125int DoDirectiveString(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
126// To set the host field in a loop over the hash list
127int SetHostInDirectives(const char *, XrdProofdDirective *d, void *h);
128
129//
130// Class to handle condensed multi-string specification, e.g <head>[01-25]<tail>
131//
133private:
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);
142public:
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
155private:
156 XrdOucString fHead;
157 XrdOucString fTail;
158 std::list<XrdProofdMultiStrToken> fTokens;
159 int fN; // Number of combinations
160
161 void Init(const char *s);
162public:
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//
177class XpdMsg {
178 int fType;
179 XrdOucString fBuf;
180 int fFrom;
181public:
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
204public:
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//
221public:
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__)
244typedef struct kinfo_proc kinfo_proc;
245#endif
246class XrdOucStream;
249public:
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
SVector< double, 2 > v
Definition: Dict.h:5
#define d(i)
Definition: RSha256.hxx:102
#define f(i)
Definition: RSha256.hxx:104
#define s0(x)
Definition: RSha256.hxx:90
#define s1(x)
Definition: RSha256.hxx:91
#define h(i)
Definition: RSha256.hxx:106
static double p3(double t, double a, double b, double c, double d)
static double p1(double t, double a, double b)
static double p2(double t, double a, double b, double c)
int type
Definition: TGX11.cxx:120
int DoDirectiveString(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf)
Process directive for a string.
int SetHostInDirectives(const char *, XrdProofdDirective *d, void *h)
Set host field for directive 'd' to (const char *h)
int DoDirectiveClass(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf)
Generic class directive processor.
int DoDirectiveInt(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf)
Process directive for an integer.
int(* XrdFunDirective_t)(XrdProofdDirective *, char *, XrdOucStream *cfg, bool reconfig)
Definition: XrdProofdAux.h:106
#define XrdSysRecMutex
Definition: XrdSysToOuc.h:18
XpdMsg(const char *buf=0)
Definition: XrdProofdAux.h:182
int Type() const
Definition: XrdProofdAux.h:194
int fFrom
Definition: XrdProofdAux.h:180
XrdOucString fBuf
Definition: XrdProofdAux.h:179
const char * Buf() const
Definition: XrdProofdAux.h:185
int Get(int &i)
Get next token and interpret it as an int.
int fType
Definition: XrdProofdAux.h:178
int Init(const char *buf)
Init from buffer.
void Reset()
Definition: XrdProofdAux.h:188
virtual ~XpdMsg()
Definition: XrdProofdAux.h:183
void Reset()
Definition: XrdProofdAux.h:68
XrdProofGI(const XrdProofGI &gi)
Definition: XrdProofdAux.h:65
XrdOucString fGroup
Definition: XrdProofdAux.h:61
XrdOucString fUser
Definition: XrdProofdAux.h:40
XrdOucString fGroup
Definition: XrdProofdAux.h:41
XrdProofUI(const XrdProofUI &ui)
Definition: XrdProofdAux.h:47
void Reset()
Definition: XrdProofdAux.h:53
XrdOucString fHomeDir
Definition: XrdProofdAux.h:42
static int ChangeOwn(const char *path, XrdProofUI ui)
Change the ownership of 'path' to the entity described by 'ui'.
static int GetUserInfo(const char *usr, XrdProofUI &ui)
Get information about user 'usr' in a thread safe way.
static int MvDir(const char *oldpath, const char *newpath)
Move content of directory at oldpath to newpath.
static int Touch(const char *path, int opt=0)
Set access (opt == 1), modify (opt =2 ) or access&modify (opt = 0, default) times of path to current ...
static void Form(XrdOucString &s, const char *fmt, int ns, const char *ss[5], int ni, int ii[6], int np, void *pp[5], int nu=0, unsigned int ui=0)
Recreate the string according to 'fmt', the up to 5 'const char *', up to 6 'int' arguments,...
static int ParseUsrGrp(const char *path, XrdOucString &usr, XrdOucString &grp)
Parse a path in the form of "<usr>[.<grp>][.<pid>]", filling 'usr' and 'grp'.
static int AssertDir(const char *path, XrdProofUI ui, bool changeown)
Make sure that 'path' exists and is owned by the entity described by 'ui'.
static int VerifyProcessByID(int pid, const char *pname="proofserv")
Check if a process named 'pname' and process 'pid' is still in the process table.
static long int GetLong(char *str)
Extract first integer from string at 'str', if any.
static const char * ProofRequestTypes(int type)
Translates the proof request type in a human readable string.
static int KillProcess(int pid, bool forcekill, XrdProofUI ui, bool changeown)
Kill the process 'pid'.
static bool HasToken(const char *s, const char *tokens)
Returns true is 's' contains at least one of the comma-separated tokens in 'tokens'.
static int GetGroupInfo(const char *grp, XrdProofGI &gi)
Get information about group with 'gid' in a thread safe way.
static int ChangeToDir(const char *dir, XrdProofUI ui, bool changeown)
Change current directory to 'dir'.
static int ChangeMod(const char *path, unsigned int mode)
Change the permission mode of 'path' to 'mode'.
static int GetNumCPUs()
Find out and return the number of CPUs in the local machine.
static int GetProcesses(const char *pn, std::map< int, XrdOucString > *plist)
Get from the process table list of PIDs for processes named "proofserv' For {linux,...
static int ReadMsg(int fd, XrdOucString &msg)
Receive 'msg' from pipe fd.
static int GetIDFromPath(const char *path, XrdOucString &emsg)
Extract an integer from a file.
static int Write(int fd, const void *buf, size_t nb)
Write nb bytes at buf to descriptor 'fd' ignoring interrupts Return the number of bytes written or -1...
static XrdSysRecMutex fgFormMutex
Definition: XrdProofdAux.h:248
static void LogEmsgToFile(const char *flog, const char *emsg, const char *pfx=0)
Logs error message 'emsg' to file 'flog' using standard technology.
static int RmDir(const char *path)
Remove directory at path and its content.
static int SymLink(const char *path, const char *link)
Create a symlink 'link' to 'path' Return 0 in case of success, -1 in case of error.
static const char * AdminMsgType(int type)
Translates the admin message type in a human readable string.
static int CheckIf(XrdOucStream *s, const char *h)
Check existence and match condition of an 'if' directive If none (valid) is found,...
static char * Expand(char *p)
Expand path 'p' relative to: $HOME if begins with ~/ <user>'s $HOME if begins with ~<user>/ $PWD if d...
static int ParsePidPath(const char *path, XrdOucString &before, XrdOucString &after)
Parse a path in the form of "<before>[.<pid>][.<after>]", filling 'rest' and returning 'pid'.
static int AssertBaseDir(const char *path, XrdProofUI ui)
Make sure that the base dir of 'path' is either owned by 'ui' or gives full permissions to 'ui'.
XrdOucString fType
Definition: XrdProofdAux.h:222
XrdOucString fUrl
Definition: XrdProofdAux.h:223
XrdOucString ToString() const
Definition: XrdProofdAux.h:232
XrdOucString fObscure
Definition: XrdProofdAux.h:227
XrdProofdDSInfo(const char *t, const char *u, bool local, bool rw, const char *o="Ar:Av:", const char *obscure="")
Definition: XrdProofdAux.h:228
XrdOucString fOpts
Definition: XrdProofdAux.h:226
XrdOucString fName
Definition: XrdProofdAux.h:111
int DoDirective(char *val, XrdOucStream *cfg, bool reconfig)
Definition: XrdProofdAux.h:119
XrdFunDirective_t fFun
Definition: XrdProofdAux.h:112
const char * fHost
Definition: XrdProofdAux.h:114
XrdProofdDirective(const char *n, void *v, XrdFunDirective_t f, bool rcf=1)
Definition: XrdProofdAux.h:116
XrdProofdFile(const char *fn=0, time_t mtime=0)
Definition: XrdProofdAux.h:78
XrdOucString fName
Definition: XrdProofdAux.h:76
XrdOucString Export(int &next)
Export 'next' token; use next < 0 start from the first.
bool Matches(const char *s)
Return true if 's' is compatible with this token.
void Init(const char *s)
Init the multi-string token.
virtual ~XrdProofdMultiStrToken()
Definition: XrdProofdAux.h:146
XrdProofdMultiStrToken(const char *s=0)
Definition: XrdProofdAux.h:145
XrdOucString fTail
Definition: XrdProofdAux.h:157
XrdOucString Export()
Return a string with comma-separated elements.
void Init(const char *s)
Init the multi-string handler.
std::list< XrdProofdMultiStrToken > fTokens
Definition: XrdProofdAux.h:158
XrdOucString Get(int i)
Return i-th combination (i : 0 -> fN-1)
XrdProofdMultiStr(const char *s)
Definition: XrdProofdAux.h:163
bool IsValid() const
Definition: XrdProofdAux.h:167
bool Matches(const char *s)
Return true if 's' is compatible with this multi-string.
XrdOucString fHead
Definition: XrdProofdAux.h:156
virtual ~XrdProofdMultiStr()
Definition: XrdProofdAux.h:164
XrdProofdPInfo(int i, const char *n)
Definition: XrdProofdAux.h:98
XrdOucString pname
Definition: XrdProofdAux.h:97
bool IsValid() const
Definition: XrdProofdAux.h:209
int Recv(XpdMsg &msg)
Recv message from the pipe.
int Poll(int to=-1)
Poll over the read pipe for to secs; return whatever poll returns.
void Close()
If open, close and invalidated the pipe descriptors.
XrdProofdPipe()
Constructor: create the pipe.
virtual ~XrdProofdPipe()
Destructor.
XrdSysRecMutex fRdMtx
Definition: XrdProofdAux.h:201
XrdSysRecMutex fWrMtx
Definition: XrdProofdAux.h:202
int Post(int type, const char *msg)
Post message on the pipe.
XrdOucString fUser
Definition: XrdProofdAux.h:86
XrdProofdPriority(const char *usr, int dp)
Definition: XrdProofdAux.h:88
const Int_t n
Definition: legend1.C:16
static constexpr double s
static constexpr double ns
int changeown(const std::string &path, uid_t u, gid_t g)
Change the ownership of 'path' to the entity described by {u,g}.
Definition: proofexecv.cxx:738