Пример #1
0
MysqlThreadData::~MysqlThreadData()
{
	mysql_thread_end();
}
Пример #2
0
				~Thread () noexcept {
				
					mysql_thread_end();
				
				}
Пример #3
0
/**
 * The entry point for the monitoring module thread
 *
 * @param arg	The handle of the monitor
 */
static void
monitorMain(void *arg)
{
    MONITOR* mon = (MONITOR*)arg;
GALERA_MONITOR		*handle;
MONITOR_SERVERS		*ptr;
size_t			nrounds = 0;
MONITOR_SERVERS		*candidate_master = NULL;
int			master_stickiness;
int			is_cluster=0;
int			log_no_members = 1;
monitor_event_t evtype;

    spinlock_acquire(&mon->lock);
    handle = (GALERA_MONITOR *)mon->handle;
    spinlock_release(&mon->lock);
    master_stickiness = handle->disableMasterFailback;
	if (mysql_thread_init())
	{
                LOGIF(LE, (skygw_log_write_flush(
                                   LOGFILE_ERROR,
                                   "Fatal : mysql_thread_init failed in monitor "
                                   "module. Exiting.\n")));
                return;
	}                         
	handle->status = MONITOR_RUNNING;
	
	while (1)
	{
		if (handle->shutdown)
		{
			handle->status = MONITOR_STOPPING;
			mysql_thread_end();
			handle->status = MONITOR_STOPPED;
			return;
		}
		/** Wait base interval */
		thread_millisleep(MON_BASE_INTERVAL_MS);
		/** 
		 * Calculate how far away the monitor interval is from its full 
		 * cycle and if monitor interval time further than the base 
		 * interval, then skip monitoring checks. Excluding the first
		 * round.
		 */ 

		if (nrounds != 0 && ((nrounds*MON_BASE_INTERVAL_MS)%mon->interval) >= MON_BASE_INTERVAL_MS)
		{
			nrounds += 1;
			continue;
		}

		nrounds += 1;

		/* reset cluster members counter */
		is_cluster=0;

		ptr = mon->databases;

		while (ptr)
		{
			ptr->mon_prev_status = ptr->server->status;

			monitorDatabase(mon, ptr);

			/* clear bits for non member nodes */
			if ( ! SERVER_IN_MAINT(ptr->server) && (ptr->server->node_id < 0 || ! SERVER_IS_JOINED(ptr->server))) {
				ptr->server->depth = -1;

				/* clear M/S status */
				server_clear_status(ptr->server, SERVER_SLAVE);
                		server_clear_status(ptr->server, SERVER_MASTER);
				
				/* clear master sticky status */
				server_clear_status(ptr->server, SERVER_MASTER_STICKINESS);
			}

			/* Log server status change */
			if (mon_status_changed(ptr))
			{
				LOGIF(LD, (skygw_log_write_flush(
					LOGFILE_DEBUG,
					"Backend server %s:%d state : %s",
					ptr->server->name,
					ptr->server->port,
					STRSRVSTATUS(ptr->server))));
			}
                        
                        if (!(SERVER_IS_RUNNING(ptr->server)) || 
                            !(SERVER_IS_IN_CLUSTER(ptr->server)))
                        {
					dcb_call_foreach(ptr->server,DCB_REASON_NOT_RESPONDING);
                        }

			if (SERVER_IS_DOWN(ptr->server))
			{
				/** Increase this server'e error count */
				dcb_call_foreach(ptr->server,DCB_REASON_NOT_RESPONDING);
				ptr->mon_err_count += 1;

			}
			else
			{
				/** Reset this server's error count */
				ptr->mon_err_count = 0;
			}

			ptr = ptr->next;
		}

		/*
		 * Let's select a master server:
		 * it could be the candidate master following MIN(node_id) rule or
		 * the server that was master in the previous monitor polling cycle
		 * Decision depends on master_stickiness value set in configuration
		 */

		/* get the candidate master, following MIN(node_id) rule */
		candidate_master = get_candidate_master(mon->databases);

		/* Select the master, based on master_stickiness */
                if (1 == handle->disableMasterRoleSetting) {
                    handle->master = NULL;
                }
                else {
                    handle->master = set_cluster_master(handle->master, candidate_master, master_stickiness);
                }

		ptr = mon->databases;

		while (ptr) {
			if (!SERVER_IS_JOINED(ptr->server) || SERVER_IN_MAINT(ptr->server)) {
				ptr = ptr->next;
				continue;
			}

                        if (handle->master) {
                            if (ptr != handle->master) {
				/* set the Slave role */
				server_set_status(ptr->server, SERVER_SLAVE);
				server_clear_status(ptr->server, SERVER_MASTER);

				/* clear master stickiness */
				server_clear_status(ptr->server, SERVER_MASTER_STICKINESS);
                            } else {
				/* set the Master role */
				server_set_status(handle->master->server, SERVER_MASTER);
				server_clear_status(handle->master->server, SERVER_SLAVE);

				if (candidate_master && handle->master->server->node_id != candidate_master->server->node_id) {
					/* set master stickiness */
					server_set_status(handle->master->server, SERVER_MASTER_STICKINESS);
				} else {
					/* clear master stickiness */
					server_clear_status(ptr->server, SERVER_MASTER_STICKINESS);
				}			
                            }
                        }

			is_cluster++;

			ptr = ptr->next;
		}

		if (is_cluster == 0 && log_no_members) {
			LOGIF(LE, (skygw_log_write_flush(
					LOGFILE_ERROR,
					"Error: there are no cluster members")));
			log_no_members = 0;
		} else {
			if (is_cluster > 0 && log_no_members == 0) {
				LOGIF(LE, (skygw_log_write_flush(
					LOGFILE_ERROR,
					"Info: found cluster members")));
				log_no_members = 1;
			}
		}


		ptr = mon->databases;

		while(ptr)
		{

		    /** Execute monitor script if a server state has changed */
		    if(mon_status_changed(ptr))
		    {
			evtype = mon_get_event_type(ptr);
			if(isGaleraEvent(evtype))
			{
			    skygw_log_write(LOGFILE_TRACE,"Server changed state: %s[%s:%u]: %s",
				     ptr->server->unique_name,
				     ptr->server->name,ptr->server->port,
				     mon_get_event_name(ptr));
			    if(handle->script && handle->events[evtype])
			    {
				monitor_launch_script(mon,ptr,handle->script);
			    }
			}
		    }
		    ptr = ptr->next;
		}
	}
}
Пример #4
0
void DatabaseMysql::ThreadEnd()
{
    mysql_thread_end();
}
Пример #5
0
void CDatabase::EndDbThread()
{
	mysql_thread_end();
}
Пример #6
0
void *func(void *arg)
{
    ThreadArgs *args = (ThreadArgs *)arg;
    MYSQL_RES *result;
    MYSQL_ROW row;
    unsigned int rowCounter = 0;
    MYSQL_FIELD *field;
    unsigned int i;
    unsigned int timeout = 3000;
    const char *pStatement = "SELECT * FROM employees";
    mysql_thread_init();
    MYSQL *mysql = mysql_init(NULL);

    if (mysql == NULL)
    {
        printf("[%ld][%d]mysql init failed: %s\n", *args->thread_id, args->id, mysql_error(mysql));
        return (void *)0;
    }

    mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);

    if (mysql_real_connect(mysql, DBHOST, DBUSER, DBPASS, DBNAME, DBPORT, DBSOCK, DBPCNT) == NULL)
    {
        printf("[%ld][%d]connect failed: %s\n", *args->thread_id, args->id, mysql_error(mysql));
        mysql_close(mysql);
        mysql_thread_end();
        return (void *)0;
    }

    if (0 != mysql_real_query(mysql, pStatement, strlen(pStatement)))
    {
        printf("[%ld][%d]query failed: %s\n", *args->thread_id, args->id, mysql_error(mysql));
        mysql_close(mysql);
        mysql_thread_end();
        return (void *)0;
    }

    result = mysql_store_result(mysql);

    if (result == NULL)
    {
        printf("[%ld][%d]fetch result failed: %s\n", *args->thread_id, args->id, mysql_error(mysql));
        mysql_close(mysql);
        mysql_thread_end();
        return (void *)0;
    }

    printf("field name: ");
    while (NULL != (field = mysql_fetch_field(result)))
    {
        printf(" %s, ", field->name);
    }

    while (NULL != (row = mysql_fetch_row(result)))
    {
        rowCounter++;
        unsigned long *lengths;
        lengths = mysql_fetch_lengths(result);

    }
    printf("loop through result, total %d rows\n", rowCounter);

    mysql_free_result(result);
    mysql_close(mysql);
    mysql_thread_end();
    return (void *)0;
}
void QueryThread::exit()
{
  mysql_thread_end();
}
int hss_mysql_query_pdns(const char   *imsi,
                         mysql_pdn_t **pdns_p,
                         uint8_t      *nb_pdns)
{
    int          ret;
    MYSQL_RES   *res = NULL;
    MYSQL_ROW    row;
    char         query[255];
    mysql_pdn_t *pdn_array = NULL;

    if (db_desc->db_conn == NULL) {
        return EINVAL;
    }
    if (nb_pdns == NULL || pdns_p == NULL) {
        return EINVAL;
    }

    sprintf(query, "SELECT * FROM `pdn` WHERE "
                   "`pdn`.`users_imsi`=%s LIMIT 10; ", imsi);

    printf("Query: %s\n", query);

    pthread_mutex_lock(&db_desc->db_cs_mutex);

    if (mysql_query(db_desc->db_conn, query))
    {
        pthread_mutex_unlock(&db_desc->db_cs_mutex);
        fprintf(stderr, "Query execution failed: %s\n",
                mysql_error(db_desc->db_conn));
        mysql_thread_end();
        return EINVAL;
    }

    res = mysql_store_result(db_desc->db_conn);

    pthread_mutex_unlock(&db_desc->db_cs_mutex);

    *nb_pdns = 0;

    while ((row = mysql_fetch_row(res)) != NULL)
    {
        mysql_pdn_t *pdn_elm;   /* Local PDN element in array */
        unsigned long *lengths;

        lengths = mysql_fetch_lengths(res);

        *nb_pdns += 1;
        if (*nb_pdns == 1) {
            /* First row, must malloc */
            pdn_array = malloc(sizeof(mysql_pdn_t));
        } else {
            pdn_array = realloc(pdn_array, *nb_pdns * sizeof(mysql_pdn_t));
        }
        if (pdn_array == NULL) {
            /* Error on malloc */
            ret = ENOMEM;
            goto err;
        }
        pdn_elm = &pdn_array[*nb_pdns - 1];
        /* Copying the APN */
        memset(pdn_elm, 0, sizeof(mysql_pdn_t));
        memcpy(pdn_elm->apn, row[1], lengths[1]);
        /* PDN Type + PDN address */
        if (strcmp(row[2], "IPv6") == 0) {
            pdn_elm->pdn_type = IPV6;
            memcpy(pdn_elm->pdn_address.ipv6_address, row[4], lengths[4]);
            pdn_elm->pdn_address.ipv6_address[lengths[4]] = '\0';
        } else if (strcmp(row[2], "IPv4v6") == 0) {
            pdn_elm->pdn_type = IPV4V6;
            memcpy(pdn_elm->pdn_address.ipv4_address, row[3], lengths[3]);
            pdn_elm->pdn_address.ipv4_address[lengths[3]] = '\0';
            memcpy(pdn_elm->pdn_address.ipv6_address, row[4], lengths[4]);
            pdn_elm->pdn_address.ipv6_address[lengths[4]] = '\0';
        } else if (strcmp(row[2], "IPv4_or_IPv6") == 0) {
            pdn_elm->pdn_type = IPV4_OR_IPV6;
            memcpy(pdn_elm->pdn_address.ipv4_address, row[3], lengths[3]);
            pdn_elm->pdn_address.ipv4_address[lengths[3]] = '\0';
            memcpy(pdn_elm->pdn_address.ipv6_address, row[4], lengths[4]);
            pdn_elm->pdn_address.ipv6_address[lengths[4]] = '\0';
        } else {
            pdn_elm->pdn_type = IPV4;
            memcpy(pdn_elm->pdn_address.ipv4_address, row[3], lengths[3]);
            pdn_elm->pdn_address.ipv4_address[lengths[3]] = '\0';
        }
        pdn_elm->aggr_ul = atoi(row[5]);
        pdn_elm->aggr_dl = atoi(row[6]);
        pdn_elm->qci     = atoi(row[9]);
        pdn_elm->priority_level = atoi(row[10]);
        if (strcmp(row[11], "ENABLED") == 0) {
            pdn_elm->pre_emp_cap = 0;
        } else {
            pdn_elm->pre_emp_cap = 1;
        }
        if (strcmp(row[12], "DISABLED") == 0) {
            pdn_elm->pre_emp_vul = 1;
        } else {
            pdn_elm->pre_emp_vul = 0;
        }
    }

    mysql_free_result(res);
    mysql_thread_end();

    /* We did not find any APN for the requested IMSI */
    if (*nb_pdns == 0) {
        return EINVAL;
    } else {
        *pdns_p = pdn_array;
        return 0;
    }

err:
    if (pdn_array) {
        free(pdn_array);
    }
    pdn_array = NULL;
    *pdns_p = pdn_array;
    *nb_pdns = 0;
    mysql_free_result(res);
    mysql_thread_end();
    return ret;
}
Пример #9
0
	void operator()()
	{
		mysql_thread_init();
		io_service_.run();
		mysql_thread_end();
	}
void *func(void *arg)
{
    ThreadArgs *args = (ThreadArgs *)arg;
    MYSQL_ROW row;
    unsigned int rowCounter = 0;
    MYSQL_FIELD *field;
    unsigned int i;
    unsigned int timeout = 3000;
    const char *pStatement = "SELECT * FROM employees";
    mysql_thread_init();
    MYSQL *mysql = mysql_init(NULL);
    MYSQL_STMT *stmt;
    MYSQL_BIND    bind[6];
    MYSQL_RES     *prepare_meta_result;
    short         small_data;
    int           int_data;
    char          str_data[STRING_SIZE];
    char          str_data2[STRING_SIZE];
    my_bool       is_null[6];
    my_bool       error[6];
    MYSQL_TIME    ts;
    unsigned long length[6];

    if (mysql == NULL)
    {
        printf("[%ld][%d]mysql init failed: %s\n", *args->thread_id, args->id, mysql_error(mysql));
        return (void *)0;
    }

    mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);

    if (mysql_real_connect(mysql, DBHOST, DBUSER, DBPASS, DBNAME, DBPORT, DBSOCK, DBPCNT) == NULL)
    {
        printf("[%ld][%d]connect failed: %s\n", *args->thread_id, args->id, mysql_error(mysql));
        mysql_close(mysql);
        mysql_thread_end();
        return (void *)0;
    }

    stmt = mysql_stmt_init(mysql);
    if (0 != mysql_stmt_prepare(stmt, pStatement, 23))
    {
        printf("[%ld][%d]prepare statement failed: %s\n", *args->thread_id, args->id, mysql_error(mysql));
        mysql_close(mysql);
        mysql_thread_end();
        return (void *)0;
    }

    prepare_meta_result = mysql_stmt_result_metadata(stmt);
    if (!prepare_meta_result)
    {
      printf( "mysql_stmt_result_metadata(), returned no meta information\n");
      printf(" %s\n", mysql_stmt_error(stmt));
      exit(0);
    }

    if (0 != mysql_stmt_execute(stmt))
    {
        printf("[%ld][%d]query failed: %s\n", *args->thread_id, args->id, mysql_error(mysql));
        mysql_close(mysql);
        mysql_thread_end();
        return (void *)0;
    }

    /* Bind the result buffers for all 4 columns before fetching them */

    memset(bind, 0, sizeof(bind));

    bind[0].buffer_type= MYSQL_TYPE_LONG;
    bind[0].buffer= (char *)&int_data;
    bind[0].is_null= &is_null[0];
    bind[0].length= &length[0];
    bind[0].error= &error[0];

    /* TIMESTAMP COLUMN */
    bind[1].buffer_type= MYSQL_TYPE_DATE;
    bind[1].buffer= (char *)&ts;
    bind[1].is_null= &is_null[1];
    bind[1].length= &length[1];
    bind[1].error= &error[1];

    /* STRING COLUMN */
    bind[2].buffer_type= MYSQL_TYPE_STRING;
    bind[2].buffer= (char *)str_data;
    bind[2].buffer_length= STRING_SIZE;
    bind[2].is_null= &is_null[2];
    bind[2].length= &length[2];
    bind[2].error= &error[2];

    /* STRING COLUMN */
    bind[3].buffer_type= MYSQL_TYPE_STRING;
    bind[3].buffer= (char *)str_data;
    bind[3].buffer_length= STRING_SIZE;
    bind[3].is_null= &is_null[3];
    bind[3].length= &length[3];
    bind[3].error= &error[3];

    /* STRING COLUMN */
    bind[4].buffer_type= MYSQL_TYPE_STRING;
    bind[4].buffer= (char *)str_data;
    bind[4].buffer_length= STRING_SIZE;
    bind[4].is_null= &is_null[4];
    bind[4].length= &length[4];
    bind[4].error= &error[4];

    /* TIMESTAMP COLUMN */
    bind[5].buffer_type= MYSQL_TYPE_DATE;
    bind[5].buffer= (char *)&ts;
    bind[5].is_null= &is_null[5];
    bind[5].length= &length[5];
    bind[5].error= &error[5];

    /* Bind the result buffers */
    if (mysql_stmt_bind_result(stmt, bind))
    {
      printf( " mysql_stmt_bind_result() failed\n");
      printf( " %s\n", mysql_stmt_error(stmt));
      exit(0);
    }
    mysql_stmt_store_result(stmt);

    printf("field name: ");
    while (NULL != (field = mysql_fetch_field(prepare_meta_result)))
    {
        printf(" %s, ", field->name);
    }

    while (!mysql_stmt_fetch(stmt))
    {
        rowCounter++;

    }
    printf("loop through result, total %d rows\n", rowCounter);

    mysql_free_result(prepare_meta_result);
    mysql_close(mysql);
    mysql_thread_end();
    return (void *)0;
}
Пример #11
0
/*
 * 子线程:用于定时给所有已连接设备发送广告信息
 *         仅在第一个连接建立时启动,
 * TODO: 包裹数据库函数,优化查询循环
 */
static void *pthr_adver_boardcast(void *arg)
{
    pthread_detach(pthread_self());
    int          connfd;
    char sendbuf[BUFFSIZE];

    MYSQL       *db_mysql;
    MYSQL_RES   *db_result;
    MYSQL_ROW    db_row;
    MYSQL_FIELD *db_field;

    unsigned int timeout = 3000;    /* mysqlopt timeout val */
    unsigned int num_fields;        /* fields numbers of query result */
    unsigned int i;                 /* int for loop */
    const char *pQuery_init = "SET NAMES UTF8";     /* set mysql for utf8 */
    const char *pQuery      = "SELECT * FROM `adverinfo`";  /* select table of advertise info */

    /* database init */
    mysql_thread_init();
    if ( NULL == (db_mysql = mysql_init(NULL))) {
        printf("mysql init error\n");
        return(NULL);
    }
    
//    mysql_options(db_mysql, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);

    if (!mysql_real_connect(db_mysql, DBHOST, DBUSER, DBPWD, 
                        DBNAME, 0, NULL, 0))
        handle_mysql_error("connect failed: %s\n", db_mysql);
    /* set mysql names for UTF8 */
    if (0 != mysql_real_query(db_mysql, pQuery_init, strlen(pQuery_init)))
        handle_mysql_error("utf8 init failded: %s\n", db_mysql);

    while(1) {  /* get adverinfo for loop */
        if (0 != mysql_real_query(db_mysql, pQuery, strlen(pQuery)))
            handle_mysql_error("query for adverinfo error: %s\n", db_mysql);
    
        if ( NULL == (db_result = mysql_store_result(db_mysql)))
            handle_mysql_error("fetch result failed: %s\n", db_mysql);

        while (NULL != (db_row = mysql_fetch_row(db_result)))
        {
            memset(sendbuf, 0, sizeof(sendbuf));
            sprintf(sendbuf, "Adver: %s\n", db_row[1]);
            for (i = 0; i <= maxi; i++)
            {
                connfd = client[i];    
                send(connfd, sendbuf, sizeof(sendbuf), 0);
            }
            sleep(2);
        }
        free(db_row);

        mysql_free_result(db_result);
    }
    mysql_close(db_mysql);
    
    mysql_thread_end();
    mysql_library_end();

    return(NULL);
}
Пример #12
0
void poll_host(int host_id) {
    char query1[BUFSIZE];
    char query2[BUFSIZE];
    char *query3;
    char query4[BUFSIZE];
    char errstr[512];
    int num_rows;
    int host_status;
    int assert_fail = 0;
    char *poll_result = NULL;
    char logmessage[LOGSIZE];
    char update_sql[BUFSIZE];

    reindex_t *reindex;
    target_t *entry;
    host_t *host;
    ping_t *ping;

    MYSQL mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;

    /* allocate host and ping structures with appropriate values */
    host = (host_t *) malloc(sizeof(host_t));
    ping = (ping_t *) malloc(sizeof(ping_t));

#ifndef OLD_MYSQL
    mysql_thread_init();
#endif

    snprintf(query1, sizeof(query1), "select action,hostname,snmp_community,snmp_version,snmp_username,snmp_password,rrd_name,rrd_path,arg1,arg2,arg3,local_data_id,rrd_num,snmp_port,snmp_timeout from poller_item where host_id=%i order by rrd_path,rrd_name", host_id);
    snprintf(query2, sizeof(query2), "select id, hostname,snmp_community,snmp_version,snmp_port,snmp_timeout,status,status_event_count,status_fail_date,status_rec_date,status_last_error,min_time,max_time,cur_time,avg_time,total_polls,failed_polls,availability from host where id=%i", host_id);
    snprintf(query4, sizeof(query4), "select data_query_id,action,op,assert_value,arg1 from poller_reindex where host_id=%i", host_id);

    db_connect(set.dbdb, &mysql);

    /* get data about this host */
    result = db_query(&mysql, query2);
    num_rows = (int)mysql_num_rows(result);

    if (num_rows != 1) {
        snprintf(logmessage, LOGSIZE, "Host[%i] ERROR: Unknown Host ID", host_id);
        cacti_log(logmessage);
        return;
    }

    row = mysql_fetch_row(result);

    /* populate host structure */
    host->id = atoi(row[0]);
    if (row[1] != NULL) snprintf(host->hostname, sizeof(host->hostname), "%s", row[1]);
    if (row[2] != NULL) snprintf(host->snmp_community, sizeof(host->snmp_community), "%s", row[2]);
    host->snmp_version = atoi(row[3]);
    host->snmp_port = atoi(row[4]);
    host->snmp_timeout = atoi(row[5]);
    if (row[6] != NULL) host->status = atoi(row[6]);
    host->status_event_count = atoi(row[7]);
    snprintf(host->status_fail_date, sizeof(host->status_fail_date), "%s", row[8]);
    snprintf(host->status_rec_date, sizeof(host->status_rec_date), "%s", row[9]);
    snprintf(host->status_last_error, sizeof(host->status_last_error), "%s", row[10]);
    host->min_time = atof(row[11]);
    host->max_time = atof(row[12]);
    host->cur_time = atof(row[13]);
    host->avg_time = atof(row[14]);
    host->total_polls = atoi(row[15]);
    host->failed_polls = atoi(row[16]);
    host->availability = atof(row[17]);

    host->ignore_host = 0;

    /* initialize SNMP */
    snmp_host_init(host);

    /* perform a check to see if the host is alive by polling it's SysDesc
     * if the host down from an snmp perspective, don't poll it.
     * function sets the ignore_host bit */
    if ((set.availability_method == AVAIL_SNMP) && (host->snmp_community == "")) {
        update_host_status(HOST_UP, host, ping, set.availability_method);

        if (set.verbose >= POLLER_VERBOSITY_MEDIUM) {
            snprintf(logmessage, LOGSIZE, "Host[%s] No host availability check possible for '%s'\n", host->id, host->hostname);
            cacti_log(logmessage);
        }
    } else {
        if (ping_host(host, ping) == HOST_UP) {
            update_host_status(HOST_UP, host, ping, set.availability_method);
        } else {
            host->ignore_host = 1;
            update_host_status(HOST_DOWN, host, ping, set.availability_method);
        }
    }

    /* update host table */
    snprintf(update_sql, sizeof(update_sql), "update host set status='%i',status_event_count='%i', status_fail_date='%s',status_rec_date='%s',status_last_error='%s',min_time='%f',max_time='%f',cur_time='%f',avg_time='%f',total_polls='%i',failed_polls='%i',availability='%.4f' where id='%i'\n",
             host->status,
             host->status_event_count,
             host->status_fail_date,
             host->status_rec_date,
             host->status_last_error,
             host->min_time,
             host->max_time,
             host->cur_time,
             host->avg_time,
             host->total_polls,
             host->failed_polls,
             host->availability,
             host->id);

    db_insert(&mysql, update_sql);

    /* do the reindex check for this host */
    if (!host->ignore_host) {
        reindex = (reindex_t *) malloc(sizeof(reindex_t));

        result = db_query(&mysql, query4);
        num_rows = (int)mysql_num_rows(result);

        if (num_rows > 0) {
            if (set.verbose == POLLER_VERBOSITY_DEBUG) {
                snprintf(logmessage, LOGSIZE, "Host[%i] RECACHE: Processing %i items in the auto reindex cache for '%s'\n", host->id, num_rows, host->hostname);
                cacti_log(logmessage);
            }

            while ((row = mysql_fetch_row(result))) {
                assert_fail = 0;

                reindex->data_query_id = atoi(row[0]);
                reindex->action = atoi(row[1]);
                if (row[2] != NULL) snprintf(reindex->op, sizeof(reindex->op), "%s", row[2]);
                if (row[3] != NULL) snprintf(reindex->assert_value, sizeof(reindex->assert_value), "%s", row[3]);
                if (row[4] != NULL) snprintf(reindex->arg1, sizeof(reindex->arg1), "%s", row[4]);

                switch(reindex->action) {
                case POLLER_ACTION_SNMP: /* snmp */
                    poll_result = snmp_get(host, reindex->arg1);
                    break;
                case POLLER_ACTION_SCRIPT: /* script (popen) */
                    poll_result = exec_poll(host, reindex->arg1);
                    break;
                }

                /* assume ok if host is up and result wasn't obtained */
                if (!strcmp(poll_result,"U")) {
                    assert_fail = 0;
                } else if ((!strcmp(reindex->op, "=")) && (strcmp(reindex->assert_value,poll_result) != 0)) {
                    snprintf(logmessage, LOGSIZE, "ASSERT: '%s=%s' failed. Recaching host '%s', data query #%i\n", reindex->assert_value, poll_result, host->hostname, reindex->data_query_id);
                    cacti_log(logmessage);

                    query3 = (char *)malloc(128);
                    snprintf(query3, 128, "insert into poller_command (poller_id,time,action,command) values (0,NOW(),%i,'%i:%i')", POLLER_COMMAND_REINDEX, host_id, reindex->data_query_id);
                    db_insert(&mysql, query3);
                    free(query3);

                    assert_fail = 1;
                } else if ((!strcmp(reindex->op, ">")) && (strtoll(reindex->assert_value, (char **)NULL, 10) <= strtoll(poll_result, (char **)NULL, 10))) {
                    snprintf(logmessage, LOGSIZE, "ASSERT: '%s>%s' failed. Recaching host '%s', data query #%i\n", reindex->assert_value, poll_result, host->hostname, reindex->data_query_id);
                    cacti_log(logmessage);

                    query3 = (char *)malloc(128);
                    snprintf(query3, 128, "insert into poller_command (poller_id,time,action,command) values (0,NOW(),%i,'%i:%i')", POLLER_COMMAND_REINDEX, host_id, reindex->data_query_id);
                    db_insert(&mysql, query3);
                    free(query3);

                    assert_fail = 1;
                } else if ((!strcmp(reindex->op, "<")) && (strtoll(reindex->assert_value, (char **)NULL, 10) >= strtoll(poll_result, (char **)NULL, 10))) {
                    snprintf(logmessage, LOGSIZE, "ASSERT: '%s<%s' failed. Recaching host '%s', data query #%i\n", reindex->assert_value, poll_result, host->hostname, reindex->data_query_id);
                    cacti_log(logmessage);

                    query3 = (char *)malloc(128);
                    snprintf(query3, 128, "insert into poller_command (poller_id,time,action,command) values (0,NOW(),%i,'%i:%i')", POLLER_COMMAND_REINDEX, host_id, reindex->data_query_id);
                    db_insert(&mysql, query3);
                    free(query3);

                    assert_fail = 1;
                }

                /* update 'poller_reindex' with the correct information if:
                 * 1) the assert fails
                 * 2) the OP code is > or < meaning the current value could have changed without causing
                 *     the assert to fail */
                if ((assert_fail == 1) || (!strcmp(reindex->op, ">")) || (!strcmp(reindex->op, ">"))) {
                    query3 = (char *)malloc(255);
                    snprintf(query3, 255, "update poller_reindex set assert_value='%s' where host_id='%i' and data_query_id='%i' and arg1='%s'", poll_result, host_id, reindex->data_query_id, reindex->arg1);
                    db_insert(&mysql, query3);
                    free(query3);
                }

                free(poll_result);
            }
        }
    }

    /* retreive each hosts polling items from poller cache */
    entry = (target_t *) malloc(sizeof(target_t));

    result = db_query(&mysql, query1);
    num_rows = (int)mysql_num_rows(result);

    while ((row = mysql_fetch_row(result)) && (!host->ignore_host)) {
        /* initialize monitored object */
        entry->target_id = 0;
        entry->action = atoi(row[0]);
        if (row[1] != NULL) snprintf(entry->hostname, sizeof(entry->hostname), "%s", row[1]);
        if (row[2] != NULL) {
            snprintf(entry->snmp_community, sizeof(entry->snmp_community), "%s", row[2]);
        } else {
            snprintf(entry->snmp_community, sizeof(entry->snmp_community), "%s", "");
        }
        entry->snmp_version = atoi(row[3]);
        if (row[4] != NULL) snprintf(entry->snmp_username, sizeof(entry->snmp_username), "%s", row[4]);
        if (row[5] != NULL) snprintf(entry->snmp_password, sizeof(entry->snmp_password), "%s", row[5]);
        if (row[6] != NULL) snprintf(entry->rrd_name, sizeof(entry->rrd_name), "%s", row[6]);
        if (row[7] != NULL) snprintf(entry->rrd_path, sizeof(entry->rrd_path), "%s", row[7]);
        if (row[8] != NULL) snprintf(entry->arg1, sizeof(entry->arg1), "%s", row[8]);
        if (row[9] != NULL) snprintf(entry->arg2, sizeof(entry->arg2), "%s", row[9]);
        if (row[10] != NULL) snprintf(entry->arg3, sizeof(entry->arg3), "%s", row[10]);
        entry->local_data_id = atoi(row[11]);
        entry->rrd_num = atoi(row[12]);
        entry->snmp_port = atoi(row[13]);
        entry->snmp_timeout = atoi(row[14]);
        snprintf(entry->result, sizeof(entry->result), "%s", "U");

        if (!host->ignore_host) {
            switch(entry->action) {
            case POLLER_ACTION_SNMP: /* raw SNMP poll */
                poll_result = snmp_get(host, entry->arg1);
                snprintf(entry->result, sizeof(entry->result), "%s", poll_result);
                free(poll_result);

                if (host->ignore_host) {
                    snprintf(logmessage, LOGSIZE, "Host[%i] ERROR: SNMP timeout detected [%i milliseconds], ignoring host '%s'\n", host_id, host->snmp_timeout, host->hostname);
                    cacti_log(logmessage);
                    snprintf(entry->result, sizeof(entry->result), "%s", "U");
                } else {
                    /* remove double or single quotes from string */
                    strncpy(entry->result, strip_quotes(entry->result), sizeof(entry->result));

                    /* detect erroneous non-numeric result */
                    if (!is_numeric(entry->result)) {
                        strncpy(errstr, entry->result,sizeof(errstr));
                        snprintf(logmessage, LOGSIZE, "Host[%i] WARNING: Result from SNMP not valid. Partial Result: %.20s...\n", host_id, errstr);
                        cacti_log(logmessage);
                        strncpy(entry->result, "U", sizeof(entry->result));
                    }
                }

                if (set.verbose >= POLLER_VERBOSITY_MEDIUM) {
                    snprintf(logmessage, LOGSIZE, "Host[%i] SNMP: v%i: %s, dsname: %s, oid: %s, value: %s\n", host_id, host->snmp_version, host->hostname, entry->rrd_name, entry->arg1, entry->result);
                    cacti_log(logmessage);
                }

                break;
            case POLLER_ACTION_SCRIPT: /* execute script file */
                poll_result = exec_poll(host, entry->arg1);
                snprintf(entry->result, sizeof(entry->result), "%s", poll_result);
                free(poll_result);

                /* remove double or single quotes from string */
                strncpy(entry->result, strip_quotes(entry->result), sizeof(entry->result));

                /* detect erroneous result. can be non-numeric */
                if (!validate_result(entry->result)) {
                    strncpy(errstr, (char *) strip_string_crlf(entry->result),sizeof(errstr));
                    snprintf(logmessage, LOGSIZE, "Host[%i] WARNING: Result from SCRIPT not valid. Partial Result: %.20s...\n", host_id, errstr);
                    cacti_log(logmessage);
                    strncpy(entry->result, "U", sizeof(entry->result));
                }

                if (set.verbose >= POLLER_VERBOSITY_MEDIUM) {
                    snprintf(logmessage, LOGSIZE, "Host[%i] SCRIPT: %s, output: %s\n", host_id, entry->arg1, entry->result);
                    cacti_log(logmessage);
                }

                break;
            case POLLER_ACTION_PHP_SCRIPT_SERVER: /* execute script server */
                poll_result = php_cmd(entry->arg1);
                snprintf(entry->result, sizeof(entry->result), "%s", poll_result);
                free(poll_result);

                /* remove double or single quotes from string */
                strncpy(entry->result, strip_quotes(entry->result), sizeof(entry->result));

                /* detect erroneous result. can be non-numeric */
                if (!validate_result(entry->result)) {
                    strncpy(errstr, entry->result, sizeof(errstr));
                    snprintf(logmessage, LOGSIZE, "Host[%i] WARNING: Result from SERVER not valid.  Partial Result: %.20s...\n", host_id, errstr);
                    cacti_log(logmessage);
                    strncpy(entry->result, "U", sizeof(entry->result));
                }

                if (set.verbose >= POLLER_VERBOSITY_MEDIUM) {
                    snprintf(logmessage, LOGSIZE, "Host[%i] SERVER: %s, output: %s\n", host_id, entry->arg1, entry->result);
                    cacti_log(logmessage);
                }

                break;
            default: /* unknown action, generate error */
                snprintf(logmessage, LOGSIZE, "Host[%i] ERROR: Unknown Poller Action: %s\n", host_id, entry->arg1);
                cacti_log(logmessage);

                break;
            }
        }

        if (entry->result != NULL) {
            /* format database insert string */
            query3 = (char *)malloc(sizeof(entry->result) + sizeof(entry->local_data_id) + 128);
            snprintf(query3, (sizeof(entry->result) + sizeof(entry->local_data_id) + 128), "insert into poller_output (local_data_id,rrd_name,time,output) values (%i,'%s','%s','%s')", entry->local_data_id, entry->rrd_name, start_datetime, entry->result);
            db_insert(&mysql, query3);
            free(query3);
        }
    }

    /* cleanup memory and prepare for function exit */
    snmp_host_cleanup(host);

    free(entry);
    free(host);
    free(ping);

    mysql_free_result(result);

#ifndef OLD_MYSQL
    mysql_thread_end();
#endif

    mysql_close(&mysql);

    if (set.verbose == POLLER_VERBOSITY_DEBUG) {
        snprintf(logmessage, LOGSIZE, "Host[%i] DEBUG: HOST COMPLETE: About to Exit Host Polling Thread Function\n", host_id);
        cacti_log(logmessage);
    }
}
Пример #13
0
void *worker_thread(void *arg)
{
	MYSQL *dbc=NULL;
	my_bool auto_reconnect=1;
	int cancelstate=0;
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&cancelstate);

	dbc = mysql_init(NULL);
	if(NULL == dbc)
	{
		printf("mysql_init failed\n");
		dbc=NULL;
		goto threadexit;
	}
	else
	{
		mysql_options(dbc,MYSQL_OPT_RECONNECT,(char*)&auto_reconnect);
		if (!mysql_real_connect(dbc,host,username,password,database,port, NULL, CLIENT_FOUND_ROWS|CLIENT_MULTI_STATEMENTS|CLIENT_MULTI_RESULTS))
		{
			printf("mysql_real_connect failed: %s (%d)", mysql_error(dbc),mysql_errno(dbc));
			dbc=NULL;
		}
	}

	unsigned int counter=0;
	char shortquery[1024];
	memset(shortquery,0,1024);
	char *longquery;
	longquery=NULL;
	char *c;
	c=NULL;
	while(0==threaddone && NULL!=dbc)
	{

		if(lrand48()%6==0)
		{
			c=shortquery;
			c+=sprintf(c,"%s","grant select(id) on test.t1 to 'u");
			c+=sprintf(c,"%ld",-128 + lrand48()%255lu);
			c+=sprintf(c,"%s","'@'127.0.0.1'");
			db_query(dbc,shortquery,0);
        	}

		if(lrand48()%6==0)
		{
			for(counter=0;counter<3;counter++)
			{
				c=shortquery;
				c+=sprintf(c,"%s","drop user 'u");
				c+=sprintf(c,"%ld",-128 + lrand48()%255lu);
				c+=sprintf(c,"%s","'");
				db_query(dbc,shortquery,0);
			}

		}

		if(lrand48()%6==0)
		{
			for(counter=0;counter<3;counter++)
			{
				c=shortquery;
				c+=sprintf(c,"%s","show grants for 'u");
				c+=sprintf(c,"%ld",-128 + lrand48()%255lu);
				c+=sprintf(c,"%s","'");
				db_query(dbc,shortquery,0);
			}

		}

                if(lrand48()%6==0)
		{
			for(counter=0;counter<3;counter++)
			{
				c=shortquery;
				c+=sprintf(c,"%s","set password for 'u");
				c+=sprintf(c,"%ld",-128 + lrand48()%255lu);
				c+=sprintf(c,"%s","' = password('password')");
				db_query(dbc,shortquery,0);
			}

		}

		if(lrand48()%6==0)
		{
			for(counter=0;counter<3;counter++)
			{
				c=shortquery;
				c+=sprintf(c,"%s","flush privileges");
				db_query(dbc,shortquery,0);
			}

		}

        }
threadexit:
	mysql_close(dbc);
	mysql_thread_end();
	pthread_exit(0);
}
Пример #14
0
/**
 * The entry point for the monitoring module thread
 *
 * @param arg	The handle of the monitor
 */
static void
monitorMain(void *arg)
{
MYSQL_MONITOR	*handle = (MYSQL_MONITOR *)arg;
MONITOR_SERVERS	*ptr;
long master_id;
size_t nrounds = 0;

	if (mysql_thread_init())
	{
                LOGIF(LE, (skygw_log_write_flush(
                                   LOGFILE_ERROR,
                                   "Fatal : mysql_thread_init failed in monitor "
                                   "module. Exiting.\n")));
                return;
	}                         
	handle->status = MONITOR_RUNNING;
	
	while (1)
	{
		if (handle->shutdown)
		{
			handle->status = MONITOR_STOPPING;
			mysql_thread_end();
			handle->status = MONITOR_STOPPED;
			return;
		}

		/** Wait base interval */
		thread_millisleep(MON_BASE_INTERVAL_MS);
		/** 
		 * Calculate how far away the monitor interval is from its full 
		 * cycle and if monitor interval time further than the base 
		 * interval, then skip monitoring checks. Excluding the first
		 * round.
		 */ 
		if (nrounds != 0 && 
			((nrounds*MON_BASE_INTERVAL_MS)%handle->interval) > 
			MON_BASE_INTERVAL_MS) 
		{
			nrounds += 1;
			continue;
		}
		nrounds += 1;
		master_id = -1;
		ptr = handle->databases;

		while (ptr)
		{
			unsigned int prev_status = ptr->server->status;
			monitorDatabase(ptr, handle->defaultUser, handle->defaultPasswd);

			if (ptr->server->status != prev_status ||
				SERVER_IS_DOWN(ptr->server))
			{
				LOGIF(LM, (skygw_log_write_flush(
					LOGFILE_MESSAGE,
					"Backend server %s:%d state : %s",
					ptr->server->name,
					ptr->server->port,
					STRSRVSTATUS(ptr->server))));
			}

			ptr = ptr->next;
		}
	}
}
Пример #15
0
int main(int argc, char *argv[]) {
	struct configuration conf= { NULL, NULL, NULL, 0 };

	GError *error= NULL;
	GOptionContext *context;

	g_thread_init(NULL);

	init_mutex= g_mutex_new();

	if(db == NULL && source_db != NULL){
		db = g_strdup(source_db);
	}

	context= g_option_context_new("multi-threaded MySQL loader");
	GOptionGroup *main_group= g_option_group_new("main", "Main Options", "Main Options", NULL, NULL);
	g_option_group_add_entries(main_group, entries);
	g_option_group_add_entries(main_group, common_entries);
	g_option_context_set_main_group(context, main_group);
	if (!g_option_context_parse(context, &argc, &argv, &error)) {
		g_print("option parsing failed: %s, try --help\n", error->message);
		exit(EXIT_FAILURE);
	}
	g_option_context_free(context);

	if (program_version) {
		g_print("myloader %s, built against MySQL %s\n", VERSION, MYSQL_SERVER_VERSION);
		exit(EXIT_SUCCESS);
	}

	set_verbose(verbose);

	if (!directory) {
		g_critical("a directory needs to be specified, see --help\n");
		exit(EXIT_FAILURE);
	} else {
		char *p= g_strdup_printf("%s/metadata", directory);
		if (!g_file_test(p, G_FILE_TEST_EXISTS)) {
			g_critical("the specified directory is not a mydumper backup\n");
			exit(EXIT_FAILURE);
		}
	}

	MYSQL *conn;
	conn= mysql_init(NULL);

	if (defaults_file != NULL) {
		mysql_options(conn,MYSQL_READ_DEFAULT_FILE,defaults_file);
	}
	mysql_options(conn, MYSQL_READ_DEFAULT_GROUP, "myloader");

	if (!mysql_real_connect(conn, hostname, username, password, NULL, port, socket_path, 0)) {
		g_critical("Error connection to database: %s", mysql_error(conn));
		exit(EXIT_FAILURE);
	}

	if (mysql_query(conn, "SET SESSION wait_timeout = 2147483")){
		g_warning("Failed to increase wait_timeout: %s", mysql_error(conn));
	}

	if (!enable_binlog)
		mysql_query(conn, "SET SQL_LOG_BIN=0");

	mysql_query(conn, "/*!40014 SET FOREIGN_KEY_CHECKS=0*/");
	conf.queue= g_async_queue_new();
	conf.ready= g_async_queue_new();

	guint n;
	GThread **threads= g_new(GThread*, num_threads);
	struct thread_data *td= g_new(struct thread_data, num_threads);
	for (n= 0; n < num_threads; n++) {
		td[n].conf= &conf;
		td[n].thread_id= n+1;
		threads[n]= g_thread_create((GThreadFunc)process_queue, &td[n], TRUE, NULL);
		g_async_queue_pop(conf.ready);
	}
	g_async_queue_unref(conf.ready);

	g_message("%d threads created", num_threads);

	restore_databases(&conf, conn);

	for (n= 0; n < num_threads; n++) {
		struct job *j= g_new0(struct job, 1);
		j->type = JOB_SHUTDOWN;
		g_async_queue_push(conf.queue, j);
	}

	for (n= 0; n < num_threads; n++) {
		g_thread_join(threads[n]);
	}

	restore_schema_post(conn);

	restore_schema_view(conn);

	restore_schema_triggers(conn);

	g_async_queue_unref(conf.queue);
	mysql_close(conn);
	mysql_thread_end();
	mysql_library_end();
	g_free(directory);
	g_free(td);
	g_free(threads);

	return errors ? EXIT_FAILURE : EXIT_SUCCESS;
}
Пример #16
0
/**
 * Load the user/passwd form mysql.user table into the service users' hashtable
 * environment.
 *
 * @param service	The current service
 * @param users		The users table into which to load the users
 * @return      -1 on any error or the number of users inserted (0 means no users at all)
 */
static int
getUsers(SERVICE *service, struct users *users)
{
	MYSQL			*con = NULL;
	MYSQL_ROW		row;
	MYSQL_RES		*result = NULL;
	int			num_fields = 0;
	char			*service_user = NULL;
	char			*service_passwd = NULL;
	char			*dpwd;
	int			total_users = 0;
	SERVER			*server;
	char			*users_query;
	unsigned char		hash[SHA_DIGEST_LENGTH]="";
	char			*users_data = NULL;
	int 			nusers = 0;
	int			users_data_row_len = MYSQL_USER_MAXLEN + MYSQL_HOST_MAXLEN + MYSQL_PASSWORD_LEN;
	struct sockaddr_in	serv_addr;
	MYSQL_USER_HOST		key;

	/* enable_root for MySQL protocol module means load the root user credentials from backend databases */
	if(service->enable_root) {
		users_query = LOAD_MYSQL_USERS_QUERY " ORDER BY HOST DESC";
	} else {
		users_query = LOAD_MYSQL_USERS_QUERY USERS_QUERY_NO_ROOT " ORDER BY HOST DESC";
	}

	serviceGetUser(service, &service_user, &service_passwd);

	/** multi-thread environment requires that thread init succeeds. */
	if (mysql_thread_init()) {
		LOGIF(LE, (skygw_log_write_flush(
                        LOGFILE_ERROR,
                        "Error : mysql_thread_init failed.")));
		return -1;
	}
    
	con = mysql_init(NULL);

 	if (con == NULL) {
		LOGIF(LE, (skygw_log_write_flush(
                        LOGFILE_ERROR,
                        "Error : mysql_init: %s",
                        mysql_error(con))));
		return -1;
	}

	if (mysql_options(con, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL)) {
		LOGIF(LE, (skygw_log_write_flush(
                        LOGFILE_ERROR,
                        "Error : failed to set external connection. "
                        "It is needed for backend server connections. "
                        "Exiting.")));
		return -1;
	}
	/*
	 * Attempt to connect to each database in the service in turn until
	 * we find one that we can connect to or until we run out of databases
	 * to try
	 */
	server = service->databases;
	dpwd = decryptPassword(service_passwd);
	while (server != NULL && mysql_real_connect(con,
                                                    server->name,
                                                    service_user,
                                                    dpwd,
                                                    NULL,
                                                    server->port,
                                                    NULL,
                                                    0) == NULL)
	{
                server = server->nextdb;
	}
	free(dpwd);

	if (server == NULL)
	{
		LOGIF(LE, (skygw_log_write_flush(
                        LOGFILE_ERROR,
                        "Error : Unable to get user data from backend database "
                        "for service %s. Missing server information.",
                        service->name)));
		mysql_close(con);
		return -1;
	}

	if (mysql_query(con, MYSQL_USERS_COUNT)) {
		LOGIF(LE, (skygw_log_write_flush(
                        LOGFILE_ERROR,
                        "Error : Loading users for service %s encountered "
                        "error: %s.",
                        service->name,
                        mysql_error(con))));
		mysql_close(con);
		return -1;
	}
	result = mysql_store_result(con);

	if (result == NULL) {
		LOGIF(LE, (skygw_log_write_flush(
                        LOGFILE_ERROR,
                        "Error : Loading users for service %s encountered "
                        "error: %s.",
                        service->name,
                        mysql_error(con))));
		mysql_close(con);
		return -1;
	}
	num_fields = mysql_num_fields(result);
	row = mysql_fetch_row(result);

	nusers = atoi(row[0]);

	mysql_free_result(result);

	if (!nusers) {
		LOGIF(LE, (skygw_log_write_flush(
                        LOGFILE_ERROR,
                        "Error : Counting users for service %s returned 0",
                        service->name)));
		mysql_close(con);
		return -1;
	}

	if (mysql_query(con, users_query)) {
		LOGIF(LE, (skygw_log_write_flush(
                        LOGFILE_ERROR,
                        "Error : Loading users for service %s encountered "
                        "error: %s.",
                        service->name,
                        mysql_error(con))));
		mysql_close(con);
		return -1;
	}

	result = mysql_store_result(con);
  
	if (result == NULL) {
		LOGIF(LE, (skygw_log_write_flush(
                        LOGFILE_ERROR,
                        "Error : Loading users for service %s encountered "
                        "error: %s.",
                        service->name,
                        mysql_error(con))));
		mysql_close(con);
		return -1;
	}
	num_fields = mysql_num_fields(result);
	
	users_data = (char *)malloc(nusers * (users_data_row_len * sizeof(char)) + 1);

	if(users_data == NULL)
		return -1;

	while ((row = mysql_fetch_row(result))) { 
		/**
                 * Four fields should be returned.
                 * user and passwd+1 (escaping the first byte that is '*') are
                 * added to hashtable.
                 */
		
		char ret_ip[INET_ADDRSTRLEN + 1]="";
		const char *rc;

		/* prepare the user@host data struct */
		memset(&serv_addr, 0, sizeof(serv_addr));
		memset(&key, 0, sizeof(key));

		/* if host == '%', 0 is passed */
		if (setipaddress(&serv_addr.sin_addr, strcmp(row[1], "%") ? row[1] : "0.0.0.0")) {

			key.user = strdup(row[0]);

			if(key.user == NULL) {
				LOGIF(LE, (skygw_log_write_flush(
					LOGFILE_ERROR,
					"%lu [getUsers()] strdup() failed for user %s",
					pthread_self(),
					row[0])));

				continue;
			}

			memcpy(&key.ipv4, &serv_addr, sizeof(serv_addr));
			
			rc = inet_ntop(AF_INET, &(serv_addr).sin_addr, ret_ip, INET_ADDRSTRLEN);

			/* add user@host as key and passwd as value in the MySQL users hash table */
			if (mysql_users_add(users, &key, strlen(row[2]) ? row[2]+1 : row[2])) {
				LOGIF(LD, (skygw_log_write_flush(
					LOGFILE_DEBUG,
					"%lu [mysql_users_add()] Added user %s@%s(%s)",
					pthread_self(),
					row[0],
					row[1],
					rc == NULL ? "NULL" : ret_ip)));
		
				/* Append data in the memory area for SHA1 digest */	
				strncat(users_data, row[3], users_data_row_len);

				total_users++;
			} else {
				LOGIF(LE, (skygw_log_write_flush(
					LOGFILE_ERROR,
					"%lu [mysql_users_add()] Failed adding user %s@%s(%s)",
					pthread_self(),
					row[0],
					row[1],
					rc == NULL ? "NULL" : ret_ip)));

				continue;
			}

		} else {
			/* setipaddress() failed, skip user add and log this*/
			LOGIF(LE, (skygw_log_write_flush(
				LOGFILE_ERROR,
				"%lu [getUsers()] setipaddress failed: user %s@%s not added",
				pthread_self(),
				row[0],
				row[1])));
		}
	}

	/* compute SHA1 digest for users' data */
        SHA1((const unsigned char *) users_data, strlen(users_data), hash);

	memcpy(users->cksum, hash, SHA_DIGEST_LENGTH);

	free(users_data);

	mysql_free_result(result);
	mysql_close(con);
	mysql_thread_end();

	return total_users;
}
Пример #17
0
int main(int argc, char** argv)
{
        slist_cursor_t*    c;
        const char*        q;
        query_test_t*      qtest;
        skygw_query_type_t qtype;
        bool               succp;
        bool               failp = true;
        unsigned int       f = 0;
        int                nsucc = 0;
        int                nfail = 0;
        MYSQL*             mysql;
        char*              workingdir;
        char               ddoption[1024];

        ss_dfprintf(stderr, ">> testmain\n");
        c = slist_init();

        /** Test some functions */
        q = "SELECT MY_UDF('Hello')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        /** This could be QUERY_TYPE_LOCAL_READ */
        q = "SELECT repeat('a', 1024)";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));

        /** This could be QUERY_TYPE_LOCAL_READ */
        q = "SELECT soundex('Hello')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));

        q = "SELECT ssoundexx('Hello')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        /** This could be QUERY_TYPE_LOCAL_READ */
        q = "SELECT now()";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));

        /** This could be QUERY_TYPE_LOCAL_READ */
        q = "SELECT rand()";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));

        q = "SELECT rand(234), MY_UDF('Hello'), soundex('Hello')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        
        /** Read-only SELECTs */
        q = "SELECT user from mysql.user";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, true));

        q = "select tt1.id, tt2.id from t1 tt1, t2 tt2 where tt1.name is "
            "not null and tt2.name is not null";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_READ, false, false));

        /** SELECT ..INTO clauses > session updates */
        q = "SELECT user from mysql.user INTO DUMPFILE '/tmp/dump1'";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        q = "SELECT user INTO DUMPFILE '/tmp/dump2 ' from mysql.user";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        q = "SELECT user from mysql.user INTO OUTFILE '/tmp/out1'";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        /** Database and table name must be separated by a dot */
        q = "SELECT user INTO OUTFILE '/tmp/out2 ' from mysql-user";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, true, false));

        /** Database and table name must be separated by a dot */
        q = "SELECT user INTO OUTFILE '/tmp/out2 ' from mysql_foo_user";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
        
        q = "SELECT user FROM mysql.user limit 1 INTO @local_variable";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        q = "SELECT user INTO @local_variable FROM mysql.user limit 1";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));
        
        q = "SELECT non_existent_attr INTO @d FROM non_existent_table";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        q = "select * from table1 "
            "where table1.field IN "
            "(select * from table1a union select * from table1b) union "
            "select * from table2 where table2.field = "
            "(select (select f1 from table2a where table2a.f2 = table2b.f3) "
            "from table2b where table2b.f1 = table2.f2) union "
            "select * from table3";
            slcursor_add_case(
                    c,
                    query_test_init(q, QUERY_TYPE_READ, false, true));
                
        /** RENAME TABLEs */
        q = "RENAME TABLE T1 to T2";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        
        /** INSERTs */
        q = "INSERT INTO T1 (SELECT * FROM T2)";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        q = "INSERT INTO T1 VALUES(2, 'foo', 'toomanyattributes')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        q = "INSERT INTO T2 VALUES(1, 'sthrgey')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        q = "INSERT INTO T2 VALUES(8, 'ergstrhe')";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        q = "INSERT INTO T2 VALUES(9, NULL)";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));


        /** Ok, delimeter is client-side parameter which shouldn't be handled
         * on server side.
         */
        q = "delimiter //";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, true, true));

        /** SETs, USEs > Session updates */
        q = "SET @a=1";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true));
        
        q = "USE TEST";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, false));

        
        /** Object creation statements */
        q = "create procedure si (out param1 int) \nbegin select count(*) "
            "into param1 from t1; \nend";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));
        
        q = "CREATE TABLE T1 (id integer primary key, name varchar(10))";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        q = "DROP TABLE T1";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        q = "ALTER TABLE T1 ADD COLUMN WHYME INTEGER NOT NULL";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        q = "TRUNCATE TABLE T1";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, false));

        q = "DROP SERVER IF EXISTS VICTIMSRV";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true));

        q = "CREATE USER FOO IDENTIFIED BY 'BAR'";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        q = "OPTIMIZE NO_WRITE_TO_BINLOG TABLE T1";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));

        q = "SELECT NOW();CREATE TABLE T1 (ID INTEGER);"
            "SET sql_log_bin=0;CREATE TABLE T2 (ID INTEGER)";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_WRITE, false, true));
        
        
        /** Setting database makes this SESSION_WRITE */
        q = "USE TEST;CREATE TABLE T1 (ID INTEGER);"
            "SET sql_log_bin=0;CREATE TABLE T2 (ID INTEGER)";
        slcursor_add_case(
                c,
                query_test_init(q, QUERY_TYPE_SESSION_WRITE, false, true));
        
        /**
         * Init libmysqld.
         */
        workingdir = getenv("PWD");
        
        if (workingdir == NULL) {
                fprintf(stderr,
                        "Failed to resolve the working directory, $PWD is not "
                        "set.\n");
                goto return_without_server;
        } else if (access(workingdir, R_OK) != 0) {
                fprintf(stderr,
                        "Failed to access the working directory due %d, %s\n",
                        errno,
                        strerror(errno));
                goto return_without_server;
        } else {
                char** so = server_options;
                snprintf(datadir, 1023, "%s/data", workingdir);
                mkdir(datadir, 0777);
                snprintf(ddoption, 1023, "--datadir=%s", datadir);

                while (strncmp(*so++, "--datadir=", 10) != 0) ;

                if (*so == NULL) {
                        fprintf(stderr, "Failed to find datadir option.\n");
                        goto return_without_server;
                }
                *so = ddoption;
                
                snprintf(mysqldir, 1023, "%s/mysql", workingdir);
                setenv("MYSQL_HOME", mysqldir, 1);
        }
        failp = mysql_library_init(num_elements, server_options, server_groups);

        if (failp) {
            MYSQL* mysql = mysql_init(NULL);
            ss_dassert(mysql != NULL);
            fprintf(stderr,
                    "mysql_init failed, %d : %s\n",
                    mysql_errno(mysql),
                    mysql_error(mysql));
            goto return_without_server;
        }

        fprintf(stderr,
                "\nExecuting selected cases in "
                "skygw_query_classifier_get_type :\n\n");
        /**
         * Set cursor to the beginning, scan through the list and execute
         * test cases.
         */
        succp = slcursor_move_to_begin(c);
        
        while(succp) {
            qtest = slcursor_get_case(c);
            qtest->qt_result_type =
                skygw_query_classifier_get_type(qtest->qt_query_str, f);
            succp = slcursor_step_ahead(c);
        }
        /**
         * Scan through test results and compare them against expected
         * results.
         */
        succp = slcursor_move_to_begin(c);
        fprintf(stderr, "\nScanning through the results :\n\n");
        
        while(succp) {
            qtest = slcursor_get_case(c);
            
            if (!query_test_types_match(qtest)) {
                nfail += 1;
                ss_dfprintf(stderr,
                            "* Failed: \"%s\" -> %s (Expected %s)\n",
                            query_test_get_querystr(qtest),
                            STRQTYPE(query_test_get_result_type(qtest)),
                            STRQTYPE(query_test_get_query_type(qtest)));
            } else {
                nsucc += 1;
                ss_dfprintf(stderr,
                            "Succeed\t: \"%s\" -> %s\n",
                            query_test_get_querystr(qtest),
                            STRQTYPE(query_test_get_query_type(qtest)));
            }
            succp = slcursor_step_ahead(c);
        }
        fprintf(stderr,
                "------------------------------------------\n"
                "Tests in total %d, SUCCEED %d, FAILED %d\n",
                nsucc+nfail,
                nsucc,
                nfail);

        /**
         * Scan test results and re-execute those which are marked to be
         * executed also in the server. This serves mostly debugging purposes.
         */
        succp = slcursor_move_to_begin(c);
        mysql = mysql_init(NULL);

        if (mysql == NULL) {
            fprintf(stderr, "mysql_init failed\n");
            goto return_without_server;
        }

        mysql_options(mysql,
                      MYSQL_READ_DEFAULT_GROUP,
                      "libmysqld_client");
        mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
        mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);

        mysql = mysql_real_connect(mysql,
                                   NULL,
                                   "skygw",
                                   "skygw",
                                   NULL,
                                   0,
                                   NULL,
                                   CLIENT_MULTI_STATEMENTS);
        
        if (mysql == NULL) {
            fprintf(stderr, "mysql_real_connect failed\n");
            goto return_with_handle;
        }

        fprintf(stderr,
                "\nRe-execution of selected cases in Embedded server :\n\n");
        
        while(succp) {
            qtest = slcursor_get_case(c);

            if (query_test_exec_also_in_server(qtest)) {
                MYSQL_RES*  results;
                MYSQL_ROW   record;
                const char* query_str;
                
                query_str = query_test_get_querystr(qtest);
                failp = mysql_query(mysql, query_str);

                if (failp) {
                    ss_dfprintf(stderr,
                                "* Failed: \"%s\" -> %d : %s\n",
                                query_str,
                                mysql_errno(mysql),
                                mysql_error(mysql));
                } else {
                    ss_dfprintf(stderr,
                                "Succeed\t: \"%s\"\n",
                                query_str);
                    results = mysql_store_result(mysql);
                    
                    if (results != NULL) {
                        
                        while((record = mysql_fetch_row(results))) {
                            while(record != NULL && *record != NULL) {
                                ss_dfprintf(stderr, "%s ", *record);
                                record++;
                            }
                            ss_dfprintf(stderr, "\n");
                        }
                        mysql_free_result(results);
                    }
                }
            }
            succp = slcursor_step_ahead(c);
            
        }
        slist_done(c);
        fprintf(stderr, "------------------------------------------\n");
        
return_with_handle:
        mysql_close(mysql);
        mysql_thread_end();
        mysql_library_end();
        
return_without_server:
        ss_dfprintf(stderr, "\n<< testmain\n");
        fflush(stderr);
        return 0;
}
Пример #18
0
static apr_status_t thread_end(void *data)
{
    mysql_thread_end();
    return APR_SUCCESS;
}
Пример #19
0
 void onThreadExit() { mysql_thread_end();}
Пример #20
0
/**
 * The main polling loop
 *
 * This routine does the polling and despatches of IO events
 * to the DCB's. It may be called either directly or as the entry point
 * of a polling thread within the gateway.
 *
 * The routine will loop as long as the variable "shutdown" is set to zero,
 * setting this to a non-zero value will cause the polling loop to return.
 *
 * There are two options for the polling, a debug option that is only useful if
 * you have a single thread. This blocks in epoll_wait until an event occurs.
 *
 * The non-debug option does an epoll with a time out. This allows the checking of
 * shutdown value to be checked in all threads. The algorithm for polling in this
 * mode is to do a poll with no-wait, if no events are detected then the poll is
 * repeated with a time out. This allows for a quick check before making the call 
 * with timeout. The call with the timeout differs in that the Linux scheduler may
 * deschedule a process if a timeout is included, but will not do this if a 0 timeout
 * value is given. this improves performance when the gateway is under heavy load.
 *
 * In order to provide a fairer means of sharing the threads between the different
 * DCB's the poll mechanism has been decoupled from the processing of the events.
 * The events are now recieved via the epoll_wait call, a queue of DCB's that have
 * events pending is maintained and as new events arrive the DCB is added to the end
 * of this queue. If an eent arrives for a DCB alreayd in the queue, then the event
 * bits are added to the DCB but the DCB mantains the same point in the queue unless
 * the original events are already being processed. If they are being processed then
 * the DCB is moved to the back of the queue, this means that a DCB that is receiving
 * events at a high rate will not block the execution of events for other DCB's and
 * should result in a fairer polling strategy.
 *
 * The introduction of the ability to inject "fake" write events into the event queue meant
 * that there was a possibility to "starve" new events sicne the polling loop would
 * consume the event queue before looking for new events. If the DCB that inject
 * the fake event then injected another fake event as a result of the first it meant
 * that new events did not get added to the queue. The strategy has been updated to
 * not consume the entire event queue, but process one event before doing a non-blocking
 * call to add any new events before processing any more events. A blocking call to
 * collect events is only made if there are no pending events to be processed on the
 * event queue.
 *
 * Also introduced a "timeout bias" mechanism. This mechansim control the length of
 * of timeout passed to epoll_wait in blocking calls based on previous behaviour.
 * The initial call will block for 10% of the define timeout peroid, this will be
 * increased in increments of 10% until the full timeout value is used. If at any
 * point there is an event to be processed then the value will be reduced to 10% again
 * for the next blocking call.
 *
 * @param arg	The thread ID passed as a void * to satisfy the threading package
 */
void
poll_waitevents(void *arg)
{
struct epoll_event events[MAX_EVENTS];
int		   i, nfds, timeout_bias = 1;
int		   thread_id = (int)arg;
DCB                *zombies = NULL;
int		   poll_spins = 0;

	/** Add this thread to the bitmask of running polling threads */
	bitmask_set(&poll_mask, thread_id);
	if (thread_data)
	{
		thread_data[thread_id].state = THREAD_IDLE;
	}

	/** Init mysql thread context for use with a mysql handle and a parser */
	mysql_thread_init();
	
	while (1)
	{
		if (pollStats.evq_pending == 0 && timeout_bias < 10)
		{
			timeout_bias++;
		}

		atomic_add(&n_waiting, 1);
#if BLOCKINGPOLL
		nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
		atomic_add(&n_waiting, -1);
#else /* BLOCKINGPOLL */
#if MUTEX_EPOLL
                simple_mutex_lock(&epoll_wait_mutex, TRUE);
#endif
		if (thread_data)
		{
			thread_data[thread_id].state = THREAD_POLLING;
		}
                
		atomic_add(&pollStats.n_polls, 1);
		if ((nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, 0)) == -1)
		{
			atomic_add(&n_waiting, -1);
                        int eno = errno;
                        errno = 0;
                        LOGIF(LD, (skygw_log_write(
                                LOGFILE_DEBUG,
                                "%lu [poll_waitevents] epoll_wait returned "
                                "%d, errno %d",
                                pthread_self(),
                                nfds,
                                eno)));
			atomic_add(&n_waiting, -1);
		}
		/*
		 * If there are no new descriptors from the non-blocking call
		 * and nothing to process on the event queue then for do a
		 * blocking call to epoll_wait.
		 *
		 * We calculate a timeout bias to alter the length of the blocking
		 * call based on the time since we last received an event to process
		 */
		else if (nfds == 0 && pollStats.evq_pending == 0 && poll_spins++ > number_poll_spins)
		{
			atomic_add(&pollStats.blockingpolls, 1);
			nfds = epoll_wait(epoll_fd,
                                                  events,
                                                  MAX_EVENTS,
                                                  (max_poll_sleep * timeout_bias) / 10);
			if (nfds == 0 && pollStats.evq_pending)
			{
				atomic_add(&pollStats.wake_evqpending, 1);
				poll_spins = 0;
			}
		}
		else
		{
			atomic_add(&n_waiting, -1);
		}

		if (n_waiting == 0)
			atomic_add(&pollStats.n_nothreads, 1);
#if MUTEX_EPOLL
                simple_mutex_unlock(&epoll_wait_mutex);
#endif
#endif /* BLOCKINGPOLL */
		if (nfds > 0)
		{
			timeout_bias = 1;
			if (poll_spins <= number_poll_spins + 1)
				atomic_add(&pollStats.n_nbpollev, 1);
			poll_spins = 0;
                        LOGIF(LD, (skygw_log_write(
                                LOGFILE_DEBUG,
                                "%lu [poll_waitevents] epoll_wait found %d fds",
                                pthread_self(),
                                nfds)));
			atomic_add(&pollStats.n_pollev, 1);
			if (thread_data)
			{
				thread_data[thread_id].n_fds = nfds;
				thread_data[thread_id].cur_dcb = NULL;
				thread_data[thread_id].event = 0;
				thread_data[thread_id].state = THREAD_PROCESSING;
			}

			pollStats.n_fds[(nfds < MAXNFDS ? (nfds - 1) : MAXNFDS - 1)]++;

			load_average = (load_average * load_samples + nfds)
						/ (load_samples + 1);
			atomic_add(&load_samples, 1);
			atomic_add(&load_nfds, nfds);

			/*
			 * Process every DCB that has a new event and add
			 * it to the poll queue.
			 * If the DCB is currently being processed then we
			 * or in the new eent bits to the pending event bits
			 * and leave it in the queue.
			 * If the DCB was not already in the queue then it was
			 * idle and is added to the queue to process after
			 * setting the event bits.
			 */
			for (i = 0; i < nfds; i++)
			{
				DCB 	*dcb = (DCB *)events[i].data.ptr;
				__uint32_t	ev = events[i].events;

				spinlock_acquire(&pollqlock);
				if (DCB_POLL_BUSY(dcb))
				{
					if (dcb->evq.pending_events == 0)
					{
						pollStats.evq_pending++;
						dcb->evq.inserted = hkheartbeat;
					}
					dcb->evq.pending_events |= ev;
				}
				else
				{
					dcb->evq.pending_events = ev;
					if (eventq)
					{
						dcb->evq.prev = eventq->evq.prev;
						eventq->evq.prev->evq.next = dcb;
						eventq->evq.prev = dcb;
						dcb->evq.next = eventq;
					}
					else
					{
						eventq = dcb;
						dcb->evq.prev = dcb;
						dcb->evq.next = dcb;
					}
					pollStats.evq_length++;
					pollStats.evq_pending++;
					dcb->evq.inserted = hkheartbeat;
					if (pollStats.evq_length > pollStats.evq_max)
					{
						pollStats.evq_max = pollStats.evq_length;
					}
				}
				spinlock_release(&pollqlock);
			}
		}

		/*
		 * Process of the queue of waiting requests
		 * This is done without checking the evq_pending count as a
		 * precautionary measure to avoid issues if the house keeping
		 * of the count goes wrong.
		 */
		if (process_pollq(thread_id))
			timeout_bias = 1;

		if (thread_data)
			thread_data[thread_id].state = THREAD_ZPROCESSING;
		zombies = dcb_process_zombies(thread_id);
		if (thread_data)
			thread_data[thread_id].state = THREAD_IDLE;

		if (do_shutdown)
		{
                        /*<
                         * Remove the thread from the bitmask of running
                         * polling threads.
                         */
			if (thread_data)
			{
				thread_data[thread_id].state = THREAD_STOPPED;
			}
			bitmask_clear(&poll_mask, thread_id);
			/** Release mysql thread context */
			mysql_thread_end();
			return;
		}
		if (thread_data)
		{
			thread_data[thread_id].state = THREAD_IDLE;
		}
	} /*< while(1) */
}
Пример #21
0
void CMySQLCallable :: Close( )
{
	mysql_thread_end( );

	CBaseCallable :: Close( );
}
void ConnectThread::exit()
{
  mysql_thread_end();
}
//======================================================================================================================
DatabaseImplementationMySql::~DatabaseImplementationMySql(void)
{
    // Close the connection and destroy our connection object.
    mysql_close(mConnection);
    mysql_thread_end();
}
Пример #24
0
void MyDriver::ShutdownThreadSafety()
{
	mysql_thread_end();
}
Пример #25
0
void* startThread(void* arg)
{
    /*
    SELECT W.PageID,PageType,URL,ResetCookies,AuthUser,AuthPass,DownloadThreshold,HackingString,ResetCache,ServiceType,SecurityID,WPS.CurrentErrorRef,WPS.AlertCount,AlertLevel2Count,AlertLevel3Count,WPS.ErrorStatus,W.Status,W.IgnoreRudeWords,W.CustomerNo,W.HTTPMethod,W.IgnoreContentErrors,W.DownloadSpeed,W.Referer,W.StoreResultDetail,W.MinSize,W.MaxSize,C.AlertCount,W.CertID,W.StepName,W.AgentString,W.sm_processScript,WPS.OverallAlertCount,W.StoreRequest,W.StoreHeaders,W.StoreHTML,W.StoreDiag,W.ContentType,W.Header1,W.Header2,W.GzipEncoding from WebPage W, Customer C, WebPageStatus WPS where W.PageID=13714 and WPS.PageID=W.PageID and W.CustomerNo=C.CustomerNo and (W.Status=1 or W.Status=3)
    select VariableName, VariableValue, Encode from FormVariables where PageID=13714 order by OrderNo asc
    SELECT Phrase, ID from ContentError
    SELECT Word, ID from ObsceneWord
    SELECT Phrase, ErrorCode from WebPagePhrase where PageID=13714 order by PhraseOrder
    select CookieValue, CookieDomain from Cookies where PageID=13714
    select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=0
    select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=1
    select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=2
    INSERT INTO ResultHeader040303 (ResultID,PageID,TestServer,StartDateTime,ResultCode,DNSTime,FirstByteTime,CompleteTime,TotalBytes,RunID,BatchID,FirstDataTime) VALUES (0,13714,10,'2004-03-03 14:39:20',1,8,9,5309,25291,0,0,15)
    SELECT LAST_INSERT_ID() from ResultHeader040303 where ResultID=LAST_INSERT_ID()
    INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:20',0,8,9,15,892,200,4338,'http://192.168.1.5',1)
    INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1012,1,1,7,4188,200,6543,'http://192.168.1.5/g/p10.png',2)
    INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1025,0,2,3,4185,200,12237,'http://192.168.1.5/g/MYSQLAuthCSP_small.png',3)
    INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1008,1,2,6,4301,200,2173,'http://192.168.1.5/g/newlogo.png',4)
    SELECT PageID from WebPageStatus where PageID=13714
    UPDATE WebPageStatus set LastResultID=40,StartDateTime='2004-03-03 14:39:20',LastResultCode=1,CompleteTime=5309 where PageID=13714
    SELECT WS.ErrorStatus,WS.CurrentErrorRef,WS.AlertCount,C.AlertLevel2Count,C.AlertLevel3Count,C.AlertCount,W.OverallAlertCount from Customer C, WebPage W, WebPageStatus WS WHERE C.CustomerNo=W.CustomerNo and W.PageID=WS.PageID and W.PageID=13714
    UPDATE WebPageStatus SET ErrorStatus=0,CurrentErrorRef=0,AlertCount=0 WHERE PageID=13714
    UPDATE WebPageStatus SET OverallAlertCount=0 WHERE PageID=13714
    select ErrorRef from WebPageError where PageID=13714 and Status<>2
    */

    mysql_thread_init();

    Connection* connection;
    Result* result;
    char query[10240];

    for (int repNo = 0; repNo < numRepsPerThreads; ++repNo)
    {
        try
        {
            Trace("Connecting to db", "", repNo);
            connection = new Connection(CConfig::getDBServer(), CConfig::getDBDatabase(),
                                        CConfig::getDBUser(), CConfig::getDBPass(), CConfig::getDBRetryTime());

            // run all the queries here...
            result = connection->Query("SELECT W.PageID,PageType,URL,ResetCookies,AuthUser,AuthPass,DownloadThreshold,HackingString,ResetCache,ServiceType,SecurityID,WPS.CurrentErrorRef,WPS.AlertCount,AlertLevel2Count,AlertLevel3Count,WPS.ErrorStatus,W.Status,W.IgnoreRudeWords,W.CustomerNo,W.HTTPMethod,W.IgnoreContentErrors,W.DownloadSpeed,W.Referer,W.StoreResultDetail,W.MinSize,W.MaxSize,C.AlertCount,W.CertID,W.StepName,W.AgentString,W.sm_processScript,WPS.OverallAlertCount,W.StoreRequest,W.StoreHeaders,W.StoreHTML,W.StoreDiag,W.ContentType,W.Header1,W.Header2,W.GzipEncoding from WebPage W, Customer C, WebPageStatus WPS where W.PageID=13714 and WPS.PageID=W.PageID and W.CustomerNo=C.CustomerNo and (W.Status=1 or W.Status=3)");
            result = connection->Query("select VariableName, VariableValue, Encode from FormVariables where PageID=13714 order by OrderNo asc");
            result = connection->Query("SELECT Phrase, ID from ContentError");
            result = connection->Query("SELECT Word, ID from ObsceneWord");
            result = connection->Query("SELECT Phrase, ErrorCode from WebPagePhrase where PageID=13714 order by PhraseOrder");
            result = connection->Query("select CookieValue, CookieDomain from Cookies where PageID=13714");
            result = connection->Query("select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=0");
            result = connection->Query("select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=1");
            result = connection->Query("select URL, RequiredText from WebPageExtraFiles where PageID=13714 and Type=2");
            // close the db connection
            connection->Close();
            delete connection;
            connection = NULL;
            Trace("Disonnected from db", "", repNo);

            Trace("Connecting to db", "", repNo);
            connection = new Connection(CConfig::getDBServer(), CConfig::getDBDatabase(),
                                        CConfig::getDBUser(), CConfig::getDBPass(), CConfig::getDBRetryTime());
            result = connection->Query("INSERT INTO ResultHeader040303 (ResultID,PageID,TestServer,StartDateTime,ResultCode,DNSTime,FirstByteTime,CompleteTime,TotalBytes,RunID,BatchID,FirstDataTime) VALUES (0,13714,10,'2004-03-03 14:39:20',1,8,9,5309,25291,0,0,15)");
            result = connection->Query("SELECT LAST_INSERT_ID() from ResultHeader040303 where ResultID=LAST_INSERT_ID()");
            result = connection->Query("INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:20',0,8,9,15,892,200,4338,'http://192.168.1.5',1)");
            result = connection->Query("INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1012,1,1,7,4188,200,6543,'http://192.168.1.5/g/p10.png',2)");
            result = connection->Query("INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1025,0,2,3,4185,200,12237,'http://192.168.1.5/g/MYSQLAuthCSP_small.png',3)");
            result = connection->Query("INSERT INTO ResultDetail040303 (ResultID,StartDateTime,StartOffset,DNSTime,FirstByteTime,FirstDataTime,CompleteTime,ResultCode,TotalBytes,FileName,ComponentNo) VALUES (40,'2004-03-03 14:39:21',1008,1,2,6,4301,200,2173,'http://192.168.1.5/g/newlogo.png',4)");
            result = connection->Query("SELECT PageID from WebPageStatus where PageID=13714");
            result = connection->Query("UPDATE WebPageStatus set LastResultID=40,StartDateTime='2004-03-03 14:39:20',LastResultCode=1,CompleteTime=5309 where PageID=13714");
            result = connection->Query("SELECT WS.ErrorStatus,WS.CurrentErrorRef,WS.AlertCount,C.AlertLevel2Count,C.AlertLevel3Count,C.AlertCount,W.OverallAlertCount from Customer C, WebPage W, WebPageStatus WS WHERE C.CustomerNo=W.CustomerNo and W.PageID=WS.PageID and W.PageID=13714");
            result = connection->Query("UPDATE WebPageStatus SET ErrorStatus=0,CurrentErrorRef=0,AlertCount=0 WHERE PageID=13714");
            result = connection->Query("UPDATE WebPageStatus SET OverallAlertCount=0 WHERE PageID=13714");
            result = connection->Query("select ErrorRef from WebPageError where PageID=13714 and Status<>2");
            // close the db connection
            connection->Close();
            delete connection;
            connection = NULL;
            Trace("Disonnected from db", "", repNo);

        }
        catch (const std::exception &e)
        {
            printf("Exception caught: - %s\r\n", e.what());
            // all done - exit the thread
//			pthread_exit(NULL);
        }

        Trace("Sleeping", "", 100 * sleepTime);
        usleep(100 * sleepTime);
    }

    mysql_thread_end();

    // all done - exit the thread
    pthread_exit(NULL);

    Trace("After Thread end", "", 0);
}
Пример #26
0
void *process_queue(struct thread_data *td) {
	struct configuration *conf= td->conf;
	g_mutex_lock(init_mutex);
	MYSQL *thrconn= mysql_init(NULL);
	g_mutex_unlock(init_mutex);

	configure_connection(thrconn,"myloader");

	if (!mysql_real_connect(thrconn, hostname, username, password, NULL, port, socket_path, 0)) {
		g_critical("Failed to connect to MySQL server: %s", mysql_error(thrconn));
		exit(EXIT_FAILURE);
	}

	if (mysql_query(thrconn, "SET SESSION wait_timeout = 2147483")){
		g_warning("Failed to increase wait_timeout: %s", mysql_error(thrconn));
	}

	if (!enable_binlog)
		mysql_query(thrconn, "SET SQL_LOG_BIN=0");

	mysql_query(thrconn, "/*!40101 SET NAMES binary*/");
	mysql_query(thrconn, "/*!40101 SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */");
	mysql_query(thrconn, "/*!40014 SET UNIQUE_CHECKS=0 */");
	mysql_query(thrconn, "SET autocommit=0");

	g_async_queue_push(conf->ready, GINT_TO_POINTER(1));

	struct job* job= NULL;
	struct restore_job* rj= NULL;
	for(;;) {
		job= (struct job*)g_async_queue_pop(conf->queue);

		switch (job->type) {
			case JOB_RESTORE:
				rj= (struct restore_job *)job->job_data;
				g_message("Thread %d restoring `%s`.`%s` part %d", td->thread_id, rj->database, rj->table, rj->part);
				restore_data(thrconn, rj->database, rj->table, rj->filename, FALSE, TRUE);
				if (rj->database) g_free(rj->database);
				if (rj->table) g_free(rj->table);
				if (rj->filename) g_free(rj->filename);
				g_free(rj);
				g_free(job);
				break;
			case JOB_SHUTDOWN:
				g_message("Thread %d shutting down", td->thread_id);
				if (thrconn)
					mysql_close(thrconn);
				g_free(job);
				mysql_thread_end();
				return NULL;
				break;
			default:
				g_critical("Something very bad happened!");
				exit(EXIT_FAILURE);
		}
	}
	if (thrconn)
		mysql_close(thrconn);
	mysql_thread_end();
	return NULL;
}
Пример #27
0
void *process(void *path) {
  char i_char[23];
  char j_char[23];
  char k_char[23];
  char t_char = '0';
  char line[512] = "0";
  int i = 0;
  FILE *f;

  getxattr(path, XATTR_ANALYZER, &t_char, 1);
  printf("\nProcessing file %s \n", path); 
  printf("  attr %s is %d\n", XATTR_ANALYZER, t_char - '0');
  switch(t_char - '0') {
    case X_A_DONE:
      mark_done(path);
      break;
    case X_A_INPROGRESS:
    case X_A_NONE:
      memset(i_char, 0, 23);
      memset(j_char, 0, 23);
      memset(k_char, 0, 23);
      getxattr(path, XATTR_ANALYZER_POS, i_char, 23);
      getxattr(path, XATTR_ANALYZER_POS_TS, j_char, 23);
      printf("  attr %s is %s\n", XATTR_ANALYZER_POS, i_char);
      printf("  attr %s is %s\n", XATTR_ANALYZER_POS_TS, j_char);
      f = fopen(path, "r");
      if(f == NULL) {
        fprintf(stderr, "Error when openning file %s\n", path);
        perror("fopen");
        return;
      }
      if(strlen(i_char) != 0) {
        fseek(f, atol(i_char), SEEK_SET);
        printf("  Moved to pos %d\n", atol(i_char));
        if(fgets(line, 512, f) == NULL) {
          fclose(f);
          return;
        }
        if(strlen(j_char) != 0 && strncmp(line, j_char, strlen(j_char)) !=0) {
          printf("  The position is not matched: %s, %s", j_char, line);
          fclose(f);
          return;
        } 
      }
      if(fgets(line, 512, f) == NULL) {
        printf("  End of the file or error occured, will read this file again after 1 min!\n");
        for(i = 0; i < 60; i++) {
          sleep(1);
          if(killed) {
            fclose(f);
            return;
          }
        }
        if(fgets(line, 512, f) == NULL) {
          printf("  Got EOF in second read, marking this file as finished!\n");
          mark_done(path);
          fclose(f);
          return;
        }
      }
      do {
        if(read_line(line, j_char) == -1) {
          continue; 
        }
        strcpy(k_char, i_char);
        sprintf(i_char, "%d", ftell(f));
        printf("  Finished line start with %s, location was %s, location is %s, killed is %d\n", j_char, k_char, i_char, killed);
        if(i++ == 600) {
          mark_pos(path, k_char, j_char);
          i = 0;
        }
        //sleep(1);
      } while(fgets(line, 512, f) != NULL && (killed == 0));
      mark_pos(path, k_char, j_char);

      MYSQL *conn = pthread_getspecific(my_con);
      if(conn != NULL) mysql_close(conn);
      mysql_thread_end();
      fclose(f);
      return;
  }
}