2 # module to timed tasks
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
19 use vars qw{@crontab @lcrontab @scrontab $mtime $lasttime $lastmin};
26 my $fn = "$main::cmd/crontab";
27 my $localfn = "$main::localcmd/crontab";
29 use vars qw($VERSION $BRANCH);
30 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
31 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
32 $main::build += $VERSION;
33 $main::branch += $BRANCH;
35 # cron initialisation / reading in cronjobs
38 if ((-e $localfn && -M $localfn < $mtime) || (-e $fn && -M $fn < $mtime) || $mtime == 0) {
41 # first read in the standard one
45 @scrontab = cread($fn);
46 $mtime = $t if !$mtime || $t <= $mtime;
49 # then read in any local ones
53 @lcrontab = cread($localfn);
54 $mtime = $t if $t <= $mtime;
56 @crontab = (@scrontab, @lcrontab);
64 my $fh = new IO::File;
68 dbg("cron: reading $fn\n") if isdbg('cron');
69 open($fh, $fn) or confess("cron: can't open $fn $!");
73 next if /^\s*#/o or /^\s*$/o;
74 my ($min, $hour, $mday, $month, $wday, $cmd) = /^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)$/o;
75 next unless defined $min;
79 $err |= parse($ref, 'min', $min, 0, 60);
80 $err |= parse($ref, 'hour', $hour, 0, 23);
81 $err |= parse($ref, 'mday', $mday, 1, 31);
82 $err |= parse($ref, 'month', $month, 1, 12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
83 $err |= parse($ref, 'wday', $wday, 0, 6, "sun", "mon", "tue", "wed", "thu", "fri", "sat");
87 dbg("cron: adding $_\n") if isdbg('cron');
89 dbg("cron: error on line $line '$_'\n") if isdbg('cron');
111 # handle comma delimited values
112 my @comma = split /,/o, $val;
114 my @minus = split /-/o;
116 return 1 if $minus[0] < $low || $minus[0] > $high;
117 return 1 if $minus[1] < $low || $minus[1] > $high;
119 for ($i = $minus[0]; $i <= $minus[1]; ++$i) {
123 return 1 if $_ < $low || $_ > $high;
127 $ref->{$sort} = \@req;
132 # process the cronjobs
135 my $now = $main::systime;
136 return if $now-$lasttime < 1;
138 my ($sec, $min, $hour, $mday, $mon, $wday) = (gmtime($now))[0,1,2,3,4,6];
140 # are we at a minute boundary?
141 if ($min != $lastmin) {
143 # read in any changes if the modification time has changed
146 $mon += 1; # months otherwise go 0-11
148 foreach $cron (@crontab) {
149 if ((!$cron->{min} || grep $_ eq $min, @{$cron->{min}}) &&
150 (!$cron->{hour} || grep $_ eq $hour, @{$cron->{hour}}) &&
151 (!$cron->{mday} || grep $_ eq $mday, @{$cron->{mday}}) &&
152 (!$cron->{mon} || grep $_ eq $mon, @{$cron->{mon}}) &&
153 (!$cron->{wday} || grep $_ eq $wday, @{$cron->{wday}}) ){
156 dbg("cron: $min $hour $mday $mon $wday -> doing '$cron->{cmd}'") if isdbg('cron');
158 dbg("cron: cmd error $@") if $@ && isdbg('cron');
164 # remember when we are now
170 # these are simple stub functions to make connecting easy in DXCron contexts
173 # is it locally connected?
177 return DXChannel->get($call);
180 # is it remotely connected anywhere (with exact callsign)?
184 return Route::get($call);
187 # is it remotely connected anywhere (ignoring SSIDS)?
191 my $c = Route::get($call);
194 $c = Route::get("$call-$_");
201 # is it remotely connected anywhere (with exact callsign) and on node?
205 my $ncall = uc shift;
206 my $node = Route::Node::get($ncall);
207 return ($node) ? grep $call eq $_, $node->users : undef;
210 # is it remotely connected (ignoring SSIDS) and on node?
214 my $ncall = uc shift;
215 my $node = Route::Node::get($ncall);
218 $present = grep {/^$call/ } $node->users;
223 # last time this thing was connected
227 return $main::systime if DXChannel->get($call);
228 my $user = DXUser->get($call);
229 return $user ? $user->lastin : 0;
232 # disconnect a locally connected thing
236 run_cmd("disconnect $call");
239 # start a connect process off
243 # connecting is now done in one place - Yeah!
244 run_cmd("connect $call");
247 # spawn any old job off
255 # in child, unset warnings, disable debugging and general clean up from us
257 eval "{ package DB; sub DB {} }";
258 DXChannel::closeall();
259 for (@main::listeners) {
262 unless ($main::is_win) {
263 $SIG{HUP} = 'IGNORE';
264 $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = $SIG{__WARN__} = 'DEFAULT';
267 exec "$line" or dbg("exec '$line' failed $!") if isdbg('cron');
269 dbg("spawn of $line started") if isdbg('cron');
271 dbg("can't fork for $line $!") if isdbg('cron');
278 # do an rcmd to another cluster from the crontab
284 # can we see it? Is it a node?
285 my $noderef = Route::Node::get($call);
286 return unless $noderef && $noderef->version;
289 DXProt::addrcmd($main::me, $call, $line);
295 my @in = DXCommandmode::run_cmd($main::me, $line);
296 dbg("cmd run: $line") if isdbg('cron');
299 dbg("cmd out: $_") if isdbg('cron');