XRootD
XrdFrmPurgMain.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d F r m P u r g M a i n . c c */
4 /* */
5 /* (c) 2009 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 /* This is the "main" part of the frm_purge command. Syntax is:
32 */
33 static const char *XrdFrmOpts = ":bc:dfhk:l:n:O:s:S:Tvz";
34 static const char *XrdFrmUsage =
35 
36  " [-b] [-c <cfgfile>] [-d] [-f] [-k {num|sz{k|m|g}|sig] [-l [=]<fn>] [-n name]"
37  " [-O free[,hold]] [-s pidfile] [-S site] [-T] [-v] [-z] [<spaces>] [<paths>]\n";
38 /*
39 Where:
40 
41  -b Run as a true daemon process in the background.
42 
43  -c The configuration file. The default is '/opt/xrootd/etc/xrootd.cf'
44 
45  -d Turns on debugging mode.
46 
47  -f Fix orphaned files (i.e., lock and pin) by removing them.
48 
49  -k Keeps num log files or no more that sz log files.
50 
51  -l Specifies location of the log file. This may also come from the
52  XrdOucLOGFILE environmental variable.
53  By default, error messages go to standard error.
54 
55  -n The instance name.
56 
57  -O Run this one time only as a command. The parms are:
58  {free% | sz{k|m|g}[,hold]
59 
60  -s The pidfile name.
61 
62  -S The site name.
63 
64  -T Runs in test mode (no actual purge will occur).
65 
66  -v Verbose mode, typically prints each file purged and other details.
67 
68  o-t-a The one-time-args run this as a command only once. The args direct
69  the purging process. These may only be specified when -O specified.
70 
71  Syntax is: [space] path | space [path]
72 */
73 
74 /******************************************************************************/
75 /* i n c l u d e f i l e s */
76 /******************************************************************************/
77 
78 #include <unistd.h>
79 #include <cctype>
80 #include <cerrno>
81 #include <cstdlib>
82 #include <cstring>
83 #include <strings.h>
84 #include <cstdio>
85 #include <sys/param.h>
86 
87 #include "XrdFrc/XrdFrcTrace.hh"
88 #include "XrdFrm/XrdFrmConfig.hh"
89 #include "XrdFrm/XrdFrmPurge.hh"
90 #include "XrdNet/XrdNetOpts.hh"
91 #include "XrdNet/XrdNetSocket.hh"
92 #include "XrdOuc/XrdOucUtils.hh"
93 #include "XrdSys/XrdSysError.hh"
94 #include "XrdSys/XrdSysHeaders.hh"
95 #include "XrdSys/XrdSysLogger.hh"
96 #include "XrdSys/XrdSysPthread.hh"
97 #include "XrdSys/XrdSysTimer.hh"
98 #include "XrdSys/XrdSysUtils.hh"
99 
100 using namespace XrdFrc;
101 using namespace XrdFrm;
102 
103 /******************************************************************************/
104 /* G l o b a l V a r i a b l e s */
105 /******************************************************************************/
106 
109 
110 // The following is needed to resolve symbols for objects included from xrootd
111 //
115 
116 /******************************************************************************/
117 /* T h r e a d I n t e r f a c e s */
118 /******************************************************************************/
119 
120 void *mainServer(void *parg)
121 {
122 // int udpFD = *static_cast<int *>(parg);
123 // XrdFrmPurge::Server(udpFD);
124  return (void *)0;
125 }
126 
127 /******************************************************************************/
128 /* m a i n */
129 /******************************************************************************/
130 
131 int main(int argc, char *argv[])
132 {
134  extern int mainConfig();
135 
136 // Turn off sigpipe and host a variety of others before we start any threads
137 //
139 
140 // Set the default stack size here
141 //
142  if (sizeof(long) > 4) XrdSysThread::setStackSize((size_t)1048576);
143  else XrdSysThread::setStackSize((size_t)786432);
144 
145 // Perform configuration
146 //
147  Say.logger(&Logger);
148  XrdLog.logger(&Logger);
149  if (!Config.Configure(argc, argv, &mainConfig)) exit(4);
150 
151 // Fill out the dummy symbol to avoid crashes
152 //
154 
155 // Display configuration (deferred because mum might have been in effect)
156 //
157  if (!Config.isOTO || Config.Verbose) XrdFrmPurge::Display();
158 
159 // Now simply poke the server every so often
160 //
161  if (Config.isOTO) XrdFrmPurge::Purge();
162  else do {if (Config.StopPurge)
163  {int n = 0;
164  struct stat buf;
165  while(!stat(Config.StopPurge, &buf))
166  {if (!n--)
167  {Say.Emsg("PurgMain", Config.StopPurge,
168  "exists; purging suspended."); n = 12;}
170  }
171  }
173  XrdSysTimer::Snooze(Config.WaitPurge);
174  } while(1);
175 
176 // All done
177 //
178  exit(0);
179 }
180 
181 /******************************************************************************/
182 /* m a i n C o n f i g */
183 /******************************************************************************/
184 
186 {
187  XrdFrmConfig::Policy *pP = Config.dfltPolicy.Next;
188  XrdFrmConfig::VPInfo *vP = Config.VPList;
189  XrdNetSocket *udpSock;
190  pthread_t tid;
191  int retc, udpFD;
192 
193 // If test is in effect, remove the fix flag
194 //
195  if (Config.Test) Config.Fix = 0;
196 
197 // Go through the policy list and add each policy
198 //
199  while((pP = Config.dfltPolicy.Next))
200  {if (!XrdFrmPurge::Policy(pP->Sname))
202  pP->Hold, pP->Ext);
203  Config.dfltPolicy.Next = pP->Next;
204  delete pP;
205  }
206 
207 // Make sure we have a public policy
208 //
209  if (!XrdFrmPurge::Policy("public"))
210  XrdFrmPurge::Policy("public", Config.dfltPolicy.minFree,
211  Config.dfltPolicy.maxFree,
212  Config.dfltPolicy.Hold,
213  Config.dfltPolicy.Ext);
214 
215 // Now add any missing policies (we need one for every space)
216 //
217  while(vP)
218  {if (!XrdFrmPurge::Policy(vP->Name))
219  XrdFrmPurge::Policy(vP->Name, Config.dfltPolicy.minFree,
220  Config.dfltPolicy.maxFree,
221  Config.dfltPolicy.Hold,
222  Config.dfltPolicy.Ext);
223  vP = vP->Next;
224  }
225 
226 // Enable the appropriate spaces and over-ride config value
227 //
228  if (!XrdFrmPurge::Init(Config.spacList, Config.cmdFree, Config.cmdHold))
229  return 1;
230 
231 // We are done if this is a one-time-only call
232 //
233  if (Config.isOTO) return 0;
234 
235 // Get a UDP socket for the server
236 //
237  if (!(udpSock = XrdNetSocket::Create(&Say, Config.AdminPath,
238  "purg.udp", Config.AdminMode, XRDNET_UDPSOCKET))) return 1;
239  else {udpFD = udpSock->Detach(); delete udpSock;}
240 
241 // Start the Server thread
242 //
243  if ((retc = XrdSysThread::Run(&tid, mainServer, (void *)&udpFD,
244  XRDSYSTHREAD_BIND, "Server")))
245  {Say.Emsg("main", retc, "create server thread"); return 1;}
246 
247 // All done
248 //
249  return 0;
250 }
int main(int argc, char *argv[])
XrdOucTrace * XrdXrootdTrace
XrdSysError XrdLog(0, "")
void * mainServer(void *parg)
int mainConfig()
static const char * XrdFrmUsage
static const char * XrdFrmOpts
#define XRDNET_UDPSOCKET
Definition: XrdNetOpts.hh:79
int stat(const char *path, struct stat *buf)
#define XRDSYSTHREAD_BIND
static void Purge()
Definition: XrdFrmPurge.cc:586
static void Display()
Definition: XrdFrmPurge.cc:295
static int Init(XrdOucTList *sP=0, long long minV=-1, int hVal=-1)
Definition: XrdFrmPurge.cc:409
static XrdFrmPurge * Policy(const char *sname)
Definition: XrdFrmPurge.hh:52
static XrdNetSocket * Create(XrdSysError *Say, const char *path, const char *fn, mode_t mode, int isudp=0)
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
XrdSysLogger * logger(XrdSysLogger *lp=0)
Definition: XrdSysError.hh:141
static int Run(pthread_t *, void *(*proc)(void *), void *arg, int opts=0, const char *desc=0)
static void setStackSize(size_t stsz, bool force=false)
static void Snooze(int seconds)
Definition: XrdSysTimer.cc:168
static bool SigBlock()
Definition: XrdSysUtils.cc:188
XrdCmsConfig Config
XrdSysError Say
XrdFrmConfig Config
XrdSysLogger Logger
Definition: XrdGlobals.cc:47
XrdSysTrace XrdTrace
Definition: XrdTrace.hh:56
char Sname[XrdOssSpace::minSNbsz]