71
|
1 |
#include <signal.h>
|
|
2 |
#include <assert.h>
|
|
3 |
#include <string.h>
|
|
4 |
#include <sys/types.h>
|
|
5 |
#include <sys/wait.h>
|
|
6 |
#include <stdlib.h>
|
|
7 |
#include <unistd.h>
|
|
8 |
#include <stdio.h>
|
|
9 |
#include <sys/stat.h>
|
|
10 |
#include <fcntl.h>
|
|
11 |
|
|
12 |
#include "main.h"
|
|
13 |
|
|
14 |
/* Returns the write pipe */
|
|
15 |
static int run_sendmail(const char *dest)
|
|
16 |
{
|
|
17 |
int pid;
|
|
18 |
int p[2];
|
|
19 |
|
|
20 |
pipe(p);
|
|
21 |
|
|
22 |
pid = fork();
|
|
23 |
|
|
24 |
switch(pid)
|
|
25 |
{
|
|
26 |
case 0: /* Child */
|
|
27 |
close(0);
|
|
28 |
close(1);
|
|
29 |
close(2);
|
|
30 |
dup2(p[0], 0);
|
|
31 |
execl("/usr/sbin/sendmail", "sendmail", "-oi", dest, NULL);
|
|
32 |
exit(-1);
|
|
33 |
case -1:
|
|
34 |
perror("fork sendmail");
|
|
35 |
exit(-1);
|
|
36 |
default: /* Parent */
|
|
37 |
;
|
|
38 |
}
|
|
39 |
return p[1];
|
|
40 |
}
|
|
41 |
|
|
42 |
static void write_header(int fd, const char *dest, const char * command,
|
|
43 |
int jobid, int errorlevel)
|
|
44 |
{
|
|
45 |
FILE *f;
|
|
46 |
|
|
47 |
f = fdopen(fd, "a");
|
|
48 |
assert(f != NULL);
|
|
49 |
|
73
|
50 |
fprintf(f, "From: Task Spooler <taskspooler>\n");
|
71
|
51 |
fprintf(f, "To: %s\n", dest);
|
|
52 |
fprintf(f, "Subject: the task %i finished with error %i. \n", jobid,
|
|
53 |
errorlevel);
|
|
54 |
fprintf(f, "\nCommand: %s\n", command);
|
|
55 |
fprintf(f, "Output:\n");
|
|
56 |
|
|
57 |
fflush(f);
|
|
58 |
}
|
|
59 |
|
|
60 |
static void copy_output(int write_fd, const char *ofname)
|
|
61 |
{
|
|
62 |
int file_fd;
|
|
63 |
char buffer[1000];
|
|
64 |
int read_bytes;
|
|
65 |
int res;
|
|
66 |
|
|
67 |
file_fd = open(ofname, O_RDONLY);
|
|
68 |
assert(file_fd != -1);
|
|
69 |
|
|
70 |
do {
|
|
71 |
read_bytes = read(file_fd, buffer, 1000);
|
|
72 |
if (read_bytes > 0)
|
|
73 |
{
|
|
74 |
res = write(write_fd, buffer, read_bytes);
|
|
75 |
assert(res != -1);
|
|
76 |
}
|
|
77 |
} while (read_bytes > 0);
|
|
78 |
assert(read_bytes != -1);
|
|
79 |
}
|
|
80 |
|
73
|
81 |
void hook_on_finish(int jobid, int errorlevel, const char *ofname,
|
|
82 |
const char *command)
|
|
83 |
{
|
|
84 |
char *onfinish;
|
|
85 |
int pid;
|
|
86 |
char sjobid[20];
|
|
87 |
char serrorlevel[20];
|
|
88 |
int status;
|
|
89 |
|
|
90 |
onfinish = getenv("TS_ONFINISH");
|
|
91 |
if (onfinish == NULL)
|
|
92 |
return;
|
|
93 |
|
|
94 |
pid = fork();
|
|
95 |
|
|
96 |
switch(pid)
|
|
97 |
{
|
|
98 |
case 0: /* Child */
|
|
99 |
sprintf(sjobid, "%i", jobid);
|
|
100 |
sprintf(serrorlevel, "%i", errorlevel);
|
|
101 |
execlp(onfinish, onfinish, sjobid, serrorlevel, ofname, command,
|
|
102 |
NULL);
|
|
103 |
exit(-1);
|
|
104 |
case -1:
|
|
105 |
perror("fork on finish");
|
|
106 |
exit(-1);
|
|
107 |
default: /* Parent */
|
|
108 |
wait(&status);
|
|
109 |
}
|
|
110 |
}
|
|
111 |
|
71
|
112 |
void send_mail(int jobid, int errorlevel, const char *ofname,
|
|
113 |
const char *command)
|
|
114 |
{
|
72
|
115 |
char to[101];
|
71
|
116 |
char *user;
|
|
117 |
char *env_to;
|
|
118 |
int write_fd;
|
|
119 |
|
|
120 |
env_to = getenv("TS_MAILTO");
|
|
121 |
|
72
|
122 |
if (env_to == NULL || strlen(env_to) > 100)
|
71
|
123 |
{
|
|
124 |
user = getenv("USER");
|
|
125 |
if (user == NULL)
|
|
126 |
user = "nobody";
|
|
127 |
|
|
128 |
strcpy(to, user);
|
72
|
129 |
/*strcat(to, "@localhost");*/
|
|
130 |
} else
|
|
131 |
strcpy(to, env_to);
|
71
|
132 |
|
|
133 |
write_fd = run_sendmail(to);
|
|
134 |
write_header(write_fd, to, command, jobid, errorlevel);
|
|
135 |
copy_output(write_fd, ofname);
|
|
136 |
close(write_fd);
|
|
137 |
}
|