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