-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
1040 lines (835 loc) · 27.9 KB
/
server.c
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <signal.h>
#include <regex.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#define TRUE 1
/* Structure contains information required of each process
* executed through RUN command
* pid contains Process ID of process
* name contains the name of process
* status contains whether process is running or terminated
* startTime contains time at which process was executed
* endTime contains time at which process was terminated
* elapsed time contains time process was active
*/
struct processStruct
{
int pid;
char *name;
char *status;
time_t startTime;
time_t endTime;
time_t elapsedTime;
};
typedef struct processStruct ProcessObject;
ProcessObject *processObject_new(int pid, char *name, char *status)
{
ProcessObject *p = malloc(sizeof(ProcessObject));
p->name = malloc(sizeof(name));
strcpy(p->name, name);
p->status = malloc(sizeof(status));
strcpy(p->status, status);
p->pid = pid;
p->startTime = time(NULL);
p->endTime = 0;
p->elapsedTime = 0;
return p;
}
void changeProcessStatus(ProcessObject *p, char *status)
{
p->status = realloc(p->status, sizeof(status));
strcpy(p->status, status);
}
void processObject_free(ProcessObject *p)
{
free(p->name);
free(p->status);
free(p);
}
/* Structure which contains all the information of clients required
* pid is client's PID
* fd contains pipe FD on which connection handler will write
* syncFD is used to sync clients
* status contains whether clients is running or disconnected
*/
struct clientHandlerInfoStruct
{
int pid;
int fd;
int syncFd;
char *status;
};
typedef struct clientHandlerInfoStruct clientHandlerInfo;
clientHandlerInfo *clientHandlerInfo_new(int pid, int fd, int syncFd, char *status)
{
clientHandlerInfo *p = malloc(sizeof(clientHandlerInfo));
p->pid = pid;
p->fd = fd;
p->syncFd = syncFd;
p->status = malloc(sizeof(status));
strcpy(p->status, status);
return p;
}
/* Global Declaration of variables
* processLst contains processes executed through run program
* processCount contains number of processes executed
* clientList contains all the clients ever connected
* clientCount contains number of clients which have ever connected
*/
ProcessObject *processLst;
int processCount;
clientHandlerInfo *clientList;
int clientCount;
/* Method to check the string provided in variable token is a number or not
* It used regex to check if it is a number
* Also supports negative numbers with negative (-) symbol in start
*/
bool checkIfNumber(char *token, int socket)
{
regex_t regex;
int return_val;
return_val = regcomp(®ex, "^[-]*[0-9]+$", REG_EXTENDED);
if (return_val != 0)
{
char invalidInput[] = {"Failed.\n;"};
write(socket, invalidInput, sizeof(invalidInput) - 1);
return false;
}
return_val = regexec(®ex, token, 0, NULL, 0);
if (return_val == REG_NOMATCH)
{
char invalidInput[] = {"Enter correct numbers.\n;"};
write(socket, invalidInput, sizeof(invalidInput) - 1);
return false;
}
return true;
}
/* Checks if string provided in variable str is valid operator
* validOps contains list of all valid operations
* size contains size of validOps list
* The method loops through validOps and compares str to it
* If it finds a match then returns true as it is a valid operation
* else if returns false
*/
bool validOpCheck(char *str, char *validOps[], int size)
{
for (int i = 0; i < size; ++i)
{
if (strcmp(validOps[i], str) == 0)
{
return true;
}
}
return false;
}
/* Performs Add/Sub/Mult/Div opeartions.
Takes 4 inputs, input[] which contains numbers,
opeartor[] which contains operations (ADD,SUB etc),
socked which contains socket's Fd
resultPrompt which contains orginal input as a whole to know answer corresponds to which input
*/
void calcOperation(char input[], char operator[], int socket, char resultPrompt[])
{
int ans = 0;
char *token;
char test[100];
token = strtok(input, " ");
token = strtok(NULL, " ");
strcpy(test, token);
if (token == NULL || !checkIfNumber(test, socket))
{
return;
}
ans = atoi(token);
token = strtok(NULL, " ");
while (token != NULL)
{
strcpy(test, token);
if (!checkIfNumber(test, socket))
{
return;
}
int num;
num = atoi(token);
if (strcmp(operator, "add") == 0)
ans += num;
if (strcmp(operator, "sub") == 0)
ans -= num;
if (strcmp(operator, "mult") == 0)
ans *= num;
if (strcmp(operator, "div") == 0)
{
if (num == 0)
{
char res[100];
char divByZeroPrompt[50];
int written = sprintf(divByZeroPrompt, "%s: Can not divide by zero.\n;", resultPrompt);
write(socket, divByZeroPrompt, written);
return;
}
ans /= num;
}
token = strtok(NULL, " ");
}
char res[100];
int written = sprintf(res, "%s: %d\n;", resultPrompt, ans);
write(socket, res, written);
}
/* Lists all processes
* Takes socket file descriptor as input to write to socket
* Boolean isServerThread identifies if method is called by ClientHandler Thread
* If isServerThread is true, then result is displayed on server
* If isServerThread is false, then result is written on socked
*/
void listMethod(int socket, bool isServerThread)
{
int bwritten = 0;
char details[1500];
if (isServerThread)
{
char listBuf[100];
int listB = sprintf(listBuf, "LIST OF %d\n\n\n", getpid());
write(STDOUT_FILENO, listBuf, listB);
}
bwritten += sprintf(&details[bwritten], "LIST:\n\n%5s %20s %25s %15s %15s %15s\n",
"Pid", "Name", "Status", "StartTime", "EndTime", "ElapsedTime");
for (int i = 0; i < processCount; i++)
{
int pid = processLst[i].pid;
char *name = processLst[i].name;
char *status = processLst[i].status;
time_t stTime = processLst[i].startTime;
time_t endTime = processLst[i].endTime;
time_t elapsedTime = processLst[i].elapsedTime;
struct tm *timeinfo;
timeinfo = localtime(&stTime);
char startTime[10];
sprintf(startTime, "%d:%d:%d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
if (endTime == 0)
{
elapsedTime = difftime(time(NULL), stTime);
char elapsedTimeString[10];
sprintf(elapsedTimeString, "%ld:%ld:%ld", (elapsedTime / 3600) % 24, (elapsedTime / 60) % 60, (elapsedTime % 60));
bwritten += sprintf(&details[bwritten], "%5d %20s %25s %15s %15s %15s\n",
pid, name, status, startTime, "{none}", elapsedTimeString);
}
else
{
elapsedTime = difftime(endTime, stTime);
char elapsedTimeString[10];
sprintf(elapsedTimeString, "%ld:%ld:%ld", (elapsedTime / 3600) % 24, (elapsedTime / 60) % 60, (elapsedTime % 60));
char endTimeString[10];
timeinfo = localtime(&endTime);
sprintf(endTimeString, "%d:%d:%d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
bwritten += sprintf(&details[bwritten], "%5d %20s %25s %15s %15s %15s\n",
pid, name, status, startTime, endTimeString, elapsedTimeString);
}
if (bwritten >= 800)
{
if (!isServerThread)
{
if (write(socket, details, bwritten) < 0)
{
perror("List write");
}
bwritten = 0;
}
else
{
write(STDOUT_FILENO, details, bwritten);
bwritten = 0;
}
}
}
if (!isServerThread)
{
write(socket, details, bwritten);
write(socket, "\n;", 2);
}
else
{
write(STDOUT_FILENO, details, bwritten);
write(STDOUT_FILENO, "\n\n\n", 3);
}
}
/* Runs a program on system path
* cpyInput contains the program to run along with its arguments
* socket has socket's fd to write to
* The method breaks cpyInput into arguments and calls execvp to execute the process
*/
void runMethod(char *token, char *cpyInput, int socket)
{
char *args[100];
int argsCount = 0;
token = strtok(cpyInput, " ");
token = strtok(NULL, " ");
if (token == NULL) //If user passed no arguments display error
{
char invalidPrompt[] = {"RUN: Enter valid arguments\n;"};
write(socket, invalidPrompt, sizeof(invalidPrompt) - 1);
return;
}
while (token != NULL) //Break command into arguments
{
args[argsCount] = token;
token = strtok(NULL, " ");
argsCount++;
}
args[argsCount] = NULL;
int pipeRun[2];
pipe2(pipeRun, O_CLOEXEC); //close pipe if exec successful
int runPid = fork();
bool errFound = false;
if (runPid == 0)
{
close(pipeRun[0]);
execvp(args[0], args);
//Will reach here if exec fails
char temp[100];
sprintf(temp, "%s\n", strerror(errno));
write(pipeRun[1], temp, strlen(temp));
exit(0);
}
else
{
close(pipeRun[1]);
processLst[processCount] = *processObject_new(runPid, args[0], "Running");
char temp[100];
//Check if anything is written on pipe, if yes that means exec failed
while (read(pipeRun[0], temp, 1) > 0)
{
if (!errFound)
errFound = true;
write(socket, temp, 1);
}
//If no error is found update process list and realloc list
if (!errFound)
{
int pid = processLst[processCount].pid;
char *name = processLst[processCount].name;
char *status = processLst[processCount].status;
char details[100];
int bwritten = sprintf(details, "RUN: Pid: %d, Name: %s, Status: %s", pid, name, status);
write(socket, details, bwritten);
write(socket, "\n;", 2);
processCount++;
processLst = realloc(processLst, (processCount + 1) * sizeof(struct processStruct));
}
else
{
write(socket, ";", 1);
}
close(pipeRun[0]);
}
}
/* Kills a process executed by run command
* cpyInput contains pid or name of program to be killed
* socket contain socket file descriptor
* Method first checks if arguments are valid
* Then uses regex to identify if pid is passed or name
* It then checks if process is not already running then sigterms it
*/
void killMethod(char *cpyInput, int socket)
{
regex_t regex;
int return_val;
return_val = regcomp(®ex, "^[0-9]+$", REG_EXTENDED);
char *token = strtok(cpyInput, " ");
token = strtok(NULL, " ");
if (token == NULL)
{
char invalidPrompt[] = {"KILL: Enter valid arguments\n;"};
write(socket, invalidPrompt, sizeof(invalidPrompt) - 1);
return;
}
return_val = regexec(®ex, token, 0, NULL, 0);
int bwritten = 0;
char details[1000];
bool found = false;
if (return_val == REG_NOMATCH)
{
//process name provided
for (int i = 0; i < processCount; i++)
{
if (strcmp(processLst[i].name, token) == 0)
{
found = true;
int pid = processLst[i].pid;
char *name = processLst[i].name;
if (strcmp(processLst[i].status, "Running") != 0)
{
bwritten = sprintf(details, "KILL: Already Terminated\n;");
continue;
// break;
}
bwritten = 0;
int stat;
kill(pid, SIGTERM);
waitpid(pid, &stat, 0);
if (WIFSIGNALED(stat))
{
changeProcessStatus(&processLst[i], "Succesful termination");
processLst[i].endTime = time(NULL);
}
char *status = processLst[i].status;
bwritten = sprintf(&details[bwritten], "KILL: Pid: %d, Name: %s, Status: %s\n;", pid, name, status);
break;
}
}
}
else
{
//process pid
for (int i = 0; i < processCount; i++)
{
if (processLst[i].pid == atoi(token))
{
found = true;
if (strcmp(processLst[i].status, "Running") != 0)
{
bwritten += sprintf(details, "Already Terminated\n;");
break;
}
int pid = processLst[i].pid;
char *name = processLst[i].name;
int stat;
kill(pid, SIGTERM);
waitpid(pid, &stat, 0);
if (WIFSIGNALED(stat))
{
processLst[i].status = "Succesful termination";
processLst[i].endTime = time(NULL);
}
char *status = processLst[i].status;
bwritten += sprintf(&details[bwritten], "KILL: Pid: %d, Name: %s, Status: %s\n;", pid, name, status);
break;
}
}
}
if (!found)
{
bwritten = sprintf(details, "KILL: Process not found\n;");
}
write(socket, details, bwritten);
}
/* Signal handler to handle when a process executed through run command terminates
* It first display the pid through which SIGCHLD was generated
* After that it keeps reading childPids then traverses the processList
* When it finds the corresponding process in processList it checks if process was terminated through signal
* If it was not signalled (not terminated through kill command) then it changed status to External Termination
*/
void childTerminationHandler(int signo, siginfo_t *siginfo, void *context)
{
int childPid;
int stat;
char sigPrompt[100];
int bwritten = sprintf(sigPrompt, "Signal sent by pid %d\n", siginfo->si_pid);
write(STDOUT_FILENO, sigPrompt, bwritten);
while ((childPid = waitpid(0, &stat, WNOHANG)) > 0)
{
for (int i = 0; i < processCount; i++)
{
if (processLst[i].pid == childPid)
{
if (!WIFSIGNALED(stat))
{
processLst[i].status = "Externally Terminated";
processLst[i].endTime = time(NULL);
break;
}
}
}
}
if (childPid == 0)
{
write(STDOUT_FILENO, "No child left\n", 14);
return;
}
}
/* Structure which stores Fds required by Client Handler Thread
The structure can be passed as void* while creating the thread */
struct threadFileDescriptorsStruct
{
int pipeFd;
int sockFd;
int syncFd;
};
typedef struct threadFileDescriptorsStruct threadFileDescriptors;
threadFileDescriptors *threadFDs_new(int pipeFd, int sockFd, int syncFd)
{
threadFileDescriptors *p = malloc(sizeof(threadFileDescriptors));
p->pipeFd = pipeFd;
p->sockFd = sockFd;
p->syncFd = syncFd;
}
/* CONNECTION HANDLER THREAD
* Responsible for taking input on server
* Supported commands print, printid <pid>, clear, list
* Uses clientList to send commands to clients
* Checks status of each client before sending command
* If status is terminated, it doesn't send command
*/
void *interactiveServer(void *ptr)
{
char inputBuff[1024];
int inputRead;
char supportedCommandsPrompt[] = "Supported commands:\n1)print\n2)printid <pid>\n3)list\n4)clear\n\n";
write(STDOUT_FILENO, supportedCommandsPrompt, strlen(supportedCommandsPrompt));
while (true)
{
inputRead = read(STDIN_FILENO, inputBuff, 1024);
if(inputRead <2){
continue;
}
inputBuff[inputRead-1] = 0;
if(strcasecmp(inputBuff, "clear") == 0){
system("clear");
write(STDOUT_FILENO, supportedCommandsPrompt, strlen(supportedCommandsPrompt));
inputRead = 0;
continue;
}
char inputcpy[1024];
strcpy(inputcpy, inputBuff);
char *operator;
operator= strtok(inputcpy, " ");
int pid = -1;
if (strcasecmp(operator, "printid") == 0)
{
char *temp = strtok(NULL, " ");
pid = atoi(temp);
}else if(strcasecmp(operator, "print") !=0
&& strcasecmp(operator,"list") != 0){
inputRead = 0;
write(STDOUT_FILENO, "Incorrect Operation\n", sizeof("Incorrect Operation\n"));
continue;
}
inputBuff[inputRead - 1] = 0;
for (int i = 0; i < clientCount; ++i)
{
clientHandlerInfo p = clientList[i];
if (pid == -1 || pid == p.pid)
{
if (strcasecmp(p.status, "terminated") != 0)
{
if( write(p.fd, inputBuff, inputRead) < 0){
perror("thread pipe write");
}
char syncBuf[2];
while(read(p.syncFd, syncBuf, 1) < 1){
}
}
}
}
inputRead = 0;
}
}
/* CLIENT HANDLER THREAD
* Responsible for listening to command sent from CONNECTION HANDLER THREAD
* ptr variable contains threadFileDescriptor object which contains required FDs
*/
void *threadedServer(void *ptr)
{
threadFileDescriptors fdObject = *(threadFileDescriptors *)ptr;
int socketFd = fdObject.sockFd;
int pipeFd = fdObject.pipeFd;
int syncFd = fdObject.syncFd;
char buff[1024];
int bwritten = 0;
while ( (bwritten = read(pipeFd, buff, 1024) ) > 0)
{
buff[bwritten] = 0;
char *operator= strtok(buff, " ");
char *token;
if (strcasecmp(operator, "print") == 0 || strcasecmp(operator, "printid")==0)
{
/* If command is printid then use strtok to remove next argument which is pid
* We dont't have to print pid so we discard it
*/
if(strcasecmp(operator,"printid") == 0){
strtok(NULL, " ");
}
bwritten = 0;
token = strtok(NULL, " ");
char resBuff[1024];
while (token != NULL)
{
bwritten += sprintf(&resBuff[bwritten], "%s ", token);
token = strtok(NULL, " ");
}
// Add newline to result and delimit with semicolon before writing to socket
resBuff[bwritten] = '\n';
resBuff[bwritten+1] = ';';
write(socketFd, resBuff, bwritten+2);
/* Write to syncFd to indicate command has executed fully so that
* command can be sent to next client without any sync issues
*/
write(syncFd, ";", 1);
}
else if (strcasecmp(operator, "list") == 0)
{
listMethod(-1, true);
write(syncFd, ";", 1);
}
bwritten = 0;
}
if (bwritten < 0) {
perror("pipeRead");
}else if(bwritten == 0){
exit(0);
}
}
/* Client Handler method
* Contains socket Fd to communicate with client
* pipeFd to communicate with Connection Handler
* syncFD to keep Client Handlers in sync
*/
void server(int socket, int pipeFd, int syncFd)
{
/* Setup advanced signal handler to handle
* termination of procceses executed through run command
* SA_SIGINFO flag used so that sa_sigaction is used instead of sa_handler
* SA_RESTART flag used to that read api can continue taking input after signal is handled
*/
struct sigaction new_action, old_action;
sigemptyset(&new_action.sa_mask);
new_action.sa_sigaction = &childTerminationHandler;
new_action.sa_flags = SA_SIGINFO | SA_RESTART;
if (sigaction(SIGCHLD, &new_action, NULL) < 0)
{
perror("sigaction");
}
/* Create a thread to handle incoming commands from connection handler */
pthread_t threadServer;
threadFileDescriptors fdObject = *threadFDs_new(pipeFd, socket, syncFd);
pthread_create(&threadServer, NULL, threadedServer, (void *)&fdObject);
/* Declare variables and initialize valid operations list */
char *token, *operator;
char input[4096];
char *validOps[] = {"print", "exit", "add", "sub", "mult", "div", "run", "list", "kill"};
int opsSize = 9;
/* Initialize process list which contains process executed through RUN command */
processLst = malloc(1 * sizeof(ProcessObject));
processCount = 0;
int totalRead = 0;
int checkB = 1;
/* Read input from socket 1 byte at a time
* Convert it to lowercase to standardize
* As input is delimited by semicolon (;) which indetifies end of a command
* It checks for semicolon and then checks if command has a valid operation
* If it is a print or exit command it's handled there only
* Every other command is passed to its respective method
*/
while ((checkB = read(socket, &input[totalRead], 1)) > 0)
{
if(input[totalRead] == '\n'){
input[totalRead] = ' ';
}
input[totalRead] = tolower(input[totalRead]);
totalRead += 1;
if (input[totalRead - 1] == ';' )
{
if (totalRead <= 2)
{
char incorrectInp[] = {"Incorrect input.\n;"};
write(socket, incorrectInp, sizeof(incorrectInp) - 1);
totalRead = 0;
continue;
}
input[totalRead - 1] = 0;
char cpyInput[4096];
char resultPrompt[4096];
strcpy(resultPrompt, input);
strcpy(cpyInput, input);
token = strtok(input, " ");
operator = token;
if (!validOpCheck(operator, validOps, opsSize))
{
char incorrectOpPrompt[] = {"Incorrect operation.\n;"};
write(socket, incorrectOpPrompt, sizeof(incorrectOpPrompt) - 1);
totalRead = 0;
continue;
}
else
{
if (strcmp(operator, "exit") == 0)
{
break;
}
else if (strcmp(operator, "print") == 0)
{
char promptBuff[100];
int bytes = sprintf(promptBuff, "Message from clientId %d: ", getpid());
write(STDOUT_FILENO, promptBuff, bytes);
token = strtok(cpyInput, " ");
token = strtok(NULL, " ");
while (token != NULL)
{
write(STDOUT_FILENO, token, strlen(token));
write(STDOUT_FILENO, " ", 1);
token = strtok(NULL, " ");
}
write(STDOUT_FILENO, "\n", 1);
write(socket, "Message Written\n;", 18);
}
else if (strcmp(operator, "run") == 0)
{
runMethod(token, cpyInput, socket);
}
else if (strcmp(operator, "list") == 0)
{
listMethod(socket, false);
}
else if (strcmp(operator, "kill") == 0)
{
killMethod(cpyInput, socket);
}
else
{
calcOperation(cpyInput, operator, socket, resultPrompt);
}
totalRead = 0;
}
}
}
if (checkB < 0)
{
perror("read failed");
}
/* When socket is closed it terminates all process in process list and exits */
for (int i = 0; i < processCount; i++)
{
kill(processLst[i].pid, SIGTERM);
}
char prompt[] = "Client Disconnected\n";
write(STDOUT_FILENO, prompt, sizeof(prompt) - 1);
write(socket, "exited\n;", 8);
exit(0);
}
/* Signal handler to handle when a client terminates
* It goes through the connected client list
* And updates the status of terminated client
* so that no command is sent to that pid
*/
void clientHandlerTermination(int signo, siginfo_t *siginfo, void *context)
{
int childPid;
while ((childPid = waitpid(0, NULL, WNOHANG)) > 0)
{
for (int i = 0; i < clientCount; i++)
{
if (clientList[i].pid == childPid)
{
clientList[i].status = "Terminated";
break;
}
}
}
}
int main(void)
{
/* Initialize variables required for socket connection */
int sock, length;
struct sockaddr_in serverInfo;
int msgsock;
char buf[1024];
int rval;
int i;
/* Allocate memory to client list to initialize it */
clientList = malloc(1 * sizeof(clientList));
clientCount = 0;
/* Setup signal handler to handle when a client terminates */
struct sigaction new_action;
sigemptyset(&new_action.sa_mask);
new_action.sa_sigaction = &clientHandlerTermination;
new_action.sa_flags = SA_SIGINFO | SA_RESTART;
if (sigaction(SIGCHLD, &new_action, NULL) < 0)
{
perror("sigaction");
}
/* Create CONNECTION HANDLER THREAD
* Which sends commands to client handlers
*/
pthread_t interactiveThread;
pthread_create(&interactiveThread, NULL, interactiveServer, NULL);
/* Create socket
* AF_INET tells that ipv4 is being used
* Sock_Stream defines that its a connection based stream (TCP)
*/
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror("opening stream socket");
exit(1);
}
/* Name socket using wildcards */
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(32768);
int option = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
if (bind(sock, (struct sockaddr *)&serverInfo, sizeof(serverInfo)))
{
perror("binding stream socket");
exit(1);
}
/* Find out assigned port number and print it out */
length = sizeof(serverInfo);
if (getsockname(sock, (struct sockaddr *)&serverInfo, (socklen_t *)&length))
{
perror("getting socket name");
exit(1);
}
/* printf("Socket has port #%d\n", ntohs(serverInfo.sin_port));
fflush(stdout);
*/
/* Start accepting connections */
listen(sock, 5);
do
{
msgsock = accept(sock, 0, 0);
if (msgsock == -1)