Skip to content
Snippets Groups Projects
Commit bbf2b84f authored by HongJiHo's avatar HongJiHo
Browse files

mysh-1 finish

parent 98fbc382
No related branches found
No related tags found
No related merge requests found
...@@ -36,7 +36,12 @@ int do_fg(int argc, char** argv) { ...@@ -36,7 +36,12 @@ int do_fg(int argc, char** argv) {
if (!validate_fg_argv(argc, argv)) if (!validate_fg_argv(argc, argv))
return -1; return -1;
// TODO: Fill this. int pid = waitpid(0, 0, 0);
if(pid == -1)
printf("no such jobs.\n");
else
printf("%d done\n", pid);
return 0; return 0;
} }
......
...@@ -2,16 +2,44 @@ ...@@ -2,16 +2,44 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <pthread.h>
#include "commands.h" #include "commands.h"
#include "built_in.h" #include "built_in.h"
#define SOCK_PATH "tpf_unix_sock.server"
#define CLIENT_PATH "tpf_unix_sock.client"
#define UNIX_PATH_MAX 108
pid_t pID;
/*
struct sockaddr_un{
unsigned short int sun_family; //AF_UNIX
char sun_path[UNIX_PATH_MAX]; //pathname
};*/
static char PATH[5][1028] = {
{"/usr/local/bin/"},
{"/usr/bin/"},
{"/bin/"},
{"/usr/sbin/"},
{"/sbin/"}
};
static struct built_in_command built_in_commands[] = { static struct built_in_command built_in_commands[] = {
{ "cd", do_cd, validate_cd_argv }, { "cd", do_cd, validate_cd_argv },
{ "pwd", do_pwd, validate_pwd_argv }, { "pwd", do_pwd, validate_pwd_argv },
{ "fg", do_fg, validate_fg_argv } { "fg", do_fg, validate_fg_argv }
}; };
void server_thread();
void client_thread(void* command);
static int is_built_in_command(const char* command_name) static int is_built_in_command(const char* command_name)
{ {
static const int n_built_in_commands = sizeof(built_in_commands) / sizeof(built_in_commands[0]); static const int n_built_in_commands = sizeof(built_in_commands) / sizeof(built_in_commands[0]);
...@@ -30,11 +58,23 @@ static int is_built_in_command(const char* command_name) ...@@ -30,11 +58,23 @@ static int is_built_in_command(const char* command_name)
*/ */
int evaluate_command(int n_commands, struct single_command (*commands)[512]) int evaluate_command(int n_commands, struct single_command (*commands)[512])
{ {
if (n_commands > 0) {
struct single_command* com = (*commands); struct single_command* com = (*commands);
if (n_commands == 1) {
//struct single_command* com = (*commands);
assert(com->argc != 0); assert(com->argc != 0);
//background processing
int background = 0;
if(strcmp(com->argv[com->argc-1], "&") == 0){
background = 1;
com->argv[com->argc-1] = NULL;
com->argc = com->argc-1;
}
//do built_in_command
int built_in_pos = is_built_in_command(com->argv[0]); int built_in_pos = is_built_in_command(com->argv[0]);
if (built_in_pos != -1) { if (built_in_pos != -1) {
if (built_in_commands[built_in_pos].command_validate(com->argc, com->argv)) { if (built_in_commands[built_in_pos].command_validate(com->argc, com->argv)) {
...@@ -50,12 +90,222 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512]) ...@@ -50,12 +90,222 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512])
} else if (strcmp(com->argv[0], "exit") == 0) { } else if (strcmp(com->argv[0], "exit") == 0) {
return 1; return 1;
} else { } else {
//process creation
pID = fork();
//fork error
if(pID == -1){
fprintf(stderr, "fork fail\n");
}
//child process
else if(pID == 0){
path_exec(com);
fprintf(stderr, "%s: command not found\n", com->argv[0]); fprintf(stderr, "%s: command not found\n", com->argv[0]);
return -1; exit(0);
}
//parent process
else{
if(background == 1)
printf("background pid : %d\n", pID);
else
waitpid(pID, 0, 0);
}
return 0;
}
}
//IPC
else if(n_commands >= 2){
void* status;
pthread_t threads[2];
int pid = fork();
if(pid == 0){
for(int l = 0; l < n_commands; l++){
pthread_create(&threads[1], NULL, &client_thread, (void *) com);
pthread_join(threads[1], &status);
com++;
}
exit(0);
} else{
pthread_create(&threads[0], NULL, &server_thread, (void *)&n_commands);
waitpid(pid, NULL, 0);
pthread_join(threads[0], NULL);
}
return 0;
}
return 0;
}
void path_exec(struct single_command* com){
if(execv(com->argv[0], com->argv) == -1){
//need path resoultion
char *tmp = com->argv[0];
for(int k = 0; k < 5; k++){
com->argv[0] = strcat(PATH[k], tmp);
if(execv(com->argv[0], com->argv) == -1)
com->argv[0] = tmp;
else
break;
}
}
}
void server_thread(){
int server_sock, client_sock, len;
struct sockaddr_un server_sockaddr;
struct sockaddr_un client_sockaddr;
char buf[256];
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
memset(buf, 0, 256);
//create domain stream socket
server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if(server_sock == -1){
printf("SOCKET ERROR\n");
exit(1);
}
//bind
server_sockaddr.sun_family = AF_UNIX;
strcpy(server_sockaddr.sun_path, SOCK_PATH);
len = sizeof(server_sockaddr);
unlink(SOCK_PATH);
if(bind(server_sock, (struct sockaddr *) &server_sockaddr, len) == -1){
printf("BIND ERROR\n");
close(server_sock);
exit(1);
}
//listen
if(listen(server_sock, 10) == -1){
printf("LISTEN ERROR\n");
close(server_sock);
exit(1);
}
//accept
client_sock = accept(server_sock, (struct sockaddr *) &client_sockaddr, &len);
if(client_sock == -1){
printf("ACCEPT ERROR\n");
close(server_sock);
close(client_sock);
exit(1);
} }
dup2(client_sock, STDIN_FILENO);
int h = 2;
while(h--){
//send data
memset(buf, 0, 256);
if(send(client_sock, buf, strlen(buf), 0) == -1){
printf("SEND ERROR\n");
close(server_sock);
close(client_sock);
exit(1);
} }
//read and print
if(recv(client_sock, buf, sizeof(buf), 0) == -1){
printf("REC ERROR\n");
close(server_sock);
close(client_sock);
exit(1);
}
else
printf("DATA RECEIVED = %s\n", buf);
}
//close
close(server_sock);
close(client_sock);
return 0; return 0;
}
void client_thread(void* command){
int client_sock, len;
struct sockaddr_un server_sockaddr;
struct sockaddr_un client_sockaddr;
char buf[256];
memset(&server_sockaddr, 0, sizeof(struct sockaddr_un));
memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
//create
client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if(client_sock == -1){
printf("SOCKET ERROR\n");
exit(1);
}
//bind
client_sockaddr.sun_family = AF_UNIX;
strcpy(client_sockaddr.sun_path, CLIENT_PATH);
len = sizeof(client_sockaddr);
unlink(CLIENT_PATH);
if(bind(client_sock, (struct sockaddr *) &client_sockaddr, len) == -1){
printf("BIND ERROR\n");
close(client_sock);
exit(1);
}
//set up
server_sockaddr.sun_family = AF_UNIX;
strcpy(server_sockaddr.sun_path, SOCK_PATH);
if(connect(client_sock, (struct sockaddr *) &server_sockaddr, len) == -1){
printf("CONNECT ERROR\n");
close(client_sock);
exit(1);
}
void* status;
int tempfd = dup(STDOUT_FILENO);
int pid = fork();
if(pid == 0){
path_exec(command);
exit(0);
}
else{
waitpid(-1, &status, 0);
dup2(tempfd, STDOUT_FILENO);
close(tempfd);
}
int h = 2;
while(h--){
//send
if(send(client_sock, buf, strlen(buf), 0) == -1){
printf("SEND ERROR\n");
close(client_sock);
exit(1);
}
//Read
memset(buf, 0, sizeof(buf));
if(recv(client_sock, buf, sizeof(buf), 0) == -1){
printf("RECV ERROR \n");
close(client_sock);
exit(1);
}
else
printf("DATA RECEIVED = %s\n", buf);
}
//close
close(client_sock);
return 0;
} }
void free_commands(int n_commands, struct single_command (*commands)[512]) void free_commands(int n_commands, struct single_command (*commands)[512])
...@@ -74,3 +324,4 @@ void free_commands(int n_commands, struct single_command (*commands)[512]) ...@@ -74,3 +324,4 @@ void free_commands(int n_commands, struct single_command (*commands)[512])
memset((*commands), 0, sizeof(struct single_command) * n_commands); memset((*commands), 0, sizeof(struct single_command) * n_commands);
} }
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h>
#include "commands.h" #include "commands.h"
#include "built_in.h" #include "built_in.h"
#include "utils.h" #include "utils.h"
#include "signal_handlers.h"
int main() int main()
{ {
char buf[8096]; char buf[8096];
catch_sigint(SIGINT);
catch_sigtstp(SIGTSTP);
while (1) { while (1) {
printf("$ ");
fgets(buf, 8096, stdin); fgets(buf, 8096, stdin);
struct single_command commands[512]; struct single_command commands[512];
...@@ -25,6 +30,7 @@ int main() ...@@ -25,6 +30,7 @@ int main()
if (ret == 1) { if (ret == 1) {
break; break;
} }
memset(buf, 0, sizeof(buf));
} }
return 0; return 0;
......
#include <signal.h>
#include "signal_handlers.h" #include "signal_handlers.h"
void catch_sigint(int signalNo) void catch_sigint(int signalNo) //process interrupt
{ {
// TODO: File this! signal(signalNo, SIG_IGN);
} }
void catch_sigtstp(int signalNo) void catch_sigtstp(int signalNo) //process stop
{ {
// TODO: File this! signal(signalNo, SIG_IGN);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment