Ejemplo n.º 1
0
static int test_initfree(void)
{
   module_t       mod    = module_FREE;
   const char *   name[] = { "testmodule", "testmodule_Debug" };
   directory_t *  dir;

   // prepare
   TEST(0 == new_directory(&dir, module_DIRECTORY, 0));

   // TEST module_FREE
   TEST(0 == mod.page_addr);
   TEST(0 == mod.page_size);
   TEST(0 == mod.code_size);

   for (unsigned i = 0; i < lengthof(name); ++i) {
      off_t code_size = 0;
      TEST(0 == filesize_directory(dir, name[i], &code_size));
      const size_t pagesizeMinus1 = (pagesize_vm()-1);
      size_t aligned_size = ((size_t) code_size + pagesizeMinus1) & ~pagesizeMinus1;

      // TEST init_module
      TEST(0 == init_module(&mod, name[i]));
      TEST(mod.page_addr != 0);
      TEST(mod.page_size == aligned_size);
      TEST(mod.code_size == castPoff_size(code_size));
      TEST(1 == ismapped_vm(cast_vmpage(&mod, page_), accessmode_RDEX));

      // TEST free_module
      vmpage_t vmpage = *cast_vmpage(&mod, page_);
      TEST(0 == free_module(&mod));
      TEST(0 == mod.page_addr);
      TEST(0 == mod.page_size);
      TEST(0 == mod.code_size);
      TEST(1 == isunmapped_vm(&vmpage));
      TEST(0 == free_module(&mod));
      TEST(0 == mod.page_addr);
      TEST(0 == mod.page_size);
      TEST(0 == mod.code_size);
      TEST(1 == isunmapped_vm(&vmpage));
   }

   // unprepare
   TEST(0 == delete_directory(&dir));

   return 0;
ONERR:
   delete_directory(&dir);
   free_module(&mod);
   return EINVAL;
}
Ejemplo n.º 2
0
M2_arrayint gbres_comp::betti_minimal() const
    // Negative numbers represent upper bounds
{
  int lev, i, d;
  int lo = nodes[0]->output_free_module()->lowest_primary_degree();
  if (n_nodes >= 2)
    {
      int lo1 = nodes[1]->output_free_module()->lowest_primary_degree()-1;
      if (lo1 < lo) lo=lo1;
    }
  if (n_nodes >= 3)
    {
      int lo2 = nodes[2]->output_free_module()->lowest_primary_degree()-2;
      if (lo2 < lo) lo=lo2;
    }
  int hi = lo;
  int len = 1;

  // Set the hi degree, and len
  for (lev=0; lev<n_nodes; lev++)
    {
      const FreeModule *F = free_module(lev);
      if (F->rank() > 0)
        len = lev;

      for (i=0; i<F->rank(); i++)
        {
          d = F->primary_degree(i) - lev;
          if (d > hi) hi = d;
        }
    }

  int *bettis;
  betti_init(lo,hi,len,bettis);

  for (lev=0; lev<=len; lev++)
    {
      const FreeModule *F = free_module(lev);

      for (i=0; i<F->rank(); i++)
        {
          d = F->primary_degree(i) - lev - lo;
          bettis[lev+(len+1)*d]++;
        }
    }
  M2_arrayint result = betti_make(lo,hi,len,bettis);
  deletearray(bettis);
  return result;
}
Ejemplo n.º 3
0
void obs_shutdown(void)
{
	size_t i;

	if (!obs)
		return;

	da_free(obs->input_types);
	da_free(obs->filter_types);
	da_free(obs->transition_types);
	da_free(obs->output_types);
	da_free(obs->service_types);

	obs_free_data();
	obs_free_video();
	obs_free_graphics();
	obs_free_audio();
	media_close(obs->media);
	proc_handler_destroy(obs->procs);
	signal_handler_destroy(obs->signals);

	for (i = 0; i < obs->modules.num; i++)
		free_module(obs->modules.array+i);
	da_free(obs->modules);

	bfree(obs);
	obs = NULL;
}
Ejemplo n.º 4
0
void obs_shutdown(void)
{
	if (!obs)
		return;

	da_free(obs->input_types);
	da_free(obs->filter_types);
	da_free(obs->encoder_types);
	da_free(obs->transition_types);
	da_free(obs->output_types);
	da_free(obs->service_types);
	da_free(obs->modal_ui_callbacks);
	da_free(obs->modeless_ui_callbacks);

	stop_video();

	obs_free_data();
	obs_free_video();
	obs_free_graphics();
	obs_free_audio();
	proc_handler_destroy(obs->procs);
	signal_handler_destroy(obs->signals);

	for (size_t i = 0; i < obs->modules.num; i++)
		free_module(obs->modules.array+i);
	da_free(obs->modules);

	bfree(obs->locale);
	bfree(obs);
	obs = NULL;
}
Ejemplo n.º 5
0
owl_term owl_code_load(vm_t *vm, owl_term owl_filename) {
  char *filename = owl_extract_ptr(owl_filename);
  compiled_module_t *compiled = compile_file_to_memory(filename);
  owl_term functions = owl_load_module(vm, compiled->bytecode, compiled->size);
  free_module(compiled);
  return functions;
}
Ejemplo n.º 6
0
static int test_exec(void)
{
   module_t       mod    = module_FREE;
   const char *   name[] = { "testmodule", "testmodule_Debug" };


   for (unsigned i = 0; i < lengthof(name); ++i) {
      // prepare
      TEST(0 == init_module(&mod, name[i]));

      // TEST module_main: execute
      typedef int (*module_main_f) (testmodule_functable_t * functable, threadcontext_t * tcontext);

      module_main_f           mmain = (module_main_f) (uintptr_t) codeaddr_module(&mod);
      testmodule_functable_t  table = { 0, 0, 0 };

      TEST(0 == mmain(&table, tcontext_maincontext()));
      TEST(codeaddr_module(&mod) < (uint8_t*)(uintptr_t)table.add && (uint8_t*)(uintptr_t)table.add < codeaddr_module(&mod) + codesize_module(&mod));
      TEST(codeaddr_module(&mod) < (uint8_t*)(uintptr_t)table.sub && (uint8_t*)(uintptr_t)table.sub < codeaddr_module(&mod) + codesize_module(&mod));
      TEST(codeaddr_module(&mod) < (uint8_t*)(uintptr_t)table.mult && (uint8_t*)(uintptr_t)table.mult < codeaddr_module(&mod) + codesize_module(&mod));

      // TEST testmodule_functable_t: execute functions
      TEST(5 == table.add(3,2));
      TEST(6 == table.add(3,3));
      TEST(0 == table.add(3,-3));
      TEST(9 == table.sub(11,2));
      TEST(-1== table.sub(11,12));
      TEST(1 == table.sub(11,10));
      TEST(8 == table.mult(2,4));
      TEST(6 == table.mult(2,3));
      TEST(9 == table.mult(3,3));

      // unprepare
      TEST(0 == free_module(&mod));
   }

   return 0;
ONERR:
   free_module(&mod);
   return EINVAL;
}
Ejemplo n.º 7
0
int do_cleanup_module(const char __user * name_user)
{
	struct module *mod;
	char name[MODULE_NAME_LEN];
	int ret = 0, forced = 0;

	struct mm_struct *mm = current->mm;
	lock_mm(mm);

	int length = strlen(name_user);
	if (!copy_from_user(mm, name, name_user, length, 1)) {
		unlock_mm(mm);
		return -E_INVAL;
	}
	unlock_mm(mm);
	name[length] = '\0';

	mod = find_module(name);

	if (!mod) {
		ret = -E_NOENT;
		goto out;
		//return -E_NOENT;
	}
	if (!list_empty(&mod->modules_which_use_me)) {
		ret = -E_INVAL;
		goto out;
	}

	if (mod->state != MODULE_STATE_LIVE) {
		kprintf("do_cleanup_module: %s already dying\n", mod->name);
		ret = -E_BUSY;
	}

	if (mod->init && !mod->exit) {
		kprintf("do_cleanup_module: %s can't be removed\n", mod->name);
		ret = -E_BUSY;
	}

	mod->waiter = current;
	mod->state = MODULE_STATE_GOING;

	if (mod->exit != NULL)
		mod->exit();

	strncpy(last_unloaded_module, mod->name, strlen(mod->name));
	free_module(mod);
	return ret;

out:
	return ret;

}
Ejemplo n.º 8
0
int do_init_module(void __user * umod, unsigned long len,
		   const char __user * uargs)
{
	struct module *mod;
	int ret = 0;

	// TODO: non-preemptive kernel does not need to lock module mutex

	mod = load_module(umod, len, uargs);
	if (mod == NULL) {
		// TODO: non-preemptive kernel does not need to unlock module mutex
		return -1;
	}
	// TODO: non-preemptive kernel does not need to unlock module mutex
	struct module test_module;
	kprintf("Module size: %d\n", sizeof(test_module));
  kprintf("list: %d\n", (int)&(test_module.list) - (int)&(test_module));
  kprintf("name: %d\n", (int)&(test_module.name) - (int)&(test_module));
  kprintf("kernel_symbol: %d\n", (int)&(test_module.syms) - (int)&(test_module));
  kprintf("num_syms: %d\n",  (int)&(test_module.num_syms) - (int)&(test_module));
  kprintf("init: %d\n", (int)&(test_module.init) - (int)&(test_module));
  kprintf("module_init: %d\n", (int)&(test_module.module_init) - (int)&(test_module));
  kprintf("module_core: %d\n", (int)&(test_module.module_core) - (int)&(test_module));
  kprintf("symtab: %d\n", (int)&(test_module.symtab) - (int)&(test_module));
  kprintf("percpu: %d\n", (int)&(test_module.percpu) - (int)&(test_module));
  kprintf("exit: %d\n", (int)&(test_module.exit) - (int)&(test_module));
	if (mod->init != NULL) {
		ret = (*mod->init) ();
	}
	if (ret < 0) {
		mod->state = MODULE_STATE_GOING;
		// TODO: non-preemptive kernel does not need to lock module mutex
		free_module(mod);
		// TODO: non-preemptive kernel does not need to unlock
		return ret;
	}
	if (ret > 0) {
		kprintf("%s: %s->init suspiciously returned %d\n"
			"%s: loading anyway...\n", __func__, mod->name, ret,
			__func__);
	}
	mod->state = MODULE_STATE_LIVE;

	// TODO: lock?
	module_free(mod, mod->module_init);
	mod->module_init = NULL;
	mod->init_size = 0;
	mod->init_text_size = 0;
	// TODO: unlock

	return 0;
}
Ejemplo n.º 9
0
void module_unload_all(irc_connection *con)
{
	module_listitem *m = con->modules;

	while (m) {
		module_listitem *next = m->next;
		printf("Unloading module %15s\n", m->name);
		free_module(m);
		m = next;
	}

	con->modules = NULL;
}
Ejemplo n.º 10
0
void
delete_module(struct carousel *car, uint32_t num)
{
	uint32_t size;

	free_module(&car->modules[num]);

	size = ((car->nmodules - 1) - num) * sizeof(struct module);
	if(size > 0)
		memmove(&car->modules[num], &car->modules[num + 1], size);

	car->nmodules --;

	return;
}
Ejemplo n.º 11
0
void obs_shutdown(void)
{
	struct obs_module *module;

	if (!obs)
		return;

	da_free(obs->input_types);
	da_free(obs->filter_types);
	da_free(obs->encoder_types);
	da_free(obs->transition_types);
	da_free(obs->output_types);
	da_free(obs->service_types);
	da_free(obs->modal_ui_callbacks);
	da_free(obs->modeless_ui_callbacks);

	stop_video();

	obs_free_data();
	obs_free_video();
	obs_free_graphics();
	obs_free_audio();
	proc_handler_destroy(obs->procs);
	signal_handler_destroy(obs->signals);

	module = obs->first_module;
	while (module) {
		struct obs_module *next = module->next;
		free_module(module);
		module = next;
	}
	obs->first_module = NULL;

	for (size_t i = 0; i < obs->module_paths.num; i++)
		free_module_path(obs->module_paths.array+i);
	da_free(obs->module_paths);

	bfree(obs->locale);
	bfree(obs);
	obs = NULL;
}
Ejemplo n.º 12
0
int module_unload(irc_connection *con, const char *module_name)
{
	int ret = 0;

	module_listitem *l = con->modules;
	module_listitem *l2;

	l = module_by_name(con, module_name, &l2);

	if (l == NULL) return -9; // Not found!

	// Remove item from list
	if (l2 == NULL) con->modules = l->next;
	else 		l2->next = l->next;

	free_module(l);

	con->module_count--;

	return ret;
}
Ejemplo n.º 13
0
module_t * init_image_tivo() {


	module_t * module;


	if(!tystream->index) {
		return(0);
	}

	module = (module_t *)malloc(sizeof(module_t));
	module->data_buffer = NULL;
	module->buffer_size = 0;

	if(!get_first_seq_tivo(tystream->index->gop_index, module)) {
		free_module(module);
		return(0);
	}

	return(module);

}
Ejemplo n.º 14
0
int main(int argc, char **argv)
{
	struct string_list *filelist = NULL;
	struct symbol_list *builtins;
	char *file;

	builtins = sparse_initialize(argc, argv, &filelist);
	FOR_EACH_PTR_NOTAG(filelist, file) {
		struct symbol_list *syms;
		module_t m;

		syms = sparse(file);
		if (die_if_error)
			return 1;
		m = alloc_module(file);
		compile(m, builtins);
		compile(m, syms);
		verify_module(m);
		print_module(m, STDOUT_FILENO);
		free_module(m);
	} END_FOR_EACH_PTR_NOTAG(file);
	return 0;
}
Ejemplo n.º 15
0
// ------------------------------------------------------------------------------------------------------------------ //
int main (int argc, const char * argv[])
{
	if (argc < 2)
		return -1;

	const char * module_name = argv[1];
	printf("loading module '%s',", module_name);
	module_t * module = load_module(module_name);
	if (!module)
		return -1;

	const module_t::header_t& header = module->get_header();
	printf(" type: '%c%c%c%c', version: %d, size: %d, count: %d crc32: 0x%08x\n\n",
		   header.m_magic&255, (header.m_magic>>8)&255, (header.m_magic>>16)&255, (header.m_magic>>24)&255,
		   header.m_version,
		   header.m_size,
		   header.m_count,
		   header.m_crc32);

	assert(header.m_magic == get_magic_code());
	assert(header.m_version == 1);
	//assert(header.m_size > sizeof(header_t));

	module->debug_dump();
	module->test1();
	module->test2();

	// verify test entries
	// int32 * pI = (int32*)buffer;
	// assert(*pI == 24);

	// stb_uint64 * pU64 = (stb_uint64*)(buffer + 4);
	// assert(*pU64 == 0xffffffffffffffff);

	free_module(module);
	return 0;
}
Ejemplo n.º 16
0
 const FreeModule /* or null */ *get_free(int level) {
     return free_module(level);
 }
Ejemplo n.º 17
0
int main(int argc, char *argv[], char **envp)
{
	QueryHandle *res;
	time_t tt;
	GLOBAL *g;
	INSTANCE *instances;
	int fval = 0, i = 0, reload = 0;
	char *inst, *instance; 
	FILE *pidf;
	openlog(PROGNAME, 0, LOG_INFO | LOG_CRIT | LOG_ERR);
	syslog(LOG_INFO, "LMS Daemon started.");

        // initialize global structure
        g = (GLOBAL *) realloc(NULL, sizeof(GLOBAL));
        g->api_version = APIVERSION;
        g->db = (DB *) realloc(NULL, sizeof(DB));
        g->db->conn = NULL;

	// initialize proces name change 
	init_set_proc_title(argc, argv, envp);

        // configuration load sequence - check if LMSini is set
        configfile = ( getenv("LMSINI") ? getenv("LMSINI") : "/etc/lms/lms.ini" );

    	// read environment and command line
	driver = ( getenv("LMSDBTYPE") ? getenv("DBTYPE") : strdup("mysql") );
	passwd = ( getenv("LMSDBPASS") ? getenv("LMSDBPASS") : "" );
	dbname = ( getenv("LMSDBNAME") ? getenv("LMSDBNAME") : "lms" );
	user = ( getenv("LMSDBUSER") ? getenv("LMSDBUSER") : "lms" );
	port = ( getenv("LMSDBPORT") ? atoi(getenv("LMSDBPORT")) : 0 );
	if( getenv("LMSDBHOST") ) strcpy(host, getenv("LMSDBHOST")); else strcpy(host, "localhost");
	gethostname(dhost, 255);

	// date/time localization according to environement settings
	setlocale(LC_TIME, "");
	
	// command line arguments
	parse_command_line(argc, argv);

        // load configuration file if exist (if not it will use default parameters - only database section)
        ini = config_load(configfile, g->db, dhost, "database");
        // assign variables
        driver = config_getstring(ini, "database", "type", driver);
        passwd = config_getstring(ini, "database", "password", passwd);
        dbname = config_getstring(ini, "database", "database", dbname);
        user = config_getstring(ini, "database", "user", user);
        port = config_getint(ini, "database", "port", port);
        strcpy(host, config_getstring(ini, "database", "host", host));

	// change process name (hide command line args)
	set_proc_title(PROGNAME);

        str_replace(&driver, "postgres", "pgsql"); // postgres in ini file is pgsql
        str_replace(&driver, "mysqli", "mysql");   // mysqli in ini file is mysql

        char dbdrv_path[strlen(LMS_LIB_DIR) + strlen(driver) + 4];
        sprintf(dbdrv_path, LMS_LIB_DIR "/%s.so", driver);

        if( !file_exists(dbdrv_path))
        {
                syslog(LOG_CRIT, "Database driver '%s' does not exist. Could not find '%s'.", driver, dbdrv_path);
                fprintf(stderr, "Database driver '%s' does not exist. Could not find '%s'.\n", driver, dbdrv_path);
                exit(1);
        }

        void    *dbdrv;
        dbdrv = dlopen(dbdrv_path, RTLD_NOW);

        if( !dbdrv )
        {
                char * errMsg = dlerror();
                syslog(LOG_CRIT, "Unable to load database driver '%s': %s", dbdrv_path, errMsg);
                fprintf(stderr, "Unable to load database driver '%s': %s.\n", dbdrv_path, errMsg);
                exit(1);
        }
        else
        {
                syslog(LOG_INFO, "Database driver '%s' loaded.", driver);
        }

	g->db->connect = dlsym(dbdrv, "db_connect");
	g->db->disconnect = dlsym(dbdrv, "db_disconnect");
   	g->db->query = dlsym(dbdrv, "db_query");
	g->db->pquery = dlsym(dbdrv, "db_pquery");
    	g->db->exec = dlsym(dbdrv, "db_exec");
	g->db->pexec = dlsym(dbdrv, "db_pexec");
	g->db->last_insert_id = dlsym(dbdrv, "db_last_insert_id");
	g->db->free = dlsym(dbdrv, "db_free");
    	g->db->begin = dlsym(dbdrv, "db_begin");
    	g->db->commit = dlsym(dbdrv, "db_commit");
	g->db->abort = dlsym(dbdrv, "db_abort");
    	g->db->get_data = dlsym(dbdrv, "db_get_data");
	g->db->nrows = dlsym(dbdrv, "db_nrows");
	g->db->ncols = dlsym(dbdrv, "db_ncols");
	g->db->concat = dlsym(dbdrv, "db_concat");
	g->db->escape = dlsym(dbdrv, "db_escape");
	g->db->colname = dlsym(dbdrv, "db_colname");

        // test database connection
        if( !(g->db->conn = g->db->connect(dbname,user,passwd,host,port,ssl)) )
        {
                fprintf(stderr, "CRITICAL: Could not connect to database. See logs for details.\n");
                termination_handler(1);
        }
        res = g->db->pquery(g->db->conn, "SELECT count(*) FROM dbinfo");
        if( ! g->db->nrows(res) )
        {
                fprintf(stderr, "CRITICAL: Could not query database. See logs for details.\n");
                termination_handler(1);
        }
        g->db->free(&res);
        g->db->disconnect(g->db->conn);


    	g->str_replace = &str_replace;
    	g->str_save = &str_save;
    	g->str_concat = &str_concat;
	g->str_lwc = &str_lwc;
	g->str_upc = &str_upc;
	g->va_list_join = &va_list_join;

    	g->config_getstring = &config_getstring;
	g->config_getint = &config_getint;
	g->config_getbool = &config_getbool;
	g->config_getdouble = &config_getdouble;

	// catch SIGCHLD to catch zombies
	sa.sa_handler = sig_child;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	sigaction(SIGCHLD, &sa, &orig);

    	// daemonize
    	if ( !quit && !dontfork )
	{
		fval = fork();
        	switch(fval) 
		{
			case -1:
    	    			fprintf(stderr, "Fork error. Exiting.");
            			termination_handler(1);
        		case 0:
				setsid();
				break;
			default:
#ifdef DEBUG1	
	    			syslog(LOG_INFO, "DEBUG: [lmsd] Daemonize. Forked child %d.", fval);
#endif
				if (pidfile != NULL && (pidf = fopen(pidfile, "w")) != NULL)
				{
				    fprintf(pidf, "%d", fval);
				    fclose(pidf);
				}
            			exit(0); // parent exits
        	}
    	}

    	// termination signals handling
    	signal(SIGINT, termination_handler);
    	signal(SIGTERM, termination_handler);

    	// main loop ****************************************************
    	for(;;)
	{
		int i_no = 0;
		
		if( quit ) 
		{
			reload = 1;
			tt = time(0);
		}
		else // daemon mode
		{
			reload = 0;
			tt = cron_sync_sleep();
		}

		// run shell command, i.e. secure connections tuneling
		if( command!=NULL )
		{
#ifdef DEBUG1
			syslog(LOG_INFO, "DEBUG: [lmsd] Executing command: %s.", command);
#endif
			system(command);
		}

		// try to connect to database
		if( !(g->db->conn = g->db->connect(dbname,user,passwd,host,port,ssl)) )
		{
			if( quit ) termination_handler(1);
			continue;
		}

		if( !reload )
		{
			// check reload order
			res = g->db->pquery(g->db->conn, "SELECT reload FROM hosts WHERE name = '?' AND reload != 0", dhost);
			if( g->db->nrows(res) )
			{
				reload = 1;
			}
			g->db->free(&res);
		}
		
		instances = (INSTANCE *) malloc(sizeof(INSTANCE));
		
		// get instances list even if reload == 0
		// maybe we should do that once before main loop, but in
		// this way we can change configuration without daemon restart
		if( iopt ) // from command line...
		{
			inst = strdup(iopt);
			for( instance=strtok(inst," "); instance!=NULL; instance=strtok(NULL, " ") )
			{
				char *name = strdup(instance);
				str_replace(&name, "\\s", " "); // instance name with spaces
				
				res = g->db->pquery(g->db->conn, "SELECT module, crontab FROM daemoninstances, hosts WHERE hosts.id = hostid AND disabled = 0 AND hosts.name = '?' AND daemoninstances.name = '?'", dhost, name);
				if( g->db->nrows(res) )
				{
					char *crontab = g->db->get_data(res, 0, "crontab");
					if( runall || (reload && !strlen(crontab)) || (!quit && crontab_match(tt, crontab)) )
					{
						instances = (INSTANCE *) realloc(instances, sizeof(INSTANCE)*(i_no+1));
						instances[i_no].name = strdup(name);
						instances[i_no].module = strdup(g->db->get_data(res, 0, "module"));
						instances[i_no].crontab = strdup(crontab);
						i_no++;
					}
				} else {
					syslog(LOG_CRIT, "Host '%s' and/or instance '%s' not found in database!", dhost, name);
					fprintf(stderr, "Host '%s' and/or instance '%s' not found in database!\n", dhost, name);
                                }
				g->db->free(&res);
				free(name);
			}
			free(inst);	
		}		
		else // ... or from database
		{
			res = g->db->pquery(g->db->conn, "SELECT module, crontab, daemoninstances.name AS name FROM daemoninstances, hosts WHERE hosts.id = hostid AND disabled = 0 AND hosts.name = '?' ORDER BY priority", dhost);
			for(i=0; i<g->db->nrows(res); i++)
			{
				char *crontab = g->db->get_data(res, i, "crontab");
				if( runall || (reload && !strlen(crontab)) || (!quit && crontab_match(tt, crontab)) )
				{
					instances = (INSTANCE *) realloc(instances, sizeof(INSTANCE)*(i_no+1));
					instances[i_no].name = strdup(g->db->get_data(res, i, "name"));
					instances[i_no].module = strdup(g->db->get_data(res, i, "module"));
					instances[i_no].crontab = strdup(crontab);
					i_no++;
				}
			}
			g->db->free(&res);
		}
		g->db->disconnect(g->db->conn);

		if( i_no )
		{
			// forking reload - we can do a job for longer than one minute
			if( quit )
				fval = 0; // don't fork in "quit mode"
			else
				fval = fork();
			
			if( fval < 0 ) 
			{
        			syslog(LOG_CRIT, "Fork error. Can't reload.");
				if ( quit ) termination_handler(1);
			}
			else if( fval == 0 ) // child or "quit mode"
			{
				set_proc_title(PROGNAME": reload");

				// restore old handler so we can wait for childs executed by modules
				if( !quit )
					sigaction(SIGCHLD, &orig, NULL);
#ifdef DEBUG1
				syslog(LOG_INFO, "DEBUG: [lmsd] Reloading...");
#endif
				// try to connect to database again
				if( !(g->db->conn = g->db->connect(dbname,user,passwd,host,port,ssl)) )
				{
					if( quit ) 
						termination_handler(1);
					else 
						exit(1);
				}
				
				// write reload timestamp and disable reload order
				if( reload )
					g->db->pexec(g->db->conn, "UPDATE hosts SET lastreload = %NOW%, reload = 0 WHERE name = '?'", dhost);
				
				for(i=0; i<i_no; i++)
				{
					MODULE *m;
					MODULE *mod = (MODULE*) malloc(sizeof(MODULE));
					MODULE * (*init)(GLOBAL *, MODULE *);

					char path[strlen(LMS_LIB_DIR) + strlen(instances[i].module) + 4];
			
					// get instance configuration and members
					mod->ini = config_load(configfile, g->db, dhost, instances[i].name);
					mod->instance = strdup(instances[i].name);
					
					// set path to module if not specified
					// be sure that it has .so extension
					str_replace(&instances[i].module, ".so", "");
					
					if( instances[i].module[0] == '/' )
						sprintf(path, "%s.so", instances[i].module);
					else
						sprintf(path, LMS_LIB_DIR "/%s.so", instances[i].module);
					
					mod->file = strdup(path);

					// try to load module
					mod->dlh = dlopen(mod->file, RTLD_NOW);
					if( !mod->dlh ) 
					{
						syslog(LOG_ERR, "Unable to load module '%s': %s", mod->file, dlerror());
						free_module(mod);
						continue;
					}

					// initialize module
					init = dlsym(mod->dlh, "init");
					if( !init ) 
					{
						syslog(LOG_CRIT, "Unable to find initialization function in module '%s'. Is that file really a lmsd module?", mod->file);
						free_module(mod);
						continue;
					}
				
					if( !(m = init(g, mod)))
					{
						syslog(LOG_CRIT, "Unable to initialize module '%s'. Perhaps there is a version mismatch?", mod->file);
						free_module(mod);
						continue;
					}

					syslog(LOG_INFO, "Running module: %s", instances[i].module);
					// now run module
					m->reload(g, m);
					
					// cleanup
					free_module(m);
				}
				
				g->db->disconnect(g->db->conn);
	
				// exit child (reload) thread
				if( !quit ) 
				{
#ifdef DEBUG1
					syslog(LOG_INFO, "DEBUG: [lmsd] Reload finished. Exiting child.");
#endif
					exit(0);
				}
			}
			else 
				sleep(10); // it's important to sleep parent for some time
			
			for(i=0; i<i_no; i++)
			{ 
				free(instances[i].name);
				free(instances[i].module);
				free(instances[i].crontab);
			}
		}

		if( quit ) termination_handler(0);
		
		free(instances);
		
    	} // end of loop **********************************************
	return 0;
}
Ejemplo n.º 18
0
void pz_modules_init(char *path) 
{
#ifdef IPOD
#define MODULEDIR "/opt/Base:/opt/Emulators:/opt/Media:/opt/Tools:/opt/Zillae"
#else
#define MODULEDIR "modules"
#endif

    /* Used for the progress bar */
    TWindow * sliderwin;
    TWidget  * slider;
    int sliderVal=0;
    int verbosity = pz_get_int_setting( pz_global_config, VERBOSITY );
    #define MAXSLIDERVAL (100)
    #define SETUPSECTIONS (6)

    PzModule *last, *cur;
    int i;
    int modCount=0;

    if (module_head) {
	pz_error (_("modules_init called more than once"));
	return;
    }

    /* set up the screen to show a pretty progress bar */
    sliderwin = ttk_new_window();
    ttk_window_hide_header(sliderwin);
    sliderwin->x = sliderwin->y = 0;
    slider = ttk_new_slider_widget(2, ttk_screen->h/3, ttk_screen->w - 8, 0,
		    MAXSLIDERVAL, &sliderVal, 0);
    slider->x = (sliderwin->w - slider->w)/2;
    ttk_add_widget(sliderwin, slider);

    sliderVal = (MAXSLIDERVAL/SETUPSECTIONS) * 1;
    updateprogress(sliderwin, slider, sliderVal, _("Scanning For Modules"), NULL);
    if (!path)
	path = MODULEDIR;

    path = strtok(strdupa(path), ":");
    while (path) {
	find_modules (path);
	path = strtok(NULL, ":");
    }

    sliderVal = (MAXSLIDERVAL/SETUPSECTIONS) * 2;
    updateprogress(sliderwin, slider, sliderVal, _("Reading Modules"), NULL);
    for (i = 0; i < __pz_builtin_number_of_init_functions; i++) {
	int found = 0;
	const char *name = __pz_builtin_names[i];

	cur = module_head;
	while (cur) {
	    char *p = strrchr (cur->podpath, '/');
	    if (!p) p = cur->podpath;
	    else p++;

	    if (!strncmp (p, name, strlen (name)) && !isalpha (p[strlen (name)])) {
		found = 1;
		break;
	    }
	    cur = cur->next;
	}
	if (!found) {
	    if (module_head) {
		cur = module_head; while (cur->next) cur = cur->next;
		cur->next = calloc (1, sizeof(PzModule));
		cur = cur->next;
	    } else {
		cur = module_head = calloc (1, sizeof(PzModule));
	    }

	    cur->podpath = 0;
	    cur->name = strdup (__pz_builtin_names[i]);
	    cur->init = __pz_builtin_init_functions[i];
	    cur->to_load = -1;
            cur->ordered = 0;
	    cur->next = 0;
	}
    }
    
    if (!module_head) {
        pz_message_title (_("Warning"), _("No modules. podzilla will probably be very boring."));
        return;
    }

    /* Used to initialize the window + progressbar used in loading the modules*/
    cur = module_head;
    while (cur) {
    	modCount++;
	cur = cur->next;
    }

    sliderVal = (MAXSLIDERVAL/SETUPSECTIONS) * 3;
    updateprogress(sliderwin, slider, sliderVal, _("Mounting Modules"), NULL);
    // Mount 'em
    cur = module_head;
    last = 0;
    while (cur) {
        if (cur->podpath && cur->extracted) {
            cur->mountpt = strdup (cur->podpath);
            last = cur;
            cur = cur->next;
        } else if (cur->podpath && mount_pod (cur) == -1) {
	    if (last) last->next = cur->next;
	    else module_head = cur->next;
	    free (cur->podpath);
	    free (cur);
	    cur = last? last->next : module_head;
	} else {
	    last = cur;
	    cur = cur->next;
	}
    }

    sliderVal = (MAXSLIDERVAL/SETUPSECTIONS) * 4;
    updateprogress(sliderwin, slider, sliderVal, _("Scanning Module Info"), NULL);
    // Load the module.inf's
    cur = module_head;
    while (cur) {
	load_modinf (cur);
	cur = cur->next;
    }

    sliderVal = (MAXSLIDERVAL/SETUPSECTIONS) * 5;
    updateprogress(sliderwin, slider, sliderVal, _("Module Dependencies"), NULL);
    // Figure out the dependencies
    cur = module_head;
    last = 0;
    while (cur) {
	if (fix_dependencies (cur, 1, SOFTDEP) == -1 ||
			fix_dependencies(cur, 1, HARDDEP) == -1) {
	    if (last) last->next = cur->next;
	    else module_head = cur->next;
	    free_module (cur);
	    cur = last? last->next : module_head;
	} else {
	    last = cur;
	    cur = cur->next;
	}
    }

    // Check which ones are linked in
    cur = module_head;
    last = 0;
    while (cur) {
	for (i = 0; i < __pz_builtin_number_of_init_functions; i++) {
	    if (!strcmp (__pz_builtin_names[i], cur->name)) {
		cur->init = __pz_builtin_init_functions[i];
		cur->to_load = -1;
                cur->ordered = 0;
	    }
	}
	cur = cur->next;
    }

    // XXX. For now, we use a slow method for deps, and it'll crash
    // for circular deps. davidc__ is working on a 
    // better solution.
    cur = module_head;
    while (cur) {
        add_deps (cur);
        cur = cur->next;
    }

    sliderVal = (MAXSLIDERVAL/SETUPSECTIONS) * 6;
    updateprogress(sliderwin, slider, sliderVal, _("Loading Modules"), NULL);

    struct dep *c = load_order;
    while (c) {
	if (c->mod->to_load > 0) {
	    do_load (c->mod);
	}
	if( verbosity < VERBOSITY_ERRORS && c->mod->longname ) {
	    updateprogress(sliderwin, slider, sliderVal, _("Loading Modules"), 
			    c->mod->longname );
	}
	c = c->next;        
    }
    c = load_order;


    sliderVal = 0;
    updateprogress(sliderwin, slider, sliderVal, _("Initializing Modules"), NULL);

    /* trigger the sliders to switch to init mode, restting the slider */
    sliderVal = 0;

    /* initialize the modules */
    while (c) {
	sliderVal += MAXSLIDERVAL / modCount;
        current_module = c->mod;

	updateprogress(sliderwin, slider, sliderVal, 
			_("Initializing Modules"), 
			(verbosity < 2)?  c->mod->longname : NULL );

        do_init (c->mod);
        c = c->next;
    }

    sliderVal = MAXSLIDERVAL;
    updateprogress(sliderwin, slider, sliderVal, 
		_("Finishing Up..."), NULL);
    // Any modules with unrecoverable errors on loading set mod->to_free.
    // Oblige them.
    cur = module_head;
    last = 0;
    while (cur) {
        if (cur->to_free) {
	    if (last) last->next = cur->next;
	    else module_head = cur->next;
            free_module (cur);
	    cur = last? last->next : module_head;
	} else {
	    last = cur;
	    cur = cur->next;
        }
    }

    ttk_free_window (sliderwin);
}
Ejemplo n.º 19
0
const Matrix *gbres_comp::change_matrix(int level)
{
  if (level <= 0 || level >= n_nodes)
    return Matrix::zero(free_module(level-1), free_module(level));
  return nodes[level]->change_matrix();
}
Ejemplo n.º 20
0
const Matrix *gbres_comp::initial_matrix(int n, int level)
{
  if (level <= 0 || level >= n_nodes)
    return Matrix::zero(free_module(level-1), free_module(level));
  return nodes[level]->initial_matrix(n);
}