Project

General

Profile

Download (16.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	system.inc
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
/* include all configuration functions */
33
require_once("functions.inc");
34

    
35
function system_resolvconf_generate($dynupdate = false) {
36
	global $config, $g;
37

    
38
	$syscfg = $config['system'];
39

    
40
	$fd = fopen("{$g['varetc_path']}/resolv.conf.new", "w");
41
	if (!$fd) {
42
		printf("Error: cannot open resolv.conf.new in system_resolvconf_generate().\n");
43
		return 1;
44
	}
45

    
46
	$dhfd = @fopen("{$g['etc_path']}/dhclient-enter-hooks", "w");
47
	if (!$dhfd) {
48
		printf("Error: cannot open dhclient-enter-hooks in system_resolvconf_generate().\n");
49
		return 1;
50
	}
51

    
52

    
53
	$resolvconf = "domain {$syscfg['domain']}\n";
54

    
55
	$havedns = false;
56

    
57
	if (isset($syscfg['dnsallowoverride'])) {
58
		/* get dynamically assigned DNS servers for ppp (if any) */
59
		$nfd = @fopen("{$g['varetc_path']}/nameservers.conf", "r");
60
		if ($nfd) {
61
			while (!feof($nfd)) {
62
				$dnss = trim(fgets($nfd));
63
				if  ( preg_match('/^(nameserver )(.*)/',$dnss, $catch) ) 
64
					$dnss = $catch[2];
65
				if (is_ipaddr($dnss)) {
66
					$resolvconf .= "nameserver $dnss\n";
67
					$havedns = true;
68
				}
69
			}
70
			fclose($nfd);
71
		}
72
	}
73

    
74
	/* if we didn't get assigned DNS servers and have some set add 'em */
75
	if (!$havedns && is_array($syscfg['dnsserver'])) {
76
		foreach ($syscfg['dnsserver'] as $ns) {
77
			if ($ns)
78
				$resolvconf .= "nameserver $ns\n";
79
			$havedns = true;
80
		}
81

    
82
		/* create a dhclient-enter-hooks, so resolv.conf stays intact */
83
		$dhcliententerhooks = "add_new_resolv_conf() {\n";
84
		$dhcliententerhooks .= "\techo search {$syscfg['domain']} > \$RESOLV\n";
85
		foreach ($syscfg['dnsserver'] as $ns) {
86
			if ($ns)
87
				$dhcliententerhooks .= "\techo nameserver $ns > \$RESOLV\n";
88
	}
89
		$dhcliententerhooks .= "}\n";
90
	}
91

    
92
	fwrite($dhfd, $dhcliententerhooks);
93
	fclose($dhfd);
94

    
95
	fwrite($fd, $resolvconf);
96
	fclose($fd);
97

    
98
	/* If we now have DNS servers, overwrite resolv.conf */
99
	if ($havedns) {
100
		if (file_exists("{$g['varetc_path']}/resolv.conf"))
101
			unlink("{$g['varetc_path']}/resolv.conf");
102
		rename("{$g['varetc_path']}/resolv.conf.new", "{$g['varetc_path']}/resolv.conf");
103
	} else {
104
		unlink("{$g['varetc_path']}/resolv.conf.new");
105
	}
106

    
107

    
108
	if (!$g['booting']) {
109
		/* restart dhcpd (nameservers may have changed) */
110
		if (!$dynupdate)
111
			services_dhcpd_configure();
112
	}
113

    
114
	return 0;
115
}
116

    
117
function system_hosts_generate() {
118
	global $config, $g;
119

    
120
	$syscfg = $config['system'];
121
	$lancfg = $config['interfaces']['lan'];
122
	$dnsmasqcfg = $config['dnsmasq'];
123

    
124
	if (!is_array($dnsmasqcfg['hosts'])) {
125
		$dnsmasqcfg['hosts'] = array();
126
	}
127
	$hostscfg = $dnsmasqcfg['hosts'];
128

    
129
	$fd = fopen("{$g['varetc_path']}/hosts", "w");
130
	if (!$fd) {
131
		printf("Error: cannot open hosts file in system_hosts_generate().\n");
132
		return 1;
133
	}
134

    
135
	$hosts = <<<EOD
136
127.0.0.1	localhost localhost.{$syscfg['domain']}
137
{$lancfg['ipaddr']}	{$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}
138

    
139
EOD;
140

    
141
	foreach ($hostscfg as $host) {
142
		if ($host['host'])
143
			$hosts .= "{$host['ip']}	{$host['host']}.{$host['domain']} {$host['host']}\n";
144
		else
145
			$hosts .= "{$host['ip']}	{$host['domain']}\n";
146
	}
147
	fwrite($fd, $hosts);
148
	fclose($fd);
149

    
150
	return 0;
151
}
152

    
153
function system_hostname_configure() {
154
	global $config, $g;
155

    
156
	$syscfg = $config['system'];
157

    
158
	/* set hostname */
159
	return mwexec("/bin/hostname " .
160
		escapeshellarg("{$syscfg['hostname']}.{$syscfg['domain']}"));
161
}
162

    
163
function system_routing_configure() {
164
	global $config, $g;
165

    
166
	/* Enable fast routing, if enabled */
167
	if(isset($config['staticroutes']['enablefastrouting']))
168
		mwexec("/sbin/sysctl net.inet.ip.fastforwarding=1");
169

    
170
	/* clear out old routes, if necessary */
171
	if (file_exists("{$g['vardb_path']}/routes.db")) {
172
		$fd = fopen("{$g['vardb_path']}/routes.db", "r");
173
		if (!$fd) {
174
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
175
			return 1;
176
		}
177
		while (!feof($fd)) {
178
			$oldrt = fgets($fd);
179
			if ($oldrt)
180
				mwexec("/sbin/route delete " . escapeshellarg($oldrt));
181
		}
182
		fclose($fd);
183
		unlink("{$g['vardb_path']}/routes.db");
184
	}
185

    
186
	if (is_array($config['staticroutes']['route'])) {
187

    
188
		$fd = fopen("{$g['vardb_path']}/routes.db", "w");
189
		if (!$fd) {
190
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
191
			return 1;
192
		}
193

    
194
		foreach ($config['staticroutes']['route'] as $rtent) {
195
			mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
196
				" " . escapeshellarg($rtent['gateway']));
197

    
198
			/* record route so it can be easily removed later (if necessary) */
199
			fwrite($fd, $rtent['network'] . "\n");
200
		}
201

    
202
		fclose($fd);
203
	}
204

    
205
	return 0;
206
}
207

    
208
function system_routing_enable() {
209
	global $config, $g;
210

    
211
	return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
212
}
213

    
214
function system_syslogd_start() {
215
	global $config, $g;
216

    
217
	$syslogcfg = $config['syslog'];
218

    
219
	if ($g['booting'])
220
		echo "Starting syslog... ";
221
	else
222
		killbypid("{$g['varrun_path']}/syslog.pid");
223

    
224
	if (isset($syslogcfg)) {
225
		if($config['installedpackages']['package']) {
226
                        foreach($config['installedpackages']['package'] as $package) {
227
                                if($package['logging']) {	
228
					$pkgfacilities[] = $package['logging']['facilityname'];
229
                                	$syslogconf .= '!' . $package['logging']['facilityname'] . "\n*.*\t\t\t\t\t\t%{$g['varlog_path']}/" . $package['logging']['logfilename'] . "\n";
230
				}
231
                        }
232
			if(is_array($pkgfacilities)) $syslogconf .= '!-' . implode(',', $pkgfacilities) . "\n";
233
                }
234

    
235
		/* write syslog.conf */
236
		$fd = fopen("{$g['varetc_path']}/syslog.conf", "w");
237
		if (!$fd) {
238
			printf("Error: cannot open syslog.conf in system_syslogd_start().\n");
239
			return 1;
240
		}
241
		if (!isset($syslogcfg['disablelocallogging'])) {
242
		$syslogconf .= <<<EOD
243
local0.*					%{$g['varlog_path']}/filter.log
244
local3.*					%{$g['varlog_path']}/vpn.log
245
local4.*					%{$g['varlog_path']}/portalauth.log
246
local7.*					%{$g['varlog_path']}/dhcpd.log
247
*.notice;kern.debug;lpr.info;mail.crit;		%{$g['varlog_path']}/system.log
248
news.err;local0.none;local3.none;local4.none;	%{$g['varlog_path']}/system.log
249
local7.none					%{$g['varlog_path']}/system.log
250
security.*					%{$g['varlog_path']}/system.log
251
auth.info;authpriv.info;daemon.info		%{$g['varlog_path']}/system.log
252
local1.*					%{$g['varlog_path']}/slbd.log
253
*.emerg						*
254

    
255
EOD;
256
		}
257

    
258
		if (isset($syslogcfg['filter'])) {
259
			$syslogconf .= <<<EOD
260
local0.*					@{$syslogcfg['remoteserver']}
261

    
262
EOD;
263
		}
264

    
265
		if (isset($syslogcfg['vpn'])) {
266
			$syslogconf .= <<<EOD
267
local3.*					@{$syslogcfg['remoteserver']}
268

    
269
EOD;
270
		}
271

    
272

    
273
		if (isset($syslogcfg['portalauth'])) {
274
			$syslogconf .= <<<EOD
275
local4.*					@{$syslogcfg['remoteserver']}
276

    
277
EOD;
278
		}
279

    
280

    
281
		if (isset($syslogcfg['dhcp'])) {
282
			$syslogconf .= <<<EOD
283
local7.*					@{$syslogcfg['remoteserver']}
284

    
285
EOD;
286
		}
287

    
288
		if (isset($syslogcfg['system'])) {
289
			$syslogconf .= <<<EOD
290
*.notice;kern.debug;lpr.info;mail.crit;		@{$syslogcfg['remoteserver']}
291
news.err;local0.none;local3.none;local7.none	@{$syslogcfg['remoteserver']}
292
security.*					@{$syslogcfg['remoteserver']}
293
auth.info;authpriv.info;daemon.info		@{$syslogcfg['remoteserver']}
294
*.emerg						@{$syslogcfg['remoteserver']}
295
EOD;
296
		}
297
		fwrite($fd, $syslogconf);
298
		fclose($fd);
299

    
300
		$retval = mwexec("/usr/sbin/syslogd -s -f {$g['varetc_path']}/syslog.conf");
301

    
302
	} else {
303
		$retval = mwexec("/usr/sbin/syslogd -ss");
304
	}
305

    
306
	if ($g['booting'])
307
		echo "done.\n";
308

    
309
	return $retval;
310
}
311

    
312
function system_pccard_start() {
313
	global $config, $g;
314

    
315
	if ($g['booting'])
316
		echo "Initializing PCMCIA... ";
317

    
318
	/* kill any running pccardd */
319
	killbypid("{$g['varrun_path']}/pccardd.pid");
320

    
321
	/* fire up pccardd */
322
	$res = mwexec("/usr/sbin/pccardd -z -f {$g['etc_path']}/pccard.conf");
323

    
324
	if ($g['booting']) {
325
		if ($res == 0)
326
			echo "done.\n";
327
		else
328
			echo "failed!\n";
329
	}
330

    
331
	return $res;
332
}
333

    
334
function system_webgui_start() {
335
	global $config, $g;
336

    
337
	if ($g['booting'])
338
		echo "Starting webGUI... ";
339

    
340
	/* kill any running mini_httpd */
341
	killbypid("{$g['varrun_path']}/mini_httpd.pid");
342

    
343
	/* generate password file */
344
	system_password_configure();
345

    
346
	chdir($g['www_path']);
347

    
348
	/* non-standard port? */
349
	if ($config['system']['webgui']['port'])
350
		$portarg = "-p {$config['system']['webgui']['port']}";
351
	else
352
		$portarg = "";
353

    
354
	if ($config['system']['webgui']['protocol'] == "https") {
355

    
356
		if ($config['system']['webgui']['certificate'] && $config['system']['webgui']['private-key']) {
357
			$cert = base64_decode($config['system']['webgui']['certificate']);
358
			$key = base64_decode($config['system']['webgui']['private-key']);
359
		} else {
360
			/* default certificate/key */
361
			$cert = <<<EOD
362
-----BEGIN CERTIFICATE-----
363
MIIC4zCCAkygAwIBAgIBADANBgkqhkiG9w0BAQQFADBbMQswCQYDVQQGEwJOQTEL
364
MAkGA1UECBMCTkExCzAJBgNVBAcTAk5BMQswCQYDVQQKEwJOQTELMAkGA1UECxMC
365
TkExCzAJBgNVBAMTAk5BMQswCQYDVQQGEwJVUzAeFw0wNTAzMDYwMDE1NDJaFw0x
366
NTAzMDQwMDE1NDJaMFsxCzAJBgNVBAYTAk5BMQswCQYDVQQIEwJOQTELMAkGA1UE
367
BxMCTkExCzAJBgNVBAoTAk5BMQswCQYDVQQLEwJOQTELMAkGA1UEAxMCTkExCzAJ
368
BgNVBAYTAlVTMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDF7luuy70OvHrl
369
xnW9ID6srsfxEFCF4d9LmlZ6XdW1rEUHQ6KTgz4iSD+pxEOxxlY+bCH6HTkAy5Sa
370
zt3eT7javvF+ILZgarwoY2x+NbDctd0VBJVkH0fEvBf1xqU7wpkOiWkw1RmfEvZI
371
6XnGi6VSjSmkm0UoQMKg9R7niRtE4QIDAQABo4G2MIGzMB0GA1UdDgQWBBTgvk9F
372
alPK6/OcZrkaE8BhBrRo2DCBgwYDVR0jBHwweoAU4L5PRWpTyuvznGa5GhPAYQa0
373
aNihX6RdMFsxCzAJBgNVBAYTAk5BMQswCQYDVQQIEwJOQTELMAkGA1UEBxMCTkEx
374
CzAJBgNVBAoTAk5BMQswCQYDVQQLEwJOQTELMAkGA1UEAxMCTkExCzAJBgNVBAYT
375
AlVTggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAv9+GXdYIWs2R
376
8B0zI4jAbHcaRsfohuzpNHD5re7ZK8H4fYbHIfmPY2UM3yOU7J2rLP8KGfKztay1
377
Z3RNW7SKJI/CagbdQOuYdMrlEyA4ZImM6NNzUbH6rNKtmDIDo1kHL3cXjzXEjBE+
378
ZZYTREFcdhtzUH5lYzJz1uVFeCSwozk=
379
-----END CERTIFICATE-----
380
EOD;
381

    
382
			$key = <<<EOD
383
-----BEGIN RSA PRIVATE KEY-----
384
MIICXAIBAAKBgQDF7luuy70OvHrlxnW9ID6srsfxEFCF4d9LmlZ6XdW1rEUHQ6KT
385
gz4iSD+pxEOxxlY+bCH6HTkAy5Sazt3eT7javvF+ILZgarwoY2x+NbDctd0VBJVk
386
H0fEvBf1xqU7wpkOiWkw1RmfEvZI6XnGi6VSjSmkm0UoQMKg9R7niRtE4QIDAQAB
387
AoGAF9dMJ9PWo+3EB+VNzUgTBI3Q+5JxgI7ibKLcg8TFtypW7jcRYB9Q3qRBNtuz
388
I7i2LrKrrQrUEOp0rej5BIwpwcjtEE2NsZwgYwDyywptoqt3WO86nPXYz2KhkQmP
389
YCDmPrff4vXCv6zgefb/AIgrOkgD3ViEoePhCAg+0l3fEIECQQD7C68Nb6KAWUND
390
Q9B0RxYrlgXikQ8yVHhlyM433APe/NCJ9kl5dLXpyjuvrWB+ml6TlLrcroLGejbd
391
tYXvIiyJAkEAydZVHqB4MpMtuY7VJoHNgl06YBoeTI+BJptPaOUNl4SlUKIYJMhX
392
oOXIGk9uDjfSNS7HvunZBjgz092GShWvmQJAQ8NhmwTZHj/58fwqFljh2R4DtKZn
393
LbSzUvYjA9z1holDWRoLtycTu2mFNuRbuZC9mqR40/ye/CgdCzdmUagt0QJBAKq1
394
00ySINd10Cive+yTwMPQIj2CGbpbbbq/hYyMntBWapQmZRFHOYZmkrZeFBGGeQ5u
395
QJdipiIyivNY2+nxKZECQCvumJPfZYxCeCAEC+G2xezrP6bC6FhzUOw6410UARTM
396
fuFjHpSfOiG62lfRdZgCPAr1L/1pJF+8RqjGlFfAuFA=
397
-----END RSA PRIVATE KEY-----
398
EOD;
399
		}
400

    
401
		$fd = fopen("{$g['varetc_path']}/cert.pem", "w");
402
		if (!$fd) {
403
			printf("Error: cannot open cert.pem in system_webgui_start().\n");
404
			return 1;
405
		}
406
		chmod("{$g['varetc_path']}/cert.pem", 0600);
407
		fwrite($fd, $cert);
408
		fwrite($fd, "\n");
409
		fwrite($fd, $key);
410
		fclose($fd);
411

    
412
		$res = mwexec("/usr/local/sbin/mini_httpd -S -E {$g['varetc_path']}/cert.pem" .
413
			" -c \"**.php|**.cgi\" -u root -maxproc 16 $portarg" .
414
			" -i {$g['varrun_path']}/mini_httpd.pid");
415
	} else {
416
		$res = mwexec("/usr/local/sbin/mini_httpd -c \"**.php|**.cgi\" -u root" .
417
			" -maxproc 16 $portarg -i {$g['varrun_path']}/mini_httpd.pid");
418
	}
419

    
420
	if ($g['booting']) {
421
		if ($res == 0)
422
			echo "done.\n";
423
		else
424
			echo "failed!\n";
425
	}
426

    
427
	return $res;
428
}
429

    
430
function system_password_configure() {
431
	global $config, $g;
432

    
433
	$fd = fopen("{$g['varrun_path']}/htpasswd", "w");
434
	if (!$fd) {
435
		printf("Error: cannot open htpasswd in system_password_configure().\n");
436
		return 1;
437
	}
438

    
439
	if ($config['system']['username'])
440
		$username = $config['system']['username'];
441
	else
442
		$username = "admin";
443

    
444
	fwrite($fd, $username . ":" . $config['system']['password'] . "\n");
445
	fclose($fd);
446
	chmod("{$g['varrun_path']}/htpasswd", 0600);
447

    
448
	return 0;
449
}
450

    
451
function system_timezone_configure() {
452
	global $config, $g;
453

    
454
	$syscfg = $config['system'];
455

    
456
	if ($g['booting'])
457
		echo "Setting timezone... ";
458

    
459
	/* extract appropriate timezone file */
460
	$timezone = $syscfg['timezone'];
461
	if (!$timezone)
462
		$timezone = "Etc/UTC";
463

    
464
	conf_mount_rw();
465

    
466
	exec("/usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
467
		escapeshellarg($timezone) . " > /etc/localtime");
468

    
469
	conf_mount_ro();
470

    
471
	if ($g['booting'])
472
		echo "done.\n";
473
}
474

    
475
function system_ntp_configure() {
476
	global $config, $g;
477

    
478
	$syscfg = $config['system'];
479

    
480
	if ($g['booting'])
481
		echo "Starting NTP client... ";
482
	else {
483
		killbypid("{$g['varrun_path']}/runmsntp.pid");
484
		killbypid("{$g['varrun_path']}/msntp.pid");
485
	}
486

    
487
	/* start ntp client if needed - needs to be forced into background */
488
	$updateinterval = $syscfg['time-update-interval'];
489

    
490
	if ($updateinterval > 0) {
491
		if ($updateinterval < 6)
492
			$updateinterval = 6;
493

    
494
		$timeservers = "";
495
		foreach (explode(' ', $syscfg['timeservers']) as $ts)
496
			$timeservers .= " " . $ts;
497

    
498
		mwexec_bg("/usr/local/bin/runmsntp.sh " .
499
			escapeshellarg("{$g['varrun_path']}/runmsntp.pid") . " " .
500
			escapeshellarg("{$g['varrun_path']}/msntp.pid") . " " .
501
			escapeshellarg($updateinterval) . " " .
502
			escapeshellarg($timeservers));
503
	}
504

    
505
	if ($g['booting'])
506
		echo "done.\n";
507
}
508

    
509
function system_halt() {
510
	global $g;
511

    
512
	system_reboot_cleanup();
513

    
514
	mwexec("nohup /etc/rc.halt > /dev/null 2>&1 &");
515
}
516

    
517
function system_reboot() {
518
	global $g;
519

    
520
	system_reboot_cleanup();
521

    
522
	mwexec("nohup /etc/rc.reboot > /dev/null 2>&1 &");
523
}
524

    
525
function system_reboot_sync() {
526
	global $g;
527

    
528
	system_reboot_cleanup();
529

    
530
	mwexec("/etc/rc.reboot > /dev/null 2>&1");
531
}
532

    
533
function system_reboot_cleanup() {
534
	captiveportal_radius_stop_all();
535
}
536

    
537
function system_do_shell_commands($early = 0) {
538
	global $config, $g;
539

    
540
	if ($early)
541
		$cmdn = "earlyshellcmd";
542
	else
543
		$cmdn = "shellcmd";
544

    
545
	if (is_array($config['system'][$cmdn])) {
546

    
547
		foreach ($config['system'][$cmdn] as $cmd) {
548
			exec($cmd);
549
		}
550
	}
551
}
552

    
553
function system_do_extensions($early = false) {
554
	global $config, $g;
555

    
556
	if (!is_dir("{$g['etc_path']}/inc/ext"))
557
		return;
558

    
559
	$dh = @opendir("{$g['etc_path']}/inc/ext");
560
	if ($dh) {
561
		while (($extd = readdir($dh)) !== false) {
562
			if (($extd === ".") || ($extd === ".."))
563
				continue;
564
			$rcfile = "{$g['etc_path']}/inc/ext/" . $extd . "/" . ($early ? "rc.early" : "rc");
565
			if (file_exists($rcfile))
566
				passthru($rcfile);
567
		}
568
		closedir($dh);
569
	}
570
}
571

    
572
function system_console_configure() {
573
	global $config, $g;
574

    
575
	if (isset($config['system']['disableconsolemenu'])) {
576
		touch("{$g['varetc_path']}/disableconsole");
577
	} else {
578
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
579
	}
580
}
581

    
582
function system_dmesg_save() {
583
	global $g;
584

    
585
	exec("/sbin/dmesg", $dmesg);
586

    
587
	/* find last copyright line (output from previous boots may be present) */
588
	$lastcpline = 0;
589

    
590
	for ($i = 0; $i < count($dmesg); $i++) {
591
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
592
			$lastcpline = $i;
593
	}
594

    
595
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
596
	if (!$fd) {
597
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
598
		return 1;
599
	}
600

    
601
	for ($i = $lastcpline; $i < count($dmesg); $i++)
602
		fwrite($fd, $dmesg[$i] . "\n");
603

    
604
	fclose($fd);
605

    
606
	return 0;
607
}
608

    
609
function system_set_harddisk_standby() {
610
	global $g, $config;
611

    
612
	if ($g['platform'] != "generic-pc")
613
		return;
614

    
615
	if (isset($config['system']['harddiskstandby'])) {
616
		if ($g['booting']) {
617
			echo 'Setting hard disk standby... ';
618
		}
619

    
620
		$standby = $config['system']['harddiskstandby'];
621
		// Check for a numeric value
622
		if (is_numeric($standby)) {
623
			// Sync the disk(s)
624
			mwexec('/bin/sync');
625
			if (!mwexec('/sbin/sysctl hw.ata.standby=' . ((int)$standby))) {
626
				// Reinitialize ATA-drives
627
				mwexec('/usr/local/sbin/atareinit');
628
				if ($g['booting']) {
629
					echo "done.\n";
630
				}
631
			} else if ($g['booting']) {
632
				echo "failed!\n";
633
			}
634
		} else if ($g['booting']) {
635
			echo "failed!\n";
636
		}
637
	}
638
}
639

    
640
function system_setup_sysctl() {
641
	$sysctl = return_filename_as_array("/etc/sysctl.conf");
642
	foreach($sysctl as $sysc) {
643
		if($sysc <> "")
644
			mwexec("sysctl {$sysc}");
645
	}
646
}
647

    
648
?>
(16-16/23)