kmain() { int i,num; /* CARGA DE IDT CON LA RUTINA DE ATENCION DE IRQ0 */ setup_IDT_entry (&idt[0x08], 0x08, (dword)&_int_08_hand, ACS_INT, 0); setup_IDT_entry (&idt[0x09], 0x08, (dword)&_int_09_hand, ACS_INT, 0); setup_IDT_entry (&idt[0x80], 0x08, (dword)&_int_80_hand, ACS_INT, 0); /* Carga de IDTR */ idtr.base = 0; idtr.base +=(dword) &idt; idtr.limit = sizeof(idt)-1; _lidt (&idtr); _Cli(); /* Habilito interrupcion de timer tick*/ //_mascaraPIC1(INT_08 & INT_09 & INT_80); _mascaraPIC1(0x00); _mascaraPIC2(NONE); _Sti(); initKeyBoard(); initVideo(); initShell(); doubleFlagsFix(1.1); while (1) { updateShell(); } }
/** @brief Runs the shell Using system processs and child process for calling various executable to mimic the shell */ void main() { // Compile al files initShell(); // Run the shell runMyshell(); }
void startShell() { initShell(); printIntro(); char initialInputChar = '\0'; while (initialInputChar != EOF) { promptPrefix(); initialInputChar = getchar(); // User hits enter if (initialInputChar == '\n') { continue; // End of file symbol reached } else if (initialInputChar == EOF) { break; // User is typing a command } else { // Adding the 'initialInputChar' as the first character of the command int inputCounter = 0; char shellInput[INPUT_SIZE]; shellInput[inputCounter++] = initialInputChar; // Waiting for more user input char tempChar; while (inputCounter < INPUT_SIZE) { tempChar = getchar(); // User has finished typing command if (tempChar == '\n') { // Properly ending the input shellInput[inputCounter] = '\0'; // Performing the command AppCommand appCommand = identifyCommand(shellInput); performCommand(appCommand, shellInput); break; // Forming command } else { shellInput[inputCounter++] = tempChar; } } // Freeing memory for the inputted string memset(shellInput, 0, INPUT_SIZE); } } }
int main(){ initShell(); initProcessList(); while(1){ prompt(); //substituir por variavel - para mostra o diretorio que esta newProcess = (Process_Object *)malloc(sizeof(Process_Object)); parser(); runProcess(newProcess); //resetAttributes(); } }
void initKernel(multiboot_info_t* multibootInfo, unsigned int magic) { asm("cli"); initArch(); initShell(); asm("sti"); if (magic != 0x2BADB002) { ttyPrint("Please use a Multiboot compliant bootloader to boot IsaacOS!\n", ttyRed); for (;;) asm("hlt"); } initKeyboardBuffer(); }
int main() { char cmd[MAX_BUF]; char **args; int socket; int i; /* allocate memory for command arguments */ args = (char **) calloc(MAX_ARGS, sizeof(char*)); for (i=0; i<MAX_ARGS; ++i) args[i] = (char *) calloc(MAX_BUF, sizeof(char)); /* initialize socket and connect to logger */ initShell(&socket); /* prompt user for shell command, fork another process to execute */ while (1) { printf("<cuShell> "); fgets(cmd, sizeof(cmd), stdin); cmd[strlen(cmd)-1] = 0; send(socket, cmd, strlen(cmd), 0); if (strcmp(cmd, "exit") == 0 || strcmp(cmd, "quit") == 0) break; int pid = fork(); if (pid != 0) { wait(NULL); } else { parse(cmd, args); execvp(args[0], args); exit(0); } } /* clean up */ for (i=0; i<MAX_ARGS; ++i) free(args[i]); free(args); close(socket); return 0; }
int main(void) { wl_err_t wl_status; int status; struct ctx_server *hs; enum wl_host_attention_mode mode; startup_init(); board_init(); led_init(); tc_init(); delay_init(FOSC0); #ifdef _TEST_SPI_ for (;;) { /* handle console input */ console_poll(); spi_poll(NULL); } #else printk("Arduino Wifi Startup... [%s]\n", timestamp); size_t size_ctx_server = sizeof(struct ctx_server); hs = calloc(1, size_ctx_server); ASSERT(hs, "out of memory"); size_t size_netif = sizeof(struct netif); hs->net_cfg.netif = calloc(1, size_netif); ASSERT(hs->net_cfg.netif, "out of memory"); hs->net_cfg.dhcp_enabled = INIT_IP_CONFIG; INFO_INIT("hs:%p size:0x%x netif:%p size:0x%x\n", hs, size_ctx_server, hs->net_cfg.netif, size_netif); initShell(hs); timer_init(NULL, NULL); lwip_init(); status = fw_download_init(); ASSERT(status == 0, "failed to prepare for firmware download\n"); wl_status = wl_transport_init(fw_read_cb, hs, &mode); if (wl_status != WL_SUCCESS) goto err; INFO_INIT("Mode: 0x%x\n", mode); wl_status = wl_init(hs, wl_init_complete_cb, mode); if (wl_status != WL_SUCCESS) goto err; /* start main loop */ for (;;) poll(hs); err: /* show error message on console and display if wlan initialization fails */ #define WL_CARD_FAILURE_STR "Could not detect wl device, aborting\n" #define WL_FIRMWARE_INVALID_STR "Invalid firmware data, aborting\n" #define WL_OTHER_FAILURE_STR "Failed to start wl initialization\n" switch (wl_status) { case WL_CARD_FAILURE: printk(WL_CARD_FAILURE_STR); break; case WL_FIRMWARE_INVALID: printk(WL_FIRMWARE_INVALID_STR); break; default: printk(WL_OTHER_FAILURE_STR); break; } for (;;) { timer_poll(); } #endif }
Initializer::Initializer(int& argc, char**& argv, ToolType tool) : argc_(&argc), argv_(&argv), tool_(tool), binary_((tool == ToolType::DAEMON) ? "osqueryd" : "osqueryi") { std::srand(static_cast<unsigned int>( chrono_clock::now().time_since_epoch().count())); // Initialize registries and plugins registryAndPluginInit(); // The 'main' thread is that which executes the initializer. kMainThreadId = std::this_thread::get_id(); // Set the tool type to allow runtime decisions based on daemon, shell, etc. kToolType = tool; // Handled boost filesystem locale problems fixes in 1.56. // See issue #1559 for the discussion and upstream boost patch. try { boost::filesystem::path::codecvt(); } catch (const std::runtime_error& /* e */) { #ifdef WIN32 setlocale(LC_ALL, "C"); #else setenv("LC_ALL", "C", 1); #endif } Flag::create("logtostderr", {"Log messages to stderr in addition to the logger plugin(s)", false, false, true, false}); Flag::create("stderrthreshold", {"Stderr log level threshold", false, false, true, false}); // osquery implements a custom help/usage output. for (int i = 1; i < *argc_; i++) { auto help = std::string((*argv_)[i]); if ((help == "--help" || help == "-help" || help == "--h" || help == "-h") && tool != ToolType::TEST) { printUsage(binary_, tool_); shutdown(); } } if (tool == ToolType::SHELL) { // The shell is transient, rewrite config-loaded paths. FLAGS_disable_logging = true; // The shell never will not fork a worker. FLAGS_disable_watchdog = true; FLAGS_disable_events = true; } bool default_flags = false; if (FLAGS_flagfile.empty() && isReadable(kDefaultFlagfile)) { // No flagfile was set (daemons and services always set a flagfile). default_flags = true; FLAGS_flagfile = kDefaultFlagfile; } // Set version string from CMake build GFLAGS_NAMESPACE::SetVersionString(kVersion.c_str()); // Let gflags parse the non-help options/flags. GFLAGS_NAMESPACE::ParseCommandLineFlags( argc_, argv_, (tool == ToolType::SHELL)); if (tool == ToolType::SHELL) { if (Flag::isDefault("database_path") && Flag::isDefault("disable_database")) { // The shell should not use a database by default, but should use the DB // specified by database_path if it is set FLAGS_disable_database = true; } // Initialize the shell after setting modified defaults and parsing flags. initShell(); } else { // The daemon will only output ERROR logs to stderr. if (Flag::isDefault("stderrthreshold")) { Flag::updateValue("stderrthreshold", "2"); } } #ifndef WIN32 // All tools handle the same set of signals. // If a daemon process is a watchdog the signal is passed to the worker, // unless the worker has not yet started. std::signal(SIGTERM, signalHandler); std::signal(SIGINT, signalHandler); std::signal(SIGHUP, signalHandler); std::signal(SIGALRM, signalHandler); std::signal(SIGCHLD, SIG_IGN); #endif std::signal(SIGABRT, signalHandler); std::signal(SIGUSR1, signalHandler); // If the caller is checking configuration, disable the watchdog/worker. if (FLAGS_config_check) { FLAGS_disable_watchdog = true; } // Initialize the status and results logger. initStatusLogger(binary_); if (tool != ToolType::EXTENSION) { if (isWorker()) { VLOG(1) << "osquery worker initialized [watcher=" << PlatformProcess::getLauncherProcess()->pid() << "]"; } else { VLOG(1) << "osquery initialized [version=" << kVersion << "]"; } } else { VLOG(1) << "osquery extension initialized [sdk=" << kSDKVersion << "]"; } if (default_flags) { VLOG(1) << "Using default flagfile: " << kDefaultFlagfile; } // Initialize the COM libs platformSetup(); }
int main(int argc, char *argv[]) { initShell(); //signal(SIGCHLD, SIG_IGN); //Declarations int bytes_read,no_of_tokens,no_of_commands=0,redirectFlag=0; char uname[80],homedir[256],input[1024],hostname[80],tempstr[256],cwd[256]; char *cmdline, *sentence, *line, *token,**savedTokens, **command, *cmd, *mcptr, *com; size_t length,homedirlen; pid_t childPid; //Change Shell home dir to ~ getcwd(homedir , sizeof(homedir) ); getusername(uname); homedirlen=strlen(homedir); strcpy(cwd,homedir); /* int ptr=0; for(ptr=0;ptr<=1000;ptr++) { jobs[ptr]=NULL; }*/ int exit_flag=0; while (1) { sigset_t mask, prevmask; //Initialize mask with just the SIGCHLD signal sigemptyset(&mask); sigaddset(&mask, SIGCHLD); sigprocmask(SIG_BLOCK, &mask, &prevmask); /*block SIGCHLD, get previous mask*/ no_of_tokens=0; command=malloc ( 200 * sizeof(char)); //Number of commands there can be will be stored in 2D Array cmdline = (char *) malloc (1025 * sizeof(char)); line = (char *) malloc (1025 * sizeof(char)); cmd = (char *) malloc (1025 * sizeof(char)); savedTokens=malloc ( 100 * sizeof(char)); //Number of tokens there can be strcpy(line,"\n"); gethostname(hostname, sizeof(hostname)); getcwd(cwd , sizeof(cwd) ); //printf("PRINT THIS : %s\n",cwd+homedirlen ); //printf("CWD: %d HOMEDIR: %d\n",strlen(cwd),strlen(homedir) ); if( strncmp( cwd, homedir, homedirlen-1) == 0) // && strncmp( cwd, homedir, homedirlen-1)!=0) //If the current working directory is not ~ { strcpy(tempstr,"~"); //printf("HOME DIR IS: %s\n",tempstr ); strcat(tempstr,cwd+homedirlen); strcpy(cwd, tempstr); } int jumper=setjmp(env); printf("<%s@%s:%s>",uname,hostname,cwd ); //PROMPT getline (&line, &length+1, stdin); //PARSING: //Stage 1: Handling multiple commands: int k=0; token = strtok (line, ";"); command[k]=token; while ( token!=NULL ) { command[k]=token; token = strtok (NULL,";"); k++; } no_of_commands=k-1; if(no_of_commands==-1) { printf("Exiting main shell!\n"); printf("\n"); return 0; } else if(command[no_of_commands]!=NULL) { int len=strlen(command[no_of_commands]); command[no_of_commands][len-1]=0; //Last token gets an extra \n .. therefore removed here. } //STAGE 2: for(k=0;k<=no_of_commands;k++) { /* sigset_t mask, prevmask; //Initialize mask with just the SIGCHLD signal sigemptyset(&mask); sigaddset(&mask, SIGCHLD); sigprocmask(SIG_BLOCK, &mask, &prevmask); /*block SIGCHLD, get previous mask*/ cmdline = command[k]; com = (char *) malloc (1025 * sizeof(char)); if(command[k]!=NULL) strcpy(com,command[k]); //com stores the whole command to be executed else com=NULL; //Stage 3: Piping int no_of_pipes=0; if(com!=NULL) { redirectFlag=isRedirect(com); no_of_pipes=isPipeJob(com); } if(no_of_pipes!=0) { int status; pid_t procid=fork(); if(procid==0) { executePipe(no_of_pipes,com,redirectFlag,homedir); } else { sigprocmask(SIG_SETMASK, &prevmask, NULL); //Unblocking wait(&status); } } else { int i=0; token = strtok(cmdline,">"); token = strtok(cmdline,"<"); token = strtok(cmdline," \t\n"); if(token==NULL) { no_of_commands=-1; } while(token != NULL) { savedTokens[i]=token; i++; token = strtok (NULL, " \t\n"); } if(i!=0) { no_of_tokens=i-1; cmd=savedTokens[0]; } else { no_of_tokens=0; cmd=NULL; } int len=0; if(savedTokens[no_of_tokens]!=NULL) { len=strlen(savedTokens[no_of_tokens]); } //savedTokens[no_of_tokens][len-1]=0; //Last token gets an extra \n .. therefore removed here. //if ((cmd!=NULL) && ((strcmp("exit",cmd)==0) || (strcmp("Exit",cmd)==0) || (strcmp("exit ",cmd)==0) || (strcmp("Exit ",cmd)==0))) if ((cmd!=NULL) && ((strcmp("quit",cmd)==0) || (strcmp("quit ",cmd)==0) || (strcmp(" quit",cmd)==0))) { //exit(1); exit_flag=1; break; } /*int j=0; while(j<=no_of_tokens) { printf("TOKEN %d: %s\n",j,savedTokens[j]); j++; } */ //record command in history list (GNU readline history ?) int std_out; if(no_of_commands!=-1) { if ( (cmd!=NULL) && isBuiltInCommand(cmd)==1 ) { if(redirectFlag!=0) { executeRedirect(cmd,com,redirectFlag); } if(outfile!=0) { int fd1; if(outfile==1) { fd1=open(output,O_CREAT|O_RDWR|O_TRUNC,00666); lseek(fd1, 0, SEEK_SET); } else if(outfile==2) { fd1=open(output,O_APPEND|O_CREAT|O_RDWR,00666); } if(fd1==-1) { fprintf(stderr, "Can't open file %s for output!\n",output); memset(output, 0, 10); outfile=0; continue; } std_out=dup(1); dup2(fd1,STDOUT_FILENO); close(fd1); memset(output, 0, 10); outfile=0; } if(infile==3) { int fd2; fd2=open(inputfile,O_RDWR,00666); if(fd2==-1) { fprintf(stderr, "Can't open file for input! 4\n"); memset(inputfile, 0, 10); infile=0; continue; } dup2(fd2,STDIN_FILENO); close(fd2); memset(inputfile, 0, 10); infile=0; } job_no++; addJob(0,cmd,0,job_no); executeBuiltInCommand(cmd,savedTokens,no_of_tokens,homedir); dup2(std_out,1); } else { if((com!=NULL) && isBackgroundJob(com)==1) { savedTokens[no_of_tokens]=NULL; } int status; childPid = fork(); switch (childPid) { case 0: //Child Process //setpgid(0,0); //make the current process the group leader //tcsetpgrp(0,getpid()); if(redirectFlag!=0) { executeRedirect(cmd,com,redirectFlag); } if(outfile!=0) { int fd1; if(outfile==1) { fd1=open(output,O_CREAT|O_RDWR|O_TRUNC,00666); lseek(fd1, 0, SEEK_SET); } else if(outfile==2) { fd1=open(output,O_APPEND|O_CREAT|O_RDWR,00666); } if(fd1==-1) { fprintf(stderr, "Can't open file for output 6!\n"); memset(output, 0, 10); outfile=0; continue; } dup2(fd1,STDOUT_FILENO); close(fd1); memset(output, 0, 10); outfile=0; } if(infile==3) { int fd2; printf("%s\n",inputfile); fd2=open(inputfile,O_RDWR,00666); if(fd2==-1) { fprintf(stderr, "Can't open file for input! 5\n"); memset(inputfile, 0, 10); infile=0; continue; } dup2(fd2,STDIN_FILENO); close(fd2); memset(inputfile, 0, 10); infile=0; } executeCommand(cmd,savedTokens,no_of_tokens); //calls execvp /* if exec returns there was an error. */ perror(savedTokens[0]); exit(-1); case -1: perror("Fork"); return -1; default: //In Parent sigprocmask(SIG_SETMASK, &prevmask, NULL); //Unblocking //handler(childPid,cmd,job_no,jobs); //Check if any of the childs exited if (isBackgroundJob(com)==1) { setpgid(childPid,childPid); //added the background process to its own group //tcsetpgrp(0,childPid); savedTokens[no_of_tokens]=NULL; // add pid to some list to track jobs job_no++; printf("[%d][proc %d started]\n",job_no, childPid); addJob(childPid,cmd,1,job_no); // sigprocmask(SIG_SETMASK, &prevmask, NULL); //Unblocking } else { //Add foreground jobs to list: job_no++; //printf("Parent: Here total jobs are %d \n",job_no ); addJob(childPid,cmd,0,job_no); curpid=childPid; // printf("jobs[%d]->cmd: %s\n",job_no,jobs[job_no]->cmd); sigprocmask(SIG_SETMASK, &prevmask, NULL); //Unblocking pid_t wpid; do { wpid = waitpid(childPid, &status, WUNTRACED); //WUNTRACED->status of child processes will be reported here! } while (!WIFEXITED(status) && !WIFSIGNALED(status)); //WIFEXITED reports normal termination and //WIFSIGNALED not 0 status if child process stopped but wasnt caught! removeJob(wpid); curpid=getpid(); //printf("I am removing the fg job with pid %d\n",wpid ); //waitpid (childPid); //printf("HERE! 2\n" ); } } } } } }//end of k loop if(exit_flag==1) break; //free(line); // } //End of while loop return 0; }