Browse Source

Merge commit '35eea90' into bfgminer

Luke Dashjr 9 years ago
parent
commit
250926f1f6
5 changed files with 110 additions and 37 deletions
  1. 9 5
      api.c
  2. 17 6
      configure.ac
  3. 61 20
      driver-stratum.c
  4. 22 5
      miner.c
  5. 1 1
      miner.h

+ 9 - 5
api.c

@@ -313,6 +313,7 @@ static const char *JSON_PARAMETER = "parameter";
 #define MSG_SETQUOTA 122
 
 #define MSG_INVSTRATEGY 0x102
+#define MSG_FAILPORT 0x103
 
 #define USE_ALTMSG 0x4000
 
@@ -466,6 +467,7 @@ struct CODES {
  { SEVERITY_ERR,   MSG_INVNUM,	PARAM_BOTH,	"Invalid number (%d) for '%s' range is 0-9999" },
  { SEVERITY_ERR,   MSG_INVNEG,	PARAM_BOTH,	"Invalid negative number (%d) for '%s'" },
  { SEVERITY_ERR,   MSG_INVSTRATEGY,	PARAM_STR,	"Invalid strategy for '%s'" },
+ { SEVERITY_ERR,   MSG_FAILPORT,	PARAM_BOTH,	"Failed to set port (%d) for '%s'" },
  { SEVERITY_SUCC,  MSG_SETQUOTA,PARAM_SET,	"Set pool '%s' to quota %d'" },
  { SEVERITY_ERR,   MSG_CONPAR,	PARAM_NONE,	"Missing config parameters 'name,N'" },
  { SEVERITY_ERR,   MSG_CONVAL,	PARAM_STR,	"Missing config value N for '%s,N'" },
@@ -3203,12 +3205,12 @@ static void debugstate(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha
 		io_close(io_data);
 }
 
-extern void stratumsrv_change_port();
+extern bool stratumsrv_change_port(unsigned);
 
 static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 {
 	char *comma;
-	int value;
+	long value;
 
 	if (param == NULL || *param == '\0') {
 		message(io_data, MSG_CONPAR, 0, NULL, isjson);
@@ -3251,7 +3253,7 @@ static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char
 		return;
 	}
 
-	value = atoi(comma);
+	value = atol(comma);
 	if (value < 0 || value > 9999) {
 		message(io_data, MSG_INVNUM, value, param, isjson);
 		return;
@@ -3275,8 +3277,10 @@ static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char
 #ifdef USE_LIBEVENT
 	else if (strcasecmp(param, "stratum-port") == 0)
 	{
-		stratumsrv_port = value;
-		stratumsrv_change_port();
+		if (!stratumsrv_change_port(value)) {
+			message(io_data, MSG_FAILPORT, value, param, isjson);
+			return;
+		}
 	}
 #endif
 	else {

+ 17 - 6
configure.ac

@@ -661,10 +661,23 @@ AC_ARG_WITH([libevent],
 )
 if test "x$libevent" != "xno"; then
 	PKG_CHECK_MODULES([libevent],[libevent >= 2.0.3],[
-		AC_DEFINE([USE_LIBEVENT],[1],[Defined to 1 if libevent support is wanted])
+		PKG_CHECK_MODULES([libevent_pthreads],[libevent_pthreads],[
+			libevent_CFLAGS="${libevent_CFLAGS} ${libevent_pthreads_CFLAGS}"
+			libevent_LIBS="${libevent_LIBS} ${libevent_pthreads_LIBS}"
+		],[true])
+		AC_MSG_CHECKING([if libevent supports threading])
+		BFG_PREPROC_IFELSE([EVTHREAD_USE_PTHREADS_IMPLEMENTED || EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED],[event2/thread.h],[
+			AC_MSG_RESULT([yes])
+			have_libevent=yes
+		],[
+			AC_MSG_RESULT([no])
+		])
+	])
+	if test "x$have_libevent" = "xyes"; then
 		libevent=yes
-	],[
-		libevent=no
+		need_work2d=yes
+		AC_DEFINE([USE_LIBEVENT],[1],[Defined to 1 if libevent support is wanted])
+	else
 		libevent_enableaction="install libevent 2.0.3+"
 		if test -n "$need_bfg_driver_proxy_enableaction"; then
 			need_bfg_driver_proxy_enableaction="${need_bfg_driver_proxy_enableaction} (getwork) or libevent 2.0.3+ (stratum)"
@@ -676,9 +689,7 @@ if test "x$libevent" != "xno"; then
 		else
 			AC_MSG_WARN([libevent 2.0.3+ not found; stratum proxy will be unavailable])
 		fi
-	])
-	if test "x$libevent" = "xyes"; then
-		need_work2d=yes
+		libevent=no
 	fi
 fi
 AM_CONDITIONAL([USE_LIBEVENT], [test x$libevent = xyes])

+ 61 - 20
driver-stratum.c

@@ -24,6 +24,7 @@
 #include <event2/bufferevent.h>
 #include <event2/event.h>
 #include <event2/listener.h>
+#include <event2/thread.h>
 
 #include <jansson.h>
 
@@ -818,32 +819,42 @@ void stratumlistener(struct evconnlistener *listener, evutil_socket_t sock, stru
 	bufferevent_enable(bev, EV_READ | EV_WRITE);
 }
 
-void stratumsrv_start();
+static bool stratumsrv_init_server(void);
 
-void stratumsrv_change_port()
+bool stratumsrv_change_port(const unsigned port)
 {
-	struct event_base * const evbase = _smm_evbase;
-	
-	if (_smm_listener)
-		evconnlistener_free(_smm_listener);
-	
-	if (!_smm_running)
-	{
-		stratumsrv_start();
-		return;
+	if (!_smm_running) {
+		if (!stratumsrv_init_server()) {
+			return false;
+		}
 	}
 	
+	struct event_base * const evbase = _smm_evbase;
+	struct evconnlistener * const old_smm_listener = _smm_listener;
+	
 	struct sockaddr_in sin = {
 		.sin_family = AF_INET,
 		.sin_addr.s_addr = INADDR_ANY,
-		.sin_port = htons(stratumsrv_port),
+		.sin_port = htons(port),
 	};
 	_smm_listener = evconnlistener_new_bind(evbase, stratumlistener, NULL, (
 		LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_REUSEABLE
 	), 0x10, (void*)&sin, sizeof(sin));
 	
+	if (!_smm_listener) {
+		applog(LOG_ERR, "SSM: Failed to listen on port %u", (unsigned)port);
+		return false;
+	}
+	
 	// NOTE: libevent doesn't seem to implement LEV_OPT_CLOSE_ON_EXEC for Windows, so we must do this ourselves
 	set_cloexec_socket(evconnlistener_get_fd(_smm_listener), true);
+	
+	if (old_smm_listener) {
+		evconnlistener_free(old_smm_listener);
+	}
+	stratumsrv_port = port;
+	
+	return true;
 }
 
 static
@@ -852,29 +863,59 @@ void *stratumsrv_thread(__maybe_unused void *p)
 	pthread_detach(pthread_self());
 	RenameThread("stratumsrv");
 	
+	struct event_base *evbase = _smm_evbase;
+	event_base_dispatch(evbase);
+	_smm_running = false;
+	
+	return NULL;
+}
+
+static
+bool stratumsrv_init_server() {
 	work2d_init();
 	
+	if (-1
+#if EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED
+	 && evthread_use_windows_threads()
+#endif
+#if EVTHREAD_USE_PTHREADS_IMPLEMENTED
+	 && evthread_use_pthreads()
+#endif
+	) {
+		applog(LOG_ERR, "SSM: %s failed", "event_use_*threads");
+		return false;
+	}
+	
 	struct event_base *evbase = event_base_new();
+	if (!evbase) {
+		applog(LOG_ERR, "SSM: %s failed", "event_base_new");
+		return false;
+	}
 	_smm_evbase = evbase;
+	
 	{
 		ev_notify = evtimer_new(evbase, _stratumsrv_update_notify, NULL);
+		if (!ev_notify) {
+			applog(LOG_ERR, "SSM: %s failed", "evtimer_new");
+			return false;
+		}
 		_stratumsrv_update_notify(-1, 0, NULL);
 	}
 	{
 		notifier_init(_ssm_update_notifier);
 		struct event *ev_update_notifier = event_new(evbase, _ssm_update_notifier[0], EV_READ | EV_PERSIST, _stratumsrv_update_notify, NULL);
+		if (!ev_update_notifier) {
+			applog(LOG_ERR, "SSM: %s failed", "event_new");
+			return false;
+		}
 		event_add(ev_update_notifier, NULL);
 	}
-	stratumsrv_change_port();
-	event_base_dispatch(evbase);
 	
-	return NULL;
-}
-
-void stratumsrv_start()
-{
 	_smm_running = true;
+	
 	pthread_t pth;
 	if (unlikely(pthread_create(&pth, NULL, stratumsrv_thread, NULL)))
 		quit(1, "stratumsrv thread create failed");
-}
+	
+	return true;
+}

+ 22 - 5
miner.c

@@ -183,7 +183,7 @@ bool opt_restart = true;
 int httpsrv_port = -1;
 #endif
 #ifdef USE_LIBEVENT
-int stratumsrv_port = -1;
+long stratumsrv_port = -1;
 #endif
 
 const
@@ -1315,6 +1315,23 @@ static char *set_int_1_to_10(const char *arg, int *i)
 	return set_int_range(arg, i, 1, 10);
 }
 
+static char *set_long_1_to_65535_or_neg1(const char * const arg, long * const i)
+{
+	const long min = 1, max = 65535;
+	
+	char * const err = opt_set_longval(arg, i);
+	
+	if (err) {
+		return err;
+	}
+	
+	if (*i != -1 && (*i < min || *i > max)) {
+		return "Value out of range";
+	}
+	
+	return NULL;
+}
+
 char *set_strdup(const char *arg, char **p)
 {
 	*p = strdup((char *)arg);
@@ -2715,7 +2732,7 @@ static struct opt_table opt_config_table[] = {
 		     "Set socks proxy (host:port)"),
 #ifdef USE_LIBEVENT
 	OPT_WITH_ARG("--stratum-port",
-	             opt_set_intval, opt_show_intval, &stratumsrv_port,
+	             set_long_1_to_65535_or_neg1, opt_show_longval, &stratumsrv_port,
 	             "Port number to listen on for stratum miners (-1 means disabled)"),
 #endif
 	OPT_WITHOUT_ARG("--submit-stale",
@@ -7949,7 +7966,7 @@ void write_config(FILE *fcfg)
 #endif
 #ifdef USE_LIBEVENT
 	if (stratumsrv_port != -1)
-		fprintf(fcfg, ",\n\"stratum-port\" : %d", stratumsrv_port);
+		fprintf(fcfg, ",\n\"stratum-port\" : %ld", stratumsrv_port);
 #endif
 	_write_config_string_elist(fcfg, "device", opt_devices_enabled_list);
 	_write_config_string_elist(fcfg, "set-device", opt_set_device_list);
@@ -13208,7 +13225,7 @@ void bfg_atexit(void)
 }
 
 extern void bfg_init_threadlocal();
-extern void stratumsrv_start();
+extern bool stratumsrv_change_port(unsigned);
 extern void test_aan_pll(void);
 
 int main(int argc, char *argv[])
@@ -13771,7 +13788,7 @@ begin_bench:
 
 #ifdef USE_LIBEVENT
 	if (stratumsrv_port != -1)
-		stratumsrv_start();
+		stratumsrv_change_port(stratumsrv_port);
 #endif
 
 #ifdef HAVE_BFG_HOTPLUG

+ 1 - 1
miner.h

@@ -985,7 +985,7 @@ extern int last_logstatusline_len;
 extern bool have_libusb;
 #endif
 extern int httpsrv_port;
-extern int stratumsrv_port;
+extern long stratumsrv_port;
 extern char *opt_api_allow;
 extern bool opt_api_mcast;
 extern char *opt_api_mcast_addr;