Project

General

Profile

« Previous | Next » 

Revision d31bc32a

Added by Ermal LUÇI over 14 years ago

Make the CP locking more granular and make use correctly of exclusive/shared locks where appripriate. This speeds up CP login process.

View differences:

usr/local/captiveportal/index.php
268 268

  
269 269
	global $redirurl, $g, $config, $type, $passthrumac, $_POST;
270 270

  
271
	/* See if a ruleno is passed, if not start locking the sessions because this means there isn't one atm */
272
	$captiveshouldunlock = false;
273
	if ($ruleno == null) {
274
		$cplock = lock('captiveportal');
275
		$captiveshouldunlock = true;
271
	/* See if a ruleno is passed, if not start sessions because this means there isn't one atm */
272
	if ($ruleno == null)
276 273
		$ruleno = captiveportal_get_next_ipfw_ruleno();
277
	}
278 274

  
279 275
	/* if the pool is empty, return appropriate message and exit */
280 276
	if (is_null($ruleno)) {
281 277
		portal_reply_page($redirurl, "error", "System reached maximum login capacity");
282 278
		log_error("WARNING!  Captive portal has reached maximum login capacity");
283
		if ($captiveshouldunlock == true)
284
		unlock($cplock);
285 279
		exit;
286 280
	}
287 281

  
......
367 361
		}
368 362
	}
369 363

  
370
	if ($attributes['voucher'] && $remaining_time <= 0) {
371
		unlock($cplock);
364
	if ($attributes['voucher'] && $remaining_time <= 0)
372 365
		return 0;       // voucher already used and no time left
373
	}
374 366

  
375 367
	if (!isset($sessionid)) {
376

  
377 368
		/* generate unique session ID */
378 369
		$tod = gettimeofday();
379 370
		$sessionid = substr(md5(mt_rand() . $tod['sec'] . $tod['usec'] . $clientip . $clientmac), 0, 16);
......
405 396
			mwexec("/sbin/ipfw -q {$g['tmp_path']}/macentry.rules.tmp");
406 397
			$writecfg = true;
407 398
		} else {
408

  
409 399
			if ($peruserbw && !empty($bw_up) && is_numeric($bw_up)) {
410 400
				$bw_up_pipeno = $ruleno + 20000;
411 401
				//$bw_up /= 1000; // Scale to Kbit/s
......
448 438
			if (isset($config['captiveportal']['radacct_enable']) && !empty($radiusservers)) {
449 439
				$acct_val = RADIUS_ACCOUNTING_START($ruleno,
450 440
                                		$username, $sessionid, $radiusservers, $clientip, $clientmac);
451

  
452 441
				if ($acct_val == 1)
453 442
					captiveportal_logportalauth($username,$clientmac,$clientip,$type,"RADIUS ACCOUNTING FAILED");
454 443
			}
......
458 447
		}
459 448
	}
460 449

  
461
	if ($captiveshouldunlock == true)
462
		unlock($cplock);
463

  
464 450
	if ($writecfg == true)
465 451
		write_config();
466 452

  
......
502 488

  
503 489

  
504 490
/* remove a single client by session ID
505
   by Dinesh Nair
491
 *  by Dinesh Nair
506 492
 */
507 493
function disconnect_client($sessionid, $logoutReason = "LOGOUT", $term_cause = 1) {
508

  
509 494
    global $g, $config;
510 495

  
511
    $cplock = lock('captiveportal');
512 496
    /* read database */
513 497
    $cpdb = captiveportal_read_db();
514 498

  
......
527 511

  
528 512
    /* write database */
529 513
    captiveportal_write_db($cpdb);
530

  
531
    unlock($cplock);
532 514
}
533 515

  
534 516
/*
......
555 537

  
556 538
	$updatetimeouts = isset($config['captiveportal']['freelogins_updatetimeouts']);
557 539

  
558
	$cplock = lock('captiveportal');
559

  
560 540
	/*
561 541
	 * Read database of used MACs.  Lines are a comma-separated list
562 542
	 * of the time, MAC, then the count of pass-through credits remaining.
......
578 558
						captiveportal_write_usedmacs_db($usedmacs);
579 559
					}
580 560

  
581
					unlock($cplock);
582 561
					return false;
583 562
				} else {
584 563
					$usedmac[2] -= 1;
......
600 579
	}
601 580

  
602 581
	captiveportal_write_usedmacs_db($usedmacs);
603
	unlock($cplock);
604 582
	return true;
605 583
}
606 584

  
607 585
function captiveportal_read_usedmacs_db() {
608 586
	global $g;
609 587

  
588
	$cpumaclck = lock('captiveusedmacs');
610 589
	if (file_exists("{$g['vardb_path']}/captiveportal_usedmacs.db")) {
611 590
		$usedmacs = file("{$g['vardb_path']}/captiveportal_usedmacs.db", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
612 591
		if (!usedmacs)
......
614 593
	} else
615 594
		$usedmacs = array();
616 595

  
596
	unlock($cpumaclck);
617 597
	return $usedmacs;
618 598
}
619 599

  
620 600
function captiveportal_write_usedmacs_db($usedmacs) {
621 601
	global $g;
622 602

  
623
	file_put_contents("{$g['vardb_path']}/captiveportal_usedmacs.db", implode("\n", $usedmacs));
603
	$cpumaclck = lock('captiveusedmacs', LOCK_EX);
604
	@file_put_contents("{$g['vardb_path']}/captiveportal_usedmacs.db", implode("\n", $usedmacs));
605
	unlock($cpumaclck);
624 606
}
625 607

  
626 608
?>

Also available in: Unified diff