File: mail.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-4%2Bdeb9u2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 56,460 kB
  • sloc: ansic: 432,641; java: 87,068; cpp: 31,958; sh: 29,419; jsp: 7,757; perl: 6,336; xml: 5,825; makefile: 4,703; csh: 3,934; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361
file content (283 lines) | stat: -rw-r--r-- 7,853 bytes parent folder | download | duplicates (5)
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
276
277
278
279
280
281
282
283
/*___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 "uti/sge_rmon.h"
#include "uti/sge_log.h"
#include "uti/sge_unistd.h"
#include "uti/sge_prog.h"
#include "uti/sge_os.h"
#include "uti/sge_stdio.h"
#include "uti/config_file.h"
#include "uti/sge_uidgid.h"

#include "sgeobj/sge_conf.h"
#include "sgeobj/sge_job.h"
#include "sgeobj/sge_mailrec.h"

#include "mail.h"
#include "sig_handlers.h"
#include "msg_common.h"
#include "msg_daemons_common.h"

#if defined(SOLARIS)
#   include "sge_smf.h"
#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, SFNMAX, MSG_MAIL_EMPTYUSERHOST));
            sge_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);
      }
   } 

   sge_free(&mailer);
   DRETURN_VOID;
}

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

static void sge_send_mail(u_long32 progid, const char *mailer_in,
                          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;
   char *mailer;

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

   int status;

   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);
   /* 
    * -2 and lower is SMF contract failure, 
    * -1 is fork() failure and will be handled later 
    */
   if (i < -1){
      ERROR((SGE_EVENT, MSG_SMF_MAIL_FORK_FAILED_S, err_str));
   }
#else
   i = fork();
#endif
   if (i>0) {
      DPRINTF(("PARENT RETURNS\n"));
      DEXIT;
      return;
   }
   /* log fork() failure */
   else if (i == -1) { /* still in parent */
      ERROR((SGE_EVENT, MSG_MAIL_NOFORK_S, strerror(errno)));
      DEXIT;
      return;
   } /* else in child */

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

   sge_close_all_fds(NULL, 0);

   /* 
      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_S, strerror(errno)));
      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_S, strerror(errno)));
      exit(1);
   }
   if (!pid) {
      if (host)
         snprintf(user_str, sizeof(user_str), "%s@%s", user, host);
      else
         snprintf(user_str, sizeof(user_str), "%s", user);

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

      close(pipefds[1]);

      mailer = strdup(mailer_in);
      /* switch user */
      {
        char *user = parse_script_params(&mailer);
        if (user != NULL) {
           char err_str[256];
           if (sge_set_uid_gid_addgrp(user, NULL, 0, 0, 0, err_str,
                                      sizeof(err_str), 0, 0, false)) {
             ERROR((SGE_EVENT, MSG_SYSTEM_SWITCHTOUSERFAILED_SS, user,
                    strerror(errno)));
             sge_free(&mailer);
             DEXIT;
             return;
           }
        }
      }

      if (mailer_has_subj_line) {
         DPRINTF(("%s mail -s %s %s", mailer, subj, user_str));  
         execl(mailer, "mail", "-s", subj, user_str, (char *) NULL);
      }
      else {
         DPRINTF(("%s mail %s", mailer, user_str));  
         execl(mailer, "mail", user_str, (char *) NULL);
      }
      CRITICAL((SGE_EVENT, MSG_MAIL_NOEXEC_SS, mailer, strerror(errno)));
      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(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, SFNMAX, MSG_MAIL_NOMAIL1));
         exit(1);
      }

      if (pid2 == -1) {         /* alarm must have went off */
         kill(pid, SIGKILL);
         ERROR((SGE_EVENT, SFNMAX, 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);
      }

      exit_status = status;
      DPRINTF(("mailer exited with exit status %d\n", exit_status));
      exit(exit_status);
   }
FCLOSE_ERROR:
   CRITICAL((SGE_EVENT, MSG_FILE_NOCLOSE_SS, "<pipefds>", strerror(errno)));
   exit(1);
}