author | viric@mandarina |
Sun, 15 Feb 2009 19:12:08 +0100 | |
changeset 261 | dc8f1609bad8 |
parent 238 | d490cad20a00 |
child 267 | 11631dd11ff8 |
permissions | -rw-r--r-- |
78 | 1 |
/* |
2 |
Task Spooler - a task queue system for the unix user |
|
231
558b281b88f5
Updating the Copyright years from 2007 to 2007-2008.
llbatlle@taga
parents:
147
diff
changeset
|
3 |
Copyright (C) 2007-2008 LluĂs Batlle i Rossell |
78 | 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); |
|
238
d490cad20a00
Fixing some bugs in 'mail', with fixes reported by Sergio Ballestrero.
viric@vicerveza
parents:
231
diff
changeset
|
36 |
close(p[1]); |
71 | 37 |
dup2(p[0], 0); |
38 |
execl("/usr/sbin/sendmail", "sendmail", "-oi", dest, NULL); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
39 |
error("run sendmail"); |
71 | 40 |
case -1: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
41 |
error("fork sendmail"); |
71 | 42 |
default: /* Parent */ |
238
d490cad20a00
Fixing some bugs in 'mail', with fixes reported by Sergio Ballestrero.
viric@vicerveza
parents:
231
diff
changeset
|
43 |
close(p[0]); |
71 | 44 |
} |
45 |
return p[1]; |
|
46 |
} |
|
47 |
||
48 |
static void write_header(int fd, const char *dest, const char * command, |
|
49 |
int jobid, int errorlevel) |
|
50 |
{ |
|
147 | 51 |
fd_nprintf(fd, 100, "From: Task Spooler <taskspooler>\n"); |
52 |
fd_nprintf(fd, 500, "To: %s\n", dest); |
|
53 |
fd_nprintf(fd, 500, "Subject: the task %i finished with error %i. \n", jobid, |
|
71 | 54 |
errorlevel); |
147 | 55 |
fd_nprintf(fd, 500, "\nCommand: %s\n", command); |
56 |
fd_nprintf(fd, 500, "Output:\n"); |
|
71 | 57 |
} |
58 |
||
59 |
static void copy_output(int write_fd, const char *ofname) |
|
60 |
{ |
|
61 |
int file_fd; |
|
62 |
char buffer[1000]; |
|
63 |
int read_bytes; |
|
64 |
int res; |
|
65 |
||
66 |
file_fd = open(ofname, O_RDONLY); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
67 |
if (file_fd == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
68 |
error("mail: Cannot open the output file %s", ofname); |
71 | 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); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
75 |
if (res == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
76 |
warning("Cannot write to the mail pipe %i", write_fd); |
71 | 77 |
} |
78 |
} while (read_bytes > 0); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
79 |
if (read_bytes == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
80 |
warning("Cannot read the output file %s from %i", ofname, file_fd); |
71 | 81 |
} |
82 |
||
73 | 83 |
void hook_on_finish(int jobid, int errorlevel, const char *ofname, |
84 |
const char *command) |
|
85 |
{ |
|
86 |
char *onfinish; |
|
87 |
int pid; |
|
88 |
char sjobid[20]; |
|
89 |
char serrorlevel[20]; |
|
90 |
int status; |
|
91 |
||
92 |
onfinish = getenv("TS_ONFINISH"); |
|
93 |
if (onfinish == NULL) |
|
94 |
return; |
|
95 |
||
96 |
pid = fork(); |
|
97 |
||
98 |
switch(pid) |
|
99 |
{ |
|
100 |
case 0: /* Child */ |
|
95 | 101 |
restore_sigmask(); |
73 | 102 |
sprintf(sjobid, "%i", jobid); |
103 |
sprintf(serrorlevel, "%i", errorlevel); |
|
104 |
execlp(onfinish, onfinish, sjobid, serrorlevel, ofname, command, |
|
105 |
NULL); |
|
106 |
case -1: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
107 |
error("fork on finish"); |
73 | 108 |
default: /* Parent */ |
109 |
wait(&status); |
|
110 |
} |
|
111 |
} |
|
112 |
||
71 | 113 |
void send_mail(int jobid, int errorlevel, const char *ofname, |
114 |
const char *command) |
|
115 |
{ |
|
72 | 116 |
char to[101]; |
71 | 117 |
char *user; |
118 |
char *env_to; |
|
119 |
int write_fd; |
|
238
d490cad20a00
Fixing some bugs in 'mail', with fixes reported by Sergio Ballestrero.
viric@vicerveza
parents:
231
diff
changeset
|
120 |
int status; |
71 | 121 |
|
122 |
env_to = getenv("TS_MAILTO"); |
|
123 |
||
72 | 124 |
if (env_to == NULL || strlen(env_to) > 100) |
71 | 125 |
{ |
126 |
user = getenv("USER"); |
|
127 |
if (user == NULL) |
|
128 |
user = "nobody"; |
|
129 |
||
130 |
strcpy(to, user); |
|
72 | 131 |
/*strcat(to, "@localhost");*/ |
132 |
} else |
|
133 |
strcpy(to, env_to); |
|
71 | 134 |
|
135 |
write_fd = run_sendmail(to); |
|
136 |
write_header(write_fd, to, command, jobid, errorlevel); |
|
137 |
copy_output(write_fd, ofname); |
|
138 |
close(write_fd); |
|
238
d490cad20a00
Fixing some bugs in 'mail', with fixes reported by Sergio Ballestrero.
viric@vicerveza
parents:
231
diff
changeset
|
139 |
wait(&status); |
71 | 140 |
} |