Logo ROOT   6.08/07
Reference Guide
XrdProofdProofServMgr.h
Go to the documentation of this file.
1 // @(#)root/proofd:$Id$
2 // Author: G. Ganis Jan 2008
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_XrdProofdProofServMgr
13 #define ROOT_XrdProofdProofServMgr
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // XrdProofdProofServMgr //
18 // //
19 // Author: G. Ganis, CERN, 2008 //
20 // //
21 // Class managing proofserv sessions manager. //
22 // //
23 //////////////////////////////////////////////////////////////////////////
24 
25 #include <list>
26 #include <map>
27 
28 #include "XpdSysPthread.h"
29 
30 #include "XrdProofdXrdVers.h"
31 #ifndef ROOT_XrdFour
32 # include <sys/types.h>
33 # include <sys/socket.h>
34 # include "XrdNet/XrdNetPeer.hh"
35 #else
36 # include "XrdNet/XrdNetAddr.hh"
37 #endif
38 #include "XrdOuc/XrdOucHash.hh"
39 #include "XrdOuc/XrdOucString.hh"
40 
41 #include "XrdProofdConfig.h"
42 #include "XrdProofdProofServ.h"
43 
44 class XrdOucStream;
45 class XrdProtocol_Config;
46 class XrdProofdManager;
47 class XrdROOTMgr;
48 class XrdSysLogger;
49 
50 #define PSMMAXCNTS 3
51 #define PSMCNTOK(t) (t >= 0 && t < PSMMAXCNTS)
52 
53 // Security handle
54 typedef int (*XrdSecCredsSaver_t)(XrdSecCredentials *, const char *fn, const XrdProofUI &ui);
55 
56 // Aux structure for session set env inputs
57 typedef struct {
59  int fLogLevel;
60  XrdOucString fCfg;
61  XrdOucString fLogFile;
62  XrdOucString fSessionTag;
63  XrdOucString fTopSessionTag;
64  XrdOucString fSessionDir;
65  XrdOucString fWrkDir;
66  bool fOld;
68 
70 public:
73  std::list<XrdProofdProofServ *> fProofServs;
74  XpdClientSessions(XrdProofdClient *c = 0) : fClient(c) { }
75  int operator==(const XpdClientSessions c) { return (c.fClient == fClient) ? 1 : 0; }
76 };
77 
79 public:
80  time_t fLastAccess;
81  int fPid;
82  int fID;
83  int fSrvType;
85  int fStatus;
86  XrdOucString fUser;
87  XrdOucString fGroup;
88  XrdOucString fUnixPath;
89  XrdOucString fTag;
90  XrdOucString fAlias;
91  XrdOucString fLogFile;
92  XrdOucString fOrdinal;
93  XrdOucString fUserEnvs;
94  XrdOucString fROOTTag;
95  XrdOucString fAdminPath;
97 
99  XrdProofSessionInfo(const char *file) { ReadFromFile(file); }
100 
101  void FillProofServ(XrdProofdProofServ &s, XrdROOTMgr *rmgr);
102  int ReadFromFile(const char *file);
103  void Reset();
104  int SaveToFile(const char *file);
105 };
106 
107 class XpdEnv {
108 public:
109  XrdOucString fName;
110  XrdOucString fEnv;
111  XrdOucString fUsers;
112  XrdOucString fGroups;
113  int fSvnMin;
114  int fSvnMax;
115  int fVerMin;
116  int fVerMax;
117  XpdEnv(const char *n, const char *env, const char *usr = 0, const char *grp = 0,
118  int smi = -1, int smx = -1, int vmi = -1, int vmx = -1) :
119  fName(n), fEnv(env), fUsers(usr), fGroups(grp),
120  fSvnMin(smi), fSvnMax(smx), fVerMin(vmi), fVerMax(vmx) { }
121  void Reset(const char *n, const char *env, const char *usr = 0, const char *grp = 0,
122  int smi = -1, int smx = -1, int vmi = -1, int vmx = -1) {
123  fName = n; fEnv = env; fUsers = usr; fGroups = grp;
124  fSvnMin = smi; fSvnMax = smx; fVerMin = vmi; fVerMax = vmx; }
125  int Matches(const char *usr, const char *grp, int ver = -1);
126  void Print(const char *what);
127  static int ToVersCode(int ver, bool hex = 0);
128 };
129 
131 
135  XrdSysRecMutex fEnvsMutex; // Protect usage of envs lists
136  XrdSysSemWait fForkSem; // To serialize fork requests
137  XrdSysSemWait fProcessSem; // To serialize process requests
138  XrdSysLogger *fLogger; // Error logger
139  int fInternalWait; // Timeout on replies from proofsrv
140  XrdOucString fProofPlugin; // String identifying the plug-in to be loaded, e.g. "condor:"
141  std::list<XpdEnv> fProofServEnvs; // Additional envs to be exported before proofserv
142  std::list<XpdEnv> fProofServRCs; // Additional rcs to be passed to proofserv
143 
144  int fShutdownOpt; // What to do when a client disconnects
145  int fShutdownDelay; // Delay shutdown by this (if enabled)
146 
148 
157  bool fUseFork; // If true, use fork to start proofserv
158  XrdOucString fParentExecs; // List of possible 'proofserv' parent names
159 
160  int fCounters[PSMMAXCNTS]; // Internal counters (see enum PSMCounters)
161  int fCurrentSessions; // Number of sessions (top masters)
162 
163  unsigned int fSeqSessionN; // Sequential number for sessions created by this instance
164 
165  int fNextSessionsCheck; // Time of next sessions check
166 
167  XrdOucString fActiAdminPath; // Active sessions admin area
168  XrdOucString fTermAdminPath; // Terminated sessions admin area
169 
170  XrdOucHash<XrdProofdProofServ> fSessions; // List of sessions
171  std::list<XrdProofdProofServ *> fActiveSessions; // List of active sessions (non-idle)
172  std::list<XpdClientSessions *> *fRecoverClients; // List of client potentially recovering
173 
174  XrdSecCredsSaver_t fCredsSaver; // If defined, function to be used to save the credentials
175 
176  std::map<XrdProofdProtocol*,int> fDestroyTimes; // Tracks destroyed sessions
177 
178  int DoDirectiveProofServMgr(char *, XrdOucStream *, bool);
179  int DoDirectivePutEnv(char *, XrdOucStream *, bool);
180  int DoDirectivePutRc(char *, XrdOucStream *, bool);
181  int DoDirectiveShutdown(char *, XrdOucStream *, bool);
182  void ExtractEnv(char *, XrdOucStream *,
183  XrdOucString &users, XrdOucString &groups,
184  XrdOucString &rcval, XrdOucString &rcnam,
185  int &smi, int &smx, int &vmi, int &vmx, bool &hex);
186  void FillEnvList(std::list<XpdEnv> *el, const char *nam, const char *val,
187  const char *usrs = 0, const char *grps = 0,
188  int smi = -1, int smx = -1, int vmi = -1, int vmx = -1, bool hex = 0);
189  unsigned int GetSeqSessionN() { XrdSysMutexHelper mhp(fMutex); return ++fSeqSessionN; }
190 
191  int CreateAdminPath(XrdProofdProofServ *xps,
192  XrdProofdProtocol *p, int pid, XrdOucString &emsg);
193  int CreateSockPath(XrdProofdProofServ *xps, XrdProofdProtocol *p,
194  unsigned int seq, XrdOucString &emsg);
195 // int CreateFork(XrdProofdProtocol *p);
196  int CreateProofServEnvFile(XrdProofdProtocol *p,
197  void *input, const char *envfn, const char *rcfn);
198  int CreateProofServRootRc(XrdProofdProtocol *p,
199  void *input, const char *rcfn);
200 #ifndef ROOT_XrdFour
201  int SetupProtocol(XrdNetPeer &peerpsrv,
202 #else
203  int SetupProtocol(XrdNetAddr &netaddr,
204 #endif
205  XrdProofdProofServ *xps, XrdOucString &e);
206  void ParseCreateBuffer(XrdProofdProtocol *p, XrdProofdProofServ *xps,
207  XrdOucString &tag, XrdOucString &ord,
208  XrdOucString &cffile, XrdOucString &uenvs,
209  int &intwait);
210  XrdProofdProofServ *PrepareProofServ(XrdProofdProtocol *p,
211  XrdProofdResponse *r, unsigned short &sid);
212  int PrepareSessionRecovering();
213  int ResolveSession(const char *fpid);
214 
215  void SendErrLog(const char *errlog, XrdProofdResponse *r);
216 
217  // Session Admin path management
218  int AddSession(XrdProofdProtocol *p, XrdProofdProofServ *s);
219  bool IsSessionSocket(const char *fpid);
220  int RmSession(const char *fpid);
221  int TouchSession(const char *fpid, const char *path = 0);
222  int VerifySession(const char *fpid, int to = -1, const char *path = 0);
223 
224  void ResolveKeywords(XrdOucString &s, ProofServEnv_t *in);
225  int SetUserOwnerships(XrdProofdProtocol *p, const char *ord, const char *stag);
226 
227 public:
228  XrdProofdProofServMgr(XrdProofdManager *mgr, XrdProtocol_Config *pi, XrdSysError *e);
230 
231  enum PSMProtocol { kSessionRemoval = 0, kClientDisconnect = 1, kCleanSessions = 2, kProcessReq = 3, kChgSessionSt = 4} ;
232  enum PSMCounters { kCreateCnt = 0, kCleanSessionsCnt = 1, kProcessCnt = 2} ;
233 
234  XrdSysRecMutex *Mutex() { return &fMutex; }
235 
236  int Config(bool rcf = 0);
237  int DoDirective(XrdProofdDirective *d,
238  char *val, XrdOucStream *cfg, bool rcf);
239  void RegisterDirectives();
240 
241  int CheckFrequency() const { return fCheckFrequency; }
242  int InternalWait() const { return fInternalWait; }
243  int VerifyTimeOut() const { return fVerifyTimeOut; }
244 
245  inline int NextSessionsCheck()
246  { XrdSysMutexHelper mhp(fMutex); return fNextSessionsCheck; }
247  inline void SetNextSessionsCheck(int t)
248  { XrdSysMutexHelper mhp(fMutex); fNextSessionsCheck = t; }
249 
250  bool IsReconnecting();
251  bool IsClientRecovering(const char *usr, const char *grp, int &deadline);
252  void SetReconnectTime(bool on = 1);
253 
254  bool Alive(XrdProofdProtocol *p);
255 
256  int Process(XrdProofdProtocol *p);
257  XrdSysSemWait *ProcessSem() { return &fProcessSem; }
258 
259  int AcceptPeer(XrdProofdProofServ *xps, int to, XrdOucString &e);
260  int Attach(XrdProofdProtocol *p);
261  int Create(XrdProofdProtocol *p);
262  int Destroy(XrdProofdProtocol *p);
263  int Detach(XrdProofdProtocol *p);
264  int Recover(XpdClientSessions *cl);
265 
266  void UpdateCounter(int t, int n) { if (PSMCNTOK(t)) {
267  XrdSysMutexHelper mhp(fMutex); fCounters[t] += n;
268  if (fCounters[t] < 0) fCounters[t] = 0;} }
269  int CheckCounter(int t) { int cnt = -1; if (PSMCNTOK(t)) {
270  XrdSysMutexHelper mhp(fMutex); cnt = fCounters[t];}
271  return cnt; }
272 
273  void BroadcastClusterInfo();
274  int BroadcastPriorities();
275  int CurrentSessions(bool recalculate = 0);
276  void DisconnectFromProofServ(int pid);
277 
278  std::list<XrdProofdProofServ *> *ActiveSessions() { return &fActiveSessions; }
279  XrdProofdProofServ *GetActiveSession(int pid);
280 
281  int CleanupProofServ(bool all = 0, const char *usr = 0);
282 
283  void FormFileNameInSessionDir(XrdProofdProtocol *p,
284  XrdProofdProofServ *xps,
285  const char *sessiondir,
286  const char *extension,
287  XrdOucString &outfn);
288 
289  void GetTagDirs(int opt, XrdProofdProtocol *p, XrdProofdProofServ *xps,
290  XrdOucString &sesstag, XrdOucString &topsesstag,
291  XrdOucString &sessiondir, XrdOucString &sesswrkdir);
292 
293  int SetProofServEnv(XrdProofdProtocol *p, void *in);
294  int SetProofServEnvOld(XrdProofdProtocol *p, void *in);
295  int SetUserEnvironment(XrdProofdProtocol *p);
296 
297  static int SetProofServEnv(XrdProofdManager *m, XrdROOT *r);
298 
299  inline XrdProofdPipe *Pipe() { return &fPipe; }
300 
301  // Checks run periodically by the cron job
302  int DeleteFromSessions(const char *pid);
303  int MvSession(const char *fpid);
304  int CheckActiveSessions(bool verify = 1);
305  int CheckTerminatedSessions();
306  int CleanClientSessions(const char *usr, int srvtype);
307  int CleanupLostProofServ();
308  int RecoverActiveSessions();
309 };
310 
312 public:
313  int fType;
315  XpdSrvMgrCreateCnt(XrdProofdProofServMgr *m, int t) : fType(t), fMgr(m)
316  { if (m && PSMCNTOK(t)) m->UpdateCounter(t,1); }
317  ~XpdSrvMgrCreateCnt() { if (fMgr && PSMCNTOK(fType)) fMgr->UpdateCounter(fType,-1); }
318 };
319 
321 public:
322  int *fCnt;
323  XpdSrvMgrCreateGuard(int *c = 0) { Set(c); }
324  ~XpdSrvMgrCreateGuard() { if (fCnt) (*fCnt)--; }
325  void Set(int *c) { fCnt = c; if (fCnt) (*fCnt)++;}
326 };
327 
328 #endif
#define XrdSysLogger
Definition: XpdSysLogger.h:8
void Reset(const char *n, const char *env, const char *usr=0, const char *grp=0, int smi=-1, int smx=-1, int vmi=-1, int vmx=-1)
XrdOucHash< XrdProofdProofServ > fSessions
XrdOucString fGroups
const double pi
std::list< XrdProofdProofServ * > * ActiveSessions()
return c
std::map< XrdProofdProtocol *, int > fDestroyTimes
#define XrdSysRecMutex
Definition: XrdSysToOuc.h:18
void UpdateCounter(int t, int n)
XrdSysSemWait * ProcessSem()
XrdSecCredsSaver_t fCredsSaver
XrdOucString fName
XpdSrvMgrCreateCnt(XrdProofdProofServMgr *m, int t)
std::list< XpdEnv > fProofServEnvs
#define XrdSysSemWait
Definition: XpdSysSemWait.h:8
std::list< XrdProofdProofServ * > fActiveSessions
static const char * what
Definition: stlLoader.cc:6
std::list< XpdClientSessions * > * fRecoverClients
XrdProofdProofServMgr * fMgr
std::list< XrdProofdProofServ * > fProofServs
std::list< XpdEnv > fProofServRCs
TRandom2 r(17)
#define XrdSysMutexHelper
Definition: XrdSysToOuc.h:17
XpdClientSessions(XrdProofdClient *c=0)
#define XrdSysError
Definition: XpdSysError.h:8
TMarker * m
Definition: textangle.C:8
void Reset(Detail::TBranchProxy *x)
int operator==(const XpdClientSessions c)
#define PSMCNTOK(t)
XrdOucString fUsers
XrdProofdProofServ * fPS
const char * extension
Definition: civetweb.c:5005
void Print(std::ostream &os, const OptionType &opt)
XrdOucString fSessionDir
XrdOucString fSessionTag
XpdEnv(const char *n, const char *env, const char *usr=0, const char *grp=0, int smi=-1, int smx=-1, int vmi=-1, int vmx=-1)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
Definition: file.py:1
XrdOucString fTopSessionTag
XrdOucString fEnv
#define PSMMAXCNTS
const Int_t n
Definition: legend1.C:16
XrdProofdClient * fClient
const char * cnt
Definition: TXMLSetup.cxx:75
int(* XrdSecCredsSaver_t)(XrdSecCredentials *, const char *fn, const XrdProofUI &ui)
XrdProofSessionInfo(const char *file)