-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
394 lines (363 loc) · 10.6 KB
/
server.c
File metadata and controls
394 lines (363 loc) · 10.6 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
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <utmp.h>
#include <wait.h>
int search( char a[]){
char user[80];
int p;
int file_desc[2];
if(pipe(file_desc)<0){
printf("Error at pipe");
exit(1);
}
else{
printf("Pipe opened\n");
}
p = fork();
if( p < 0) {
printf("Error occured at fork");
exit(1);
}
else if (p == 0){
printf("Currently in the child\n");
FILE *ptr;
ptr = fopen("clienti.txt","r"); // deschid fisierul care contine username-urile
if(ptr == NULL){
printf("An error occured at opening the clients file");
exit(1);
}
else{
printf("File opened\n");
}
int ok = 0;
while(fgets(user, sizeof(user),ptr)!=NULL && ok == 0){
if(strcmp(a,user) == 0){
ok = 1;
printf("user found: ");
printf("%d\n",ok);
}
}
if(ok == 0){
printf("user not found: ");
printf("%d\n",ok);
}
fclose(ptr);
//trimit informatia catre parinte
close(file_desc[0]);
write(file_desc[1],&ok, sizeof(int));
close(file_desc[1]);
exit(0);
}
else {
wait(NULL);
printf("Currently in the parent\n");
int ok;
close(file_desc[1]);
read(file_desc[0],&ok, sizeof(int));
close(file_desc[0]);
//trimit mesaj catre client
int fd = open("canal", O_WRONLY);
if(fd == -1){
printf("An error occured at opening the channel");
exit(1);
}
else{
printf("File opened\n");
}
if(ok == 1){
strcpy(user,"User found. Welcome back!");
write(fd, user, strlen(user)+1);
}
else{
strcpy(user,"User not found. Please try again");
write(fd, user, strlen(user)+1);
}
close(fd);
return ok;
}
}
void quit()
{
pid_t current_pid = getpid();
kill(current_pid,SIGKILL);
}
void writing_channel(char b[]){ // opening fifo writing channel
int fd = open("canal", O_WRONLY);
if(fd == -1){
printf("An error occured at opening the channel");
exit(1);
}
else{
printf("File opened\n");
}
write(fd, b, strlen(b)+1);
close(fd);
}
void reading_channel(char a[]) // opening fifo reading channel
{
int fd = open("canal", O_RDONLY);
if(fd == -1){
printf("An error occured at opening the channel");
exit(1);
}
else{
printf("File opened\n");
}
read(fd, a, 80);
close(fd);
}
struct my_utmp{
char user[32], host[256];
struct{
int32_t sec;
int32_t usec;
}tv;
}a[256];
void get_users(){
int sockp[2], p, k = 0;
struct utmp *data;
if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockp) < 0){
printf("Error occured at socketpair");
exit(1);
}
else {
printf("Socketpair opened\n");
}
p = fork();
if( p < 0 ){
printf("Error occured at fork");
exit(1);
}
else if( p == 0 ){
printf("We're in the child\n");
close(sockp[0]);// parent file descriptor
data = getutent(); // man: It returns a pointer to a structure containing the fields of the line. The definition of this structure is shown in utmp(5)
// username: char ut_user[UT_NAMESIZE]
// hostname for remote login: char ut_host[UT_HOSTSIZE]
// time entry was made
// #define UT_NAMESIZE 32
// #define UT_HOSTSIZE 256
while(data != NULL)
{
strncpy(a[k].user, data -> ut_user,32);
a[k].user[32]='\0';
strncpy(a[k].host, data -> ut_host, 256);
a[k].host[256]='\0';
a[k].tv.sec = data -> ut_tv.tv_sec;
a[k].tv.usec = data -> ut_tv.tv_usec;
k++;
data = getutent();
}
write(sockp[1], &k, sizeof(int)); // sends size of a[]
int i;
for(i = 0; i<k;i++)
{
write(sockp[1], a[i].user, sizeof(a[i].user));
write(sockp[1], a[i].host, sizeof(a[i].host));
write(sockp[1], &a[i].tv.sec, sizeof(int));
write(sockp[1], &a[i].tv.usec, sizeof(int));
}
printf("Leaving child\n");
close(sockp[1]);
exit(0);
}
else{
wait(NULL);
printf("We're in the parent\n");
close(sockp[1]);
read(sockp[0], &k, sizeof(int));
int i;
for(i = 0;i < k; i++)
{
read(sockp[0], a[i].user, sizeof(a[i].user));
read(sockp[0], a[i].host, sizeof(a[i].host));
read(sockp[0], &a[i].tv.sec, sizeof(int));
read(sockp[0], &a[i].tv.usec, sizeof(int));
}
close(sockp[0]);
FILE* fd = fopen("canal", "w");
if(fd == NULL){
printf("An error occured at opening the channel");
exit(1);
}
else{
printf("File opened\n");
}
for(i = 0; i < k ; i++){
fprintf(fd, "User %s\n",a[i].user);
fprintf(fd, "Host %s\n",a[i].host);
fprintf(fd, "%d\n",a[i].tv.sec);
fprintf(fd, "%d\n",a[i].tv.usec);
printf("User %s ",a[i].user);
printf("Host %s ",a[i].host);
printf("Sec %d ",a[i].tv.sec);
printf("Usec %d ",a[i].tv.usec);
printf("\n");
}
fclose(fd);
printf("Leaving the parent\n");
}
}
struct my_info{
char name[80], state[80],ppid[80],uid[80],vmsize[80];
};
void get_info(char pid[]){
int sockp[2], p,k = 0;
struct my_info b[100];
if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockp) < 0){
printf("Error occured at socketpair");
exit(1);
}
else {
printf("Socketpair opened\n");
}
p = fork();
if( p < 0){
printf("Error occured at fork");
exit(1);
}
else if(p == 0){
printf("We're in the child\n");
close(sockp[0]);
//create /proc/pid/status
char file[80];
file[0]='\0';
strcat(file, "/proc/");
strcat(pid, "/status");
strcat(file, pid);
printf("File created: %s\n",file);
FILE* ptr = fopen(file, "r");
if(ptr == NULL){
printf("Error at opening");
exit(1);
}
else{
printf("File opened\n");
}
char info[80];
while(fgets(info, 80, ptr) != NULL){
info[strlen(info)-1]='\0';
if(strstr(info, "Name") != NULL){
strcpy(b[k].name,info);
}
else if(strstr(info, "State") != NULL){
strcpy(b[k].state,info);
}
else if(strstr(info, "PPid") != NULL){
strcpy(b[k].ppid,info);
}
else if(strstr(info, "Uid") != NULL){
strcpy(b[k].uid,info);
}
else if(strstr(info, "VmSize") != NULL){
strcpy(b[k].vmsize,info);
}
}
fclose(ptr);
write(sockp[1],b[k].name, sizeof(b[k].name));
write(sockp[1],b[k].state, sizeof(b[k].state));
write(sockp[1],b[k].ppid, sizeof(b[k].ppid));
write(sockp[1],b[k].uid,sizeof(b[k].uid));
write(sockp[1],b[k].vmsize,sizeof(b[k].vmsize));
close(sockp[1]);
printf("Leaving child\n");
exit(0);
}
else{//parinte
wait(NULL);
close(sockp[1]);
printf("We're in the parent\n");
read(sockp[0],b[k].name, sizeof(b[k].name));
read(sockp[0],b[k].state, sizeof(b[k].state));
read(sockp[0],b[k].ppid, sizeof(b[k].ppid));
read(sockp[0],b[k].uid,sizeof(b[k].uid));
read(sockp[0],b[k].vmsize,sizeof(b[k].vmsize));
close(sockp[0]);
FILE* fd = fopen("canal","w");
if(fd == NULL){
printf("An error occured at opening the channel");
exit(1);
}
else{
printf("File opened\n");
}
fprintf(fd, "%s\n",b[k].name);
fprintf(fd, "%s\n",b[k].state);
fprintf(fd, "%s\n",b[k].ppid);
fprintf(fd, "%s\n",b[k].uid);
fprintf(fd, "%s\n",b[k].vmsize);
printf("Name %s\n",b[k].name);
printf("State %s\n",b[k].state);
printf("Ppid %s\n",b[k].ppid);
printf("Uid %s\n",b[k].uid);
printf("VmSize %s\n",b[k].vmsize);
fclose(fd);
printf("Leaving the parent\n");
}
}
int main()
{
int logged;
mkfifo("canal",0666);
char a[80], b[80];
while(1){
reading_channel(a);
printf("CLIENTUL cere: %s\n", a); // comanda dorita de client
if( strstr(a,"login") != NULL && logged == 0){
strcpy(b,"Write your username");
writing_channel(b);
reading_channel(a);
printf("Username-ul este: %s\n", a);
logged = search(a); // daca 1 = logged, 0 else
printf("logged %d\n",logged);
}
else if(strstr(a,"login") != NULL && logged == 1){
printf("already logged in\n");
strcpy(b,"Already logged in");
writing_channel(b);
}
else if(strstr(a,"users") != NULL && logged == 1 ){
get_users();
}
else if(strstr(a, "info") != NULL && logged == 1 ){
strcpy(b,"Write the process id");
writing_channel(b);
reading_channel(a);
printf("pid-ul este: %s\n", a);
a[strlen(a)-1]='\0';
get_info(a);
}
//apel logout
else if(strstr(a,"logout") != NULL && logged == 1){
logged = 0;
printf("logged %d\n", logged);
strcpy(b,"Logged out succesfully");
writing_channel(b);
}
else if((strstr(a,"logout") != NULL && logged == 0 ) || ( strstr(a,"users") != NULL && logged == 0 ) || ( strstr(a,"info") != NULL && logged == 0 )){
printf("logged %d\n", logged);
strcpy(b,"Not logged in");
writing_channel(b);
}
else if(strstr(a,"quit")!=NULL){
printf("Se inchid programele\n");
strcpy(b,"Goodbye");
writing_channel(b);
quit();
}
else{ // nu cunoaste comanda
printf("unknown command\n");
strcpy(b,"Unknown command, try again");
writing_channel(b);
}
}
return 0;
}