diff --git a/src/etc/inc/system.inc b/src/etc/inc/system.inc index 3bec83722..4f0fff3f7 100644 --- a/src/etc/inc/system.inc +++ b/src/etc/inc/system.inc @@ -1645,10 +1645,10 @@ function system_reboot($sync = false) mwexec($cmd); } -function system_reboot_cleanup() { +function system_reboot_cleanup() +{ global $config, $cpzone; - mwexec("/usr/local/bin/beep.sh stop"); require_once("captiveportal.inc"); if (is_array($config['captiveportal'])) { foreach ($config['captiveportal'] as $cpzone=>$cp) { @@ -1656,8 +1656,6 @@ function system_reboot_cleanup() { captiveportal_send_server_accounting(true); } } - require_once("voucher.inc"); - voucher_save_db_to_config(); } function system_console_configure() diff --git a/src/etc/inc/voucher.inc b/src/etc/inc/voucher.inc index 0b1769331..6ab1022b6 100644 --- a/src/etc/inc/voucher.inc +++ b/src/etc/inc/voucher.inc @@ -1,7 +1,8 @@ - Copyright (C) 2010 Scott Ullrich + Copyright (C) 2010-2012 Ermal Luci + Copyright (C) 2010 Scott Ullrich Copyright (C) 2007 Marcel Wiget All rights reserved. @@ -28,15 +29,8 @@ */ -/* - pfSense_BUILDER_BINARIES: /usr/local/bin/voucher - pfSense_MODULE: captiveportal -*/ - /* include all configuration functions */ -if(!function_exists('captiveportal_syslog')) - require_once("captiveportal.inc"); - +require_once("captiveportal.inc"); function voucher_expire($voucher_received) { global $g, $config, $cpzone; @@ -276,8 +270,9 @@ function voucher_auth($voucher_received, $test = 0) { return $total_minutes; } -function voucher_configure($sync = false) { - global $config, $g, $cpzone; +function voucher_configure($sync = false) +{ + global $config, $cpzone; if (is_array($config['voucher'])) { foreach ($config['voucher'] as $voucherzone => $vcfg) { @@ -474,57 +469,70 @@ function voucher_log($priority, $message) { closelog(); } -/* Save active and used voucher DB into XML config and write it to flash - * Called during reboot -> system_reboot_cleanup() and every active voucher change +/* + * Save active and used voucher DB into XML config and write it to config + * Called during reboot and every active voucher change */ -function voucher_save_db_to_config() { - global $config, $g, $cpzone; +function voucher_save_db_to_config() +{ + global $config, $cpzone; - if (is_array($config['voucher'])) { - foreach ($config['voucher'] as $voucherzone => $vcfg) { - $cpzone = $voucherzone; - voucher_save_db_to_config_zone(); - } + if (!isset($config['voucher'])) { + return; + } + + $needs_write = 0; + + foreach ($config['voucher'] as $voucherzone => $vcfg) { + $cpzone = $voucherzone; + $needs_write += voucher_save_db_to_config_zone(); + } + + if ($needs_write) { + write_config("Backing up vouchers"); } } -function voucher_save_db_to_config_zone() { - global $config, $g, $cpzone; +function voucher_save_db_to_config_zone() +{ + global $config, $cpzone; - if (!isset($config['voucher'][$cpzone]['enable'])) - return; // no vouchers or don't want to save DB's + if (!isset($config['voucher'][$cpzone]['enable'])) { + // no vouchers or don't want to save DB's + return 0; + } - if (!is_array($config['voucher'][$cpzone]['roll'])) - return; + if (!isset($config['voucher'][$cpzone]['roll'])) { + return 0; + } - $voucherlck = lock("voucher{$cpzone}", LOCK_EX); + $voucherlck = lock("voucher{$cpzone}", LOCK_EX); - // walk all active rolls and save runtime DB's to flash - $a_roll = &$config['voucher'][$cpzone]['roll']; - while (list($key, $value) = each($a_roll)) { - $rollent = &$a_roll[$key]; - $roll = $rollent['number']; - $bitmask = voucher_read_used_db($roll); - $rollent['used'] = base64_encode($bitmask); - $active_vouchers = voucher_read_active_db($roll); - $db = array(); + // walk all active rolls and save runtime DBs + $a_roll = &$config['voucher'][$cpzone]['roll']; + while (list($key, $value) = each($a_roll)) { + $rollent = &$a_roll[$key]; + $roll = $rollent['number']; + $bitmask = voucher_read_used_db($roll); + $rollent['used'] = base64_encode($bitmask); + $active_vouchers = voucher_read_active_db($roll); + $db = array(); $dbi = 1; - foreach($active_vouchers as $voucher => $line) { - list($timestamp,$minutes) = explode(",", $line); - $activent['voucher'] = $voucher; - $activent['timestamp'] = $timestamp; - $activent['minutes'] = $minutes; - $db["v{$dbi}"] = $activent; - $dbi++; - } - $rollent['active'] = $db; - unset($active_vouchers); - } - unlock($voucherlck); + foreach($active_vouchers as $voucher => $line) { + list($timestamp, $minutes) = explode(',', $line); + $activent['voucher'] = $voucher; + $activent['timestamp'] = $timestamp; + $activent['minutes'] = $minutes; + $db["v{$dbi}"] = $activent; + $dbi++; + } - write_config("Synching vouchers"); - return; + $rollent['active'] = $db; + unset($active_vouchers); + } + + unlock($voucherlck); + + return 1; } - -?> diff --git a/src/etc/rc.halt b/src/etc/rc.halt index 45cf37e58..319b4774c 100755 --- a/src/etc/rc.halt +++ b/src/etc/rc.halt @@ -1,19 +1,7 @@ #!/bin/sh -# shutdown rc scripts -/usr/local/etc/rc.opnsense stop - -/usr/local/etc/rc.backup_rrd -/usr/local/etc/rc.backup_dhcpleases - -# wait for config lock to release -php -a >/dev/null << EOF -\$fp = fopen('/conf/config.xml', "w+"); -if (flock(\$fp, LOCK_EX)) { - flock(\$fp, LOCK_UN); - fclose(\$fp); -} - -EOF +# invoke shared powerdown routines +. /usr/local/etc/rc.halt.common +# power down the system /sbin/shutdown -op now diff --git a/src/etc/rc.halt.common b/src/etc/rc.halt.common new file mode 100755 index 000000000..4036e56ee --- /dev/null +++ b/src/etc/rc.halt.common @@ -0,0 +1,21 @@ +#!/bin/sh + +# shutdown rc scripts +/usr/local/etc/rc.opnsense stop + +# backup volatile internals +/usr/local/etc/rc.backup_rrd +/usr/local/etc/rc.backup_dhcpleases +/usr/local/etc/rc.savevouchers + +# wait for config lock to release +php -a >/dev/null << EOF +\$fp = fopen('/conf/config.xml', "w+"); +if (flock(\$fp, LOCK_EX)) { + flock(\$fp, LOCK_UN); + fclose(\$fp); +} +EOF + +# run beep sequence if enabled +/usr/local/bin/beep.sh stop diff --git a/src/etc/rc.reboot b/src/etc/rc.reboot index 57ae3ce16..3268b5536 100755 --- a/src/etc/rc.reboot +++ b/src/etc/rc.reboot @@ -1,19 +1,7 @@ #!/bin/sh -# shutdown rc scripts -/usr/local/etc/rc.opnsense stop - -/usr/local/etc/rc.backup_rrd -/usr/local/etc/rc.backup_dhcpleases - -# wait for config lock to release -php -a >/dev/null << EOF -\$fp = fopen('/conf/config.xml', "w+"); -if (flock(\$fp, LOCK_EX)) { - flock(\$fp, LOCK_UN); - fclose(\$fp); -} - -EOF +# invoke shared powerdown routines +. /usr/local/etc/rc.halt.common +# reboot the system /sbin/shutdown -or now