Пример #1
0
//
// ACE_TMAIN
//
int ACE_TMAIN (int argc, char * argv [])
{
  int retval;

  u_long default_mask =
    LM_EMERGENCY | LM_ALERT | LM_CRITICAL | LM_ERROR | LM_WARNING | LM_NOTICE;

  ACE_Log_Msg::instance ()->priority_mask (default_mask, ACE_Log_Msg::PROCESS);

  try
  {
    // Register the signal handler.
    register_sighandler ();

    // Run the main part of the application.
    retval = LOGGING_SERVER->run_main (argc, argv);
  }
  catch (...)
  {
    ACE_ERROR ((LM_ERROR, "%T - [%M] - caught unknown exception\n"));
    retval = 1;
  }

  // Destroy the server's resources.
  LOGGING_SERVER->destroy ();

  return retval;
}
Пример #2
0
//
// ACE_TMAIN
//
int ACE_TMAIN (int argc, char * argv [])
{
  int retval = 0;

  // Get the current priority levels.
  u_long prio = LM_ALERT | LM_CRITICAL | LM_EMERGENCY | LM_ERROR | LM_WARNING;
  ACE_Log_Msg::instance ()->priority_mask (prio, ACE_Log_Msg::PROCESS);

  // Register the signal handler.
  register_sighandler ();

  try
  {
    retval = OASIS_SYSTEM_PROBE_DAEMON->run_main (argc, argv);
  }
  catch (const std::exception & ex)
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("%T (%t) - %M - %s\n"),
                ex.what ()));

    retval = 1;
  }

  return retval;
}
Пример #3
0
//
// main
//
int ACE_TMAIN (int argc, ACE_TCHAR * argv [])
{
  CUTS_TEST_TRACE ("ACE_TMAIN (int, ACE_TCHAR * [])");

  try
  {
    // Register the signal handler.
    register_sighandler ();

    // Run the main application.
    return TESTING_APP->run_main (argc, argv);
  }
  catch (...)
  {
    ACE_ERROR ((LM_ERROR,
                "%T (%t) - %M - caught unknown exception\n"));
  }

  return 1;
}
Пример #4
0
/* main 
 *
 * main handles the shell program flow and returns 0 on exit.
 */
int main () {
    pid_t child_pid;    /* Keep track of child process while forking. */
    long long timediff; /* Helps us time processes. */
    int FOREGROUND;     /* Foreground or background process branch? */
    char command[MAX_COMMAND_SIZE];         /* User-specified command. */
    char * command_args[MAX_COMMAND_SIZE];  /* Command arguments. */
    char * pchr;            /* char pointer used for string parsing. */
    int param_index = 0;    /* Holds which parameter we're at while parsing,
                               and after parsing is done equals the number of
                               parameters. */
    int child_status;   /* Keep track of child process status. */
    struct timeval time1,time2;             /* Structs used to track time. */
    int return_value;   /* Temp variable to save return values of system calls. */
    char * return_value_ptr; /* Temp variable to save return value from fgets. */
    
    /* 
     * Ignore sig-interrupt (Ctrl-C) in the parent (this) process.
     */
    signal(SIGINT,SIG_IGN);
    
    /* 
     * Install handler for SIGCHLD if needed.
     */
#if SIGNALDETECTION
    register_sighandler(SIGCHLD, sigchld_handler);
#endif

    /* main program loop */
    for (;;) {
        
        /* get command string */
        return_value_ptr = fgets(command,MAX_COMMAND_SIZE,stdin);

        /* if EINTR, no nothing */
        if (return_value_ptr == NULL && errno == EINTR)
            continue;

        /* empty line (only newline), do nothing */
        if (strlen(command) == 1) 
            continue;

        /* if last char is '&', set FOREGROUND to 0, otherwise set it to 1 */
        if (strlen(command) >= 2 && command[strlen(command)-2] == '&') {
            /* replace '&' with '\0' */
            command[strlen(command)-2] = '\0';
            FOREGROUND = 0;
        } else {
            FOREGROUND = 1;
        }

        /* replace newline character with null terminator */
        command[strlen(command)-1] = '\0';

        /* check for exit command */
        if (!strcmp(command,"exit"))
            break;

        /* split command string at spaces */
        pchr = strtok(command," ");

        /* first parameter is always the program itself */
        param_index = 0;
        command_args[param_index] = pchr;
        ++param_index;
        
        /* go through and save parameters into string array */
        while (pchr != NULL) {
            pchr = strtok(NULL," ");
            command_args[param_index] = pchr;
            ++param_index;
        }
        
        /* check for cd command */
        if (!strncmp(command,"cd",2)) {
            /* cd always takes 3 parameters ("cd cd example/directory/") */
            if (param_index != 3) {
                printf("==> ERROR: Invalid argument count to cd!\n");
                continue;
            }

            /* check the return value from chdir */
            return_value = chdir(command_args[1]);
            if (return_value == -1) {

                /* the directory is not valid */
                if (ENOTDIR) {
                    printf("==> ERROR: Invalid directory, sending you home...\n");
                    chdir(getenv("HOME"));

                /* another error, unknown... */
                } else {
                    printf("==> ERROR: Couldn't change directory to %s\n",command_args[1]);
                }
            } 

            /* don't run rest of loop */
            continue;
        } 

        /* check no. of arguments */
        if (param_index > MAX_NO_ARGS)
            fprintf(stderr,"Error: Too many arguments!");

        /* executed for foreground process execution */
        if (FOREGROUND) {
            child_pid = fork();
            if (child_pid == 0) {
                /* unignore sig-interrupt in the child process (Ctrl-C) */
                signal(SIGINT,SIG_DFL);

                /* execute command with specified arguments */
                (void)execvp(command,command_args);
                
                printf("==> ERROR: Could not execute command: %s\n",command);
                exit(1);
            } else if (child_pid == -1) {
                /* couldn't fork, something is wrong */
                fprintf(stderr,"==> ERROR: Couldn't fork!"); 
                exit(1);
            } 
            
            /* this is executed by the parent process, we want to wait for
             * the child process to finish execution, and wait for its finished
             * execution */

            /* get first time measurement */
            gettimeofday(&time1,NULL);

            printf("==> %d - spawned foreground process\n",(int)child_pid);

            /* wait for child to finish */
            child_pid = waitpid(child_pid,&child_status,0);

            /* get second time measurement */
            gettimeofday(&time2,NULL);

            /* calculate time difference in microseconds */
            timediff = ((time2.tv_sec*1e6+time2.tv_usec)-(time1.tv_sec*1e6+time1.tv_usec));

            print_exit_msg(child_pid);

            printf("==> execution time: %lf seconds\n",timediff/1e6);

        } else { /* background process execution */
            child_pid = fork();
            if (child_pid == 0) {
                /* unignore sig-interrupt in child (Ctrl-C) */
                signal(SIGINT,SIG_DFL);
                
                /* execute command with specified arguments */
                (void)execvp(command,command_args);
                
                printf("==> ERROR: Could not execute command: %s\n",command);
                exit(1);
            } else if (child_pid == -1) {
                /* unknown forking error, shouldn't happen */
                fprintf(stderr,"==> ERROR: Couldn't fork!"); exit(1);
            } 
            
            /* this is executed by the parent process */
            printf("==> %d - spawned background process\n",(int)child_pid);

        }

        /*
         * Perform polling of child process state changes to detect if any such
         * has terminated. Only performed if signal detection is not activated.
         */
#if !SIGNALDETECTION
        for (;;) {
            /* Asynchronously check if any process has terminated. */
            child_pid = waitpid(-1,&child_status,WNOHANG);            

            /* if nothing has changed, or if error, break the loop */
            if (child_pid == 0 || child_pid == -1) {
                break;
            }

            /* here, a process has terminated */
            print_exit_msg(child_pid);
        }
#endif

    }
    return 0;
}
Пример #5
0
int main(int argc, const char *argv[]) {
    char linebuf[LIMIT+1];
    char *read;
    char **tokens;
    int len, bg;

    #if SIGDET == 0
    /*pid_t pid;
    int status;*/
    #endif

    register_sighandler(SIGINT, parent_sigint);
    register_sighandler(SIGTSTP, parent_sigtstp);

    /* SIGDET = 1 means that the child is responsible for reporting to
      its parent when it's done so we register a sighandler for that */
    #if SIGDET == 1
    register_sighandler(SIGCHLD, parent_sigchld);
    #endif

    while (TRUE) {
        read = NULL;
        tokens = 0;
        bg = FALSE;

        #if SIGDET == 0
        poll_childs();
        #endif

        type_prompt();
        read = fgets(linebuf, LIMIT, stdin);
        if (read != linebuf) {
            /* end of file - quit as with exit command */
            if (feof(stdin)) {
                printf("^D\n");
                strcpy(linebuf, "exit\n");
            } else {
                /* some interrupt, proceed to next read */
                continue;
            }
        }

        /* trim whitespace right (in order to read flag later) */
        len = strlen(linebuf);
        while ((len > 0) &&
                (linebuf[len-1] == '\n' || linebuf[len-1] == ' ')) {
            linebuf[len-1] = '\0';
            len -= 1;
        }

        /* check for background flag */
        if (len > 0 && linebuf[len-1] == '&') {
            bg = TRUE;
            linebuf[len-1] = '\0';
            len -= 1;
        }

        tokens = tokenize(linebuf, '|');
        exec_command(tokens, bg);
    }
    return 0;
}
Пример #6
0
/*
 * Interpret the user input as either
 * a shell command or a program to be run
 */
void exec_command(char **tokens, int bg) {
    char *path, *first, **firstparsed, **checkenv, *pager;
    char *pathenv, *pathcp, **pathtokens;
    char *tmp_str;
    int i;
    pid_t parent;

    /* copy the first command string and handle
     * this case specially - it might be a shell command */
    first = malloc(sizeof(char)*strlen(tokens[0])+1);
    strcpy(first, tokens[0]);
    firstparsed = tokenize(first, ' ');

    if (strcmp(firstparsed[0], "") == 0) {
        free(first);
        free(firstparsed);
        free(tokens);
        return;
    }

    if (strcmp(firstparsed[0], "exit") == 0) {
        free(first);
        free(firstparsed);
        free(tokens);
        /* try to terminate all childs before exiting */

        /* send term to whole group, but ignore in parent */
        register_sighandler(SIGTERM, SIG_IGN);
        parent = getpid();
        sighold(SIGCHLD);

        /* give processes a chance to finish */
        if (kill(-parent, SIGTERM) == -1) {
            printf("Error: %s\n", strerror(errno));
            exit(1);
        }
        sleep(1);

        poll_childs();
        sigrelse(SIGCHLD);

        printf("Bye!\n");
        exit(0);
    }

    if (strcmp(firstparsed[0], "cd") == 0) {
        if (firstparsed[1] == '\0') {
            /* default dir to change to */
            path = getenv("HOME");
        } else {
            path = firstparsed[1];
        }

        if (DEBUG) {
            printf("Change Directory to %s\n", path);
        }

        if (chdir(path) == -1) {
            fprintf(stderr, "Error: %s\n", strerror(errno));
        }

        free(first);
        free(firstparsed);
        free(tokens);
        return;
    }

    if (strcmp(firstparsed[0], "checkEnv") == 0) {
        pager = getenv("PAGER");

        if (pager == NULL) {
            pathenv = getenv("PATH");
            pathcp = malloc(strlen(pathenv)+1);
            strcpy(pathcp, pathenv);
            pathtokens = tokenize(pathcp, ':');

            for (i = 0; i < tokens_length(pathtokens); i++) {
                if((tmp_str = malloc(strlen(pathtokens[i])+strlen("/less")+1)) != NULL){
                    tmp_str[0] = '\0';
                    strcat(tmp_str,pathtokens[i]);
                    strcat(tmp_str,"/less");

                    if (access(tmp_str, X_OK) != -1) {
                        pager = "less";
                        free(tmp_str);
                        break;
                    }
                    free(tmp_str);
                }
            }

            if (pager == NULL) pager = "more";
            free(pathcp);
            free(pathtokens);
        }

        if (DEBUG) {
            printf("Pager: %s\n", pager);
        }

        /* As our fork_and_run function takes an array of commands, we prepare an
          array with these commands in the checkenv array */

        /* tmp_str will become "grep <arguments>" if any arguments are passed to
          checkEnv */
        tmp_str = malloc(sizeof(char*) * (LIMIT + 1));

        if (tokens_length(firstparsed) > 1) {
            checkenv = malloc(sizeof(char*) * (4 + 1));

            tmp_str[0] = '\0';
            strcat(tmp_str, "grep ");
            for (i = 1; firstparsed[i] != NULL; i++) {
                strcat(tmp_str, firstparsed[i]);
                strcat(tmp_str, " ");
            }
            checkenv[0] = "printenv";
            checkenv[1] = tmp_str;
            checkenv[2] = "sort";
            checkenv[3] = pager;
            checkenv[4] = NULL;
        } else {
            checkenv = malloc(sizeof(char*) * (3 + 1));
            checkenv[0] = "printenv";
            checkenv[1] = "sort";
            checkenv[2] = pager;
            checkenv[3] = NULL;
        }

        fork_and_run(checkenv, bg);

        free(tmp_str);
        free(checkenv);
        free(first);
        free(firstparsed);
        free(tokens);
        return;
    }

    /* what was wrote wasn't "cd", "exit" or "checkEnv". We still execute
      the command. */
    free(first);
    free(firstparsed);
    fork_and_run(tokens, bg);
    free(tokens);
}