2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1996, Gary J. Palmer
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer,
13 * verbatim and that no modifications are made prior to this
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * ctm_dequeue: Dequeue queued delta pieces and mail them.
37 * The pieces have already been packaged up as mail messages by ctm_smail,
38 * and will be simply passed to sendmail in alphabetical order.
46 #include <sys/types.h>
56 #define DEFAULT_NUM 1 /* Default number of pieces mailed per run. */
58 int fts_sort(const FTSENT * const *, const FTSENT * const *);
59 int run_sendmail(int ifd);
62 main(int argc, char **argv)
64 char *log_file = NULL;
65 char *queue_dir = NULL;
67 int num_to_send = DEFAULT_NUM, chunk;
72 char filename[PATH_MAX];
74 err_prog_name(argv[0]);
76 OPTIONS("[-l log] [-n num] queuedir")
77 NUMBER('n', num_to_send)
85 err_set_log(log_file);
91 fts = fts_open(list, FTS_PHYSICAL|FTS_COMFOLLOW, fts_sort);
94 err("fts failed on `%s'", queue_dir);
98 ftsent = fts_read(fts);
99 if (ftsent == NULL || ftsent->fts_info != FTS_D)
101 err("not a directory: %s", queue_dir);
105 ftsent = fts_children(fts, 0);
106 if (ftsent == NULL && errno)
108 err("*ftschildren failed");
112 for (chunk = 1; ftsent != NULL; ftsent = ftsent->fts_link)
115 * Skip non-files and ctm_smail tmp files (ones starting with `.')
117 if (ftsent->fts_info != FTS_F || ftsent->fts_name[0] == '.')
120 sprintf(filename, "%s/%s", queue_dir, ftsent->fts_name);
121 fd = open(filename, O_RDONLY);
124 err("*open: %s", filename);
128 if (run_sendmail(fd))
133 if (unlink(filename) < 0)
135 err("*unlink: %s", filename);
140 * Deduce the delta, piece number, and number of pieces from
141 * the name of the file in the queue. Ideally, we should be
142 * able to get the mail alias name too.
144 * NOTE: This depends intimately on the queue name used in ctm_smail.
146 npieces = atoi(&ftsent->fts_name[ftsent->fts_namelen-3]);
147 piece = atoi(&ftsent->fts_name[ftsent->fts_namelen-7]);
148 err("%.*s %d/%d sent", (int)(ftsent->fts_namelen-8), ftsent->fts_name,
151 if (chunk++ == num_to_send)
161 fts_sort(const FTSENT * const * a, const FTSENT * const * b)
163 if ((*a)->fts_info != FTS_F)
165 if ((*a)->fts_info != FTS_F)
168 return (strcmp((*a)->fts_name, (*b)->fts_name));
172 * Run sendmail with the given file descriptor as standard input.
173 * Sendmail will decode the destination from the message contents.
174 * Returns 0 on success, 1 on failure.
177 run_sendmail(int ifd)
182 switch (child = fork())
190 execl(_PATH_SENDMAIL, _PATH_SENDMAIL, "-odq", "-t", (char *)NULL);
191 err("*exec: %s", _PATH_SENDMAIL);
194 default: /* Parent */
195 while ((pid = wait(&status)) != child)
197 if (pid == -1 && errno != EINTR)
203 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
205 err("sendmail failed");