48 : fChangeOwn(changeown), fUI(ui)
50 XPDLOC(CMGR,
"XrdProofdSandbox")
60 if (!defdir.endswith(
'/')) defdir +=
"/";
62 XrdOucString initus = ui.
fUser[0];
67 if (
fDir.find(
"<user>") == STR_NPOS) {
68 if (!
fDir.endswith(
'/'))
fDir +=
"/";
72 fDir.replace(
"<workdir>", defdir);
74 iph =
fDir.find(
"<effuser>");
75 int iu =
fDir.find(
"<u>");
76 int ius =
fDir.find(
"<user>");
78 if ((iph != STR_NPOS && iu < iph) || (iph == STR_NPOS)) iph = iu;
80 if ((iph != STR_NPOS && ius < iph) || (iph == STR_NPOS)) iph = ius;
83 fDir.replace(
"<u>", initus);
89 if (!
fDir.endswith(
'/'))
95 if (!
fDir.endswith(
'/'))
106 XrdOucString path, sb;
107 path.assign(
fDir, 0, iph - 1);
109 while ((from =
fDir.tokenize(sb, from,
'/')) != -1) {
112 fErrMsg +=
"unable to create work dir: ";
121 fErrMsg +=
"unable to create work dir: ";
129 const char *basicdirs[4] = {
"/cache",
"/packages",
"/.creds",
"/queries" };
131 int n = (full) ? 4 : 3;
132 for (i = 0; i <
n; i++) {
133 XrdOucString dir =
fDir;
136 fErrMsg +=
"unable to create dir: ";
159 XrdOucString ll(*lhs);
160 ll.erase(ll.rfind(
'-'));
161 ll.erase(0, ll.rfind(
'-')+1);
162 int tl = strtol(ll.c_str(), 0, 10);
165 XrdOucString rr(*rhs);
166 rr.erase(rr.rfind(
'-'));
167 rr.erase(0, rr.rfind(
'-')+1);
168 int tr = strtol(rr.c_str(), 0, 10);
171 return ((tl < tr) ? 0 : 1);
181 static void Sort(std::list<XrdOucString *> *lst)
192 XrdOucString **ta =
new XrdOucString *[lst->size()];
193 std::list<XrdOucString *>::iterator i;
195 for (i = lst->begin(); i != lst->end(); ++i)
199 XrdOucString *tmp = 0;
231 lst->push_back(ta[n]);
255 XPDLOC(CMGR,
"Sandbox::GetSessionDirs")
258 opt = (opt >= 0 && opt <= 3) ? opt : 0;
261 if ((opt < 3 && !sdirs) || (opt == 3 && !tag)) {
262 TRACE(XERR,
"invalid inputs");
266 TRACE(DBG,
"opt: "<<opt<<
", dir: "<<
fDir);
269 DIR *dir = opendir(
fDir.c_str());
271 TRACE(XERR,
"cannot open dir "<<
fDir<<
" (errno: "<<errno<<
")");
278 struct dirent *ent = 0;
279 while ((ent = (
struct dirent *)readdir(dir))) {
280 if (!strncmp(ent->d_name,
"session-", 8)) {
282 if (opt == 3 && tag->length() > 0) {
283 if (strstr(ent->d_name, tag->c_str())) {
289 XrdOucString fterm(
fDir.c_str());
291 fterm += ent->d_name;
292 fterm +=
"/.terminated";
293 int rc = access(fterm.c_str(), F_OK);
294 if ((opt == 1 && rc == 0) || (opt == 2 && rc != 0))
298 TRACE(HDBG,
"found entry "<<ent->d_name<<
", keep: "<<keep);
300 sdirs->push_back(
new XrdOucString(ent->d_name));
316 return ((opt == 3 && found) ? 1 : 0);
326 XPDLOC(CMGR,
"Sandbox::AddSession")
333 TRACE(DBG,
"tag:"<<tag);
337 TRACE(XERR,
"could not get privileges");
342 XrdOucString fn =
fDir;
346 FILE *fact = fopen(fn.c_str(),
"a+");
348 TRACE(XERR,
"cannot open file "<<fn<<
" for appending (errno: "<<errno<<
")");
353 lseek(fileno(fact), 0, SEEK_SET);
354 if (lockf(fileno(fact), F_LOCK, 0) == -1) {
355 TRACE(XERR,
"cannot lock file "<<fn<<
" (errno: "<<errno<<
")");
363 std::list<XrdOucString *> actln;
365 while (fgets(ln,
sizeof(ln), fact)) {
367 if (ln[strlen(ln)-1] ==
'\n')
368 ln[strlen(ln)-1] =
'\0';
370 if (strlen(ln) <= 0 || ln[0] ==
'#')
379 lseek(fileno(fact), 0, SEEK_END);
380 fprintf(fact,
"%s\n", tag);
384 lseek(fileno(fact), 0, SEEK_SET);
385 if (lockf(fileno(fact), F_ULOCK, 0) == -1)
386 TRACE(XERR,
"cannot unlock file "<<fn<<
" (errno: "<<errno<<
")");
403 XPDLOC(CMGR,
"Sandbox::GuessTag")
405 TRACE(DBG,
"tag: "<<tag);
408 bool last = (tag ==
"last") ? 1 : 0;
410 if (!last && tag.length() > 0) {
412 XrdOucString fn =
fDir;
416 FILE *fact = fopen(fn.c_str(),
"a+");
419 if (lockf(fileno(fact), F_LOCK, 0) == 0) {
422 while (fgets(ln,
sizeof(ln), fact)) {
424 if (ln[strlen(ln)-1] ==
'\n')
425 ln[strlen(ln)-1] =
'\0';
427 if (strlen(ln) <= 0 || ln[0] ==
'#')
430 if (!strstr(ln, tag.c_str())) {
437 lseek(fileno(fact), 0, SEEK_SET);
438 if (lockf(fileno(fact), F_ULOCK, 0) == -1)
439 TRACE(DBG,
"cannot unlock file "<<fn<<
" ; fact: "<<fact<<
440 ", fd: "<< fileno(fact) <<
" (errno: "<<errno<<
")");
443 TRACE(DBG,
"cannot lock file: "<<fn<<
" ; fact: "<<fact<<
444 ", fd: "<< fileno(fact) <<
" (errno: "<<errno<<
")");
450 TRACE(DBG,
"cannot open file "<<fn<<
451 " for reading (errno: "<<errno<<
")");
458 std::list<XrdOucString *> staglst;
465 found = (rc == 1) ? 1 : 0;
467 if (!found && staglst.size() > 0) {
470 tag = staglst.front()->c_str();
476 std::list<XrdOucString *>::iterator i;
477 for (i = staglst.begin(); i != staglst.end(); i++) {
492 tag.replace(
"session-",
"");
494 TRACE(DBG,
"tag "<<tag<<
" not found in dir");
499 return ((found) ? 0 : -1);
510 XPDLOC(CMGR,
"Sandbox::RemoveSession")
516 TRACE(XERR,
"invalid input");
519 TRACE(DBG,
"tag:"<<tag);
523 TRACE(XERR,
"could not get privileges");
528 XrdOucString fna =
fDir;
532 FILE *fact = fopen(fna.c_str(),
"a+");
534 TRACE(XERR,
"cannot open file "<<fna<<
" (errno: "<<errno<<
")");
539 if (lockf(fileno(fact), F_LOCK, 0) == -1) {
540 TRACE(XERR,
"cannot lock file "<<fna<<
" (errno: "<<errno<<
")");
546 std::list<XrdOucString *> actln;
547 while (fgets(ln,
sizeof(ln), fact)) {
549 if (ln[strlen(ln)-1] ==
'\n')
550 ln[strlen(ln)-1] =
'\0';
552 if (strlen(ln) <= 0 || ln[0] ==
'#')
555 if (!strstr(ln, tag))
556 actln.push_back(
new XrdOucString(ln));
560 if (ftruncate(fileno(fact), 0) == -1) {
561 TRACE(XERR,
"cannot truncate file "<<fna<<
" (errno: "<<errno<<
")");
562 lseek(fileno(fact), 0, SEEK_SET);
563 if (lockf(fileno(fact), F_ULOCK, 0) != 0)
564 TRACE(XERR,
"cannot lockf file "<<fna<<
" (errno: "<<errno<<
")");
571 if (actln.size() > 0) {
573 std::list<XrdOucString *>::iterator i;
574 for (i = actln.begin(); i != actln.end(); ++i) {
575 fprintf(fact,
"%s\n", (*i)->c_str());
581 lseek(fileno(fact), 0, SEEK_SET);
582 if (lockf(fileno(fact), F_ULOCK, 0) == -1)
583 TRACE(DBG,
"cannot unlock file "<<fna<<
" (errno: "<<errno<<
")");
590 if (unlink(fna.c_str()) == -1)
591 TRACE(DBG,
"cannot unlink file "<<fna<<
" (errno: "<<errno<<
")");
594 XrdOucString fterm =
fDir;
595 fterm += (strstr(tag,
"session-")) ?
"/" :
"/session-";
597 fterm +=
"/.terminated";
599 FILE *ft = fopen(fterm.c_str(),
"w");
601 TRACE(XERR,
"cannot open file "<<fterm<<
" (errno: "<<errno<<
")");
619 XPDLOC(CMGR,
"Sandbox::TrimSessionDirs")
624 XrdOucString tobemv, fnact =
fDir;
625 fnact +=
"/.sessions";
626 FILE *f = fopen(fnact.c_str(),
"r");
629 while (fgets(ln,
sizeof(ln), f)) {
630 if (ln[strlen(ln)-1] ==
'\n')
631 ln[strlen(ln)-1] = 0;
632 char *p = strrchr(ln,
'-');
634 int pid = strtol(p+1, 0, 10);
646 TRACE(XERR,
"could not get privileges to trim directories");
651 if (tobemv.length() > 0) {
655 while ((from = tobemv.tokenize(tag, from, del)) != -1) {
657 TRACE(XERR,
"problems tagging session as old in sandbox");
665 std::list<XrdOucString *> staglst;
668 TRACE(XERR,
"cannot get list of dirs ");
671 TRACE(DBG,
"number of working dirs: "<<staglst.size());
674 std::list<XrdOucString *>::iterator i;
675 for (i = staglst.begin(); i != staglst.end(); ++i) {
676 TRACE(HDBG,
"found "<<(*i)->c_str());
682 XrdOucString *
s = staglst.back();
684 TRACE(HDBG,
"removing "<<s->c_str());
686 XrdOucString rmcmd =
"/bin/rm -rf ";
690 if (system(rmcmd.c_str()) == -1)
691 TRACE(XERR,
"cannot invoke system("<<rmcmd<<
") (errno: "<<errno<<
")");
static int GetUserInfo(const char *usr, XrdProofUI &ui)
Get information about user 'usr' in a thread safe way.
int TrimSessionDirs()
If the static fgMaxOldLogs > 0, logs for a fgMaxOldLogs number of sessions are kept in the sandbox; w...
#define TRACE(Flag, Args)
static int fgMaxOldSessions
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
int changeown(const std::string &path, uid_t u, gid_t g)
Change the ownership of 'path' to the entity described by {u,g}.
#define XpdBadPGuard(g, u)
int GuessTag(XrdOucString &tag, int ridx=1)
Guess session tag completing 'tag' (typically "-<pid>") by scanning the active session file or the se...
int AddSession(const char *tag)
Record entry for new proofserv session tagged 'tag' in the active sessions file (<SandBox>/.sessions).
int RemoveSession(const char *tag)
Move record for tag from the active sessions file to the old sessions file (<SandBox>/.sessions).
static int AssertDir(const char *path, XrdProofUI ui, bool changeown)
Make sure that 'path' exists and is owned by the entity described by 'ui'.
XrdProofdSandbox(XrdProofUI ui, bool full, bool changeown)
Assert existence on the sandbox for the user defined by 'ui'.
static constexpr double s
bool XpdSessionTagComp(XrdOucString *&lhs, XrdOucString *&rhs)
Compare times from session tag strings.
static XrdOucString fgWorkdir
static int VerifyProcessByID(int pid, const char *pname="proofserv")
Check if a process named 'pname' and process 'pid' is still in the process table. ...
int GetSessionDirs(int opt, std::list< XrdOucString *> *sdirs, XrdOucString *tag=0)
Scan the sandbox for sessions working dirs and return their sorted (according to creation time...