-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogServer.c
More file actions
471 lines (356 loc) · 15 KB
/
logServer.c
File metadata and controls
471 lines (356 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include <stdio.h>
#include <stdlib.h> /* for atoi() and exit() */
#include <string.h> /* for memset() */
#include <signal.h>
#include <time.h>
#include <sys/socket.h> /* for socket(), bind(), connect() */
#include <sys/types.h>
#include <sys/wait.h> /* for waitpid() */
#include <unistd.h> /* for close() */
#include <netinet/in.h>
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <dirent.h>
#include <fcntl.h> /* for the flags to set the access mode */
#include <sys/stat.h> /* for the flags to define the file permissions */
#define MAXQUEUE 3
#define MAXLOGFILE 5 // maximum number of log files in the given directory
// Global variables
char *directory; // directory to store the log file (global to be accessible by sign. handler)
char fullpath[50]; // full path to the log file (global to be accessible by sign. handler)
int is_main_process = 1; // to distinguish between parent and child
// Helper function to compute the current time to put in the log file
char * get_timestamp(char *t, time_t mt);
// Function that logs the received message inside the log file, implementing the advisory locking mechanism
int logReceivedMessage(char *pathToFile, char *message, char *time, char *addr, int pn);
// Function similar to the one above, used to log a general message in the log file
int logMessage(char *pathToFile, char *message, char *time);
// Signal handler for the SIGINT signal (Ctrl+C)
void shutdown_handler(int signum);
int main(int argc, char *argv[])
{
/* Variables declarations */
unsigned int i; // counter to give the name to the log file
int serverSocket; // socket descriptor for the server
int newSocket; // socket descriptor for the client
struct sockaddr_in server_address; // local address of the server
struct sockaddr_in client_address; // address of the client
unsigned short serverPort; // port on which the server will listen
DIR *d;
unsigned int clientAddrLength; // length of client_address structure
pid_t processID; // Process ID returned by fork()
char buffer[1024]; // buffer where to write the data read by recv()
int recv_length; // number of bytes written in the buffer by recv()
time_t mytime; // to get the timestamp for the log file
char *t;
/* Check correct number of arguments */
if (argc != 3) {
fprintf(stderr, "Usage: %s <listening_port> <directory>\n", argv[0]);
exit(1);
}
serverPort = atoi(argv[1]); // first argument
directory = argv[2]; // second argument
/**********************************************************************************/
/* Check if the given directory exists */
d = opendir(directory);
// If it does not exist, we create it setting the permissions for the user (owner)
if (d == NULL) {
if (mkdir(directory, S_IRUSR|S_IWUSR|S_IXUSR) == -1) {
perror("mkdir() failed");
exit(1);
}
printf("[+] Directory \'%s\' successfully created.\n", directory);
}
/**********************************************************************************/
/* When started the server should open a new log file (without removing any old file) */
for (i = 0; i <= MAXLOGFILE; i++) {
/*
* Craft the name of the log file.
* Concatenate the given directory path with the name of the log file.
* The function sprintf(), instead of printing on stdout, stores the string into the specified buffer.
*/
sprintf(fullpath, "%s/server_%d.log", directory, i);
// Test for the file existence (F_OK): if it does not exist then we found a name for a new log file
if ((access(fullpath, F_OK)) == -1) {
// exit from the for
break;
}
}
/*
* At this point 'fullpath' will contain the name of the new log file to be created.
* It will be created for the first time whenever the functions logReceivedMessage() or logMessage() are called.
*/
// If the directory contains the maximum number of possible log files then we terminate.
// 'i' will be equal to MAXLOGFILE only if we did not exit from the for loop through the break.
if (i == MAXLOGFILE) {
perror("Max number of log files reached!");
exit(1);
}
/**********************************************************************************/
// Register the signal handler for SIGINT signal
if (signal(SIGINT, shutdown_handler) == SIG_ERR) {
perror("signal() failed");
exit(1);
}
/**********************************************************************************/
/* (1) Create a socket for incoming connections */
// Creates a socket specifying the domain (Internet), the type (Stream) and protocol (0 for TCP). Returns a descriptor.
if ((serverSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket() failed");
exit(1);
}
/**********************************************************************************/
/* (2) Bind the socket to an IP address and to the port number specified in the command line */
// Construct local address structure
server_address.sin_family = AF_INET; // Internet address familiy
server_address.sin_addr.s_addr = INADDR_ANY; // Any address of the machine
server_address.sin_port = htons(serverPort);
memset(&(server_address.sin_zero), '\0', 8); // Zero the rest of the struct
// Bind to the local address
if (bind(serverSocket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
perror("bind() failed");
exit(1);
}
/**********************************************************************************/
/* (3) Listen for incoming connections from clients */
// specify willingness to accept incoming connections and a queue limit for pending connections
if (listen(serverSocket, MAXQUEUE)) {
perror("listen() failed");
exit(1);
}
/**********************************************************************************/
/* (4) Accept a connection / Wait for a client to connect */
for (;;) { // Accept loop
// size of client_address structure
clientAddrLength = sizeof(client_address);
/*
* Returns a new socket file descriptor for the accepted connection.
* The connecting client's address information are written into the structure client_address.
*/
if ((newSocket = accept(serverSocket, (struct sockaddr *) &client_address, &clientAddrLength)) < 0) {
perror("accept() failed");
exit(1);
}
// newSocket is now connected to a client
printf("Server: got connection from %s port %d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
t = get_timestamp(t, mytime);
// Record the new connection on the log file
if ((logReceivedMessage(fullpath, "NEW Connection established", t, inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port))) == -1) {
perror("Error while logging the new connection");
exit(1);
}
/**********************************************************************************/
// Fork a child process: it will handle the client requests
if ((processID = fork()) < 0) {
perror("fork() failed");
exit(1);
}
else if (processID == 0) { // if this is the child process
// Set to 0 (for the signal handler)
is_main_process = 0;
// Child closes parent socket
close(serverSocket);
/**********************************************************************************/
/* Child handles the client *
/* Loop that receives data from the connected client and prints it out. */
while (1) {
/*
* The recv() function is given a pointer to a buffer and a maximum length to read from
* the socket. The function writes the data into the buffer passed to it and returns the
* number of bytes it actually wrote.
*/
if ((recv_length = recv(newSocket, buffer, 1024, 0)) < 0) {
perror("recv() failed");
exit(1);
}
// Check if the client requested to close the connection
if (strcmp(buffer, "CLOSE_CONNECTION") == 0) {
printf("Received close signal. Closing connection...\n");
t = get_timestamp(t, mytime);
// Log the disconnection
if ((logReceivedMessage(fullpath, buffer, t, inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port))) == -1) {
perror("Error while logging the disconnection");
exit(1);
}
break; // exit from while loop to disconnect
}
// Print the received number of bytes
printf("RECV: %d bytes\n", recv_length);
t = get_timestamp(t, mytime);
// print time, client address and port number
printf("%s | from %s port %d --> ", t, inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
/*
* Print the received message.
* By using %.*s, we provide the width as an argument, ensuring that only the
* specified number of data (the length of the received data) are printed.
* This is a safe way to handle non null-terminated string.
*/
printf("%.*s\n\n", (int)recv_length, buffer);
// Log the message inside the log file
if ((logReceivedMessage(fullpath, buffer, t, inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port))) == -1) {
perror("Error while logging the received message");
exit(1);
}
/*
* Clear the buffer array before using it to receive other data from the client.
* By setting all the bytes to zero, we ensure that there will not be any previous
* data left in the buffer.
*/
memset(buffer, 0, sizeof(buffer));
}
// child closes client socket
close(newSocket);
printf("Disconnected from %s:%d\n\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
/**********************************************************************************/
// child process terminates
exit(0);
}
else { // This is the parent process
// Parent closes client socket descriptor
close(newSocket);
/*
* To avoid zombie processes (processes that have terminated but still have an entry in the process table)
* and properly release the resources, the parent process should wait for its child process.
*/
// Wait for any terminated child process, without storing status info, return immediately if no child has exited (non-blocking wait)
if ((waitpid(-1, NULL, WNOHANG)) < 0) {
perror("waitpid() failed");
exit(1);
}
}
} // end of for loop
/**********************************************************************************/
// Never executed
return 0;
}
/***********************************************************************************************************/
// Helper function to compute the current time to put in the log file
char * get_timestamp(char *t, time_t mt) {
// get current time in seconds
time(&mt);
// ctime() returns a pointer to a string which ends with a new line character
t = ctime(&mt);
// remove the new line character at the end
if (t[strlen(t)-1] == '\n') t[strlen(t)-1] = '\0';
return t;
}
/*
* This function appends a message to the file specified as an argument only if no other process holds a lock on the file.
* In particular, using fcntl(), if another process holds a lock on the file, the caller waits for that process to release
* the lock, otherwise the function acquires a lock on the entire file, append a string to it, release the lock and terminates.
*/
int logReceivedMessage(char *pathToFile, char *message, char *time, char *addr, int pn) {
int fd;
struct flock lock;
char line[250]; // a single line of the log file
/*
* Opening file
* The first set of flags is used to set the access mode:
* O_WRONLY --> open file for write-only access
* O_CREAT --> create the file if it doesn't exist
* O_APPEND --> write data at the end of the file
* The second set of flags is used to define the file permissions of the newly created file
* S_IRUSR --> give the file read permission for the user (owner)
* S_IWUSR --> give the file write permission for the user (owner)
*/
fd = open(pathToFile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
if (fd == -1) {
perror("Error opening the log file");
return -1;
}
// Try to acquire a lock
lock.l_type = F_WRLCK; // Write Lock
lock.l_whence = SEEK_SET; // Start of the file (how to interpret l_start)
lock.l_start = 0; // Offset at start of the file
lock.l_len = 0; // Special value: lock all bytes
/*
* By using F_SETLKW, if another process holds the lock,
* the fcntl() call will block until the lock can be acquired,
* instead of immediately returning an error.
*/
if (fcntl(fd, F_SETLKW, &lock) == -1) {
perror("Error acquiring the lock");
close(fd);
return -1;
}
// At this point, the lock has been acquired
// Craft a single line of the log by concatenating different info
sprintf(line, "%s | from %s port %d --> %s\n", time, addr, pn, message);
// Append the line to the log file
if (write(fd, line, strlen(line)) == -1) {
perror("write() failed");
return -1;
}
// Release the lock
lock.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &lock) == -1) {
perror("Error releasing the lock");
close(fd);
return -1;
}
// At this point the lock has been released
close(fd); // close the file
return 0;
}
/* This function is similar to logReceivedMessage() but takes less arguments. It is used by the signal handler to log the shutdown */
int logMessage(char *pathToFile, char *message, char *time) {
int fd;
struct flock lock;
char line[250]; // a single line of the log file
// Opening file
fd = open(pathToFile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
if (fd == -1) {
perror("Error opening the file");
return -1;
}
// Try to acquire a lock
lock.l_type = F_WRLCK; // Write Lock
lock.l_whence = SEEK_SET; // Start of the file (how to interpret l_start)
lock.l_start = 0; // Offset at start of the file
lock.l_len = 0; // Special value: lock all bytes
if (fcntl(fd, F_SETLKW, &lock) == -1) {
perror("Error acquiring the lock");
close(fd);
return -1;
}
// At this point, the lock has been acquired
// Craft a single line of the log by concatenating different info
sprintf(line, "%s | %s\n", time, message);
// Append the line to the log file
if (write(fd, line, strlen(line)) == -1) {
perror("write() failed");
return -1;
}
// Release the lock
lock.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &lock) == -1) {
perror("Error releasing the lock");
close(fd);
return -1;
}
// At this point the lock has been released
close(fd); // close the file
return 0;
}
/*
* When the SIGINT signal is received this signal handler will be executed both by the main process and the child
* processes. However, only the main process will handle the shut down.
*/
void shutdown_handler(int signum) {
if (is_main_process == 0) {
// The child process will only terminate
exit(0);
}
else {
time_t mytime;
char *t;
t = get_timestamp(t, mytime);
// Record the order of shutdown in the log file (fullpath is a global variable)
if (logMessage(fullpath, "The server was shut down", t) == -1) {
perror("Error while logging the shutdown message");
exit(1);
}
// Use write() instead of printf() because write() is an async-signal-safe function
write(1, "\nShutting down the server... Goodbye!\n", 38); // 1 is the file descriptor for stdout
exit(0);
}
}