00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef ROOT_XrdProofdAux
00013 #define ROOT_XrdProofdAux
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <list>
00025 #include <map>
00026 #include <stdarg.h>
00027
00028 #ifdef OLDXRDOUC
00029 # include "XrdSysToOuc.h"
00030 # include "XrdOuc/XrdOucSemWait.hh"
00031 #else
00032 # include "XrdSys/XrdSysSemWait.hh"
00033 #endif
00034
00035 #include "Xrd/XrdProtocol.hh"
00036 #include "XProofProtocol.h"
00037 #include "XrdOuc/XrdOucHash.hh"
00038 #include "XrdOuc/XrdOucString.hh"
00039
00040
00041
00042
00043 class XrdProofUI {
00044 public:
00045 XrdOucString fUser;
00046 XrdOucString fGroup;
00047 XrdOucString fHomeDir;
00048 int fUid;
00049 int fGid;
00050
00051 XrdProofUI() { fUid = -1; fGid = -1; }
00052 XrdProofUI(const XrdProofUI &ui) { fUser = ui.fUser;
00053 fGroup = ui.fGroup;
00054 fHomeDir = ui.fHomeDir;
00055 fUid = ui.fUid; fGid = ui.fGid; }
00056 ~XrdProofUI() { }
00057
00058 void Reset() { fUser = ""; fHomeDir = ""; fGroup = ""; fUid = -1; fGid = -1; }
00059 };
00060
00061
00062
00063
00064 class XrdProofGI {
00065 public:
00066 XrdOucString fGroup;
00067 int fGid;
00068
00069 XrdProofGI() { fGid = -1; }
00070 XrdProofGI(const XrdProofGI &gi) { fGroup = gi.fGroup; fGid = gi.fGid; }
00071 ~XrdProofGI() { }
00072
00073 void Reset() { fGroup = ""; fGid = -1; }
00074 };
00075
00076
00077
00078
00079 class XrdProofdFile {
00080 public:
00081 XrdOucString fName;
00082 time_t fMtime;
00083 XrdProofdFile(const char *fn = 0, time_t mtime = 0) : fName(fn), fMtime(mtime) { }
00084 };
00085
00086
00087
00088
00089 class XrdProofdPriority {
00090 public:
00091 XrdOucString fUser;
00092 int fDeltaPriority;
00093 XrdProofdPriority(const char *usr, int dp) : fUser(usr), fDeltaPriority(dp) { }
00094 };
00095
00096
00097
00098
00099 class XrdProofdPInfo {
00100 public:
00101 int pid;
00102 XrdOucString pname;
00103 XrdProofdPInfo(int i, const char *n) : pid(i) { pname = n; }
00104 };
00105
00106
00107
00108
00109 class XrdProofdDirective;
00110 class XrdOucStream;
00111 typedef int (*XrdFunDirective_t)(XrdProofdDirective *, char *,
00112 XrdOucStream *cfg, bool reconfig);
00113 class XrdProofdDirective {
00114 public:
00115 void *fVal;
00116 XrdOucString fName;
00117 XrdFunDirective_t fFun;
00118 bool fRcf;
00119 const char *fHost;
00120
00121 XrdProofdDirective(const char *n, void *v, XrdFunDirective_t f, bool rcf = 1) :
00122 fVal(v), fName(n), fFun(f), fRcf(rcf) { }
00123
00124 int DoDirective(char *val, XrdOucStream *cfg, bool reconfig)
00125 { return (*fFun)(this, val, cfg, reconfig); }
00126 };
00127
00128 int DoDirectiveClass(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
00129 int DoDirectiveInt(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
00130 int DoDirectiveString(XrdProofdDirective *, char *val, XrdOucStream *cfg, bool rcf);
00131
00132 int SetHostInDirectives(const char *, XrdProofdDirective *d, void *h);
00133
00134
00135
00136
00137 class XrdProofdMultiStrToken {
00138 private:
00139 long fIa;
00140 long fIb;
00141 XrdOucString fA;
00142 XrdOucString fB;
00143 int fType;
00144 int fN;
00145
00146 void Init(const char *s);
00147 public:
00148 enum ETokenType { kUndef, kSimple, kLetter, kDigit, kDigits };
00149
00150 XrdProofdMultiStrToken(const char *s = 0) { Init(s); }
00151 virtual ~XrdProofdMultiStrToken() { }
00152
00153 XrdOucString Export(int &next);
00154 bool IsValid() const { return (fType == kUndef) ? 0 : 1; }
00155 bool Matches(const char *s);
00156 int N() const { return fN; }
00157 };
00158
00159 class XrdProofdMultiStr {
00160 private:
00161 XrdOucString fHead;
00162 XrdOucString fTail;
00163 std::list<XrdProofdMultiStrToken> fTokens;
00164 int fN;
00165
00166 void Init(const char *s);
00167 public:
00168 XrdProofdMultiStr(const char *s) { Init(s); }
00169 virtual ~XrdProofdMultiStr() { }
00170
00171 XrdOucString Get(int i);
00172 bool IsValid() const { return (fTokens.size() > 0 ? 1 : 0); }
00173 bool Matches(const char *s);
00174 int N() const { return fN; }
00175
00176 XrdOucString Export();
00177 };
00178
00179
00180
00181
00182 class XpdMsg {
00183 int fType;
00184 XrdOucString fBuf;
00185 int fFrom;
00186 public:
00187 XpdMsg(const char *buf = 0) { Init(buf); }
00188 virtual ~XpdMsg() { }
00189
00190 const char *Buf() const {return fBuf.c_str(); }
00191
00192 int Init(const char *buf);
00193 void Reset() { fFrom = 0; }
00194
00195 int Get(int &i);
00196 int Get(XrdOucString &s);
00197 int Get(void **p);
00198
00199 int Type() const { return fType; }
00200 };
00201
00202
00203
00204
00205 class XrdProofdPipe {
00206 XrdSysRecMutex fRdMtx;
00207 XrdSysRecMutex fWrMtx;
00208 int fPipe[2];
00209 public:
00210 XrdProofdPipe();
00211 virtual ~XrdProofdPipe();
00212
00213 void Close();
00214 bool IsValid() const { return (fPipe[0] > 0 && fPipe[1] > 0) ? 1 : 0; }
00215
00216 int Poll(int to = -1);
00217
00218 int Post(int type, const char *msg);
00219 int Recv(XpdMsg &msg);
00220 };
00221
00222
00223
00224
00225 class XrdProofdDSInfo {
00226 public:
00227 XrdOucString fType;
00228 XrdOucString fUrl;
00229 bool fLocal;
00230 bool fRW;
00231 XrdOucString fOpts;
00232 XrdProofdDSInfo(const char *t, const char *u, bool local, bool rw,
00233 const char *o = "Ar:Av:") :
00234 fType(t), fUrl(u), fLocal(local), fRW(rw), fOpts(o) { }
00235 };
00236
00237
00238
00239
00240 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
00241 typedef struct kinfo_proc kinfo_proc;
00242 #endif
00243 class XrdOucStream;
00244 class XrdProofdAux {
00245 static XrdSysRecMutex fgFormMutex;
00246 public:
00247 XrdProofdAux() { }
00248
00249 static const char *AdminMsgType(int type);
00250 static int AssertDir(const char *path, XrdProofUI ui, bool changeown);
00251 static int ChangeMod(const char *path, unsigned int mode);
00252 static int ChangeOwn(const char *path, XrdProofUI ui);
00253 static int ChangeToDir(const char *dir, XrdProofUI ui, bool changeown);
00254 static int CheckIf(XrdOucStream *s, const char *h);
00255 static char *Expand(char *p);
00256 static void Expand(XrdOucString &path);
00257
00258 static void Form(XrdOucString &s, const char *fmt, int ns, const char *ss[5], int ni, int ii[5],
00259 int np, void *pp[5]);
00260 static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1 = 0,
00261 const char *s2 = 0, const char *s3 = 0, const char *s4 = 0);
00262 static void Form(XrdOucString &s, const char *fmt, int i0, int i1 = 0, int i2 = 0,
00263 int i3 = 0, int i4 = 0);
00264 static void Form(XrdOucString &s, const char *fmt, void *p0, void *p1 = 0, void *p2 = 0,
00265 void *p3 = 0, void *p4 = 0);
00266 static void Form(XrdOucString &s, const char *fmt, int i0, const char *s0,
00267 const char *s1 = 0, const char *s2 = 0, const char *s3 = 0);
00268 static void Form(XrdOucString &s, const char *fmt, const char *s0,
00269 int i0, int i1 = 0, int i2 = 0, int i3 = 0);
00270 static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1,
00271 int i0, int i1, int i2);
00272 static void Form(XrdOucString &s, const char *fmt, int i0, int i1,
00273 const char *s0, const char *s1, const char *s2);
00274 static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1,
00275 const char *s2, int i0, int i1 = 0);
00276 static void Form(XrdOucString &s, const char *fmt, int i0, int i1, int i2,
00277 const char *s0, const char *s1);
00278
00279 static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1, const char *s2,
00280 const char *s3, int i1);
00281 static void Form(XrdOucString &s, const char *fmt, int i0, int i1, int i2, int i3, const char *s0);
00282
00283 static void Form(XrdOucString &s, const char *fmt, int i0, int i1, void *p0);
00284 static void Form(XrdOucString &s, const char *fmt, int i0, int i1, int i2, void *p0);
00285 static void Form(XrdOucString &s, const char *fmt, int i0, int i1, int i2, int i3, void *p0);
00286 static void Form(XrdOucString &s, const char *fmt, int i0, int i1, void *p0, int i2, int i3 = 0);
00287 static void Form(XrdOucString &s, const char *fmt, void *p0, int i0, int i1);
00288 static void Form(XrdOucString &s, const char *fmt, const char *s0, void *p0, int i0, int i1);
00289 static void Form(XrdOucString &s, const char *fmt, void *p0, const char *s0, int i0);
00290 static void Form(XrdOucString &s, const char *fmt, const char *s0, const char *s1, void *p0);
00291 static void Form(XrdOucString &s, const char *fmt, int i0, const char *s0, const char *s1,
00292 int i1, int i2 = 0);
00293 static void Form(XrdOucString &s, const char *fmt, int i0, const char *s0, int i1, int i2 = 0);
00294
00295 static int GetIDFromPath(const char *path, XrdOucString &emsg);
00296 static long int GetLong(char *str);
00297 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
00298 static int GetMacProcList(kinfo_proc **plist, int &nproc);
00299 #endif
00300 static int GetNumCPUs();
00301 static int GetGroupInfo(const char *grp, XrdProofGI &gi);
00302 static int GetGroupInfo(int gid, XrdProofGI &gi);
00303 static int GetProcesses(const char *pn, std::map<int,XrdOucString> *plist);
00304 static int GetUserInfo(const char *usr, XrdProofUI &ui);
00305 static int GetUserInfo(int uid, XrdProofUI &ui);
00306 static bool HasToken(const char *s, const char *tokens);
00307 static int KillProcess(int pid, bool forcekill, XrdProofUI ui, bool changeown);
00308 static int MvDir(const char *oldpath, const char *newpath);
00309 static int ParsePidPath(const char *path, XrdOucString &before, XrdOucString &after);
00310 static int ParseUsrGrp(const char *path, XrdOucString &usr, XrdOucString &grp);
00311 static const char *ProofRequestTypes(int type);
00312 static int ReadMsg(int fd, XrdOucString &msg);
00313 static int RmDir(const char *path);
00314 static int SymLink(const char *path, const char *link);
00315 static int Touch(const char *path, int opt = 0);
00316 static int VerifyProcessByID(int pid, const char *pname = "proofserv");
00317 static int Write(int fd, const void *buf, size_t nb);
00318 };
00319
00320
00321 #ifndef SafeDel
00322 #define SafeDel(x) { if (x) { delete x; x = 0; } }
00323 #endif
00324 #ifndef SafeDelArray
00325 #define SafeDelArray(x) { if (x) { delete[] x; x = 0; } }
00326 #endif
00327 #ifndef SafeFree
00328 #define SafeFree(x) { if (x) free(x); x = 0; }
00329 #endif
00330
00331 #ifndef INRANGE
00332 #define INRANGE(x,y) ((x >= 0) && (x < (int)y->size()))
00333 #endif
00334
00335 #ifndef DIGIT
00336 #define DIGIT(x) (x >= 48 && x <= 57)
00337 #endif
00338
00339 #ifndef LETTOIDX
00340 #define LETTOIDX(x, ilet) \
00341 if (x >= 97 && x <= 122) ilet = x - 96; \
00342 if (x >= 65 && x <= 90) ilet = x - 38;
00343 #endif
00344 #ifndef IDXTOLET
00345 #define IDXTOLET(ilet, x) \
00346 if ((ilet) >= 1 && (ilet) <= 26) x = (ilet) + 96; \
00347 if ((ilet) >= 27 && (ilet) <= 52) x = (ilet) + 38;
00348 #endif
00349
00350 #ifndef XPDSWAP
00351 #define XPDSWAP(a,b,t) { t = a ; a = b; b = t; }
00352 #endif
00353
00354 #ifndef XpdBadPGuard
00355 #define XpdBadPGuard(g,u) (!(g.Valid()) && (geteuid() != (uid_t)u))
00356 #endif
00357
00358 #undef MHEAD
00359 #define MHEAD "--- Proofd: "
00360
00361 #undef TRACELINK
00362 #define TRACELINK fLink
00363
00364 #undef RESPONSE
00365 #define RESPONSE fResponse
00366
00367 #ifndef XPDFORM
00368 #define XPDFORM XrdProofdAux::Form
00369 #endif
00370
00371 #endif