File: mail.c

package info (click to toggle)
task-spooler 1.0.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 364 kB
  • sloc: ansic: 4,291; sh: 149; makefile: 105
file content (141 lines) | stat: -rw-r--r-- 3,387 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
    Task Spooler - a task queue system for the unix user
    Copyright (C) 2007-2009  LluĂ­s Batlle i Rossell

    Please find the license in the provided COPYING file.
*/
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h> /* Needed for any main.h inclusion */

#include "main.h"

/* Returns the write pipe */
static int run_sendmail(const char *dest)
{
    int pid;
    int p[2];

    pipe(p);

    pid = fork();

    switch(pid)
    {
        case 0: /* Child */
            restore_sigmask();
            close(0);
            close(1);
            close(2);
	    close(p[1]);
            dup2(p[0], 0);
            execl("/usr/sbin/sendmail", "sendmail", "-oi", dest, NULL);
            error("run sendmail");
        case -1:
            error("fork sendmail");
        default: /* Parent */
	    close(p[0]);
    }
    return p[1];
}

static void write_header(int fd, const char *dest, const char * command,
        int jobid, int errorlevel)
{
    fd_nprintf(fd, 100, "From: Task Spooler <taskspooler>\n");
    fd_nprintf(fd, 500, "To: %s\n", dest);
    fd_nprintf(fd, 500, "Subject: the task %i finished with error %i. \n", jobid,
            errorlevel);
    fd_nprintf(fd, 500, "\nCommand: %s\n", command);
    fd_nprintf(fd, 500, "Output:\n");
}

static void copy_output(int write_fd, const char *ofname)
{
    int file_fd;
    char buffer[1000];
    int read_bytes;
    int res;

    file_fd = open(ofname, O_RDONLY);
    if (file_fd == -1)
        error("mail: Cannot open the output file %s", ofname);

    do {
        read_bytes = read(file_fd, buffer, 1000);
        if (read_bytes > 0)
        {
            res = write(write_fd, buffer, read_bytes);
            if (res == -1)
                warning("Cannot write to the mail pipe %i", write_fd);
        }
    } while (read_bytes > 0);
    if (read_bytes == -1)
        warning("Cannot read the output file %s from %i", ofname, file_fd);
}

void hook_on_finish(int jobid, int errorlevel, const char *ofname,
    const char *command)
{
    char *onfinish;
    int pid;
    char sjobid[20];
    char serrorlevel[20];
    int status;

    onfinish = getenv("TS_ONFINISH");
    if (onfinish == NULL)
        return;

    pid = fork();

    switch(pid)
    {
        case 0: /* Child */
            restore_sigmask();
            sprintf(sjobid, "%i", jobid);
            sprintf(serrorlevel, "%i", errorlevel);
            execlp(onfinish, onfinish, sjobid, serrorlevel, ofname, command,
                    NULL);
        case -1:
            error("fork on finish");
        default: /* Parent */
            wait(&status);
    }
}

void send_mail(int jobid, int errorlevel, const char *ofname,
    const char *command)
{
    char to[101];
    char *user;
    char *env_to;
    int write_fd;
    int status;

    env_to = getenv("TS_MAILTO");

    if (env_to == NULL || strlen(env_to) > 100)
    {
        user = getenv("USER");
        if (user == NULL)
            user = "nobody";

        strcpy(to, user);
        /*strcat(to, "@localhost");*/
    } else
        strcpy(to, env_to);

    write_fd = run_sendmail(to);
    write_header(write_fd, to, command, jobid, errorlevel);
    copy_output(write_fd, ofname);
    close(write_fd);
    wait(&status);
}