ROOT  6.06/09
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 
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 
192  XrdProofdProtocol *p, int pid, XrdOucString &emsg);
194  unsigned int seq, XrdOucString &emsg);
195 // int CreateFork(XrdProofdProtocol *p);
197  void *input, const char *envfn, const char *rcfn);
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);
207  XrdOucString &tag, XrdOucString &ord,
208  XrdOucString &cffile, XrdOucString &uenvs,
209  int &intwait);
211  XrdProofdResponse *r, unsigned short &sid);
213  int ResolveSession(const char *fpid);
214 
215  void SendErrLog(const char *errlog, XrdProofdResponse *r);
216 
217  // Session Admin path management
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 
233 
234  XrdSysRecMutex *Mutex() { return &fMutex; }
235 
236  int Config(bool rcf = 0);
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);
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; }
280 
281  int CleanupProofServ(bool all = 0, const char *usr = 0);
282 
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);
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);
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
int CreateSockPath(XrdProofdProofServ *xps, XrdProofdProtocol *p, unsigned int seq, XrdOucString &emsg)
Create the socket path for the starting session Return 0 on success, -1 on error (error message in 'e...
#define XrdSysLogger
Definition: XpdSysLogger.h:8
int SetupProtocol(XrdNetPeer &peerpsrv, XrdProofdProofServ *xps, XrdOucString &e)
Setup the protocol object serving the peer described by 'peerpsrv'.
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)
int BroadcastPriorities()
Broadcast priorities to the active sessions.
XrdOucHash< XrdProofdProofServ > fSessions
void SetReconnectTime(bool on=1)
Change reconnecting status.
int CurrentSessions(bool recalculate=0)
Return the number of current sessions (top masters)
static int ToVersCode(int ver, bool hex=0)
Transform version number ver (format patch + 100*minor + 10000*maj, e.g.
XrdOucString fGroups
unsigned int hex
Definition: math.cpp:442
int Process(XrdProofdProtocol *p)
Process manager request.
void Print(const char *what)
Print the content of this env.
const double pi
std::list< XrdProofdProofServ * > * ActiveSessions()
int CleanupProofServ(bool all=0, const char *usr=0)
Cleanup (kill) all 'proofserv' processes from the process table.
bool IsClientRecovering(const char *usr, const char *grp, int &deadline)
Returns true (an the recovering deadline) if the client has sessions in recovering state; returns fal...
void ParseCreateBuffer(XrdProofdProtocol *p, XrdProofdProofServ *xps, XrdOucString &tag, XrdOucString &ord, XrdOucString &cffile, XrdOucString &uenvs, int &intwait)
Extract relevant quantities from the buffer received during a create request.
int SetUserOwnerships(XrdProofdProtocol *p, const char *ord, const char *stag)
Set user ownerships on some critical files or directories.
int SetProofServEnv(XrdProofdProtocol *p, void *in)
Set environment for proofserv.
std::map< XrdProofdProtocol *, int > fDestroyTimes
#define XrdSysRecMutex
Definition: XrdSysToOuc.h:18
bool IsSessionSocket(const char *fpid)
Checks is fpid is the path of a session UNIX socket Returns TRUE is yes; cleans the socket if the ses...
int CreateProofServRootRc(XrdProofdProtocol *p, void *input, const char *rcfn)
Create in 'rcfn' the rootrc file for the proofserv being created return 0 on success, -1 on error.
void UpdateCounter(int t, int n)
int RmSession(const char *fpid)
Remove session file from the terminated sessions area.
int DeleteFromSessions(const char *pid)
Delete from the hash list the session with ID pid.
void ResolveKeywords(XrdOucString &s, ProofServEnv_t *in)
Resolve some keywords in 's' , ,
bool Alive(XrdProofdProtocol *p)
Check destroyed status.
int AddSession(XrdProofdProtocol *p, XrdProofdProofServ *s)
Add new active session.
XrdSysSemWait * ProcessSem()
XrdSecCredsSaver_t fCredsSaver
int CreateProofServEnvFile(XrdProofdProtocol *p, void *input, const char *envfn, const char *rcfn)
Create in 'rcfn' the rootrc file for the proofserv being created return 0 on success, -1 on error.
void ExtractEnv(char *, XrdOucStream *, XrdOucString &users, XrdOucString &groups, XrdOucString &rcval, XrdOucString &rcnam, int &smi, int &smx, int &vmi, int &vmx, bool &hex)
Extract env information from the stream 'cfg'.
const char * ord
Definition: TXSlave.cxx:46
int Destroy(XrdProofdProtocol *p)
Handle a request to shutdown an existing session.
void FormFileNameInSessionDir(XrdProofdProtocol *p, XrdProofdProofServ *xps, const char *sessiondir, const char *extension, XrdOucString &outfn)
int CreateAdminPath(XrdProofdProofServ *xps, XrdProofdProtocol *p, int pid, XrdOucString &emsg)
Create the admin path for the starting session Return 0 on success, -1 on error (error message in 'em...
XrdProofSessionInfo(XrdProofdClient *c, XrdProofdProofServ *s)
Construct from 'c' and 's'.
XrdOucString fName
XpdSrvMgrCreateCnt(XrdProofdProofServMgr *m, int t)
std::list< XpdEnv > fProofServEnvs
bool IsReconnecting()
Return true if in reconnection state, i.e.
int CheckActiveSessions(bool verify=1)
Go through the active sessions admin path and make sure sessions are alive.
#define XrdSysSemWait
Definition: XpdSysSemWait.h:8
int TouchSession(const char *fpid, const char *path=0)
Update the access time for the session pid file to the current time.
std::list< XrdProofdProofServ * > fActiveSessions
void DisconnectFromProofServ(int pid)
Change reconnecting status.
int Matches(const char *usr, const char *grp, int ver=-1)
Check if this env applies to 'usr', 'grp, 'ver'.
static const char * what
Definition: stlLoader.cc:6
std::list< XpdClientSessions * > * fRecoverClients
XrdProofdProofServMgr * fMgr
void FillProofServ(XrdProofdProofServ &s, XrdROOTMgr *rmgr)
Fill 's' fields using the stored info.
void RegisterDirectives()
Register directives for configuration.
std::list< XrdProofdProofServ * > fProofServs
int Config(bool rcf=0)
Run configuration and parse the entered config directives.
int Attach(XrdProofdProtocol *p)
Handle a request to attach to an existing session.
std::list< XpdEnv > fProofServRCs
ROOT::R::TRInterface & r
Definition: Object.C:4
int ResolveSession(const char *fpid)
Handle a request to recover a session after stop&restart.
#define XrdSysMutexHelper
Definition: XrdSysToOuc.h:17
XpdClientSessions(XrdProofdClient *c=0)
#define XrdSysError
Definition: XpdSysError.h:8
void Reset()
Reset the content.
int Recover(XpdClientSessions *cl)
Handle a request to recover a session after stop&restart for a specific client.
TMarker * m
Definition: textangle.C:8
int CleanClientSessions(const char *usr, int srvtype)
Go through the sessions admin path and clean all sessions belonging to 'usr'.
int MvSession(const char *fpid)
Move session file from the active to the terminated areas.
int PrepareSessionRecovering()
Go through the active sessions admin path and prepare reconnection of those still alive...
int VerifySession(const char *fpid, int to=-1, const char *path=0)
Check if the session is alive, i.e.
int operator==(const XpdClientSessions c)
#define PSMCNTOK(t)
int Detach(XrdProofdProtocol *p)
Handle a request to detach from an existing session.
XrdOucString fUsers
void GetTagDirs(int opt, XrdProofdProtocol *p, XrdProofdProofServ *xps, XrdOucString &sesstag, XrdOucString &topsesstag, XrdOucString &sessiondir, XrdOucString &sesswrkdir)
Determine the unique tag and relevant dirs for this session.
XrdProofdProofServ * fPS
const char * extension
Definition: civetweb.c:2467
int CheckTerminatedSessions()
Go through the terminated sessions admin path and make sure sessions they are gone.
void SendErrLog(const char *errlog, XrdProofdResponse *r)
Send content of errlog upstream asynchronously.
int CleanupLostProofServ()
Cleanup (kill) all 'proofserv' processes which lost control from their creator or controller daemon...
XrdOucString fSessionDir
XrdOucString fSessionTag
int SetProofServEnvOld(XrdProofdProtocol *p, void *in)
Set environment for proofserv; old version preparing the environment for proofserv protocol version <...
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)
XrdProofdProofServ * PrepareProofServ(XrdProofdProtocol *p, XrdProofdResponse *r, unsigned short &sid)
Allocate and prepare the XrdProofdProofServ object describing this session.
int DoDirectiveShutdown(char *, XrdOucStream *, bool)
Process 'shutdown' directive.
void FillEnvList(std::list< XpdEnv > *el, const char *nam, const char *val, const char *usrs=0, const char *grps=0, int smi=-1, int smx=-1, int vmi=-1, int vmx=-1, bool hex=0)
Fill env entry(ies) in the relevant list.
XrdProofdProofServMgr(XrdProofdManager *mgr, XrdProtocol_Config *pi, XrdSysError *e)
Constructor.
int Create(XrdProofdProtocol *p)
Handle a request to create a new session.
int DoDirective(XrdProofdDirective *d, char *val, XrdOucStream *cfg, bool rcf)
Update the priorities of the active sessions.
XrdOucString fTopSessionTag
int DoDirectivePutRc(char *, XrdOucStream *, bool)
Process 'putrc' directives.
XrdProofdProofServ * GetActiveSession(int pid)
Return active session with process ID pid, if any.
int ReadFromFile(const char *file)
Read content from 'file'.
int AcceptPeer(XrdProofdProofServ *xps, int to, XrdOucString &e)
Accept a callback from a starting-up server and setup the related protocol object.
int RecoverActiveSessions()
Accept connections from sessions still alive.
int DoDirectivePutEnv(char *, XrdOucStream *, bool)
Process 'putenv' directives.
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)
int SaveToFile(const char *file)
Save content to 'file'.
int DoDirectiveProofServMgr(char *, XrdOucStream *, bool)
Process 'proofswrvmgr' directive eg: xpd.proofswrvmgr checkfq:120 termto:100 verifyto:5 recoverto:20...
void BroadcastClusterInfo()
Broadcast cluster info to the active sessions.
int SetUserEnvironment(XrdProofdProtocol *p)
Set user environment: set effective user and group ID of the process to the ones of the owner of this...
XrdProofSessionInfo(const char *file)