void settings_free() { for(int i = 0;i < sizeof(struct_to_fields)/sizeof(struct_to_field);i++) { const struct_to_field *s2f = &struct_to_fields[i]; settings_free_strings(s2f->_struct, s2f->fields, s2f->num_fields); } conf_close(); }
/* * test main for conf module, usage: a.out conffile */ int main(int argc, char *argv[]) { struct opts *opts; err_init(argv[0]); setbuf(stdout, NULL); opts_init(Opttable, Opttable_cnt); opts = opts_parse(NULL, NULL, 0); if (argc != 2) err(EF_RAW, "usage: %s conffile\n", argv[0]); conf_open(argv[1], argv[1], opts); printf("conffile <%s>:\n", argv[1]); conf_print(stdout, NULL); conf_close(opts); err_done(0); /* NOTREACHED */ return (0); }
/* * test main for conf module, usage: a.out conffile */ int main(int argc, char *argv[]) { err_init(argv[0]); setbuf(stdout, NULL); if (argc != 2) err(EF_RAW, "usage: %s conffile\n", argv[0]); conf_open(argv[1], 1); printf("conffile <%s>:\n", argv[1]); conf_print(stdout); conf_close(opts_parse(NULL, 0)); err_done(0); /* NOTREACHED */ return (0); }
int main(int argc, char *argv[]) { bool prefer_ipv6 = false, run_daemon = false, test = false; const char *ua_eprm = NULL; const char *exec = NULL; const char *modv[16]; size_t modc = 0; int err; (void)re_fprintf(stderr, "baresip v%s" " Copyright (C) 2010 - 2015" " Alfred E. Heggestad et al.\n", BARESIP_VERSION); (void)sys_coredump_set(true); err = libre_init(); if (err) goto out; #ifdef HAVE_GETOPT for (;;) { const int c = getopt(argc, argv, "6de:f:p:hu:vtm:"); if (0 > c) break; switch (c) { case '?': case 'h': usage(); return -2; #if HAVE_INET6 case '6': prefer_ipv6 = true; break; #endif case 'd': run_daemon = true; break; case 'e': exec = optarg; break; case 'f': conf_path_set(optarg); break; case 'm': if (modc >= ARRAY_SIZE(modv)) { warning("max %zu modules\n", ARRAY_SIZE(modv)); err = EINVAL; goto out; } modv[modc++] = optarg; break; case 'p': play_set_path(optarg); break; case 't': test = true; break; case 'u': ua_eprm = optarg; break; case 'v': log_enable_debug(true); break; default: break; } } #else (void)argc; (void)argv; #endif err = conf_configure(); if (err) { warning("main: configure failed: %m\n", err); goto out; } /* NOTE: must be done after all arguments are processed */ if (modc) { size_t i; info("pre-loading modules: %zu\n", modc); for (i=0; i<modc; i++) { err = module_preload(modv[i]); if (err) { re_fprintf(stderr, "could not pre-load module" " '%s' (%m)\n", modv[i], err); } } } /* Initialise User Agents */ err = ua_init("baresip v" BARESIP_VERSION " (" ARCH "/" OS ")", true, true, true, prefer_ipv6); if (err) goto out; if (ua_eprm) { err = uag_set_extra_params(ua_eprm); if (err) goto out; } if (test) goto out; /* Load modules */ err = conf_modules(); if (err) goto out; if (run_daemon) { err = sys_daemon(); if (err) goto out; log_enable_stderr(false); } info("baresip is ready.\n"); if (exec) ui_input_str(exec); /* Main loop */ err = re_main(signal_handler); out: if (err) ua_stop_all(true); ua_close(); conf_close(); libre_close(); /* Check for memory leaks */ tmr_debug(); mem_debug(); return err; }
int main(int argc, char** argv) { char path[50] = { 0x00 }; if (2 <= argc) { strncpy(path, argv[1], strlen(argv[1])); } else { strncpy(path, "server.cfg", 10); } //读取配置文件 TinyConf* config; char* run; for(;;) { config = conf_open(path, '='); if(NULL == config) { sleep(5); continue; } run = conf_get(config, "run"); if(0 >= strlen(run)) { sleep(5); continue; } if(0 != strcmp(run, "on")) { sleep(5); continue; } break; //一切ok,启动正常处理 } //初始化mysql配置 char* mysql_host = conf_get(config, "mysql_host"); char* mysql_port_str = conf_get(config, "mysql_port"); char* mysql_user = conf_get(config, "mysql_user"); char* mysql_password = conf_get(config, "mysql_password"); char* mysql_database = conf_get(config, "mysql_database"); //将数据库设置存入到map中 TinyMap* store_map = map_create(); map_add(store_map, "host", mysql_host, NULL); map_add(store_map, "port", mysql_port_str, NULL); map_add(store_map, "user", mysql_user, NULL); map_add(store_map, "password", mysql_password, NULL); map_add(store_map, "database", mysql_database, NULL); //初始化tcpserver的相关代码 int tcp_port = 7000; char* tcp_port_str = conf_get(config, "port"); if(0 < strlen(tcp_port_str)) { tcp_port = atoi(tcp_port_str); } TCPServer* tcp_server = tcp_server_create(tcp_port); //生成tcp服务器实例 if(NULL == tcp_server) { fprintf(stderr, "Create server falied.\n"); } tcp_server_set_proc_callback(tcp_server, store_map, analyze_proc); //注册数据处理的回调函数 tcp_server_set_timeout_callback(tcp_server, store_map, analyze_timeout); //注册客户端连接超时的回调函数 int ret = tcp_server_start(tcp_server); //启动tcp服务器 if(-1 == ret) { fprintf(stderr, "Start tcpserver falied.\n"); return 0; } //创建存储实例 Store* store = store_create(mysql_host, atoi(mysql_port_str), mysql_user, mysql_password, mysql_database); //创建网络检测线程 pthread_t thread_id; int p_ret = pthread_create(&thread_id, NULL, (void*)check_networks, store); if(0 != p_ret) { fprintf(stderr, "%s\n", "create networks check thread err."); } for(;;) { //阻塞 sleep(1000); } pthread_cancel(thread_id); //退出线程 store_destroy(store); //销毁存储实例 tcp_server_stop(tcp_server); //停止tcp服务器实例 tcp_server_destroy(tcp_server); //销毁tcp服务器实例 map_destroy(store_map); //销毁store_map实例 conf_close(config); //关闭配置信息读取实例 return 0; }
/** * Kick off the daap server and wait for events. * * This starts the initial db scan, sets up the signal * handling, starts the webserver, then sits back and waits * for events, as notified by the signal handler and the * web interface. These events are communicated via flags * in the config structure. * * \param argc count of command line arguments * \param argv command line argument pointers * \returns 0 on success, -1 otherwise * * \todo split out a ws_init and ws_start, so that the * web space handlers can be registered before the webserver * starts. * */ int main(int argc, char *argv[]) { int option; char *configfile=CONFFILE; WSCONFIG ws_config; int reload=0; int start_time; int end_time; int rescan_counter=0; int old_song_count, song_count; int force_non_root=0; int skip_initial=1; int kill_server=0; int convert_conf=0; char *db_type,*db_parms,*web_root,*runas, *tmp; char **mp3_dir_array; char *servername, *iface; char *ffid = NULL; int appdir = 0; char *perr=NULL; char txtrecord[255]; void *phandle; char *plugindir; int err; char *apppath; int debuglevel=0; int plugins_loaded = 0; #ifdef ALPHA_CUSTOMIZE char *share_path; pthread_t thread1; #endif config.use_mdns=1; err_setlevel(2); config.foreground=0; while((option=getopt(argc,argv,"D:d:c:P:mfrysiuvab:Vk")) != -1) { switch(option) { case 'a': appdir = 1; break; case 'b': ffid=optarg; break; case 'd': debuglevel = atoi(optarg); err_setlevel(debuglevel); break; case 'D': if(err_setdebugmask(optarg)) { usage(argv[0]); exit(EXIT_FAILURE); } break; case 'f': config.foreground=1; err_setdest(err_getdest() | LOGDEST_STDERR); break; case 'c': configfile=optarg; break; case 'm': config.use_mdns=0; break; #ifndef WIN32 case 'P': os_set_pidfile(optarg); break; #endif case 'r': reload=1; break; case 's': skip_initial=0; break; case 'y': force_non_root=1; break; #ifdef WIN32 case 'i': os_register(); exit(EXIT_SUCCESS); break; case 'u': os_unregister(); exit(EXIT_SUCCESS); break; #endif case 'v': convert_conf=1; break; case 'k': kill_server=1; break; case 'V': fprintf(stderr,"Firefly Media Server: Version %s\n",VERSION); exit(EXIT_SUCCESS); break; default: usage(argv[0]); exit(EXIT_FAILURE); break; } } if((getuid()) && (!force_non_root) && (!convert_conf)) { fprintf(stderr,"You are not root. This is almost certainly wrong. " "If you are\nsure you want to do this, use the -y " "command-line switch\n"); exit(EXIT_FAILURE); } if(kill_server) { os_signal_server(S_STOP); exit(0); } io_init(); io_set_errhandler(main_io_errhandler); ws_set_errhandler(main_ws_errhandler); /* read the configfile, if specified, otherwise * try defaults */ config.stats.start_time=start_time=(int)time(NULL); config.stop=0; /* set appdir first, that way config resolves relative to appdir */ if(appdir) { apppath = os_apppath(argv[0]); DPRINTF(E_INF,L_MAIN,"Changing cwd to %s\n",apppath); chdir(apppath); free(apppath); configfile="mt-daapd.conf"; } if(CONF_E_SUCCESS != conf_read(configfile)) { fprintf(stderr,"Error reading config file (%s)\n",configfile); exit(EXIT_FAILURE); } if(debuglevel) /* was specified, should override the config file */ err_setlevel(debuglevel); if(convert_conf) { fprintf(stderr,"Converting config file...\n"); if(CONF_E_SUCCESS != conf_write()) { fprintf(stderr,"Error writing config file.\n"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } DPRINTF(E_LOG,L_MAIN,"Firefly Version %s: Starting with debuglevel %d\n", VERSION,err_getlevel()); /* load plugins before we drop privs? Maybe... let the * plugins do stuff they might need to */ plugin_init(); if((plugindir=conf_alloc_string("plugins","plugin_dir",NULL)) != NULL) { /* instead of specifying plugins, let's walk through the directory * and load each of them */ if(!load_plugin_dir(plugindir)) { DPRINTF(E_LOG,L_MAIN,"Warning: Could not load plugins\n"); } else { plugins_loaded = TRUE; } free(plugindir); } if(!plugins_loaded) { if((!load_plugin_dir("/usr/lib/firefly/plugins")) && (!load_plugin_dir("/usr/lib/mt-daapd/plugins")) && (!load_plugin_dir("/lib/mt-daapd/plugins")) && (!load_plugin_dir("/lib/mt-daapd/plugins")) && (!load_plugin_dir("/usr/local/lib/mt-daapd/plugins")) && (!load_plugin_dir("/usr/local/lib/mt-daapd/plugins")) && (!load_plugin_dir("/opt/share/firefly/plugins")) && (!load_plugin_dir("/opt/share/mt-daapd/plugins")) && (!load_plugin_dir("/opt/lib/firefly/plugins")) && (!load_plugin_dir("/opt/lib/mt-daapd/plugins")) && (!load_plugin_dir("plugins/.libs"))) { DPRINTF(E_FATAL,L_MAIN,"plugins/plugin_dir not specified\n"); } } phandle=NULL; while((phandle=plugin_enum(phandle))) { DPRINTF(E_LOG,L_MAIN,"Plugin loaded: %s\n",plugin_get_description(phandle)); } runas = conf_alloc_string("general","runas","nobody"); #ifndef WITHOUT_MDNS if(config.use_mdns) { DPRINTF(E_LOG,L_MAIN,"Starting rendezvous daemon\n"); if(rend_init(runas)) { DPRINTF(E_FATAL,L_MAIN|L_REND,"Error in rend_init: %s\n", strerror(errno)); } } #endif if(!os_init(config.foreground,runas)) { DPRINTF(E_LOG,L_MAIN,"Could not initialize server\n"); os_deinit(); exit(EXIT_FAILURE); } free(runas); #ifdef UPNP upnp_init(); #endif /* this will require that the db be readable by the runas user */ db_type = conf_alloc_string("general","db_type","sqlite"); db_parms = conf_alloc_string("general","db_parms","/var/cache/mt-daapd"); err=db_open(&perr,db_type,db_parms); if(err) { DPRINTF(E_LOG,L_MAIN|L_DB,"Error opening db: %s\n",perr); #ifndef WITHOUT_MDNS if(config.use_mdns) { rend_stop(); } #endif os_deinit(); exit(EXIT_FAILURE); } free(db_type); free(db_parms); /* Initialize the database before starting */ DPRINTF(E_LOG,L_MAIN|L_DB,"Initializing database\n"); if(db_init(reload)) { DPRINTF(E_FATAL,L_MAIN|L_DB,"Error in db_init: %s\n",strerror(errno)); } err=db_get_song_count(&perr,&song_count); if(err != DB_E_SUCCESS) { DPRINTF(E_FATAL,L_MISC,"Error getting song count: %s\n",perr); } /* do a full reload if the db is empty */ if(!song_count) reload = 1; if(conf_get_array("general","mp3_dir",&mp3_dir_array)) { if((!skip_initial) || (reload)) { DPRINTF(E_LOG,L_MAIN|L_SCAN,"Starting mp3 scan\n"); #ifdef ALPHA_CUSTOMIZE share_path = conf_alloc_string("general", "mp3_dir", "AAA"); printf("mp3_dir_array[0] = [%s]\n", mp3_dir_array[0]); if (strlen(share_path) > 0) { Cnt_total_file(mp3_dir_array[0]); pthread_create( &thread1, NULL, (void*)process_bar, NULL); free(share_path); } #endif plugin_event_dispatch(PLUGIN_EVENT_FULLSCAN_START,0,NULL,0); start_time=(int) time(NULL); if(scan_init(mp3_dir_array)) { DPRINTF(E_LOG,L_MAIN|L_SCAN,"Error scanning MP3 files: %s\n",strerror(errno)); } if(!config.stop) { /* don't send popup when shutting down */ plugin_event_dispatch(PLUGIN_EVENT_FULLSCAN_END,0,NULL,0); err=db_get_song_count(&perr,&song_count); end_time=(int) time(NULL); DPRINTF(E_LOG,L_MAIN|L_SCAN,"Scanned %d songs in %d seconds\n", song_count,end_time - start_time); } } conf_dispose_array(mp3_dir_array); } #ifdef ALPHA_CUSTOMIZE thread_exit = 1; #endif /* start up the web server */ web_root = conf_alloc_string("general","web_root",NULL); ws_config.web_root=web_root; ws_config.port=conf_get_int("general","port",0); DPRINTF(E_LOG,L_MAIN|L_WS,"Starting web server from %s on port %d\n", ws_config.web_root, ws_config.port); config.server=ws_init(&ws_config); if(!config.server) { /* pthreads or malloc error */ DPRINTF(E_FATAL,L_MAIN|L_WS,"Error initializing web server\n"); } if(E_WS_SUCCESS != ws_start(config.server)) { /* listen or pthread error */ DPRINTF(E_FATAL,L_MAIN|L_WS,"Error starting web server\n"); } ws_registerhandler(config.server, "/",main_handler,main_auth, 0,1); #ifndef WITHOUT_MDNS if(config.use_mdns) { /* register services */ servername = conf_get_servername(); memset(txtrecord,0,sizeof(txtrecord)); txt_add(txtrecord,"txtvers=1"); txt_add(txtrecord,"Database ID=%0X",util_djb_hash_str(servername)); txt_add(txtrecord,"Machine ID=%0X",util_djb_hash_str(servername)); txt_add(txtrecord,"Machine Name=%s",servername); txt_add(txtrecord,"mtd-version=" VERSION); txt_add(txtrecord,"iTSh Version=131073"); /* iTunes 6.0.4 */ txt_add(txtrecord,"Version=196610"); /* iTunes 6.0.4 */ tmp = conf_alloc_string("general","password",NULL); if(tmp && (strlen(tmp)==0)) tmp=NULL; txt_add(txtrecord,"Password=%s",tmp ? "true" : "false"); if(tmp) free(tmp); srand((unsigned int)time(NULL)); if(ffid) { txt_add(txtrecord,"ffid=%s",ffid); } else { txt_add(txtrecord,"ffid=%08x",rand()); } DPRINTF(E_LOG,L_MAIN|L_REND,"Registering rendezvous names\n"); iface = conf_alloc_string("general","interface",""); rend_register(servername,"_http._tcp",ws_config.port,iface,txtrecord); plugin_rend_register(servername,ws_config.port,iface,txtrecord); free(servername); free(iface); } #endif end_time=(int) time(NULL); err=db_get_song_count(&perr,&song_count); if(err != DB_E_SUCCESS) { DPRINTF(E_FATAL,L_MISC,"Error getting song count: %s\n",perr); } DPRINTF(E_LOG,L_MAIN,"Serving %d songs. Startup complete in %d seconds\n", song_count,end_time-start_time); if(conf_get_int("general","rescan_interval",0) && (!reload) && (!conf_get_int("scanning","skip_first",0))) config.reload = 1; /* force a reload on start */ while(!config.stop) { if((conf_get_int("general","rescan_interval",0) && (rescan_counter > conf_get_int("general","rescan_interval",0)))) { if((conf_get_int("general","always_scan",0)) || (config_get_session_count())) { config.reload=1; } else { DPRINTF(E_DBG,L_MAIN|L_SCAN|L_DB,"Skipped bground scan... no users\n"); } rescan_counter=0; } if(config.reload) { old_song_count = song_count; start_time=(int) time(NULL); DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Rescanning database\n"); if(conf_get_array("general","mp3_dir",&mp3_dir_array)) { if(config.full_reload) { config.full_reload=0; db_force_rescan(NULL); } if(scan_init(mp3_dir_array)) { DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Error rescanning... bad path?\n"); } conf_dispose_array(mp3_dir_array); } config.reload=0; db_get_song_count(NULL,&song_count); DPRINTF(E_LOG,L_MAIN|L_DB|L_SCAN,"Scanned %d songs (was %d) in " "%d seconds\n",song_count,old_song_count, time(NULL)-start_time); } os_wait(MAIN_SLEEP_INTERVAL); rescan_counter += MAIN_SLEEP_INTERVAL; } DPRINTF(E_LOG,L_MAIN,"Stopping gracefully\n"); #ifndef WITHOUT_MDNS if(config.use_mdns) { DPRINTF(E_LOG,L_MAIN|L_REND,"Stopping rendezvous daemon\n"); rend_stop(); } #endif #ifdef UPNP upnp_deinit(); #endif /* Got to find a cleaner way to stop the web server. * Closing the fd of the socking accepting doesn't necessarily * cause the accept to fail on some libcs. * DPRINTF(E_LOG,L_MAIN|L_WS,"Stopping web server\n"); ws_stop(config.server); */ free(web_root); conf_close(); DPRINTF(E_LOG,L_MAIN|L_DB,"Closing database\n"); db_deinit(); DPRINTF(E_LOG,L_MAIN,"Done!\n"); os_deinit(); io_deinit(); mem_dump(); return EXIT_SUCCESS; }