From 0c234055bb89c0fe059140a19969ccb65d13198a Mon Sep 17 00:00:00 2001 From: kanoi Date: Mon, 11 Aug 2014 18:08:25 +1000 Subject: [PATCH 1/7] ckdb - split get_data and make all functions allow a DB connection to be passed --- src/ckdb.c | 265 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 152 insertions(+), 113 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index 586b729a..20ab608e 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -2853,6 +2853,7 @@ static int64_t workinfo_add(PGconn *conn, char *workinfoidstr, char *poolinstanc char *code, char *inet, tv_t *cd, bool igndup) { ExecStatusType rescode; + bool conned = false; K_TREE_CTX ctx[1]; PGresult *res; K_ITEM *item; @@ -2914,6 +2915,11 @@ static int64_t workinfo_add(PGconn *conn, char *workinfoidstr, char *poolinstanc "prevhash,coinbase1,coinbase2,version,bits,ntime,reward" HISTORYDATECONTROL ") values (" PQPARAM16 ")"; + if (!conn) { + conn = dbconnect(); + conned = true; + } + res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0); rescode = PQresultStatus(res); if (!PGOK(rescode)) { @@ -2925,6 +2931,9 @@ static int64_t workinfo_add(PGconn *conn, char *workinfoidstr, char *poolinstanc unparam: PQclear(res); + if (conned) + PQfinish(conn); + for (n = 0; n < par; n++) free(params[n]); @@ -3271,9 +3280,10 @@ static void zero_sharesummary(SHARESUMMARY *row, tv_t *cd) static K_ITEM *find_sharesummary(int64_t userid, char *workername, int64_t workinfoid); // Memory (and log file) only -static bool shares_add(char *workinfoid, char *username, char *workername, char *clientid, - char *enonce1, char *nonce2, char *nonce, char *diff, char *sdiff, - char *secondaryuserid, char *by, char *code, char *inet, tv_t *cd) +static bool shares_add(PGconn *conn, char *workinfoid, char *username, char *workername, + char *clientid, char *enonce1, char *nonce2, char *nonce, + char *diff, char *sdiff, char *secondaryuserid, char *by, + char *code, char *inet, tv_t *cd) { K_ITEM *s_item, *u_item, *wi_item, *w_item, *ss_item; SHARES *shares; @@ -3311,7 +3321,7 @@ static bool shares_add(char *workinfoid, char *username, char *workername, char if (!wi_item) goto unitem; - w_item = new_default_worker(NULL, false, shares->userid, shares->workername, + w_item = new_default_worker(conn, false, shares->userid, shares->workername, by, code, inet, cd); if (!w_item) goto unitem; @@ -3335,7 +3345,7 @@ static bool shares_add(char *workinfoid, char *username, char *workername, char workerstatus_update(NULL, shares, NULL, NULL); - sharesummary_update(NULL, shares, NULL, NULL, by, code, inet, cd); + sharesummary_update(conn, shares, NULL, NULL, by, code, inet, cd); ok = true; unitem: @@ -3381,9 +3391,10 @@ static cmp_t cmp_shareerrors(K_ITEM *a, K_ITEM *b) // Memory (and log file) only // TODO: handle shareerrors that appear after a workinfoid is aged or doesn't exist? -static bool shareerrors_add(char *workinfoid, char *username, char *workername, - char *clientid, char *errn, char *error, char *secondaryuserid, - char *by, char *code, char *inet, tv_t *cd) +static bool shareerrors_add(PGconn *conn, char *workinfoid, char *username, + char *workername, char *clientid, char *errn, + char *error, char *secondaryuserid, char *by, + char *code, char *inet, tv_t *cd) { K_ITEM *s_item, *u_item, *wi_item, *w_item, *ss_item; SHAREERRORS *shareerrors; @@ -3440,7 +3451,7 @@ static bool shareerrors_add(char *workinfoid, char *username, char *workername, } } - sharesummary_update(NULL, NULL, shareerrors, NULL, by, code, inet, cd); + sharesummary_update(conn, NULL, shareerrors, NULL, by, code, inet, cd); ok = true; unitem: @@ -4365,6 +4376,7 @@ static char *auths_add(PGconn *conn, char *poolinstance, char *username, tv_t *cd, bool igndup) { ExecStatusType rescode; + bool conned = false; PGresult *res; K_TREE_CTX ctx[1]; K_ITEM *a_item, *u_item; @@ -4411,6 +4423,11 @@ static char *auths_add(PGconn *conn, char *poolinstance, char *username, // Update even if DB fails workerstatus_update(row, NULL, NULL, NULL); + if (conn == NULL) { + conn = dbconnect(); + conned = true; + } + row->authid = nextid(conn, "authid", (int64_t)1, cd, by, code, inet); if (row->authid == 0) goto unitem; @@ -4444,6 +4461,8 @@ unparam: for (n = 0; n < par; n++) free(params[n]); unitem: + if (conned) + PQfinish(conn); K_WLOCK(auths_free); if (!secuserid) k_add_head(auths_free, a_item); @@ -5339,7 +5358,7 @@ static bool check_db_version(PGconn *conn) return true; } -static bool getdata() +static bool getdata1() { PGconn *conn = dbconnect(); bool ok = true; @@ -5350,25 +5369,36 @@ static bool getdata() goto matane; if (!(ok = workers_fill(conn))) goto matane; + ok = auths_fill(conn); + +matane: + + PQfinish(conn); + return ok; +} + +static bool getdata2() +{ + PGconn *conn = dbconnect(); + bool ok = true; + + if (!(ok = blocks_fill(conn))) + goto sukamudai; if (!(ok = payments_fill(conn))) - goto matane; + goto sukamudai; if (!(ok = workinfo_fill(conn))) - goto matane; + goto sukamudai; if (!(ok = shares_fill())) - goto matane; + goto sukamudai; if (!(ok = shareerrors_fill())) - goto matane; + goto sukamudai; if (!(ok = sharesummary_fill(conn))) - goto matane; - if (!(ok = blocks_fill(conn))) - goto matane; - if (!(ok = auths_fill(conn))) - goto matane; + goto sukamudai; if (!(ok = poolstats_fill(conn))) - goto matane; + goto sukamudai; ok = userstats_fill(conn); -matane: +sukamudai: PQfinish(conn); return ok; @@ -5623,7 +5653,10 @@ static bool setup_data() workerstatus_store = k_new_store(workerstatus_free); workerstatus_root = new_ktree(); - if (!getdata()) + if (!getdata1()) + return false; + + if (!getdata2()) return false; db_load_complete = true; @@ -5651,14 +5684,13 @@ static bool setup_data() return true; } -static char *cmd_adduser(char *cmd, char *id, tv_t *now, char *by, char *code, char *inet, - __maybe_unused tv_t *notcd) +static char *cmd_adduser(PGconn *conn, char *cmd, char *id, tv_t *now, char *by, + char *code, char *inet, __maybe_unused tv_t *notcd) { char reply[1024] = ""; size_t siz = sizeof(reply); K_ITEM *i_username, *i_emailaddress, *i_passwordhash; - PGconn *conn; bool ok; LOGDEBUG("%s(): cmd '%s'", __func__, cmd); @@ -5675,12 +5707,10 @@ static char *cmd_adduser(char *cmd, char *id, tv_t *now, char *by, char *code, c if (!i_passwordhash) return strdup(reply); - conn = dbconnect(); ok = users_add(conn, DATA_TRANSFER(i_username)->data, - DATA_TRANSFER(i_emailaddress)->data, - DATA_TRANSFER(i_passwordhash)->data, - now, by, code, inet); - PQfinish(conn); + DATA_TRANSFER(i_emailaddress)->data, + DATA_TRANSFER(i_passwordhash)->data, + now, by, code, inet); if (!ok) { LOGERR("%s.failed.DBE", id); @@ -5691,9 +5721,10 @@ static char *cmd_adduser(char *cmd, char *id, tv_t *now, char *by, char *code, c return strdup(reply); } -static char *cmd_chkpass(char *cmd, char *id, __maybe_unused tv_t *now, __maybe_unused char *by, - __maybe_unused char *code, __maybe_unused char *inet, - __maybe_unused tv_t *notcd) +static char *cmd_chkpass(__maybe_unused PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *now, __maybe_unused char *by, + __maybe_unused char *code, __maybe_unused char *inet, + __maybe_unused tv_t *notcd) { K_ITEM *i_username, *i_passwordhash, *u_item; char reply[1024] = ""; @@ -5729,13 +5760,12 @@ static char *cmd_chkpass(char *cmd, char *id, __maybe_unused tv_t *now, __maybe_ return strdup("ok."); } -static char *cmd_poolstats_do(char *cmd, char *id, char *by, char *code, - char *inet, tv_t *cd, bool igndup) +static char *cmd_poolstats_do(PGconn *conn, char *cmd, char *id, char *by, + char *code, char *inet, tv_t *cd, bool igndup) { char reply[1024] = ""; size_t siz = sizeof(reply); K_TREE_CTX ctx[1]; - PGconn *conn; bool store; // log to logfile @@ -5803,7 +5833,6 @@ static char *cmd_poolstats_do(char *cmd, char *id, char *by, char *code, store = false; } - conn = dbconnect(); ok = poolstats_add(conn, store, DATA_TRANSFER(i_poolinstance)->data, DATA_TRANSFER(i_elapsed)->data, DATA_TRANSFER(i_users)->data, @@ -5813,7 +5842,6 @@ static char *cmd_poolstats_do(char *cmd, char *id, char *by, char *code, DATA_TRANSFER(i_hashrate1hr)->data, DATA_TRANSFER(i_hashrate24hr)->data, by, code, inet, cd, igndup); - PQfinish(conn); if (!ok) { LOGERR("%s.failed.DBE", id); @@ -5824,8 +5852,9 @@ static char *cmd_poolstats_do(char *cmd, char *id, char *by, char *code, return strdup(reply); } -static char *cmd_poolstats(char *cmd, char *id, __maybe_unused tv_t *notnow, - char *by, char *code, char *inet, tv_t *cd) +static char *cmd_poolstats(PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *notnow, char *by, + char *code, char *inet, tv_t *cd) { bool igndup = false; @@ -5836,11 +5865,12 @@ static char *cmd_poolstats(char *cmd, char *id, __maybe_unused tv_t *notnow, return NULL; } - return cmd_poolstats_do(cmd, id, by, code, inet, cd, igndup); + return cmd_poolstats_do(conn, cmd, id, by, code, inet, cd, igndup); } -static char *cmd_userstats(char *cmd, char *id, __maybe_unused tv_t *notnow, - char *by, char *code, char *inet, tv_t *cd) +static char *cmd_userstats(__maybe_unused PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *notnow, char *by, char *code, + char *inet, tv_t *cd) { char reply[1024] = ""; size_t siz = sizeof(reply); @@ -5917,8 +5947,8 @@ static char *cmd_userstats(char *cmd, char *id, __maybe_unused tv_t *notnow, return strdup(reply); } -static char *cmd_newid(char *cmd, char *id, tv_t *now, char *by, char *code, char *inet, - __maybe_unused tv_t *cd) +static char *cmd_newid(PGconn *conn, char *cmd, char *id, tv_t *now, char *by, + char *code, char *inet, __maybe_unused tv_t *cd) { char reply[1024] = ""; size_t siz = sizeof(reply); @@ -5928,8 +5958,8 @@ static char *cmd_newid(char *cmd, char *id, tv_t *now, char *by, char *code, cha int par; bool ok = false; ExecStatusType rescode; + bool conned = false; PGresult *res; - PGconn *conn; char *ins; int n; @@ -5962,7 +5992,10 @@ static char *cmd_newid(char *cmd, char *id, tv_t *now, char *by, char *code, cha ins = "insert into idcontrol " "(idname,lastid" MODIFYDATECONTROL ") values (" PQPARAM10 ")"; - conn = dbconnect(); + if (!conn) { + conn = dbconnect(); + conned = true; + } res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0); rescode = PQresultStatus(res); @@ -5974,7 +6007,8 @@ static char *cmd_newid(char *cmd, char *id, tv_t *now, char *by, char *code, cha ok = true; foil: PQclear(res); - PQfinish(conn); + if (conned) + PQfinish(conn); for (n = 0; n < par; n++) free(params[n]); @@ -5992,9 +6026,10 @@ foil: return strdup(reply); } -static char *cmd_payments(char *cmd, char *id, __maybe_unused tv_t *now, __maybe_unused char *by, - __maybe_unused char *code, __maybe_unused char *inet, - __maybe_unused tv_t *notcd) +static char *cmd_payments(__maybe_unused PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *now, __maybe_unused char *by, + __maybe_unused char *code, __maybe_unused char *inet, + __maybe_unused tv_t *notcd) { K_ITEM *i_username, look, *u_item, *p_item; K_TREE_CTX ctx[1]; @@ -6047,9 +6082,10 @@ static char *cmd_payments(char *cmd, char *id, __maybe_unused tv_t *now, __maybe return buf; } -static char *cmd_workers(char *cmd, char *id, __maybe_unused tv_t *now, __maybe_unused char *by, - __maybe_unused char *code, __maybe_unused char *inet, - __maybe_unused tv_t *notcd) +static char *cmd_workers(__maybe_unused PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *now, __maybe_unused char *by, + __maybe_unused char *code, __maybe_unused char *inet, + __maybe_unused tv_t *notcd) { K_ITEM *i_username, *i_stats, wlook, *u_item, *w_item, uslook, *us_item, *ws_item; K_TREE_CTX w_ctx[1], us_ctx[1]; @@ -6184,9 +6220,10 @@ static char *cmd_workers(char *cmd, char *id, __maybe_unused tv_t *now, __maybe_ return buf; } -static char *cmd_allusers(char *cmd, char *id, __maybe_unused tv_t *now, __maybe_unused char *by, - __maybe_unused char *code, __maybe_unused char *inet, - __maybe_unused tv_t *notcd) +static char *cmd_allusers(__maybe_unused PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *now, __maybe_unused char *by, + __maybe_unused char *code, __maybe_unused char *inet, + __maybe_unused tv_t *notcd) { K_TREE *userstats_workername_root = new_ktree(); K_ITEM *us_item, *usw_item, *tmp_item, *u_item; @@ -6289,12 +6326,12 @@ static char *cmd_allusers(char *cmd, char *id, __maybe_unused tv_t *now, __maybe return buf; } -static char *cmd_sharelog(char *cmd, char *id, __maybe_unused tv_t *notnow, - char *by, char *code, char *inet, tv_t *cd) +static char *cmd_sharelog(PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *notnow, char *by, + char *code, char *inet, tv_t *cd) { char reply[1024] = ""; size_t siz = sizeof(reply); - PGconn *conn; // log to logfile with processing success/failure code @@ -6358,7 +6395,6 @@ static char *cmd_sharelog(char *cmd, char *id, __maybe_unused tv_t *notnow, if (!i_reward) return strdup(reply); - conn = dbconnect(); workinfoid = workinfo_add(conn, DATA_TRANSFER(i_workinfoid)->data, DATA_TRANSFER(i_poolinstance)->data, DATA_TRANSFER(i_transactiontree)->data, @@ -6371,7 +6407,6 @@ static char *cmd_sharelog(char *cmd, char *id, __maybe_unused tv_t *notnow, DATA_TRANSFER(i_ntime)->data, DATA_TRANSFER(i_reward)->data, by, code, inet, cd, igndup); - PQfinish(conn); if (workinfoid == -1) { LOGERR("%s.failed.DBE", id); @@ -6431,17 +6466,17 @@ static char *cmd_sharelog(char *cmd, char *id, __maybe_unused tv_t *notnow, if (!i_secondaryuserid) return strdup(reply); - ok = shares_add(DATA_TRANSFER(i_workinfoid)->data, - DATA_TRANSFER(i_username)->data, - DATA_TRANSFER(i_workername)->data, - DATA_TRANSFER(i_clientid)->data, - DATA_TRANSFER(i_enonce1)->data, - DATA_TRANSFER(i_nonce2)->data, - DATA_TRANSFER(i_nonce)->data, - DATA_TRANSFER(i_diff)->data, - DATA_TRANSFER(i_sdiff)->data, - DATA_TRANSFER(i_secondaryuserid)->data, - by, code, inet, cd); + ok = shares_add(conn, DATA_TRANSFER(i_workinfoid)->data, + DATA_TRANSFER(i_username)->data, + DATA_TRANSFER(i_workername)->data, + DATA_TRANSFER(i_clientid)->data, + DATA_TRANSFER(i_enonce1)->data, + DATA_TRANSFER(i_nonce2)->data, + DATA_TRANSFER(i_nonce)->data, + DATA_TRANSFER(i_diff)->data, + DATA_TRANSFER(i_sdiff)->data, + DATA_TRANSFER(i_secondaryuserid)->data, + by, code, inet, cd); if (!ok) { LOGERR("%s.failed.DATA", id); @@ -6489,14 +6524,14 @@ static char *cmd_sharelog(char *cmd, char *id, __maybe_unused tv_t *notnow, if (!i_secondaryuserid) return strdup(reply); - ok = shareerrors_add(DATA_TRANSFER(i_workinfoid)->data, - DATA_TRANSFER(i_username)->data, - DATA_TRANSFER(i_workername)->data, - DATA_TRANSFER(i_clientid)->data, - DATA_TRANSFER(i_errn)->data, - DATA_TRANSFER(i_error)->data, - DATA_TRANSFER(i_secondaryuserid)->data, - by, code, inet, cd); + ok = shareerrors_add(conn, DATA_TRANSFER(i_workinfoid)->data, + DATA_TRANSFER(i_username)->data, + DATA_TRANSFER(i_workername)->data, + DATA_TRANSFER(i_clientid)->data, + DATA_TRANSFER(i_errn)->data, + DATA_TRANSFER(i_error)->data, + DATA_TRANSFER(i_secondaryuserid)->data, + by, code, inet, cd); if (!ok) { LOGERR("%s.failed.DATA", id); return strdup("failed.DATA"); @@ -6521,7 +6556,7 @@ static char *cmd_sharelog(char *cmd, char *id, __maybe_unused tv_t *notnow, if (!i_poolinstance) return strdup(reply); - ok = workinfo_age(NULL, DATA_TRANSFER(i_workinfoid)->data, + ok = workinfo_age(conn, DATA_TRANSFER(i_workinfoid)->data, DATA_TRANSFER(i_poolinstance)->data, by, code, inet, cd); @@ -6543,12 +6578,11 @@ static char *cmd_sharelog(char *cmd, char *id, __maybe_unused tv_t *notnow, } // TODO: the confirm update: identify block changes from workinfo height? -static char *cmd_blocks_do(char *cmd, char *id, char *by, char *code, char *inet, - tv_t *cd, bool igndup) +static char *cmd_blocks_do(PGconn *conn, char *cmd, char *id, char *by, + char *code, char *inet, tv_t *cd, bool igndup) { char reply[1024] = ""; size_t siz = sizeof(reply); - PGconn *conn; K_ITEM *i_height, *i_blockhash, *i_confirmed, *i_workinfoid, *i_username; K_ITEM *i_workername, *i_clientid, *i_enonce1, *i_nonce2, *i_nonce, *i_reward; char *msg; @@ -6604,7 +6638,6 @@ static char *cmd_blocks_do(char *cmd, char *id, char *by, char *code, char *inet return strdup(reply); msg = "added"; - conn = dbconnect(); ok = blocks_add(conn, DATA_TRANSFER(i_height)->data, DATA_TRANSFER(i_blockhash)->data, DATA_TRANSFER(i_confirmed)->data, @@ -6620,7 +6653,6 @@ static char *cmd_blocks_do(char *cmd, char *id, char *by, char *code, char *inet break; case BLOCKS_CONFIRM: msg = "confirmed"; - conn = dbconnect(); ok = blocks_add(conn, DATA_TRANSFER(i_height)->data, DATA_TRANSFER(i_blockhash)->data, DATA_TRANSFER(i_confirmed)->data, @@ -6634,8 +6666,6 @@ static char *cmd_blocks_do(char *cmd, char *id, char *by, char *code, char *inet return strdup("failed.DATA"); } - PQfinish(conn); - if (!ok) { LOGERR("%s.failed.DBE", id); return strdup("failed.DBE"); @@ -6646,8 +6676,9 @@ static char *cmd_blocks_do(char *cmd, char *id, char *by, char *code, char *inet return strdup(reply); } -static char *cmd_blocks(char *cmd, char *id, __maybe_unused tv_t *notnow, - char *by, char *code, char *inet, tv_t *cd) +static char *cmd_blocks(PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *notnow, char *by, + char *code, char *inet, tv_t *cd) { bool igndup = false; @@ -6658,15 +6689,14 @@ static char *cmd_blocks(char *cmd, char *id, __maybe_unused tv_t *notnow, return NULL; } - return cmd_blocks_do(cmd, id, by, code, inet, cd, igndup); + return cmd_blocks_do(conn, cmd, id, by, code, inet, cd, igndup); } -static char *cmd_auth_do(char *cmd, char *id, __maybe_unused tv_t *now, char *by, +static char *cmd_auth_do(PGconn *conn, char *cmd, char *id, char *by, char *code, char *inet, tv_t *cd, bool igndup) { char reply[1024] = ""; size_t siz = sizeof(reply); - PGconn *conn; K_ITEM *i_poolinstance, *i_username, *i_workername, *i_clientid; K_ITEM *i_enonce1, *i_useragent; char *secuserid; @@ -6697,7 +6727,6 @@ static char *cmd_auth_do(char *cmd, char *id, __maybe_unused tv_t *now, char *by if (!i_useragent) return strdup(reply); - conn = dbconnect(); secuserid = auths_add(conn, DATA_TRANSFER(i_poolinstance)->data, DATA_TRANSFER(i_username)->data, DATA_TRANSFER(i_workername)->data, @@ -6705,7 +6734,6 @@ static char *cmd_auth_do(char *cmd, char *id, __maybe_unused tv_t *now, char *by DATA_TRANSFER(i_enonce1)->data, DATA_TRANSFER(i_useragent)->data, by, code, inet, cd, igndup); - PQfinish(conn); if (!secuserid) { LOGDEBUG("%s.failed.DBE", id); @@ -6717,8 +6745,9 @@ static char *cmd_auth_do(char *cmd, char *id, __maybe_unused tv_t *now, char *by return strdup(reply); } -static char *cmd_auth(char *cmd, char *id, tv_t *now, char *by, - char *code, char *inet, tv_t *cd) +static char *cmd_auth(PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *now, char *by, + char *code, char *inet, tv_t *cd) { bool igndup = false; @@ -6729,12 +6758,13 @@ static char *cmd_auth(char *cmd, char *id, tv_t *now, char *by, return NULL; } - return cmd_auth_do(cmd, id, now, by, code, inet, cd, igndup); + return cmd_auth_do(conn, cmd, id, by, code, inet, cd, igndup); } -static char *cmd_homepage(char *cmd, char *id, __maybe_unused tv_t *now, __maybe_unused char *by, - __maybe_unused char *code, __maybe_unused char *inet, - __maybe_unused tv_t *notcd) +static char *cmd_homepage(__maybe_unused PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *now, __maybe_unused char *by, + __maybe_unused char *code, __maybe_unused char *inet, + __maybe_unused tv_t *notcd) { K_ITEM *i_username, *u_item, *b_item, *p_item, *us_item, look; double u_hashrate5m, u_hashrate1hr; @@ -6868,9 +6898,10 @@ static char *cmd_homepage(char *cmd, char *id, __maybe_unused tv_t *now, __maybe return buf; } -static char *cmd_dsp(char *cmd, char *id, __maybe_unused tv_t *now, - __maybe_unused char *by, __maybe_unused char *code, - __maybe_unused char *inet, __maybe_unused tv_t *notcd) +static char *cmd_dsp(__maybe_unused PGconn *conn, __maybe_unused char *cmd, + char *id, __maybe_unused tv_t *now, + __maybe_unused char *by, __maybe_unused char *code, + __maybe_unused char *inet, __maybe_unused tv_t *notcd) { __maybe_unused K_ITEM *i_file; __maybe_unused char reply[1024] = ""; @@ -6897,9 +6928,10 @@ static char *cmd_dsp(char *cmd, char *id, __maybe_unused tv_t *now, */ } -static char *cmd_stats(char *cmd, char *id, __maybe_unused tv_t *now, - __maybe_unused char *by, __maybe_unused char *code, - __maybe_unused char *inet, __maybe_unused tv_t *notcd) +static char *cmd_stats(__maybe_unused PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *now, __maybe_unused char *by, + __maybe_unused char *code, __maybe_unused char *inet, + __maybe_unused tv_t *notcd) { char tmp[1024], *buf; size_t len, off; @@ -6987,7 +7019,7 @@ static struct CMDS { char *cmd_str; bool noid; // doesn't require an id bool createdate; // requires a createdate - char *(*func)(char *, char *, tv_t *, char *, char *, char *, tv_t *); + char *(*func)(PGconn *, char *, char *, tv_t *, char *, char *, char *, tv_t *); char *access; } cmds[] = { { CMD_SHUTDOWN, "shutdown", true, false, NULL, ACCESS_SYSTEM }, @@ -7425,7 +7457,7 @@ static void *summariser(__maybe_unused void *arg) return NULL; } -static void reload_line(char *filename, uint64_t count, char *buf) +static void reload_line(PGconn *conn, char *filename, uint64_t count, char *buf) { char cmd[CMD_SIZ+1], id[ID_SIZ+1]; enum cmd_values cmdnum; @@ -7475,7 +7507,8 @@ static void reload_line(char *filename, uint64_t count, char *buf) case CMD_POOLSTAT: case CMD_USERSTAT: case CMD_BLOCK: - ans = cmds[which_cmds].func(cmd, id, &now, (char *)"code", + ans = cmds[which_cmds].func(conn, cmd, id, &now, + (char *)"code", (char *)__func__, (char *)"127.0.0.1", &cd); if (ans) @@ -7513,6 +7546,7 @@ static void reload_line(char *filename, uint64_t count, char *buf) * when ckdb aborts at the beginning of the reload */ static void reload_from(tv_t *start) { + PGconn *conn = NULL; char buf[DATE_BUFSIZ+1], run[DATE_BUFSIZ+1]; size_t rflen = strlen(restorefrom); char *missingfirst = NULL, *missinglast = NULL; @@ -7540,6 +7574,8 @@ static void reload_from(tv_t *start) snprintf(data, sizeof(data), "reload.%s.s0", run); LOGFILE(data); + conn = dbconnect(); + total = 0; processing = 0; while (!finished) { @@ -7548,7 +7584,7 @@ static void reload_from(tv_t *start) count = 0; while (fgets_unlocked(data, MAX_READ, fp)) - reload_line(filename, ++count, data); + reload_line(conn, filename, ++count, data); if (ferror(fp)) { int err = errno; @@ -7609,6 +7645,8 @@ static void reload_from(tv_t *start) } } + PQfinish(conn); + snprintf(data, sizeof(data), "reload.%s.%"PRIu64, run, total); LOGFILE(data); LOGWARNING("%s(): read %d file%s, total %"PRIu64" line%s", @@ -7734,7 +7772,8 @@ static void *listener(void *arg) if (dup) send_unix_msg(sockd, last_reply); else { - ans = cmds[which_cmds].func(cmd, id, &now, (char *)"code", + ans = cmds[which_cmds].func(NULL, cmd, id, &now, + (char *)"code", (char *)__func__, (char *)"127.0.0.1", &cd); siz = strlen(ans) + strlen(id) + 32; From 48a891372cce21758a35105f6db07045ad9f6acd Mon Sep 17 00:00:00 2001 From: kanoi Date: Mon, 11 Aug 2014 18:16:45 +1000 Subject: [PATCH 2/7] ckdb - poolstats needs to check conn --- src/ckdb.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ckdb.c b/src/ckdb.c index 20ab608e..d21d941e 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -4614,6 +4614,7 @@ static bool poolstats_add(PGconn *conn, bool store, char *poolinstance, bool igndup) { ExecStatusType rescode; + bool conned = false; PGresult *res; K_TREE_CTX ctx[1]; K_ITEM *p_item; @@ -4671,6 +4672,11 @@ static bool poolstats_add(PGconn *conn, bool store, char *poolinstance, "hashrate5m,hashrate1hr,hashrate24hr" SIMPLEDATECONTROL ") values (" PQPARAM12 ")"; + if (!conn) { + conn = dbconnect(); + conned = true; + } + res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0); rescode = PQresultStatus(res); if (!PGOK(rescode)) { @@ -4685,6 +4691,8 @@ static bool poolstats_add(PGconn *conn, bool store, char *poolinstance, unparam: if (store) { PQclear(res); + if (conned) + PQfinish(conn); for (n = 0; n < par; n++) free(params[n]); } From e2ab8603da9591fd45d7054a9825d299d9955df0 Mon Sep 17 00:00:00 2001 From: kanoi Date: Tue, 12 Aug 2014 00:51:26 +1000 Subject: [PATCH 3/7] php - fix registration breakage --- pool/base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pool/base.php b/pool/base.php index 8903293d..97211a63 100644 --- a/pool/base.php +++ b/pool/base.php @@ -229,7 +229,7 @@ function validate() $whoid = ''; if (!isset($_SESSION['ckpkey'])) - return false; + return array(false, NULL); $key = $_SESSION['ckpkey']; if (!isset($_SESSION[$key])) From 095402b069ba4c197b4ed2238e00d962ef416624 Mon Sep 17 00:00:00 2001 From: kanoi Date: Tue, 12 Aug 2014 00:56:43 +1000 Subject: [PATCH 4/7] ckdb - fix nextid breakage --- src/ckdb.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ckdb.c b/src/ckdb.c index d21d941e..1f527027 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -1742,6 +1742,7 @@ static int64_t nextid(PGconn *conn, char *idname, int64_t increment, tv_t *cd, char *by, char *code, char *inet) { ExecStatusType rescode; + bool conned = false; PGresult *res; char qry[1024]; char *params[5]; @@ -1757,6 +1758,11 @@ static int64_t nextid(PGconn *conn, char *idname, int64_t increment, "where idname='%s' for update", idname); + if (conn == NULL) { + conn = dbconnect(); + conned = true; + } + res = PQexec(conn, qry); rescode = PQresultStatus(res); if (!PGOK(rescode)) { @@ -1811,6 +1817,8 @@ static int64_t nextid(PGconn *conn, char *idname, int64_t increment, free(params[n]); cleanup: PQclear(res); + if (conned) + PQfinish(conn); return lastid; } From 1619b9b159ad0ccd4a41fd95d4610aa1a0dbd0b3 Mon Sep 17 00:00:00 2001 From: kanoi Date: Tue, 12 Aug 2014 01:15:57 +1000 Subject: [PATCH 5/7] ckdb - ensure a DB connection is always initiated before use --- src/ckdb.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/ckdb.c b/src/ckdb.c index 1f527027..bd38d44a 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -2024,6 +2024,7 @@ static bool users_add(PGconn *conn, char *username, char *emailaddress, char *pa tv_t *now, char *by, char *code, char *inet) { ExecStatusType rescode; + bool conned = false; PGresult *res; K_ITEM *item; int n; @@ -2080,6 +2081,11 @@ static bool users_add(PGconn *conn, char *username, char *emailaddress, char *pa "secondaryuserid" HISTORYDATECONTROL ") values (" PQPARAM11 ")"; + if (!conn) { + conn = dbconnect(); + conned = true; + } + res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0); rescode = PQresultStatus(res); if (!PGOK(rescode)) { @@ -2090,6 +2096,8 @@ static bool users_add(PGconn *conn, char *username, char *emailaddress, char *pa ok = true; unparam: PQclear(res); + if (conned) + PQfinish(conn); for (n = 0; n < par; n++) free(params[n]); unitem: @@ -2253,6 +2261,7 @@ static K_ITEM *workers_add(PGconn *conn, int64_t userid, char *workername, char *code, char *inet, tv_t *cd) { ExecStatusType rescode; + bool conned = false; PGresult *res; K_ITEM *item, *ret = NULL; int n; @@ -2271,6 +2280,11 @@ static K_ITEM *workers_add(PGconn *conn, int64_t userid, char *workername, row = DATA_WORKERS(item); + if (conn == NULL) { + conn = dbconnect(); + conned = true; + } + row->workerid = nextid(conn, "workerid", (int64_t)1, cd, by, code, inet); if (row->workerid == 0) goto unitem; @@ -2337,6 +2351,8 @@ unparam: for (n = 0; n < par; n++) free(params[n]); unitem: + if (conned) + PQfinish(conn); K_WLOCK(workers_free); if (!ret) k_add_head(workers_free, item); @@ -2354,6 +2370,7 @@ static bool workers_update(PGconn *conn, K_ITEM *item, char *difficultydefault, char *by, char *code, char *inet, tv_t *cd) { ExecStatusType rescode; + bool conned = false; PGresult *res; int n; WORKERS *row; @@ -2410,6 +2427,11 @@ static bool workers_update(PGconn *conn, K_ITEM *item, char *difficultydefault, params[par++] = tv_to_buf((tv_t *)&default_expiry, NULL, 0); PARCHKVAL(par, 3, params); + if (conn == NULL) { + conn = dbconnect(); + conned = true; + } + res = PQexec(conn, "Begin"); rescode = PQresultStatus(res); if (!PGOK(rescode)) { @@ -2465,6 +2487,8 @@ static bool workers_update(PGconn *conn, K_ITEM *item, char *difficultydefault, ok = true; unparam: PQclear(res); + if (conned) + PQfinish(conn); for (n = 0; n < par; n++) free(params[n]); early: @@ -2941,7 +2965,6 @@ unparam: PQclear(res); if (conned) PQfinish(conn); - for (n = 0; n < par; n++) free(params[n]); @@ -4061,6 +4084,7 @@ static bool blocks_add(PGconn *conn, char *height, char *blockhash, bool igndup, char *id) { ExecStatusType rescode; + bool conned = false; PGresult *res = NULL; K_TREE_CTX ctx[1]; K_ITEM *item, *u_item; @@ -4130,6 +4154,11 @@ static bool blocks_add(PGconn *conn, char *height, char *blockhash, "clientid,enonce1,nonce2,nonce,reward,confirmed" HISTORYDATECONTROL ") values (" PQPARAM16 ")"; + if (conn == NULL) { + conn = dbconnect(); + conned = true; + } + res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0); rescode = PQresultStatus(res); if (!PGOK(rescode)) { @@ -4146,6 +4175,11 @@ static bool blocks_add(PGconn *conn, char *height, char *blockhash, params[par++] = tv_to_buf((tv_t *)&default_expiry, NULL, 0); PARCHKVAL(par, 3, params); + if (conn == NULL) { + conn = dbconnect(); + conned = true; + } + res = PQexec(conn, "Begin"); rescode = PQresultStatus(res); if (!PGOK(rescode)) { @@ -4205,6 +4239,9 @@ unparam: for (n = 0; n < par; n++) free(params[n]); flail: + if (conned) + PQfinish(conn); + K_WLOCK(blocks_free); if (!ok) k_add_head(blocks_free, item); @@ -4944,6 +4981,7 @@ static cmp_t cmp_userstats_workerstatus(K_ITEM *a, K_ITEM *b) static bool userstats_add_db(PGconn *conn, USERSTATS *row) { ExecStatusType rescode; + bool conned = false; PGresult *res; char *ins; bool ok = false; @@ -4972,6 +5010,11 @@ static bool userstats_add_db(PGconn *conn, USERSTATS *row) "hashrate24hr,summarylevel,summarycount,statsdate" SIMPLEDATECONTROL ") values (" PQPARAM14 ")"; + if (conn == NULL) { + conn = dbconnect(); + conned = true; + } + res = PQexecParams(conn, ins, par, NULL, (const char **)params, NULL, NULL, 0); rescode = PQresultStatus(res); if (!PGOK(rescode)) { @@ -4982,6 +5025,8 @@ static bool userstats_add_db(PGconn *conn, USERSTATS *row) ok = true; unparam: PQclear(res); + if (conned) + PQfinish(conn); for (n = 0; n < par; n++) free(params[n]); From 43d8ab3b1eeea7852d1aa5362c91510c24022d14 Mon Sep 17 00:00:00 2001 From: kanoi Date: Tue, 12 Aug 2014 01:25:29 +1000 Subject: [PATCH 6/7] php - describe 'safe' --- pool/page_reg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pool/page_reg.php b/pool/page_reg.php index 5a97f3a4..7b7345da 100644 --- a/pool/page_reg.php +++ b/pool/page_reg.php @@ -96,7 +96,7 @@ function show_reg($menu, $name, $u) if ($pass === NULL || safepass($pass) !== true) { $ok = false; - $data['error'] = "Password is unsafe"; + $data['error'] = "Password is unsafe - requires 6 or more chars, including upper+lower+digits"; } elseif ($pass2 === NULL || $pass2 != $pass) { $ok = false; From 647fe51a2048227f8579b4582d23283c7018b089 Mon Sep 17 00:00:00 2001 From: kanoi Date: Tue, 12 Aug 2014 01:53:05 +1000 Subject: [PATCH 7/7] php - align hash rates better --- pool/page_workers.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pool/page_workers.php b/pool/page_workers.php index 86e10e6c..83c5da01 100644 --- a/pool/page_workers.php +++ b/pool/page_workers.php @@ -82,10 +82,15 @@ function doworker($data, $user) else { $uhr /= 10000000; - if ($uhr < 100000) - $uhr = (round($uhr)/100).'GHs'; + if ($uhr < 0.01) + $uhr = '0GHs'; else - $uhr = (round($uhr/1000)/100).'THs'; + { + if ($uhr < 100000) + $uhr = number_format(round($uhr)/100,2).'GHs'; + else + $uhr = number_format(round($uhr/1000)/100,2).'THs'; + } } $pg .= "$uhr"; $pg .= "\n";