/** * main replayer method */ static void start(int option, int argc, char* argv[], char** envp) { pid_t pid; int status, fake_argc; if (option == RECORD) { copy_executable(argv[2]); if (access(__executable, X_OK)) { printf("The specified file '%s' does not exist or is not executable\n", __executable); return; } /* create directory for trace files */ setup_trace_dir(0); /* initialize trace files */ open_trace_files(); init_trace_files(); copy_argv(argc, argv); copy_envp(envp); record_argv_envp(argc, __argv, __envp); close_trace_files(); pid = sys_fork(); /* child process */ if (pid == 0) { sys_start_trace(__executable, __argv, __envp); /* parent process */ } else { child = pid; /* make sure that the child process dies when the master process gets interrupted */ install_signal_handler(); /* sync with the child process */ sys_waitpid(pid, &status); /* configure the child process to get a message upon a thread start, fork(), etc. */ sys_ptrace_setup(pid); /* initialize stuff */ init_libpfm(); /* initialize the trace file here -- we need to record argc and envp */ open_trace_files(); /* register thread at the scheduler and start the HPC */ rec_sched_register_thread(0, pid); /* perform the action recording */ fprintf(stderr, "start recording...\n"); start_recording(); fprintf(stderr, "done recording -- cleaning up\n"); /* cleanup all initialized data-structures */ close_trace_files(); close_libpfm(); } /* replayer code comes here */ } else if (option == REPLAY) { init_environment(argv[2], &fake_argc, __argv, __envp); copy_executable(__argv[0]); if (access(__executable, X_OK)) { printf("The specified file '%s' does not exist or is not executable\n", __executable); return; } pid = sys_fork(); //child process if (pid == 0) { sys_start_trace(__executable, __argv, __envp); /* parent process */ } else { child = pid; /* make sure that the child process dies when the master process gets interrupted */ install_signal_handler(); sys_waitpid(pid, &status); sys_ptrace_setup(pid); /* initialize stuff */ init_libpfm(); rep_sched_init(); /* sets the file pointer to the first trace entry */ read_trace_init(argv[2]); pid_t rec_main_thread = get_recorded_main_thread(); rep_sched_register_thread(pid, rec_main_thread); /* main loop */ replay(); /* thread wants to exit*/ close_libpfm(); read_trace_close(); rep_sched_close(); } } }
#include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include "my_functions.h" #define SUCESSFUL 0 #define FAILURE 1 int main (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], char *envp[]){ char **envp_c, **alias, *line, **args, **semicolon, **d_ampersand, **d_or; int status, i, j, k; envp_c = copy_envp(envp); alias = NULL; do{ print_char('$'); print_char(' '); line = read_line(0); semicolon = string_split(line, ';'); free(line); for (i = 0; semicolon[i]; i++){ d_ampersand = string_split(semicolon[i], '&'); for (status = 0, j = 0; d_ampersand[j]; j++){ if (status == 0){ d_or = string_split(d_ampersand[j], '|'); for (status = 1, k = 0; d_or[k]; k++){ if (status != 0){ args = string_split(d_or[k], ' '); status = shell_execute(args, &envp_c,d_or,d_ampersand, semicolon);
#include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include "functions.h" int main (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], char *envp[]){ char **envp_c, *line, **args, **semicolon, **d_ampersand, **d_or; int status, last_status, i, j, k; last_status=1; /*stores last status*/ envp_c=copy_envp(envp); /*copy env variables*/ do{ print_char('$'); print_char(' '); line=read_line(0); /*read line*/ semicolon=string_split(line, ';'); free(line); for (i=0;semicolon[i];i++){ /*separate semicolon statements*/ d_ampersand=string_split(semicolon[i], '&'); /*separate && statements*/ for (status=0, j=0;d_ampersand[j];j++){ if (status==0){ d_or=string_split(d_ampersand[j], '|'); /*separate || statements*/ for (status=1, k=0;d_or[k];k++){ if (status!=0){ args=string_split(d_or[k], ' '); status=shell_execute(args, &envp_c,d_or, d_ampersand, semicolon, last_status); last_status=status;
int main(int argc, char*argv[], char*envp[]){ char next_char; int i, fd; copy_envp(envp); char changepath[1024]; head = (struct node *) malloc( sizeof(struct node) ); head->next = NULL; head->val = 0; struct node *ptr = (struct node *)malloc(sizeof(struct node *)); ptr->val=0; ptr->next = NULL; head = current = ptr; /** clears screen for RSI **/ if(fork() == 0) { execvp("clear", argv); exit(1); } else { wait(NULL); } /** Prints prompt for first time **/ strcpy(cwd,""); if (getcwd(cwd, sizeof(cwd)) == NULL){ perror("getcwd() error"); } printf("RSI: %s > ", cwd); fflush(stdout); char word[24] = "\0"; char* args[256]; int m = 0; int child_pid; int h; char command[24]; static char arg_buffer[256] = "\0"; /* while user has not pushed ctrl-d */ while(next_char != EOF){ /* gets next char typed in cmd line, adds to words. */ /* puts the words (arguments) into argv *************/ next_char=getchar(); if(isspace(next_char) == 0){ strncat(word, &next_char, 1); } else{ strcpy(command, word); args[m] = (char *)malloc(sizeof(char) * 100); strcpy(args[m], command); strcpy(word, ""); m++; } /* if the line is done, ...*/ if(next_char == '\n'){ args[m] = (char *)malloc(sizeof(char) * 2); args[m] = NULL; int r = 0; int j = 0; int wpid; /* if statements check if cmd is bg, bglist, or cd. */ /* if not, it executes the command, waits for the child to complete, then prompts again */ if (strcmp(args[0], "cd" ) == 0){ change_directory(args, changepath, envp); } else if(strcmp(args[0], "\0")==0){ printf("\nNo Command Entered.\n"); } else if(strcmp(args[0], "bglist") == 0){ bglist(); } /** if it's a bg process, execute process, add info to list, then continue. **/ else if(strcmp(args[0], "bg") == 0){ for(r = 0; r<m-1; r++){ bzero(args[r], sizeof(args[r])); strcpy(args[r],args[r+1]); } bzero(args[m-1], sizeof(args[m-1])); args[m-1] = NULL; if ((child_pid=fork()) == 0) { h = execvp(args[0], args); printf("errno is %d\n", errno); } else { int c = 1; /* combine separate args to single string for node and future printout at bglist */ for(c=1; c<m-1; c++){ strcat(arg_buffer, args[c]); strcat(arg_buffer, " "); } add_to_list(child_pid, args[0], arg_buffer); bzero(arg_buffer, 256); } } /* execute process, then wait until process has finished to continue */ else { pid_t num; if ((num = fork()) == 0){ h = execvp(args[0], args); printf("errno is %d\n", errno); } else { waitpid(num, NULL, 0); } } m = 0; /* gets cwd and checks for finished processes. If there are, remove them from the list of bg processes. */ strcpy(cwd,""); if (getcwd(cwd, sizeof(cwd)) == NULL){ perror("getcwd() error"); } int status; int pid; /* waitpid() returns a PID on success*/ for(;;){ pid = waitpid(-1, &status, WNOHANG); if(pid <= 0){ break; } printf("[proc %d exited with code %d]\n", pid, WEXITSTATUS(status)); delete_from_list(pid); } /* prints prompt */ printf("\nRSI: %s > ", cwd); fflush(stdout); } } }