12 #ifndef ROOT_XrdProofdAux
13 #define ROOT_XrdProofdAux
30 #include "Xrd/XrdProtocol.hh"
32 #include "XrdOuc/XrdOucHash.hh"
33 #include "XrdOuc/XrdOucString.hh"
42 XrdOucString fHomeDir;
46 XrdProofUI() { fUid = -1; fGid = -1; }
47 XrdProofUI(
const XrdProofUI &) =
default;
50 void Reset() { fUser =
""; fHomeDir =
""; fGroup =
""; fUid = -1; fGid = -1; }
61 XrdProofGI() { fGid = -1; }
62 XrdProofGI(
const XrdProofGI &gi) { fGroup = gi.fGroup; fGid = gi.fGid; }
65 void Reset() { fGroup =
""; fGid = -1; }
75 XrdProofdFile(
const char *fn = 0, time_t mtime = 0) : fName(fn), fMtime(mtime) { }
81 class XrdProofdPriority {
85 XrdProofdPriority(
const char *usr,
int dp) : fUser(usr), fDeltaPriority(dp) { }
91 class XrdProofdPInfo {
95 XrdProofdPInfo(
int i,
const char *n) : pid(i) { pname = n; }
101 class XrdProofdDirective;
103 typedef int (*XrdFunDirective_t)(XrdProofdDirective *,
char *,
104 XrdOucStream *cfg,
bool reconfig);
105 class XrdProofdDirective {
109 XrdFunDirective_t fFun;
113 XrdProofdDirective(
const char *n,
void *v, XrdFunDirective_t f,
bool rcf = 1) :
114 fVal(v), fName(n), fFun(f), fRcf(rcf), fHost(0) { }
116 int DoDirective(
char *val, XrdOucStream *cfg,
bool reconfig)
117 {
return (*fFun)(
this, val, cfg, reconfig); }
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);
124 int SetHostInDirectives(
const char *, XrdProofdDirective *d,
void *h);
129 class XrdProofdMultiStrToken {
138 void Init(
const char *s);
140 enum ETokenType { kUndef, kSimple, kLetter, kDigit, kDigits };
142 XrdProofdMultiStrToken(
const char *s = 0) { Init(s); }
143 virtual ~XrdProofdMultiStrToken() { }
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; }
151 class XrdProofdMultiStr {
155 std::list<XrdProofdMultiStrToken> fTokens;
158 void Init(
const char *s);
160 XrdProofdMultiStr(
const char *s) { Init(s); }
161 virtual ~XrdProofdMultiStr() { }
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; }
168 XrdOucString Export();
179 XpdMsg(
const char *buf = 0) { Init(buf); }
180 virtual ~XpdMsg() { }
182 const char *Buf()
const {
return fBuf.c_str(); }
184 int Init(
const char *buf);
185 void Reset() { fFrom = 0; }
188 int Get(XrdOucString &s);
191 int Type()
const {
return fType; }
197 class XrdProofdPipe {
198 XrdSysRecMutex fRdMtx;
199 XrdSysRecMutex fWrMtx;
203 virtual ~XrdProofdPipe();
206 bool IsValid()
const {
return (fPipe[0] > 0 && fPipe[1] > 0) ? 1 : 0; }
208 int Poll(
int to = -1);
210 int Post(
int type,
const char *msg);
211 int Recv(XpdMsg &msg);
217 class XrdProofdDSInfo {
224 XrdOucString fObscure;
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;
240 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
241 typedef struct kinfo_proc kinfo_proc;
245 static XrdSysRecMutex fgFormMutex;
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);
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,
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);
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);
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,
302 static void Form(XrdOucString &s,
const char *fmt,
int i0,
const char *s0,
int i1,
int i2 = 0);
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);
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);
332 #define SafeDel(x) { if (x) { delete x; x = 0; } }
335 #define SafeDelArray(x) { if (x) { delete[] x; x = 0; } }
338 #define SafeFree(x) { if (x) free(x); x = 0; }
342 #define INRANGE(x,y) ((x >= 0) && (x < (int)y->size()))
346 #define DIGIT(x) (x >= 48 && x <= 57)
350 #define LETTOIDX(x, ilet) \
351 if (x >= 97 && x <= 122) ilet = x - 96; \
352 if (x >= 65 && x <= 90) ilet = x - 38;
355 #define IDXTOLET(ilet, x) \
356 if ((ilet) >= 1 && (ilet) <= 26) x = (ilet) + 96; \
357 if ((ilet) >= 27 && (ilet) <= 52) x = (ilet) + 38;
361 #define XPDSWAP(a,b,t) { t = a ; a = b; b = t; }
365 #define XpdBadPGuard(g,u) (!(g.Valid()) && (geteuid() != (uid_t)u))
369 #define MHEAD "--- Proofd: "
372 #define TRACELINK fLink
375 #define RESPONSE fResponse
378 #define XPDFORM XrdProofdAux::Form