Beispiel #1
0
void _critical(const char *const function_name, const char *fmt, ...) {
	if (*quiet)
		return;

	struct timespec abs_time;
	clock_gettime(CLOCK_REALTIME , &abs_time);
	abs_time.tv_sec += 1;

	if (error_mutex_p != NULL)
		pthread_mutex_timedlock(error_mutex_p, &abs_time);

	outputmethod_t method = *outputmethod;

	{
		va_list args;
		pthread_t thread = pthread_self();
		pid_t pid = getpid();

		outfunct[method]("Critical (pid: %u; thread: %p): %s(): ", pid, thread, function_name);
		va_start(args, fmt);
		voutfunct[method](fmt, args);
		va_end(args);
		outfunct[method](" (current errno %i: %s)", errno, strerror(errno));
		flushfunct[method](LOG_CRIT);
	}

#ifdef BACKTRACE_SUPPORT
	{
		void  *buf[BACKTRACE_LENGTH];
		char **strings;
		int backtrace_len = backtrace((void **)buf, BACKTRACE_LENGTH);

		strings = backtrace_symbols(buf, backtrace_len);
		if (strings == NULL) {
			outfunct[method]("_critical(): Got error, but cannot print the backtrace. Current errno: %u: %s\n",
				errno, strerror(errno));
			flushfunct[method](LOG_CRIT);
			pthread_mutex_unlock(error_mutex_p);
			exit(EXIT_FAILURE);
		}

		for (int j = 1; j < backtrace_len; j++) {
			outfunct[method]("        %s", strings[j]);
			flushfunct[method](LOG_CRIT);
		}
	}
#endif

	if (error_mutex_p != NULL)
		pthread_mutex_unlock(error_mutex_p);

	error_deinit();
	exit(errno);

	return;
}
Beispiel #2
0
int main ( int _argc, char *_argv[] )
{
	struct ctx *ctx_p = xcalloc ( 1, sizeof ( *ctx_p ) );
	argv = _argv;
	argc = _argc;
	int ret = 0, nret;
	//SAFE ( posixhacks_init(), errno = ret = _SAFE_rc );
	ctx_p->config_group			 = DEFAULT_CONFIG_GROUP;
	ctx_p->vms_min				 = DEFAULT_VMS_MIN;
	ctx_p->vms_max				 = DEFAULT_VMS_MAX;
	ctx_p->vms_spare_min			 = DEFAULT_VMS_SPARE_MIN;
	ctx_p->vms_spare_max			 = DEFAULT_VMS_SPARE_MAX;
	strncpy ( ctx_p->listen_addr, strdup ( DEFAULT_LISTEN ), 256 );
	ctx_p->flags[KILL_ON_DISCONNECT]	 = DEFAULT_KILL_ON_DISCONNECT;
	ncpus					 = sysconf ( _SC_NPROCESSORS_ONLN ); // Get number of available logical CPUs
	memory_init();
	ctx_p->pid				 = getpid();
	int quiet = 0, verbose = 3;
	error_init ( &ctx_p->flags[OUTPUT_METHOD], &quiet, &verbose, &ctx_p->flags[DEBUG] );
	nret = arguments_parse ( argc, argv, ctx_p );

	if ( nret ) ret = nret;

	if ( !ret ) {
		nret = configs_parse ( ctx_p, PS_CONFIG );

		if ( nret ) ret = nret;
	}

	if ( !ctx_p->kvm_args[SHARGS_PRIMARY].c ) {
		char *args_line = strdup ( DEFAULT_KVM_ARGS );
		parse_parameter ( ctx_p, KVM_ARGS, args_line, PS_DEFAULTS );
	}

	debug ( 4, "ncpus == %u", ncpus );
	debug ( 4, "debugging flags: %u 0 3 %u", ctx_p->flags[OUTPUT_METHOD], ctx_p->flags[DEBUG] );
	{
		int n = 0;

		while ( n < SHARGS_MAX ) {
			kvm_args_t *args_p = &ctx_p->kvm_args[n++];
			debug ( 9, "Custom arguments %u count: %u", n - 1, args_p->c );
			int i = 0;

			while ( i < args_p->c ) {
				int macros_count = -1, expanded = -1;
				args_p->v[i] = parameter_expand ( ctx_p, args_p->v[i], 4, &macros_count, &expanded, parameter_get, ctx_p );
				debug ( 12, "args_p->v[%u] == \"%s\" (t: %u; e: %u)", i, args_p->v[i], macros_count, expanded );

				if ( macros_count == expanded )
					args_p->isexpanded[i]++;

				i++;
			}
		}
	}
	ctx_p->state = STATE_STARTING;
	nret = main_rehash ( ctx_p );

	if ( nret )
		ret = nret;

	{
		int rc = ctx_check ( ctx_p );

		if ( !ret ) ret = rc;
	}
	debug ( 3, "Current errno is %i.", ret );

	// == RUNNING ==
	if ( ret == 0 )
		ret = kvmpool ( ctx_p );

	// == /RUNNING ==
	main_cleanup ( ctx_p );
	error_deinit();
	ctx_cleanup ( ctx_p );
	debug ( 1, "finished, exitcode: %i: %s.", ret, strerror ( ret ) );
	free ( ctx_p );
#ifndef __FreeBSD__	// Hanging up with 100%CPU eating, https://github.com/xaionaro/clsync/issues/97
	//SAFE ( posixhacks_deinit(), errno = ret = _SAFE_rc );
#endif
	return ret;
}