/* * Set the on-screen status text */ void set_smart_status() { static char status_text[15]; snprintf(status_text, sizeof(status_text), "%d:%02d - %d:%02d", twenty_four_to_twelve(get_config_data()->fromhr), get_config_data()->frommin, twenty_four_to_twelve(get_config_data()->tohr), get_config_data()->tomin); set_icon(get_config_data()->smart && get_config_data()->weekend_until != 0, IS_WEEKEND); set_smart_status_on_screen(get_config_data()->smart, status_text); analogue_set_smart_times(); }
/* * Bed time with the alarm turned off */ static void reset_with_alarm_off() { if (get_config_data()->smart) { get_config_data()->smart = false; trigger_config_save(); set_smart_status(); } reset_sleep_period(); }
/* * Set the fields to the values from the rest of the system */ static void set_values() { values[F_SMART_ALARM] = get_config_data()->smart; values[F_FROM_HOUR] = get_config_data()->fromhr; values[F_FROM_MINUTE] = get_config_data()->frommin; values[F_TO_HOUR] = get_config_data()->tohr; values[F_TO_MINUTE] = get_config_data()->tomin; values[F_DONE] = 1; }
/* * Store the current information at this point in time - keep it until replaced */ EXTFN void store_chart_data() { chart_data.chart_ver = CHART_VER; chart_data.base = get_internal_data()->base; chart_data.gone_off = get_internal_data()->gone_off; chart_data.highest_entry = get_internal_data()->highest_entry; for (int i = 0; i < LIMIT; i++) { chart_data.points[i] = get_internal_data()->points[i]; chart_data.ignore[i] = get_internal_data()->ignore[i]; } chart_data.snoozes = get_internal_data()->snoozes; chart_data.from = get_config_data()->from; chart_data.to = get_config_data()->to; chart_data.smart = get_config_data()->smart; save_chart_data(); }
Stats * add_new_stats(username_t username, Stats * st, long long tick_id) { User_stats *us = (User_stats *) g_hash_table_lookup(users, username); struct governor_config data_cfg; get_config_data( &data_cfg ); //pthread_mutex_lock(&mtx_account); if (!us) { us = add_user_stats(username, accounts, users); } if(data_cfg.debug_user && !us->account->need_dbg){ int len = strlen(data_cfg.debug_user); if(!strncmp(data_cfg.debug_user, us->account->id, len)){ us->account->need_dbg = 1; } } /*if(check_if_user_restricted(username, accounts) && data_cfg.use_lve){ return NULL; }*/ Stats *stt = NULL; if(us){ if (us->tick == tick_id){ stt = refresh_stats(st, us); } else { us->tick = get_current_tick(); stt = push_stats(st, us); } } //pthread_mutex_unlock(&mtx_account); return stt; }
/* * Toggle weekend mode */ void toggle_weekend_mode() { if (!get_config_data()->smart) { show_notice(RESOURCE_ID_NOTICE_NEED_SMART_ALARM); return; } // Toggle weekend if (get_config_data()->weekend_until > 0) { // Turn off weekend get_config_data()->weekend_until = 0; set_smart_status(); } else { // Turn on weekend get_config_data()->weekend_until = time(NULL) + WEEKEND_PERIOD; set_smart_status(); } }
int get_timeout(int *restrict_level, int penalty) { struct governor_config data_cfg; get_config_data( &data_cfg ); *restrict_level += penalty; if (*restrict_level > 3) *restrict_level = 3; if (*restrict_level < 0) *restrict_level = 0; switch (*restrict_level) { case 0: return data_cfg.level1; break; case 1: return data_cfg.level2; break; case 2: return data_cfg.level3; break; case 3: return data_cfg.level4; break; default: return data_cfg.level4; } return -1; }
void account_unrestrict(Account * ac) { int i; User_stats *us; //Command cmd; struct governor_config data_cfg; get_config_data( &data_cfg ); if( data_cfg.is_gpl ) return; if( data_cfg.all_lve ) return; //lve use=all if( !data_cfg.use_lve && !data_cfg.separate_lve ) return; //lve use=off for (i = 0; i < ac->users->len; i++) { us = g_ptr_array_index (ac->users, i); Command *cmd = g_malloc(sizeof(Command)); if (cmd) { if (command_list) { strlcpy(cmd->username, us->id, USERNAMEMAXLEN); cmd->command = UNFREEZE; pthread_mutex_lock(&mtx_commands); command_list = g_list_append(command_list, cmd); pthread_mutex_unlock(&mtx_commands); } } } }
//Set new max_user_connections parameter void update_user_limit(char *user_name, unsigned int limit, MODE_TYPE debug_mode) { char buffer[_DBGOVERNOR_BUFFER_2048]; char sql_buffer[_DBGOVERNOR_BUFFER_8192]; char user_name_alloc[USERNAMEMAXLEN * 2]; MYSQL_RES *res; struct governor_config data_cfg; get_config_data( &data_cfg ); (*_mysql_real_escape_string)(mysql_do_command, user_name_alloc, user_name, strlen(user_name)); snprintf(sql_buffer, _DBGOVERNOR_BUFFER_8192, QUERY_USER_CONN_LIMIT, (int) limit, user_name_alloc); if (db_mysql_exec_query(sql_buffer, mysql_do_command, debug_mode)) { if (debug_mode != DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't execute sql request. Restriction aborted", data_cfg.log_mode); } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "%s --- Request: %s", data_cfg.log_mode,"Can't execute sql request. Restriction aborted", sql_buffer); } } res = (*_mysql_store_result)(mysql_do_command); (*_mysql_free_result)(res); flush_user_priv(debug_mode); }
//LVE CONNECTION request void lve_connection(char *user_name, MODE_TYPE debug_mode) { char buffer[_DBGOVERNOR_BUFFER_2048]; char sql_buffer[_DBGOVERNOR_BUFFER_8192]; char user_name_alloc[USERNAMEMAXLEN * 2]; MYSQL_RES *res; struct governor_config data_cfg; get_config_data( &data_cfg ); (*_mysql_real_escape_string)(mysql_do_command, user_name_alloc, user_name, strlen(user_name)); snprintf(sql_buffer, _DBGOVERNOR_BUFFER_8192, QUERY_LVE_USER_CONNECTION, user_name_alloc); if (db_mysql_exec_query(sql_buffer, mysql_do_command, debug_mode)) { if (debug_mode != DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't execute sql request. LVE connection", data_cfg.log_mode); } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "%s --- Request: %s", data_cfg.log_mode, "Can't execute sql request. LVE connection", sql_buffer); } } res = (*_mysql_store_result)(mysql_do_command); (*_mysql_free_result)(res); }
void account_analyze(gpointer * key, Account * ac, void *data) { stats_limit_cfg cfg_buf; stats_limit_cfg *sl = config_get_account_limit(ac->id, &cfg_buf); int restrict_period = 0; char tmp_buf[_DBGOVERNOR_BUFFER_8192]; struct governor_config data_cfg; get_config_data( &data_cfg ); if(data_cfg.all_lve || !data_cfg.use_lve) return; if (sl->mode != IGNORE_MODE) { if (0 == ac->timeout) { //I am not restricted, but I might be in penalty period. Restrict account if need if (check_restrict(ac)) { //If restricted, just exit return; } } time_t now; time(&now); if (ac->start_count > 0) { //check if account need to unrestrict if (0 != ac->timeout && ac->start_count + ac->timeout <= now) { ac->timeout = 0; time(&ac->start_count); ac->info.field_restrict = NO_PERIOD; ac->info.field_level_restrict = NORESTRICT_PARAM; if (!check_restrict(ac)) account_unrestrict(ac); else { sprintf(tmp_buf, "No unrestrict yet for %s %d %d\n", ac->id, ac->timeout, ac->start_count); WRITE_LOG( NULL, 1, tmp_buf, _DBGOVERNOR_BUFFER_8192, tmp_buf, data_cfg.log_mode); } //} else if (ac->start_count + data_cfg.timeout <= now) { } else if (((ac->start_count + data_cfg.timeout) <= now) && (!ac->timeout)) { //Check if penalty period ended ac->start_count = 0; ac->restricted = 0; ac->info.field_restrict = NO_PERIOD; ac->info.field_level_restrict = NORESTRICT_PARAM2; ac->timeout = 0; sprintf(tmp_buf, "Penalty period is over %s %d %d\n", ac->id, ac->timeout, ac->start_count); WRITE_LOG( NULL, 1, tmp_buf, _DBGOVERNOR_BUFFER_8192, tmp_buf, data_cfg.log_mode); } } } }
int check_mysql_version(MODE_TYPE debug_mode) { MYSQL_RES *res; MYSQL_ROW row; unsigned long *lengths; char buffer[_DBGOVERNOR_BUFFER_2048], outbuffer[_DBGOVERNOR_BUFFER_2048]; struct governor_config data_cfg; get_config_data( &data_cfg ); if (mysql_send_governor != NULL) { if (db_mysql_exec_query(QUERY_GET_SERVER_INFO, mysql_send_governor, debug_mode)) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Get mysql vesrion request failed", data_cfg.log_mode); return 0; } res = (*_mysql_store_result)(mysql_send_governor); row = (*_mysql_fetch_row)(res); if (row) { lengths = (*_mysql_fetch_lengths)(res); db_mysql_get_string(buffer, row[0], lengths[0]); if (strstr(buffer, "-cll-lve")) { sprintf(outbuffer, "MySQL version correct %s", buffer); WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, outbuffer, data_cfg.log_mode); (*_mysql_free_result)(res); return 1; } else { sprintf( outbuffer, "Update your MySQL to CLL version from repo.cloudlinux.com. Current is %s", buffer); WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, outbuffer, data_cfg.log_mode); (*_mysql_free_result)(res); return 0; } } else { (*_mysql_free_result)(res); WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Unknown mysql version", data_cfg.log_mode); return 0; } } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Connection to db is absent", data_cfg.log_mode); return 0; } }
void send_commands(Command * cmd, void *data) { char buffer[_DBGOVERNOR_BUFFER_2048]; struct governor_config data_cfg; get_config_data( &data_cfg ); if (cmd) { switch (cmd->command) { case FREEZE: { if (data_cfg.use_lve) { if (add_user_to_list(cmd->username, data_cfg.all_lve) < 0) { if (data_cfg.log_mode == DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't add user to BAD list %s", data_cfg.log_mode, cmd->username); } } else { if(data_cfg.max_user_connections) { update_user_limit_no_flush(cmd->username, (unsigned int) data_cfg.max_user_connections, data_cfg.log_mode); is_any_flush = 1; } } } /*else update_user_limit(cmd->username, (unsigned int) -1, data_cfg.log_mode); if(data_cfg.killuser==1) kill_connection(cmd->username, data_cfg.log_mode);*/ //lve_connection(cmd->username, data_cfg.log_mode); if( data_cfg.logqueries_use == 1 ) log_user_queries( cmd->username, data_cfg.log_mode ); } break; case UNFREEZE: { if (data_cfg.use_lve) { if (delete_user_from_list(cmd->username) < 0) { if(data_cfg.log_mode == DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't delete user form BAD list %s", data_cfg.log_mode, cmd->username); } } if(data_cfg.max_user_connections) { update_user_limit_no_flush(cmd->username, (unsigned int) 0, data_cfg.log_mode); is_any_flush = 1; } //kill_connection(cmd->username, data_cfg.log_mode); } else { if(data_cfg.max_user_connections) { update_user_limit_no_flush(cmd->username, 0, data_cfg.log_mode); is_any_flush = 1; } } } break; } } }
void log_user_queries( char *user_name, MODE_TYPE debug_mode ) { char buffer[ _DBGOVERNOR_BUFFER_8192 ]; char sql_buffer[ _DBGOVERNOR_BUFFER_8192 ]; char user_name_alloc[ USERNAMEMAXLEN * 2 ]; char file_name[ USERNAMEMAXLEN + 1 + 10 ]; unsigned long counts; unsigned long *lengths; MYSQL_RES *res; MYSQL_ROW row; FILE *log_queries; const time_t timestamp = time( NULL ); struct governor_config data_cfg; get_config_data( &data_cfg ); (*_mysql_real_escape_string)( mysql_do_command, user_name_alloc, user_name, strlen( user_name ) ); snprintf( sql_buffer, _DBGOVERNOR_BUFFER_8192, QUERY_GET_PROCESSLIST_INFO ); if( db_mysql_exec_query( sql_buffer, mysql_do_command, debug_mode ) ) { WRITE_LOG( NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Get show processlist failed", data_cfg.log_mode ); return; } res = (*_mysql_store_result)( mysql_do_command ); counts = (*_mysql_num_rows)( res ); int tek_r = 0; if( create_dir() && counts > 0 ) { snprintf( file_name, USERNAMEMAXLEN + 1 + 10, "%s.%d", user_name, timestamp ); log_queries = fopen( file_name, "w" ); while( ( row = (*_mysql_fetch_row)( res ) ) ) { if( row ) { if( strcmp( row[ 1 ], user_name ) == 0 ) { lengths = (*_mysql_fetch_lengths)( res ); db_mysql_get_string( buffer, row[ 7 ], lengths[ 7 ] ); fprintf( log_queries, "%s\n", buffer ); } } else { (*_mysql_free_result)( res ); WRITE_LOG( NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "No queries retrieved", data_cfg.log_mode ); fclose( log_queries ); return; } } fclose( log_queries ); } (*_mysql_free_result)( res ); }
void *send_commands_cycle_in(void *data) { struct governor_config data_cfg; get_config_data( &data_cfg ); is_any_flush = 0; g_list_foreach(command_list_send, (GFunc) send_commands, NULL); if(data_cfg.max_user_connections && is_any_flush) flush_user_priv(data_cfg.log_mode); is_any_flush = 0; is_send_command_cycle = 0; return NULL; }
/****************************************************************************** * * * Function: process_nodes * * * * Purpose: calculates checksums of config data * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ void process_nodes() { DB_RESULT result; DB_ROW row; int nodeid; int master_nodeid; char *data, *answer; zbx_sock_t sock; int res; int sender_nodeid; master_nodeid = CONFIG_MASTER_NODEID; if (0 == master_nodeid) return; result = DBselect("select nodeid from nodes"); while (NULL != (row=DBfetch(result))) { nodeid = atoi(row[0]); if (SUCCEED == is_master_node(CONFIG_NODEID, nodeid)) continue; node_sync_lock(nodeid); /* DBbegin();*/ res = calculate_checksums(nodeid, NULL, 0); if (SUCCEED == res && NULL != (data = get_config_data(nodeid, ZBX_NODE_MASTER))) { zabbix_log( LOG_LEVEL_WARNING, "NODE %d: Sending configuration changes to master node %d for node %d datalen %d", CONFIG_NODEID, master_nodeid, nodeid, strlen(data)); if (SUCCEED == (res = connect_to_node(master_nodeid, &sock))) { if (SUCCEED == res) res = send_data_to_node(master_nodeid, &sock, data); if (SUCCEED == res) res = recv_data_from_node(master_nodeid, &sock, &answer); if (SUCCEED == res && 0 == strncmp(answer, "Data", 4)) { res = update_checksums(nodeid, ZBX_NODE_MASTER, SUCCEED, NULL, 0, NULL); if (SUCCEED == res) res = node_sync(answer, &sender_nodeid, &nodeid); send_data_to_node(master_nodeid, &sock, SUCCEED == res ? "OK" : "FAIL"); } disconnect_node(&sock); } zbx_free(data); } /* DBcommit();*/ node_sync_unlock(nodeid); } DBfree_result(result); }
/* * Do menu action after shutting the menu and allowing time for the animations to complete */ static void do_menu_action(void *data) { get_config_data()->fromhr = preset_data.fromhr[selected_row]; get_config_data()->frommin = preset_data.frommin[selected_row]; get_config_data()->tohr = preset_data.tohr[selected_row]; get_config_data()->tomin = preset_data.tomin[selected_row]; get_config_data()->from = preset_data.from[selected_row]; get_config_data()->to = preset_data.to[selected_row]; get_config_data()->smart = true; resend_all_data(true); // Force resend - we've fiddled with the times trigger_config_save(); set_smart_status(); }
/* * Do menu action after shutting the menu and allowing time for the animations to complete */ static void do_long_menu_action(void *data) { preset_data.fromhr[selected_row] = get_config_data()->fromhr; preset_data.frommin[selected_row] = get_config_data()->frommin; preset_data.tohr[selected_row] = get_config_data()->tohr; preset_data.tomin[selected_row] = get_config_data()->tomin; preset_data.from[selected_row] = get_config_data()->from; preset_data.to[selected_row] = get_config_data()->to; save_preset_data(); }
/* * Set the fields to the values from the rest of the system */ static void set_values() { values[F_FROM_HOUR] = twenty_four_to_twelve(get_config_data()->fromhr); values[F_FROM_MINUTE] = get_config_data()->frommin; values[F_TO_HOUR] = twenty_four_to_twelve(get_config_data()->tohr); values[F_TO_MINUTE] = get_config_data()->tomin; if (!is24hr) { values[F_FROM_AMPM] = get_config_data()->fromhr >= 12 ? 1 : 0; values[F_TO_AMPM] = get_config_data()->tohr >= 12 ? 1 : 0; } }
void governor_enable_reconn_lve(MODE_TYPE debug_mode) { char buffer[_DBGOVERNOR_BUFFER_2048]; MYSQL_RES *res; struct governor_config data_cfg; get_config_data( &data_cfg ); if (db_mysql_exec_query(QUERY_GOVERNOR_MODE_ENABLE_RECON_LVE, mysql_send_governor, debug_mode)) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't execute sql request. ENABLE_GOVERNOR", data_cfg.log_mode); } res = (*_mysql_store_result)(mysql_send_governor); (*_mysql_free_result)(res); }
void send_commands_cycle() { struct governor_config data_cfg; get_config_data( &data_cfg ); if( data_cfg.is_gpl ) return; int ret; pthread_t thread; if (!is_send_command_cycle) { is_send_command_cycle = 1; send_command_copy_list(); if(g_list_length(command_list_send)>0){ pthread_create(&thread, NULL, send_commands_cycle_in, NULL); pthread_detach(thread); } else { is_send_command_cycle = 0; } } }
//Exec query, if error occurred - try again EXEC_QUERY_TRIES times int db_mysql_exec_query(const char *query, MYSQL * mysql_internal, MODE_TYPE debug_mode) { char buf[_DBGOVERNOR_BUFFER_2048]; struct governor_config data_cfg; get_config_data( &data_cfg ); //Проверим наличие соединения, а вдруг пропало if ((*_mysql_ping)(mysql_internal)) { //База действительно ушла прочь, что даже реконнект не помог char *unm = NULL; char *upwd = NULL; if(global_user_name[0]) unm = global_user_name; if(global_user_password[0]) upwd = global_user_password; my_bool reconnect = 1; //Авторекоонет - подключить (*_mysql_options)(mysql_internal, MYSQL_OPT_RECONNECT, &reconnect); //Еще разок соединимся if (!(*_mysql_real_connect)(mysql_internal, global_host, unm, upwd, global_db_name, 0, _unix_socket_addres, 0)) { return -1; } } int execution_counters = EXEC_QUERY_TRIES; while ((*_mysql_query)(mysql_internal, query)) { execution_counters--; if (execution_counters == 0) { if (debug_mode != DEBUG_MODE) { WRITE_LOG(NULL, 0, buf, _DBGOVERNOR_BUFFER_2048, db_getlasterror (mysql_internal), data_cfg.log_mode); } else { WRITE_LOG(NULL, 0, buf, _DBGOVERNOR_BUFFER_2048, "%s --- Request: %s", data_cfg.log_mode, db_getlasterror (mysql_internal), query); } return -1; } } return 0; }
void *send_governor(void *data) { struct governor_config data_cfg; get_config_data( &data_cfg ); for (;;) { if( !data_cfg.is_gpl ) { if (data_cfg.use_lve) governor_enable_lve(data_cfg.log_mode); else governor_enable(data_cfg.log_mode); } else governor_enable(data_cfg.log_mode); sleep(60); } return NULL; }
//Save all privileges to base void flush_user_priv(MODE_TYPE debug_mode) { char buffer[_DBGOVERNOR_BUFFER_2048]; MYSQL_RES *res; struct governor_config data_cfg; get_config_data( &data_cfg ); if (db_mysql_exec_query(QUERY_FLUSH_USER_PRIV, mysql_do_command, debug_mode)) { if (debug_mode != DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't flush user privs", data_cfg.log_mode); } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "%s --- Request: %s", data_cfg.log_mode, "Can't flush user privs", QUERY_FLUSH_USER_PRIV); } } res = (*_mysql_store_result)(mysql_do_command); (*_mysql_free_result)(res); }
void proceed_accounts(double tm) { struct governor_config data_cfg; get_config_data( &data_cfg ); send_to_glib_info snd = { data_cfg.debug_user, tm, data_cfg.log_mode }; increment_tick(); pthread_mutex_lock(&mtx_account); pthread_mutex_lock(&mtx_counters); g_hash_table_foreach(get_counters_table(), (GHFunc) add_user_stats_from_counter, (gpointer)&snd); pthread_mutex_unlock(&mtx_counters); g_hash_table_foreach(users, (GHFunc) tick_empty_users, NULL); g_hash_table_foreach(accounts, (GHFunc) calc_acc_stats, (gpointer)&snd); if(data_cfg.restrict_mode){ g_hash_table_foreach(accounts, (GHFunc) account_analyze_limit, NULL); } else { g_hash_table_foreach(accounts, (GHFunc) account_analyze, NULL); } if( data_cfg.statistic_mode ) old_tm_stats = AddDbGovStatitrics( old_tm_stats ); pthread_mutex_unlock(&mtx_account); }
int db_connect(const char *host, const char *user_name, const char *user_password, const char *db_name, int argc, char *argv[], MODE_TYPE debug_mode) { char buffer[_DBGOVERNOR_BUFFER_2048]; struct governor_config data_cfg; get_config_data( &data_cfg ); //Обнулим глобальные переменные доступа strcpy(global_user_name, ""); strcpy(global_host, ""); strcpy(global_user_password, ""); strcpy(global_db_name, ""); WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Open govern connection operation", data_cfg.log_mode); if (db_connect_common(&mysql_send_governor, host, user_name, user_password, db_name, debug_mode, argc, argv, 1) < 0) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Send governor connection error", data_cfg.log_mode); return -1; } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Open write connection operation", data_cfg.log_mode); if (db_connect_common(&mysql_do_command, host, user_name, user_password, db_name, debug_mode, argc, argv, 0) < 0) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Write connection error", data_cfg.log_mode); return -1; } } return 0; }
void kill_query_by_id(long id, MODE_TYPE debug_mode) { char buffer[_DBGOVERNOR_BUFFER_2048]; char sql_buffer[_DBGOVERNOR_BUFFER_8192]; MYSQL_RES *res; struct governor_config data_cfg; get_config_data( &data_cfg ); snprintf(sql_buffer, _DBGOVERNOR_BUFFER_8192, QUERY_KILL_USER_QUERY_ID, id); if (db_mysql_exec_query(sql_buffer, mysql_do_command, debug_mode)) { if (debug_mode != DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't execute sql request. Kill query", data_cfg.log_mode); } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "%s --- Request: %s", data_cfg.log_mode, "Can't execute sql request. Kill query", sql_buffer); } } res = (*_mysql_store_result)(mysql_do_command); (*_mysql_free_result)(res); }
int db_connect_common(MYSQL ** internal_db, const char *host, const char *user_name, const char *user_password, const char *db_name, MODE_TYPE debug_mode, int argc, char *argv[], int save_global) { const char *groups_client[] = { "client", "mysqld", "dbgovernor", NULL }; //const char *groups_server[] = { "mysqld", "client", NULL }; int i, option_index = 0; char c; char *hst = NULL; char *user = NULL; char *password = NULL; char buf[_DBGOVERNOR_BUFFER_512]; int db_connection_method = 0; my_bool reconnect = 1; struct governor_config data_cfg; get_config_data( &data_cfg ); //Prepare local options name for default access with name and password from my.cnf, .my.cnf struct option long_options[] = { { "host", optional_argument, NULL, 'r' }, { "user", optional_argument, NULL, 'g' }, { "password", optional_argument, NULL, 'j' }, { "pass", optional_argument, NULL, 'i' }, { "socket", optional_argument, NULL, 'S' }, { 0, 0, 0, 0 } }; if (*internal_db != NULL) { //Already connected WRITE_LOG(NULL, 0, buf, _DBGOVERNOR_BUFFER_512, "Connection already exists", data_cfg.log_mode); return -1; } (*_my_init)(); /* * Здесь мы читаем my.cnf и .my.cnf * Это читает сам mysql, это его родное API */ (*_load_defaults)("my", groups_client, &argc, &argv); opterr = 0; optind = 0; //Parse argc, argv modified by _load_defaults while ((c = getopt_long(argc, argv, ":S:u:p:h:s:m:l:c:", long_options, &option_index)) != EOF) { switch (c) { case 'S': _unix_socket_addres = optarg; break; case 'r': hst = optarg; break; case 'g': user = optarg; break; case 'j': password = optarg; break; case 'i': password = optarg; break; default: continue; } } *internal_db = (*_mysql_init)(NULL); if (*internal_db == NULL) { WRITE_LOG(NULL, 0, buf, _DBGOVERNOR_BUFFER_512, "Can't init mysql structure", data_cfg.log_mode); return -1; } WRITE_LOG(NULL, 0, buf, _DBGOVERNOR_BUFFER_512, "Try to connect with options from dbgovernor config file", data_cfg.log_mode); strlcpy(work_user, "", USERNAMEMAXLEN); if (user_name) strlcpy(work_user, user_name, USERNAMEMAXLEN); (*_mysql_options)(*internal_db, MYSQL_OPT_RECONNECT, &reconnect); //Try to connect with options from goernor's config if (!(*_mysql_real_connect)(*internal_db, host, user_name, user_password, db_name, 0, _unix_socket_addres, 0)) { //Previous attempt failed, try with data from my.cnf, .my.cnf WRITE_LOG(NULL, 0, buf, _DBGOVERNOR_BUFFER_512, "Try to connect with no password under root", data_cfg.log_mode); /*opterr = 0; optind = 0; while ((c = getopt_long(argc, argv, ":u:p:h:s:m:l:c:", long_options, &option_index)) != EOF) { switch (c) { case 'r': hst = optarg; break; case 'g': user = optarg; break; case 'j': password = optarg; break; case 'i': password = optarg; break; default: continue; } }*/ //Try to connect again if (user) strlcpy(work_user, user, USERNAMEMAXLEN); if (!(*_mysql_real_connect)(*internal_db, host, user, password, db_name, 0, _unix_socket_addres, 0)) { //Error again, stop to try WRITE_LOG(NULL, 0, buf, _DBGOVERNOR_BUFFER_512, db_getlasterror (*internal_db), data_cfg.log_mode); return -1; } else { //Сохраним праматеры с которыми успешно соединились if (save_global) { strlcpy(global_user_name, (user?user:""), _DBGOVERNOR_BUFFER_128); strlcpy(global_host, (host?host:""), _DBGOVERNOR_BUFFER_512); strlcpy(global_user_password, (password?password:""), _DBGOVERNOR_BUFFER_256); strlcpy(global_db_name, (db_name?db_name:""), _DBGOVERNOR_BUFFER_512); } } } else { //Сохраним праматеры с которыми успешно соединились if (save_global) { strlcpy(global_user_name, (user_name?user_name:""), _DBGOVERNOR_BUFFER_128); strlcpy(global_host, (host?host:""), _DBGOVERNOR_BUFFER_512); strlcpy(global_user_password, (user_password?user_password:""), _DBGOVERNOR_BUFFER_256); strlcpy(global_db_name, (db_name?db_name:""), _DBGOVERNOR_BUFFER_512); } } return 0; }
/** MAIN - Dyn DNS update entry point Actions: - read the configuration options - perform various init actions as specified in the options - create and init dyn_dns object. - launch the IP update action loop */ int dyn_dns_main(DYN_DNS_CLIENT *p_dyndns, int argc, char* argv[]) { FILE *fp; RC_TYPE rc = RC_OK; int i, s; int iterations_err = 0; char name[DYNDNS_SERVER_NAME_LENGTH]; if (p_dyndns == NULL) { return RC_INVALID_POINTER; } /* Create pid and cache file repository. */ mkdir(DYNDNS_RUNTIME_DATA_DIR, 0755); /* read cmd line options and set object properties */ rc = get_config_data(p_dyndns, argc, argv); if (rc != RC_OK || p_dyndns->abort) { return rc; } if (p_dyndns->change_persona) { OS_USER_INFO os_usr_info; memset(&os_usr_info, 0, sizeof(os_usr_info)); os_usr_info.gid = p_dyndns->sys_usr_info.gid; os_usr_info.uid = p_dyndns->sys_usr_info.uid; rc = os_change_persona(&os_usr_info); if (rc != RC_OK) { return rc; } } /* if logfile provided, redirect output to log file */ if (strlen(p_dyndns->dbg.p_logfilename) != 0) { rc = os_open_dbg_output(DBG_FILE_LOG, "", p_dyndns->dbg.p_logfilename); if (rc != RC_OK) { return rc; } } if (p_dyndns->debug_to_syslog == TRUE || (p_dyndns->run_in_background == TRUE)) { if (get_dbg_dest() == DBG_STD_LOG) /* avoid file and syslog output */ { rc = os_open_dbg_output(DBG_SYS_LOG, "inadyn", NULL); if (rc != RC_OK) { return rc; } } } /* if silent required, close console window */ if (p_dyndns->run_in_background == TRUE) { rc = close_console_window(); if (rc != RC_OK) { return rc; } if (get_dbg_dest() == DBG_SYS_LOG) { fclose(stdout); } } /* Create files with permissions 0644 */ umask(S_IWGRP | S_IWOTH); /* write pid file */ fp = fopen(p_dyndns->pidfile, "w"); if (!fp) { logit(LOG_ERR, MODULE_TAG "Failed opening pidfile %s for writing: %s", p_dyndns->pidfile, strerror(errno)); return RC_ERROR; } fprintf(fp, "%u", getpid()); fclose(fp); dyn_dns_print_hello(); /* At boot, or when restarting inadyn at runtime, the memory struct holding * our current IP# is empty. We want to avoid unnecessary updates of our * DDNS server record, since we might get locked out for abuse, so we "seed" * each of the DDNS records of our struct with the cached IP# from our cache * file, or from a regular DNS query. */ fp = fopen(p_dyndns->cachefile, "r"); if (!fp) { struct addrinfo hints; struct addrinfo *result; /* Clear DNS cache before querying for the IP below. */ res_init(); /* Try a DNS lookup of our last known IP#. */ for (i = 0; i < p_dyndns->info_count; i++) { if (p_dyndns->info[i].alias_count && /* exception for tunnelbroker.net - no name to lookup */ strcmp(p_dyndns->info[i].p_dns_system->p_key, "*****@*****.**")) { /* DNS Lookup */ memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_INET; /* IPv4 */ hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ hints.ai_flags = 0; hints.ai_protocol = 0; /* Any protocol */ if (!(s = getaddrinfo(p_dyndns->info[i].alias_info[0].names.name, NULL, &hints, &result))) { /* DNS reply for alias found, convert to IP# */ if (!getnameinfo(result->ai_addr, result->ai_addrlen, name, sizeof(name), NULL, 0, NI_NUMERICHOST)) { /* Update local record for next checkip call. */ strncpy(p_dyndns->info[i].my_ip_address.name, name, sizeof(p_dyndns->info[i].my_ip_address.name)); logit(LOG_INFO, MODULE_TAG "Resolving hostname %s => IP# %s", p_dyndns->info[i].alias_info[0].names.name, name); } freeaddrinfo(result); } else logit(LOG_WARNING, MODULE_TAG "Failed resolving hostname %s: %s", p_dyndns->info[i].alias_info[0].names.name, gai_strerror(s)); } } } else { /* Read cached IP# from inadyn cache file. */ if (fgets(name, sizeof(name), fp)) { logit(LOG_INFO, MODULE_TAG "Cached IP# %s from previous invocation.", name); /* Update local record for next checkip call. */ for (i = 0; i < p_dyndns->info_count; i++) { strncpy(p_dyndns->info[i].my_ip_address.name, name, sizeof(p_dyndns->info[i].my_ip_address.name)); } } fclose(fp); } do { rc = dyn_dns_init(p_dyndns); if (rc != RC_OK) { break; } rc = get_encoded_user_passwd(p_dyndns); if (rc != RC_OK) { break; } /* DDNS client main loop */ while (1) { rc = dyn_dns_update_ip(p_dyndns); if (rc != RC_OK) { if (p_dyndns->cmd == CMD_RESTART) { logit(LOG_DEBUG, "RESTART command received. Restarting."); rc = RC_RESTART; break; } if (rc == RC_DYNDNS_RSP_NOTOK && p_dyndns->total_iterations == 1) { logit(LOG_ERR, MODULE_TAG "Error response from DDNS server, exiting!"); break; } iterations_err++; } else { /* count only the successful iterations */ p_dyndns->num_iterations++; iterations_err = 0; } /* check if the user wants us to stop */ if (p_dyndns->total_iterations != 0 && p_dyndns->num_iterations >= p_dyndns->total_iterations) { break; } if (p_dyndns->total_iterations != 1) { if (rc == RC_DYNDNS_RSP_RETRY_LATER || rc == RC_DYNDNS_RSP_NOTOK) p_dyndns->sleep_sec = p_dyndns->error_update_period_sec; else p_dyndns->sleep_sec = p_dyndns->normal_update_period_sec; } else p_dyndns->sleep_sec = DYNDNS_MIN_SLEEP; if (rc != RC_OK) { /* dyn_dns_update_ip() failed above, and we've not reached MAX iterations. * Time to inform the user the (network) error is not fatal and that we * will try again in a short while. */ logit(LOG_WARNING, MODULE_TAG "Will retry again in %d sec...", p_dyndns->sleep_sec); } /* Now sleep a while. Using the time set in sleep_sec data member */ dyn_dns_wait_for_cmd(p_dyndns); if (p_dyndns->cmd == CMD_STOP) { logit(LOG_DEBUG, MODULE_TAG "STOP command received, exiting."); rc = RC_OK; break; } else if (p_dyndns->cmd == CMD_RESTART) { if (p_dyndns->dbg.level > 0) logit(LOG_DEBUG, "RESTART command received, restarting."); rc = RC_RESTART; break; } if (p_dyndns->total_iterations > 0 && iterations_err > p_dyndns->total_iterations) { rc = RC_OK; break; } if (p_dyndns->dbg.level > 0) { logit(LOG_DEBUG, "."); // logit(LOG_DEBUG, "Time since last update: %d", p_dyndns->time_since_last_update); } p_dyndns->time_since_last_update += p_dyndns->sleep_sec; } } while (0); /* if everything ok here we should exit. End of program */ if (rc == RC_OK) { rc = dyn_dns_shutdown(p_dyndns); } return rc; }
/* * Drop values back into the rest of the system */ static void return_values() { get_config_data()->smart = true; get_config_data()->fromhr = hour_from_fields(values[F_FROM_HOUR],values[F_FROM_AMPM]); get_config_data()->frommin = values[F_FROM_MINUTE]; get_config_data()->tohr = hour_from_fields(values[F_TO_HOUR],values[F_TO_AMPM]); get_config_data()->tomin = values[F_TO_MINUTE]; get_config_data()->from = to_mins(get_config_data()->fromhr, get_config_data()->frommin); get_config_data()->to = to_mins(get_config_data()->tohr, get_config_data()->tomin); resend_all_data(true); // Force resend - we've fiddled with the times trigger_config_save(); set_smart_status(); }