Esempio n. 1
0
/**
 * printInfo:
 * @pid: process id of the process whose information is to be printed
 *
 * The function prints the information available fot a certain process. The
 * information includes name, pid, status, virtual memory size, and executable
 * path (only for the shell)
 */
void
printInfo(int pid) {
    char statFile[DIRECTORY_LEN_MAX];
    char statInfo[INFO_LEN_MAX];
    char **info = NULL;
    sprintf(statFile, "/proc/%d/stat", pid != -1 ? pid: getpid());
    FILE *fp = fopen(statFile, "r");
    if (!fp) {
        perror("fopen() error");
        return;
    }
    fgets(statInfo, INFO_LEN_MAX, fp);
    info = parseCommand(statInfo, " ");
    printf("Process ID\t--\t%s\n"
           "Process Name\t--\t%s\n"
           "Process Status\t--\t%s\n"
           "Virtual Memory Size\t--\t%s\n",
           info[0],
           info[1],
           info[2],
           info[22]);
   if (pid == -1)
       printf("Executable Path\t--\t%s\n", EXECPATH);
   stringListFree(info);
}
Esempio n. 2
0
int main(int argc, char **argv) {
    char currdir[DIRECTORY_LEN_MAX];
    char homedir[DIRECTORY_LEN_MAX];
    char **commands = NULL;
    unsigned int cmdCount = 0;

    /* Current directory is home directory */
    if (getcwd(homedir, DIRECTORY_LEN_MAX) == NULL)
        perror("getcwd() error");
    HOME = homedir;
    CMDCOUNT = &cmdCount;
    EXECPATH = argv[0];

    /* Signal handlers */
    signal(SIGINT, signalCallbackHandler);
    signal(SIGTSTP, signalCallbackHandler);

    while (1) {
        /* Update current working directory */
        if (getcwd(currdir, DIRECTORY_LEN_MAX) == NULL)
            perror("getcwd() error");

        /* Clean all zombie processes */
        cleanZombies();

        /* Print the prompt */
        printPrompt(currdir);

        /* Do the Dew! */
        commands = processCommand(commands, &cmdCount);
    }
    stringListFree(commands);
    return 0;
}
Esempio n. 3
0
TA_RetCode TA_DirectoryFree( TA_Directory *directory )
{
   TA_RetCode retCode;
   TA_SystemGlobal *global;

   retCode = TA_GetGlobal(  &TA_SystemGlobalControl, (void **)&global );
   if( retCode != TA_SUCCESS )
      return retCode;

   if( directory )
   {
      stringListFree( global->filenameCache, directory->listOfFile );
      stringListFree( global->dirnameCache, directory->listOfDirectory );
      TA_Free(  directory );
   }

   return TA_SUCCESS;
}
Esempio n. 4
0
/**
 * runCommand:
 * @command: string holding the current command
 * @cmdCount: variable holding the current number of commands
 *
 * The function analyzes the command, categorizes as built-in, user-defined and
 * system. Performs the necessary task of creating child processes and executing
 * he command. Handles all the piping and redirection stuff.
 */
void
runCommand(char *command,
           unsigned int *cmdCount)
{
    pid_t pid;
    int status;
    size_t i, j;
    int procLoc = PROC_LOC_FG;
    int jobID = 0;
    char *commandCopy = strdup(command);
    char **cmd = parseCommand(command, " ");

    unsigned int cmdLength = stringListLength(cmd);
    if (!cmdLength) return;

    /* Is the process to be run in background? */
    for (i = 0; i < cmdLength; i++) {
        if (STREQ(cmd[i], "&")) {
            cmd[i] = NULL;
            procLoc = PROC_LOC_BG;
        }
    }
    /* Is it a built-in command? */
    if (STREQ(cmd[0], "cd")) {
        if (chdir(cmdLength > 1 && cmd[1] != NULL
                  ? cmd[1] : HOME) < 0)
            perror("chdir() error");
    }
    /* Is it a user-defined command? */
    else if (STREQ(cmd[0], "pinfo")) {
        if (cmdLength > 1)
            printInfo(atoi(cmd[1]));
        else
            printInfo(-1);
    }
    else if (STREQ(cmd[0], "jobs")) {
        printJobs();
    }
    else if (STREQ(cmd[0], "quit") || STREQ(cmd[0], "exit")) {
        killBGProcs();
        _exit(0);
    }
    else if (STREQ(cmd[0], "kjob")) {
        if (cmdLength != 3)
            printf("Correct syntax is: kjob <jobNumber> <signalNumber>\n");
        else
            killProc(atoi(cmd[1]), atoi(cmd[2]));
    }
    else if (STREQ(cmd[0], "overkill")) {
        killBGProcs();
    }
    else if (STREQ(cmd[0], "fg")) {
        if (cmdLength != 2)
            printf("Correct syntax is: fg <jobNumber>\n");
        else
            wakeUpBG(atoi(cmd[1]));
    }
    else if (cmd[0][0] == 0x0C) {
        printf("\033[2J\033[H");
    }

    else {
        /* Is it a system command? */
        if ((pid = fork()) < 0) {
            printf("ERROR: forking child process failed\n");
            _exit(1);
        }
        else if (pid == 0) {
            char **cmdChild = NULL;
            int redirection[REDIRECTION_MAX];
            int redirectCount = 0;
            unsigned int nCmds = 0;
            pid_t cpid;
            int fd[2];
            int tmp_fd;

            if (procLoc == PROC_LOC_BG)
                setpgid(0, 0);

            for (i = 0; i < strlen(commandCopy); i++) {
                switch(commandCopy[i]) {
                case '>': redirection[redirectCount++] = '>'; break;
                case '|': redirection[redirectCount++] = '|'; break;
                }
            }
            cmdChild = parseCommand(commandCopy, "|>");

            nCmds = stringListLength(cmdChild);
            if (nCmds > 2) {
                for (i = 0; i < nCmds; i++) {
                    if (pipe(fd) < 0)
                        perror("pipe error");
                    if ((cpid = fork()) < 0) {
                        perror("fork error");
                    } else if (cpid == 0) {
                        close(fd[0]);
                        char **tmpCmd = parseCommand(cmdChild[i], " ");
                        int tmpLen = stringListLength(tmpCmd);
                        for (j = 0; j < tmpLen; j++) {
                            if (STREQ(tmpCmd[j], "<")) {
                                tmpCmd[j] = tmpCmd[j + 1];
                                tmpCmd[j + 1] = NULL;
                                break;
                            } else if (STREQ(tmpCmd[j], "&"))
                                tmpCmd[j] = NULL;
                        }
                        if (redirection[i] == '>') {
                            char **path = parseCommand(cmdChild[i + 1], " ");
                            tmp_fd = open(path[0], O_CREAT | O_WRONLY | O_TRUNC, 0644);
                            dup2(tmp_fd, STDOUT_FILENO);
                            close(tmp_fd);
                        }
                        else if (i != (nCmds - 1))
                            dup2(fd[1], STDOUT_FILENO);

                        if ((redirection[i - 1] != '>') &&
                            (execvp(*tmpCmd, tmpCmd) < 0)){
                            printf("ERROR: %s: command not found\n", tmpCmd[0]);
                            _exit(1);
                        }

                        close(fd[1]);
                        _exit(0);
                    }
                    else {
                        close(fd[1]);
                        if (waitpid(cpid, NULL, 0) < 0)
                            perror("waitpid error");
                        if (i != (nCmds - 1))
                            dup2(fd[0], STDIN_FILENO);

                        close(fd[0]);
                    }
                }
            }
            else if (execvp(*cmd, cmd) < 0) {
                printf("ERROR: %s: command not found\n", cmd[0]);
                _exit(1);
            }
            _exit(0);
        }
        else{
            CURRPID = pid;
            jobID = JOBID++;
            PROCINFO = realloc(PROCINFO, sizeof(procInfo)*(jobID + 1));
            PROCINFO[jobID].cmd = commandCopy;
            PROCINFO[jobID].pid = pid;
            PROCINFO[jobID].location = procLoc;
            if(procLoc == PROC_LOC_FG) {
                waitpid(-1, &status, WUNTRACED);
            }
        }
    }
    stringListFree(cmd);
    cleanZombies();
}