changeset 2:b91d9b779dd6

Added process struct Added free/close calls
author Josef "Jeff" Sipek <jeffpc@optonline.net>
date Fri, 26 Aug 2005 14:31:29 -0500
parents 32210a783cf7
children 2607e160df57
files main.c
diffstat 1 files changed, 37 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Fri Aug 26 14:21:10 2005 -0500
+++ b/main.c	Fri Aug 26 14:31:29 2005 -0500
@@ -15,6 +15,15 @@
 
 #define BUF_LEN getpagesize()
 #define MAX_PID 65535
+#define BOT_NAME_LEN 24
+
+#define CONFIG_OWNER 1
+
+struct proc {
+	pid_t pid;
+	int ignore;
+	char comm[BOT_NAME_LEN];
+};
 
 inline void init_outbuf(char* b)
 {
@@ -27,8 +36,11 @@
 	int sockfd;
 	struct sockaddr_in dest_addr;
 	char *ibuf, *obuf;
+	struct proc *bots;
 	int pid, pids;
 	int ret;
+	
+	int owner = CONFIG_OWNER;
 
 	ibuf = (char*) malloc(sizeof(char)*getpagesize());
 	if (!ibuf) {
@@ -43,6 +55,14 @@
 		return 1;
 	}
 	
+	bots = (struct proc*) malloc(sizeof(struct proc)*MAX_PID);
+	if (!bots) {
+		fprintf(stderr,"OOM\n");
+		free(obuf);
+		free(ibuf);
+		return 1;
+	}
+	
 	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 
 	dest_addr.sin_family = AF_INET;
@@ -53,13 +73,21 @@
 	connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
 	
 	init_outbuf(obuf);
-	
-	strcpy(obuf + strlen(obuf), "bot addbot foobar 1 3");
+	memset(bots, 0, sizeof(struct proc)*MAX_PID);
 	
 	for(pid=0, pids=0; pid < MAX_PID; pid++) {
 		ret = kill(pid,0);
-		if ((!ret) || (ret==-1 && errno==EPERM)) {
-			printf("pid %d exists%s\n",pid, (pid==getpid()?", ignoring":""));
+		
+		if (owner)
+			ret = !ret;
+		else
+			ret = (!ret || (ret==-1 && errno==EPERM));
+		
+		if (ret) {
+			bots[pid].pid = pid;
+			bots[pid].ignore = (pid==getpid());
+			snprintf(bots[pid].comm, BOT_NAME_LEN, "bot%d", pid);
+			
 			pids++;
 		}
 	}
@@ -71,5 +99,10 @@
 		printf("%d bytes: %s\n",ret, ibuf);
 	}
 	
+	close(sockfd);
+	free(ibuf);
+	free(obuf);
+	free(bots);
+	
 	return 0;
 }