int server_init(server_t *server) { assert(server!= NULL); pthread_mutex_init(&server->send_pending_lock, NULL); pthread_cond_init(&server->send_pending_cond, NULL); UNUSED int r; get_instance_parent_full_path(server->root_dir, NAME_MAX); sprintf(server->storage.storage_dir, "%s/data/storage", server->root_dir); if ( mkdir_if_not_exist(server->storage.storage_dir) != 0 ){ error_log("mkdir %s failed.", server->storage.storage_dir); return -1; } char log_dir[NAME_MAX]; sprintf(log_dir, "%s/data/log", server->root_dir); if ( mkdir_if_not_exist(log_dir) != 0 ) { error_log("mkdir %s failed.", log_dir); return -1; } /*r = storage_init(&server->storage);*/ int i; for ( i = 0 ; i < VNODES ; i++ ){ vnode_t *vnode = vnode_new(server->storage.storage_dir, i); if ( vnode == NULL ){ error_log("vnode_init() failed. id:%d", i); return -1; } server->vnodes[i] = vnode; } /* logfile */ for ( i = 0 ; i < LOGFILES ; i++ ) { char logfile_name[NAME_MAX]; sprintf(logfile_name, "%s/%02d.log", log_dir, i); logfile_t *logfile = logfile_new(i, logfile_name); if ( logfile_open(logfile, 1) != 0 ) { error_log("logfile_open(%d) failed.", i); return -1; } server->logfiles[i] = logfile; } /* FIXME */ r = init_server_work_queue(server); if ( r != 0 ){ error_log("init_server_work_queue() failed."); return -1; } return 0; }
int main(int argc, char *argv[]) { program_options_t po; memset(&po, 0, sizeof(program_options_t)); /*po.frontend = "tcp://127.0.0.1:19977";*/ /*po.backend = "tcp://127.0.0.1:19978";*/ po.frontend = "tcp://*:19977"; po.backend = "tcp://*:19978"; po.is_daemon = 0; po.log_level = LOG_INFO; int ch, longindex; while ((ch = getopt_long(argc, argv, short_options, long_options, &longindex)) >= 0) { switch (ch) { case 'f': po.frontend = optarg; break; case 'b': po.backend = optarg; break; case 'd': po.is_daemon = 1; break; case 'v': po.log_level = LOG_DEBUG; break; case 't': po.log_level = LOG_TRACE; break; case 'h': usage(0); break; default: usage(1); break; } } /* -------- Init logger -------- */ char root_dir[NAME_MAX]; get_instance_parent_full_path(root_dir, NAME_MAX); char log_dir[NAME_MAX]; sprintf(log_dir, "%s/log", root_dir); mkdir_if_not_exist(log_dir); char logfile[PATH_MAX]; sprintf(logfile, "%s/%s.log", log_dir, program_name); if (log_init(program_name, LOG_SPACE_SIZE, po.is_daemon, po.log_level, logfile)) return -1; if ( po.is_daemon ){ return daemon_fork(daemon_loop, (void*)&po); } else return run_broker(po.frontend, po.backend, po.log_level >= LOG_DEBUG ? 1 : 0); }
int datazone_init(datazone_t *datazone, struct vnode_t *vnode, int id) { memset(datazone, 0, sizeof(datazone_t)); datazone->vnode = vnode; datazone->id = id; sprintf(datazone->storage_dir, "%s/%02d", vnode->root_dir, id); if ( mkdir_if_not_exist(datazone->storage_dir) == 0 ){ return 0; } else { error_log("Cann't create datazone(%d) dir:%s", id, datazone->storage_dir); return -1; } }
/* Open the database and set up the database handle for it */ int sqlite_prepare_dbh(const char *topdir) { int ret; /* Do nothing if the database handle is already set up */ if (dbh) return 0; ret = snprintf(buf, PATH_MAX - 1, "%s/main.sqlite", topdir); if (ret < 0) return ret; buf[PATH_MAX - 1] = '\0'; /* open a new DB handle */ ret = sqlite3_open(buf, &dbh); if (ret != SQLITE_OK) { /* try to create the dir */ ret = mkdir_if_not_exist(topdir); if (ret) goto out_close; /* retry open */ ret = sqlite3_open(buf, &dbh); if (ret != SQLITE_OK) goto out_close; } /* set busy timeout */ ret = sqlite3_busy_timeout(dbh, CLTRACK_SQLITE_BUSY_TIMEOUT); if (ret != SQLITE_OK) { xlog(L_ERROR, "Unable to set sqlite busy timeout: %s", sqlite3_errmsg(dbh)); goto out_close; } ret = sqlite_query_schema_version(); switch (ret) { case CLTRACK_SQLITE_LATEST_SCHEMA_VERSION: /* DB is already set up. Do nothing */ ret = 0; break; case 1: /* Old DB -- update to new schema */ ret = sqlite_maindb_update_v1_to_v2(); if (ret) goto out_close; break; case 0: /* Query failed -- try to set up new DB */ ret = sqlite_maindb_init_v2(); if (ret) goto out_close; break; default: /* Unknown DB version -- downgrade? Fail */ xlog(L_ERROR, "Unsupported database schema version! " "Expected %d, got %d.", CLTRACK_SQLITE_LATEST_SCHEMA_VERSION, ret); ret = -EINVAL; goto out_close; } return ret; out_close: sqlite3_close(dbh); dbh = NULL; return ret; }
vnode_t *vnode_new(const char *root_dir, uint32_t id, enum eVnodeStorageType storage_type, vnode_write_queue_handle_write_cb vnode_write_queue_handle_write) { vnode_t *vnode = (vnode_t*)zmalloc(sizeof(vnode_t)); memset(vnode, 0, sizeof(vnode_t)); vnode->id = id; vnode->storage_type = storage_type; /*vnode->max_dbsize = 1024L * 1024L * 1024L * 4L;*/ vnode->max_dbsize = 1024L * 1024L * 500L; /* Create vnode root dir */ sprintf(vnode->root_dir, "%s/%04d", root_dir, id); if ( mkdir_if_not_exist(vnode->root_dir) != 0 ){ error_log("Cann't create vnode(%d) dir:%s", id, vnode->root_dir); zfree(vnode); return NULL; } /* datazones */ int i; for ( i = 0 ; i < MAX_DATAZONES ; i++ ){ vnode->datazones[i] = (datazone_t*)zmalloc(sizeof(datazone_t)); if ( datazone_init(vnode->datazones[i], vnode, i) != 0 ){ zfree(vnode); return NULL; } } /* Slices DB */ if ( vnode->storage_type >= STORAGE_KVDB ){ // Create Metadata DB. const char *metadata_dbname = "metadata"; kvdb_t *kvdb_metadata = vnode_open_kvdb(vnode, metadata_dbname); if ( kvdb_metadata == NULL ){ error_log("MetadataDB create failed. dbname:%s", metadata_dbname); zfree(vnode); return NULL; } vnode->kvdb_metadata = kvdb_metadata; uint32_t active_slicedb_id = 0; if ( kvdb_get_uint32(kvdb_metadata, "active_slicedb_id", &active_slicedb_id) != 0 ){ active_slicedb_id = 0; } notice_log("vnode active_slicedb_id:%d", active_slicedb_id); // Create Slice DB. for ( int db_id = 0 ; db_id <= active_slicedb_id ; db_id++ ){ slicedb_t *slicedb = vnode_open_slicedb(vnode, db_id); if ( slicedb != NULL ){ } else { /*char dbname[NAME_MAX];*/ /*sprintf(dbname, "slice-%03d", db_id);*/ /*kvdb_t *kvdb = vnode_open_kvdb(vnode, dbname);*/ /*if ( kvdb != NULL ){*/ /*vnode->slicedbs[db_id] = slicedb_new(db_id, kvdb, vnode->max_dbsize);*/ /*} else {*/ /*error_log("SliceDB create failed. dbname:%s", dbname);*/ for ( int n = 0 ; n < db_id ; n++ ){ slicedb_free(vnode->slicedbs[n]); vnode->slicedbs[n] = NULL; } zfree(vnode); return NULL; } } vnode->active_slicedb = vnode->slicedbs[active_slicedb_id]; /*vnode->kvdb = vnode->active_slicedb->kvdb;*/ } vnode->caching_objects = object_queue_new(object_compare_md5_func); vnode->received_objects = listCreate(); vnode->received_object_size = 0; vnode->standby_objects = listCreate(); vnode->standby_object_size = 0; vnode->write_queue = init_work_queue(vnode_write_queue_handle_write, VNODE_WRITE_QUEUE_INTERVAL); return vnode; }
/* * Open the "main" database, and attempt to initialize it by creating the * parameters table and inserting the schema version into it. Ignore any errors * from that, and then attempt to select the version out of it again. If the * version appears wrong, then assume that the DB is corrupt or has been * upgraded, and return an error. If all of that works, then attempt to create * the "clients" table. */ int sqlite_maindb_init(const char *topdir) { int ret; char *err = NULL; sqlite3_stmt *stmt = NULL; ret = mkdir_if_not_exist(topdir); if (ret) return ret; ret = sqlite_prepare_dbh(topdir); if (ret) return ret; /* Try to create table */ ret = sqlite3_exec(dbh, "CREATE TABLE IF NOT EXISTS parameters " "(key TEXT PRIMARY KEY, value TEXT);", NULL, NULL, &err); if (ret != SQLITE_OK) { xlog(L_ERROR, "Unable to create parameter table: %d", ret); goto out_err; } /* insert version into table -- ignore error if it fails */ ret = snprintf(buf, sizeof(buf), "INSERT OR IGNORE INTO parameters values (\"version\", " "\"%d\");", CLD_SQLITE_SCHEMA_VERSION); if (ret < 0) { goto out_err; } else if ((size_t)ret >= sizeof(buf)) { ret = -EINVAL; goto out_err; } ret = sqlite3_exec(dbh, (const char *)buf, NULL, NULL, &err); if (ret != SQLITE_OK) { xlog(L_ERROR, "Unable to insert into parameter table: %d", ret); goto out_err; } ret = sqlite3_prepare_v2(dbh, "SELECT value FROM parameters WHERE key == \"version\";", -1, &stmt, NULL); if (ret != SQLITE_OK) { xlog(L_ERROR, "Unable to prepare select statement: %d", ret); goto out_err; } /* check schema version */ ret = sqlite3_step(stmt); if (ret != SQLITE_ROW) { xlog(L_ERROR, "Select statement execution failed: %s", sqlite3_errmsg(dbh)); goto out_err; } /* process SELECT result */ ret = sqlite3_column_int(stmt, 0); if (ret != CLD_SQLITE_SCHEMA_VERSION) { xlog(L_ERROR, "Unsupported database schema version! " "Expected %d, got %d.", CLD_SQLITE_SCHEMA_VERSION, ret); ret = -EINVAL; goto out_err; } /* now create the "clients" table */ ret = sqlite3_exec(dbh, "CREATE TABLE IF NOT EXISTS clients " "(id BLOB PRIMARY KEY, time INTEGER);", NULL, NULL, &err); if (ret != SQLITE_OK) { xlog(L_ERROR, "Unable to create clients table: %s", err); goto out_err; } sqlite3_free(err); sqlite3_finalize(stmt); return 0; out_err: if (err) { xlog(L_ERROR, "sqlite error: %s", err); sqlite3_free(err); } sqlite3_finalize(stmt); sqlite3_close(dbh); return ret; }
void core_spytask() { int cnt = 1; int i=0; #ifdef CAM_HAS_GPS int gps_delay_timer = 200 ; int gps_state = -1 ; #endif #if (OPT_DISABLE_CAM_ERROR) extern void DisableCamError(); int dce_cnt=0; int dce_prevmode=0; int dce_nowmode; #endif parse_version(&chdk_version, BUILD_NUMBER, BUILD_SVNREV); // Init camera_info bits that can't be done statically camera_info_init(); spytask_can_start=0; extern void aram_malloc_init(void); aram_malloc_init(); extern void exmem_malloc_init(void); exmem_malloc_init(); #ifdef CAM_CHDK_PTP extern void init_chdk_ptp_task(); init_chdk_ptp_task(); #endif while((i++<400) && !spytask_can_start) msleep(10); started(); msleep(50); finished(); #if !CAM_DRYOS drv_self_unhide(); #endif conf_restore(); extern void gui_init(); gui_init(); #if CAM_CONSOLE_LOG_ENABLED extern void cam_console_init(); cam_console_init(); #endif static char *chdk_dirs[] = { "A/CHDK", "A/CHDK/FONTS", "A/CHDK/SYMBOLS", "A/CHDK/SCRIPTS", "A/CHDK/LANG", "A/CHDK/BOOKS", "A/CHDK/MODULES", "A/CHDK/MODULES/CFG", "A/CHDK/GRIDS", "A/CHDK/CURVES", "A/CHDK/DATA", "A/CHDK/LOGS", "A/CHDK/EDGE", }; for (i = 0; i < sizeof(chdk_dirs) / sizeof(char*); i++) mkdir_if_not_exist(chdk_dirs[i]); no_modules_flag = stat("A/CHDK/MODULES/FSELECT.FLT",0) ? 1 : 0 ; // Calculate the value of get_tick_count() when the clock ticks over to the next second // Used to calculate the SubSecondTime value when saving DNG files. long t1, t2; t2 = time(0); do { t1 = t2; camera_info.tick_count_offset = get_tick_count(); t2 = time(0); msleep(10); } while (t1 != t2); camera_info.tick_count_offset = camera_info.tick_count_offset % 1000; // remote autostart if (conf.script_startup==SCRIPT_AUTOSTART_ALWAYS) { script_autostart(); } else if (conf.script_startup==SCRIPT_AUTOSTART_ONCE) { conf.script_startup=SCRIPT_AUTOSTART_NONE; conf_save(); script_autostart(); } shooting_init(); while (1) { // Set up camera mode & state variables mode_get(); // update HDMI power override based on mode and remote settings #ifdef CAM_REMOTE_HDMI_POWER_OVERRIDE extern void update_hdmi_power_override(void); update_hdmi_power_override(); #endif extern void set_palette(); set_palette(); #if (OPT_DISABLE_CAM_ERROR) dce_nowmode = camera_info.state.mode_play; if (dce_prevmode==dce_nowmode) { //no mode change dce_cnt++; // overflow is not a concern here } else { //mode has changed dce_cnt=0; } if (dce_cnt==100) { // 1..2s past play <-> rec mode change DisableCamError(); } dce_prevmode=dce_nowmode; #endif if ( memdmptick && (get_tick_count() >= memdmptick) ) { memdmptick = 0; dump_memory(); } #ifdef CAM_HAS_GPS if ( --gps_delay_timer == 0 ) { gps_delay_timer = 50 ; if ( gps_state != (int)conf.gps_on_off ) { gps_state = (int)conf.gps_on_off ; init_gps_startup(!gps_state) ; } } #endif // Change ALT mode if the KBD task has flagged a state change gui_activate_alt_mode(); #ifdef CAM_LOAD_CUSTOM_COLORS // Color palette function extern void load_chdk_palette(); load_chdk_palette(); #endif if (raw_data_available) { raw_process(); extern void hook_raw_save_complete(); hook_raw_save_complete(); raw_data_available = 0; #ifdef CAM_HAS_GPS if (((int)conf.gps_on_off == 1) && ((int)conf.gps_waypoint_save == 1)) gps_waypoint(); #endif #if defined(CAM_CALC_BLACK_LEVEL) // Reset to default in case used by non-RAW process code (e.g. raw merge) camera_sensor.black_level = CAM_BLACK_LEVEL; #endif continue; } if ((camera_info.state.state_shooting_progress != SHOOTING_PROGRESS_PROCESSING) || recreview_hold) { if (((cnt++) & 3) == 0) gui_redraw(); } if (camera_info.state.state_shooting_progress != SHOOTING_PROGRESS_PROCESSING) { if (conf.show_histo) libhisto->histogram_process(); if ((camera_info.state.gui_mode_none || camera_info.state.gui_mode_alt) && conf.edge_overlay_thresh && conf.edge_overlay_enable) { // We need to skip first tick because stability if (chdk_started_flag) { libedgeovr->edge_overlay(); } } } if ((camera_info.state.state_shooting_progress == SHOOTING_PROGRESS_PROCESSING) && (!shooting_in_progress())) { camera_info.state.state_shooting_progress = SHOOTING_PROGRESS_DONE; } i = 0; #ifdef DEBUG_PRINT_TO_LCD sprintf(osd_buf, "%d", cnt ); // modify cnt to what you want to display draw_txt_string(1, i++, osd_buf, user_color(conf.osd_color)); #endif #if defined(OPT_FILEIO_STATS) sprintf(osd_buf, "%3d %3d %3d %3d %3d %3d %3d %4d", camera_info.fileio_stats.fileio_semaphore_errors, camera_info.fileio_stats.close_badfile_count, camera_info.fileio_stats.write_badfile_count, camera_info.fileio_stats.open_count, camera_info.fileio_stats.close_count, camera_info.fileio_stats.open_fail_count, camera_info.fileio_stats.close_fail_count, camera_info.fileio_stats.max_semaphore_timeout); draw_txt_string(1, i++, osd_buf,user_color( conf.osd_color)); #endif if (camera_info.perf.md_af_tuning) { sprintf(osd_buf, "MD last %-4d min %-4d max %-4d avg %-4d", camera_info.perf.af_led.last, camera_info.perf.af_led.min, camera_info.perf.af_led.max, (camera_info.perf.af_led.count>0)?camera_info.perf.af_led.sum/camera_info.perf.af_led.count:0); draw_txt_string(1, i++, osd_buf, user_color(conf.osd_color)); } // Process async module unload requests module_tick_unloader(); msleep(20); chdk_started_flag=1; } }
/* * Main function game called every frame */ void gameUpdate(StcGame *game) { long sysTime; /* Read user input */ platformReadInput(game); /* Update game state */ if (game->isOver) { if (game->stats.score > game->stats.high) { game->stats.high = game->stats.score; FILE * f; long buf; buf = game->stats.score; mkdir_if_not_exist("A/CHDK/GAMES"); f = fopen ( "A/CHDK/GAMES/TETRIS.SCO" , "wb" ); fwrite (&buf , 1 , sizeof(buf) , f ); fclose (f); } //if (game->events & EVENT_RESTART) { if (game->events & EVENT_PAUSE) { //TurnOnBackLight(); game->isOver = 0; startGame(game); } } else { sysTime = platformGetSystemTime(); /* Always handle pause event */ if (game->events & EVENT_PAUSE) { game->isPaused = !game->isPaused; game->events = EVENT_NONE; } /* Check if the game is paused */ if (game->isPaused) { /* We achieve the effect of pausing the game * adding the last frame duration to lastFallTime */ game->lastFallTime += (sysTime - game->systemTime); } else { if (game->events != EVENT_NONE) { if (game->events & EVENT_SHOW_NEXT) { game->showPreview = !game->showPreview; } if (game->events & EVENT_DROP) { dropTetramino(game); } if (game->events & EVENT_ROTATE_CW) { rotateTetramino(game, 1); } if (game->events & EVENT_MOVE_RIGHT) { moveTetramino(game, 1, 0); } else if (game->events & EVENT_MOVE_LEFT) { moveTetramino(game, -1, 0); } if (game->events & EVENT_MOVE_DOWN) { moveTetramino(game, 0, 1); } game->events = EVENT_NONE; } /* Check if it's time to move downwards the falling tetromino */ if (sysTime - game->lastFallTime >= game->delay) { moveTetramino(game, 0, 1); game->lastFallTime = sysTime; } } game->systemTime = sysTime; } /* Draw game state */ platformRenderGame(game); }