Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

sbbs_status.c 21.3 KB
Newer Older
1 2
#include <sys/stat.h>

3
#include <stdarg.h>
4 5 6 7 8
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

9 10 11 12 13
#ifdef SBBS
#undef SBBS
#endif

#include "sbbs.h"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "sbbs_status.h"

#include "genwrap.h"
#include "sockwrap.h"
#include "link_list.h"

static uint64_t svc_error_count[SERVICE_COUNT] = {0};
static char *status[SERVICE_COUNT] = {0};
static unsigned started = 0;
static int clients[SERVICE_COUNT] = {0};
static int threads[SERVICE_COUNT] = {0};
static int sockets[SERVICE_COUNT] = {0};
static link_list_t svc_client_list[SERVICE_COUNT] = {{0}};
static uint64_t served[SERVICE_COUNT] = {0};
static link_list_t status_sock;

static pthread_once_t init_once = PTHREAD_ONCE_INIT;
static pthread_mutex_t status_mutex[SERVICE_COUNT];
static pthread_mutex_t status_thread_mutex;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
static sbbs_status_startup_t* startup;
static scfg_t scfg;
static protected_uint32_t active_clients;
static protected_uint32_t thread_count;

static int lprintf(int level, const char *fmt, ...)
{
	va_list argptr;
	char sbuf[1024];

	va_start(argptr,fmt);
	vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
	sbuf[sizeof(sbuf)-1]=0;
	va_end(argptr);

	if(level <= LOG_ERR) {
		char errmsg[sizeof(sbuf)+16];
50
		SAFEPRINTF(errmsg, "stat %s", sbuf);
51
		errorlog(&scfg, level, startup==NULL ? NULL:startup->host_name, errmsg);
52 53 54 55 56 57 58 59 60 61 62 63 64 65
		if(startup!=NULL && startup->errormsg!=NULL)
			startup->errormsg(startup->cbdata,level,errmsg);
	}

	if(startup==NULL || startup->lputs==NULL || level > startup->log_level)
		return(0);

#if defined(_WIN32)
	if(IsBadCodePtr((FARPROC)startup->lputs))
		return(0);
#endif

	return startup->lputs(startup->cbdata,level,sbuf);
}
66 67 68 69 70 71 72 73 74 75 76 77 78

static void init_lists(void)
{
	int i;

	listInit(&status_sock, LINK_LIST_MUTEX);
	pthread_mutex_init(&status_thread_mutex, NULL);
	for (i=0; i<SERVICE_COUNT; i++) {
		pthread_mutex_init(&status_mutex[i], NULL);
		listInit(&svc_client_list[i], LINK_LIST_MUTEX);
	}
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static void client_on(SOCKET sock, client_t* client, BOOL update)
{
	if(startup!=NULL && startup->client_on!=NULL)
		startup->client_on(startup->cbdata,TRUE,sock,client,update);
}

static void client_off(SOCKET sock)
{
	if(startup!=NULL && startup->client_on!=NULL)
		startup->client_on(startup->cbdata,FALSE,sock,NULL,FALSE);
}

static void update_clients(void)
{
	if (startup != NULL && startup->clients != NULL)
		startup->clients(startup->cbdata, protected_uint32_value(active_clients));
}

97 98 99 100 101 102
static void sendsmsg(struct sbbs_status_msg *msg)
{
	int ret;
	list_node_t* node;
	list_node_t* next;
	SOCKET *sock;
103 104
	static link_list_t off_socks;
	bool os_init = false;
105 106 107 108 109 110 111 112 113

	listLock(&status_sock);
	for(node=status_sock.first; node!=NULL; node=next) {
		next = node->next;
		sock = node->data;
		ret = send(*sock, msg, msg->hdr.len, MSG_EOR);
		if (ret == SOCKET_ERROR) {
			if (ERROR_VALUE != EAGAIN) {
				closesocket(*sock);
114
				if (!os_init) {
115
					os_init = true;
116 117 118 119
					listInit(&off_socks, 0);
				}
				listPushNode(&off_socks, sock);
				listRemoveNode(&status_sock, node, FALSE);
120 121 122 123
			}
		}
	}
	listUnlock(&status_sock);
124 125 126 127 128 129

	if (os_init) {
		for (node = off_socks.first; node != NULL; node=next) {
			next = node->next;
			sock = node->data;
			client_off(*sock);
130
			free(sock);
131
			(void)protected_uint32_adjust(&active_clients, -1);
132 133 134 135
			update_clients();
		}
		listFree(&off_socks);
	}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
}

void status_lputs(enum sbbs_status_service svc, int level, const char *str)
{
	struct sbbs_status_msg *msg;
	size_t sz;

	pthread_once(&init_once, init_lists);
	if (status_sock.count == 0)
		return;
	sz = offsetof(struct sbbs_status_msg, msg.lputs.msg)+strlen(str)+1;
	msg = malloc(sz);
	if (msg == NULL)
		return;
	msg->hdr.service = svc;
	msg->hdr.type = STATUS_LPUTS;
	msg->hdr.len = sz;
	msg->msg.lputs.level = level;
	strcpy(msg->msg.lputs.msg, str);
	sendsmsg(msg);
	free(msg);
}

void status_errormsg(enum sbbs_status_service svc, int level, const char *str)
{
	struct sbbs_status_msg *msg;
	size_t sz;

	pthread_once(&init_once, init_lists);
	pthread_mutex_lock(&status_mutex[svc]);
	svc_error_count[svc]++;
	pthread_mutex_unlock(&status_mutex[svc]);
	if (status_sock.count == 0)
		return;
	sz = offsetof(struct sbbs_status_msg, msg.errormsg.msg)+strlen(str)+1;
	msg = malloc(sz);
	if (msg == NULL)
		return;
	msg->hdr.service = svc;
	msg->hdr.type = STATUS_ERRORMSG;
	msg->hdr.len = sz;
	msg->msg.errormsg.level = level;
	strcpy(msg->msg.errormsg.msg, str);
	sendsmsg(msg);
	free(msg);
}

void status_status(enum sbbs_status_service svc, const char *str)
{
	struct sbbs_status_msg *msg;
	size_t sz;

	pthread_once(&init_once, init_lists);
	pthread_mutex_lock(&status_mutex[svc]);
	if (status[svc])
		free(status[svc]);
	status[svc] = strdup(str);
	pthread_mutex_unlock(&status_mutex[svc]);
	if (status_sock.count == 0)
		return;
	sz = offsetof(struct sbbs_status_msg, msg.status.status)+strlen(str)+1;
	msg = malloc(sz);
	if (msg == NULL)
		return;
	msg->hdr.service = svc;
	msg->hdr.type = STATUS_STATUS;
	msg->hdr.len = sz;
	strcpy(msg->msg.status.status, str);
	sendsmsg(msg);
	free(msg);
}

void status_started(enum sbbs_status_service svc)
{
	struct sbbs_status_msg *msg;
	size_t sz;

	pthread_once(&init_once, init_lists);
	pthread_mutex_lock(&status_mutex[svc]);
	started |= 1<<svc;
	pthread_mutex_unlock(&status_mutex[svc]);
	if (status_sock.count == 0)
		return;
	sz = offsetof(struct sbbs_status_msg, msg.started.unused);
	msg = malloc(sz);
	if (msg == NULL)
		return;
	msg->hdr.service = svc;
	msg->hdr.type = STATUS_STARTED;
	msg->hdr.len = sz;
	sendsmsg(msg);
	free(msg);
}

void status_recycle(enum sbbs_status_service svc)
{
	struct sbbs_status_msg *msg;
	size_t sz;

	pthread_once(&init_once, init_lists);
	if (status_sock.count == 0)
		return;
	sz = offsetof(struct sbbs_status_msg, msg.recycle.unused);
	msg = malloc(sz);
	if (msg == NULL)
		return;
	msg->hdr.service = svc;
	msg->hdr.type = STATUS_RECYCLE;
	msg->hdr.len = sz;
	sendsmsg(msg);
	free(msg);
}

void status_terminated(enum sbbs_status_service svc, int code)
{
	struct sbbs_status_msg *msg;
	size_t sz;

	pthread_once(&init_once, init_lists);
	pthread_mutex_lock(&status_mutex[svc]);
	started &= ~(1<<svc);
	pthread_mutex_unlock(&status_mutex[svc]);
	if (status_sock.count == 0)
		return;
	sz = offsetof(struct sbbs_status_msg, msg.terminated) + sizeof(msg->msg.terminated);
	msg = malloc(sz);
	if (msg == NULL)
		return;
	msg->hdr.service = svc;
	msg->hdr.type = STATUS_TERMINATED;
	msg->hdr.len = sz;
	msg->msg.terminated.code = code;
	sendsmsg(msg);
	free(msg);
}

void status_clients(enum sbbs_status_service svc, int active)
{
	struct sbbs_status_msg *msg;
	size_t sz;

	pthread_once(&init_once, init_lists);
	pthread_mutex_lock(&status_mutex[svc]);
	clients[svc] = active;
	pthread_mutex_unlock(&status_mutex[svc]);
	if (status_sock.count == 0)
		return;
	sz = offsetof(struct sbbs_status_msg, msg.clients) + sizeof(msg->msg.clients);
	msg = malloc(sz);
	if (msg == NULL)
		return;
	msg->hdr.service = svc;
	msg->hdr.type = STATUS_CLIENTS;
	msg->hdr.len = sz;
	msg->msg.clients.active = active;
	sendsmsg(msg);
	free(msg);
}

void status_thread_up(enum sbbs_status_service svc, BOOL up, BOOL setuid)
{
	struct sbbs_status_msg *msg;
	size_t sz;

	pthread_once(&init_once, init_lists);
	pthread_mutex_lock(&status_mutex[svc]);
	threads[svc] += up ? 1 : -1;
	pthread_mutex_unlock(&status_mutex[svc]);
	if (status_sock.count == 0)
		return;
	sz = offsetof(struct sbbs_status_msg, msg.thread_up) + sizeof(msg->msg.thread_up);
	msg = malloc(sz);
	if (msg == NULL)
		return;
	msg->hdr.service = svc;
	msg->hdr.type = STATUS_THREAD_UP;
	msg->hdr.len = sz;
	msg->msg.thread_up.up = up;
	msg->msg.thread_up.setuid = setuid;
	sendsmsg(msg);
	free(msg);
}

void status_socket_open(enum sbbs_status_service svc, BOOL open)
{
	struct sbbs_status_msg *msg;
	size_t sz;

	pthread_once(&init_once, init_lists);
	pthread_mutex_lock(&status_mutex[svc]);
	sockets[svc] += open ? 1 : -1;
	pthread_mutex_unlock(&status_mutex[svc]);
	if (status_sock.count == 0)
		return;
	sz = offsetof(struct sbbs_status_msg, msg.socket_open) + sizeof(msg->msg.socket_open);
	msg = malloc(sz);
	if (msg == NULL)
		return;
	msg->hdr.service = svc;
	msg->hdr.type = STATUS_SOCKET_OPEN;
	msg->hdr.len = sz;
	msg->msg.socket_open.open = open;
	sendsmsg(msg);
	free(msg);
}

void status_client_on(enum sbbs_status_service svc, BOOL on, SOCKET sock, client_t *client, BOOL update)
{
344
	client_t tmpc = {0};
345 346
	struct sbbs_status_msg *msg;
	size_t sz;
347
	char *p;
348 349 350 351
	const char *prot;
	const char *user;
	list_node_t *node;
	client_t *clientp;
352 353 354 355

	pthread_once(&init_once, init_lists);
	pthread_mutex_lock(&status_mutex[svc]);
	if(on) {
356
		prot = client->protocol;
357
		if (prot == NULL)
358 359
			prot = "<null>";
		user = client->user;
360
		if (user == NULL)
361
			user = "<null>";
362 363
		if(update) {
			list_node_t*	node;
364
			list_node_t*	old_node;
365 366

			listLock(&svc_client_list[svc]);
367 368 369 370 371 372 373 374 375 376 377
			if((old_node=listFindTaggedNode(&svc_client_list[svc], sock)) != NULL) {
				node = listAddNodeData(&svc_client_list[svc], client, sizeof(client_t) + strlen(prot) + 1 + strlen(user) + 1, sock, LAST_NODE);
				if (node != NULL) {
					clientp = node->data;
					clientp->protocol = ((char *)node->data)+sizeof(client_t);
					strcpy((char *)clientp->protocol, prot);
					clientp->user = clientp->protocol + strlen(prot) + 1;
					strcpy((char *)clientp->user, user);
				}
				listRemoveNode(&svc_client_list[svc], old_node, TRUE);
			}
378 379 380
			listUnlock(&svc_client_list[svc]);
		} else {
			served[svc]++;
381 382 383 384 385 386 387 388
			node = listAddNodeData(&svc_client_list[svc], client, sizeof(client_t) + strlen(prot) + 1 + strlen(user) + 1, sock, LAST_NODE);
			if (node != NULL) {
				clientp = node->data;
				clientp->protocol = ((char *)node->data)+sizeof(client_t);
				strcpy((char *)clientp->protocol, prot);
				clientp->user = clientp->protocol + strlen(prot) + 1;
				strcpy((char *)clientp->user, user);
			}
389 390 391 392 393
		}
	} else
		listRemoveTaggedNode(&svc_client_list[svc], sock, /* free_data: */TRUE);

	pthread_mutex_unlock(&status_mutex[svc]);
394

395 396
	if (status_sock.count == 0)
		return;
397 398 399 400 401 402 403
	if (client)
		tmpc = *client;
	client = &tmpc;
	if (client->protocol == NULL)
		client->protocol = "<null>";
	if (client->user == NULL)
		client->user = "<null>";
404
	sz = offsetof(struct sbbs_status_msg, msg.client_on) + sizeof(msg->msg.client_on) + strlen(client->protocol) + 1 + strlen(client->user) + 1;
405 406 407 408 409 410 411 412 413 414
	msg = malloc(sz);
	if (msg == NULL)
		return;
	msg->hdr.service = svc;
	msg->hdr.type = STATUS_CLIENT_ON;
	msg->hdr.len = sz;
	msg->msg.client_on.on = on;
	msg->msg.client_on.sock = sock;
	msg->msg.client_on.client = *client;
	msg->msg.client_on.update = update;
415
	strcpy(msg->msg.client_on.strdata, client->protocol);
416 417
	p = strchr(msg->msg.client_on.strdata, 0);
	p++;
418
	strcpy(p, client->user);
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
	sendsmsg(msg);
	free(msg);
}

bool status_thread_terminated = false;

void status_thread_terminate(void)
{
	pthread_mutex_lock(&status_thread_mutex);
	status_thread_terminated = true;
	pthread_mutex_unlock(&status_thread_mutex);
}

void status_thread(void *arg)
{
	SOCKET sock;
	struct sockaddr_un addr;
	socklen_t addrlen;
	SOCKET *csock;
	char auth[1024];
	ssize_t len;
	struct sbbs_status_msg *msg;
	size_t mlen;
	int i;
	int nb;
	list_node_t* node;
445
	list_node_t* next;
446
	struct stat st;
447 448
	char error[256];
	client_t client = {0};
449
	client_t *clientp;
450 451 452
	user_t user;
	char *p;
	int rc = 0;
453 454
	const char *prot;
	const char *cuser;
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

	startup = arg;
	SetThreadName("sbbs/status");

#ifdef _THREAD_SUID_BROKEN
	if (thread_suid_broken)
		startup->seteuid(TRUE);
#endif

	if (startup == NULL) {
		sbbs_beep(100, 500);
		fprintf(stderr, "No status startup structure passed!\n");
		return;
	}

	if (startup->size != sizeof(sbbs_status_startup_t)) {	/* verify size */
		sbbs_beep(100, 500);
		sbbs_beep(300, 500);
		sbbs_beep(100, 500);
rswindell's avatar
rswindell committed
474
		fprintf(stderr, "Invalid status startup structure!\n");
475 476 477
		return;
	}

478
	protected_uint32_init(&thread_count, 0);
479 480

	pthread_once(&init_once, init_lists);
481
	startup->status(startup->cbdata, "Initializing");
482 483
	SAFECOPY(client.addr, startup->sock_fname);
	SAFECOPY(client.host, "<unix-domain>");
484 485 486 487 488 489 490 491 492 493 494 495 496
	client.protocol = "STATUS";
	client.size = sizeof(client);

	memset(&scfg, 0, sizeof(scfg));

	if (chdir(startup->ctrl_dir) != 0)
		lprintf(LOG_ERR, "!ERROR %d changing directory to: %s", errno, startup->ctrl_dir);

        /* Initial configuration and load from CNF files */
	SAFECOPY(scfg.ctrl_dir, startup->ctrl_dir);
	lprintf(LOG_INFO,"Loading configuration files from %s", scfg.ctrl_dir);
	scfg.size=sizeof(scfg);
	SAFECOPY(error,UNKNOWN_LOAD_ERROR);
497
	if(!load_cfg(&scfg, /* text: */NULL, /* prep: */TRUE, /* node: */FALSE, error, sizeof(error))) {
498 499 500 501 502 503 504 505
		lprintf(LOG_CRIT,"!ERROR %s",error);
		lprintf(LOG_CRIT,"!Failed to load configuration files");
		return;
	}

	if(startup->host_name[0]==0)
		SAFECOPY(startup->host_name,scfg.sys_inetaddr);
	prep_dir(scfg.ctrl_dir, scfg.temp_dir, sizeof(scfg.temp_dir));
506
	protected_uint32_init(&active_clients, 0);
507 508 509 510
	update_clients();

	startup->thread_up(startup->cbdata, TRUE, TRUE);
	if (stat(startup->sock_fname, &st) == 0) {
511
		if ((st.st_mode & S_IFMT) == S_IFSOCK)
512
			unlink(startup->sock_fname);
513 514 515 516 517
	}
	sock = socket(PF_UNIX, SOCK_SEQPACKET, 0);
	if (sock == INVALID_SOCKET)
		return;
	memset(&addr, 0, sizeof(addr));
518
	SAFECOPY(addr.sun_path, startup->sock_fname);
519 520 521 522 523 524 525
	addr.sun_family = AF_UNIX;
#ifdef SUN_LEN
	addrlen = SUN_LEN(&addr);
#else
	addrlen = offsetof(struct sockaddr_un, un_addr.sun_path) + strlen(addr.sun_path) + 1;
#endif
	if (bind(sock, (void *)&addr, addrlen) != 0) {
526
		lprintf(LOG_ERR, "Unable to bind to %s", addr.sun_path);
527 528 529 530
		closesocket(sock);
		return;
	}
	if (listen(sock, 2) != 0) {
531
		lprintf(LOG_ERR, "Unable to listen on %s", addr.sun_path);
532 533 534 535
		closesocket(sock);
		return;
	}

536 537
	startup->status(startup->cbdata, "Listening");
	startup->started(startup->cbdata);
538
	lprintf(LOG_INFO, "Listening on %s", startup->sock_fname);
539

540 541 542
	pthread_mutex_lock(&status_thread_mutex);
	while (!status_thread_terminated) {
		pthread_mutex_unlock(&status_thread_mutex);
Deucе's avatar
Deucе committed
543
		if (socket_readable(sock, 1000)) {
544
			csock = malloc(sizeof(SOCKET));
545 546 547
			if (csock == NULL) {
				lprintf(LOG_CRIT, "Error allocating memory!");
				rc = 1;
548
				break;
549
			}
550
			*csock = accept(sock, (void *)&addr, &addrlen);
551
			lprintf(LOG_DEBUG, "accept()ed new stat socket %d", *csock);
552 553 554
			if (*csock != INVALID_SOCKET) {
				nb = 1;
				ioctlsocket(*csock, FIONBIO, &nb);
Deucе's avatar
Deucе committed
555
				if (socket_readable(*csock, 5000)) {
556 557 558 559 560
					len = recv(*csock, auth, sizeof(auth), 0);
					if (len <= 0) {
						closesocket(*csock);
						free(csock);
						pthread_mutex_lock(&status_thread_mutex);
561
						lprintf(LOG_CRIT, "Error recv returned %d (%d)!", len, errno);
562 563 564
						continue;
					}
					// TODO: Check auth... "User\0Pass\0SysPass"
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
					client.user = auth;
					user.number = matchuser(&scfg, auth, TRUE);
					if (user.number == 0) {
						closesocket(*csock);
						free(csock);
						lprintf(LOG_WARNING, "Invalid username \"%s\"", auth);
						pthread_mutex_lock(&status_thread_mutex);
						continue;
					}
					if (getuserdat(&scfg, &user) != 0) {
						closesocket(*csock);
						free(csock);
						lprintf(LOG_WARNING, "Unable to read data for \"%s\"", auth);
						pthread_mutex_lock(&status_thread_mutex);
						continue;
					}
581
					client.user = user.alias;
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
					p = strchr(auth, 0);
					p++;
					if (p-auth >= len) {
						closesocket(*csock);
						free(csock);
						lprintf(LOG_WARNING, "No password specified");
						pthread_mutex_lock(&status_thread_mutex);
						continue;
					}
					if (stricmp(p, user.pass)) {
						closesocket(*csock);
						free(csock);
						if (scfg.sys_misc & SM_ECHO_PW)
							lprintf(LOG_WARNING, "Invalid password %s", p);
						else
							lprintf(LOG_WARNING, "Invalid password");
						pthread_mutex_lock(&status_thread_mutex);
						continue;
					}
					if (user.misc & (DELETED|INACTIVE)) {
						closesocket(*csock);
						free(csock);
						lprintf(LOG_WARNING, "!DELETED or INACTIVE user #%d (%s)", user.number, user.alias);
						pthread_mutex_lock(&status_thread_mutex);
						continue;
					}
					if (user.pass[0] == 0) {
						closesocket(*csock);
						free(csock);
						lprintf(LOG_WARNING, "User with empty password #%d (%s)", user.number, user.alias);
						pthread_mutex_lock(&status_thread_mutex);
						continue;
					}
					if (user.level < SYSOP_LEVEL) {
						closesocket(*csock);
						free(csock);
						lprintf(LOG_WARNING, "User not SysOp #%d (%s)", user.number, user.alias);
						pthread_mutex_lock(&status_thread_mutex);
						continue;
					}
					p = strchr(p, 0);
					p++;
					if (p-auth >= len) {
						closesocket(*csock);
						free(csock);
						lprintf(LOG_WARNING, "No syspass specified", p);
						pthread_mutex_lock(&status_thread_mutex);
						continue;
					}
					if (stricmp(p, scfg.sys_pass)) {
						closesocket(*csock);
						free(csock);
						lprintf(LOG_WARNING, "Invalid syspass %s", p);
						pthread_mutex_lock(&status_thread_mutex);
						continue;
					}
					client.time = time(NULL);
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
					listLock(&status_sock);
					listPushNode(&status_sock, csock);
					for (i=0; i<SERVICE_COUNT; i++) {
						/* SERVICE_EVENT doesn't have status, just logs */
						if (i == SERVICE_EVENT)
							continue;
						pthread_mutex_lock(&status_mutex[i]);
						mlen = offsetof(struct sbbs_status_msg, msg.stats.status) + (status[i] == NULL ? 0 : strlen(status[i])) + 1;
						msg = malloc(mlen);
						if (msg != NULL) {
							msg->hdr.service = i;
							msg->hdr.type = STATUS_STATS;
							msg->hdr.len = mlen;
							msg->msg.stats.error_count = svc_error_count[i];
							msg->msg.stats.served = served[i];
							msg->msg.stats.started = started & (1<<i) ? TRUE : FALSE;
							msg->msg.stats.clients = clients[i];
							msg->msg.stats.threads = threads[i];
							msg->msg.stats.sockets = sockets[i];
							if (status[i] == NULL)
								msg->msg.stats.status[0] = 0;
							else
								strcpy(msg->msg.stats.status, status[i]);
662

663 664 665 666
							send(*csock, msg, msg->hdr.len, 0);
							free(msg);
						}
						// Now send all the client info...
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
						listLock(&svc_client_list[i]);
						for (node = svc_client_list[i].first; node != NULL; node = node->next) {
							clientp = node->data;
							prot = clientp->protocol;
							if (prot == NULL)
								prot = "<null>";
							cuser = clientp->user;
							if (cuser == NULL)
								cuser = "<null>";
							mlen = offsetof(struct sbbs_status_msg, msg.client_on) + sizeof(msg->msg.client_on) + strlen(prot) + 1 + strlen(cuser) + 1;
							msg = malloc(mlen);
							if (msg != NULL) {
								msg->hdr.service = i;
								msg->hdr.type = STATUS_CLIENT_ON;
								msg->hdr.len = mlen;
								msg->msg.client_on.on = TRUE;
								msg->msg.client_on.update = FALSE;
684
								msg->msg.client_on.sock = node->tag;
685 686 687 688 689
								msg->msg.client_on.client = *clientp;
								msg->msg.client_on.client.protocol = msg->msg.client_on.strdata;
								msg->msg.client_on.client.user = msg->msg.client_on.strdata + strlen(prot) + 1;
								strcpy((char *)msg->msg.client_on.client.protocol, prot);
								strcpy((char *)msg->msg.client_on.client.user, cuser);
690 691 692
								send(*csock, msg, msg->hdr.len, 0);
							}
						}
693
						listUnlock(&svc_client_list[i]);
694 695 696 697
						pthread_mutex_unlock(&status_mutex[i]);
					}
					// Send current status
					listUnlock(&status_sock);
698
					client_on(*csock, &client, FALSE);
699
					(void)protected_uint32_adjust(&active_clients, 1);
700
					update_clients();
701 702 703 704
				}
				else {
					closesocket(*csock);
					free(csock);
Deucе's avatar
Deucе committed
705
					lprintf(LOG_WARNING, "socket did not become readable");
706 707 708 709 710 711
					pthread_mutex_lock(&status_thread_mutex);
					continue;
				}
			}
			else {
				free(csock);
712
				lprintf(LOG_WARNING, "accept() failed");
713 714 715 716 717 718
				pthread_mutex_lock(&status_thread_mutex);
				continue;
			}
		}
		pthread_mutex_lock(&status_thread_mutex);
	}
719 720 721 722 723 724 725 726 727 728 729
	if (sock != INVALID_SOCKET)
		closesocket(sock);
	unlink(startup->sock_fname);
	listLock(&status_sock);
	for(node=status_sock.first; node!=NULL; node=next) {
		next = node->next;
		csock = node->data;
		closesocket(*csock);
		listRemoveNode(&status_sock, node, FALSE);
	}
	listUnlock(&status_sock);
730 731
	protected_uint32_destroy(thread_count);
	protected_uint32_destroy(active_clients);
732 733 734

	startup->thread_up(startup->cbdata, FALSE, FALSE);
	startup->terminated(startup->cbdata, rc);
735 736 737 738 739 740 741 742 743 744 745 746 747 748
}

#define makestubs(lower, UPPER)                                                                                          \
void status_##lower##_lputs(void *cbdata, int level, const char *str) { status_lputs(SERVICE_##UPPER, level, str); }      \
void status_##lower##_errormsg(void *cbdata, int level, const char *str) { status_errormsg(SERVICE_##UPPER, level, str); } \
void status_##lower##_status(void *cbdata, const char *str) { status_status(SERVICE_##UPPER, str); }                        \
void status_##lower##_started(void *cbdata) { status_started(SERVICE_##UPPER); }                                             \
void status_##lower##_recycle(void *cbdata) { status_recycle(SERVICE_##UPPER); }                                              \
void status_##lower##_terminated(void *cbdata, int code) { status_terminated(SERVICE_##UPPER, code); }                         \
void status_##lower##_clients(void *cbdata, int active) { status_clients(SERVICE_##UPPER, active); }                            \
void status_##lower##_thread_up(void *cbdata, BOOL up, BOOL setuid) { status_thread_up(SERVICE_##UPPER, up, setuid); }           \
void status_##lower##_socket_open(void *cbdata, BOOL open) { status_socket_open(SERVICE_##UPPER, open); }                         \
void status_##lower##_client_on(void *cbdata, BOOL on, SOCKET sock, client_t *client, BOOL update) { status_client_on(SERVICE_##UPPER, on, sock, client, update); }

749
makestubs(ftp, FTP);
750 751 752 753 754
makestubs(mail, MAIL);
makestubs(term, TERM);
makestubs(web, WEB);
makestubs(services, SERVICES);
makestubs(event, EVENT);
755
makestubs(status, STATUS);