00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "irodsReServer.h"
00011 #include "reServerLib.h"
00012 #include "rsApiHandler.h"
00013 #include "rsIcatOpr.h"
00014 #include <syslog.h>
00015 #include "miscServerFunct.h"
00016 #include "reconstants.h"
00017
00018 extern int msiAdmClearAppRuleStruct(ruleExecInfo_t *rei);
00019
00020 int usage (char *prog);
00021
00022 int
00023 main(int argc, char **argv)
00024 {
00025 int status;
00026 int c;
00027 rsComm_t rsComm;
00028 int runMode = IRODS_SERVER;
00029 int flagval = 0;
00030 char *logDir = NULL;
00031 char *tmpStr;
00032 int logFd;
00033 char *ruleExecId = NULL;
00034 int jobType = 0;
00035
00036 ProcessType = RE_SERVER_PT;
00037
00038 #ifdef RUN_SERVER_AS_ROOT
00039 #ifndef windows_platform
00040 if (initServiceUser() < 0) {
00041 exit (1);
00042 }
00043 #endif
00044 #endif
00045
00046 #ifndef _WIN32
00047 signal(SIGINT, signalExit);
00048 signal(SIGHUP, signalExit);
00049 signal(SIGTERM, signalExit);
00050 signal(SIGUSR1, signalExit);
00051 signal(SIGPIPE, rsPipSigalHandler);
00052
00053
00054
00055 signal(SIGCHLD, SIG_DFL);
00056 #endif
00057
00058
00059 tmpStr = getenv (SP_LOG_SQL);
00060 if (tmpStr != NULL) {
00061 #ifdef IRODS_SYSLOG
00062 int j = atoi(tmpStr);
00063 rodsLogSqlReq(j);
00064 #else
00065 rodsLogSqlReq(1);
00066 #endif
00067 }
00068
00069
00070 tmpStr = getenv (SP_LOG_LEVEL);
00071 if (tmpStr != NULL) {
00072 int i;
00073 i = atoi(tmpStr);
00074 rodsLogLevel(i);
00075 } else {
00076 rodsLogLevel(LOG_NOTICE);
00077 }
00078
00079 #ifdef IRODS_SYSLOG
00080
00081 openlog("rodsReServer",LOG_ODELAY|LOG_PID,LOG_DAEMON);
00082 #endif
00083
00084 while ((c=getopt(argc, argv,"sSvD:j:t:")) != EOF) {
00085 switch (c) {
00086 case 's':
00087 runMode = SINGLE_PASS;
00088 break;
00089 case 'S':
00090 runMode = STANDALONE_SERVER;
00091 break;
00092 case 'v':
00093 flagval |= v_FLAG;
00094 break;
00095 case 'D':
00096 logDir = strdup (optarg);
00097 break;
00098 case 'j':
00099 runMode = SINGLE_PASS;
00100 ruleExecId = strdup (optarg);
00101 break;
00102 case 't':
00103 jobType = atoi (optarg);
00104 break;
00105 default:
00106 usage (argv[0]);
00107 exit (1);
00108 }
00109 }
00110
00111 status = initRsComm (&rsComm);
00112
00113 if (status < 0) {
00114 cleanupAndExit (status);
00115 }
00116
00117 if ((logFd = logFileOpen (runMode, logDir, RULE_EXEC_LOGFILE)) < 0) {
00118 exit (1);
00119 }
00120
00121 daemonize (runMode, logFd);
00122
00123 #ifdef RULE_ENGINE_N
00124 status = initAgent (RULE_ENGINE_INIT_CACHE, &rsComm);
00125 #else
00126 status = initAgent (&rsComm);
00127 #endif
00128 if (status < 0) {
00129 cleanupAndExit (status);
00130 }
00131
00132 if (ruleExecId != NULL) {
00133 status = reServerSingleExec (&rsComm, ruleExecId, jobType);
00134 if (status >= 0) {
00135 exit (0);
00136 } else {
00137 exit (1);
00138 }
00139 } else {
00140 reServerMain (&rsComm);
00141 }
00142 cleanupAndExit (status);
00143
00144 exit (0);
00145 }
00146
00147 int usage (char *prog)
00148 {
00149 fprintf(stderr, "Usage: %s [-sSv] [j jobID] [-t jobType] [-D logDir] \n",prog);
00150 fprintf(stderr, "-s Run like a client process - no fork\n");
00151 fprintf(stderr, "-S Run like a daemon server process - forked\n");
00152 fprintf(stderr, "-j jobID Run a single job\n");
00153 fprintf(stderr, "-t jobType An integer for job type. \n");
00154 return 0;
00155 }
00156
00157 void
00158 reServerMain (rsComm_t *rsComm)
00159 {
00160 int status = 0;
00161 genQueryOut_t *genQueryOut = NULL;
00162 time_t endTime;
00163 int runCnt;
00164 reExec_t reExec;
00165 int repeatedQueryErrorCount=0;
00166
00167 initReExec (rsComm, &reExec);
00168 LastRescUpdateTime = time (NULL);
00169 while (1) {
00170 chkAndResetRule (rsComm);
00171 rodsLog (LOG_NOTICE,
00172 "reServerMain: checking the queue for jobs");
00173 status = getReInfo (rsComm, &genQueryOut);
00174 if (status < 0) {
00175 if (status != CAT_NO_ROWS_FOUND) {
00176 rodsLog (LOG_ERROR,
00177 "reServerMain: getReInfo error. status = %d", status);
00178 #ifdef ORA_ICAT // JMC - backport 4520
00179
00180
00181
00182
00183
00184 if (repeatedQueryErrorCount>3) {
00185 disconnectRcat (rsComm);
00186 repeatedQueryErrorCount=0;
00187 }
00188 #endif
00189 } else {
00190 repeatedQueryErrorCount++;
00191 }
00192 reSvrSleep (rsComm);
00193 continue;
00194 } else {
00195 repeatedQueryErrorCount=0;
00196 }
00197 endTime = time (NULL) + RE_SERVER_EXEC_TIME;
00198 runCnt = runQueuedRuleExec (rsComm, &reExec, &genQueryOut, endTime, 0);
00199 if (runCnt > 0 ||
00200 (genQueryOut != NULL && genQueryOut->continueInx > 0)) {
00201
00202 svrCloseQueryOut (rsComm, genQueryOut);
00203 freeGenQueryOut (&genQueryOut);
00204 status = getReInfo (rsComm, &genQueryOut);
00205 if (status < 0) {
00206 reSvrSleep (rsComm);
00207 continue;
00208 }
00209 }
00210
00211
00212
00213 runCnt =
00214 runQueuedRuleExec (rsComm, &reExec, &genQueryOut, endTime,
00215 RE_FAILED_STATUS);
00216 svrCloseQueryOut (rsComm, genQueryOut);
00217 freeGenQueryOut (&genQueryOut);
00218 if (runCnt > 0 ||
00219 (genQueryOut != NULL && genQueryOut->continueInx > 0)) {
00220 continue;
00221 } else {
00222
00223 reSvrSleep (rsComm);
00224 }
00225 }
00226 }
00227
00228 int
00229 reSvrSleep (rsComm_t *rsComm)
00230 {
00231 int status;
00232 rodsServerHost_t *rodsServerHost = NULL;
00233
00234 if ((status = disconnRcatHost (rsComm, MASTER_RCAT,
00235 rsComm->myEnv.rodsZone)) == LOCAL_HOST) {
00236 #ifdef RODS_CAT
00237 #ifndef ORA_ICAT
00238
00239
00240 disconnectRcat (rsComm);
00241 if (status < 0) {
00242 rodsLog (LOG_ERROR,
00243 "reSvrSleep: disconnectRcat error. status = %d", status);
00244 }
00245 #endif
00246 #endif
00247 }
00248 rodsSleep (RE_SERVER_SLEEP_TIME, 0);
00249
00250 if ((status = getAndConnRcatHost (rsComm, MASTER_RCAT,
00251 rsComm->myEnv.rodsZone, &rodsServerHost)) == LOCAL_HOST) {
00252 #ifdef RODS_CAT
00253 status = connectRcat (rsComm);
00254 if (status < 0) {
00255 rodsLog (LOG_ERROR,
00256 "reSvrSleep: connectRcat error. status = %d", status);
00257 }
00258 #endif
00259 }
00260 return (status);
00261 }
00262
00263 int
00264 chkAndResetRule (rsComm_t *rsComm)
00265 {
00266 char *configDir;
00267 char rulesFileName[MAX_NAME_LEN];
00268 #ifndef USE_BOOST_FS
00269 struct stat statbuf;
00270 #endif
00271 int status;
00272 ruleExecInfo_t rei;
00273 uint mtime;
00274
00275 configDir = getConfigDir ();
00276 #ifdef RULE_ENGINE_N
00277 snprintf (rulesFileName, MAX_NAME_LEN, "%s/reConfigs/core.re",
00278 configDir);
00279 #else
00280 snprintf (rulesFileName, MAX_NAME_LEN, "%s/reConfigs/core.irb",
00281 configDir);
00282 #endif
00283 #ifdef USE_BOOST_FS
00284 path p (rulesFileName);
00285 if (!exists (p)) {
00286 #else
00287 status = stat (rulesFileName, &statbuf);
00288
00289 if (status != 0) {
00290 #endif
00291 status = UNIX_FILE_STAT_ERR - errno;
00292 rodsLog (LOG_ERROR,
00293 "chkAndResetRule: unable to read rule config file %s, status = %d",
00294 rulesFileName, status);
00295 return (status);
00296 }
00297
00298 #ifdef USE_BOOST_FS
00299 mtime = (uint) last_write_time (p);
00300 #else
00301 mtime = (uint) statbuf.st_mtime;
00302 #endif
00303
00304 if (CoreIrbTimeStamp == 0) {
00305
00306 CoreIrbTimeStamp = mtime;
00307 return (0);
00308 }
00309
00310 if (mtime > CoreIrbTimeStamp) {
00311
00312 rodsLog (LOG_NOTICE,
00313 "chkAndResetRule: reconf file %s has been changed. re-initializing",
00314 rulesFileName);
00315 CoreIrbTimeStamp = mtime;
00316 rei.rsComm = rsComm;
00317 clearCoreRule();
00318 #ifdef RULE_ENGINE_N
00319
00320 status = initRuleEngine(RULE_ENGINE_TRY_CACHE, NULL, reRuleStr, reFuncMapStr, reVariableMapStr);
00321 #else
00322 msiAdmClearAppRuleStruct (&rei);
00323 status = initRuleEngine(NULL, reRuleStr, reFuncMapStr, reVariableMapStr);
00324 #endif
00325 if (status < 0) {
00326 rodsLog (LOG_ERROR,
00327 "chkAndResetRule: initRuleEngine error, status = %d", status);
00328 }
00329 }
00330 return status;
00331 }
00332