-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
628 lines (562 loc) · 15.2 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <sys/poll.h>
#include <arpa/inet.h>
#define ERRINV "Invalid Command\n"
#define ERRINC "Incomplete Command\n"
#define CLITERM "Client Terminated\n"
#define CLIDISCONN "Client Disconnected\n"
#define HELP "add: Syntax: add [number1] [number2] ...\n\nsub: Syntax: sub [number1] [number2] ...\n\nmult: Syntax: mult [number1] [number2] ...\n\ndiv: Syntax: div [number1] [number2] ...\n\nrun: Syntax: run [program] [filename1] [filename2] ...\n\nlist: Syntax: list [all]\n\nexit: Syntax: exit\n\nexit: Syntax: kill [Process Id]\n\n"
//#define LISTALL "--------------------------------------------------------------------------\n Process Name | PId | Status | StartTime | End Time | ElapsedTime\n--------------------------------------------------------------------------\n"
//#define LIST "-------------------------------------------------\n Process Name | PId | Status | StartTime \n-------------------------------------------------\n
void add(int);
void sub(int);
void mult(int);
void divide(int);
void list(int);
void sig_handler_client(int);
void sig_handler_server(int);
void run(int);
void killProcess(int);
void listConns();
void listAll(int);
void killAll();
struct conn{
int connId;
char ip[INET_ADDRSTRLEN];
int port;
int pid;
int rfd;//server read fd
int wfd;//client write fd
int status;
};
struct processInfo{
char name[50];
int pid;
int active;
time_t st_t;
time_t et_t;
int elapsedTime;
};
int processCount=0;
int connCount=0;
int is_from_server=0;
struct processInfo pinfo[100];
struct conn conns[500];
char answer[100];
int result = 0;
int number;
char *token;
int p1[2];
int p2[2];
const char listAllHeader[]= "--------------------------------------------------------------------------\n Process Name | PId | Status | StartTime | End Time | ElapsedTime\n--------------------------------------------------------------------------\n\0";
const char listHeader[]="-------------------------------------------------\n Process Name | PId | Status | StartTime \n-------------------------------------------------\n";
const char connListHeader[]="-----------------------------------------------\nConn Id | IP Address | Port | Pid | Status \n-----------------------------------------------\n\0";
int
main (int argc, char *argv[])
{
signal(SIGCHLD,sig_handler_server);
int ser_sock, ser_len,cli_len,msgsock,ser_port,cli_port;
struct sockaddr_in server,client;
char server_ip[INET_ADDRSTRLEN],client_ip[INET_ADDRSTRLEN];
cli_len=sizeof(client);
/* Create socket */
ser_sock = socket(AF_INET, SOCK_STREAM, 0);
if (ser_sock < 0) {
perror("opening stream socket");
exit(1);
}
/* Name socket using wildcards */
server.sin_family = AF_INET;
server.sin_addr.s_addr = (INADDR_ANY);
server.sin_port = 0;
if (bind(ser_sock, (struct sockaddr *) &server, sizeof(server))) {
perror("binding stream socket");
exit(1);
}
/* Find out assigned port number and print it out */
ser_len = sizeof(server);
if (getsockname(ser_sock, (struct sockaddr *) &server, (socklen_t*) &ser_len)) {
perror("getting socket name");
exit(1);
}
inet_ntop(AF_INET, &(server.sin_addr), server_ip, INET_ADDRSTRLEN);
ser_port=ntohs(server.sin_port);
printf("Socket has port #%d\n",ser_port );
printf("Socket has ip %s\n",server_ip);
fflush(stdout);
/* Start accepting connections */
listen(ser_sock, 5);
//p1[0]--->read fd
//p2[1]--->write fd
struct pollfd fds[2];
int ready ;
fds[0].fd=ser_sock;
fds[0].events=POLLIN;
fds[1].fd=0;
fds[1].events=POLLIN;
do {
int ret=poll(fds,2,-1);
if(ret<0){
//perror("Poll in Server:");
if(errno==EINTR)
ret=poll(fds,2,-1);
}
if(fds[1].revents==POLLIN && fds[1].fd==0){
char buff3[100],result[100];
int read_count=read(0,buff3,sizeof(buff3));
buff3[read_count]='\0';
if(read_count==1)
continue;
char* token = strtok(buff3, " ,\n");
if(strcmp(token,"list")==0){
token=strtok(NULL," ,\n");
if(token!=NULL && strcmp(token,"conn")==0){
listConns();
}
if(token!=NULL && strcmp(token,"process")==0){
char pBuff[5000];
pBuff[0]='\0';
strcpy(pBuff,"No Connections\n");
for(int i =0;i<connCount;i++){
if(i==0)
pBuff[0]='\0';
if(conns[i].status==1){
char temp[1000];
int r=sprintf(temp," Conn Id: %2d\n",conns[i].connId);
temp[r]='\0';
strcat(pBuff,temp);
temp[0]='\0';
if(write(conns[i].wfd,"list",4)<0)
perror("write error");
r=read(conns[i].rfd,temp,sizeof(temp));
if(r<0)
perror("read error");
temp[r]='\0';
strcat(pBuff,temp);
}
}
strcat(pBuff,"\0");
write(1,pBuff,strlen(pBuff));
}
}
else if(strcmp(token,"exit")==0){
for(int i=0; i<connCount;i++){
if(conns[i].status==1){
if(write(conns[i].wfd,"exit",4)<0)
perror("write error");
write(1,"ending a client\n",16);
}
}
exit(0);
}else{
write(1,ERRINV,strlen(ERRINV));
}
}
if(fds[0].revents==POLLIN && fds[0].fd==ser_sock){
msgsock = accept(ser_sock, (struct sockaddr *)&client, &cli_len);
if (msgsock == -1){
perror("accept");
continue;
}
inet_ntop(AF_INET, &(client.sin_addr), client_ip, INET_ADDRSTRLEN);
cli_port=ntohs(client.sin_port);
printf("Starting connection with %s\n",client_ip);
if(pipe(p1)<0)
perror("pipe");
if(pipe(p2)<0)
perror("pipe");
int f=fork();
if(f>0){
conns[connCount].connId=connCount;
conns[connCount].pid=f;
strcpy(conns[connCount].ip,client_ip);
conns[connCount].port=ser_port;
conns[connCount].wfd=p1[1];
conns[connCount].rfd=p2[0];
conns[connCount].status=1;
connCount++;
close(p1[0]);
close(p2[1]);
}
if(f==0){
close(p2[0]);//p2[0]-->read fd
close(p1[1]);//p1[1]-->write fd
signal(SIGCHLD,sig_handler_client);
struct pollfd fds2[2];
int ready2;
fds2[0].fd=msgsock;
fds2[0].events=POLLIN;
fds2[1].fd=p1[0];
fds2[1].events=POLLIN;
while(1==1){
int ret2=poll(fds2,2,-1);
if(ret2<0){
//perror("Poll in Client:");
}
if(fds2[0].revents==POLLIN && fds2[0].fd==msgsock){
char firstCount[3];
int rc=read(msgsock,firstCount,3);
char *t=strtok(firstCount," \n");
int count=(int)atoi(t);
char str[100];
int read_count = read(msgsock,str,count);
str[read_count]='\0';
char* token = strtok(str, " ,\n");
if(strcmp(token,"add")==0)
add(msgsock);
else if(strcmp(token,"sub")==0)
sub(msgsock);
else if(strcmp(token,"mult")==0)
mult(msgsock);
else if(strcmp(token,"div")==0)
divide(msgsock);
else if(strcmp(token,"help")==0){
token = strtok(NULL, " ,\n");
if(token==NULL)
write(msgsock,HELP, strlen(HELP));
else
write(msgsock,ERRINV,strlen(ERRINV));
}
else if (strcmp(token,"run")==0)
run(msgsock);
else if(strcmp(token,"exit")==0){
write(msgsock,CLITERM, strlen(CLITERM));
write(1,CLITERM, strlen(CLITERM));
killAll();
close(msgsock);
exit(0);
}
else if(strcmp(token,"disconnect")==0){
write(msgsock,CLIDISCONN, strlen(CLIDISCONN));
write(1,CLIDISCONN, strlen(CLIDISCONN));
killAll();
close(msgsock);
exit(0);
}
else if(strcmp(token,"list")==0){
token = strtok(NULL, " ,\n");
if(token!=NULL && strcmp(token,"all")==0){
token = strtok(NULL, " ,\n");
if(token==NULL)
listAll(msgsock);
else
write(msgsock,ERRINV,strlen(ERRINV));
}else if(token==NULL){
list(msgsock);
}else{
write(msgsock,ERRINV,strlen(ERRINV));
}
}
else if(strcmp(token,"kill")==0)
killProcess(msgsock);
else
write(msgsock,ERRINV,strlen(ERRINV));
}
if(fds2[1].revents==POLLIN && fds2[1].fd==p1[0]){
char t[10];
int rcount=read(p1[0],t,4);
if(rcount==-1)
perror("read error:");
t[rcount]='\0';
if(strcmp(t,"list")==0)
listAll(p2[1]);
else if (strcmp(t,"exit")==0){
write(msgsock,CLITERM, strlen(CLITERM));
write(1,CLITERM, strlen(CLITERM));
killAll();
close(msgsock);
exit(0);
}
}
}
}
}
}while(1==1);
}
//--------------------------------------------------------------------------------------------------------------------------------
void add(int msgsock){
char answer[100];
result=0;
char *token = strtok(NULL, " ,\n");
while(token!=NULL){
number = (int)atoi(token);
result = result + number;
token = strtok(NULL, " ,\n");
}
sprintf(answer, "%d\n", result);
write(msgsock,answer,strlen(answer));
}
void sub(int msgsock){
char answer[100];
result=0;
token = strtok(NULL, " ,\n");
if(token!=NULL){
number = (int)atoi(token);
result = number;
}
else{
write(msgsock,ERRINC,strlen(ERRINC));
return;
}
token = strtok(NULL, " ,\n");
while(token!=NULL){
number = (int)atoi(token);
result = result-number;
token = strtok(NULL, " ,\n");
}
sprintf(answer, "%d\n", result);
write(msgsock,answer,strlen(answer));
}
void mult(int msgsock){
char answer[100];
token = strtok(NULL, " ,\n");
if(token!=NULL){
number = (int)atoi(token);
result = number;
}
else{
write(msgsock,ERRINC,strlen(ERRINC));
return;
}
token = strtok(NULL, " ,\n");
while(token!=NULL){
number = (int)atoi(token);
result = result*number;
token = strtok(NULL, " ,\n");
}
sprintf(answer, "%d\n", result);
write(msgsock,answer,strlen(answer));
}
void divide(int msgsock){
char answer[100];
float result;
token = strtok(NULL, " ,\n");
if(token!=NULL){
number = (int)atoi(token);
result = number;
}
else{
write(msgsock,ERRINC,strlen(ERRINC));
return;
}
token = strtok(NULL, " ,\n");
while(token!=NULL){
number = (int)atoi(token);
result = result/number;
token = strtok(NULL, " ,\n");
}
sprintf(answer, "%0.4f\n", result);
write(msgsock,answer,strlen(answer));
}
void listAll(int msgsock){
int i;
char buff[5000];
char temp[100];
strcpy(buff,listAllHeader);
for(i=0;i<processCount;i++){
struct tm *st=localtime(&(pinfo[i].st_t));
sprintf(temp,"%16s | %5d | %6d | %02d:%02d:%02d ",pinfo[i].name,pinfo[i].pid,pinfo[i].active,
st->tm_hour,st->tm_min,st->tm_sec);
strcat(temp,"\0");
strcat(buff,temp);
if(pinfo[i].active!=1){
struct tm *et=localtime(&(pinfo[i].et_t));
sprintf(temp,"| %02d:%02d:%02d | %02dm %02ds ",et->tm_hour,et->tm_min,et->tm_sec,
pinfo[i].elapsedTime/60,pinfo[i].elapsedTime%60);
strcat(temp,"\0");
strcat(buff,temp);
}
strcat(buff,"\n");
}
strcat(buff,"\0");
write(msgsock,buff,strlen(buff));
}
void list(int msgsock){
int i;
char buff2[5000];
char temp[100];
strcpy(buff2,listHeader);
for(i=0;i<processCount;i++){
if(pinfo[i].active==1){
struct tm *st=localtime(&(pinfo[i].st_t));
sprintf(temp,"%16s | %5d | %6d | %02d:%02d:%02d \n",pinfo[i].name,pinfo[i].pid,pinfo[i].active,
st->tm_hour,st->tm_min,st->tm_sec);
strcat(buff2,temp);
}
}
strcat(buff2,"\0");
write(msgsock,buff2,strlen(buff2));
}
void sig_handler_client(int signo){
if(signo==SIGCHLD){
int status,count;
int pid=waitpid(-1,&status,WNOHANG);
while(pid>0){
count=0;
while(count<processCount){
if(pinfo[count].pid==pid){
pinfo[count].active=0;
pinfo[count].et_t=time(NULL);
pinfo[count].elapsedTime=pinfo[count].et_t-pinfo[count].st_t;
}
count++;
}
pid=waitpid(-1,&status,WNOHANG);
}
}
}
void run(int msgsock){
char *mylist[10];
int count=0;
token = strtok(NULL, " ,\n");
if(token!=NULL){
while(token!=NULL){
mylist[count++]=token;
token = strtok(NULL, " ,\n");
}
mylist[count]=NULL;
//creating new process child
int fd[2];
if(pipe(fd)<0){
write(msgsock,strerror(errno),strlen(strerror(errno)));
return;
}
int flags=fcntl(fd[1],F_GETFD);
flags |=FD_CLOEXEC;
fcntl(fd[1],F_SETFD,flags);
int pid=fork();
if(pid==-1){
write(msgsock,strerror(errno),strlen(strerror(errno)));
return;
}
//------------New process Code------------------
else if(pid==0){
close(fd[0]);
//if it fails then it will be exit with status 99
//status will be used in waitpid
if(execvp(mylist[0],mylist)==-1){
if(write(fd[1],strerror(errno),strlen(strerror(errno)))<0)
perror("exec failed error:");
exit(99);
}
}
//------------Parent Process Code------------------
else if(pid>0){
close(fd[1]);
char temp[100];
int rc=read(fd[0],temp,sizeof(temp));
if(rc==-1){
if(write(msgsock,strerror(errno),strlen(strerror(errno))))
perror("run write msgsock error:");
return;
}
else if(rc==0){
strcpy(pinfo[processCount].name,mylist[0]);
pinfo[processCount].pid=pid;
pinfo[processCount].active=1;
pinfo[processCount].st_t=time(NULL);
processCount++;
if(write(msgsock,"Success\n",8)<0)
perror("run write success error:");
}
else if(rc>0){
if(sprintf(temp,"%s\n",temp)<0)
perror("sprintf error:");
if(write(msgsock,temp,rc+1)<0)
perror("run write error:");
}
}
}
else{
write(msgsock,ERRINC,strlen(ERRINC));
return;
}
}
void killAll()
{
for(int i=0;i<processCount;i++){
if(pinfo[i].active==1)
kill(pinfo[i].pid,SIGTERM);
}
}
void killProcess(int msgsock){
token = strtok(NULL, " ,\n");
if(token==NULL){
write(msgsock,ERRINC,strlen(ERRINC));
}else if(strcmp(token,"all")==0){
for(int i=0;i<processCount;i++){
if(pinfo[i].active==1)
kill(pinfo[i].pid,SIGTERM);
}
write(msgsock,"All Processes terminated\n",25);
}else{
int found=0;
for(int i=0;i<processCount;i++){
if(strcmp(pinfo[i].name,token)==0){
found=1;
if(pinfo[i].active==1){
kill(pinfo[i].pid,SIGTERM);
write(msgsock,"Process terminated\n",19);
return;
}
}
}
int kpid=atoi(token);
found=0;
for(int i=0;i<processCount;i++){
if(pinfo[i].pid==kpid){
found=1;
if(pinfo[i].active==0){
write(msgsock,"Already terminated\n",19);
}else{
kill(kpid,SIGTERM);
write(msgsock,"Process terminated\n",19);
}
return;
}
}
if(found==0){
write(msgsock,"Process with the given name/pid is not found/running\n",53);
}
}
}
void listConns(){
char buff4[5000];
char temp[100];
strcpy(buff4,connListHeader);
for(int i=0;i<connCount;i++){
sprintf(temp,"%7d | %12s | %05d | %5d | %d",conns[i].connId,conns[i].ip,conns[i].port,conns[i].pid,conns[i].status);
strcat(buff4,temp);
strcat(buff4,"\n");
}
write(0,buff4,strlen(buff4));
}
void sig_handler_server(int signo){
if(signo==SIGCHLD){
int status,count;
int pid=waitpid(-1,&status,WNOHANG);
while(pid>0){
count=0;
while(count<connCount){
if(conns[count].pid==pid){
conns[count].status=0;
}
count++;
}
pid=waitpid(-1,&status,WNOHANG);
}
}
}