Browse Source

Fix memory leaks on failed api messages.

master
Con Kolivas 8 years ago
parent
commit
4108456ff4
  1. 137
      src/stratifier.c

137
src/stratifier.c

@ -3569,35 +3569,37 @@ static json_t *userinfo(const user_instance_t *user)
static void getuser(sdata_t *sdata, const char *buf, int *sockd) static void getuser(sdata_t *sdata, const char *buf, int *sockd)
{ {
json_t *val = NULL, *res = NULL;
char *username = NULL; char *username = NULL;
user_instance_t *user; user_instance_t *user;
json_error_t err_val; json_error_t err_val;
json_t *val = NULL;
val = json_loads(buf, 0, &err_val); val = json_loads(buf, 0, &err_val);
if (unlikely(!val)) { if (unlikely(!val)) {
val = json_encode_errormsg(&err_val); res = json_encode_errormsg(&err_val);
goto out; goto out;
} }
if (!json_get_string(&username, val, "user")) { if (!json_get_string(&username, val, "user")) {
val = json_errormsg("Failed to find user key"); res = json_errormsg("Failed to find user key");
goto out; goto out;
} }
if (!strlen(username)) { if (!strlen(username)) {
val = json_errormsg("Zero length user key"); res = json_errormsg("Zero length user key");
goto out; goto out;
} }
user = get_user(sdata, username); user = get_user(sdata, username);
val = userinfo(user); res = userinfo(user);
out: out:
if (val)
json_decref(val);
free(username); free(username);
send_api_response(val, *sockd); send_api_response(res, *sockd);
_Close(sockd); _Close(sockd);
} }
static void userclients(sdata_t *sdata, const char *buf, int *sockd) static void userclients(sdata_t *sdata, const char *buf, int *sockd)
{ {
json_t *val = NULL, *client_arr; json_t *val = NULL, *res = NULL, *client_arr;
stratum_instance_t *client; stratum_instance_t *client;
char *username = NULL; char *username = NULL;
user_instance_t *user; user_instance_t *user;
@ -3605,15 +3607,15 @@ static void userclients(sdata_t *sdata, const char *buf, int *sockd)
val = json_loads(buf, 0, &err_val); val = json_loads(buf, 0, &err_val);
if (unlikely(!val)) { if (unlikely(!val)) {
val = json_encode_errormsg(&err_val); res = json_encode_errormsg(&err_val);
goto out; goto out;
} }
if (!json_get_string(&username, val, "user")) { if (!json_get_string(&username, val, "user")) {
val = json_errormsg("Failed to find user key"); res = json_errormsg("Failed to find user key");
goto out; goto out;
} }
if (!strlen(username)) { if (!strlen(username)) {
val = json_errormsg("Zero length user key"); res = json_errormsg("Zero length user key");
goto out; goto out;
} }
user = get_user(sdata, username); user = get_user(sdata, username);
@ -3625,32 +3627,34 @@ static void userclients(sdata_t *sdata, const char *buf, int *sockd)
} }
ck_runlock(&sdata->instance_lock); ck_runlock(&sdata->instance_lock);
JSON_CPACK(val, "{ss,so}", "user", username, "clients", client_arr); JSON_CPACK(res, "{ss,so}", "user", username, "clients", client_arr);
out: out:
if (val)
json_decref(val);
free(username); free(username);
send_api_response(val, *sockd); send_api_response(res, *sockd);
_Close(sockd); _Close(sockd);
} }
static void workerclients(sdata_t *sdata, const char *buf, int *sockd) static void workerclients(sdata_t *sdata, const char *buf, int *sockd)
{ {
json_t *val = NULL, *res = NULL, *client_arr;
char *tmp, *username, *workername = NULL; char *tmp, *username, *workername = NULL;
json_t *val = NULL, *client_arr;
stratum_instance_t *client; stratum_instance_t *client;
user_instance_t *user; user_instance_t *user;
json_error_t err_val; json_error_t err_val;
val = json_loads(buf, 0, &err_val); val = json_loads(buf, 0, &err_val);
if (unlikely(!val)) { if (unlikely(!val)) {
val = json_encode_errormsg(&err_val); res = json_encode_errormsg(&err_val);
goto out; goto out;
} }
if (!json_get_string(&workername, val, "worker")) { if (!json_get_string(&workername, val, "worker")) {
val = json_errormsg("Failed to find worker key"); res = json_errormsg("Failed to find worker key");
goto out; goto out;
} }
if (!strlen(workername)) { if (!strlen(workername)) {
val = json_errormsg("Zero length worker key"); res = json_errormsg("Zero length worker key");
goto out; goto out;
} }
tmp = strdupa(workername); tmp = strdupa(workername);
@ -3666,10 +3670,12 @@ static void workerclients(sdata_t *sdata, const char *buf, int *sockd)
} }
ck_runlock(&sdata->instance_lock); ck_runlock(&sdata->instance_lock);
JSON_CPACK(val, "{ss,so}", "worker", workername, "clients", client_arr); JSON_CPACK(res, "{ss,so}", "worker", workername, "clients", client_arr);
out: out:
if (val)
json_decref(val);
free(workername); free(workername);
send_api_response(val, *sockd); send_api_response(res, *sockd);
_Close(sockd); _Close(sockd);
} }
@ -3688,32 +3694,34 @@ static json_t *workerinfo(const user_instance_t *user, const worker_instance_t *
static void getworker(sdata_t *sdata, const char *buf, int *sockd) static void getworker(sdata_t *sdata, const char *buf, int *sockd)
{ {
char *tmp, *username, *workername = NULL; char *tmp, *username, *workername = NULL;
json_t *val = NULL, *res = NULL;
worker_instance_t *worker; worker_instance_t *worker;
user_instance_t *user; user_instance_t *user;
json_error_t err_val; json_error_t err_val;
json_t *val = NULL;
val = json_loads(buf, 0, &err_val); val = json_loads(buf, 0, &err_val);
if (unlikely(!val)) { if (unlikely(!val)) {
val = json_encode_errormsg(&err_val); res = json_encode_errormsg(&err_val);
goto out; goto out;
} }
if (!json_get_string(&workername, val, "worker")) { if (!json_get_string(&workername, val, "worker")) {
val = json_errormsg("Failed to find worker key"); res = json_errormsg("Failed to find worker key");
goto out; goto out;
} }
if (!strlen(workername)) { if (!strlen(workername)) {
val = json_errormsg("Zero length worker key"); res = json_errormsg("Zero length worker key");
goto out; goto out;
} }
tmp = strdupa(workername); tmp = strdupa(workername);
username = strsep(&tmp, "._"); username = strsep(&tmp, "._");
user = get_user(sdata, username); user = get_user(sdata, username);
worker = get_worker(sdata, user, workername); worker = get_worker(sdata, user, workername);
val = workerinfo(user, worker); res = workerinfo(user, worker);
out: out:
if (val)
json_decref(val);
free(workername); free(workername);
send_api_response(val, *sockd); send_api_response(res, *sockd);
_Close(sockd); _Close(sockd);
} }
@ -3784,35 +3792,38 @@ static json_t *clientinfo(const stratum_instance_t *client)
json_set_double(val, "bestdiff", client->best_diff); json_set_double(val, "bestdiff", client->best_diff);
json_set_int(val, "proxyid", client->proxyid); json_set_int(val, "proxyid", client->proxyid);
json_set_int(val, "subproxyid", client->subproxyid); json_set_int(val, "subproxyid", client->subproxyid);
return val; return val;
} }
static void getclient(sdata_t *sdata, const char *buf, int *sockd) static void getclient(sdata_t *sdata, const char *buf, int *sockd)
{ {
json_t *val = NULL, *res = NULL;
stratum_instance_t *client; stratum_instance_t *client;
json_error_t err_val; json_error_t err_val;
json_t *val = NULL;
int64_t client_id; int64_t client_id;
val = json_loads(buf, 0, &err_val); val = json_loads(buf, 0, &err_val);
if (unlikely(!val)) { if (unlikely(!val)) {
val = json_encode_errormsg(&err_val); res = json_encode_errormsg(&err_val);
goto out; goto out;
} }
if (!json_get_int64(&client_id, val, "id")) { if (!json_get_int64(&client_id, val, "id")) {
val = json_errormsg("Failed to find id key"); res = json_errormsg("Failed to find id key");
goto out; goto out;
} }
client = ref_instance_by_id(sdata, client_id); client = ref_instance_by_id(sdata, client_id);
if (!client) { if (!client) {
val = json_errormsg("Failed to find client %"PRId64, client_id); res = json_errormsg("Failed to find client %"PRId64, client_id);
goto out; goto out;
} }
val = clientinfo(client); res = clientinfo(client);
dec_instance_ref(sdata, client); dec_instance_ref(sdata, client);
out: out:
send_api_response(val, *sockd); if (val)
json_decref(val);
send_api_response(res, *sockd);
_Close(sockd); _Close(sockd);
} }
@ -3836,7 +3847,7 @@ static void getclients(sdata_t *sdata, int *sockd)
static void user_clientinfo(sdata_t *sdata, const char *buf, int *sockd) static void user_clientinfo(sdata_t *sdata, const char *buf, int *sockd)
{ {
json_t *val = NULL, *client_arr; json_t *val = NULL, *res = NULL, *client_arr;
stratum_instance_t *client; stratum_instance_t *client;
char *username = NULL; char *username = NULL;
user_instance_t *user; user_instance_t *user;
@ -3844,15 +3855,15 @@ static void user_clientinfo(sdata_t *sdata, const char *buf, int *sockd)
val = json_loads(buf, 0, &err_val); val = json_loads(buf, 0, &err_val);
if (unlikely(!val)) { if (unlikely(!val)) {
val = json_encode_errormsg(&err_val); res = json_encode_errormsg(&err_val);
goto out; goto out;
} }
if (!json_get_string(&username, val, "user")) { if (!json_get_string(&username, val, "user")) {
val = json_errormsg("Failed to find user key"); res = json_errormsg("Failed to find user key");
goto out; goto out;
} }
if (!strlen(username)) { if (!strlen(username)) {
val = json_errormsg("Zero length user key"); res = json_errormsg("Zero length user key");
goto out; goto out;
} }
user = get_user(sdata, username); user = get_user(sdata, username);
@ -3864,32 +3875,34 @@ static void user_clientinfo(sdata_t *sdata, const char *buf, int *sockd)
} }
ck_runlock(&sdata->instance_lock); ck_runlock(&sdata->instance_lock);
JSON_CPACK(val, "{ss,so}", "user", username, "clients", client_arr); JSON_CPACK(res, "{ss,so}", "user", username, "clients", client_arr);
out: out:
if (val)
json_decref(val);
free(username); free(username);
send_api_response(val, *sockd); send_api_response(res, *sockd);
_Close(sockd); _Close(sockd);
} }
static void worker_clientinfo(sdata_t *sdata, const char *buf, int *sockd) static void worker_clientinfo(sdata_t *sdata, const char *buf, int *sockd)
{ {
json_t *val = NULL, *res = NULL, *client_arr;
char *tmp, *username, *workername = NULL; char *tmp, *username, *workername = NULL;
json_t *val = NULL, *client_arr;
stratum_instance_t *client; stratum_instance_t *client;
user_instance_t *user; user_instance_t *user;
json_error_t err_val; json_error_t err_val;
val = json_loads(buf, 0, &err_val); val = json_loads(buf, 0, &err_val);
if (unlikely(!val)) { if (unlikely(!val)) {
val = json_encode_errormsg(&err_val); res = json_encode_errormsg(&err_val);
goto out; goto out;
} }
if (!json_get_string(&workername, val, "worker")) { if (!json_get_string(&workername, val, "worker")) {
val = json_errormsg("Failed to find worker key"); res = json_errormsg("Failed to find worker key");
goto out; goto out;
} }
if (!strlen(workername)) { if (!strlen(workername)) {
val = json_errormsg("Zero length worker key"); res = json_errormsg("Zero length worker key");
goto out; goto out;
} }
tmp = strdupa(workername); tmp = strdupa(workername);
@ -3905,10 +3918,12 @@ static void worker_clientinfo(sdata_t *sdata, const char *buf, int *sockd)
} }
ck_runlock(&sdata->instance_lock); ck_runlock(&sdata->instance_lock);
JSON_CPACK(val, "{ss,so}", "worker", workername, "clients", client_arr); JSON_CPACK(res, "{ss,so}", "worker", workername, "clients", client_arr);
out: out:
if (val)
json_decref(val);
free(workername); free(workername);
send_api_response(val, *sockd); send_api_response(res, *sockd);
_Close(sockd); _Close(sockd);
} }
@ -3940,18 +3955,18 @@ static json_t *json_proxyinfo(const proxy_t *proxy)
static void getproxy(sdata_t *sdata, const char *buf, int *sockd) static void getproxy(sdata_t *sdata, const char *buf, int *sockd)
{ {
json_t *val = NULL, *res = NULL;
json_error_t err_val; json_error_t err_val;
json_t *val = NULL;
int id, subid = 0; int id, subid = 0;
proxy_t *proxy; proxy_t *proxy;
val = json_loads(buf, 0, &err_val); val = json_loads(buf, 0, &err_val);
if (unlikely(!val)) { if (unlikely(!val)) {
val = json_encode_errormsg(&err_val); res = json_encode_errormsg(&err_val);
goto out; goto out;
} }
if (!json_get_int(&id, val, "id")) { if (!json_get_int(&id, val, "id")) {
val = json_errormsg("Failed to find id key"); res = json_errormsg("Failed to find id key");
goto out; goto out;
} }
json_get_int(&subid, val, "subid"); json_get_int(&subid, val, "subid");
@ -3960,18 +3975,20 @@ static void getproxy(sdata_t *sdata, const char *buf, int *sockd)
else else
proxy = existing_subproxy(sdata, id, subid); proxy = existing_subproxy(sdata, id, subid);
if (!proxy) { if (!proxy) {
val = json_errormsg("Failed to find proxy %d:%d", id, subid); res = json_errormsg("Failed to find proxy %d:%d", id, subid);
goto out; goto out;
} }
val = json_proxyinfo(proxy); res = json_proxyinfo(proxy);
out: out:
send_api_response(val, *sockd); if (val)
json_decref(val);
send_api_response(res, *sockd);
_Close(sockd); _Close(sockd);
} }
static void proxyinfo(sdata_t *sdata, const char *buf, int *sockd) static void proxyinfo(sdata_t *sdata, const char *buf, int *sockd)
{ {
json_t *val = NULL, *arr_val = json_array(); json_t *val = NULL, *res = NULL, *arr_val = json_array();
proxy_t *proxy, *subproxy; proxy_t *proxy, *subproxy;
bool all = true; bool all = true;
int userid = 0; int userid = 0;
@ -3992,41 +4009,45 @@ static void proxyinfo(sdata_t *sdata, const char *buf, int *sockd)
} }
mutex_unlock(&sdata->proxy_lock); mutex_unlock(&sdata->proxy_lock);
JSON_CPACK(val, "{so}", "proxies", arr_val); if (val)
send_api_response(val, *sockd); json_decref(val);
JSON_CPACK(res, "{so}", "proxies", arr_val);
send_api_response(res, *sockd);
_Close(sockd); _Close(sockd);
} }
static void setproxy(sdata_t *sdata, const char *buf, int *sockd) static void setproxy(sdata_t *sdata, const char *buf, int *sockd)
{ {
json_t *val = NULL, *res = NULL;
json_error_t err_val; json_error_t err_val;
json_t *val = NULL;
int id, priority; int id, priority;
proxy_t *proxy; proxy_t *proxy;
val = json_loads(buf, 0, &err_val); val = json_loads(buf, 0, &err_val);
if (unlikely(!val)) { if (unlikely(!val)) {
val = json_encode_errormsg(&err_val); res = json_encode_errormsg(&err_val);
goto out; goto out;
} }
if (!json_get_int(&id, val, "id")) { if (!json_get_int(&id, val, "id")) {
val = json_errormsg("Failed to find id key"); res = json_errormsg("Failed to find id key");
goto out; goto out;
} }
if (!json_get_int(&priority, val, "priority")) { if (!json_get_int(&priority, val, "priority")) {
val = json_errormsg("Failed to find priority key"); res = json_errormsg("Failed to find priority key");
goto out; goto out;
} }
proxy = existing_proxy(sdata, id); proxy = existing_proxy(sdata, id);
if (!proxy) { if (!proxy) {
val = json_errormsg("Failed to find proxy %d", id); res = json_errormsg("Failed to find proxy %d", id);
goto out; goto out;
} }
if (priority != proxy_prio(proxy)) if (priority != proxy_prio(proxy))
set_proxy_prio(sdata, proxy, priority); set_proxy_prio(sdata, proxy, priority);
val = json_proxyinfo(proxy); res = json_proxyinfo(proxy);
out: out:
send_api_response(val, *sockd); if (val)
json_decref(val);
send_api_response(res, *sockd);
_Close(sockd); _Close(sockd);
} }

Loading…
Cancel
Save