Beispiel #1
0
void cChildProcess::OnChildRun()
{
	if (changeProcessName) prename_setproctitle("%s", "postfix_edm_client child");
	//kill -s USR1  pid  或者  kill -n 12  pid  或者 kill -12 pid   或者   kill -USR1 pid  参看 kill
	signal(SIGUSR1, sigUsr1Handler);
	signal(SIGCHLD, SIG_DFL);

	//工作子进程
	//INIT_RDONLY_SHMAT

	uint16_t delay = 0;

	mail_info_t *mail_info_ptr = NULL;
	while (true) {

		if (child_quit || getppid() == 1) {
			exit(SIGUSR1);
		}

		if (CHECK_CAN_START(shm_ptr)) {

			shm_ptr->childLock.Lock();
			pullSentInfoFromMysql();
			shm_ptr->havePushNum += send_mail_queue.size();
			shm_ptr->childLock.Unlock();

			if (!send_mail_queue.empty()) {
				//有数据的话
				while (!send_mail_queue.empty()) {
					mail_info_ptr = send_mail_queue.front();
					send_mail_queue.pop();
					_sendMail->send(mail_info_ptr);
					free_mail_info(mail_info_ptr);
				}
				delay = shm_ptr->delay;
				if (delay) {
					sleep(delay);
				}
			} else {
				cWorker::instance->done();

				shm_ptr->consumeLock.Lock();
				COND_LOCK_NOTIFYALL(shm_ptr->productLock)
				shm_ptr->consumeLock.wait();
				shm_ptr->consumeLock.Unlock();


			}
		} else {
			shm_ptr->commandLock.Lock();
			shm_ptr->commandLock.wait();
			shm_ptr->commandLock.Unlock();

		}
	}
}
Beispiel #2
0
void *send_the_mail(

  void *vp)

  {
  mail_info *mi = (mail_info *)vp;

  int         i;
  const char *mailfrom = NULL;
  const char *subjectfmt = NULL;
  char       *bodyfmt = NULL;
  char       *cmdbuf = NULL;
  char        bodyfmtbuf[MAXLINE];
  FILE       *outmail;
  
  /* Who is mail from, if SRV_ATR_mailfrom not set use default */
  get_svr_attr_str(SRV_ATR_mailfrom, (char **)&mailfrom);
  if (mailfrom == NULL)
    {
    if (LOGLEVEL >= 5)
      {
      char tmpBuf[LOG_BUF_SIZE];

      snprintf(tmpBuf,sizeof(tmpBuf),
        "Updated mailto from user list: '%s'\n",
        mi->mailto);
      log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
        PBS_EVENTCLASS_JOB,
        mi->jobid,
        tmpBuf);
      }

    mailfrom = PBS_DEFAULT_MAIL;
    }

  /* mail subject line formating statement */
  get_svr_attr_str(SRV_ATR_MailSubjectFmt, (char **)subjectfmt);
  if (subjectfmt == NULL)
    {
    subjectfmt = "PBS JOB %i";
    }

  /* mail body formating statement */
  get_svr_attr_str(SRV_ATR_MailBodyFmt, &bodyfmt);
  if (bodyfmt == NULL)
    {
    bodyfmt =  strcpy(bodyfmtbuf, "PBS Job Id: %i\n"
                                  "Job Name:   %j\n");
    if (mi->exec_host != NULL)
      {
      strcat(bodyfmt, "Exec host:  %h\n");
      }

    strcat(bodyfmt, "%m\n");

    if (mi->text != NULL)
      {
      strcat(bodyfmt, "%d\n");
      }
    }

  /* setup sendmail command line with -f from_whom */
  i = strlen(SENDMAIL_CMD) + strlen(mailfrom) + strlen(mi->mailto) + 6;

  if ((cmdbuf = (char *)calloc(1, i + 1)) == NULL)
    {
    char tmpBuf[LOG_BUF_SIZE];

    snprintf(tmpBuf,sizeof(tmpBuf),
      "Unable to popen() command '%s' for writing: '%s' (error %d)\n",
      SENDMAIL_CMD,
      strerror(errno),
      errno);
    log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
      PBS_EVENTCLASS_JOB,
      mi->jobid,
      tmpBuf);
  
    free_mail_info(mi);

    return(NULL);
    }

  sprintf(cmdbuf, "%s -f %s %s",
    SENDMAIL_CMD,
    mailfrom,
    mi->mailto);

  outmail = popen(cmdbuf, "w");

  if (outmail == NULL)
    {
    char tmpBuf[LOG_BUF_SIZE];

    snprintf(tmpBuf,sizeof(tmpBuf),
      "Unable to popen() command '%s' for writing: '%s' (error %d)\n",
      cmdbuf,
      strerror(errno),
      errno);
    log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
      PBS_EVENTCLASS_JOB,
      mi->jobid,
      tmpBuf);

    free_mail_info(mi);
    free(cmdbuf);

    return(NULL);
    }

  /* Pipe in mail headers: To: and Subject: */
  fprintf(outmail, "To: %s\n", mi->mailto);

  fprintf(outmail, "Subject: ");
  svr_format_job(outmail, mi, subjectfmt);
  fprintf(outmail, "\n");

  /* Set "Precedence: bulk" to avoid vacation messages, etc */
  fprintf(outmail, "Precedence: bulk\n\n");

  /* Now pipe in the email body */
  svr_format_job(outmail, mi, bodyfmt);

  errno = 0;
  if ((i = pclose(outmail)) != 0)
    {
    char tmpBuf[LOG_BUF_SIZE];

    snprintf(tmpBuf,sizeof(tmpBuf),
      "Email '%c' to %s failed: Child process '%s' %s %d (errno %d:%s)\n",
      mi->mail_point,
      mi->mailto,
      cmdbuf,
      ((WIFEXITED(i)) ? ("returned") : ((WIFSIGNALED(i)) ? ("killed by signal") : ("croaked"))),
      ((WIFEXITED(i)) ? (WEXITSTATUS(i)) : ((WIFSIGNALED(i)) ? (WTERMSIG(i)) : (i))),
      errno,
      strerror(errno));
    log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
      PBS_EVENTCLASS_JOB,
      mi->jobid,
      tmpBuf);
    }
  else if (LOGLEVEL >= 4)
    {
    log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
      PBS_EVENTCLASS_JOB,
      mi->jobid,
      "Email sent successfully\n");
    }

  free_mail_info(mi);
  free(cmdbuf);
    
  return(NULL);
  } /* END send_the_mail() */