Files
@ 479a5c9e3a02
Branch filter:
Location: DistRen/src/server/mysql.c - annotation
479a5c9e3a02
8.7 KiB
text/plain
Move mysql connectino configuration to distrendaemon.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | 742325b16d40 abcf8952747b 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 a1fb4d1ec4fa 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 742325b16d40 9b974d776960 9b974d776960 9b974d776960 9b974d776960 9b974d776960 d5f4334ff4e6 9b974d776960 f5a1573ebc08 f5a1573ebc08 f5a1573ebc08 d7bff2b0cd98 f5a1573ebc08 d7bff2b0cd98 f5a1573ebc08 f5a1573ebc08 f5a1573ebc08 f5a1573ebc08 f5a1573ebc08 f5a1573ebc08 f5a1573ebc08 93e5a4f8f153 f5a1573ebc08 5cc750f10d9d 5cc750f10d9d 5cc750f10d9d 5cc750f10d9d 5cc750f10d9d 5cc750f10d9d fc1b152b132b fc1b152b132b 3395cf085378 fc1b152b132b 5cc750f10d9d 5cc750f10d9d 5cc750f10d9d f5a1573ebc08 f5a1573ebc08 f5a1573ebc08 9b974d776960 9b974d776960 9b974d776960 9b974d776960 9b974d776960 9b974d776960 9b974d776960 d5f4334ff4e6 d5f4334ff4e6 9b974d776960 9b974d776960 c4234d261cdb 9b974d776960 9b974d776960 9b974d776960 479a5c9e3a02 9b974d776960 9b974d776960 742325b16d40 3479dcafbf12 3479dcafbf12 9b974d776960 742325b16d40 479a5c9e3a02 9b974d776960 9b974d776960 9b974d776960 9b974d776960 742325b16d40 3479dcafbf12 3479dcafbf12 3479dcafbf12 3479dcafbf12 3479dcafbf12 868944ab533c 868944ab533c f5a1573ebc08 d5f4334ff4e6 d5f4334ff4e6 d5f4334ff4e6 d5f4334ff4e6 d5f4334ff4e6 d5f4334ff4e6 9b974d776960 9b974d776960 9b974d776960 9b974d776960 742325b16d40 742325b16d40 f5a1573ebc08 9b974d776960 9b974d776960 9b974d776960 9b974d776960 9b974d776960 f5a1573ebc08 742325b16d40 9b974d776960 9b974d776960 9b974d776960 9b974d776960 9b974d776960 9b974d776960 d5f4334ff4e6 9b974d776960 9b974d776960 9b974d776960 742325b16d40 742325b16d40 17ee4de48309 d7bff2b0cd98 9b974d776960 9b974d776960 9b974d776960 d7bff2b0cd98 9b974d776960 9b974d776960 9b974d776960 9b974d776960 9b974d776960 f5a1573ebc08 c4234d261cdb 9b974d776960 9b974d776960 868944ab533c 9b974d776960 3479dcafbf12 93e5a4f8f153 93e5a4f8f153 3479dcafbf12 868944ab533c 868944ab533c 9b974d776960 3479dcafbf12 3479dcafbf12 3479dcafbf12 3479dcafbf12 868944ab533c 9b974d776960 868944ab533c 9b974d776960 d7bff2b0cd98 8514c3a9b389 8514c3a9b389 8514c3a9b389 8514c3a9b389 8514c3a9b389 0cb5e6f974b8 0cb5e6f974b8 0cb5e6f974b8 0cb5e6f974b8 0cb5e6f974b8 0cb5e6f974b8 0cb5e6f974b8 0cb5e6f974b8 0cb5e6f974b8 0cb5e6f974b8 0cb5e6f974b8 0cb5e6f974b8 8514c3a9b389 8514c3a9b389 8514c3a9b389 8514c3a9b389 8514c3a9b389 8514c3a9b389 d7bff2b0cd98 d7bff2b0cd98 d7bff2b0cd98 d7bff2b0cd98 d7bff2b0cd98 d7bff2b0cd98 d7bff2b0cd98 d7bff2b0cd98 868944ab533c 8514c3a9b389 8514c3a9b389 8514c3a9b389 8514c3a9b389 9b974d776960 d5f4334ff4e6 d5f4334ff4e6 93e5a4f8f153 93e5a4f8f153 8d3babc6275c d5f4334ff4e6 d5f4334ff4e6 c4234d261cdb d5f4334ff4e6 d5f4334ff4e6 c4234d261cdb 9b974d776960 9b974d776960 9b974d776960 93e5a4f8f153 d5f4334ff4e6 489f33eeccf4 93e5a4f8f153 489f33eeccf4 f5a1573ebc08 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 d5f4334ff4e6 d5f4334ff4e6 d5f4334ff4e6 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 489f33eeccf4 489f33eeccf4 489f33eeccf4 489f33eeccf4 93e5a4f8f153 489f33eeccf4 489f33eeccf4 489f33eeccf4 3479dcafbf12 489f33eeccf4 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 93e5a4f8f153 d5f4334ff4e6 d5f4334ff4e6 d5f4334ff4e6 a1fb4d1ec4fa f5a1573ebc08 f5a1573ebc08 a1fb4d1ec4fa a1fb4d1ec4fa c4234d261cdb a1fb4d1ec4fa 50915ba5beb7 a1fb4d1ec4fa c4234d261cdb c4234d261cdb d7bff2b0cd98 f5a1573ebc08 c4234d261cdb c4234d261cdb c4234d261cdb c4234d261cdb c4234d261cdb c4234d261cdb c4234d261cdb c4234d261cdb d7bff2b0cd98 c4234d261cdb 50915ba5beb7 50915ba5beb7 dd687d0e8de0 dd687d0e8de0 dd687d0e8de0 dd687d0e8de0 b8f62f7ddf7f dd687d0e8de0 dd687d0e8de0 dd687d0e8de0 dd687d0e8de0 dd687d0e8de0 50915ba5beb7 50915ba5beb7 50915ba5beb7 a1fb4d1ec4fa c4234d261cdb 50915ba5beb7 d7bff2b0cd98 c4234d261cdb 50915ba5beb7 50915ba5beb7 a1fb4d1ec4fa 12d457aff611 50915ba5beb7 50915ba5beb7 50915ba5beb7 c4234d261cdb a1fb4d1ec4fa fc1b152b132b fc1b152b132b 1143afdf4fdd ffce970408ba d7bff2b0cd98 50915ba5beb7 a1fb4d1ec4fa 50915ba5beb7 50915ba5beb7 50915ba5beb7 5cc750f10d9d 5cc750f10d9d 83357c47ff93 93e5a4f8f153 83357c47ff93 83357c47ff93 c4234d261cdb fc1b152b132b fc1b152b132b 93e5a4f8f153 93e5a4f8f153 50915ba5beb7 fc1b152b132b a1fb4d1ec4fa 96697bb825b9 dd687d0e8de0 96697bb825b9 3725d1cf5bfc 3725d1cf5bfc 3725d1cf5bfc 3725d1cf5bfc 3725d1cf5bfc 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 dd687d0e8de0 96697bb825b9 3725d1cf5bfc 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 8d3babc6275c 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 96697bb825b9 8d3babc6275c 96697bb825b9 96697bb825b9 96697bb825b9 81f92ae54ec0 81f92ae54ec0 81f92ae54ec0 81f92ae54ec0 81f92ae54ec0 81f92ae54ec0 81f92ae54ec0 81f92ae54ec0 81f92ae54ec0 | /*
Copyright 2010 Nathan Phillip Brink, Ethan Zonca, Matthew Orlando
This file is a part of DistRen.
DistRen is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DistRen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with DistRen. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mysql.h"
#include <mysql/mysql.h>
#include "common/asprintf.h"
#include "common/protocol.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
/**
local types
*/
#define SEVENTY_FIVE 75
#define FORTY_TWO 42
/**
performs mysql query.
errors will be logged to the user by this function.
@return pointer to query handle on success, NULL on failure or if expected_columns=0 (no result set expected)
*/
distrend_mysql_result_t mysqlQuery(distrend_mysql_conn_t conn, char *query, MYSQL_FIELD_OFFSET expected_columns);
/**
frees mysql query result. Accepts a NULL pointer and ignores it to
help deal with one-shot calls to mysqlQuery so that you don't have to
check if it returned NULL or not.
@return 0 on success
*/
int mysqlResultFree(distrend_mysql_conn_t conn, distrend_mysql_result_t result);
/**
reads an integer mysql field value
@return 0 on success
*/
int distrend_mysql_getint(MYSQL_ROW row, MYSQL_FIELD_OFFSET column, int32_t *theint)
{
if(!row[column])
return 1;
*theint = atol(row[column]);
return 0;
}
struct distrend_mysql_conn
{
MYSQL *mysqlconn;
short pointlesscheck;
};
struct distrend_mysql_result
{
MYSQL_RES *mysqlresult;
short pointlesscheck;
};
/**
funcs
*/
int mysqlConnect(distrend_mysql_conn_t *conn, const char *user, const char *host, const char *pass, const char *database)
{
MYSQL *mysqlconn;
my_bool mybool;
mysqlconn = mysql_init(NULL);
if(!mysql_real_connect(mysqlconn, host, user, pass, database, 0, NULL, CLIENT_MULTI_STATEMENTS))
{
fprintf(stderr, "%s\n", mysql_error(mysqlconn));
return 1;
}
/**
Called after mysql_real_connect() due to bug fixed in MySQL 5.1.6 and later
*/
mybool = 1;
mysql_options(mysqlconn, MYSQL_OPT_RECONNECT, (char *)&mybool);
*conn = malloc(sizeof(struct distrend_mysql_conn));
if(!*conn)
{
mysql_close(mysqlconn);
mysql_server_end();
return 2;
}
(*conn)->mysqlconn = mysqlconn;
(*conn)->pointlesscheck = SEVENTY_FIVE;
return 0;
}
int mysqlDisconnect(distrend_mysql_conn_t conn)
{
/**
check if this handle is valid
*/
if(conn->pointlesscheck != SEVENTY_FIVE)
fprintf(stderr, "warning, I was passed a bad struct distrend_mysql_conn...\n");
/**
invalidate handle :-D
*/
conn->pointlesscheck ++;
mysql_close(conn->mysqlconn);
mysql_server_end();
free(conn);
return 0;
}
distrend_mysql_result_t mysqlQuery(distrend_mysql_conn_t conn, char *query, MYSQL_FIELD_OFFSET expected_columns)
{
MYSQL_RES *result;
distrend_mysql_result_t distrenresult;
MYSQL_FIELD_OFFSET num_columns;
/**
pointless sanity check
*/
if(conn->pointlesscheck != SEVENTY_FIVE)
fprintf(stderr, "warning, I was passed a bad struct distrend_mysql_conn...\n");
/** make sure that connection is still alive
*/
if(mysql_ping(conn->mysqlconn))
{
fprintf(stderr, "mysql_ping() failed: %s\n", mysql_error(conn->mysqlconn));
return NULL;
}
fprintf(stderr,"Querying... ");
if (mysql_query(conn->mysqlconn, query))
{
fprintf(stderr, "calling mysql_query() resulted in: %s\n", mysql_error(conn->mysqlconn));
return NULL;
}
fprintf(stderr,"Queried!\n");
fprintf(stderr,"Getting results... ");
result = mysql_use_result(conn->mysqlconn);
/**
Flush stuff out when an empty set is expected.
*/
if(!expected_columns)
{
if(result)
{
while(mysql_fetch_row(result))
;
mysql_free_result(result);
}
return NULL;
}
if(!result)
{
fprintf(stderr, "expected response/result for query ``%s'', got nothing\n", query);
return NULL;
}
/**
Sanity check
*/
num_columns = mysql_num_fields(result);
if(num_columns != expected_columns)
{
fprintf(stderr, "Expected %d columns, got %d for ``%s''\n", expected_columns, num_columns, query);
mysql_free_result(result);
return NULL;
}
fprintf(stderr,"Done!\n");
/**
Prepare data for return.
*/
distrenresult = malloc(sizeof(struct distrend_mysql_result));
if(!distrenresult)
{
while(mysql_fetch_row(result))
;
mysql_free_result(result);
return NULL;
}
distrenresult->mysqlresult = result;
distrenresult->pointlesscheck = FORTY_TWO;
return distrenresult;
}
int mysqlResultFree(distrend_mysql_conn_t conn, distrend_mysql_result_t result)
{
size_t counter;
MYSQL_RES *mysqlresult;
if(!result)
{
fprintf(stderr, "mysqlResultFree(): warning, passed NULL parameter\n");
return 0;
}
if(result->pointlesscheck != FORTY_TWO)
fprintf(stderr, "%s:%d: I didn't get the type of handle I wanted\n", __FILE__, __LINE__);
mysqlresult = result->mysqlresult;
free(result);
/**
Must flush the resultset buffer.
*/
for(counter = 0;
mysql_fetch_row(mysqlresult);
counter ++)
;
if(counter)
fprintf(stderr, "Calling function did not flush %d rows\n", (int)counter);
while(mysql_more_results(conn->mysqlconn))
{
fprintf(stderr, "flushing an extraneous result set\n");
mysql_next_result(conn->mysqlconn);
mysqlresult = mysql_use_result(conn->mysqlconn);
if(mysqlresult)
{
while(mysql_fetch_row(mysqlresult))
;
mysql_free_result(mysqlresult);
}
}
return 0;
}
/*
Individual query functions:
*/
void finish_frame(distrend_mysql_conn_t conn, int32_t slavekey, int32_t jobkey, int32_t framenum)
{
char *query;
_distren_asprintf(&query, "CALL `distren`.`Frame_Complete`(%d,%d,%d);", slavekey, jobkey, framenum);
mysqlQuery(conn, query, 0);
free(query);
}
void start_frame(distrend_mysql_conn_t conn, int32_t slavekey, int32_t jobkey, int32_t framenum)
{
char *query;
_distren_asprintf(&query, "CALL `distren`.`Frame_Start`(%d,%d,%d);", slavekey, jobkey, framenum);
mysqlQuery(conn, query, 0);
free(query);
}
void set_renderpower(distrend_mysql_conn_t conn, int32_t slavekey, int32_t renderpower)
{
char *query;
_distren_asprintf(&query, "CALL `distren`.`Slave_Render_Power_Update`(%d,%d);", slavekey, renderpower);
mysqlQuery(conn, query, 0);
free(query);
}
int change_job_priority(distrend_mysql_conn_t conn, int32_t jobkey, int32_t newpriority)
{
char *query;
_distren_asprintf(&query, "UPDATE `distren`.`Job` SET `Priority`=%d WHERE `Job_Key`=%d",
newpriority, jobkey);
mysqlQuery(conn, query, 0);
return 0;
}
int find_jobframe(distrend_mysql_conn_t conn, int32_t slavekey, jobnum_t *jobkey, int32_t *framenum)
{
distrend_mysql_result_t result;
char *query;
MYSQL_ROW row;
int rtn;
_distren_asprintf(&query, "CALL `distren`.`Frame_Get`(%d);", slavekey);
result = mysqlQuery(conn, query, 2);
free(query);
if(!result)
return 1;
row = mysql_fetch_row(result->mysqlresult);
if(!row)
{
mysqlResultFree(conn, result);
return 1;
}
rtn = distrend_mysql_getint(row, 0, (int32_t *)jobkey);
rtn += distrend_mysql_getint(row, 1, framenum);
mysqlResultFree(conn, result);
return rtn;
}
int auth_slave(distrend_mysql_conn_t conn, int32_t slavekey, char *slavepass)
{
// Return values:
// -- 2-user doesn't exist
// -- 1-pass is wrong
// -- 0-parameters correct
distrend_mysql_result_t result;
char *query;
MYSQL_ROW row;
int32_t *isAuth = 0;
int rtn;
mysql_real_escape_string(conn->mysqlconn,slavepass,slavepass,(unsigned int)strlen(slavepass));
_distren_asprintf(&query, "CALL `distren`.`Slave_Check`(%d,'%s');", slavekey, slavepass);
result = mysqlQuery(conn, query, 2);
free(query);
if(!result)
return 1;
row = mysql_fetch_row(result->mysqlresult);
if(!row)
{
mysqlResultFree(conn, result);
return 1;
}
rtn = distrend_mysql_getint(row, 0, (int32_t *)isAuth);
mysqlResultFree(conn, result);
return rtn;
}
/** Runs frame watchdog to reassign stale frames */
void frame_watchdog(distrend_mysql_conn_t conn)
{
char *query;
_distren_asprintf(&query, "CALL `distren`.`Watchdog_Delete`()");
mysqlQuery(conn, query, 0);
free(query);
}
|