Beispiel #1
0
pbs_queue *get_dfltque(void)

  {
  pbs_queue *pq = NULL;
  char      *dque = NULL;

  if (get_svr_attr_str(SRV_ATR_dflt_que, &dque) == PBSE_NONE)
    {
    pq = find_queuebyname(dque);
    }

  return(pq);
  }  /* END get_dfltque() */
/*
 * get_correct_jobname() - makes sure the job searches for the correct name
 * necessary because of SRV_ATR_display_job_server_suffix and
 * SRV_ATR_job_suffix_alias
 *
 * allocs the correct job name
 * @param jobid (I) - the jobid as passed in (NUM.SERVER_NAME)
 * @return a pointer to the correct job name (alloc'd)
 */
char *get_correct_jobname(

  const char *jobid) /* I */

  {
  char *correct = NULL;
  char *dot;
  char *work_jobid = strdup(jobid);
  /* first suffix could be the server name or the alias */
  char *first_suffix = NULL;

  /* second suffix can only be the alias */
  char *second_suffix = NULL;
  int   server_suffix = TRUE;

  int len;

  long  display_suffix = TRUE;
  char *alias = NULL;

  get_svr_attr_l(SRV_ATR_display_job_server_suffix, &display_suffix);
  if (display_suffix == FALSE)
    server_suffix = FALSE;

  if ((dot = strchr((char *)work_jobid,'.')) != NULL)
    {
    first_suffix = dot + 1;

    if ((dot = strchr(first_suffix,'.')) != NULL)
      {
      second_suffix = dot + 1;
      }
  
    dot = NULL;
    }

  /* check current settings */
  get_svr_attr_str(SRV_ATR_job_suffix_alias, &alias);
  if ((alias != NULL) &&
      (server_suffix == TRUE))
    {
    /* display the server suffix and the alias */

    /* check if alias is already there */
    if (second_suffix != NULL)
      {
      if (strcmp(second_suffix,alias) == 0)
        {
        correct = strdup(jobid);

        if (correct == NULL)
          log_err(-1, __func__, "ERROR:    Fatal - Cannot allocate memory\n");
      
        free(work_jobid);

        return(correct);
        }
      }
    else if (first_suffix == NULL)
      {
      /* alloc memory and sprint, add 3 for 2 '.' and NULL terminator */
      len = strlen(work_jobid) + strlen(server_name) + strlen(alias) + 3;
      correct = (char *)calloc(1, len);

      if (correct == NULL)
        {
        log_err(-1, __func__, "ERROR:    Fatal - Cannot allocate memory\n");
        free(work_jobid);
        return(NULL);
        }

      snprintf(correct,len,"%s.%s.%s",
        work_jobid,server_name,alias);
      }
    else
      {
      /* add 2 for null terminator and '.' */
      len = strlen(alias) + 2 + strlen(work_jobid);

      correct = (char *)calloc(1, len);

      if (correct == NULL)
        {
        log_err(-1, __func__, "ERROR:    Fatal - Cannot allocate memory\n");
        free(work_jobid);
        return(NULL);
        }

      snprintf(correct,len,"%s.%s",work_jobid,alias);
      }
    } /* END if (server_suffix && alias) */
  else if (server_suffix == TRUE)
    {
    /* just the server suffix */

    if (first_suffix != NULL)
      {
      correct = strdup(work_jobid);

      if (correct == NULL)
        {
        log_err(-1, __func__, "ERROR:    Fatal - Cannot allocate memory\n");
        free(work_jobid);
        return(NULL);
        }
      }
    else
      {
      len = strlen(work_jobid) + strlen(server_name) + 2;

      correct = (char *)calloc(1, len);

      if (correct == NULL)
        {
        log_err(-1, __func__, "ERROR:    Fatal - Cannot allocate memory\n");
        free(work_jobid);
        return(NULL);
        }

      snprintf(correct,len,"%s.%s",
        work_jobid,server_name);
      }
    } /* END if (just server_suffix) */
  else if (alias != NULL)
    {
    /* just the alias, not the server */

    if (first_suffix == NULL)
      {
      len = strlen(work_jobid) + strlen(alias) + 2;

      correct = (char *)calloc(1, len);

      if (correct == NULL)
        {
        log_err(-1, __func__, "ERROR:    Fatal - Cannot allocate memory\n");
        free(work_jobid);
        return(NULL);
        }

      snprintf(correct,len,"%s.%s",work_jobid,alias);
      }
    else
      {
      len = strlen(alias) + 2;

      dot = first_suffix - 1;
      *dot = '\0';

      len += strlen(work_jobid);
      correct = (char *)calloc(1, len);

      if (correct == NULL)
        {
        log_err(-1, __func__, "ERROR:    Fatal - Cannot allocate memory\n");
        free(work_jobid);
        return(NULL);
        }

      snprintf(correct,len,"%s.%s",
        work_jobid,
        alias);

      *dot = '.';
      }
    } /* END else if (just alias) */
  else
    {
    /* no server suffix nor alias */
    if (first_suffix != NULL)
      {
      dot = first_suffix - 1;
      *dot = '\0';
      }

    len = strlen(work_jobid) + 1;
    correct = (char *)calloc(1, len);

    if (correct == NULL)
      {
      log_err(-1, __func__, "ERROR:    Fatal - Cannot allocate memory\n");
      free(work_jobid);
      return(NULL);
      }

    snprintf(correct,len,"%s",work_jobid);

    if (first_suffix != NULL)
      *dot = '.';
    }

  free(work_jobid);

  return(correct);
  } /* END get_correct_jobname() */
Beispiel #3
0
void svr_mailowner(

  job   *pjob,      /* I */
  int    mailpoint, /* note, single character  */
  int    force,     /* if set to MAIL_FORCE, force mail delivery */
  const char  *text)      /* (optional) additional message text */

  {
  static const char   *memory_err = "Cannot allocate memory to send email";

  char                  mailto[1024];
  char                 *domain = NULL;
  int                   i;
  mail_info            *mi;
  long                  no_force = FALSE;

  struct array_strings *pas;
  memset(mailto, 0, sizeof(mailto));

  get_svr_attr_str(SRV_ATR_MailDomain, &domain);
  if ((domain != NULL) &&
      (!strcasecmp("never", domain)))
    {
    /* never send user mail under any conditions */
    if (LOGLEVEL >= 3) 
      {
      log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
        PBS_EVENTCLASS_JOB,
        pjob->ji_qs.ji_jobid,
        "Not sending email: Mail domain set to 'never'\n");
      }

    return;
    }

  if (LOGLEVEL >= 3)
    {
    char tmpBuf[LOG_BUF_SIZE];

    snprintf(tmpBuf, LOG_BUF_SIZE, "preparing to send '%c' mail for job %s to %s (%.64s)\n",
             (char)mailpoint,
             pjob->ji_qs.ji_jobid,
             pjob->ji_wattr[JOB_ATR_job_owner].at_val.at_str,
             (text != NULL) ? text : "---");

    log_event(
      PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
      PBS_EVENTCLASS_JOB,
      pjob->ji_qs.ji_jobid,
      tmpBuf);
    }

  /*
   * if force is true, force the mail out regardless of mailpoint
   * unless server no_mail_force attribute is set to true
   */
  get_svr_attr_l(SRV_ATR_NoMailForce, &no_force);

  if ((force != MAIL_FORCE) ||
      (no_force == TRUE))
    {

    if (pjob->ji_wattr[JOB_ATR_mailpnts].at_flags & ATR_VFLAG_SET)
      {
      if (*(pjob->ji_wattr[JOB_ATR_mailpnts].at_val.at_str) ==  MAIL_NONE)
        {
        /* do not send mail. No mail requested on job */
        log_event(PBSEVENT_JOB,
                  PBS_EVENTCLASS_JOB,
                  pjob->ji_qs.ji_jobid,
                  "Not sending email: job requested no e-mail");
        return;
        }
      /* see if user specified mail of this type */
      if (strchr(
            pjob->ji_wattr[JOB_ATR_mailpnts].at_val.at_str,
            mailpoint) == NULL)
        {
        /* do not send mail */
        log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
          PBS_EVENTCLASS_JOB,
          pjob->ji_qs.ji_jobid,
          "Not sending email: User does not want mail of this type.\n");

        return;
        }
      }
    else if (mailpoint != MAIL_ABORT) /* not set, default to abort */
      {
      log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
        PBS_EVENTCLASS_JOB,
        pjob->ji_qs.ji_jobid,
        "Not sending email: Default mailpoint does not include this type.\n");

      return;
      }
    }

  mi = (mail_info *)calloc(1, sizeof(mail_info));

  if (mi == NULL)
    {
    log_err(ENOMEM, __func__, memory_err);
    return;
    }

  /* Who does the mail go to?  If mail-list, them; else owner */
  mailto[0] = '\0';

  if (pjob->ji_wattr[JOB_ATR_mailuser].at_flags & ATR_VFLAG_SET)
    {
    /* has mail user list, send to them rather than owner */

    pas = pjob->ji_wattr[JOB_ATR_mailuser].at_val.at_arst;

    if (pas != NULL)
      {
      for (i = 0;i < pas->as_usedptr;i++)
        {
        if ((strlen(mailto) + strlen(pas->as_string[i]) + 2) < sizeof(mailto))
          {
          strcat(mailto, pas->as_string[i]);
          strcat(mailto, " ");
          }
        }
      }
    }
  else
    {
    /* no mail user list, just send to owner */

    if (domain != NULL)
      {
      snprintf(mailto, sizeof(mailto), "%s@%s",
        pjob->ji_wattr[JOB_ATR_euser].at_val.at_str, domain);

      if (LOGLEVEL >= 5) 
        {
        char tmpBuf[LOG_BUF_SIZE];

        snprintf(tmpBuf,sizeof(tmpBuf),
          "Updated mailto from job owner and mail domain: '%s'\n",
          mailto);
        log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
          PBS_EVENTCLASS_JOB,
          pjob->ji_qs.ji_jobid,
          tmpBuf);
        }
      }
    else
      {
#ifdef TMAILDOMAIN
      snprintf(mailto, sizeof(mailto), "%s@%s",
        pjob->ji_wattr[JOB_ATR_euser].at_val.at_str, TMAILDOMAIN);
#else /* TMAILDOMAIN */
      snprintf(mailto, sizeof(mailto), "%s", pjob->ji_wattr[JOB_ATR_job_owner].at_val.at_str);
#endif /* TMAILDOMAIN */

      if (LOGLEVEL >= 5)
        {
        char tmpBuf[LOG_BUF_SIZE];

        snprintf(tmpBuf,sizeof(tmpBuf),
          "Updated mailto from job owner: '%s'\n",
          mailto);
        log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
          PBS_EVENTCLASS_JOB,
          pjob->ji_qs.ji_jobid,
          tmpBuf);
        }
      }
    }

  /* initialize the mail information */

  if ((mi->mailto = strdup(mailto)) == NULL)
    {
    log_err(ENOMEM, __func__, memory_err);
    free(mi);
    return;
    }

  mi->mail_point = mailpoint;

  if (pjob->ji_wattr[JOB_ATR_exec_host].at_val.at_str != NULL)
    {
    mi->exec_host = strdup(pjob->ji_wattr[JOB_ATR_exec_host].at_val.at_str);

    if (mi->exec_host == NULL)
      {
      log_err(ENOMEM, __func__, memory_err);
      free(mi);
      return;
      }
    }
  else
    mi->exec_host = NULL;

  if ((mi->jobid = strdup(pjob->ji_qs.ji_jobid)) == NULL)
    {
    log_err(ENOMEM, __func__, memory_err);
    free(mi);
    return;
    }

  if (pjob->ji_wattr[JOB_ATR_jobname].at_val.at_str != NULL)
    {
    mi->jobname = strdup(pjob->ji_wattr[JOB_ATR_jobname].at_val.at_str);

    if (mi->jobname == NULL)
      {
      log_err(ENOMEM, __func__, memory_err);
      free(mi);
      return;
      }
    }
  else
    mi->jobname = NULL;

  if (text)
    {
    if ((mi->text = strdup(text)) == NULL)
      {
      free(mi);
      log_err(ENOMEM, __func__, memory_err);
      return;
      }
    }
  else
    mi->text = NULL;

  /* have a thread do the work of sending the mail */
  enqueue_threadpool_request(send_the_mail,mi);

  return;
  }  /* END svr_mailowner() */
Beispiel #4
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() */