diff --git a/include/built_in.h b/include/built_in.h index b2df0aa845f50b0ba45508d0418a3fbe9643579c..27588e0490a89fcc7539eb95d51b4b5d2ee88c11 100644 --- a/include/built_in.h +++ b/include/built_in.h @@ -1,6 +1,9 @@ #ifndef BUILT_IN_H_ + #define BUILT_IN_H_ +int bgpid_num; +char running[1024]; typedef int (*built_in_command_do)(int, char**); typedef int (*built_in_command_validate)(int, char**); diff --git a/include/commands.c b/include/commands.c new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/built_in.c b/src/built_in.c index 9a466e91501d5877107fb67e308275c4847bcb38..cb8c540f2e1f86aec06686ae9736b434db9caf65 100644 --- a/src/built_in.c +++ b/src/built_in.c @@ -32,12 +32,16 @@ int do_pwd(int argc, char** argv) { return 0; } + int do_fg(int argc, char** argv) { if (!validate_fg_argv(argc, argv)) return -1; // TODO: Fill this. - + int state; + // printf("\nbgpid_num=%d\n",bgpid_num); + waitpid(bgpid_num,&state,0); + // printf("waitpid complete\n"); return 0; } diff --git a/src/commands.c b/src/commands.c index 13e9c330aedcb9d3c1c0979a5b22fd7844516ada..5347500c3e190791692f8566524a4f45e4d0baa2 100644 --- a/src/commands.c +++ b/src/commands.c @@ -2,16 +2,140 @@ #include <stdlib.h> #include <string.h> #include <assert.h> - +#include <stdio_ext.h> +#include <unistd.h> #include "commands.h" #include "built_in.h" +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <arpa/inet.h> +#include <pthread.h> +#include"built_in.h" + +#define BUFF_SIZE 1024 +#define SOCKET_PATH "tpf_unix_sock.server" +#define SERVER_PATH "tpf_unix_sock.server" +#define CLIENT_PATH "tpf_unix_sock.clent" + + 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 } + { "fg", do_fg, validate_fg_argv }, + }; + + +void * server(void*** argvp ){ + int server_socket, client_socket, client_addr_size; + char buf[256]; + char ***argv=*(char ***)argvp; + struct sockaddr_un server_addr; + struct sockaddr_un client_addr; + + memset(&server_addr,0, sizeof(struct sockaddr_un)); + memset(&client_addr, 0, sizeof(struct sockaddr_un)); + memset(buf, 0, 256); + + server_socket = socket(AF_UNIX, SOCK_STREAM, 0); + if(server_socket < 0) {printf("server socket fail\n"); + exit(0);} + + bzero(&server_addr, sizeof(server_addr)); + server_addr.sun_family = AF_UNIX; + strcpy(server_addr.sun_path, SOCKET_PATH); + int len = sizeof(server_addr); + unlink(SOCKET_PATH); +int state =bind(server_socket,(struct sockaddr*)&server_addr, len); + if(state ==-1) { + printf("bind error\n"); + exit(1); + } +state = listen(server_socket, 5); + if(state ==-1){ + printf("listen error\n"); + exit(1); + } +while(1){ + client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &len); + +if(client_socket == -1){ + printf("client connect fail\n"); + close(server_socket); + close(client_socket); + exit(1); + } + + +int pid; +if((pid=fork())<0) + perror("fork fail\n"); +else if (pid !=0) + sleep(1); +else { + + dup2(client_socket,0); + execv(argv[0][0],argv[0]); + } +} + + +close(client_socket); + +} + +void * client(void*** argvp){ + + int client_socket; + char ***argv=*(char ***)argvp; + struct sockaddr_un server_addr; + struct sockaddr_un client_addr; + char buf[256]; + + memset(&server_addr, 0, sizeof(struct sockaddr_un)); + memset(&client_addr,0, sizeof(struct sockaddr_un)); + + + client_socket = socket (AF_UNIX, SOCK_STREAM, 0); + if(client_socket == -1) + { + printf("client socket fail\n"); + exit(1); + } + bzero(&client_addr, sizeof(client_addr)); + client_addr.sun_family = AF_UNIX; + strcpy(client_addr.sun_path, CLIENT_PATH); + int len = sizeof(client_addr); + + server_addr.sun_family = AF_UNIX; + strcpy(server_addr.sun_path, SERVER_PATH); + + if(connect(client_socket, (struct sockaddr*)&server_addr, len) == -1) + { + printf("connect fail\n"); + exit(1); + } + + + + int pid; + if((pid = fork())<0) + perror("fork fail\n"); + else if (pid != 0) + sleep(1); + else { + dup2(client_socket,1); + close(client_socket); + execv(argv[0][0],argv[0]); + exit(1); + } + close(client_socket); + exit(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]); @@ -22,12 +146,9 @@ static int is_built_in_command(const char* command_name) } } - return -1; // Not found + return -1; } -/* - * 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]) { if (n_commands > 0) { @@ -49,12 +170,84 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512]) return 0; } else if (strcmp(com->argv[0], "exit") == 0) { return 1; - } else { - fprintf(stderr, "%s: command not found\n", com->argv[0]); - return -1; + } else if (strcmp(com->argv[0], "vim") == 0){ + for(int i=0;i<n_commands; i++){ + int pid, status; + if((pid = fork())<0) perror("Don't execute fork"); + else if (pid >0) wait(&status); + else { + char temp[512] = "/usr/bin/"; + strcat(temp, com->argv[0]); + strcpy(com->argv,temp); + bgpid_num=getpid(); + printf("\n%d\n",bgpid_num); + execv(com->argv[0], com->argv); + if(execv(com->argv[0], com->argv)) + { + fprintf(stderr, "%s: command not found\n", com->argv[0]); + return -1; + } + + } + } + return 0; + } else if(n_commands >=2){ + + int pid, status; + pthread_t pt[2]; + int a =5; + char ***pointer; + pointer=&(com->argv); + if((pid = fork())<0) + perror("fork fail\n"); + if(pid > 0){ + com = (*commands)+1; + pointer=&(com->argv); + pthread_create(&pt[0], NULL, &server, (void*)&pointer); + sleep(2); + } + else { + pthread_create(&pt[1], NULL, &client, (void*)&pointer); + sleep(2); + } + return 0; + } else { + + + for(int i=0;i<n_commands; i++){ + int status; + int pid; + + if((pid = fork())<0) perror("Don't execute fork"); + else if (pid>0){ + printf("%s\n",com->argv[com->argc-1]); + if(strcmp(com->argv[com->argc-1],"&")) + wait(&status); + } + else { + bgpid_num = getpid(); + printf("\n%d\n",bgpid_num); + execv(com->argv[0], com->argv); + + if(execv(com->argv[0], com->argv)) + { + char c[100] = "/bin/"; + strcat(c, com->argv[0]); + com->argv[0] = c; + execv(com->argv[0], com->argv); + if(execv(com->argv[0],com->argv)){ + fprintf(stderr, "%s: command not found\n", com->argv[0]); + return -1; + } + } + } + } + + } - } + + } return 0; } @@ -74,3 +267,4 @@ void free_commands(int n_commands, struct single_command (*commands)[512]) memset((*commands), 0, sizeof(struct single_command) * n_commands); } + diff --git a/src/main.c b/src/main.c index 77c78049273e013e278ad9021fa088531b636a97..8116d8f30a095287fba326bb614c21a31a0924c7 100644 --- a/src/main.c +++ b/src/main.c @@ -5,27 +5,58 @@ #include "commands.h" #include "built_in.h" #include "utils.h" - +#include "signal_handlers.h" +#include <signal.h> int main() { - char buf[8096]; +/* +signal(SIGINT,(void*)catch_sigint); +signal(SIGTSTP,(void*)catch_sigtstp); +*/ + char buf[8096]; while (1) { + signal(SIGINT, &catch_sigint); + signal(SIGTSTP, &catch_sigtstp); fgets(buf, 8096, stdin); - struct single_command commands[512]; int n_commands = 0; + int background = 0; + if(buf[strlen(buf)-1]=='&'){ + background = 1; + buf[strlen(buf)]=0; + memset(running,NULL,1024); + strcpy(running,buf); + break; + } mysh_parse_command(buf, &n_commands, &commands); - + if(background){ + bgpid_num = fork(); + if(bgpid_num){ + free_commands(n_commands, &commands); + n_commands =0; + continue; + } + } int ret = evaluate_command(n_commands, &commands); - free_commands(n_commands, &commands); n_commands = 0; + /* if(bgpid_num == 0){ + if(ret == 0){ + fprintf(stdout,"%d done %s\n",getpid(),running); + } + free_commands(n_commands,&commands); + n_commands = 0; + exit(0); + }*/ + free_commands(n_commands, &commands); + n_commands = 0; + if (ret == 1) { break; - } + } + memset(buf,NULL,8096); } - return 0; } diff --git a/src/signal_handlers.c b/src/signal_handlers.c index 4b6fe2e073f327917964b5327b6649f74bcbda1e..1968cf5cda8174fbc95d82b002eaa554b8ebe85a 100644 --- a/src/signal_handlers.c +++ b/src/signal_handlers.c @@ -1,11 +1,27 @@ + #include "signal_handlers.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/wait.h> +#include <signal.h> void catch_sigint(int signalNo) -{ +{ + signal(SIGINT, &catch_sigint); + printf("\nShell doesn't close!\n"); + + // TODO: File this! } void catch_sigtstp(int signalNo) -{ +{ + signal(SIGTSTP, &catch_sigtstp); + printf("\nIt doesn't move!\n"); // TODO: File this! } + + + diff --git a/test b/test new file mode 100755 index 0000000000000000000000000000000000000000..d0f40c3cf28498811ed4a1c215e1f9c6258d1f01 Binary files /dev/null and b/test differ diff --git a/test.c b/test.c new file mode 100644 index 0000000000000000000000000000000000000000..2459f924782e639d260bdba39af352e1e813fd01 --- /dev/null +++ b/test.c @@ -0,0 +1,12 @@ +#include<string.h> +#include<stdio.h> +#include<unistd.h> + +void main(){ + + while(1){ + printf("background\n"); + sleep(5); + } +} +