Logo ROOT  
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//
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 &) = default;
49
50 void Reset() { fUser = ""; fHomeDir = ""; fGroup = ""; fUid = -1; fGid = -1; }
51};
52
53//
54// Group Info class
55//
57public:
58 XrdOucString fGroup;
59 int fGid;
60
61 XrdProofGI() { fGid = -1; }
62 XrdProofGI(const XrdProofGI &gi) { fGroup = gi.fGroup; fGid = gi.fGid; }
64
65 void Reset() { fGroup = ""; fGid = -1; }
66};
67
68//
69// File container (e.g. for config files)
70//
72public:
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//
82public:
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//
92public:
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//
102class XrdOucStream;
103typedef int (*XrdFunDirective_t)(XrdProofdDirective *, char *,
104 XrdOucStream *cfg, bool reconfig);
106public:
107 void *fVal;
108 XrdOucString fName;
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
120int DoDirectiveClass(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
121int DoDirectiveInt(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
122int DoDirectiveString(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
123// To set the host field in a loop over the hash list
124int SetHostInDirectives(const char *, XrdProofdDirective *d, void *h);
125
126//
127// Class to handle condensed multi-string specification, e.g <head>[01-25]<tail>
128//
130private:
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);
139public:
141
142 XrdProofdMultiStrToken(const char *s = 0) { Init(s); }
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
152private:
153 XrdOucString fHead;
154 XrdOucString fTail;
155 std::list<XrdProofdMultiStrToken> fTokens;
156 int fN; // Number of combinations
157
158 void Init(const char *s);
159public:
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//
174class XpdMsg {
175 int fType;
176 XrdOucString fBuf;
177 int fFrom;
178public:
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//
198 XrdSysRecMutex fRdMtx; // Mutex for read operations
199 XrdSysRecMutex fWrMtx; // Mutex for write operations
200 int fPipe[2]; // pipe descriptors
201public:
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//
218public:
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__)
241typedef struct kinfo_proc kinfo_proc;
242#endif
243class XrdOucStream;
246public:
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
#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
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:103
#define XrdSysRecMutex
Definition: XrdSysToOuc.h:18
XpdMsg(const char *buf=0)
Definition: XrdProofdAux.h:179
int Type() const
Definition: XrdProofdAux.h:191
int fFrom
Definition: XrdProofdAux.h:177
XrdOucString fBuf
Definition: XrdProofdAux.h:176
const char * Buf() const
Definition: XrdProofdAux.h:182
int Get(int &i)
Get next token and interpret it as an int.
int fType
Definition: XrdProofdAux.h:175
int Init(const char *buf)
Init from buffer.
void Reset()
Definition: XrdProofdAux.h:185
virtual ~XpdMsg()
Definition: XrdProofdAux.h:180
void Reset()
Definition: XrdProofdAux.h:65
XrdProofGI(const XrdProofGI &gi)
Definition: XrdProofdAux.h:62
XrdOucString fGroup
Definition: XrdProofdAux.h:58
XrdOucString fUser
Definition: XrdProofdAux.h:40
XrdOucString fGroup
Definition: XrdProofdAux.h:41
void Reset()
Definition: XrdProofdAux.h:50
XrdOucString fHomeDir
Definition: XrdProofdAux.h:42
XrdProofUI(const XrdProofUI &)=default
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:245
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:219
XrdOucString fUrl
Definition: XrdProofdAux.h:220
XrdOucString ToString() const
Definition: XrdProofdAux.h:229
XrdOucString fObscure
Definition: XrdProofdAux.h:224
XrdProofdDSInfo(const char *t, const char *u, bool local, bool rw, const char *o="Ar:Av:", const char *obscure="")
Definition: XrdProofdAux.h:225
XrdOucString fOpts
Definition: XrdProofdAux.h:223
XrdOucString fName
Definition: XrdProofdAux.h:108
int DoDirective(char *val, XrdOucStream *cfg, bool reconfig)
Definition: XrdProofdAux.h:116
XrdFunDirective_t fFun
Definition: XrdProofdAux.h:109
const char * fHost
Definition: XrdProofdAux.h:111
XrdProofdDirective(const char *n, void *v, XrdFunDirective_t f, bool rcf=1)
Definition: XrdProofdAux.h:113
XrdProofdFile(const char *fn=0, time_t mtime=0)
Definition: XrdProofdAux.h:75
XrdOucString fName
Definition: XrdProofdAux.h:73
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:143
XrdProofdMultiStrToken(const char *s=0)
Definition: XrdProofdAux.h:142
XrdOucString fTail
Definition: XrdProofdAux.h:154
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:155
XrdOucString Get(int i)
Return i-th combination (i : 0 -> fN-1)
XrdProofdMultiStr(const char *s)
Definition: XrdProofdAux.h:160
bool IsValid() const
Definition: XrdProofdAux.h:164
bool Matches(const char *s)
Return true if 's' is compatible with this multi-string.
XrdOucString fHead
Definition: XrdProofdAux.h:153
virtual ~XrdProofdMultiStr()
Definition: XrdProofdAux.h:161
XrdProofdPInfo(int i, const char *n)
Definition: XrdProofdAux.h:95
XrdOucString pname
Definition: XrdProofdAux.h:94
bool IsValid() const
Definition: XrdProofdAux.h:206
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:198
XrdSysRecMutex fWrMtx
Definition: XrdProofdAux.h:199
int Post(int type, const char *msg)
Post message on the pipe.
XrdOucString fUser
Definition: XrdProofdAux.h:83
XrdProofdPriority(const char *usr, int dp)
Definition: XrdProofdAux.h:85
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