File: mail.c

package info (click to toggle)
gridengine 6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,532 kB
  • ctags: 51,172
  • sloc: ansic: 418,155; java: 37,080; sh: 22,593; jsp: 7,699; makefile: 5,292; csh: 4,244; xml: 2,901; cpp: 2,086; perl: 1,895; tcl: 1,188; lisp: 669; ruby: 642; yacc: 393; lex: 266
file content (275 lines) | stat: -rw-r--r-- 7,183 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 * 
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 * 
 *  Sun Microsystems Inc., March, 2001
 * 
 * 
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 * 
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 * 
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 * 
 *   Copyright: 2001 by Sun Microsystems, Inc.
 * 
 *   All Rights Reserved.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdlib.h>

#include "sge_conf.h"
#include "mail.h"
#include "sgermon.h"
#include "sge_log.h"
#include "sig_handlers.h"
#include "sge_unistd.h"
#include "sge_prog.h"
#include "sge_os.h"
#include "sge_job.h"
#include "sge_mailrec.h"
#include "uti/sge_stdio.h"

#include "msg_common.h"
#include "msg_daemons_common.h"

#if defined(SOLARIS)
#   include "sge_smf.h"
#endif

#if defined(SOLARIS) || defined(ALPHA)
/* ALPHA only has wait3() prototype if _XOPEN_SOURCE_EXTENDED is defined */
pid_t wait3(int *, int, struct rusage *);
#endif

static void sge_send_mail(u_long32 progid,
                          const char *mailer, int mailer_has_subj_line, 
                          const char *user, const char *host, const char *subj,
                          const char *buf);

/*
** NAME
**   cull_mail
** PARAMETER
**   user_list     -   mail recipients list (MR_Type)
**   subj          -   subject line
**   buf           -   message contents
** RETURN
**   none
** EXTERNAL
**   sge_send_mail
** DESCRIPTION
**   sends a mail to each of the recipients in the list
*/
void cull_mail(u_long32 progid, lList *user_list, const char *subj, const char *buf, const char *mail_type) {
   char *mailer;
   int mailer_has_subj_line;
   lListElem *ep;
   const char *user, *host;

   DENTER(TOP_LAYER, "cull_mail");

   mailer = mconf_get_mailer();
   mailer_has_subj_line = 1;

   if (!buf) {
      buf = subj;
   }

   if (user_list) {
      for_each(ep, user_list) {
         user = lGetString(ep, MR_user);
         host = lGetHost(ep, MR_host);
         if (!user && !host) {
            ERROR((SGE_EVENT, MSG_MAIL_EMPTYUSERHOST));
            FREE(mailer);
            DRETURN_VOID;
         } else if (!host) {
            INFO((SGE_EVENT, MSG_MAIL_MAILUSER_SSSS, 
                  mail_type, user, mailer, subj ? subj : MSG_MAIL_NOSUBJ));
         } else {
            INFO((SGE_EVENT, MSG_MAIL_MAILUSERHOST_SSSSS, 
                  mail_type, user, host, mailer, 
                  subj ? subj : MSG_MAIL_NOSUBJ));
         }
         sge_send_mail(progid, mailer, mailer_has_subj_line, user, host, subj, buf);
      }
   } 

   FREE(mailer);
   DRETURN_VOID;
}

/************************************************************/

static void sge_send_mail(
u_long32 progid,
const char *mailer,
int mailer_has_subj_line,
const char *user,
const char *host,
const char *subj,
const char *buf 
) {
   int pid;
   int pid2;
   int i;
   int exit_status;
   int pipefds[2];
   FILE *fp;
   stringT user_str;
   bool done;

#if !(defined(CRAY) || defined(INTERIX))
   struct rusage rusage;
#endif

#if defined(SVR3) || defined(_BSD)
   union wait status;
#else
   int status;
#endif

   DENTER(TOP_LAYER, "sge_send_mail");

   if (!user) {
      DEXIT;
      return;
   }

   alarm(0);

#if defined(SOLARIS)
   char err_str[256];
   i = sge_smf_contract_fork(err_str, 256);
#else
   i = fork();
#endif
   if (i>0) {
      DPRINTF(("PARENT RETURNS\n"));
      DEXIT;
      return;
   }
   /* We just log that fork was not succesful */
   else if (i<0) {
#if defined(SOLARIS)
       ERROR((SGE_EVENT, MSG_SMF_MAIL_FORK_FAILED_S, err_str));
#else
       ERROR((SGE_EVENT, MSG_MAIL_NOFORK));
#endif
   }

   DPRINTF(("CHILD CONTINUES\n"));
   SETPGRP;

   sge_close_all_fds(NULL);

   /* 
      may never call SGE_EXIT() here because
      leave_commd() gets called by a the mailer child
      and leave_commd() unregisters the commproc
   */
   if (pipe(pipefds) < 0) {
      ERROR((SGE_EVENT, MSG_MAIL_NOPIPE));
      exit(1);
   }
   /* Don't need to start in new contract on Solaris - already in new one */
   if ((pid = fork()) < 0) {
      ERROR((SGE_EVENT, MSG_MAIL_NOFORK));
      exit(1);
   }
   if (!pid) {
      if (host)
         sprintf(user_str, "%s@%s", user, host);
      else
         sprintf(user_str, "%s", user);

      if (dup2(pipefds[0], 0) < 0) {
         CRITICAL((SGE_EVENT, MSG_MAIL_NODUP));
         exit(1);
      }

      close(pipefds[1]);

      if (mailer_has_subj_line) {
         DPRINTF(("%s mail -s %s %s", mailer, subj, user_str));  
         execl(mailer, "mail", "-s", subj, user_str, NULL);
      }
      else {
         DPRINTF(("%s mail %s", mailer, user_str));  
         execl(mailer, "mail", user_str, NULL);
      }
      CRITICAL((SGE_EVENT, MSG_MAIL_NOEXEC_S, mailer));
      exit(1);
   }

   close(pipefds[0]);
   fp = fdopen(pipefds[1], "w");
   fprintf(fp, "%s\n", buf);
   FCLOSE(fp);

   sge_setup_sig_handlers(progid);

   done = false;
   while (!done) {
      alarm(60);                /* max time to allow for mail */
      sigprocmask(SIG_SETMASK, &io_mask, &omask);
      sigaction(SIGALRM, &sigalrm_vec, &sigalrm_ovec);

#if defined(CRAY) || defined(INTERIX)
      pid2 = waitpid(pid, &status, 0);
#else
      pid2 = wait3(&status, 0, &rusage);
#endif

      alarm(0);
      if (pid2 == 0) {          /* how could this happen? */
         kill(pid, SIGKILL);
         ERROR((SGE_EVENT, MSG_MAIL_NOMAIL1));
         exit(1);
      }

      if (pid2 == -1) {         /* alarm must have went off */
         kill(pid, SIGKILL);
         ERROR((SGE_EVENT, MSG_MAIL_NOMAIL2));
         exit(1);
      }

      if (WIFSTOPPED(status)) { /* how could this happen? */
         kill(pid, SIGKILL);
         ERROR((SGE_EVENT, MSG_MAIL_NOMAIL3_I, WSTOPSIG(status)));
         exit(1);
      }

#if defined(SVR3) || defined(_BSD)
      exit_status = status.w_retcode;
#else
      exit_status = status;
#endif
      DPRINTF(("mailer exited with exit status %d\n", exit_status));
      exit(exit_status);
   }
FCLOSE_ERROR:
   CRITICAL((SGE_EVENT, MSG_FILE_ERRORCLOSEINGXY_SS, "<pipefds>", strerror(errno)));
   exit(1);
}