author | viric@mandarina |
Mon, 05 May 2008 23:14:22 +0200 | |
changeset 216 | b4476e12a717 |
parent 147 | e173645f5221 |
child 231 | 558b281b88f5 |
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 |
{ |
|
147 | 50 |
fd_nprintf(fd, 100, "From: Task Spooler <taskspooler>\n"); |
51 |
fd_nprintf(fd, 500, "To: %s\n", dest); |
|
52 |
fd_nprintf(fd, 500, "Subject: the task %i finished with error %i. \n", jobid, |
|
71 | 53 |
errorlevel); |
147 | 54 |
fd_nprintf(fd, 500, "\nCommand: %s\n", command); |
55 |
fd_nprintf(fd, 500, "Output:\n"); |
|
71 | 56 |
} |
57 |
||
58 |
static void copy_output(int write_fd, const char *ofname) |
|
59 |
{ |
|
60 |
int file_fd; |
|
61 |
char buffer[1000]; |
|
62 |
int read_bytes; |
|
63 |
int res; |
|
64 |
||
65 |
file_fd = open(ofname, O_RDONLY); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
66 |
if (file_fd == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
67 |
error("mail: Cannot open the output file %s", ofname); |
71 | 68 |
|
69 |
do { |
|
70 |
read_bytes = read(file_fd, buffer, 1000); |
|
71 |
if (read_bytes > 0) |
|
72 |
{ |
|
73 |
res = write(write_fd, buffer, read_bytes); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
74 |
if (res == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
75 |
warning("Cannot write to the mail pipe %i", write_fd); |
71 | 76 |
} |
77 |
} while (read_bytes > 0); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
78 |
if (read_bytes == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
79 |
warning("Cannot read the output file %s from %i", ofname, file_fd); |
71 | 80 |
} |
81 |
||
73 | 82 |
void hook_on_finish(int jobid, int errorlevel, const char *ofname, |
83 |
const char *command) |
|
84 |
{ |
|
85 |
char *onfinish; |
|
86 |
int pid; |
|
87 |
char sjobid[20]; |
|
88 |
char serrorlevel[20]; |
|
89 |
int status; |
|
90 |
||
91 |
onfinish = getenv("TS_ONFINISH"); |
|
92 |
if (onfinish == NULL) |
|
93 |
return; |
|
94 |
||
95 |
pid = fork(); |
|
96 |
||
97 |
switch(pid) |
|
98 |
{ |
|
99 |
case 0: /* Child */ |
|
95 | 100 |
restore_sigmask(); |
73 | 101 |
sprintf(sjobid, "%i", jobid); |
102 |
sprintf(serrorlevel, "%i", errorlevel); |
|
103 |
execlp(onfinish, onfinish, sjobid, serrorlevel, ofname, command, |
|
104 |
NULL); |
|
105 |
case -1: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
106 |
error("fork on finish"); |
73 | 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 |
} |