~ubuntu-branches/ubuntu/precise/torque/precise-updates

« back to all changes in this revision

Viewing changes to src/server/svr_mail.c

  • Committer: Bazaar Package Importer
  • Author(s): Dominique Belhachemi
  • Date: 2010-05-17 20:56:46 UTC
  • mfrom: (0.1.3 sid) (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100517205646-yipvh4jq5n5n1kgo
Tags: 2.4.8+dfsg-5
install man pages together with their associated binaries (Closes: #581576)

Show diffs side-by-side

added added

removed removed

Lines of Context:
85
85
 
86
86
#include "pbs_ifl.h"
87
87
#include <sys/types.h>
 
88
#include <sys/wait.h>
88
89
#include <unistd.h>
89
90
#include <errno.h>
90
91
#include <stdio.h>
93
94
#include "list_link.h"
94
95
#include "attribute.h"
95
96
#include "server_limits.h"
96
 
#include "job.h"
 
97
#include "pbs_job.h"
97
98
#include "log.h"
98
99
#include "server.h"
99
100
#include "rpp.h"
102
103
/* External Functions Called */
103
104
 
104
105
extern void net_close A_((int));
 
106
extern void svr_format_job A_((FILE *, job *, char *, int, char *));
105
107
 
106
108
/* Global Data */
107
109
 
108
110
extern struct server server;
109
 
extern char *msg_job_abort;
110
 
extern char *msg_job_start;
111
 
extern char *msg_job_end;
112
 
extern char *msg_job_del;
113
 
extern char *msg_job_stageinfail;
114
 
extern char *msg_job_otherfail;
115
111
 
116
112
extern int LOGLEVEL;
117
113
 
127
123
  int    i;
128
124
  char *mailfrom;
129
125
  char  mailto[1024];
 
126
  char *bodyfmt, *subjectfmt;
 
127
  char bodyfmtbuf[1024];
130
128
  FILE *outmail;
131
129
 
132
130
  struct array_strings *pas;
133
 
  char *stdmessage = NULL;
134
131
 
135
132
  if ((server.sv_attr[(int)SRV_ATR_MailDomain].at_flags & ATR_VFLAG_SET) &&
136
133
      (server.sv_attr[(int)SRV_ATR_MailDomain].at_val.at_str != NULL) &&
137
134
      (!strcasecmp("never", server.sv_attr[(int)SRV_ATR_MailDomain].at_val.at_str)))
138
135
    {
139
136
    /* never send user mail under any conditions */
 
137
    if (LOGLEVEL >= 3) 
 
138
      {
 
139
      log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
 
140
        PBS_EVENTCLASS_JOB,
 
141
        pjob->ji_qs.ji_jobid,
 
142
        "Not sending email: Mail domain set to 'never'\n");
 
143
      }
140
144
 
141
145
    return;
142
146
    }
171
175
            mailpoint) == NULL)
172
176
        {
173
177
        /* do not send mail */
 
178
        log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
 
179
          PBS_EVENTCLASS_JOB,
 
180
          pjob->ji_qs.ji_jobid,
 
181
          "Not sending email: User does not want mail of this type.\n");
174
182
 
175
183
        return;
176
184
        }
177
185
      }
178
186
    else if (mailpoint != MAIL_ABORT) /* not set, default to abort */
179
187
      {
 
188
      log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
 
189
        PBS_EVENTCLASS_JOB,
 
190
        pjob->ji_qs.ji_jobid,
 
191
        "Not sending email: Default mailpoint does not include this type.\n");
 
192
 
180
193
      return;
181
194
      }
182
195
    }
203
216
  /* Who is mail from, if SRV_ATR_mailfrom not set use default */
204
217
 
205
218
  if ((mailfrom = server.sv_attr[(int)SRV_ATR_mailfrom].at_val.at_str) == NULL)
 
219
    {
 
220
    if (LOGLEVEL >= 5)
 
221
      {
 
222
      char tmpBuf[LOG_BUF_SIZE];
 
223
 
 
224
      snprintf(tmpBuf,sizeof(tmpBuf),
 
225
        "Updated mailto from user list: '%s'\n",
 
226
        mailto);
 
227
      log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
 
228
        PBS_EVENTCLASS_JOB,
 
229
        pjob->ji_qs.ji_jobid,
 
230
        tmpBuf);
 
231
      }
206
232
    mailfrom = PBS_DEFAULT_MAIL;
 
233
    }
207
234
 
208
235
  /* Who does the mail go to?  If mail-list, them; else owner */
209
236
 
237
264
      strcpy(mailto, pjob->ji_wattr[(int)JOB_ATR_euser].at_val.at_str);
238
265
      strcat(mailto, "@");
239
266
      strcat(mailto, server.sv_attr[(int)SRV_ATR_MailDomain].at_val.at_str);
 
267
 
 
268
      if (LOGLEVEL >= 5) 
 
269
        {
 
270
        char tmpBuf[LOG_BUF_SIZE];
 
271
 
 
272
        snprintf(tmpBuf,sizeof(tmpBuf),
 
273
          "Updated mailto from job owner and mail domain: '%s'\n",
 
274
          mailto);
 
275
        log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
 
276
          PBS_EVENTCLASS_JOB,
 
277
          pjob->ji_qs.ji_jobid,
 
278
          tmpBuf);
 
279
        }
240
280
      }
241
281
    else
242
282
      {
247
287
#else /* TMAILDOMAIN */
248
288
      strcpy(mailto, pjob->ji_wattr[(int)JOB_ATR_job_owner].at_val.at_str);
249
289
#endif /* TMAILDOMAIN */
250
 
      }
251
 
    }
252
 
 
 
290
 
 
291
      if (LOGLEVEL >= 5)
 
292
        {
 
293
        char tmpBuf[LOG_BUF_SIZE];
 
294
 
 
295
        snprintf(tmpBuf,sizeof(tmpBuf),
 
296
          "Updated mailto from job owner: '%s'\n",
 
297
          mailto);
 
298
        log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
 
299
          PBS_EVENTCLASS_JOB,
 
300
          pjob->ji_qs.ji_jobid,
 
301
          tmpBuf);
 
302
        }
 
303
      }
 
304
    }
 
305
 
 
306
  /* mail subject line formating statement */
 
307
 
 
308
  if ((server.sv_attr[(int)SRV_ATR_MailSubjectFmt].at_flags & ATR_VFLAG_SET) &&
 
309
      (server.sv_attr[(int)SRV_ATR_MailSubjectFmt].at_val.at_str != NULL))
 
310
    {
 
311
    subjectfmt = server.sv_attr[(int)SRV_ATR_MailSubjectFmt].at_val.at_str;
 
312
    }
 
313
  else
 
314
    {
 
315
    subjectfmt = "PBS JOB %i";
 
316
    }
 
317
 
 
318
  /* mail body formating statement */
 
319
 
 
320
  if ((server.sv_attr[(int)SRV_ATR_MailBodyFmt].at_flags & ATR_VFLAG_SET) &&
 
321
      (server.sv_attr[(int)SRV_ATR_MailBodyFmt].at_val.at_str != NULL))
 
322
    {
 
323
    bodyfmt = server.sv_attr[(int)SRV_ATR_MailBodyFmt].at_val.at_str;
 
324
    }
 
325
  else
 
326
    {
 
327
    bodyfmt =  strcpy(bodyfmtbuf, "PBS Job Id: %i\n"
 
328
                                  "Job Name:   %j\n");
 
329
    if (pjob->ji_wattr[(int)JOB_ATR_exec_host].at_flags & ATR_VFLAG_SET)
 
330
      {
 
331
      strcat(bodyfmt, "Exec host:  %h\n");
 
332
      }
 
333
 
 
334
    strcat(bodyfmt, "%m\n");
 
335
 
 
336
    if (text != NULL)
 
337
      {
 
338
      strcat(bodyfmt, "%d\n");
 
339
      }
 
340
    }
253
341
  /* setup sendmail command line with -f from_whom */
254
342
 
255
343
  i = strlen(SENDMAIL_CMD) + strlen(mailfrom) + strlen(mailto) + 6;
269
357
 
270
358
  if (outmail == NULL)
271
359
    {
 
360
    char tmpBuf[LOG_BUF_SIZE];
 
361
 
 
362
    snprintf(tmpBuf,sizeof(tmpBuf),
 
363
      "Unable to popen() command '%s' for writing: '%s' (error %d)\n",
 
364
      cmdbuf,
 
365
      strerror(errno),
 
366
      errno);
 
367
    log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
 
368
      PBS_EVENTCLASS_JOB,
 
369
      pjob->ji_qs.ji_jobid,
 
370
      tmpBuf);
 
371
 
272
372
    exit(1);
273
373
    }
274
374
 
277
377
  fprintf(outmail, "To: %s\n",
278
378
          mailto);
279
379
 
280
 
  fprintf(outmail, "Subject: PBS JOB %s\n",
281
 
          pjob->ji_qs.ji_jobid);
 
380
  fprintf(outmail, "Subject: ");
 
381
  svr_format_job(outmail, pjob, subjectfmt, mailpoint, text);
 
382
  fprintf(outmail, "\n");
282
383
 
283
384
  /* Set "Precedence: bulk" to avoid vacation messages, etc */
284
385
 
285
386
  fprintf(outmail, "Precedence: bulk\n\n");
286
387
 
287
 
  /* Now pipe in "standard" message */
288
 
 
289
 
  switch (mailpoint)
290
 
    {
291
 
 
292
 
    case MAIL_ABORT:
293
 
 
294
 
      stdmessage = msg_job_abort;
295
 
 
296
 
      break;
297
 
 
298
 
    case MAIL_BEGIN:
299
 
 
300
 
      stdmessage = msg_job_start;
301
 
 
302
 
      break;
303
 
 
304
 
    case MAIL_END:
305
 
 
306
 
      stdmessage = msg_job_end;
307
 
 
308
 
      break;
309
 
 
310
 
    case MAIL_DEL:
311
 
 
312
 
      stdmessage = msg_job_del;
313
 
 
314
 
      break;
315
 
 
316
 
    case MAIL_STAGEIN:
317
 
 
318
 
      stdmessage = msg_job_stageinfail;
319
 
 
320
 
      break;
321
 
 
322
 
    case MAIL_OTHER:
323
 
 
324
 
    default:
325
 
 
326
 
      stdmessage = msg_job_otherfail;
327
 
 
328
 
      break;
329
 
    }  /* END switch (mailpoint) */
330
 
 
331
 
  fprintf(outmail, "PBS Job Id: %s\n",
332
 
          pjob->ji_qs.ji_jobid);
333
 
 
334
 
  fprintf(outmail, "Job Name:   %s\n",
335
 
          pjob->ji_wattr[(int)JOB_ATR_jobname].at_val.at_str);
336
 
 
337
 
  if (pjob->ji_wattr[(int)JOB_ATR_exec_host].at_flags & ATR_VFLAG_SET)
338
 
    {
339
 
    fprintf(outmail, "Exec host:  %s\n",
340
 
            pjob->ji_wattr[(int)JOB_ATR_exec_host].at_val.at_str);
341
 
    }
342
 
 
343
 
  if (stdmessage != NULL)
344
 
    {
345
 
    fprintf(outmail, "%s\n",
346
 
            stdmessage);
347
 
    }
348
 
 
349
 
  if (text != NULL)
350
 
    {
351
 
    fprintf(outmail, "%s\n",
352
 
            text);
353
 
    }
354
 
 
355
 
  fclose(outmail);
 
388
  /* Now pipe in the email body */
 
389
  svr_format_job(outmail, pjob, bodyfmt, mailpoint, text);
 
390
 
 
391
  errno = 0;
 
392
  if ((i = pclose(outmail)) != 0)
 
393
    {
 
394
    char tmpBuf[LOG_BUF_SIZE];
 
395
 
 
396
    snprintf(tmpBuf,sizeof(tmpBuf),
 
397
      "Email '%c' to %s failed: Child process '%s' %s %d (errno %d:%s)\n",
 
398
      mailpoint,
 
399
      mailto,
 
400
      cmdbuf,
 
401
      ((WIFEXITED(i)) ? ("returned") : ((WIFSIGNALED(i)) ? ("killed by signal") : ("croaked"))),
 
402
      ((WIFEXITED(i)) ? (WEXITSTATUS(i)) : ((WIFSIGNALED(i)) ? (WTERMSIG(i)) : (i))),
 
403
      errno,
 
404
      strerror(errno));
 
405
    log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
 
406
      PBS_EVENTCLASS_JOB,
 
407
      pjob->ji_qs.ji_jobid,
 
408
      tmpBuf);
 
409
    }
 
410
  else if (LOGLEVEL >= 4)
 
411
    {
 
412
    log_event(PBSEVENT_ERROR | PBSEVENT_ADMIN | PBSEVENT_JOB,
 
413
      PBS_EVENTCLASS_JOB,
 
414
      pjob->ji_qs.ji_jobid,
 
415
      "Email sent successfully\n");
 
416
    }
356
417
 
357
418
  exit(0);
358
419