From eadb28c76da50dab84eb1871c67ba955b0e8b33f Mon Sep 17 00:00:00 2001 From: kanoi Date: Sat, 20 Feb 2016 23:36:50 +1100 Subject: [PATCH] ckdb - implement ovents --- src/ckdb.h | 6 ++++- src/ckdb_cmd.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ckdb_data.c | 4 ++-- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/ckdb.h b/src/ckdb.h index dd50c1e4..87cedafc 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -51,7 +51,7 @@ #define DB_VLOCK "1" #define DB_VERSION "1.0.4" -#define CKDB_VERSION DB_VERSION"-1.957" +#define CKDB_VERSION DB_VERSION"-1.958" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -2143,6 +2143,8 @@ extern double ovent_limits_ipc_factor; // maximum lifetime of all o_limits - set by code extern int o_limits_max_lifetime; +#define APIKEY "KAPIKey" + // AUTHS authorise.id.json={...} typedef struct auths { int64_t authid; @@ -3078,6 +3080,8 @@ extern void ips_add(char *group, char *ip, char *eventname, bool is_event, char *des, bool log, bool cclass, int life, bool locked); extern int _events_add(int id, char *by, char *inet, tv_t *cd, K_TREE *trf_root); #define events_add(_id, _trf_root) _events_add(_id, NULL, NULL, NULL, _trf_root) +extern int _ovents_add(int id, char *by, char *inet, tv_t *cd, K_TREE *trf_root); +#define ovents_add(_id, _trf_root) _ovents_add(_id, NULL, NULL, NULL, _trf_root) extern bool auths_add(PGconn *conn, char *poolinstance, char *username, char *workername, char *clientid, char *enonce1, char *useragent, char *preauth, char *by, char *code, diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index da64f9ea..49bf09cc 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -794,6 +794,7 @@ static char *cmd_workerset(PGconn *conn, char *cmd, char *id, tv_t *now, USERATTS *useratts; WORKERS *workers; USERS *users; + int ovent = OVENT_OK, done; int32_t difficultydefault; char *reason = NULL; char *answer = NULL; @@ -802,6 +803,12 @@ static char *cmd_workerset(PGconn *conn, char *cmd, char *id, tv_t *now, LOGDEBUG("%s(): cmd '%s'", __func__, cmd); + ovent = ovents_add(OVENTID_WORKERS, trf_root); + if (ovent != OVENT_OK) { + snprintf(reply, siz, "ERR"); + return reply_ovent(ovent, reply); + } + i_username = require_name(trf_root, "username", MIN_USERNAME, (char *)userpatt, reply, siz); if (!i_username) { @@ -854,6 +861,7 @@ static char *cmd_workerset(PGconn *conn, char *cmd, char *id, tv_t *now, goto kazuki; } + done = 0; // Loop through the list of workers and do any changes for (workernum = 0; workernum < 9999; workernum++) { snprintf(workername_buf, sizeof(workername_buf), @@ -864,6 +872,17 @@ static char *cmd_workerset(PGconn *conn, char *cmd, char *id, tv_t *now, if (!i_workername) break; + // More than 1? + if (done++ == 1) { + ovent = ovents_add(OVENTID_MULTIADDR, trf_root); + if (ovent != OVENT_OK) { + if (answer) + free(answer); + snprintf(reply, siz, "ERR"); + return reply_ovent(ovent, reply); + } + } + w_item = find_workers(false, users->userid, transfer_data(i_workername)); // Abort if any dont exist @@ -928,6 +947,16 @@ static char *cmd_workerset(PGconn *conn, char *cmd, char *id, tv_t *now, K_WUNLOCK(heartbeatqueue_free); } } + // Only 1? + if (done == 1) { + ovent = ovents_add(OVENTID_ONEADDR, trf_root); + if (ovent != OVENT_OK) { + if (answer) + free(answer); + snprintf(reply, siz, "ERR"); + return reply_ovent(ovent, reply); + } + } } kazuki: @@ -1236,6 +1265,7 @@ static char *cmd_blocklist(__maybe_unused PGconn *conn, char *cmd, char *id, __maybe_unused tv_t *notcd, __maybe_unused K_TREE *trf_root) { + int ovent = OVENT_OK; K_TREE_CTX ctx[1]; K_ITEM *b_item; BLOCKS *blocks; @@ -1250,6 +1280,12 @@ static char *cmd_blocklist(__maybe_unused PGconn *conn, char *cmd, char *id, LOGDEBUG("%s(): cmd '%s'", __func__, cmd); + ovent = ovents_add(OVENTID_BLOCKS, trf_root); + if (ovent != OVENT_OK) { + snprintf(tmp, sizeof(tmp), "ERR"); + return reply_ovent(ovent, tmp); + } + maxrows = sys_setting(BLOCKS_SETTING_NAME, BLOCKS_DEFAULT, now); APPEND_REALLOC_INIT(buf, off, len); @@ -2030,6 +2066,7 @@ static char *cmd_workers(__maybe_unused PGconn *conn, char *cmd, char *id, USERSTATS *userstats; USERATTS *useratts; USERS *users; + int ovent = OVENT_OK; char reply[1024] = ""; char tmp[1024]; int64_t oldworkers = USER_OLD_WORKERS_DEFAULT; @@ -2042,6 +2079,12 @@ static char *cmd_workers(__maybe_unused PGconn *conn, char *cmd, char *id, LOGDEBUG("%s(): cmd '%s'", __func__, cmd); + ovent = ovents_add(OVENTID_WORKERS, trf_root); + if (ovent != OVENT_OK) { + snprintf(reply, siz, "ERR"); + return reply_ovent(ovent, reply); + } + i_username = adminuser(trf_root, reply, siz); if (!i_username) return strdup(reply); @@ -3287,6 +3330,7 @@ static char *cmd_homepage(__maybe_unused PGconn *conn, char *cmd, char *id, { K_ITEM *i_username, *u_item, *b_item, *p_item, *us_item, look; K_ITEM *ua_item, *pa_item; + int ovent = OVENT_OK; double u_hashrate5m, u_hashrate1hr; char reply[1024], tmp[1024], *buf; size_t siz = sizeof(reply); @@ -3302,6 +3346,12 @@ static char *cmd_homepage(__maybe_unused PGconn *conn, char *cmd, char *id, LOGDEBUG("%s(): cmd '%s'", __func__, cmd); + ovent = ovents_add(OVENTID_HOMEPAGE, trf_root); + if (ovent != OVENT_OK) { + snprintf(reply, siz, "ERR"); + return reply_ovent(ovent, reply); + } + i_username = optional_name(trf_root, "username", 1, NULL, reply, siz); APPEND_REALLOC_INIT(buf, off, len); @@ -3622,6 +3672,18 @@ static char *cmd_getatts(__maybe_unused PGconn *conn, char *cmd, char *id, goto nuts; } *(dot++) = '\0'; + if (strcmp(ptr, APIKEY) == 0) { + // API request count + event = ovents_add(OVENTID_API, trf_root); + if (event != OVENT_OK) { + if (attlist) + free(attlist); + if (answer) + free(answer); + snprintf(reply, siz, "ERR"); + return reply_ovent(event, reply); + } + } K_RLOCK(useratts_free); ua_item = find_useratts(users->userid, ptr); K_RUNLOCK(useratts_free); diff --git a/src/ckdb_data.c b/src/ckdb_data.c index 86dfcddb..00a374e1 100644 --- a/src/ckdb_data.c +++ b/src/ckdb_data.c @@ -5118,7 +5118,7 @@ int check_ovents(int id, char *u_key, char *i_key, char *c_key, tv_t *now) return OVENT_OK; } - K_WLOCK(events_free); + K_WLOCK(ovents_free); K_RLOCK(event_limits_free); if (u_key[0]) { @@ -5193,7 +5193,7 @@ int check_ovents(int id, char *u_key, char *i_key, char *c_key, tv_t *now) } } K_RUNLOCK(event_limits_free); - K_WUNLOCK(events_free); + K_WUNLOCK(ovents_free); if (alert) { LOGERR("%s() OLERT ID:%d %s Lim:%d Time:%d Life:%d '%s/%s' " "'%s' '%s'", __func__,