-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsh.c
245 lines (195 loc) · 5.42 KB
/
rsh.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
#include <stdio.h>
#include <stdlib.h>
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#define N 13
extern char **environ;
char uName[20];
char *allowed[N] = {"cp","touch","mkdir","ls","pwd","cat","grep","chmod","diff","cd","exit","help","sendmsg"};
struct message {
char source[50];
char target[50];
char msg[200];
};
void terminate(int sig) {
printf("Exiting....\n");
fflush(stdout);
exit(0);
}
void sendmsg (char *user, char *target, char *msg) {
// TODO:
// Send a request to the server to send the message (msg) to the target user (target)
// by creating the message structure and writing it to server's FIFO
int server;
//open server FIFO for writing
server = open("serverFIFO", O_WRONLY);
if(server < 0) {
//throw error if serverFIFO doesn't open
perror("Error opening serverFIFO");
return;
}
//populate message structure
struct message req;
snprintf(req.source, sizeof(req.source), "%s", user);
snprintf(req.target, sizeof(req.target), "%s", target);
snprintf(req.msg, sizeof(req.msg), "%s", msg);
if(write(server, &req, sizeof(req)) < 0) {
perror("Error writing to server FIFO");
}
close(server);
}
void* messageListener(void *arg) {
// TODO:
// Read user's own FIFO in an infinite loop for incoming messages
// The logic is similar to a server listening to requests
// print the incoming message to the standard output in the
// following format
// Incoming message from [source]: [message]
// put an end of line at the end of the message
char userFIFO[256];
//print user's FIFO
snprintf(userFIFO, sizeof(userFIFO), "%s", uName);
int userFD;
userFD = open(userFIFO, O_RDONLY);
if (userFD < 0) {
//throw error if user FIFO doesn't open
perror("Error opening user FIFO");
pthread_exit(NULL);
}
struct message incomingMsg;
while (read(userFD, &incomingMsg, sizeof(incomingMsg)) > 0) {
//if there are bytes to read, print message
printf("Incoming message from %s: %s\n", incomingMsg.source, incomingMsg.msg);
fflush(stdout);
}
close(userFD);
//pthread_exit((void*)0);
return NULL;
}
int isAllowed(const char*cmd) {
int i;
for (i=0;i<N;i++) {
if (strcmp(cmd,allowed[i])==0) {
return 1;
}
}
return 0;
}
int main(int argc, char **argv) {
pid_t pid;
char **cargv;
char *path;
char line[256];
int status;
posix_spawnattr_t attr;
if (argc!=2) {
printf("Usage: ./rsh <username>\n");
exit(1);
}
signal(SIGINT,terminate);
strcpy(uName,argv[1]);
// TODO:
// create the message listener thread
pthread_t listenerThread;
//test if the thread was created, if not created throw error
/*if(pthread_create(&listenerThread, NULL, messageListener, NULL) != 0 ) {
perror("Error creating message listener thread");
exit(1);
}*/
if(pthread_create(&listenerThread, NULL, messageListener, (void*)uName) != 0 ) {
perror("Error creating message listener thread");
exit(1);
}
while (1) {
fprintf(stderr,"rsh>");
if (fgets(line,256,stdin)==NULL) continue;
if (strcmp(line,"\n")==0) continue;
line[strlen(line)-1]='\0';
char cmd[256];
char line2[256];
strcpy(line2,line);
strcpy(cmd,strtok(line," "));
if (!isAllowed(cmd)) {
printf("NOT ALLOWED!\n");
continue;
}
if (strcmp(cmd,"sendmsg")==0) {
// TODO: Create the target user and
// the message string and call the sendmsg function
// NOTE: The message itself can contain spaces
// If the user types: "sendmsg user1 hello there"
// target should be "user1"
// and the message should be "hello there"
// if no argument is specified, you should print the following
// printf("sendmsg: you have to specify target user\n");
// if no message is specified, you should print the followingA
// printf("sendmsg: you have to enter a message\n");
char *target = strtok(NULL, " ");
if (target == NULL) {
printf("sendmsg: you have to specify target user\n");
continue;
}
char *msg = strtok(NULL, "");
if (msg == NULL) {
printf("sendmsg: you have to enter a message\n");
continue;
}
sendmsg(uName, target, msg);
continue;
}
if (strcmp(cmd,"exit")==0) break;
if (strcmp(cmd,"cd")==0) {
char *targetDir=strtok(NULL," ");
if (strtok(NULL," ")!=NULL) {
printf("-rsh: cd: too many arguments\n");
}
else {
chdir(targetDir);
}
continue;
}
if (strcmp(cmd,"help")==0) {
printf("The allowed commands are:\n");
for (int i=0;i<N;i++) {
printf("%d: %s\n",i+1,allowed[i]);
}
continue;
}
cargv = (char**)malloc(sizeof(char*));
cargv[0] = (char *)malloc(strlen(cmd)+1);
path = (char *)malloc(9+strlen(cmd)+1);
strcpy(path,cmd);
strcpy(cargv[0],cmd);
char *attrToken = strtok(line2," "); /* skip cargv[0] which is completed already */
attrToken = strtok(NULL, " ");
int n = 1;
while (attrToken!=NULL) {
n++;
cargv = (char**)realloc(cargv,sizeof(char*)*n);
cargv[n-1] = (char *)malloc(strlen(attrToken)+1);
strcpy(cargv[n-1],attrToken);
attrToken = strtok(NULL, " ");
}
cargv = (char**)realloc(cargv,sizeof(char*)*(n+1));
cargv[n] = NULL;
// Initialize spawn attributes
posix_spawnattr_init(&attr);
// Spawn a new process
if (posix_spawnp(&pid, path, NULL, &attr, cargv, environ) != 0) {
perror("spawn failed");
exit(EXIT_FAILURE);
}
// Wait for the spawned process to terminate
if (waitpid(pid, &status, 0) == -1) {
perror("waitpid failed");
exit(EXIT_FAILURE);
}
// Destroy spawn attributes
posix_spawnattr_destroy(&attr);
}
return 0;
}