1 =head1 webmincron-lib.pl
3 Functions for creating and listing Webmin scheduled functions.
7 BEGIN { push(@INC, ".."); };
11 $webmin_crons_directory = "$module_config_directory/crons";
12 @special_modes = ( 'hourly', 'daily', 'weekly', 'monthly', 'yearly' );
14 =head2 list_webmin_crons
16 Returns a list of all scheduled Webmin functions. Each of which is a hash ref
19 =item id - A unique ID number
21 =item module - The module in which the function is defined
23 =item file - File in which the function is declared
25 =item func - Name of the function to call
27 =item args - Array ref of strings to pass to the function as parameters
29 =item interval - Number of seconds between runs (optional)
31 =item mins - Minutes on which to run. Can be * or a comma-separated list
33 =item hours - Hours on which to run
35 =item days - Days of the month on which to run
37 =item months - Months of the year on which to run
39 =item weekdays - Days of week on which to run
41 =item special - Can be 'hourly', 'daily', 'weekly' or 'monthly'
47 opendir(CRONS, $webmin_crons_directory) || return ( );
48 foreach my $f (readdir(CRONS)) {
49 if ($f =~ /^(\d+)\.cron$/) {
51 &read_file_cached("$webmin_crons_directory/$f", \%cron);
54 for(my $i=0; defined($cron{'arg'.$i}); $i++) {
55 push(@args, $cron{'arg'.$i});
56 delete($cron{'arg'.$i});
59 $cron{'args'} = \@args;
67 =head2 save_webmin_crons(&cron)
69 Create or update a webmin cron function. Also locks the file being written to.
75 if (!-d $webmin_crons_directory) {
76 &make_dir($webmin_crons_directory, 0700);
79 $cron->{'id'} = time().$$;
81 my $file = "$webmin_crons_directory/$cron->{'id'}.cron";
84 for(my $i=0; $i<@{$wcron{'args'}}; $i++) {
85 $wcron{'arg'.$i} = $wcron{'args'}->[$i];
87 delete($wcron{'args'});
90 &write_file($file, \%wcron);
93 # Reload may fail in Webmin isn't running
94 $main::error_must_die = 1;
99 =head2 delete_webmin_cron(&cron)
101 Deletes the file for a webmin cron function. Also does locking.
104 sub delete_webmin_cron
107 my $file = "$webmin_crons_directory/$cron->{'id'}.cron";
114 =head2 find_webmin_cron(module, function, [&args])
116 Returns a Webmin cron hash ref matching the given module and function
121 my ($module, $func, $args) = @_;
122 my @crons = &list_webmin_crons();
123 foreach my $oc (@crons) {
124 next if ($oc->{'module'} ne $module);
125 next if ($oc->{'func'} ne $func);
128 for(my $i=0; $i < scalar(@{$oc->{'args'}}) ||
129 $i < scalar(@$args); $i++) {
130 $sameargs = 0 if ($oc->{'args'}->[$i] ne $args->[$i]);
132 next if (!$sameargs);
139 =head2 create_webmin_cron(&cron, [old-cron-command])
141 Create or update a webmin cron job that calls some function.
142 If the old-cron parameter is given, find and replace the regular cron job
146 sub create_webmin_cron
148 my ($cron, $old_cmd) = @_;
150 # Find and replace existing cron with same module, function and args
151 my $already = &find_webmin_cron($cron->{'module'}, $cron->{'func'},
154 # Update existing, possibly with new interval
155 $cron->{'id'} = $already->{'id'};
157 &save_webmin_cron($cron);
159 # Find and delete any Unix cron job that this is replacing
160 if ($old_cmd && &foreign_installed("cron")) {
161 &foreign_require("cron");
162 my @jobs = &cron::list_cron_jobs();
164 $_->{'user'} eq 'root' &&
165 $_->{'command'} =~ /(^|[ \|\&;\/])\Q$old_cmd\E($|[ \|\&><;])/
167 foreach my $job (reverse(@jobs)) {
168 &lock_file(&cron::cron_file($job));
169 &cron::delete_cron_job($job);
170 &unlock_file(&cron::cron_file($job));
175 =head2 delete_webmin_module_crons(module)
177 Remove all Webmin cron jobs for some module
180 sub delete_webmin_module_crons
183 foreach my $cron (&list_webmin_crons()) {
184 if ($cron->{'module'} eq $mod) {
185 &delete_webmin_cron($cron);
190 =head2 show_times_input(&job, [special])
192 Returns HTML for inputs for selecting the schedule for a cron job, defined
193 by the first parameter which must be a hash ref returned by list_cron_jobs.
195 =item job - Hash ref for a webmincron object
197 =item special - 0=don't allow special times (like @hourly), 1=allow
202 my ($job, $special) = @_;
203 $special = 0 if (!defined($special));
204 my $rv = "<table width=100%>\n";
205 if ($special || $job->{'special'}) {
206 # Allow selection of special @ times
207 $rv .= "<tr $cb> <td colspan=6>\n";
208 $rv .= &ui_radio("special_def", $job->{'special'} ? 1 : 0,
209 [ [ 1, $text{'edit_special1'}." ".
210 &ui_select("special", $job->{'special'},
211 [ map { [ $_, $text{'edit_special_'.$_} ] }
213 [ 0, $text{'edit_special0'} ] ]);
214 $rv .= "</td></tr>\n";
217 # Javascript to disable and enable fields
220 function enable_cron_fields(name, form, ena)
222 var els = form.elements[name];
224 for(i=0; i<els.length; i++) {
225 els[i].disabled = !ena;
232 $rv .= "<td><b>$text{'edit_mins'}</b></td> ".
233 "<td><b>$text{'edit_hours'}</b></td> ".
234 "<td><b>$text{'edit_days'}</b></td> ".
235 "<td><b>$text{'edit_months'}</b></td> ".
236 "<td><b>$text{'edit_weekdays'}</b></td>";
237 $rv .= "</tr> <tr $cb>\n";
242 my @months = map { $text{"month_$_"}."=".$_ } (1 .. 12);
243 my @weekdays = map { $text{"day_$_"}."=".$_ } (0 .. 6);
244 my $arrmap = { 'mins' => \@mins,
247 'months' => \@months,
248 'weekdays' => \@weekdays };
250 foreach my $arr ("mins", "hours", "days", "months", "weekdays") {
251 # Find out which ones are being used
253 my $min = ($arr =~ /days|months/ ? 1 : 0);
254 my $max = $min+scalar(@{$arrmap->{$arr}})-1;
255 foreach my $w (split(/,/ , $job->{$arr})) {
258 for(my $j=$min; $j<=$max; $j++) { $inuse{$j}++; }
260 elsif ($w =~ /^\*\/(\d+)$/) {
262 for(my $j=$min; $j<=$max; $j+=$1) { $inuse{$j}++; }
264 elsif ($w =~ /^(\d+)-(\d+)\/(\d+)$/) {
265 # only every Nth of some range
266 for(my $j=$1; $j<=$2; $j+=$3) { $inuse{int($j)}++; }
268 elsif ($w =~ /^(\d+)-(\d+)$/) {
270 for(my $j=$1; $j<=$2; $j++) { $inuse{int($j)}++; }
277 if ($job->{$arr} eq "*") { undef(%inuse); }
279 # Output selection list
280 $rv .= "<td valign=top>\n";
282 "<input type=radio name=all_$arr value=1 %s %s %s> %s<br>\n",
283 $arr eq "mins" && $hourly_only ? "disabled" : "",
284 $job->{$arr} eq "*" || $job->{$arr} eq "" ? "checked" : "",
285 "onClick='enable_cron_fields(\"$arr\", form, 0)'",
288 "<input type=radio name=all_$arr value=0 %s %s> %s<br>\n",
289 $job->{$arr} eq "*" || $job->{$arr} eq "" ? "" : "checked",
290 "onClick='enable_cron_fields(\"$arr\", form, 1)'",
291 $text{'edit_selected'};
292 $rv .= "<table> <tr>\n";
293 my @arrlist = @{$arrmap->{$arr}};
294 for(my $j=0; $j<@arrlist; $j+=12) {
296 if ($jj >= @arrlist) { $jj = @arrlist - 1; }
297 my @sec = @arrlist[$j .. $jj];
299 "<td valign=top><select %s size=%d name=$arr %s>\n",
300 $arr eq "mins" && $hourly_only ? "" : "multiple",
301 @sec > 12 ? 12 : scalar(@sec),
302 $job->{$arr} eq "*" || $job->{$arr} eq "" ?
304 foreach my $v (@sec) {
305 if ($v =~ /^(.*)=(.*)$/) { $disp = $1; $code = $2; }
306 else { $disp = $code = $v; }
307 $rv .= sprintf "<option value=\"$code\" %s>$disp\n",
308 $inuse{$code} ? "selected" : "";
310 $rv .= "</select></td>\n";
312 $rv .= "</tr></table></td>\n";
314 $rv .= "</tr></table>\n";
318 =head2 parse_times_input(&job, &in)
320 Parses inputs from the form generated by show_times_input, and updates a cron
321 job hash ref. The in parameter must be a hash ref as generated by the
325 sub parse_times_input
329 my @pers = ("mins", "hours", "days", "months", "weekdays");
330 if ($in{'special_def'}) {
331 # Job time is a special period
332 foreach my $arr (@pers) {
333 delete($job->{$arr});
335 $job->{'special'} = $in{'special'};
338 # User selection of times
339 foreach my $arr (@pers) {
340 if ($in{"all_$arr"}) {
341 # All mins/hrs/etc.. chosen
344 elsif (defined($in{$arr})) {
345 # Need to work out and simplify ranges selected
346 my @range = split(/\0/, $in{$arr});
347 my @range = sort { $a <=> $b } @range;
350 for(my $i=0; $i<@range; $i++) {
351 if ($i && $range[$i]-1 == $range[$i-1]) {
352 # ok.. looks like a range
353 if ($start < 0) { $start = $i-1; }
356 # Not in a range at all
357 push(@newrange, $range[$i]);
360 # End of the range.. add it
361 $newrange[@newrange - 1] =
362 "$range[$start]-".$range[$i-1];
363 push(@newrange, $range[$i]);
368 # Reached the end while in a range
369 $newrange[@newrange - 1] =
370 "$range[$start]-".$range[$i-1];
372 $job->{$arr} = join(',' , @newrange);
375 &error(&text('save_enone', $text{"edit_$arr"}));
378 delete($job->{'special'});