Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / ctm / ctm_dequeue / ctm_dequeue.c
1 /*
2  * Copyright (c) 1996, Gary J. Palmer
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer,
11  *    verbatim and that no modifications are made prior to this
12  *    point in the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/usr.sbin/ctm/ctm_dequeue/ctm_dequeue.c,v 1.9 1999/08/28 01:16:01 peter Exp $
30  * $DragonFly: src/usr.sbin/ctm/ctm_dequeue/Attic/ctm_dequeue.c,v 1.2 2003/06/17 04:29:53 dillon Exp $
31  */
32
33 /*
34  * ctm_dequeue:  Dequeue queued delta pieces and mail them.
35  *
36  * The pieces have already been packaged up as mail messages by ctm_smail,
37  * and will be simply passed to sendmail in alphabetical order.
38  */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/wait.h>
48 #include <fts.h>
49 #include <limits.h>
50 #include <errno.h>
51 #include <paths.h>
52 #include "error.h"
53 #include "options.h"
54
55 #define DEFAULT_NUM     1       /* Default number of pieces mailed per run. */
56
57 int fts_sort(const FTSENT **, const FTSENT **);
58 int run_sendmail(int ifd);
59
60 int
61 main(int argc, char **argv)
62 {
63     char *log_file = NULL;
64     char *queue_dir = NULL;
65     char *list[2];
66     int num_to_send = DEFAULT_NUM, chunk;
67     int fd;
68     FTS *fts;
69     FTSENT *ftsent;
70     int piece, npieces;
71     char filename[PATH_MAX];
72
73     err_prog_name(argv[0]);
74
75     OPTIONS("[-l log] [-n num] queuedir")
76         NUMBER('n', num_to_send)
77         STRING('l', log_file)
78     ENDOPTS
79
80     if (argc != 2)
81         usage();
82
83     if (log_file)
84         err_set_log(log_file);
85
86     queue_dir = argv[1];
87     list[0] = queue_dir;
88     list[1] = NULL;
89
90     fts = fts_open(list, FTS_PHYSICAL|FTS_COMFOLLOW, fts_sort);
91     if (fts == NULL)
92     {
93         err("fts failed on `%s'", queue_dir);
94         exit(1);
95     }
96
97     ftsent = fts_read(fts);
98     if (ftsent == NULL || ftsent->fts_info != FTS_D)
99     {
100         err("not a directory: %s", queue_dir);
101         exit(1);
102     }
103
104     ftsent = fts_children(fts, 0);
105     if (ftsent == NULL && errno)
106     {
107         err("*ftschildren failed");
108         exit(1);
109     }
110
111     for (chunk = 1; ftsent != NULL; ftsent = ftsent->fts_link)
112     {
113         /*
114          * Skip non-files and ctm_smail tmp files (ones starting with `.')
115          */
116         if (ftsent->fts_info != FTS_F || ftsent->fts_name[0] == '.')
117             continue;
118
119         sprintf(filename, "%s/%s", queue_dir, ftsent->fts_name);
120         fd = open(filename, O_RDONLY);
121         if (fd < 0)
122         {
123             err("*open: %s", filename);
124             exit(1);
125         }
126
127         if (run_sendmail(fd))
128             exit(1);
129
130         close(fd);
131         
132         if (unlink(filename) < 0)
133         {
134             err("*unlink: %s", filename);
135             exit(1);
136         }
137         
138         /*
139          * Deduce the delta, piece number, and number of pieces from
140          * the name of the file in the queue.  Ideally, we should be
141          * able to get the mail alias name too.
142          *
143          * NOTE: This depends intimately on the queue name used in ctm_smail.
144          */
145         npieces = atoi(&ftsent->fts_name[ftsent->fts_namelen-3]);
146         piece = atoi(&ftsent->fts_name[ftsent->fts_namelen-7]);
147         err("%.*s %d/%d sent", ftsent->fts_namelen-8, ftsent->fts_name,
148                 piece, npieces);
149
150         if (chunk++ == num_to_send)
151             break;
152     }
153
154     fts_close(fts);
155
156     return(0);
157 }
158
159 int
160 fts_sort(const FTSENT ** a, const FTSENT ** b)
161 {
162     if ((*a)->fts_info != FTS_F)
163         return(0);
164     if ((*a)->fts_info != FTS_F)
165         return(0);
166
167     return (strcmp((*a)->fts_name, (*b)->fts_name));
168 }
169
170 /*
171  * Run sendmail with the given file descriptor as standard input.
172  * Sendmail will decode the destination from the message contents.
173  * Returns 0 on success, 1 on failure.
174  */
175 int
176 run_sendmail(int ifd)
177 {
178     pid_t child, pid;
179     int status;
180
181     switch (child = fork())
182     {
183     case -1:
184         err("*fork");
185         return(1);
186
187     case 0:     /* Child */
188         dup2(ifd, 0);
189         execl(_PATH_SENDMAIL, _PATH_SENDMAIL, "-odq", "-t", NULL);
190         err("*exec: %s", _PATH_SENDMAIL);
191         _exit(1);
192
193     default:    /* Parent */
194         while ((pid = wait(&status)) != child)
195         {
196             if (pid == -1 && errno != EINTR)
197             {
198                 err("*wait");
199                 return(1);
200             }
201         }
202         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
203         {
204             err("sendmail failed");
205             return(1);
206         }
207     }
208
209     return(0);
210 }