diff --git a/src/built_in.c b/src/built_in.c index 1644fdb21fff3b3bfbf578749722667960d92c66..647d3fe5d1f542b51c49312d53a1b10ee9d988fb 100644 --- a/src/built_in.c +++ b/src/built_in.c @@ -3,7 +3,6 @@ #include <sys/types.h> #include <sys/stat.h> -#include <sys/wait.h> #include <unistd.h> #include <linux/limits.h> @@ -37,21 +36,6 @@ int do_fg(int argc, char** argv) { if (!validate_fg_argv(argc, argv)) return -1; - int pid; - int stat; - - pid = fork(); - waitpid(pid, &stat, 0); - - if(WIFEXITED(stat)) - { - printf("%d done", pid); - } - else - { - printf("%d running", pid); - } - return 0; } diff --git a/src/commands.c b/src/commands.c index eea613d2966bbd1dfa0f8e09b1b188770476e096..a43340a4bfb2e927ba46174278e33c5b14578ef5 100644 --- a/src/commands.c +++ b/src/commands.c @@ -3,24 +3,27 @@ #include <string.h> #include <assert.h> #include <unistd.h> -#include <sys/types.h> -#include <sys/socket.h> +#include <signal.h> +#include <pthread.h> #include <sys/un.h> +#include <sys/types.h> #include <sys/wait.h> -#include <errno.h> -#include <pthread.h> - - - +#include <sys/socket.h> #include "commands.h" #include "built_in.h" +#define SOCK_PATH "tpf_unix_sock.socket" +#define CLIENT_PATH "tpf_unix_sock.client" + static struct built_in_command built_in_commands[] = { { "cd", do_cd, validate_cd_argv }, { "pwd", do_pwd, validate_pwd_argv }, { "fg", do_fg, validate_fg_argv } }; +char path_1[5][256]={"/usr/local/bin","/usr/bin","/bin","/usr/sbin","/sbin"}; +int status=0; + 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]); @@ -34,72 +37,193 @@ static int is_built_in_command(const char* command_name) return -1; // Not found } -/* - * Description: Currently this function only handles single built_in commands. You should modify this structure to launch process and offer pipeline functionality. - */ +//int evaluate_command(int n_commands, struct single_command (*commands)[512]); +//void client(struct single_command (*commands)[512]); + +void *server(void *arg) +{ + int server_socket,client_socket,rc; + int backlog = 10; + struct sockaddr_un server_addr,client_addr; + server_socket=socket(AF_UNIX,SOCK_STREAM,0); + memset(&server_addr,0,sizeof(server_addr)); + server_addr.sun_family=AF_UNIX; + strcpy(server_addr.sun_path,SOCK_PATH); + unlink(SOCK_PATH); + + while(1) + { + if(bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))==-1) + { + exit(0); + } + + rc = listen(server_socket, backlog); + if (rc == -1) + { + close(server_socket); + exit(1); + } + + if(listen(server_socket,5)==-1) + { + exit(0); + } + + int client_addr_size=sizeof(client_addr); + client_socket=accept(server_socket,(struct sockaddr*)&client_addr,&client_addr_size); + + if(fork()==0) + { + close(0); + dup2(client_socket,0); + struct single_command proc[512]; + struct single_command *b=(struct single_command *)arg; + memcpy(&proc[0],b,sizeof(struct single_command)); + evaluate_command(1,&proc); + exit(0); + } + + wait(&status); + close(client_socket); + close(server_socket); + pthread_exit(0); + exit(0); + } +} + +void client(struct single_command (*commands)[512]) +{ + int client_socket,rc,len; + struct sockaddr_un server_sockaddr, client_sockaddr; + struct single_command a[512]; + struct single_command b[512]; + struct single_command *c1=(*commands); + struct single_command *c2=&(*commands)[1]; + memcpy(&a[0],c1,sizeof(struct single_command)); + memcpy(&b[0],c2,sizeof(struct single_command)); + pthread_t threadId; + if(pthread_create(&threadId,NULL,server,&b)<0) + { + perror("error \n"); + exit(0); + } + + if(!fork()) + { + client_socket=socket(AF_UNIX,SOCK_STREAM,0); + + if (client_socket == -1) + { + exit(1); + } + + client_sockaddr.sun_family = AF_UNIX; + len = sizeof(client_sockaddr); + unlink(CLIENT_PATH); + rc = bind(client_socket, (struct sockaddr *) &client_sockaddr, len); + if (rc == -1) + { + close(client_socket); + exit(1); + } + + memset(&server_sockaddr,0,sizeof(server_sockaddr)); + server_sockaddr.sun_family=AF_UNIX; + strcpy(server_sockaddr.sun_path,SOCK_PATH); + + while(connect(client_socket,(struct sockaddr*)&server_sockaddr,sizeof(server_sockaddr))==-1); + + if(!fork()) + { + close(0);close(1); + dup2(client_socket,1); + evaluate_command(1,&a); + pthread_exit(0);exit(0); + } + + wait(&status); + pthread_join(threadId,NULL); + close(client_socket); + exit(0); + } + wait(&status); +} + int evaluate_command(int n_commands, struct single_command (*commands)[512]) { - if (n_commands > 0) { + if(n_commands>1) + { + client(commands); + } + else if (n_commands ==1) + { struct single_command* com = (*commands); - assert(com->argc != 0); - int built_in_pos = is_built_in_command(com->argv[0]); - 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_do(com->argc, com->argv) != 0) { - fprintf(stderr, "%s: Error occurs\n", com->argv[0]); - } - } else { - fprintf(stderr, "%s: Invalid arguments\n", com->argv[0]); - return -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_do(com->argc, com->argv) != 0) + { + fprintf(stderr, "%s: Error \n", com->argv[0]); + } + } + else + { + fprintf(stderr, "%s: Error \n", com->argv[0]); + return -1; } - } else if (strcmp(com->argv[0], "") == 0) { + } + else if (strcmp(com->argv[0], "") == 0) + { return 0; - } else if (strcmp(com->argv[0], "exit") == 0) { + } + else if (strcmp(com->argv[0], "exit") == 0) + { return 1; } - else + else if (com->argc>0) + { + pid_t pid=fork(); + if(pid==0) { - - pid_t pid; - pid = fork(); - if(pid<0) - printf("FAIL \n"); - else if(pid==0) + execv(com->argv[0],com->argv); + for(int i=0;i<5;i++) { - if(strcmp(com->argv[0], "ls")==0 || strcmp(com->argv[0], "cat")==0) - { - char path[]= "/bin/"; - strcat(path, com->argv[0]); - com->argv[0]=path; - execv(com->argv[0], com->argv); - } - else if(strcmp(com->argv[0], "vim")==0) - { - char path[]="/usr/bin/"; - strcat(path, com->argv[0]); - com->argv[0]=path; - execv(com->argv[0], com->argv); - } - else - execv(com->argv[0], com->argv); + char *temp; + temp=strcat(path_1[i],"/"); + temp=strcat(temp,com->argv[0]); + execv(temp,com->argv); } - else - wait(&pid); + exit(1); } + else + { + waitpid(pid,0,0); + } + } + else + { + fprintf(stderr, "%s: Fail \n", com->argv[0]); + return -1; + } + } return 0; - } } void free_commands(int n_commands, struct single_command (*commands)[512]) { - for (int i = 0; i < n_commands; ++i) { + for (int i = 0; i < n_commands; ++i) + { struct single_command *com = (*commands) + i; int argc = com->argc; char** argv = com->argv; - for (int j = 0; j < argc; ++j) { + for (int j = 0; j < argc; ++j) + { free(argv[j]); } diff --git a/src/main.c b/src/main.c index 23cc7d10f3e98b73ab00ad521f647c1cc2f63c13..6b2e9da65410abb2db734456936632e94bff3386 100644 --- a/src/main.c +++ b/src/main.c @@ -1,36 +1,41 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> #include <signal.h> -#include "signal_handlers.h" #include "commands.h" #include "built_in.h" #include "utils.h" +#include "signal_handlers.h" + -int main() -{ - char buf[8096]; - catch_sigint(SIGINT); - catch_sigtstp(SIGTSTP); +int main(){ + char buf[8096]; + + while (1) { + fgets(buf, 8096, stdin); - while (1) { - fgets(buf, 8096, stdin); + signal(SIGINT, (void*) catch_sigint); + signal(SIGTSTP, (void*) catch_sigtstp); - struct single_command commands[512]; //argv - int n_commands = 0; - mysh_parse_command(buf, &n_commands, &commands); + struct single_command commands[512]; + int n_commands = 0; + mysh_parse_command(buf, &n_commands, &commands); - int ret = evaluate_command(n_commands, &commands); + int ret = evaluate_command(n_commands, &commands); - free_commands(n_commands, &commands); - n_commands = 0; //argc + free_commands(n_commands, &commands); + n_commands = 0; - if (ret == 1) { - break; + if (ret == 1) { + break; + } + memset(buf,0,8096); } - } - return 0; + return 0; } diff --git a/src/signal_handlers.c b/src/signal_handlers.c index 8e341220ffbf6587d1c53822f70afdd177ae2c86..60e8313e7dbb2633bd9e6905930ce6f7e29574ce 100644 --- a/src/signal_handlers.c +++ b/src/signal_handlers.c @@ -4,12 +4,12 @@ void catch_sigint(int signalNo) { - signal(signalNo, SIG_IGN); + signal(SIGINT, SIG_IGN); return ; } void catch_sigtstp(int signalNo) { - signal(signalNo, SIG_IGN); + signal(SIGTSTP, SIG_IGN); return ; } diff --git a/src/utils.c b/src/utils.c index bbe6ed098ec9a4b7e0548d84f5c3f49260d5a8a7..6fbc970d8bb0840c88240f73cdc330414d4cd680 100644 --- a/src/utils.c +++ b/src/utils.c @@ -45,7 +45,7 @@ void parse_single_command(const char* command, int ti = 0; while (tok != NULL) { - (*argv)[ti] = (char*)malloc(strlen(tok) + 1); + (*argv)[ti] = (char*)malloc(strlen(tok)); strcpy((*argv)[ti], tok); ++ti;