/***************************************************** set a variable in the shared area *******************************************************/ void smbw_setshared(const char *name, const char *val) { int l1, l2; /* we don't allow variable overwrite */ if (smbw_getshared(name)) return; lockit(); l1 = strlen(name)+1; l2 = strlen(val)+1; variables = (char *)Realloc(variables, shared_size + l1+l2+4); if (!variables) { DEBUG(0,("out of memory in smbw_setshared\n")); exit(1); } SSVAL(&variables[shared_size], 0, l1); SSVAL(&variables[shared_size], 2, l2); pstrcpy(&variables[shared_size] + 4, name); pstrcpy(&variables[shared_size] + 4 + l1, val); shared_size += l1+l2+4; lseek(shared_fd, 0, SEEK_SET); if (write(shared_fd, variables, shared_size) != shared_size) { DEBUG(0,("smbw_setshared failed (%s)\n", strerror(errno))); exit(1); } unlockit(); }
void unlockPsgQ(const char *dir) { char lockPath[MAXSTR]; char idPath[MAXSTR]; const char *lockname = "lockPsgQ"; sprintf(lockPath,"%s/%s",dir,lockname); sprintf(idPath,"%s/pq_%d",dir,getpid()); unlockit(lockPath,idPath); }
void unlockAtcmd(const char *dir) { char lockPath[MAXSTR]; char idPath[MAXSTR]; const char *lockname = "lockAtcmd"; sprintf(lockPath,"%s/acqqueue/%s",dir,lockname); sprintf(idPath,"%s/acqqueue/pq_%d",dir,getpid()); unlockit(lockPath,idPath); }
/***************************************************** get a variable from the shared area *******************************************************/ char *smbw_getshared(const char *name) { int i; struct stat st; char *var; lockit(); /* maybe the area has changed */ if (fstat(shared_fd, &st)) goto failed; if (st.st_size != shared_size) { var = (char *)Realloc(variables, st.st_size, True); if (!var) goto failed; else variables = var; shared_size = st.st_size; lseek(shared_fd, 0, SEEK_SET); if (read(shared_fd, variables, shared_size) != shared_size) { goto failed; } } unlockit(); i=0; while (i < shared_size) { char *n, *v; int l1, l2; l1 = SVAL(&variables[i], 0); l2 = SVAL(&variables[i], 2); n = &variables[i+4]; v = &variables[i+4+l1]; i += 4+l1+l2; if (strcmp(name,n)) { continue; } return v; } return NULL; failed: DEBUG(0,("smbw: shared variables corrupt (%s)\n", strerror(errno))); exit(1); return NULL; }
int autoq(int argc, char *argv[], int retc, char *retv[]) { char autodir[MAXPATH]; char lockPath[MAXPATH]; char idPath[MAXPATH]; char lockLogPath[MAXPATH]; char idLogPath[MAXPATH]; char logBasePath[MAXPATH]; char *blockname = "autoqBlock"; char *lockname = "lockEnterQ"; time_t lockSecs = 5; /* default lock timeout */ int res = 1; if (argc < 2) { Werrprintf("usage: %s requires add, get, lock, unlock, sendmsg, or recvmsg argument", argv[0]); ABORT; } if ( !strcmp(argv[1],"sendmsg") || !strcmp(argv[1],"recvmsg") ) { res = 4; } else { #ifdef VNMRJ if ( ! strcmp(argv[1],"add") ) P_getstring(GLOBAL,"autodir",autodir,1,MAXPATH); else #endif getAutoDir(autodir, MAXPATH); sprintf(lockPath,"%s/%s",autodir,lockname); sprintf(idPath,"%s/eq_%s_%s_%d",autodir,HostName,UserName,HostPid); } if ( ! strcmp(argv[1],"add") ) { char queuename[MAXPATH]; if (argc < 3) { Werrprintf("usage: %s('add',filename) requires a filename.",argv[0]); ABORT; } if (argc == 4) strcpy(queuename,argv[3]); else strcpy(queuename,"std"); /* Wait for get operations to complete first */ blockedByGet(autodir, blockname); if ( lockit(lockPath,idPath,lockSecs) ) { res = addQueueFile(autodir, argv[2], queuename); unlockit(lockPath,idPath); } else { res = -4; } } else if ( ! strcmp(argv[1],"get") ) { FILE *fd; char blockfile[MAXPATH]; if (argc < 3) { Werrprintf("usage: %s('get',filename) requires a filename.",argv[0]); ABORT; } /* The get operation will block add and lock from other processes */ sprintf(blockfile,"%s/%s",autodir,blockname); fd = fopen(blockfile,"w"); fclose(fd); if ( lockit(lockPath,idPath,lockSecs) ) { res = getQueueFile(autodir, argv[2]); unlockit(lockPath,idPath); } else { res = -4; } #ifdef TESTING if (argc == 4) sleepnano(atoi(argv[3])); #endif /* Allow access by add and lock from other processes */ unlink(blockfile); } else if ( ! strcmp(argv[1],"lock") ) { if (argc == 3) { lockSecs = atoi(argv[2]); if (lockSecs < 1) lockSecs = 1; else if (lockSecs > 15) lockSecs = 15; } /* Wait for get operations to complete first */ blockedByGet(autodir, blockname); DPRINT1("lock called with %ld timeout\n",lockSecs); res = lockit(lockPath,idPath,lockSecs); } else if ( ! strcmp(argv[1],"unlock") ) { unlockit(lockPath,idPath); res = 1; } else if ( ! strcmp(argv[1],"addAcct") ) { char accFile[MAXPATH]; char idFile[MAXPATH]; if (argc != 3) { Werrprintf("autoq('addAcct',filename) requires filename as the second argument"); ABORT; } if ( access(argv[2], R_OK|W_OK) ) { Werrprintf("autoq('addAcct',filename) filename does not exist or has wrong permissions"); ABORT; } res = 0; strcpy(lockPath, getenv("vnmrsystem") ); strcat(lockPath,"/adm/accounting/"); strcpy(accFile, lockPath); strcat(accFile,"acctLog.xml"); if ( !access(accFile,F_OK) ) { FILE *inputFd; int bufSize=4095; char buf[bufSize+1]; int size = 0; inputFd = fopen(argv[2],"r"); if (inputFd) { size = fread(buf, 1, bufSize, inputFd); /* Terminate the buffer */ buf[size] = 0; fclose(inputFd); } unlink(argv[2]); if (size) { lockSecs = 2; // Create filepaths for the lock files to lock acctLog strcpy(idPath, lockPath); strcat(lockPath, "acctLogLock"); sprintf(idFile, "acctLogLockId_%s_%d", HostName, HostPid); strcat(idPath, idFile); DPRINT1("lock called with %ld timeout\n",lockSecs); if ( lockit(lockPath,idPath,lockSecs) ) { const char root_end[]="</accounting>\n"; FILE *xmlFd; xmlFd = fopen(accFile,"r+"); if (xmlFd) { /* find the closing /> at the end and put log info above that */ fseek(xmlFd,-strlen(root_end),SEEK_END); /* Write to the log file */ fprintf(xmlFd,"%s",buf); fprintf(xmlFd,"%s",root_end); fflush(xmlFd); fclose(xmlFd); } unlockit(lockPath,idPath); res = 1; } } } } else if ( ! strcmp(argv[1],"locklog") ) { lockSecs = 15; // Create filepaths for the lock files to lock acctLog strcpy(logBasePath, (char *)getenv("vnmrsystem") ); strcat(logBasePath,"/adm/accounting/"); sprintf(lockLogPath, "%s%s", logBasePath, "acctLogLock"); sprintf(idLogPath, "%sacctLogLockId_%s_%d", logBasePath, HostName, HostPid); /* Wait for get operations to complete first */ blockedByGet(autodir, blockname); DPRINT1("lock called with %ld timeout\n",lockSecs); res = lockit(lockLogPath,idLogPath,lockSecs); } else if ( ! strcmp(argv[1],"unlocklog") ) { // Create filepaths for the lock files to lock acctLog strcpy(logBasePath, (char *)getenv("vnmrsystem") ); strcat(logBasePath,"/adm/accounting/"); sprintf(lockLogPath, "%s%s", logBasePath, "acctLogLock"); sprintf(idLogPath, "%sacctLogLockId_%s_%d", logBasePath, HostName, HostPid); unlockit(lockLogPath,idLogPath); res = 1; } else if (res == 4) /* ( !strcmp(argv[1],"sendmsg") || !strcmp(argv[1],"recvmsg") ) */ { if ( ! strcmp(argv[1],"recvmsg") ) { if ( (argc != 3) || ( strcmp(argv[2],"on") && strcmp(argv[2],"off")) ) { Werrprintf("autoq %s requires 'on' or 'off' as the second argument",argv[1]); ABORT; } } if (argc != 3) { Werrprintf("autoq %s requires another argument",argv[1]); ABORT; } res = 1; if (is_acqproc_active()) { char msg[MAXSTR]; sprintf(msg,"%s %s",argv[1],argv[2]); if (send2Acq(AUTOQMSG, msg) < 0) res = 0; if (res) receivingMsg = ( ! strcmp(argv[2],"on")); } } DPRINT1("operation returns %d\n",res); if (retc) { retv[0] = intString(res); } RETURN; }
void bill_done(SHR_EXP_INFO ExpInfo) { const char root_end[]="</accounting>\n"; char tmpStr[80],*tmpPtr; char inputPath[MAXPATHL]; int bufSize=4095; char buf[bufSize+1]; int i,n; FILE *inputFd=NULL; char lockPath[MAXPATHL]; char idPath[MAXPATHL]; char idFile[MAXPATHL]; char accFile[MAXPATHL]; time_t lockSecs = 2; /* default lock timeout */ char t_format[] = "%a %b %d %T %Z %Y"; FILE *xmlFd; if (ExpInfo->Billing.wroteRecord != 0) return; strcpy(lockPath, getenv("vnmrsystem") ); strcat(lockPath,"/adm/accounting/"); strcpy(accFile, lockPath); strcat(accFile,"acctLog.xml"); if ( access(accFile,F_OK) ) return; ExpInfo->Billing.doneTime = time(0); /* Lock the Log file so other instances of vnmrj cannot write at the same time. This is to lock the acctLog file. */ strcpy(idPath, lockPath); strcat(lockPath, "acctLogLock"); sprintf(idFile, "acctLogLockId_%d", getpid()); strcat(idPath, idFile); lockit(lockPath, idPath, lockSecs); xmlFd = fopen(accFile,"r+"); if (xmlFd == NULL) { unlockit(lockPath,idPath); printf("Cannot (re-re)open '%s'\n",accFile); return; } /* find the closing /> at the end and put log info above that */ fseek(xmlFd,-strlen(root_end),SEEK_END); fprintf(xmlFd,"<entry type=\"gorecord\" account=\"%s\" operator=\"%s\"\n", ExpInfo->Billing.account, ExpInfo->Billing.Operator); fprintf(xmlFd," goflag=\"%d\" loc=\"%d\" result=\"%d\"\n", ExpInfo->GoFlag, ExpInfo->SampleLoc, ExpInfo->ExpState); fprintf(xmlFd," seqfil=\"%s\"\n",ExpInfo->Billing.seqfil); i=0; n=sizeof(tmpStr); // strncpy(tmpStr,ctime(&(ExpInfo->Billing.submitTime)),n-1); strftime(tmpStr,n-1, t_format, localtime(&(ExpInfo->Billing.submitTime))); tmpPtr = tmpStr; // remove <lf> from submitTime while ( *tmpPtr != '\0' && i<n) { if ( *tmpPtr == '\n') *tmpPtr='\0'; tmpPtr++; i++; } fprintf(xmlFd," submit=\"%s\"\n",tmpStr); // strncpy(tmpStr,ctime(&(ExpInfo->Billing.startTime)),n-1); strftime(tmpStr,n-1, t_format, localtime(&(ExpInfo->Billing.startTime))); tmpPtr = tmpStr; // remove <lf> from startTime i=0; while ( *tmpPtr != '\0' && i<n) { if ( *tmpPtr == '\n') *tmpPtr='\0'; tmpPtr++; i++; } fprintf(xmlFd," start=\"%s\"\n",tmpStr); fprintf(xmlFd," startsec=\"%ld\"\n",ExpInfo->Billing.startTime); // strncpy(tmpStr,ctime( &(ExpInfo->Billing.doneTime)),n-1); strftime(tmpStr,n-1, t_format, localtime(&(ExpInfo->Billing.doneTime))); tmpPtr = tmpStr; // remove <lf> from doneTime i=0; while ( *tmpPtr != '\0' && i<n ) { if ( *tmpPtr == '\n') *tmpPtr='\0'; tmpPtr++; i++; } fprintf(xmlFd," end=\"%s\"\n",tmpStr); fprintf(xmlFd," endsec=\"%ld\"\n",ExpInfo->Billing.doneTime); // Write optional params /* - Open file .../[go_id].loginfo */ sprintf(inputPath, "/vnmr/acqqueue/%s.loginfo", ExpInfo->Billing.goID); inputFd = fopen(inputPath, "r"); if(inputFd != NULL) { /* Get the optional param info */ int size = fread(buf, 1, bufSize, inputFd); /* Terminate the buffer */ buf[size] = 0; /* Write to the log file */ fprintf(xmlFd,"%s",buf); fclose(inputFd); } fprintf(xmlFd,"/>\n"); fprintf(xmlFd,"%s",root_end); fflush(xmlFd); fclose(xmlFd); unlink(inputPath); ExpInfo->Billing.wroteRecord = 1; unlockit(lockPath,idPath); }