44#define getdents(fd, dirp, cnt) syscall(SYS_getdents, fd, dirp, cnt)
69 int XrdXrootdPrepare::scrubtime = 60*60;
70 int XrdXrootdPrepare::scrubkeep = 60*60*24;
71 char *XrdXrootdPrepare::LogDir = 0;
72 int XrdXrootdPrepare::LogDirLen = 0;
73const char *XrdXrootdPrepare::TraceID =
"Prepare";
80 bool nomsg) :
XrdJob(
"Prep log scrubber")
101 if (!LogDir)
return -1;
106 {
if (!(pargs.dirP =
opendir((
const char *)LogDir)))
107 {
eDest->
Emsg(
"List", errno,
"open prep log directory", LogDir);
110 if (pargs.
reqid) pargs.reqlen = strlen(pargs.
reqid);
111 if (pargs.
user) pargs.usrlen = strlen(pargs.
user);
117 while((dp =
readdir(pargs.dirP)))
118 {
if (!(up = (
char *) index((
const char *)dp->d_name,
'_')))
continue;
119 if (pargs.reqlen && strncmp(dp->d_name, pargs.
reqid, pargs.reqlen))
122 if (!up || strcmp((
const char *)up+1,(
const char *)pargs.
user))
124 strcpy(path, (
const char *)LogDir);
125 strcpy(path+LogDirLen, (
const char *)dp->d_name);
126 if (
stat((
const char *)path, &buf))
continue;
128 if ((up = (
char *) index((
const char *)(up+1), (
int)
'_'))) *up =
' ';
130 if ((up = (
char *) index((
const char *)(up+1), (
int)
'_'))) *up =
' ';
132 return snprintf(resp, resplen-1,
"%s %lld",
133 dp->d_name, (
long long) buf.st_mtime);
139 eDest->
Emsg(
"List", errno,
"read prep log directory", LogDir);
142 return (rc ? -1 : 0);
151 int rc, pnum = 0, xfd;
153 char buff[2048], blink[2048];
154 struct iovec iovec[2];
162 while(tp) {pnum++; tp = tp->
next;}
166 snprintf(buff,
sizeof(buff)-1,
"%s%s_%s_%d_%d", LogDir,
171 if ((xfd =
open(buff, O_WRONLY|O_CREAT|O_TRUNC,0644)) < 0)
172 {
eDest->
Emsg(
"Log", errno,
"open prep log file", buff);
178 iovec[1].iov_base = (
char *)
" ";
179 iovec[1].iov_len = 1;
182 {
if (tp->
next == 0) iovec[1].iov_base = (
char *)
"\n";
183 iovec[0].iov_base = tp->
text;
184 iovec[0].iov_len = strlen(tp->
text);
185 do {rc =
writev(xfd, (
const struct iovec *)iovec, 2);}
186 while(rc < 0 && errno == EINTR);
188 {
eDest->
Emsg(
"Log", errno,
"write prep log file", buff);
198 strcpy(blink, LogDir);
200 if (symlink((
const char *)buff, (
const char *)blink))
201 {
eDest->
Emsg(
"Log", errno,
"create symlink to prep log file", buff);
213 char path[MAXPATHLEN+256], buff[MAXPATHLEN+1];
217 if (!LogDir || strlen(reqid) > 255)
return;
221 strcpy(path, (
const char *)LogDir);
222 strcpy(&path[LogDirLen], (
const char *)reqid);
226 if ((rc = readlink((
const char *)path, buff,
sizeof(buff)-1)) < 0)
227 {
if (errno != ENOENT)
eDest->
Emsg(
"Logdel",errno,
"read symlink",path);
234 if (
unlink((
const char *)buff)
235 && errno != ENOENT)
eDest->
Emsg(
"Logdel",errno,
"remove",buff);
237 if (
unlink((
const char *)path)
238 && errno != ENOENT)
eDest->
Emsg(
"Logdel", errno,
"remove", path);
249 char path[MAXPATHLEN+264];
254 if (!LogDir)
return -ENOTSUP;
258 strcpy(path, (
const char *)LogDir);
259 strcpy(path+LogDirLen, reqid);
263 if (
stat((
const char *)path, &buf))
return -errno;
268 if ((fd =
open((
const char *)path, O_RDONLY)) < 0)
return -errno;
279 time_t stale = time(0) - scrubkeep;
280 char *up, path[2048], *fn = path+LogDirLen;
290 if (!(prepD =
opendir((
const char *)LogDir)))
291 {
eDest->
Emsg(
"Scrub", errno,
"open prep log directory", LogDir);
294 strcpy(path, (
const char *)LogDir);
300 {
if (!(up = (
char *) index((
const char *)dp->d_name,
'_')))
continue;
301 strcpy(fn, (
const char *)dp->d_name);
302 if (
stat((
const char *)path, &buf))
continue;
303 if (buf.st_mtime <= stale)
304 {
TRACE(
DEBUG,
"Scrub removed stale prep log " <<path);
305 unlink((
const char *)path);
306 *(fn+(up-dp->d_name)) =
'\0';
307 unlink((
const char *)path);
315 eDest->
Emsg(
"List", errno,
"read prep log directory", LogDir);
324{
if (stime > 0) scrubtime = stime;
325 if (keep > 0) scrubkeep = keep;
341 if (
access((
const char *)ldir, X_OK | W_OK | R_OK) ||
stat(ldir, &buf))
343 if ((buf.st_mode & S_IFMT) != S_IFDIR)
return -ENOTDIR;
347 if (LogDir) free(LogDir);
351 if (path[plen-1] !=
'/') path[plen++] =
'/';
356 LogDir = strdup(path);
357 LogDirLen = strlen(LogDir);
static XrdSysError eDest(0,"crypto_")
XrdSysTrace XrdXrootdTrace
void Schedule(XrdJob *jp)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
static int List(XrdXrootdPrepArgs &pargs, char *resp, int resplen)
static void Log(XrdXrootdPrepArgs &pargs)
static void Logdel(char *reqid)
static int setParms(int stime, int skeep)
XrdXrootdPrepare(XrdSysError *lp, XrdScheduler *sp, bool nomsg)
static int Open(const char *reqid, int &fsz)