srs_core_config.cpp 15.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
/*
The MIT License (MIT)

Copyright (c) 2013 winlin

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <srs_core_config.hpp>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
// file operations.
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <vector>
#include <algorithm>

#include <srs_core_error.hpp>
#include <srs_core_log.hpp>
#include <srs_core_autofree.hpp>

#define FILE_OFFSET(fd) lseek(fd, 0, SEEK_CUR)

int64_t FILE_SIZE(int fd)
{
	int64_t pre = FILE_OFFSET(fd);
	int64_t pos = lseek(fd, 0, SEEK_END);
	lseek(fd, pre, SEEK_SET);
	return pos;
}

#define LF (char)0x0a
#define CR (char)0x0d

bool is_common_space(char ch)
{
	return (ch == ' ' || ch == '\t' || ch == CR || ch == LF);
}

#define CONF_BUFFER_SIZE 4096

SrsFileBuffer::SrsFileBuffer()
{
	fd = -1;
	line = 0;

	pos = last = start = new char[CONF_BUFFER_SIZE];
	end = start + CONF_BUFFER_SIZE;
}

SrsFileBuffer::~SrsFileBuffer()
{
	if (fd > 0) {
		close(fd);
	}
	srs_freepa(start);
}

int SrsFileBuffer::open(const char* filename)
{
	assert(fd == -1);
	
	if ((fd = ::open(filename, O_RDONLY, 0)) < 0) {
		srs_error("open conf file error. errno=%d(%s)", errno, strerror(errno));
		return ERROR_SYSTEM_CONFIG_INVALID;
	}
	
	line = 1;
	
	return ERROR_SUCCESS;
}

SrsConfDirective::SrsConfDirective()
{
}

SrsConfDirective::~SrsConfDirective()
{
	std::vector<SrsConfDirective*>::iterator it;
	for (it = directives.begin(); it != directives.end(); ++it) {
		SrsConfDirective* directive = *it;
		srs_freep(directive);
	}
	directives.clear();
}

std::string SrsConfDirective::arg0()
{
	if (args.size() > 0) {
		return args.at(0);
	}
	
	return "";
}

std::string SrsConfDirective::arg1()
{
	if (args.size() > 1) {
		return args.at(1);
	}
	
	return "";
}

std::string SrsConfDirective::arg2()
{
	if (args.size() > 2) {
		return args.at(2);
	}
	
	return "";
}

SrsConfDirective* SrsConfDirective::at(int index)
{
	return directives.at(index);
}

SrsConfDirective* SrsConfDirective::get(std::string _name)
{
	std::vector<SrsConfDirective*>::iterator it;
	for (it = directives.begin(); it != directives.end(); ++it) {
		SrsConfDirective* directive = *it;
		if (directive->name == _name) {
			return directive;
		}
	}
	
	return NULL;
}

int SrsConfDirective::parse(const char* filename)
{
	int ret = ERROR_SUCCESS;
	
	SrsFileBuffer buffer;
	
	if ((ret = buffer.open(filename)) != ERROR_SUCCESS) {
		return ret;
	}
	
	return parse_conf(&buffer, parse_file);
}

// see: ngx_conf_parse
int SrsConfDirective::parse_conf(SrsFileBuffer* buffer, SrsDirectiveType type)
{
	int ret = ERROR_SUCCESS;
	
	while (true) {
		std::vector<std::string> args;
		ret = read_token(buffer, args);
		
		/**
		* ret maybe:
		* ERROR_SYSTEM_CONFIG_INVALID 		error.
		* ERROR_SYSTEM_CONFIG_DIRECTIVE		directive terminated by ';' found
		* ERROR_SYSTEM_CONFIG_BLOCK_START	token terminated by '{' found
		* ERROR_SYSTEM_CONFIG_BLOCK_END		the '}' found
		* ERROR_SYSTEM_CONFIG_EOF			the config file is done
		*/
		if (ret == ERROR_SYSTEM_CONFIG_INVALID) {
			return ret;
		}
		if (ret == ERROR_SYSTEM_CONFIG_BLOCK_END) {
			if (type != parse_block) {
				srs_error("line %d: unexpected \"}\"", buffer->line);
				return ret;
			}
			return ERROR_SUCCESS;
		}
		if (ret == ERROR_SYSTEM_CONFIG_EOF) {
			if (type == parse_block) {
				srs_error("line %d: unexpected end of file, expecting \"}\"", buffer->line);
				return ret;
			}
			return ERROR_SUCCESS;
		}
		
		if (args.empty()) {
			srs_error("line %d: empty directive.", buffer->line);
			return ret;
		}
		
		// build directive tree.
		SrsConfDirective* directive = new SrsConfDirective();

		directive->conf_line = buffer->line;
		directive->name = args[0];
		args.erase(args.begin());
		directive->args.swap(args);
		
		directives.push_back(directive);
		
		if (ret == ERROR_SYSTEM_CONFIG_BLOCK_START) {
			if ((ret = directive->parse_conf(buffer, parse_block)) != ERROR_SUCCESS) {
				return ret;
			}
		}
	}
	
	return ret;
}

// see: ngx_conf_read_token
int SrsConfDirective::read_token(SrsFileBuffer* buffer, std::vector<std::string>& args)
{
	int ret = ERROR_SUCCESS;

	char* pstart = buffer->pos;
	int startline = buffer->line;

	bool sharp_comment = false;
	
	bool d_quoted = false;
	bool s_quoted = false;
	
	bool need_space = false;
	bool last_space = true;
	
	while (true) {
		if ((ret = refill_buffer(buffer, d_quoted, s_quoted, startline, pstart)) != ERROR_SUCCESS) {
			if (!args.empty() || !last_space) {
				srs_error("line %d: unexpected end of file, expecting ; or \"}\"", buffer->line);
				return ERROR_SYSTEM_CONFIG_INVALID;
			}
			return ret;
		}
		
		char ch = *buffer->pos++;
		
		if (ch == LF) {
			buffer->line++;
			sharp_comment = false;
		}
		
		if (sharp_comment) {
			continue;
		}
		
		if (need_space) {
			if (is_common_space(ch)) {
				last_space = true;
				need_space = false;
				continue;
			}
			if (ch == ';') {
				return ERROR_SYSTEM_CONFIG_DIRECTIVE;
			}
			if (ch == '{') {
				return ERROR_SYSTEM_CONFIG_BLOCK_START;
			}
			srs_error("line %d: unexpected '%c'", buffer->line, ch);
			return ERROR_SYSTEM_CONFIG_INVALID; 
		}
		
		// last charecter is space.
		if (last_space) {
			if (is_common_space(ch)) {
				continue;
			}
			pstart = buffer->pos - 1;
			startline = buffer->line;
			switch (ch) {
				case ';':
					if (args.size() == 0) {
						srs_error("line %d: unexpected ';'", buffer->line);
						return ERROR_SYSTEM_CONFIG_INVALID;
					}
					return ERROR_SYSTEM_CONFIG_DIRECTIVE;
				case '{':
					if (args.size() == 0) {
						srs_error("line %d: unexpected '{'", buffer->line);
						return ERROR_SYSTEM_CONFIG_INVALID;
					}
					return ERROR_SYSTEM_CONFIG_BLOCK_START;
				case '}':
					if (args.size() != 0) {
						srs_error("line %d: unexpected '}'", buffer->line);
						return ERROR_SYSTEM_CONFIG_INVALID;
					}
					return ERROR_SYSTEM_CONFIG_BLOCK_END;
				case '#':
					sharp_comment = 1;
					continue;
				case '"':
					pstart++;
					d_quoted = true;
					last_space = 0;
					continue;
				case '\'':
					pstart++;
					s_quoted = true;
					last_space = 0;
					continue;
				default:
					last_space = 0;
					continue;
			}
		} else {
		// last charecter is not space
			bool found = false;
			if (d_quoted) {
				if (ch == '"') {
					d_quoted = false;
					need_space = true;
					found = true;
				}
			} else if (s_quoted) {
				if (ch == '\'') {
					s_quoted = false;
					need_space = true;
					found = true;
				}
			} else if (is_common_space(ch) || ch == ';' || ch == '{') {
				last_space = true;
				found = 1;
			}
			
			if (found) {
				int len = buffer->pos - pstart;
				char* word = new char[len];
				memcpy(word, pstart, len);
				word[len - 1] = 0;
				
				args.push_back(word);
				srs_freepa(word);
				
				if (ch == ';') {
					return ERROR_SYSTEM_CONFIG_DIRECTIVE;
				}
				if (ch == '{') {
					return ERROR_SYSTEM_CONFIG_BLOCK_START;
				}
			}
		}
	}
	
	return ret;
}

int SrsConfDirective::refill_buffer(SrsFileBuffer* buffer, bool d_quoted, bool s_quoted, int startline, char*& pstart)
{
	int ret = ERROR_SUCCESS;
	
	if (buffer->pos < buffer->last) {
		return ret;
	}
	
	int size = FILE_SIZE(buffer->fd) - FILE_OFFSET(buffer->fd);
	
	if (size <= 0) {
		return ERROR_SYSTEM_CONFIG_EOF;
	}
	
	int len = buffer->pos - buffer->start;
	if (len >= CONF_BUFFER_SIZE) {
		buffer->line = startline;
		
		if (!d_quoted && !s_quoted) {
			srs_error("line %d: too long parameter \"%*s...\" started", 
				buffer->line, 10, buffer->start);
			
		} else {
			srs_error("line %d: too long parameter, "
				"probably missing terminating '%c' character", buffer->line, d_quoted? '"':'\'');
		}
		return ERROR_SYSTEM_CONFIG_INVALID;
	}
	
	if (len) {
		memmove(buffer->start, pstart, len);
	}
	
	size = srs_min(size, buffer->end - (buffer->start + len));
	int n = read(buffer->fd, buffer->start + len, size);
	if (n != size) {
		srs_error("read file read error. expect %d, actual %d bytes.", size, n);
		return ERROR_SYSTEM_CONFIG_INVALID;
	}
	
	buffer->pos = buffer->start + len;
	buffer->last = buffer->pos + n;
	pstart = buffer->start;
	
	return ret;
}

SrsConfig* config = new SrsConfig();

SrsConfig::SrsConfig()
{
	show_help = false;
	show_version = false;
	
	root = new SrsConfDirective();
	root->conf_line = 0;
	root->name = "root";
}

SrsConfig::~SrsConfig()
{
	srs_freep(root);
}

int SrsConfig::reload()
{
	int ret = ERROR_SUCCESS;

	SrsConfig conf;
	if ((ret = conf.parse_file(config_file.c_str())) != ERROR_SUCCESS) {
		srs_error("config reloader parse file failed. ret=%d", ret);
		return ret;
	}
	srs_info("config reloader parse file success.");
	
	// store current root to old_root,
	// and reap the root from conf to current root.
	SrsConfDirective* old_root = root;
	SrsAutoFree(SrsConfDirective, old_root, false);
	
	root = conf.root;
	conf.root = NULL;
	
	// merge config.
	std::vector<SrsReloadHandler*>::iterator it;

	// merge config: listen
	if (!srs_directive_equals(root->get("listen"), old_root->get("listen"))) {
		for (it = subscribes.begin(); it != subscribes.end(); ++it) {
			SrsReloadHandler* subscribe = *it;
			if ((ret = subscribe->on_reload_listen()) != ERROR_SUCCESS) {
				srs_error("notify subscribes reload listen failed. ret=%d", ret);
				return ret;
			}
		}
		srs_trace("reload listen success.");
	}
	// merge config: pithy_print
	if (!srs_directive_equals(root->get("pithy_print"), old_root->get("pithy_print"))) {
		for (it = subscribes.begin(); it != subscribes.end(); ++it) {
			SrsReloadHandler* subscribe = *it;
			if ((ret = subscribe->on_reload_pithy_print()) != ERROR_SUCCESS) {
				srs_error("notify subscribes pithy_print listen failed. ret=%d", ret);
				return ret;
			}
		}
		srs_trace("reload pithy_print success.");
	}
	
	return ret;
}

void SrsConfig::subscribe(SrsReloadHandler* handler)
{
	std::vector<SrsReloadHandler*>::iterator it;
	
	it = std::find(subscribes.begin(), subscribes.end(), handler);
	if (it != subscribes.end()) {
		return;
	}
	
	subscribes.push_back(handler);
}

void SrsConfig::unsubscribe(SrsReloadHandler* handler)
{
	std::vector<SrsReloadHandler*>::iterator it;
	
	it = std::find(subscribes.begin(), subscribes.end(), handler);
	if (it == subscribes.end()) {
		return;
	}
	
	subscribes.erase(it);
}

// see: ngx_get_options
int SrsConfig::parse_options(int argc, char** argv)
{
	int ret = ERROR_SUCCESS;
	
	for (int i = 1; i < argc; i++) {
		if ((ret = parse_argv(i, argv)) != ERROR_SUCCESS) {
			return ret;
		}
	}
	
	if (show_help) {
		print_help(argv);
	}
	
	if (show_version) {
		printf("%s\n", RTMP_SIG_SRS_VERSION);
	}
	
	if (show_help || show_version) {
		exit(0);
	}
	
	if (config_file.empty()) {
		ret = ERROR_SYSTEM_CONFIG_INVALID;
		srs_error("config file not specified, see help: %s -h, ret=%d", argv[0], ret);
		return ret;
	}

	return parse_file(config_file.c_str());
}

SrsConfDirective* SrsConfig::get_vhost(std::string vhost)
{
	srs_assert(root);
	
	for (int i = 0; i < (int)root->directives.size(); i++) {
		SrsConfDirective* conf = root->at(i);
		
		if (conf->name != "vhost") {
			continue;
		}
		
		if (conf->arg0() == vhost) {
			return conf;
		}
	}
	
	if (vhost != RTMP_VHOST_DEFAULT) {
		return get_vhost(RTMP_VHOST_DEFAULT);
	}
	
	return NULL;
}

SrsConfDirective* SrsConfig::get_gop_cache(std::string vhost)
{
	SrsConfDirective* conf = get_vhost(vhost);

	if (!conf) {
		return NULL;
	}
	
	return conf->get("gop_cache");
}

SrsConfDirective* SrsConfig::get_hls(std::string vhost)
{
	SrsConfDirective* conf = get_vhost(vhost);

	if (!conf) {
		return NULL;
	}
	
	return conf->get("hls");
}

SrsConfDirective* SrsConfig::get_hls_path(std::string vhost)
{
	SrsConfDirective* conf = get_vhost(vhost);

	if (!conf) {
		return NULL;
	}
	
	return conf->get("hls_path");
}

SrsConfDirective* SrsConfig::get_refer(std::string vhost)
{
	SrsConfDirective* conf = get_vhost(vhost);

	if (!conf) {
		return NULL;
	}
	
	return conf->get("refer");
}

SrsConfDirective* SrsConfig::get_refer_play(std::string vhost)
{
	SrsConfDirective* conf = get_vhost(vhost);

	if (!conf) {
		return NULL;
	}
	
	return conf->get("refer_play");
}

SrsConfDirective* SrsConfig::get_refer_publish(std::string vhost)
{
	SrsConfDirective* conf = get_vhost(vhost);

	if (!conf) {
		return NULL;
	}
	
	return conf->get("refer_publish");
}

SrsConfDirective* SrsConfig::get_listen()
{
	return root->get("listen");
}

SrsConfDirective* SrsConfig::get_chunk_size()
{
	return root->get("chunk_size");
}

SrsConfDirective* SrsConfig::get_pithy_print_publish()
{
	SrsConfDirective* pithy = root->get("pithy_print");
	if (!pithy) {
		return NULL;
	}
	
	return pithy->get("publish");
}

SrsConfDirective* SrsConfig::get_pithy_print_play()
{
	SrsConfDirective* pithy = root->get("pithy_print");
	if (!pithy) {
		return NULL;
	}
	
	return pithy->get("play");
}

int SrsConfig::parse_file(const char* filename)
{
	int ret = ERROR_SUCCESS;
	
	config_file = filename;
	
	if (config_file.empty()) {
		return ERROR_SYSTEM_CONFIG_INVALID;
	}
	
	if ((ret = root->parse(config_file.c_str())) != ERROR_SUCCESS) {
		return ret;
	}
	
	SrsConfDirective* conf = NULL;
	if ((conf = get_listen()) == NULL || conf->args.size() == 0) {
		ret = ERROR_SYSTEM_CONFIG_INVALID;
		srs_error("line %d: conf error, "
			"directive \"listen\" is empty, ret=%d", (conf? conf->conf_line:0), ret);
		return ret;
	}
	
	return ret;
}

int SrsConfig::parse_argv(int& i, char** argv)
{
	int ret = ERROR_SUCCESS;

	char* p = argv[i];
		
	if (*p++ != '-') {
		ret = ERROR_SYSTEM_CONFIG_INVALID;
		srs_error("invalid options(index=%d, value=%s), "
			"must starts with -, see help: %s -h, ret=%d", i, argv[i], argv[0], ret);
		return ret;
	}
	
	while (*p) {
		switch (*p++) {
			case '?':
			case 'h':
				show_help = true;
				break;
			case 'v':
			case 'V':
				show_version = true;
				break;
			case 'c':
				if (*p) {
					config_file = p;
					return ret;
				}
				if (argv[++i]) {
					config_file = argv[i];
					return ret;
				}
				ret = ERROR_SYSTEM_CONFIG_INVALID;
				srs_error("option \"-c\" requires parameter, ret=%d", ret);
				return ret;
			default:
				ret = ERROR_SYSTEM_CONFIG_INVALID;
				srs_error("invalid option: \"%c\", see help: %s -h, ret=%d", *(p - 1), argv[0], ret);
				return ret;
		}
	}
	
	return ret;
}

void SrsConfig::print_help(char** argv)
{
	printf(RTMP_SIG_SRS_NAME" "RTMP_SIG_SRS_VERSION
		" Copyright (c) 2013 winlin\n"
		"Usage: %s [-h?vV] [-c <filename>]\n" 
		"\n"
		"Options:\n"
		"   -?-h            : show help\n"
		"   -v-V            : show version and exit\n"
		"   -c filename     : set configuration file\n"
		"\n"
		RTMP_SIG_SRS_WEB"\n"
		RTMP_SIG_SRS_URL"\n"
		"Email: "RTMP_SIG_SRS_EMAIL"\n",
		argv[0]);
}

bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b)
{
	if (!a || !b) {
		return false;
	}
	
	if (a->name != b->name) {
		return false;
	}
	
	if (a->args.size() != b->args.size()) {
		return false;
	}
	
	for (int i = 0; i < (int)a->args.size(); i++) {
		if (a->args.at(i) != b->args.at(i)) {
			return false;
		}
	}
	
	if (a->directives.size() != b->directives.size()) {
		return false;
	}
	
	for (int i = 0; i < (int)a->directives.size(); i++) {
		SrsConfDirective* a0 = a->at(i);
		SrsConfDirective* b0 = b->at(i);
		
		if (!srs_directive_equals(a0, b0)) {
			return false;
		}
	}
	
	return true;
}