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 status = initAgent (RULE_ENGINE_INIT_CACHE, &rsComm);
00124 if (status < 0) {
00125 cleanupAndExit (status);
00126 }
00127
00128 if (ruleExecId != NULL) {
00129 status = reServerSingleExec (&rsComm, ruleExecId, jobType);
00130 if (status >= 0) {
00131 exit (0);
00132 } else {
00133 exit (1);
00134 }
00135 } else {
00136 reServerMain (&rsComm, logDir);
00137 }
00138 cleanupAndExit (status);
00139
00140 exit (0);
00141 }
00142
00143 int usage (char *prog)
00144 {
00145 fprintf(stderr, "Usage: %s [-sSv] [j jobID] [-t jobType] [-D logDir] \n",prog);
00146 fprintf(stderr, "-s Run like a client process - no fork\n");
00147 fprintf(stderr, "-S Run like a daemon server process - forked\n");
00148 fprintf(stderr, "-j jobID Run a single job\n");
00149 fprintf(stderr, "-t jobType An integer for job type. \n");
00150 return 0;
00151 }
00152
00153 void
00154 reServerMain (rsComm_t *rsComm, char* logDir )
00155 {
00156 int status = 0;
00157 genQueryOut_t *genQueryOut = NULL;
00158 time_t endTime;
00159 int runCnt;
00160 reExec_t reExec;
00161 int repeatedQueryErrorCount=0;
00162
00163 initReExec (rsComm, &reExec);
00164 LastRescUpdateTime = time (NULL);
00165 while (1) {
00166 #ifndef windows_platform
00167 #ifndef IRODS_SYSLOG
00168 chkLogfileName (logDir, RULE_EXEC_LOGFILE);
00169 #endif
00170 #endif
00171 chkAndResetRule (rsComm);
00172 rodsLog (LOG_NOTICE,
00173 "reServerMain: checking the queue for jobs");
00174 status = getReInfo (rsComm, &genQueryOut);
00175 if (status < 0) {
00176 if (status != CAT_NO_ROWS_FOUND) {
00177 rodsLog (LOG_ERROR,
00178 "reServerMain: getReInfo error. status = %d", status);
00179 #ifdef ORA_ICAT // JMC - backport 4520
00180
00181
00182
00183
00184
00185 if (repeatedQueryErrorCount>3) {
00186 disconnectRcat (rsComm);
00187 repeatedQueryErrorCount=0;
00188 }
00189 #endif
00190 } else {
00191 repeatedQueryErrorCount++;
00192 }
00193 reSvrSleep (rsComm);
00194 continue;
00195 } else {
00196 repeatedQueryErrorCount=0;
00197 }
00198 endTime = time (NULL) + RE_SERVER_EXEC_TIME;
00199 runCnt = runQueuedRuleExec (rsComm, &reExec, &genQueryOut, endTime, 0);
00200 if (runCnt > 0 ||
00201 (genQueryOut != NULL && genQueryOut->continueInx > 0)) {
00202
00203 svrCloseQueryOut (rsComm, genQueryOut);
00204 freeGenQueryOut (&genQueryOut);
00205 status = getReInfo (rsComm, &genQueryOut);
00206 if (status < 0) {
00207 reSvrSleep (rsComm);
00208 continue;
00209 }
00210 }
00211
00212
00213
00214 runCnt =
00215 runQueuedRuleExec (rsComm, &reExec, &genQueryOut, endTime,
00216 RE_FAILED_STATUS);
00217 svrCloseQueryOut (rsComm, genQueryOut);
00218 freeGenQueryOut (&genQueryOut);
00219 if (runCnt > 0 ||
00220 (genQueryOut != NULL && genQueryOut->continueInx > 0)) {
00221 continue;
00222 } else {
00223
00224 reSvrSleep (rsComm);
00225 }
00226 }
00227 }
00228
00229 int
00230 reSvrSleep (rsComm_t *rsComm)
00231 {
00232 int status;
00233 rodsServerHost_t *rodsServerHost = NULL;
00234
00235 if ((status = disconnRcatHost (rsComm, MASTER_RCAT,
00236 rsComm->myEnv.rodsZone)) == LOCAL_HOST) {
00237 #ifdef RODS_CAT
00238 #ifndef ORA_ICAT
00239
00240
00241 disconnectRcat (rsComm);
00242 if (status < 0) {
00243 rodsLog (LOG_ERROR,
00244 "reSvrSleep: disconnectRcat error. status = %d", status);
00245 }
00246 #endif
00247 #endif
00248 }
00249 rodsSleep (RE_SERVER_SLEEP_TIME, 0);
00250
00251 if ((status = getAndConnRcatHost (rsComm, MASTER_RCAT,
00252 rsComm->myEnv.rodsZone, &rodsServerHost)) == LOCAL_HOST) {
00253 #ifdef RODS_CAT
00254 status = connectRcat (rsComm);
00255 if (status < 0) {
00256 rodsLog (LOG_ERROR,
00257 "reSvrSleep: connectRcat error. status = %d", status);
00258 }
00259 #endif
00260 }
00261 return (status);
00262 }
00263
00264 int
00265 chkAndResetRule (rsComm_t *rsComm)
00266 {
00267 char *configDir;
00268 char rulesFileName[MAX_NAME_LEN];
00269 int status;
00270 uint mtime;
00271
00272 configDir = getConfigDir ();
00273 snprintf (rulesFileName, MAX_NAME_LEN, "%s/reConfigs/core.re",
00274 configDir);
00275 path p (rulesFileName);
00276 if (!exists (p)) {
00277 status = UNIX_FILE_STAT_ERR - errno;
00278 rodsLog (LOG_ERROR,
00279 "chkAndResetRule: unable to read rule config file %s, status = %d",
00280 rulesFileName, status);
00281 return (status);
00282 }
00283
00284 mtime = (uint) last_write_time (p);
00285
00286 if (CoreIrbTimeStamp == 0) {
00287
00288 CoreIrbTimeStamp = mtime;
00289 return (0);
00290 }
00291
00292 if (mtime > CoreIrbTimeStamp) {
00293
00294 rodsLog (LOG_NOTICE,
00295 "chkAndResetRule: reconf file %s has been changed. re-initializing",
00296 rulesFileName);
00297 CoreIrbTimeStamp = mtime;
00298 clearCoreRule();
00299
00300 status = initRuleEngine(RULE_ENGINE_TRY_CACHE, NULL, reRuleStr, reFuncMapStr, reVariableMapStr);
00301 if (status < 0) {
00302 rodsLog (LOG_ERROR,
00303 "chkAndResetRule: initRuleEngine error, status = %d", status);
00304 }
00305 }
00306 return status;
00307 }
00308