author | viric <viriketo@gmail.com> |
Sun, 20 Mar 2016 11:25:53 +0100 | |
changeset 346 | 90545736507e |
parent 274 | 62048132f95f |
permissions | -rw-r--r-- |
78 | 1 |
/* |
2 |
Task Spooler - a task queue system for the unix user |
|
267 | 3 |
Copyright (C) 2007-2009 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> |
|
274
62048132f95f
Making changes so it looks like the code in ts-0.6.4, which I can't find
viric@mandarina
parents:
267
diff
changeset
|
16 |
#include <sys/time.h> /* Needed for any main.h inclusion */ |
71 | 17 |
|
18 |
#include "main.h" |
|
19 |
||
20 |
/* Returns the write pipe */ |
|
21 |
static int run_sendmail(const char *dest) |
|
22 |
{ |
|
23 |
int pid; |
|
24 |
int p[2]; |
|
25 |
||
26 |
pipe(p); |
|
27 |
||
28 |
pid = fork(); |
|
29 |
||
30 |
switch(pid) |
|
31 |
{ |
|
32 |
case 0: /* Child */ |
|
95 | 33 |
restore_sigmask(); |
71 | 34 |
close(0); |
35 |
close(1); |
|
36 |
close(2); |
|
238
d490cad20a00
Fixing some bugs in 'mail', with fixes reported by Sergio Ballestrero.
viric@vicerveza
parents:
231
diff
changeset
|
37 |
close(p[1]); |
71 | 38 |
dup2(p[0], 0); |
39 |
execl("/usr/sbin/sendmail", "sendmail", "-oi", dest, NULL); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
40 |
error("run sendmail"); |
71 | 41 |
case -1: |
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
42 |
error("fork sendmail"); |
71 | 43 |
default: /* Parent */ |
238
d490cad20a00
Fixing some bugs in 'mail', with fixes reported by Sergio Ballestrero.
viric@vicerveza
parents:
231
diff
changeset
|
44 |
close(p[0]); |
71 | 45 |
} |
46 |
return p[1]; |
|
47 |
} |
|
48 |
||
49 |
static void write_header(int fd, const char *dest, const char * command, |
|
50 |
int jobid, int errorlevel) |
|
51 |
{ |
|
147 | 52 |
fd_nprintf(fd, 100, "From: Task Spooler <taskspooler>\n"); |
53 |
fd_nprintf(fd, 500, "To: %s\n", dest); |
|
54 |
fd_nprintf(fd, 500, "Subject: the task %i finished with error %i. \n", jobid, |
|
71 | 55 |
errorlevel); |
147 | 56 |
fd_nprintf(fd, 500, "\nCommand: %s\n", command); |
57 |
fd_nprintf(fd, 500, "Output:\n"); |
|
71 | 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); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
68 |
if (file_fd == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
69 |
error("mail: Cannot open the output file %s", ofname); |
71 | 70 |
|
71 |
do { |
|
72 |
read_bytes = read(file_fd, buffer, 1000); |
|
73 |
if (read_bytes > 0) |
|
74 |
{ |
|
75 |
res = write(write_fd, buffer, read_bytes); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
76 |
if (res == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
77 |
warning("Cannot write to the mail pipe %i", write_fd); |
71 | 78 |
} |
79 |
} while (read_bytes > 0); |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
80 |
if (read_bytes == -1) |
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
81 |
warning("Cannot read the output file %s from %i", ofname, file_fd); |
71 | 82 |
} |
83 |
||
73 | 84 |
void hook_on_finish(int jobid, int errorlevel, const char *ofname, |
85 |
const char *command) |
|
86 |
{ |
|
87 |
char *onfinish; |
|
88 |
int pid; |
|
89 |
char sjobid[20]; |
|
90 |
char serrorlevel[20]; |
|
91 |
int status; |
|
92 |
||
93 |
onfinish = getenv("TS_ONFINISH"); |
|
94 |
if (onfinish == NULL) |
|
95 |
return; |
|
96 |
||
97 |
pid = fork(); |
|
98 |
||
99 |
switch(pid) |
|
100 |
{ |
|
101 |
case 0: /* Child */ |
|
95 | 102 |
restore_sigmask(); |
73 | 103 |
sprintf(sjobid, "%i", jobid); |
104 |
sprintf(serrorlevel, "%i", errorlevel); |
|
105 |
execlp(onfinish, onfinish, sjobid, serrorlevel, ofname, command, |
|
106 |
NULL); |
|
107 |
case -1: |
|
92
05004c52ecff
Better error reports on internal handled errors.
viric@llimona
parents:
78
diff
changeset
|
108 |
error("fork on finish"); |
73 | 109 |
default: /* Parent */ |
110 |
wait(&status); |
|
111 |
} |
|
112 |
} |
|
113 |
||
71 | 114 |
void send_mail(int jobid, int errorlevel, const char *ofname, |
115 |
const char *command) |
|
116 |
{ |
|
72 | 117 |
char to[101]; |
71 | 118 |
char *user; |
119 |
char *env_to; |
|
120 |
int write_fd; |
|
238
d490cad20a00
Fixing some bugs in 'mail', with fixes reported by Sergio Ballestrero.
viric@vicerveza
parents:
231
diff
changeset
|
121 |
int status; |
71 | 122 |
|
123 |
env_to = getenv("TS_MAILTO"); |
|
124 |
||
72 | 125 |
if (env_to == NULL || strlen(env_to) > 100) |
71 | 126 |
{ |
127 |
user = getenv("USER"); |
|
128 |
if (user == NULL) |
|
129 |
user = "nobody"; |
|
130 |
||
131 |
strcpy(to, user); |
|
72 | 132 |
/*strcat(to, "@localhost");*/ |
133 |
} else |
|
134 |
strcpy(to, env_to); |
|
71 | 135 |
|
136 |
write_fd = run_sendmail(to); |
|
137 |
write_header(write_fd, to, command, jobid, errorlevel); |
|
138 |
copy_output(write_fd, ofname); |
|
139 |
close(write_fd); |
|
238
d490cad20a00
Fixing some bugs in 'mail', with fixes reported by Sergio Ballestrero.
viric@vicerveza
parents:
231
diff
changeset
|
140 |
wait(&status); |
71 | 141 |
} |