00001 #include "ruleExecSubmit.h"
00002 #include "icatHighLevelRoutines.h"
00003
00004 int
00005 rsRuleExecSubmit (rsComm_t *rsComm, ruleExecSubmitInp_t *ruleExecSubmitInp,
00006 char **ruleExecId)
00007 {
00008 rodsServerHost_t *rodsServerHost;
00009 int status;
00010
00011 *ruleExecId = NULL;
00012
00013 if (ruleExecSubmitInp == NULL ||
00014 ruleExecSubmitInp->packedReiAndArgBBuf == NULL ||
00015 ruleExecSubmitInp->packedReiAndArgBBuf->len <= 0 ||
00016 ruleExecSubmitInp->packedReiAndArgBBuf->buf == NULL) {
00017 rodsLog(LOG_NOTICE,
00018 "rsRuleExecSubmit error. NULL input");
00019 return (SYS_INTERNAL_NULL_INPUT_ERR);
00020 }
00021
00022 #if 0
00023 status = getAndConnRcatHost(rsComm, MASTER_RCAT, NULL,
00024 &rodsServerHost);
00025 #else
00026 status = getAndConnReHost (rsComm, &rodsServerHost);
00027 #endif
00028 if (status < 0) {
00029 return(status);
00030 }
00031
00032 if (rodsServerHost->localFlag == LOCAL_HOST) {
00033 #ifdef RODS_CAT
00034 status = _rsRuleExecSubmit (rsComm, ruleExecSubmitInp);
00035 if (status >= 0) {
00036 *ruleExecId = strdup (ruleExecSubmitInp->ruleExecId);
00037 }
00038 #else
00039 rodsLog(LOG_NOTICE,
00040 "rsRuleExecSubmit error. ICAT is not configured on this host");
00041 return (SYS_NO_ICAT_SERVER_ERR);
00042 #endif
00043 } else {
00044 if (getValByKey (&ruleExecSubmitInp->condInput, EXEC_LOCALLY_KW) !=
00045 NULL) {
00046 rodsLog (LOG_ERROR,
00047 "rsRuleExecSubmit: reHost config error. reServer not running locally");
00048 return SYS_CONFIG_FILE_ERR;
00049 } else {
00050 addKeyVal (&ruleExecSubmitInp->condInput, EXEC_LOCALLY_KW, "");
00051 }
00052 status = rcRuleExecSubmit(rodsServerHost->conn, ruleExecSubmitInp,
00053 ruleExecId);
00054 }
00055 if (status < 0) {
00056 rodsLog (LOG_ERROR,
00057 "rsRuleExecSubmit: rcRuleExecSubmit failed, status = %d",
00058 status);
00059 }
00060 return (status);
00061 }
00062
00063 int
00064 _rsRuleExecSubmit (rsComm_t *rsComm, ruleExecSubmitInp_t *ruleExecSubmitInp)
00065 {
00066 int reiFd;
00067 int status;
00068
00069
00070
00071 while (1) {
00072 status = getReiFilePath (ruleExecSubmitInp->reiFilePath,
00073 ruleExecSubmitInp->userName);
00074 if (status < 0) {
00075 rodsLog (LOG_ERROR,
00076 "rsRuleExecSubmit: getReiFilePath failed, status = %d", status);
00077 return (status);
00078 }
00079 #if 0
00080 reiFd = creat (ruleExecSubmitInp->reiFilePath, 0640);
00081 #endif
00082 reiFd = open (ruleExecSubmitInp->reiFilePath, O_CREAT|O_EXCL|O_RDWR,
00083 0640);
00084 if (reiFd < 0) {
00085 if (errno == EEXIST) {
00086 continue;
00087 } else {
00088 status = SYS_OPEN_REI_FILE_ERR - errno;
00089 rodsLog (LOG_ERROR,
00090 "rsRuleExecSubmit: creat failed for %s, status = %d",
00091 ruleExecSubmitInp->reiFilePath, status);
00092 return (status);
00093 }
00094 } else {
00095 break;
00096 }
00097 }
00098
00099 status = write (reiFd, ruleExecSubmitInp->packedReiAndArgBBuf->buf,
00100 ruleExecSubmitInp->packedReiAndArgBBuf->len);
00101
00102 close (reiFd);
00103
00104 if (status != ruleExecSubmitInp->packedReiAndArgBBuf->len) {
00105 rodsLog (LOG_ERROR,
00106 "rsRuleExecSubmit: write rei error.toWrite %d, %d written",
00107 ruleExecSubmitInp->packedReiAndArgBBuf->len, status);
00108 return (SYS_COPY_LEN_ERR - errno);
00109 }
00110
00111
00112 #ifdef RODS_CAT
00113 status = chlRegRuleExec (rsComm, ruleExecSubmitInp);
00114 if (status < 0) {
00115 rodsLog(LOG_ERROR,
00116 "_rsRuleExecSubmit: chlRegRuleExec error. status = %d", status);
00117 }
00118 return (status);
00119 #else
00120 rodsLog(LOG_ERROR,
00121 "_rsRuleExecSubmit error. ICAT is not configured on this host");
00122 return (SYS_NO_ICAT_SERVER_ERR);
00123 #endif
00124
00125 }
00126
00127 int
00128 getReiFilePath (char *reiFilePath, char *userName)
00129 {
00130 char *myUserName;
00131
00132 if (reiFilePath == NULL) {
00133 return (SYS_INTERNAL_NULL_INPUT_ERR);
00134 }
00135
00136 if (userName == NULL || strlen (userName) == 0) {
00137 myUserName = DEF_REI_USER_NAME;
00138 } else {
00139 myUserName = userName;
00140 }
00141
00142 snprintf (reiFilePath, MAX_NAME_LEN,
00143 "%-s/%-s/%-s.%-s.%-d", getConfigDir(), PACKED_REI_DIR,
00144 REI_FILE_NAME, myUserName, (uint) random());
00145
00146 return (0);
00147 }
00148