Browse Source

Ntk-console: Clean up file descriptors.

* Fork incomming console connections to support
  multiple connections at once.
* Rework ntpd console connection acceptance.
Alexander von Gluck IV 10 years ago
parent
commit
0a5463d759
2 changed files with 70 additions and 55 deletions
  1. 11
    13
      src/Ntk-Console/Netsukuku-Console.c
  2. 59
    42
      src/ntk-console-bindings.c

+ 11
- 13
src/Ntk-Console/Netsukuku-Console.c View File

@@ -4,9 +4,6 @@
4 4
 #include <unistd.h>
5 5
 
6 6
 
7
-char response[BUFFER_LENGTH];
8
-
9
-
10 7
 const struct supported_commands {
11 8
 	command_t 		id;
12 9
 	const char* 	command;
@@ -95,13 +92,14 @@ ntkd_request(char *request)
95 92
 		exit(-1);
96 93
 	}
97 94
 
95
+	char response[BUFFER_LENGTH];
98 96
 	request_receive(sockfd, response, BUFFER_LENGTH);
99 97
 	if (rc < 0) {
100 98
 		perror("recv() failed");
101 99
 		exit(-1);
102 100
 	}
103 101
 
104
-	printf("%s\n", response);
102
+	printf("Response: %s\n", response);
105 103
 }
106 104
 
107 105
 
@@ -121,6 +119,7 @@ opensocket(void)
121 119
 	rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
122 120
 	if (rc < 0) {
123 121
 		perror("connect() failed");
122
+		printf("Unable to connect to ntk daemon console.\n");
124 123
 		exit(-1);
125 124
 	}
126 125
 	printf("ntkd console connection opened successfully.\n");
@@ -193,6 +192,7 @@ console(char* request)
193 192
 		case COMMAND_QUIT:
194 193
 			closesocket();
195 194
 			exit(0);
195
+			break;
196 196
 		case COMMAND_UPTIME:
197 197
 		case COMMAND_INETCONN:
198 198
 		case COMMAND_CURIFS:
@@ -237,21 +237,19 @@ main(void)
237 237
 	uptime_month = timeinfo->tm_mon;
238 238
 	uptime_year = timeinfo->tm_year;
239 239
 	
240
-	printf("This is the Netsukuku Console, Please type 'help' for more information.\n");
241
-	
242
-	char* request = (char*)malloc(BUFFER_LENGTH);
243
-	
244 240
 	opensocket();
245
-	do {
241
+
242
+	printf("This is the Netsukuku Console. Please type 'help' for more information.\n");
243
+	for(;;) {
244
+		char* request = (char*)malloc(BUFFER_LENGTH);
246 245
 		printf("\n> ");
247 246
 		fgets(request, 16, stdin);
248 247
 		fflush(stdin);
249 248
 		console(request);
250
-		memset(request, 0, BUFFER_LENGTH);
251
-	} while(TRUE);
249
+		free(request);
250
+	}
252 251
 	closesocket();
253
-	
254
-	clean_up(); 
252
+
255 253
 	return 0;
256 254
 }
257 255
 

+ 59
- 42
src/ntk-console-bindings.c View File

@@ -14,14 +14,14 @@
14 14
 #define FALSE 			0
15 15
 #define TRUE 			1
16 16
 
17
-/* Variable and structure defintions, sockfd refers to socket file descriptor
17
+/* Variable and structure defintions, serverfd refers to socket file descriptor
18 18
  * length refers to the required length of the requests that will be sent.
19 19
  * recv won't wake up until length is received.
20 20
  * rc is used for error checking in socket operations.
21 21
  * serveraddr is a structure describing the address of 
22 22
  * an AF_LOCAL (aka AF_UNIX) socket. */
23 23
 
24
-int sockfd_1 = -1, sockfd_2 = -1;
24
+int serverfd = -1;
25 25
 struct sockaddr_un serveraddr;
26 26
 int rc, length;
27 27
 
@@ -33,15 +33,11 @@ void clean_up(void) {
33 33
 	const int optVal = 1;
34 34
 	const socklen_t optLen = sizeof(optVal);
35 35
 	
36
-	setsockopt(sockfd_1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
37
-	setsockopt(sockfd_2, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
36
+	setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
38 37
 	
39
-	if (sockfd_1 != -1)
40
-		close(sockfd_1);
38
+	if (serverfd != -1)
39
+		close(serverfd);
41 40
 
42
-	if (sockfd_2 != -1)
43
-		close(sockfd_2);
44
-   
45 41
    unlink(SERVER_PATH);
46 42
 	
47 43
 }
@@ -52,8 +48,8 @@ void opensocket(void) {
52 48
 	
53 49
 	int stop_trying;
54 50
 	
55
-	sockfd_1 = socket(AF_UNIX, SOCK_STREAM, 0);
56
-	if (sockfd_1 < 0) {
51
+	serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
52
+	if (serverfd < 0) {
57 53
 		 perror("socket creation failed");
58 54
 		 exit(-1);
59 55
 	}
@@ -62,7 +58,7 @@ void opensocket(void) {
62 58
 	serveraddr.sun_family = AF_UNIX;
63 59
 	strcpy(serveraddr.sun_path, SERVER_PATH);
64 60
 
65
-	rc = bind(sockfd_1, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
61
+	rc = bind(serverfd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
66 62
 	if (rc < 0) {
67 63
 		perror("bind() failed");
68 64
 		clean_up();
@@ -80,10 +76,10 @@ void opensocket(void) {
80 76
 /* Sends a parsed response to the ntk console client. */
81 77
 
82 78
 void
83
-send_response(char response[REQUEST_LENGTH], ...)
79
+send_response(int session_fd, char response[REQUEST_LENGTH], ...)
84 80
 {
85 81
 	int response_length = (int)strlen(response);
86
-	rc = send(sockfd_2, response, sizeof(response), 0);
82
+	rc = send(session_fd, response, response_length, 0);
87 83
 	if (rc < 0){
88 84
 		perror("send() failed");
89 85
 		exit(-1);
@@ -96,28 +92,28 @@ send_response(char response[REQUEST_LENGTH], ...)
96 92
  * into a response for the ntk console client. */
97 93
 
98 94
 int
99
-request_processing(char unprocessed_request[REQUEST_LENGTH])
95
+request_processing(int session_fd, char unprocessed_request[REQUEST_LENGTH])
100 96
 {
101 97
 	if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request))  == 0)
102
-		send_response((char)time(0)-me.uptime);
98
+		send_response(session_fd, (char)time(0)-me.uptime);
103 99
 	
104 100
 	else if(strncmp(unprocessed_request,"version", (int)strlen(unprocessed_request))  == 0)
105
-		send_response(VERSION_STR);
101
+		send_response(session_fd, VERSION_STR);
106 102
 	
107 103
 	else if(strncmp(unprocessed_request,"inet_connected", (int)strlen(unprocessed_request))  == 0)
108
-		send_response((char)me.inet_connected);
104
+		send_response(session_fd, (char)me.inet_connected);
109 105
 	
110 106
 	else if(strncmp(unprocessed_request,"cur_ifs", (int)strlen(unprocessed_request))  == 0)
111
-		send_response((char)me.cur_ifs);
107
+		send_response(session_fd, (char)me.cur_ifs);
112 108
 		
113 109
 	else if(strncmp(unprocessed_request,"cur_ifs_n", (int)strlen(unprocessed_request))  == 0)
114
-		send_response((char)me.cur_ifs_n);
110
+		send_response(session_fd, (char)me.cur_ifs_n);
115 111
 		
116 112
 	else if(strncmp(unprocessed_request,"cur_qspn_id", (int)strlen(unprocessed_request))  == 0)
117
-		send_response((char)me.cur_qspn_id);
113
+		send_response(session_fd, (char)me.cur_qspn_id);
118 114
 		
119 115
 	else if(strncmp(unprocessed_request,"cur_ip", (int)strlen(unprocessed_request))  == 0)
120
-		send_response((char)me.cur_ip.data);
116
+		send_response(session_fd, (char)me.cur_ip.data);
121 117
 		
122 118
 	/*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request))  == 0)
123 119
 		send_response(me.cur_node);
@@ -127,7 +123,7 @@ request_processing(char unprocessed_request[REQUEST_LENGTH])
127 123
 		
128 124
 	else if(strncmp(unprocessed_request,"ifs_n", (int)strlen(unprocessed_request))  == 0)
129 125
 		return 0;*/
130
-	send_response(unprocessed_request, " Is invalid or yet to be implemented.");
126
+	send_response(session_fd, unprocessed_request, " Is invalid or yet to be implemented.");
131 127
 	return -1;
132 128
 }
133 129
 
@@ -157,10 +153,10 @@ request_receive(int sock, char message[], int max)
157 153
 }
158 154
 
159 155
 
160
-void ntkd_request(void) {
161
-	char request[REQUEST_LENGTH];
162
-	
163
-	rc = listen(sockfd_1, 10);
156
+void
157
+wait_session(int server_fd)
158
+{
159
+	rc = listen(serverfd, 10);
164 160
 	if (rc< 0) {
165 161
 		perror("listen() failed");
166 162
 		exit(-1);
@@ -168,28 +164,49 @@ void ntkd_request(void) {
168 164
 
169 165
 	printf("Ready for client connect().\n");
170 166
 
171
-	do {
172
-		sockfd_2 = accept(sockfd_1, NULL, NULL);
173
-		if (sockfd_2 < 0) {
167
+	for(;;) {
168
+		int session_fd = accept(server_fd, NULL, NULL);
169
+		if (session_fd < 0) {
174 170
 			perror("accept() failed");
175 171
 			exit(-1);
176 172
 		}
177 173
 
178
-		rc = request_receive(sockfd_2, request, REQUEST_LENGTH);
179
-		if (rc < 0) {
180
-			perror("recv() failed");
174
+		pid_t pid = fork();
175
+		if (pid == -1) {
176
+			perror("Failed to spawn child console process");
181 177
 			exit(-1);
178
+		} else if (pid == 0) {
179
+			close(server_fd);
180
+			handle_session(session_fd);
181
+			_exit(0);
182
+		} else {
183
+			close(session_fd);
182 184
 		}
183
-		
184
-		printf("%d bytes of data were received\n", rc);
185
-		
186
-		request_processing(request);
187
-	} while(TRUE);
188
-		
189
-	clean_up();
185
+
186
+	}
187
+}
188
+
189
+
190
+void
191
+handle_session(int session_fd)
192
+{
193
+	char request[REQUEST_LENGTH];
194
+
195
+	rc = request_receive(session_fd, request, REQUEST_LENGTH);
196
+	if (rc < 0) {
197
+		perror("recv() failed");
198
+		exit(-1);
199
+	}
200
+	
201
+	printf("%d bytes of data were received\n", rc);
202
+	
203
+	request_processing(session_fd, request);
190 204
 }
191 205
 
192
-void console_recv_send(void) {
206
+
207
+void
208
+console_recv_send(void)
209
+{
193 210
 	opensocket();
194
-	ntkd_request();
211
+	wait_session(serverfd);
195 212
 }

Loading…
Cancel
Save