Project

General

Profile

« Previous | Next » 

Revision 1cb3a834

Added by Ryan Wagoner almost 18 years ago

-move upnp_action to services.inc
-make sure to clear rules when stopping miniupnpd
-fix status_upnp and status_services pages so they use upnp_action and not the rcfile

View differences:

etc/inc/services.inc
1269 1269
		conf_mount_ro();
1270 1270
}
1271 1271

  
1272
function upnp_action ($action) {
1273
	switch($action) {
1274
		case "start":
1275
			if(file_exists('/var/etc/miniupnpd.conf'))
1276
				mwexec_bg('/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf');
1277
			break;
1278
		case "stop":
1279
			while((int)exec("pgrep miniupnpd | wc -l") > 0)
1280
				mwexec('killall miniupnpd 2>/dev/null');
1281
			mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null');
1282
			mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null');
1283
			break;
1284
		case "restart":
1285
			upnp_action('stop');
1286
			upnp_action('start');
1287
			break;
1288
	}
1289
}
1290

  
1272 1291
function upnp_start() {
1273
	global $config;
1274
	if($config['installedpackages']['miniupnpd']['config'][0]["enable"]) {
1275
		mwexec_bg("/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf");
1292
	global $config, $g;
1293
	if($config['installedpackages']['miniupnpd']['config'][0]['enable']) {
1294
		if($g['booting']) {
1295
			echo "Starting UPnP service...";
1296
			include('/usr/local/pkg/miniupnpd.inc');
1297
			sync_package_miniupnpd();
1298
			echo "done.\n";
1299
		}
1300
		else {
1301
			upnp_action('start');
1302
		}
1276 1303
	}
1277 1304
}
1278 1305

  
1279
?>
1306
?>
usr/local/pkg/miniupnpd.inc
4 4

  
5 5
	/* MiniUPnPd */
6 6

  
7
	define('UPNP_CONFIG','/var/etc/miniupnpd.conf');
8

  
7 9
	function upnp_notice ($msg) { syslog(LOG_NOTICE, "miniupnpd: {$msg}"); return; }
8 10
	function upnp_warn ($msg) { syslog(LOG_WARNING, "miniupnpd: {$msg}"); return; }
9 11

  
10
	function upnp_action ($action) {
11
		switch($action) {
12
			case "start":
13
				mwexec("killall miniupnpd 2>/dev/null");
14
				upnp_start();
15
				break;
16
			case "stop":
17
				mwexec("killall miniupnpd");
18
				break;
19
			case "restart":
20
				mwexec("killall miniupnpd");
21
				upnp_start();
22
				break;
23
		}
24
	}
25

  
26 12
	function upnp_running () {
27 13
		if((int)exec("pgrep miniupnpd | wc -l") > 0)
28 14
			return true;
......
37 23
	}
38 24

  
39 25
	function upnp_write_config($conf_file, $conf_text) {
40
		$conf_file = "/var/etc/miniupnpd.conf";
41 26
		$conf = fopen($conf_file, "w");
42 27
		if(!$conf) {
43 28
			upnp_warn("Could not open {$conf_file} for writing.");
......
154 139
		/* since config is written before this file invoked we don't need to read post data */
155 140
		if(upnp_config('enable') && upnp_config('iface_array'))
156 141
			$iface_array = explode(',',upnp_config('iface_array'));
157
		else 
158
			upnp_action("stop");
159 142

  
160 143
		if($iface_array) {
161 144
			foreach($iface_array as $iface) {
......
233 216
				if(upnp_config('permdefault'))
234 217
					$configtext .= "deny 0-65535 0.0.0.0/0 0-65535\n";
235 218

  
236
				/* generate rc file start and stop */
237
				$stop = <<<EOD
238
if [ `pgrep miniupnpd | wc -l` != 0  ]; then
239
		/usr/bin/killall miniupnpd
240
		while [ `pgrep miniupnpd | wc -l` != 0 ]; do
241
			sleep 1
242
		done
243
	fi
244
	# Clear existing rules and rdr entries
245
	if [ `pfctl -aminiupnpd -sr | wc -l` != 0  ]; then
246
		/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null
247
	fi
248
	if [ `pfctl -aminiupnpd -sn | wc -l` != 0  ]; then
249
		/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null
250
	fi
251
EOD;
252
				$start = $stop."\n\t/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf";
253

  
254 219
				/* write out the configuration */
255
				conf_mount_rw();
256
				upnp_write_config("/var/etc/miniupnpd.conf",$configtext);
257
				conf_mount_ro();
220
				upnp_write_config(UPNP_CONFIG,$configtext);
258 221
				
259 222
				/* if miniupnpd not running start it */
260 223
				if(!upnp_running()) {
......
273 236
			/* no parameters user does not want miniupnpd running */
274 237
			/* lets stop the service and remove the rc file */
275 238

  
276
			if(file_exists(UPNP_RCFILE)) {
239
			if(file_exists(UPNP_CONFIG)) {
277 240
				if(!upnp_config('enable'))
278 241
					upnp_notice('Stopping service: miniupnpd disabled');
279 242
				else
280
					upnp_notice('Stopping service: no interfaces selected');
281
					
282
				upnp_action('stop');
243
					upnp_notice('Stopping service: no interfaces selected');				
283 244

  
284
				conf_mount_rw();
285
				unlink(UPNP_RCFILE);
286
				unlink("/var/etc/miniupnpd.conf");
287
				conf_mount_ro();
245
				upnp_notice('Stopping service: miniupnpd disabled');
246
				upnp_action('stop');
247
				unlink(UPNP_CONFIG);
288 248
			}
289 249
		}
290 250

  
usr/local/pkg/miniupnpd.xml
123 123
	<custom_php_resync_config_command>
124 124
		sync_package_miniupnpd();
125 125
	</custom_php_resync_config_command>
126
	<custom_php_install_command>
127
		sync_package_miniupnpd();
128
	</custom_php_install_command>
129
	<custom_php_deinstall_command>
130
		exec("rm -f /usr/local/etc/rc.d/miniupnpd*");
131
	</custom_php_deinstall_command>
132 126
</packagegui>
usr/local/www/status_services.php
54 54
			services_dhcpd_configure();
55 55
			break;
56 56
		case 'miniupnpd':
57
			if(file_exists('/usr/local/etc/rc.d/miniupnpd.sh'))
58
				mwexec('/usr/local/etc/rc.d/miniupnpd.sh restart');
57
			upnp_action('restart');
59 58
			break;
60 59
        case 'racoon':
61 60
        	exec("killall -9 racoon");
......
82 81
			services_dhcpd_configure();
83 82
			break;
84 83
		case 'miniupnpd':
85
			if(file_exists('/usr/local/etc/rc.d/miniupnpd.sh'))
86
				mwexec('/usr/local/etc/rc.d/miniupnpd.sh start');
84
			upnp_action('start');
87 85
			break;
88 86
        case 'racoon':
89 87
        	exec("killall -9 racoon");
......
117 115
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
118 116
			break;
119 117
		case 'miniupnpd':
120
			/* can't just killbyname since we need to clear pf rules */
121
			if(file_exists('/usr/local/etc/rc.d/miniupnpd.sh'))
122
				mwexec('/usr/local/etc/rc.d/miniupnpd.sh stop');
118
			upnp_action('stop');
123 119
			break;
124 120
		case 'ntpd':
125 121
			killbyname("ntpd");
usr/local/www/status_upnp.php
33 33

  
34 34
if ($_POST) {
35 35
	if ($_POST['clear'] == "Clear") {
36
		mwexec("/bin/sh /usr/local/etc/rc.d/miniupnpd.sh restart");
36
		upnp_action('restart');
37 37
		$savemsg = "Rules have been cleared and the daemon restarted";
38 38
	}
39 39
}

Also available in: Unified diff