示例#1
0
文件: test_cimd2.c 项目: armic/erpts
/* Return the minimum interval (in microseconds) after which we will
 * want to be called again.  This value is only used if we _don't_
 * generate data this time through. */
static long gen_data(Octstr *out) {
	unsigned char buf[EVIL_BUFSIZE];
	size_t i;
	long interval = -1;
	static int last_sms;  /* Used by ACT_deliver */
	time_t now;

	if (max_deliveries < 0 || deliveries < max_deliveries) {
		switch (activity) {
		case ACT_deliver:
			now = time(NULL);
			if (last_sms == 0)
				last_sms = now;
			while (last_sms < now) {
				if (random() % 7 == 1) {
					gen_message(out);
					last_sms = now;
				} else
					last_sms++;
			}
			interval = 1000000;
			break;
		case ACT_flood:
			gen_message(out);
			break;
		}
	}

	switch (spew) {
	case SPEW_binary:
		for (i = 0; i < sizeof(buf); i++) {
			buf[i] = random() % 256;
		}
		octstr_append_data(out, buf, sizeof(buf));
		break;
	}

	return interval;
}
示例#2
0
void nodelogger(const char *job, const char* type, const char* loop_ext, const char *message, const char* datestamp, const char* _seq_exp_home)
{
   int sock=-1,ret, write_ret;
   char *tmpfrommaestro = NULL;
   char tmp[10];
   struct passwd *p, *p2;
   struct sigaction sa;
   char *logtocreate = NULL;
   int pathcounter=0;
   char *pathelement=NULL, *tmpbuf=NULL;

   if ( loop_ext == NULL ) { loop_ext=strdup(""); }
   if ( message  == NULL ) { message=strdup(""); }
    
   SeqUtil_TRACE(TL_FULL_TRACE, "nodelogger job:%s signal:%s message:%s loop_ext:%s datestamp:%s exp:%s\n", job, type, message, loop_ext, datestamp, _seq_exp_home);

   memset(LOG_PATH,'\0',sizeof LOG_PATH);
   memset(TMP_LOG_PATH,'\0',sizeof TMP_LOG_PATH);
   memset(TOP_LOG_PATH,'\0',sizeof TOP_LOG_PATH);
   memset(username,'\0',sizeof username);

   if ( ( p = getpwuid ( getuid() ) ) == NULL ) {
      fprintf(stderr,"nodelogger: getpwuid() error" );
      return;
   } else {
      /* example: pw_name = afsipat */
      strcpy(username, p->pw_name);
   }

   p2 = getpwnam(username);
   if (p2 == NULL) {
     fprintf( stderr, "Nodelogger::ERROR: getpwnam error... returns null.\n" );
   }
   if( _seq_exp_home == NULL ) {
      fprintf( stderr, "Nodelogger::ERROR: You must provide a valid SEQ_EXP_HOME.\n" );
      exit(1);
   }

   snprintf(LOG_PATH,sizeof(LOG_PATH),"%s/logs/%s_nodelog",_seq_exp_home,datestamp);
   snprintf(TMP_LOG_PATH,sizeof(TMP_LOG_PATH),"%s/sequencing/sync/%s",_seq_exp_home,datestamp);
   snprintf(TOP_LOG_PATH,sizeof(TOP_LOG_PATH),"%s/logs/%s_toplog",_seq_exp_home,datestamp);

    /* setup an alarm so that if the logging is stuck
     * it will timeout after 60 seconds. This will prevent the
     * processes from hanging when there are network problems.
     */

    memset(NODELOG_JOB,'\0',NODELOG_BUFSIZE);
    strcpy(NODELOG_JOB,job);
    memset(NODELOG_LOOPEXT,'\0',NODELOG_BUFSIZE);
    strcpy(NODELOG_LOOPEXT,loop_ext);
    memset(NODELOG_MESSAGE,'\0',NODELOG_BUFSIZE);
    strcpy(NODELOG_MESSAGE,message);
    memset(NODELOG_DATE,'\0',NODELOG_BUFSIZE);
    strcpy(NODELOG_DATE,datestamp);
    
    pathcounter = 0;
    tmpbuf=strdup(NODELOG_JOB);
    pathelement = strtok(tmpbuf, "/");
    while (pathelement != NULL) {
       if (strcmp(pathelement, "") != 0) {
           pathcounter=pathcounter+1;
       }
       pathelement=strtok(NULL, "/");
    }

    /* if called inside maestro, a connection is already open  */
    tmpfrommaestro = getenv("FROM_MAESTRO");
    if ( tmpfrommaestro == NULL ) {
       SeqUtil_TRACE(TL_MEDIUM, "\n================= NODELOGGER: NOT_FROM_MAESTRO signal:%s================== \n",type);
       FromWhere = FROM_NODELOGGER;
       if ( (sock=OpenConnectionToMLLServer( job , "LOG",_seq_exp_home )) < 0 ) { 
          gen_message(NODELOG_JOB, type, loop_ext, NODELOG_MESSAGE);
          if ((pathcounter <= 1) || (strcmp(type, "abort") == 0) || (strcmp(type, "event") == 0) || (strcmp(type, "info") == 0) ) {
            logtocreate = "both";
          } else {
            logtocreate = "nodelog";
          }
          ret=sync_nodelog_over_nfs(NODELOG_JOB, type, loop_ext, NODELOG_MESSAGE, datestamp, logtocreate,_seq_exp_home);
          return;
       }
       /* install SIGALRM handler */
       /* set the signal handler */
       memset (&sa, '\0', sizeof(sa));
       sa.sa_handler = &log_alarm_handler;
       sa.sa_flags = 0; /* was SA_RESTART  */
       sigemptyset(&sa.sa_mask);
       /* register signals */
       if ( sigaction(SIGALRM,&sa,NULL) == -1 ) fprintf(stderr,"Nodelogger::error in registring SIGALRM\n");
    } else {
       SeqUtil_TRACE(TL_MEDIUM, "\n================= NODELOGGER: TRYING TO USE CONNECTION FROM MAESTRO PROCESS IF SERVER IS UP signal:%s================== \n",type);
       if ( MLLServerConnectionFid > 0 ) {
          FromWhere = FROM_MAESTRO;
          sock = MLLServerConnectionFid;
          SeqUtil_TRACE(TL_MEDIUM, "\n================= NODELOGGER: OK,HAVE A CONNECTION FROM MAESTRO PROCESS ================== \n");
       } else {
        /* it could be that we dont have the env. variable set to use Server */
           FromWhere = FROM_MAESTRO_NO_SVR;
	   if ( (sock=OpenConnectionToMLLServer( job , "LOG" , _seq_exp_home )) < 0 ) { 
               SeqUtil_TRACE(TL_MEDIUM, "\n================= NODELOGGER: CANNOT ACQUIRE CONNECTION FROM MAESTRO PROCESS signal:%s================== \n",type);
               gen_message(NODELOG_JOB, type, loop_ext, NODELOG_MESSAGE);
               if ((pathcounter <= 1) || (strcmp(type, "abort") == 0) || (strcmp(type, "event") == 0) || (strcmp(type, "info") == 0) ) {
                 logtocreate = "both";
               } else {
                 logtocreate = "nodelog";
               }
               ret=sync_nodelog_over_nfs(NODELOG_JOB, type, loop_ext, NODELOG_MESSAGE, datestamp, logtocreate, _seq_exp_home);
               return;
            } else {
               SeqUtil_TRACE(TL_MEDIUM, "\n================= ACQUIRED A NEW CONNECTION FROM NODELOGGER PROCESS ================== \n");
            }
       }
    }
 
    /* if we are here socket is Up then why the second test > -1 ??? */ 
    gen_message(NODELOG_JOB, type, loop_ext, NODELOG_MESSAGE);
    if ( sock > -1 ) {
      if ((write_ret=write_line(sock, 0, type )) == -1) {
        logtocreate = "nodelog";
        ret=sync_nodelog_over_nfs(NODELOG_JOB, type, loop_ext, NODELOG_MESSAGE, datestamp, logtocreate, _seq_exp_home); 
      }
      if ((pathcounter <= 1) || (strcmp(type, "abort") == 0) || (strcmp(type, "event") == 0) || (strcmp(type, "info") == 0) ) {
         if ((write_ret=write_line(sock, 1, type )) == -1) {
           logtocreate = "toplog";
           ret=sync_nodelog_over_nfs(NODELOG_JOB, type, loop_ext, NODELOG_MESSAGE, datestamp, logtocreate, _seq_exp_home); 
         }
      }
      if (write_ret == -1) return;
    } else {
      if ((pathcounter <= 1) || (strcmp(type, "abort") == 0) || (strcmp(type, "event") == 0) || (strcmp(type, "info") == 0) ) {
        logtocreate = "both";
      } else {
        logtocreate = "nodelog";
      }
      ret=sync_nodelog_over_nfs(NODELOG_JOB, type, loop_ext, NODELOG_MESSAGE, datestamp, logtocreate, _seq_exp_home);
      return;
    }
        /* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@ CRITICAL  : CLOSE SOCKET ONLY WHEN NOT ACQUIRED FROM MAESTRO @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
    switch (FromWhere)
    {

       case FROM_NODELOGGER:
       case FROM_MAESTRO_NO_SVR:
	   ret=write(sock,"S \0",3);
           close(sock);
           SeqUtil_TRACE(TL_MEDIUM, "\n================= ClOSING CONNECTION FROM NODELOGGER PROCESS ================== \n");
	   break;
       default:
           break;
    }
}