Browse Source

vccd: add a monitor which isn't so pretty

intirain 4 năm trước cách đây
mục cha
commit
4351c69ac6
7 tập tin đã thay đổi với 72 bổ sung và 47 xóa
  1. 3 3
      Makefile
  2. 3 0
      include/klist.h
  3. 8 0
      include/vcc/vccd.h
  4. 2 2
      include/vcc/version.h
  5. 25 6
      main.c
  6. 22 11
      users.c
  7. 9 25
      vcc.c

+ 3 - 3
Makefile

@@ -1,5 +1,5 @@
 VCCD_MAJOR 		= 1
-VCCD_MINOR 		= 4
+VCCD_MINOR 		= 5
 VCCD_PATCH 		= 0
 VCCD_STR 		= -rc 
 
@@ -7,8 +7,8 @@ VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_BY 	= $(shell whoami)
 VCCD_SEMVER 		= vccd $(VCCD_MAJOR).$(VCCD_MINOR).$(VCCD_PATCH)$(VCCD_STR)
 
-srcs 			= main.c vcc.c klist.c users.c 
-objs 			= main.o vcc.o klist.o users.o  
+srcs 			= main.c vcc.c klist.c users.c monitor.c 
+objs 			= main.o vcc.o klist.o users.o monitor.o 
 target 			= vccd
 CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g 
 MAKEFLAGS 		= --no-print-directory

+ 3 - 0
include/klist.h

@@ -27,6 +27,9 @@
 
 struct klist_node {
 	long 			value;
+
+	/* only used by monitor.c, amalloc() */
+	long 			time;
 	void 			*data;
 	char 			*usrname;
 	struct klist_node 	*sending;

+ 8 - 0
include/vcc/vccd.h

@@ -31,6 +31,14 @@
 #define vcc_log(msg, ...) 	fprintf(stderr, "[%ld] " msg, times(NULL), ##__VA_ARGS__)
 
 
+void *__amalloc(int nbytes, const char *);
+int afree(void *, int nbytes);
+int amalloc_report(char *s);
+
+#define amalloc(n) 		__amalloc(n, __FUNCTION__)
+
+int monitor_cmd(void);
+
 int vcc_new_connection(int fd, struct in_addr *addr);
 int vcc_close_connection(int fd);
 

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCCD_VERSION "vccd 1.4.0 Fri Mar 25 21:27:53 CST 2022 root"
-#define VCCD_SEMVER "vccd 1.4.0-rc "
+#define VCCD_VERSION "vccd 1.5.0 Fri Mar 25 22:36:50 CST 2022 root"
+#define VCCD_SEMVER "vccd 1.5.0-rc "

+ 25 - 6
main.c

@@ -128,7 +128,7 @@ static int init_socket(void) {
 		exit(1);
 	}
 
-	if (!(fds = malloc(sizeof(struct pollfd) * POLLFDS))) {
+	if (!(fds = amalloc(sizeof(struct pollfd) * POLLFDS))) {
 		vcc_log("*** malloc() failed\n");
 
 		exit(1);
@@ -140,11 +140,20 @@ static int init_socket(void) {
 	fds[0].fd = fd;
 	fds[0].events = POLLIN;
 
-	next_fd = 1;
+	fds[1].fd = 0;
+	fds[1].events = POLLIN;
+
+	next_fd = 2;
 
 	return fd;
 }
 
+static inline int prompt(void) {
+	fputs("> ", stderr);
+
+	return 0;
+}
+
 
 int main(int argc, const char **argv) {
 	__uint32_t 	size;
@@ -162,14 +171,16 @@ int main(int argc, const char **argv) {
 	init_socket();
 	users_init();
 
-	nfds_total = 1;
+	nfds_total = 2;
 
-	if (unlikely(!(buf = malloc(REQ_SIZE)))) {
+	if (unlikely(!(buf = amalloc(REQ_SIZE)))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;
 	}
 
+	prompt();
+
 	for (;;) {
 		nfds_ready = poll(fds, nfds_total, -1);
 
@@ -205,7 +216,14 @@ int main(int argc, const char **argv) {
 			nfds_total++;
 		}
 
-		for (i = 1; i < nfds_total; i++) {
+		if (fds[1].revents & POLLIN) {
+			monitor_cmd();
+			prompt();
+
+			fds[1].revents = 0;
+		}
+
+		for (i = 2; i < nfds_total; i++) {
 			if (fds[i].fd == -1) 
 				continue;
 
@@ -235,7 +253,8 @@ int main(int argc, const char **argv) {
 		}
 	}
 
-	free(buf);
+	afree(buf, REQ_SIZE);
+	afree(fds, POLLFDS * sizeof(struct pollfd));
 
 	return 0;
 }

+ 22 - 11
users.c

@@ -50,7 +50,7 @@ int users_init(void) {
 		exit(1);
 	}
 
-	if (unlikely(!(vccd_dir = malloc(PATH_MAX)))) {
+	if (unlikely(!(vccd_dir = amalloc(PATH_MAX)))) {
 		vcc_log("*** malloc() failed\n");
 
 		exit(1);
@@ -73,7 +73,7 @@ int open_user_file(char *usr) {
 	char 	*fname;
 	int 	fdt;
 
-	if (unlikely(!(fname = malloc(PATH_MAX)))) {
+	if (unlikely(!(fname = amalloc(PATH_MAX)))) {
 		vcc_log("*** malloc() failed\n");
 
 		return -1;
@@ -87,7 +87,7 @@ int open_user_file(char *usr) {
 		return -1;
 	}
 
-	free(fname);
+	afree(fname, PATH_MAX);
 
 	return fdt;
 }
@@ -116,13 +116,13 @@ int do_user_login(int fd, struct vcc_request *req) {
 	static const char 	*join_msg = "%s logged in. now there are %d users. \n";
 	char 			*t;
 
-	if (unlikely(!(out = malloc(sizeof(struct vcc_request))))) {
+	if (unlikely(!(out = amalloc(sizeof(struct vcc_request))))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;
 	}
 
-	if (unlikely(!(usr = malloc(sizeof(struct user))))) {
+	if (unlikely(!(usr = amalloc(sizeof(struct user))))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;
@@ -137,20 +137,20 @@ int do_user_login(int fd, struct vcc_request *req) {
 		out->uid = 0;
 		vcc_send_req(find_connection(fd), out);
 
-		free(usr);
+		afree(usr, sizeof(struct user));
 
 		return 0;
 	}
 
 	vcc_log("%s logged in. \n", req->usrname);
 
-	if (unlikely(!(t = malloc(128)))) {
+	if (unlikely(!(t = amalloc(128)))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;
 	}
 
-	if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
+	if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;
@@ -171,7 +171,7 @@ int do_user_login(int fd, struct vcc_request *req) {
 	sprintf(t, join_msg, req->usrname, nusers);
 	vcc_message_broadcast(t, "system", 0);
 
-	free(t);
+	afree(t, 128);
 
 	return 0;
 }
@@ -193,6 +193,8 @@ static struct klist_node *find_user(int fd) {
 int do_user_logout(int fd) {
 	struct klist_node 	*n;
 	struct user 		*usr;
+	static const char 	*leave_msg = "%s left, now there are %d users here. \n";
+	char 			*t;
 	int 			fdt;
 
 	if (unlikely(!(n = find_user(fd)))) 
@@ -206,13 +208,22 @@ int do_user_logout(int fd) {
 		return 1;
 	}
 
+	if (unlikely(!(t = amalloc(128)))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
 	write(fdt, usr, sizeof(struct user));
 	vcc_log("Bye, %s. \n", usr->username);
 
+	sprintf(t, leave_msg, usr->username, --nusers);
+	vcc_message_broadcast(t, "system", 0);
+
 	klist_del(&logged_users, n);
 
-	free(usr);
-	free(n);
+	afree(usr, sizeof(struct user));
+	afree(n, sizeof(struct klist_node));
 
 	close(fdt);
 

+ 9 - 25
vcc.c

@@ -48,7 +48,7 @@ int vcc_new_connection(int fd, struct in_addr *addr) {
 
 	vcc_log("New connection from %s\n", inet_ntoa(*addr));
 
-	if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
+	if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;
@@ -58,7 +58,7 @@ int vcc_new_connection(int fd, struct in_addr *addr) {
 	n->data = addr;
 	n->value = fd;
 
-	if (unlikely(!(n->sending = malloc(sizeof(struct klist_node))))) {
+	if (unlikely(!(n->sending = amalloc(sizeof(struct klist_node))))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;
@@ -77,38 +77,22 @@ int vcc_new_connection(int fd, struct in_addr *addr) {
 
 int vcc_close_connection(int fd) {
 	struct klist_node *n, *t;
-	static const char *leave_msg = "An user left, now there are only %d users here. \n";
-	char *new_msg;
-
-	n = find_connection(fd);
-
-	if (unlikely(!n)) 
-		return 1;
-
-	if (unlikely(!(new_msg = malloc(128)))) {
-		vcc_log("*** malloc() failed\n");
 
+	if (unlikely(!(n = find_connection(fd)))) 
 		return 1;
-	}
 
 	do_user_logout(fd);
-
-	sprintf(new_msg, leave_msg, --nusers);
 	klist_del(&connections, n);
 
 	for (t = n->sending->next; t != n->sending; t = t->next) 
 		/* it's dangerous here */
 		if (t->data) 
-			free(t->data);
+			afree(t->data, REQ_SIZE);
 	
 
-	free(n->sending);
-
+	afree(n->sending, sizeof(struct klist_node));
 	vcc_log("Client closed. \n");
 
-	vcc_message_broadcast(new_msg, "system", 0);
-	free(new_msg);
-
 	return 0;
 }
 
@@ -142,8 +126,8 @@ int write_packages(struct klist_node *node) {
 	p = node->sending->next->data;
 
 	write(node->value, p, REQ_SIZE);
+	afree(p, REQ_SIZE);
 
-	free(p);
 	klist_del(node->sending, node->sending->next);
 
 	return 0;
@@ -157,7 +141,7 @@ int vcc_write_packages(int fd) {
 int vcc_send_req(struct klist_node *node, void *p) {
 	struct klist_node *n;
 
-	if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
+	if (unlikely(!(n = amalloc(sizeof(struct klist_node))))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;
@@ -181,7 +165,7 @@ int vcc_message_broadcast(char *msg, char *usrname, int except_fd) {
 		if (n->value == except_fd) 
 			continue;
 
-		if (unlikely(!(req = malloc(sizeof(struct vcc_request))))) {
+		if (unlikely(!(req = amalloc(sizeof(struct vcc_request))))) {
 			vcc_log("*** malloc() failed\n");
 
 			continue;
@@ -225,7 +209,7 @@ int send_users(int fd) {
 	struct klist_node *n;
 	int i;
 
-	if (unlikely(!(req = malloc(sizeof(struct vcc_request))))) {
+	if (unlikely(!(req = amalloc(sizeof(struct vcc_request))))) {
 		vcc_log("*** malloc() failed\n");
 
 		return 1;