int main(int argc, char** argv){ int i; char buffer[256]; char* tk; FILTERCHAIN* tmp_chn; FILTERCHAIN* del_chn; HARNESS_INSTANCE* hinstance; if(harness_init(argc,argv,&hinstance)){ printf("Error: Initialization failed.\n"); skygw_log_write(LOGFILE_ERROR,"Error: Initialization failed.\n"); skygw_logmanager_done(); skygw_logmanager_exit(); return 1; } if(instance.verbose){ printf("\n\n\tFilter Test Harness\n\n"); } while(instance.running){ printf("Harness> "); memset(buffer,0,256); fgets(buffer,256,stdin); tk = strtok(buffer," \n"); switch(user_input(tk)) { case RUNFILTERS: if(instance.head->next == NULL){ printf("No filters loaded.\n"); break; } if(instance.buffer == NULL){ if(instance.infile<0){ manual_query(); }else{ load_query(); } } route_buffers(); break; case LOAD_FILTER: tk = strtok(NULL," \n"); tmp_chn = load_filter_module(tk); if(!tmp_chn || !load_filter(tmp_chn,instance.conf)){ printf("Error creating filter instance.\n"); skygw_log_write(LOGFILE_ERROR,"Error: Error creating filter instance.\n"); }else{ instance.head = tmp_chn; } break; case DELETE_FILTER: tk = strtok(NULL," \n\0"); tmp_chn = instance.head; del_chn = instance.head; if(tk){ if(strcmp(instance.head->name,tk) == 0){ instance.head = instance.head->next; }else{ while(del_chn->next){ if(strcmp(del_chn->name,tk) == 0){ tmp_chn->next = del_chn->next; break; }else{ tmp_chn = del_chn; del_chn = del_chn->next; } } } if(del_chn && del_chn->next){ printf("Deleted %s.\n",del_chn->name); if(del_chn->instance){ del_chn->instance->freeSession(del_chn->filter,del_chn->session); } free(del_chn->filter); free(del_chn->down); free(del_chn->name); free(del_chn); }else{ printf("No matching filter found.\n"); } } break; case LOAD_CONFIG: tk = strtok(NULL," \n\0"); if(!load_config(tk)){ free_filters(); } break; case SET_INFILE: tk = strtok(NULL," \n\0"); if(instance.infile >= 0){ close(instance.infile); free(instance.infile_name); } if(tk!= NULL){ free_buffers(); instance.infile = open_file(tk,0); if(instance.infile >= 0){ load_query(); instance.infile_name = strdup(tk); if(instance.verbose){ printf("Loaded %d queries from file '%s'\n",instance.buffer_count,instance.infile_name); } } }else{ instance.infile = -1; printf("Queries are read from: command line\n"); } break; case SET_OUTFILE: tk = strtok(NULL," \n\0"); if(instance.outfile >= 0){ close(instance.outfile); free(instance.outfile_name); } if(tk!= NULL){ instance.outfile = open_file(tk,1); if(instance.outfile >= 0){ instance.outfile_name = strdup(tk); printf("Output is logged to: %s\n",tk); } }else{ instance.outfile = -1; printf("Output logging disabled.\n"); } break; case SESS_COUNT: tk = strtok(NULL," \n\0"); free_buffers(); free_filters(); instance.session_count = atoi(tk); printf("Sessions set to: %d\n", instance.session_count); break; case THR_COUNT: instance.running = 0; pthread_mutex_unlock(&instance.work_mtx); for(i = 0;i<instance.thrcount;i++){ pthread_join(instance.thrpool[i],NULL); } pthread_mutex_lock(&instance.work_mtx); instance.running = 1; tk = strtok(NULL," \n\0"); instance.thrcount = atoi(tk); void* t_thr_pool; if(!(t_thr_pool = realloc(instance.thrpool,instance.thrcount * sizeof(pthread_t)))){ printf("Error: Out of memory\n"); skygw_log_write(LOGFILE_ERROR,"Error: Out of memory\n"); instance.running = 0; break; } instance.thrpool = t_thr_pool; int thr_num = 1; for(i = 0;i<instance.thrcount;i++){ pthread_create(&instance.thrpool[i], NULL, (void*)work_buffer, (void*)thr_num++); } printf("Threads set to: %d\n", instance.thrcount); break; case QUIT: instance.running = 0; pthread_mutex_unlock(&instance.work_mtx); for(i = 0;i<instance.thrcount;i++){ pthread_join(instance.thrpool[i],NULL); } break; case UNDEFINED: printf("Command not found, enter \"help\" for a list of commands\n"); break; default: break; } } if(instance.infile >= 0){ close(instance.infile); } if(instance.outfile >= 0){ close(instance.outfile); } free_buffers(); free_filters(); skygw_logmanager_done(); skygw_logmanager_exit(); free(instance.head); return 0; }
int load_config( char* fname) { CONFIG* iter; CONFIG_ITEM* item; int config_ok = 1,inirval; free_filters(); if((inirval = ini_parse(fname,handler,instance.conf)) < 0){ printf("Error parsing configuration file!\n"); if(inirval == -1) printf("Inih file open error.\n"); else if(inirval == -2) printf("inih memory error.\n"); MXS_ERROR("Error parsing configuration file!\n"); config_ok = 0; goto cleanup; } if(instance.verbose){ printf("Configuration loaded from %s\n\n",fname); } if(instance.conf == NULL){ printf("Nothing valid was read from the file.\n"); MXS_NOTICE("Nothing valid was read from the file.\n"); config_ok = 0; goto cleanup; } instance.conf = process_config(instance.conf); if(instance.conf){ if(instance.verbose){ printf("Modules Loaded:\n"); } iter = instance.conf; }else{ printf("No filters found in the configuration file.\n"); MXS_NOTICE("No filters found in the configuration file.\n"); config_ok = 0; goto cleanup; } while(iter){ item = iter->item; while(item){ if(!strcmp("module",item->name)){ if(instance.mod_dir){ char* modstr = malloc(sizeof(char)*(strlen(instance.mod_dir) + strlen(item->value) + 1)); strcpy(modstr,instance.mod_dir); strcat(modstr,"/"); strcat(modstr,item->value); instance.head = load_filter_module(modstr); free(modstr); }else{ instance.head = load_filter_module(item->value); } if(!instance.head || !load_filter(instance.head,instance.conf)){ printf("Error creating filter instance!\nModule: %s\n",item->value); MXS_ERROR("Error creating filter instance!\nModule: %s\n",item->value); config_ok = 0; goto cleanup; }else{ if(instance.verbose){ printf("\t%s\n",iter->section); } } } item = item->next; } iter = iter->next; } while(instance.conf){ item = instance.conf->item; while(item){ item = instance.conf->item; instance.conf->item = instance.conf->item->next; free(item->name); free(item->value); free(item); item = instance.conf->item; } instance.conf = instance.conf->next; } cleanup: while(instance.conf){ iter = instance.conf; instance.conf = instance.conf->next; item = iter->item; while(item){ free(item->name); free(item->value); free(item); iter->item = iter->item->next; item = iter->item; } free(iter); } instance.conf = NULL; return config_ok; }