Esempio n. 1
0
static wi_process_t * _wi_process_init_with_argv(wi_process_t *process, int argc, const char **argv) {
	wi_array_t			*array;
	wi_string_t			*string;
	struct utsname		name;
#if defined(HAVE_NXGETLOCALARCHINFO)
	const NXArchInfo	*archinfo;
	cpu_type_t			cputype;
	size_t				cputypesize;
#elif defined(HAVE_SYSINFO) && defined(SI_ARCHITECTURE)
	char				buffer[SYS_NMLN];
#endif

	
	array = wi_array_init_with_argv(wi_array_alloc(), argc, argv);
	
	string = wi_array_first_data(array);
	
	if(string) {
		process->path = wi_retain(string);
		process->name = wi_retain(wi_string_last_path_component(process->path));
	} else {
		process->path = wi_retain(WI_STR("unknown"));
		process->name = wi_retain(process->path);
	}
	
	if(wi_array_count(array) <= 1)
		process->arguments = wi_array_init(wi_array_alloc());
	else
		process->arguments = wi_retain(wi_array_subarray_with_range(array, wi_make_range(1, wi_array_count(array) - 1)));
	
	wi_release(array);
	
	uname(&name);
	
	process->os_name = wi_string_init_with_cstring(wi_string_alloc(), name.sysname);
	process->os_release = wi_string_init_with_cstring(wi_string_alloc(), name.release);

#if defined(HAVE_NXGETLOCALARCHINFO)
	cputypesize = sizeof(cputype);
	
	if(sysctlbyname("sysctl.proc_cputype", &cputype, &cputypesize, NULL, 0) < 0)
		cputype = NXGetLocalArchInfo()->cputype;
	
	archinfo = NXGetArchInfoFromCpuType(cputype, CPU_SUBTYPE_MULTIPLE);
	
	if(archinfo)
		process->arch = wi_string_init_with_cstring(wi_string_alloc(), archinfo->name);
#elif defined(HAVE_SYSINFO) && defined(SI_ARCHITECTURE)
	if(sysinfo(SI_ARCHITECTURE, buffer, sizeof(buffer)) >= 0)
		process->arch = wi_string_init_with_cstring(wi_string_alloc(), buffer);
#endif

	if(!process->arch)
		process->arch = wi_string_init_with_cstring(wi_string_alloc(), name.machine);

	return process;
}
Esempio n. 2
0
static wi_boolean_t _wi_file_copy_directory(wi_string_t *frompath, wi_string_t *topath) {
	WI_FTS			*fts;
	WI_FTSENT		*p;
	wi_string_t		*path, *newpath;
	char			*paths[2];
	uint32_t		pathlength;
	wi_boolean_t	result = true;

	paths[0] = (char *) wi_string_cstring(frompath);;
	paths[1] = NULL;

	fts = wi_fts_open(paths, WI_FTS_LOGICAL | WI_FTS_NOSTAT, NULL);

	if(!fts)
		return false;
	
	pathlength = wi_string_length(frompath);

	while((p = wi_fts_read(fts))) {
		path = wi_string_init_with_cstring(wi_string_alloc(), p->fts_path);
		newpath = wi_string_init_with_cstring(wi_string_alloc(), p->fts_path + pathlength);
		wi_string_insert_string_at_index(newpath, topath, 0);		

		switch(p->fts_info) {
			case WI_FTS_NS:
			case WI_FTS_ERR:
			case WI_FTS_DNR:
				errno = p->fts_errno;
				result = false;
				break;
			
			case WI_FTS_DC:
			case WI_FTS_DP:
				break;
				
			case WI_FTS_D:
				if(!wi_file_create_directory(newpath, 0777))
					result = false;
				break;
			
			default:
				if(!_wi_file_copy_file(path, newpath))
					result = false;
				break;
		}

		wi_release(newpath);
		wi_release(path);
	}

	wi_fts_close(fts);
	
	return result;
}
Esempio n. 3
0
wi_string_t * wi_socket_certificate_hostname(wi_socket_t *socket) {
#ifdef HAVE_OPENSSL_SSL_H
	X509			*x509;
	wi_string_t		*string;
	char			hostname[MAXHOSTNAMELEN];

	x509 = SSL_get_peer_certificate(socket->ssl);

	if(!x509)
		return NULL;

	X509_NAME_get_text_by_NID(X509_get_subject_name(x509),
							  NID_commonName,
							  hostname,
							  sizeof(hostname));
	
	string = wi_string_init_with_cstring(wi_string_alloc(), hostname);
	
	X509_free(x509);
	
	return wi_autorelease(string);
#else
	return NULL;
#endif
}
Esempio n. 4
0
wi_array_t * wi_file_directory_contents_at_path(wi_string_t *path) {
	wi_array_t		*contents;
	wi_string_t		*name;
	DIR				*dir;
	struct dirent	de, *dep;
	
	dir = opendir(wi_string_cstring(path));
	
	if(!dir) {
		wi_error_set_errno(errno);
		
		return NULL;
	}
	
	contents = wi_array_init_with_capacity(wi_array_alloc(), 100);
	
	while(readdir_r(dir, &de, &dep) == 0 && dep) {
		if(strcmp(dep->d_name, ".") != 0 && strcmp(dep->d_name, "..") != 0) {
			name = wi_string_init_with_cstring(wi_string_alloc(), dep->d_name);
			wi_array_add_data(contents, name);
			wi_release(name);
		}
	}

	closedir(dir);
	
	return wi_autorelease(contents);
}
Esempio n. 5
0
wi_string_t * wi_error_string(void) {
	wi_error_t		*error;

	error = _wi_error_get_error();

	if(!error->string) {
		switch(error->domain) {
			case WI_ERROR_DOMAIN_ERRNO:
				error->string = wi_string_init_with_cstring(wi_string_alloc(), strerror(error->code));
				break;

			case WI_ERROR_DOMAIN_GAI:
				error->string = wi_string_init_with_cstring(wi_string_alloc(), gai_strerror(error->code));
				break;

			case WI_ERROR_DOMAIN_REGEX:
			case WI_ERROR_DOMAIN_OPENSSL:
			case WI_ERROR_DOMAIN_OPENSSL_SSL:
			case WI_ERROR_DOMAIN_COMMONCRYPTO:
			case WI_ERROR_DOMAIN_LIBXML2:
			case WI_ERROR_DOMAIN_SQLITE3:
				break;
			
			case WI_ERROR_DOMAIN_ZLIB:
#ifdef WI_ZLIB
				error->string = wi_string_init_with_format(wi_string_alloc(), WI_STR("zlib: %s"), zError(error->code));
#endif
				break;
			
			case WI_ERROR_DOMAIN_CARBON:
				error->string = wi_string_init_with_format(wi_string_alloc(), WI_STR("Carbon: %d"), error->code);
				break;

			case WI_ERROR_DOMAIN_LIBWIRED:
				error->string = wi_string_init_with_cstring(wi_string_alloc(), _wi_error_strings[error->code]);
				break;

			case WI_ERROR_DOMAIN_NONE:
				error->string = wi_string_init_with_format(wi_string_alloc(), WI_STR("Unknown error domain: %d"), error->code);
				break;
		}
	}

	return error->string;
}
Esempio n. 6
0
wi_string_t * wi_socket_certificate_name(wi_socket_t *socket) {
#ifdef HAVE_OPENSSL_SSL_H
	X509			*x509 = NULL;
	EVP_PKEY		*pkey = NULL;
	wi_string_t		*string = NULL;

	x509 = SSL_get_peer_certificate(socket->ssl);

	if(!x509)
		goto end;

	pkey = X509_get_pubkey(x509);

	if(!pkey)
		goto end;
	
	switch(EVP_PKEY_type(pkey->type)) {
		case EVP_PKEY_RSA:
			string = wi_string_init_with_cstring(wi_string_alloc(), "RSA");
			break;

		case EVP_PKEY_DSA:
			string = wi_string_init_with_cstring(wi_string_alloc(), "DSA");
			break;

		case EVP_PKEY_DH:
			string = wi_string_init_with_cstring(wi_string_alloc(), "DH");
			break;

		default:
			break;
	}
	
end:
	if(x509)
		X509_free(x509);

	if(pkey)
		EVP_PKEY_free(pkey);

	return wi_autorelease(string);
#else
	return NULL;
#endif
}
Esempio n. 7
0
static wi_process_t * _wi_process_init_with_argv(wi_process_t *process, int argc, const char **argv) {
	wi_array_t			*array;
	wi_string_t			*string;
	struct utsname		name;
#ifdef HAVE_MACH_O_ARCH_H
	const NXArchInfo	*arch_info;
#endif
	
	array = wi_array_init_with_argv(wi_array_alloc(), argc, argv);
	
	string = wi_array_first_data(array);
	
	if(string) {
		process->path = wi_retain(string);
		process->name = wi_retain(wi_string_last_path_component(process->path));
	} else {
		process->path = wi_retain(WI_STR("unknown"));
		process->name = wi_retain(process->path);
	}
	
	if(wi_array_count(array) <= 1)
		process->arguments = wi_array_init(wi_array_alloc());
	else
		process->arguments = wi_retain(wi_array_subarray_with_range(array, wi_make_range(1, wi_array_count(array) - 1)));
	
	wi_release(array);
	
	uname(&name);
	
	process->os_name = wi_string_init_with_cstring(wi_string_alloc(), name.sysname);
	process->os_release = wi_string_init_with_cstring(wi_string_alloc(), name.release);

#ifdef HAVE_MACH_O_ARCH_H
	arch_info = NXGetArchInfoFromCpuType(NXGetLocalArchInfo()->cputype, CPU_SUBTYPE_MULTIPLE);
	
	process->arch = wi_string_init_with_cstring(wi_string_alloc(), arch_info->name);
#else
	process->arch = wi_string_init_with_cstring(wi_string_alloc(), name.machine);
#endif

	return process;
}
Esempio n. 8
0
void wi_error_set_regex_error(regex_t *regex, int code) {
	wi_error_t		*error;
	char			string[256];

	error = _wi_error_get_error();
	error->domain = WI_ERROR_DOMAIN_REGEX;
	error->code = code;
	
	regerror(code, regex, string, sizeof(string));

	wi_release(error->string);
	error->string = wi_string_init_with_cstring(wi_string_alloc(), string);
}
Esempio n. 9
0
wi_array_t * wi_array_init_with_argv(wi_array_t *array, int argc, const char **argv) {
	wi_string_t		*string;
	int				i;
	
	array = wi_array_init_with_capacity(array, argc);
	
	for(i = 0; i < argc; i++) {
		string = wi_string_init_with_cstring(wi_string_alloc(), argv[i]);
		wi_array_add_data(array, string);
		wi_release(string);
	}
	
	return array;
}
Esempio n. 10
0
wi_terminal_t * wi_terminal_init(wi_terminal_t *terminal) {
	wi_string_t		*type;
	const char		*term;
	
	term = getenv("TERM");
		
	if(!term)
		term = "vt100";
	
	type = wi_string_init_with_cstring(wi_string_alloc(), term);
	terminal = wi_terminal_init_with_type(terminal, type);
	wi_release(type);
	
	return terminal;
}
Esempio n. 11
0
void wi_error_set_libxml2_error(void) {
	wi_error_t		*error;
	xmlErrorPtr		xml_error;

	xml_error = xmlGetLastError();

	error = _wi_get_error();
	error->domain = WI_ERROR_DOMAIN_REGEX;
	error->code = xml_error->code;
	
	wi_release(error->string);

	error->string = wi_string_init_with_cstring(wi_string_alloc(), xml_error->message);
	wi_string_delete_surrounding_whitespace(error->string);
}
Esempio n. 12
0
wi_string_t * _wi_string_constant_string(const char *cstring) {
	wi_string_t			*string;
	
	wi_lock_lock(_wi_string_constant_string_lock);
	string = wi_dictionary_data_for_key(_wi_string_constant_string_table, (void *) cstring);
	
	if(!string) {
		string = wi_string_init_with_cstring(wi_string_alloc(), cstring);
		wi_mutable_dictionary_set_data_for_key(_wi_string_constant_string_table, string, (void *) cstring);
		wi_release(string);
	}
	
	wi_lock_unlock(_wi_string_constant_string_lock);

	return string;
}
Esempio n. 13
0
void wi_error_set_libwired_error_with_string(int code, wi_string_t *string) {
	wi_string_t		*errorstring;
	
	errorstring = wi_string_init_with_cstring(wi_mutable_string_alloc(), _wi_error_strings[code]);

	if(wi_string_length(string) > 0) {
		if(wi_string_length(errorstring) > 0)
			wi_mutable_string_append_string(errorstring, WI_STR(": "));
		
		wi_mutable_string_append_string(errorstring, string);
	}
	
	wi_error_set_error_with_string(WI_ERROR_DOMAIN_LIBWIRED, code, errorstring);
	
	wi_release(errorstring);
}
Esempio n. 14
0
void wi_error_set_libwired_error_with_string(int code, wi_string_t *string) {
	wi_error_t		*error;

	error = _wi_get_error();
	error->domain = WI_ERROR_DOMAIN_LIBWIRED;
	error->code = code;
	
	wi_release(error->string);
	
	error->string = wi_string_init_with_cstring(wi_string_alloc(), _wi_error_strings[error->code]);

	if(wi_string_length(string) > 0) {
		if(wi_string_length(error->string) > 0)
			wi_string_append_string(error->string, WI_STR(": "));
		
		wi_string_append_string(error->string, string);
	}
}
Esempio n. 15
0
void wr_client_init(void) {
	wr_socket_tls = wi_socket_tls_init_with_type(wi_socket_tls_alloc(), WI_SOCKET_TLS_CLIENT);
	
	if(!wr_socket_tls)
		wi_log_error(WI_STR("Could not create TLS context: %m"));
	
	if(!wi_socket_tls_set_ciphers(wr_socket_tls, WI_STR("ALL:NULL:!MD5:@STRENGTH")))
		wi_log_error(WI_STR("Could not set TLS ciphers: %m"));
	
	wr_server_string_encoding = wi_string_encoding_init_with_charset(
		wi_string_encoding_alloc(),
		WI_STR("UTF-8"),
		WI_STRING_ENCODING_IGNORE | WI_STRING_ENCODING_TRANSLITERATE);
	
	wr_set_charset(WI_STR("UTF-8"));
	
	wr_nick = wi_retain(wi_user_name());
	wr_icon = wi_string_init_with_cstring(wi_string_alloc(), wr_default_icon);
}
Esempio n. 16
0
void wr_init_client(void) {
	int			options;
	
	wr_socket_context = wi_socket_context_init(wi_socket_context_alloc());
	
	if(!wi_socket_context_set_ssl_type(wr_socket_context, WI_SOCKET_SSL_CLIENT))
		wi_log_err(WI_STR("Could not set SSL context: %m"));
	
	options = WI_STRING_ENCODING_IGNORE | WI_STRING_ENCODING_TRANSLITERATE;
	
	wr_client_string_encoding = wi_string_encoding_init_with_charset(wi_string_encoding_alloc(),
																	 WI_STR("ISO-8859-1"),
																	 options);
	
	wr_server_string_encoding = wi_string_encoding_init_with_charset(wi_string_encoding_alloc(),
																	 WI_STR("UTF-8"),
																	 options);
	
	wr_icon = wi_string_init_with_cstring(wi_string_alloc(), wr_default_icon);
}
Esempio n. 17
0
void wd_version_init(void) {
	wd_version_string			= wi_string_init_with_format(wi_string_alloc(), WI_STR("%s (%u)"), WD_VERSION, WI_REVISION);
	wd_protocol_version_string	= wi_string_init_with_cstring(wi_string_alloc(), WD_PROTOCOL_VERSION);

#ifdef HAVE_CORESERVICES_CORESERVICES_H
	wd_server_version_string	= wi_string_init_with_format(wi_string_alloc(), WI_STR("Wired/%@ (%@; %@; %@) (%s; CoreFoundation %.1f)"),
		wd_version_string,
		wi_process_os_name(wi_process()),
		wi_process_os_release(wi_process()),
		wi_process_os_arch(wi_process()),
		SSLeay_version(SSLEAY_VERSION),
		kCFCoreFoundationVersionNumber);
#else
	wd_server_version_string	= wi_string_init_with_format(wi_string_alloc(), WI_STR("Wired/%@ (%@; %@; %@) (%s)"),
		wd_version_string,
		wi_process_os_name(wi_process()),
		wi_process_os_release(wi_process()),
		wi_process_os_arch(wi_process()),
		SSLeay_version(SSLEAY_VERSION));
#endif
}
Esempio n. 18
0
wt_server_t * wt_server_init_with_packed(wt_server_t *server, wt_server_packed_t server_packed) {
	server->key				= wi_string_init_with_cstring(wi_string_alloc(), server_packed.key);
	server->update_time		= server_packed.update_time;
	server->register_time	= server_packed.register_time;

	server->ip				= wi_string_init_with_cstring(wi_string_alloc(), server_packed.ip);
	server->port			= server_packed.port;

	server->category		= wi_string_init_with_cstring(wi_string_alloc(), server_packed.category);
	server->url				= wi_string_init_with_cstring(wi_string_alloc(), server_packed.url);
	server->name			= wi_string_init_with_cstring(wi_string_alloc(), server_packed.name);
	server->users			= server_packed.users;
	server->bandwidth		= server_packed.bandwidth;
	server->guest			= server_packed.guest;
	server->download		= server_packed.download;
	server->files			= server_packed.files;
	server->size			= server_packed.size;
	server->description		= wi_string_init_with_cstring(wi_string_alloc(), server_packed.description);
	
	return server;
}
Esempio n. 19
0
wi_string_t * wi_string_with_cstring(const char *cstring) {
	return wi_autorelease(wi_string_init_with_cstring(wi_string_alloc(), cstring));
}
Esempio n. 20
0
wi_array_t * wi_array_init_with_argument_string(wi_array_t *array, wi_string_t *string, wi_integer_t index) {
	wi_string_t		*data;
	const char		*cstring;
	char			*buffer, *end;
	wi_uinteger_t	count;
	wi_boolean_t	squote, dquote, bsquote;
	
	array		= wi_array_init_with_capacity(array, 0);
	cstring		= wi_string_cstring(string);
	buffer		= wi_malloc(strlen(cstring) + 1);
	count		= 0;

	squote = dquote = bsquote = false;

	while(*cstring) {
		if(index < 0 || (index >= 0 && count != (wi_uinteger_t) index)) {
			while(isspace(*cstring))
				cstring++;
		}
		
		end = buffer;
		
		while(*cstring) {
			if(index >= 0 && count == (wi_uinteger_t) index) {
				*end++ = *cstring++;
				
				continue;
			}

			if(isspace(*cstring) && !squote && !dquote && !bsquote)
				break;
			
			if(bsquote) {
				bsquote = false;
				*end++ = *cstring;
			}
			else if(squote) {
				if(*cstring == '\'')
					squote = false;
				else
					*end++ = *cstring;
			}
			else if(dquote) {
				if(*cstring == '"')
					dquote = false;
				else
					*end++ = *cstring;
			}
			else {
				if(*cstring == '\'')
					squote = true;
				else if(*cstring == '"')
					dquote = true;
				else if(*cstring == '\\')
					bsquote = true;
				else
					*end++ = *cstring;
			}
			
			cstring++;
		}
		
		*end = '\0';
		
		data = wi_string_init_with_cstring(wi_string_alloc(), buffer);
		wi_array_add_data(array, data);
		wi_release(data);
		
		count++;
		
		while(isspace(*cstring))
			cstring++;
	}
	
	wi_free(buffer);
	
	return array;
}
Esempio n. 21
0
int main(int argc, const char **argv) {
	wi_mutable_array_t		*arguments;
	wi_pool_t				*pool;
	wi_string_t				*string, *root_path;
	int						ch, facility;
	wi_boolean_t			test_config, daemonize, change_directory, switch_user;

	/* init libwired */
	wi_initialize();
	wi_load(argc, argv);
	
	pool					= wi_pool_init(wi_pool_alloc());
	wi_log_syslog			= true;
	wi_log_syslog_facility	= LOG_DAEMON;

	/* init core systems */
	wt_version_init();
	wt_status_lock			= wi_lock_init(wi_lock_alloc());
	wt_start_date			= wi_date_init(wi_date_alloc());
	
	/* set defaults */
	root_path				= WI_STR(WT_ROOT);
	wi_settings_config_path	= wi_string_init_with_cstring(wi_string_alloc(), WT_CONFIG_PATH);
	test_config				= false;
	daemonize				= true;
	change_directory		= true;
	switch_user				= true;

	/* init reexec argument list */
	arguments				= wi_array_init(wi_mutable_array_alloc());

	/* parse command line switches */
	while((ch = getopt(argc, (char * const *) argv, "46Dd:f:hi:L:ls:tuVvXx")) != -1) {
		switch(ch) {
			case '4':
				wt_address_family = WI_ADDRESS_IPV4;
				break;

			case '6':
				wt_address_family = WI_ADDRESS_IPV6;
				break;

			case 'D':
				daemonize = false;
				wi_log_stderr = true;
				break;

			case 'd':
				root_path = wi_string_with_cstring(optarg);
				break;

			case 'f':
				wi_release(wi_settings_config_path);
				wi_settings_config_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'i':
				wi_log_limit = wi_string_uint32(wi_string_with_cstring(optarg));
				break;

			case 'L':
				wi_log_syslog = false;
				wi_log_file = true;

				wi_release(wi_log_path);
				wi_log_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'l':
				wi_log_level++;
				break;

			case 's':
				string = wi_string_with_cstring(optarg);
				facility = wi_log_syslog_facility_with_name(string);
				
				if(facility < 0)
					wi_log_fatal(WI_STR("Could not find syslog facility \"%@\": %m"), string);
				
				wi_log_syslog_facility = facility;
				break;

			case 't':
				test_config = true;
				break;

			case 'u':
				break;

			case 'V':
			case 'v':
				wt_version();
				break;
				
			case 'X':
				daemonize = false;
				break;
				
			case 'x':
				daemonize = false;
				change_directory = false;
				switch_user = false;
				break;
			
			case '?':
			case 'h':
			default:
				wt_usage();
				break;
		}
		
		wi_mutable_array_add_data(arguments, wi_string_with_format(WI_STR("-%c"), ch));
		
		if(optarg)
			wi_mutable_array_add_data(arguments, wi_string_with_cstring(optarg));
	}
	
	/* detach */
	if(daemonize) {
		wi_mutable_array_add_data(arguments, WI_STR("-X"));
		
		switch(wi_fork()) {
			case -1:
				wi_log_fatal(WI_STR("Could not fork: %m"));
				break;
				
			case 0:
				if(!wi_execv(wi_string_with_cstring(argv[0]), arguments))
					wi_log_fatal(WI_STR("Could not execute %s: %m"), argv[0]);
				break;
				
				default:
				_exit(0);
				break;
		}
	}
	
	wi_release(arguments);
	
	/* change directory */
	if(change_directory) {
		if(!wi_fs_change_directory(root_path))
			wi_log_error(WI_STR("Could not change directory to %@: %m"), root_path);
	}
	
	/* open log */
	wi_log_open();

	/* init subsystems */
	wt_ssl_init();
	wt_clients_init();
	wt_servers_init();

	/* read the config file */
	wt_settings_init();

	if(!wt_settings_read_config())
		exit(1);

	/* apply settings */
	wt_settings_apply_settings();

	if(test_config) {
		printf("Config OK\n");

		exit(0);
	}

	/* dump command line */
	wi_log_info(WI_STR("Started as %@ %@"),
		wi_process_path(wi_process()),
		wi_array_components_joined_by_string(wi_process_arguments(wi_process()), WI_STR(" ")));

	/* init tracker */
	wi_log_info(WI_STR("Starting Wired Tracker version %@"), wt_version_string);
	wt_tracker_init();

	/* switch user/group */
	if(switch_user)
		wi_switch_user(wt_settings.user, wt_settings.group);
		
	/* create tracker threads after privilege drop */
	wt_signals_init();
	wt_block_signals();
	wt_servers_schedule();
	wt_tracker_create_threads();
	wt_write_pid();
	wt_write_status(true);
	
	/* clean up pool after startup */
	wi_pool_drain(pool);
	
	/* enter the signal handling thread in the main thread */
	wt_signal_thread(NULL);

	/* dropped out */
	wt_cleanup();
	wi_log_close();
	wi_release(pool);

	return 0;
}
Esempio n. 22
0
int main(int argc, const char **argv) {
	wi_pool_t			*pool;
	wi_string_t			*string;
	int					ch, facility;
	wi_boolean_t		no_chroot, test_config;

	/* init libwired */
	wi_initialize();
	wi_load(argc, argv);
	
	pool					= wi_pool_init(wi_pool_alloc());
	wi_log_startup			= true;
	wi_log_syslog			= true;
	wi_log_syslog_facility	= LOG_DAEMON;

	/* init core systems */
	wt_init_version();
	wt_status_lock			= wi_lock_init(wi_lock_alloc());
	wt_start_date			= wi_date_init(wi_date_alloc());
	
	/* set defaults */
	wi_root_path			= wi_string_init_with_cstring(wi_string_alloc(), WT_ROOT);
	wi_settings_config_path	= wi_string_init_with_cstring(wi_string_alloc(), WT_CONFIG_PATH);
	no_chroot				= false;
	test_config				= false;

	/* parse command line switches */
	while((ch = getopt(argc, (char * const *) argv, "46Dd:f:hi:L:ls:tuVv")) != -1) {
		switch(ch) {
			case '4':
				wt_address_family = WI_ADDRESS_IPV4;
				break;

			case '6':
				wt_address_family = WI_ADDRESS_IPV6;
				break;

			case 'D':
				wt_daemonize = false;
				wi_log_stderr = true;
				break;

			case 'd':
				wi_release(wi_root_path);
				wi_root_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'f':
				wi_release(wi_settings_config_path);
				wi_settings_config_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'i':
				wi_log_limit = wi_string_uint32(wi_string_with_cstring(optarg));
				break;

			case 'L':
				wi_log_syslog = false;
				wi_log_file = true;

				wi_release(wi_log_path);
				wi_log_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'l':
				wi_log_level++;
				break;

			case 's':
				string = wi_string_with_cstring(optarg);
				facility = wi_log_syslog_facility_with_name(string);
				
				if(facility < 0) {
					wi_log_err(WI_STR("Could not find syslog facility \"%@\": %m"),
						string);
				}
				
				wi_log_syslog_facility = facility;
				break;

			case 't':
				test_config = true;
				break;

			case 'u':
				no_chroot = true;
				break;

			case 'V':
			case 'v':
				wt_version();
				break;

			case '?':
			case 'h':
			default:
				wt_usage();
				break;
		}
	}
	
	/* open log */
	wi_log_open();

	/* init subsystems */
	wt_init_ssl();
	wt_init_clients();
	wt_init_servers();

	/* read the config file */
	wt_settings_chroot = !no_chroot;
	wt_init_settings();

	if(!wt_read_config())
		exit(1);

	/* change root directory */
	if(!no_chroot) {
		if(!wi_change_root())
			wi_log_err(WI_STR("Could not change root to %@: %m"), wi_root_path);
	}

	/* apply config */
	wt_apply_config();

	if(test_config) {
		printf("Config OK\n");

		exit(0);
	}

	/* dump command line */
	if(wi_log_level >= WI_LOG_DEBUG) {
		wi_log_debug(WI_STR("Started as %@ %@"),
			wi_process_path(wi_process()),
			wi_array_components_joined_by_string(wi_process_arguments(wi_process()), WI_STR(" ")));
	}

	/* init tracker */
	wi_log_info(WI_STR("Starting Wired Tracker version %@"), wt_version_string);
	wt_init_tracker();

	/* detach (don't chdir, don't close i/o channels) */
	if(wt_daemonize) {
		if(!wi_daemon())
			wi_log_err(WI_STR("Could not become a daemon: %m"));
	}

	/* switch user/group */
	wi_switch_user(wt_settings.user, wt_settings.group);
		
	/* create tracker threads after privilege drop */
	wt_init_signals();
	wt_block_signals();
	wt_schedule_servers();
	wt_fork_tracker();
	wt_write_pid();
	wt_write_status(true);
	wi_log_startup = false;
	
	wi_release(pool);
	pool = wi_pool_init(wi_pool_alloc());
	
	/* enter the signal handling thread in the main thread */
	wt_signal_thread(NULL);

	/* dropped out */
	wt_cleanup();
	wi_log_close();
	wi_release(pool);

	return 0;
}
Esempio n. 23
0
void wi_version_initialize(void) {
	wi_version_string = wi_string_init_with_cstring(wi_string_alloc(), WI_VERSION);
}
Esempio n. 24
0
static wi_runtime_instance_t * _wi_string_copy(wi_runtime_instance_t *instance) {
	wi_string_t		*string = instance;
	
	return wi_string_init_with_cstring(wi_string_alloc(), string->string);
}