chiark / gitweb /
Properly document new backlog file resilience
[inn-innduct.git] / backends / send-uucp.in
1 #!/usr/bin/perl -w
2 # fixscript will replace this line with code to load innshellvars
3
4 ##############################################################################
5 # send-uucp.pl  create news batches from the outgoing files
6 #
7 # Author:       Edvard Tuinder <ed@elm.net>
8 #
9 # Copyright (C) 1994 Edvard Tuinder - ELM Consultancy B.V.
10 # Copyright (C) 1995-1997 Miquel van Smoorenburg - Cistron Internet Services
11 #
12 # Copyright (C) 2003 Marco d'Itri <md@linux.it>
13 #   Nearly rewritten. Added syslog support, real errors checking and more.
14 #
15 # This program is free software; you can redistribute it and/or modify it
16 # under the terms of the GNU General Public License as published by the Free
17 # Software Foundation; either version 2 of the License, or (at your option)
18 # any later version.
19 ##############################################################################
20
21 use strict;
22 use Sys::Syslog;
23
24 # for compatibility with earlier versions of INN
25 $inn::pathetc ||= '/etc/news';
26 $inn::syslog_facility ||= 'news';
27 $inn::uux ||= 'uux';
28
29 # some default values
30 my $MAXSIZE = 500000;
31 my $MAXJOBS = 200;
32
33 my %UNBATCHER = (
34     compress    => 'cunbatch',
35     bzip2       => 'bunbatch',
36     gzip        => 'gunbatch',
37 );
38
39 my $UUX_FLAGS = '- -z -r -gd';
40 my $BATCHER_FLAGS = '';
41
42 ##############################################################################
43 my $config_file = $inn::pathetc . '/send-uucp.cf';
44 my $lockfile = $inn::locks . '/LOCK.send-uucp';
45
46 openlog('send-uucp', 'pid', $inn::syslog_facility);
47
48 my @sitelist;
49 if (@ARGV) {
50     foreach my $site (@ARGV) {
51         my @cfg = read_cf($config_file, $site);
52         if (not @cfg) {
53             logmsg("site $site not found in the configuration", 'err');
54             next;
55         }
56         push @sitelist, @cfg;
57     }
58 } else {
59     @sitelist = read_cf($config_file, undef);
60 }
61
62 if (not @sitelist) {
63     logmsg('nothing to do', 'debug');
64     exit 0;
65 }
66
67 chdir $inn::batch or logdie("Can't access $inn::batch: $!", 'crit');
68
69 shlock($lockfile);
70
71 run_site($_) foreach @sitelist;
72 unlink $lockfile;
73 exit 0;
74
75 # lint food
76 $inn::compress.$inn::locks.$inn::syslog_facility.$inn::have_uustat = 0 if 0;
77
78 ##############################################################################
79 sub read_cf {
80     my ($conf_file, $site_wanted) = @_;
81
82     my $hour = (localtime time)[2];
83
84     my @sites;
85     open(CF, $conf_file) or logdie("cannot open $conf_file: $!", 'crit');
86     while (<CF>) {
87         chop;
88         s/\s*\#.*$//;
89         next if /^$/;
90
91         my ($sitespec, $compress, $size, $time) = split(/\s+/);
92         next if not $sitespec;
93
94         my ($site, $host, $funnel) = split(/:/, $sitespec);
95         $host = $site if not $host;
96         $funnel = $site if not $funnel;
97
98         $compress =~ s/_/ /g if $compress;
99
100         if ($site_wanted) {
101             if ($site eq $site_wanted) {
102                 push @sites, [$site, $host, $funnel, $compress, $size];
103                 last;
104             }
105             next;
106         }
107
108         if ($time) {
109             foreach my $time (split(/,/, $time)) {
110                 next if $time != $hour;
111                 push @sites, [$site, $host, $funnel, $compress, $size];
112             }
113         } else {
114             push @sites, [$site, $host, $funnel, $compress, $size];
115         }
116     }
117     close CF;
118     return @sites;
119 }
120
121 ##############################################################################
122 # count number of jobs in the UUCP queue for a given site
123 sub count_jobs {
124     my ($site) = @_;
125
126     return 0 if not $inn::have_uustat;
127     open(JOBS, "uustat -s $site 2> /dev/null |") or logdie("cannot fork: $!");
128     my $count = grep(/ Executing rnews /, <JOBS>);
129     close JOBS;                    # ignore errors, uustat may fail
130     return $count;
131 }
132
133 # select the rnews label appropriate for the compressor program used
134 sub unbatcher {
135     my ($compressor) = @_;
136
137     $compressor =~ s%.*/%%;   # Do not keep the complete path.
138     $compressor =~ s% .*%%;   # Do not keep the optional parameters.
139     return $UNBATCHER{$compressor} || 'cunbatch';
140 }
141
142 ##############################################################################
143 # batch articles for one site
144 sub run_site {
145     my ($cfg) = @_;
146     my ($site, $host, $funnel, $compress, $size) = @$cfg;
147
148     logmsg("checking site $site", 'debug');
149     my $maxjobs = '';
150     if ($MAXJOBS) {
151         my $jobs = count_jobs($site);
152         if ($jobs >= $MAXJOBS) {
153             logmsg("too many jobs queued for $site");
154             return;
155         }
156         $maxjobs = '-N ' . ($MAXJOBS - $jobs);
157     }
158
159     $compress ||= $inn::compress;
160     $size ||= $MAXSIZE;
161
162     # if exists a .work temp file left by a previous invocation, rename
163     # it to .work.tmp, we'll append it to the current batch file once it
164     # has been renamed and flushed.
165     if (-f "$site.work") {
166         rename("$site.work", "$site.work.tmp")
167             or logdie("cannot rename $site.work: $!", 'crit');
168     }
169
170     if (not -f $site and not -f "$site.work.tmp") {
171         logmsg("no batch file for site $site", 'err');
172         return;
173     }
174
175     rename($site, "$site.work") or logdie("cannot rename $site: $!", 'crit');
176     logmsg("Flushing $funnel for site $site", 'debug');
177     ctlinnd('-t120', 'flush', $funnel);
178
179     # append the old .work temp file to the current batch file if needed
180     if (-f "$site.work.tmp") {
181         my $err = '';
182         open(OUT, ">>$site.work")
183             or logdie("cannot open $site.work: $!", 'crit');
184         open(IN, "$site.work.tmp")
185             or logdie("cannot open $site.work.tmp: $!", 'crit');
186         print OUT while <IN>;
187         close IN;
188         close OUT or logdie("cannot close $site.work: $!");;
189         unlink "$site.work.tmp"
190             or logmsg("cannot delete $site.work.tmp: $!", 'err');
191     }
192
193     if (not -s "$site.work") {
194         logmsg("no articles for $site", 'debug');
195         unlink "$site.work" or logmsg("cannot delete $site.work: $!", 'err');
196     } else {
197         if ($compress eq 'none') {
198             system "batcher -b $size $maxjobs $BATCHER_FLAGS "
199                 . "-p\"$inn::uux $UUX_FLAGS %s!rnews\" $host $site.work";
200         } else {
201             system "batcher -b $size $maxjobs $BATCHER_FLAGS "
202                 . "-p\"{ echo '#! " . unbatcher($compress)
203                 . "' ; exec $compress; } | "
204                 . "$inn::uux $UUX_FLAGS %s!rnews\" $host $site.work";
205         }
206         logmsg("batched articles for $site", 'debug');
207     }
208 }
209
210 ##############################################################################
211 sub logmsg {
212     my ($msg, $lvl) = @_;
213
214     syslog($lvl || 'notice', '%s', $msg);
215 }
216
217 sub logdie {
218     my ($msg, $lvl) = @_;
219
220     logmsg($msg, $lvl || 'err');
221     unlink $lockfile;
222     exit 1;
223 }
224
225 sub ctlinnd {
226     my ($cmd, @args) = @_;
227
228     my $st = system("$inn::newsbin/ctlinnd", '-s', $cmd, @args);
229     logdie('Cannot run ctlinnd: ' . $!) if $st == -1;
230     logdie('ctlinnd returned status ' . ($st & 255)) if $st > 0;
231 }
232
233 sub shlock {
234     my $lockfile = shift;
235
236     my $locktry = 0;
237     while ($locktry < 60) {
238         if (system("$inn::newsbin/shlock", '-p', $$, '-f', $lockfile) == 0) {
239             return 1;
240         }
241         $locktry++;
242         sleep 2;
243     }
244
245     my $lockreason;
246     if (open(LOCKFILE, $lockfile)) {
247         $lockreason = 'held by ' . (<LOCKFILE> || '?');
248         close LOCKFILE;
249     } else {
250         $lockreason = $!;
251     }
252     logdie("Cannot get lock $lockfile: $lockreason");
253     return undef;
254 }
255
256 __END__
257
258 =head1 NAME
259
260 send-uucp - Send Usenet articles via UUCP
261
262 =head1 SYNOPSIS
263
264 B<send-uucp> [I<SITE> ...]
265
266 =head1 DESCRIPTION
267
268 The B<send-uucp> program processes batch files written by innd(8) to send
269 Usenet articles to UUCP sites.  It reads a configuration file to control how
270 it behaves with various sites.  Normally, it's run periodically out of cron
271 to put together batches and send them to remote UUCP sites.
272
273 =head1 OPTIONS
274
275 Any arguments provided to the program are interpreted as a list of sites
276 specfied in F<send-uucp.cf> for which batches should be generated.  If no
277 arguments are supplied then batches will be generated for all sites listed
278 in that configuration file.
279
280 =head1 CONFIGURATION
281
282 The sites to which articles are to be sent must be configured in the
283 configuration file F<send-uucp.cf>.  Each site is specified with a line of
284 the form:
285
286     site[:host[:funnel]] [compressor [maxsize [batchtime]]]
287
288 =over 4
289
290 =item I<site>
291
292 The news site name being configured.  This must match a site name 
293 from newsfeeds(5).
294
295 =item I<host>
296
297 The UUCP host name to which batches should be sent for this site.
298 If omitted, the news site name will be used as the UUCP host name.
299
300 =item I<funnel>
301
302 In the case of a site configured as a funnel, B<send-uucp> needs to flush
303 the channel (or exploder) being used as the target of the funnel instead of
304 flushing the site.  This is the way to tell B<send-uucp> the name of the
305 channel or exploder to flush for this site.  If not specified, default to
306 flushing the site.
307
308 =item I<compressor>
309
310 The compression method to use for batches.  This should be one of compress,
311 gzip or none.  Arguments for the compression command may be specified by
312 using C<_> instead of spaces. For example, C<gzip_-9>.  The default value is
313 C<compress>.
314
315 =item I<maxsize>
316
317 The maximum size of a single batch before compression.  The default value is
318 500,000 bytes.
319
320 =item I<batchtime>
321
322 A comma separated list of hours during which batches should be generated for
323 a given site.  When B<send-uucp> runs, a site will only be processed if the
324 current hour matches one of the hours in I<batchtime>.  The default is no
325 limitation on when to generate batches.
326
327 =back
328
329 Fields are seperated by spaces and only the site name needs to be specified,
330 with defaults being used for unspecified values.  If the first character on
331 a line is a C<#> then the rest of the line is ignored.
332
333 =head1 EXAMPLE
334
335 Here is an example send-uucp.cf configuration file:
336
337     zoetermeer      gzip            1048576         5,18,22
338     hoofddorp       gzip            1048576         5,18,22
339     pa3ebv          gzip            1048576         5,18,22
340     drinkel         gzip            1048576         5,6,18,20,22,0,2
341     manhole         compress        1048576         5,18,22
342     owl             compress        1048576
343     able
344     pern::MYFUNNEL!
345
346 This defines eight UUCP sites.  The first four use gzip compression and the
347 last three use compress.  The first six use a batch size of 1MB, and the
348 last site (able) uses the default of 500,000 bytes.  The zoetermeer,
349 hoofddorp, pa3ebv, and manhole sites will only have batches generated for
350 them during the hours of 05:00, 18:00, and 22:00, and the drinkel site will
351 only have batches generated during those hours and 20:00, 00:00, and 02:00.
352 There are no restrictions on when batches will be generated for owl or able.
353
354 The pern site is configured as a funnel into C<MYFUNNEL!>.  B<send-uucp> will
355 issue C<ctlinnd flush MYFUNNEL!> instead of C<ctlinnd flush pern>.
356
357 =head1 FILES
358
359 =over 4
360
361 =item I<pathetc>/send-uucp.cf
362
363 Configuration file specifying a list of sites to be processed.  
364
365 =back
366
367 =head1 NOTES
368
369 The usual flags used for a UUCP feed in the I<newsfeeds> file are C<Tf,Wfb>.
370
371 =head1 SEE ALSO
372
373 innd(8), newsfeeds(5), uucp(8)
374
375 =head1 AUTHOR
376
377 This program was originally written by Edvard Tuinder <ed@elm.net> and then
378 maintained and extended by Miquel van Smoorenburg <miquels@cistron.nl>.
379 Marco d'Itri <md@linux.it> cleaned up the code for inclusion in INN.  This
380 manual page was written by Mark Brown <broonie@sirena.org.uk>.
381
382 =cut