1 /* -*- c-file-style: "java"; indent-tabs-mode: nil; tab-width: 4; fill-column: 78 -*-
2  *
3  * Copyright 2007, 2009 Google Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
18  * USA.
19  */
20 
21 #include <config.h>
22 
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 
32 #include "emaillog.h"
33 #include "distcc.h"
34 #include "util.h"
35 #include "trace.h"
36 #include "bulk.h"
37 #include "snprintf.h"
38 #include "exitcode.h"
39 
40 /* if never_send_email is true, we won't send email
41    even if should_send_email is true */
42 static int should_send_email = 0;
43 static int never_send_email = 0;
44 static char *email_fname;
45 static int email_fileno = -1;
46 static int email_errno;
47 
48 static const char logmailer[] = "/bin/mail";
49 static const char email_subject[] = "distcc-pump email" ;
50 static const char cant_send_message_format[] =
51    "Please notify %s that distcc tried to send them email but failed";
52 static const char will_send_message_format[] = "Will send an email to %s";
53 
54 static const char dcc_emaillog_whom_to_blame[] = DCC_EMAILLOG_WHOM_TO_BLAME;
55 
dcc_please_send_email(void)56 void dcc_please_send_email(void) {
57     should_send_email = 1;
58 }
59 
dcc_setup_log_email(void)60 void dcc_setup_log_email(void) {
61     never_send_email = !dcc_getenv_bool("DISTCC_ENABLE_DISCREPANCY_EMAIL", 0);
62     if (never_send_email)
63       return;
64 
65     /* email_fname lives until the program exits.
66        The file itself will eventually get unlinked by dcc_cleanup_tempfiles(),
67        but email_fileno survives until after we send email, so the file won't
68        get removed until the emailing (child) process is done.
69     */
70 
71     dcc_make_tmpnam("distcc_error_log", "txt", &email_fname);
72 
73     email_fileno = open(email_fname, O_RDWR | O_TRUNC);
74     if (email_fileno >= 0) {
75       rs_add_logger(rs_logger_file, RS_LOG_DEBUG, NULL, email_fileno);
76       rs_trace_set_level(RS_LOG_DEBUG);
77     } else {
78        email_errno = errno;
79     }
80 }
81 
dcc_add_file_to_log_email(const char * description,const char * fname)82 int dcc_add_file_to_log_email(const char *description,
83                               const char *fname) {
84   char begin[] = "\nBEGIN ";
85   char end[] = "\nEND ";
86   int in_fd = 0;
87   off_t fsize;
88   int ret;
89 
90   if (never_send_email) return 0;
91 
92   ret = dcc_open_read(fname, &in_fd, &fsize);
93   if (ret != 0) return ret;
94 
95   ret = write(email_fileno, begin, strlen(begin));
96   if (ret != (ssize_t) strlen(begin)) return EXIT_IO_ERROR;
97 
98   ret = write(email_fileno, description, strlen(description));
99   if (ret != (ssize_t) strlen(description)) return EXIT_IO_ERROR;
100 
101   ret = write(email_fileno, "\n", 1);
102   if (ret != 1) return EXIT_IO_ERROR;
103 
104   ret = dcc_pump_readwrite(email_fileno, in_fd, fsize);
105   if (ret != 0) return ret;
106 
107   ret = write(email_fileno, end, strlen(end));
108   if (ret != (ssize_t) strlen(end)) return EXIT_IO_ERROR;
109 
110   ret = write(email_fileno, description, strlen(description));
111   if (ret != (ssize_t) strlen(description)) return EXIT_IO_ERROR;
112 
113   ret = write(email_fileno, "\n", 1);
114   if (ret != 1) return EXIT_IO_ERROR;
115 
116   close(in_fd);
117 
118   return 0;
119 }
120 
dcc_maybe_send_email(void)121 void dcc_maybe_send_email(void) {
122   int child_pid = 0;
123   const char *whom_to_blame;
124   if ((whom_to_blame = getenv("DISTCC_EMAILLOG_WHOM_TO_BLAME"))
125       == NULL) {
126     whom_to_blame = dcc_emaillog_whom_to_blame;
127   }
128   char *cant_send_message_to;
129   int ret;
130 
131   if (should_send_email == 0) return;
132   if (never_send_email) return;
133 
134   rs_log_warning(will_send_message_format, whom_to_blame);
135   ret = asprintf(&cant_send_message_to, cant_send_message_format, whom_to_blame);
136   if (ret == -1) {
137       fprintf(stderr, "error sending email: asprintf() failed");
138       return;
139   }
140 
141   if (email_fileno < 0) {
142       errno = email_errno;
143       perror(cant_send_message_to);
144       free(cant_send_message_to);
145       return;
146   }
147 
148   child_pid = fork();
149   if (child_pid == 0) {
150     if (dup2(email_fileno, 0) == -1 ||
151         lseek(email_fileno, 0, SEEK_SET) == -1 ||
152         execl(logmailer,
153               logmailer, "-s", email_subject, whom_to_blame,
154               (char*)NULL) == -1) {
155       perror(cant_send_message_to);
156       /* The fork succeeded but we didn't get to exec, or the exec
157          failed. We need to exit immediately, otherwise the cleanup
158          code will get executed twice.
159       */
160       _exit(1);
161     }
162   } else if (child_pid < 0) {
163       perror(cant_send_message_to);
164   }
165   free(cant_send_message_to);
166 }
167