Browse Source

Provide locking around the change of the stratum curl structures to avoid possible races.

nfactor-troky
Con Kolivas 12 years ago
parent
commit
b3d4258940
  1. 35
      util.c

35
util.c

@ -867,10 +867,9 @@ bool extract_sockaddr(struct pool *pool, char *url)
} }
/* Send a single command across a socket, appending \n to it */ /* Send a single command across a socket, appending \n to it */
bool stratum_send(struct pool *pool, char *s, ssize_t len) static bool __stratum_send(struct pool *pool, char *s, ssize_t len)
{ {
ssize_t ssent = 0; ssize_t ssent = 0;
bool ret = false;
if (opt_protocol) if (opt_protocol)
applog(LOG_DEBUG, "SEND: %s", s); applog(LOG_DEBUG, "SEND: %s", s);
@ -878,22 +877,32 @@ bool stratum_send(struct pool *pool, char *s, ssize_t len)
strcat(s, "\n"); strcat(s, "\n");
len++; len++;
mutex_lock(&pool->stratum_lock);
while (len > 0 ) { while (len > 0 ) {
size_t sent = 0; size_t sent = 0;
if (curl_easy_send(pool->stratum_curl, s + ssent, len, &sent) != CURLE_OK) { if (curl_easy_send(pool->stratum_curl, s + ssent, len, &sent) != CURLE_OK) {
applog(LOG_DEBUG, "Failed to curl_easy_send in stratum_send"); applog(LOG_DEBUG, "Failed to curl_easy_send in stratum_send");
ret = false; return false;
goto out_unlock;
} }
ssent += sent; ssent += sent;
len -= ssent; len -= ssent;
} }
ret = true;
out_unlock: return true;
}
bool stratum_send(struct pool *pool, char *s, ssize_t len)
{
bool ret = false;
mutex_lock(&pool->stratum_lock);
if (pool->stratum_active)
ret = __stratum_send(pool, s, len);
else
applog(LOG_DEBUG, "Stratum send failed due to no pool stratum_active");
mutex_unlock(&pool->stratum_lock); mutex_unlock(&pool->stratum_lock);
return ret;;
return ret;
} }
#define RECVSIZE 8191 #define RECVSIZE 8191
@ -1287,11 +1296,15 @@ bool initiate_stratum(struct pool *pool)
json_error_t err; json_error_t err;
bool ret = false; bool ret = false;
mutex_lock(&pool->stratum_lock);
pool->stratum_active = false;
if (!pool->stratum_curl) { if (!pool->stratum_curl) {
pool->stratum_curl = curl_easy_init(); pool->stratum_curl = curl_easy_init();
if (unlikely(!pool->stratum_curl)) if (unlikely(!pool->stratum_curl))
quit(1, "Failed to curl_easy_init in initiate_stratum"); quit(1, "Failed to curl_easy_init in initiate_stratum");
} }
mutex_unlock(&pool->stratum_lock);
curl = pool->stratum_curl; curl = pool->stratum_curl;
/* Create a http url for use with curl */ /* Create a http url for use with curl */
@ -1320,7 +1333,7 @@ bool initiate_stratum(struct pool *pool)
sprintf(s, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": []}", swork_id++); sprintf(s, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": []}", swork_id++);
if (!stratum_send(pool, s, strlen(s))) { if (!__stratum_send(pool, s, strlen(s))) {
applog(LOG_DEBUG, "Failed to send s in initiate_stratum"); applog(LOG_DEBUG, "Failed to send s in initiate_stratum");
goto out; goto out;
} }
@ -1386,11 +1399,13 @@ out:
pool->pool_no, pool->nonce1, pool->n2size); pool->pool_no, pool->nonce1, pool->n2size);
} }
} else { } else {
pool->stratum_active = false; applog(LOG_DEBUG, "Initiate stratum failed, disabling stratum_active");
mutex_lock(&pool->stratum_lock);
if (curl) { if (curl) {
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
pool->stratum_curl = NULL; pool->stratum_curl = NULL;
} }
mutex_unlock(&pool->stratum_lock);
} }
return ret; return ret;

Loading…
Cancel
Save