Example #1
0
void
get_ps_data(void)
{
    ps_ldata_t *ps_last = PS_LNULL;
    ps_ldata_t *ps_head = PS_LNULL;
    ps_ldata_t *psp;
    ps_data_t  *pstp;
    static char *usrname;
    int i = 0;
    DIR *dirp;
    struct dirent *dentp;
    char pname[MAXNAMELEN];
    int pdlen;
    char *gettty();

    if (pstable != PS_NULL) {  /* Don't run ps unless we need to */
        if ((cache_now - ps_cache_time) <= cache_lifetime)
            return;
        free(pstable);
    }

    pstable_lines = 0;
    ps_cache_time = cache_now;
    /*
     * Determine root path for remote machine.
     */
    if (!readata()) {        /* get data from psfile */
        call_ftw_for_dev();
        wrdata();
    }

    /*
     * Determine which processes to print info about by searching
     * the /proc directory and looking at each process.
     */
    if ((dirp = opendir(procdir)) == NULL) {
        (void) SYSLOG0("Cannot open PROC directory\n");
        return;
    }

    (void) strcpy(pname, procdir);
    pdlen = strlen(pname);
    pname[pdlen++] = '/';

    /* for each active process --- */
    while (dentp = readdir(dirp)) {
        int procfd;

        if (dentp->d_name[0] == '.')                /* skip . and .. */
                continue;
        (void) strcpy(pname + pdlen, dentp->d_name);
retry:
        if ((procfd = open(pname, O_RDONLY)) == -1)
                continue;

        /*
         * Get the info structure for the process and close quickly.
         */
        if (ioctl(procfd, PIOCPSINFO, (char *)&info) == -1) {
            int saverr = errno;

            (void) close(procfd);
            if (saverr == EAGAIN)
                goto retry;
            if (saverr != ENOENT)
                (void) SYSLOG2("PIOCPSINFO on %s: %s\n",
                               pname, strerror(saverr));
            continue;
        }
        (void) close(procfd);
        if ((psp = (ps_ldata_t *)malloc(sizeof (ps_ldata_t))) == PS_LNULL)
            break;
        memset((char *)psp, 0, sizeof (ps_ldata_t));
        psp->pdata.uid = info.pr_uid;
        psp->pdata.pid = info.pr_pid;
        psp->pdata.ppid = info.pr_ppid;
        psp->pdata.sz = info.pr_size;
        if (info.pr_wchan)
            sprintf(psp->pdata.wchan, "%9x", info.pr_wchan);
        else
            strcpy(psp->pdata.wchan, "         ");
        memset(&psp->pdata.stat[0], 0, STAT_SZ+1);
        if (info.pr_sname)
            psp->pdata.stat[0] = info.pr_sname;
        i = 0;
        strcpy(psp->pdata.tty, (char *)gettty(&i));
        psp->pdata.cpu = info.pr_time.tv_sec;
        strcpy(psp->pdata.cmd, info.pr_fname);

        if ((usrname = (get_usr_name(psp->pdata.uid))) != NULL)
            strncpy(psp->pdata.usrname, usrname, USRNM_SZ);
        else {
            free(psp);
            continue;
        }

        psp->pdata.usrname[USRNM_SZ] = '\0';
        pstable_lines++;
        if (ps_last == PS_LNULL)
            ps_head = psp;
        else
            ps_last->link = psp;
        ps_last = psp;
    }

    (void) closedir(dirp);
    if ((pstable = (ps_data_t *)malloc(pstable_lines
                    * sizeof (ps_data_t))) == PS_NULL) {
        clean_ps(ps_head);
        return;
    }
    for (pstp = pstable, psp = ps_head; psp != PS_LNULL;
                                    pstp++, psp = psp->link) {
        memcpy((char *)pstp, (char *)&(psp->pdata), sizeof (ps_data_t));
    }
    clean_ps(ps_head);
    qsort(pstable, pstable_lines, sizeof (ps_data_t), (int (*)())pscomp);
}
Example #2
0
void statement(unsigned long long fsys)
{
    long i,cx1,cx2;

    switch(sym)
    {
        case ident:              // 以标识符开始,则为赋值语句
        {
            i=position(id);
            if(i==0) error(11);
            else if(  table[i].kind!=integer&&table[i].kind!=real&&
            table[i].kind!=boolean)// 非变量 
            {
                error(12);
                i=0;
            }

            getsym();

            if(sym==becomes) getsym();
            else error(13);

            simpexpression(fsys);

            if(i!=0)
            {
                if (table[i].kind==boolean) gen(opr,0,21);
                                 // 对boolean变量进行数值整理
                gen(sto,lev-table[i].level,table[i].addr);
            }
        } break;
        case callsym:            // 调用语句
        {
            getsym();
            if(sym!=ident)
            {
                error(14);
            }
            else
            {
                i=position(id);
                if(i==0)
                {
                    error(11);
                }
                else if(table[i].kind==proc)
                {
                    gen(cal,lev-table[i].level,table[i].addr);
                }
                else
                {
                    error(15);
                }

                getsym();
            }
        } break;
        case ifsym:              // if语句
        {
            getsym();
            expression(fsys|thensym|dosym);

            if(sym==thensym)
            {
                getsym();
            }
            else
            {
                error(16);
            }
            cx1=cx;              // 记录下跳转代码的位置,此时跳转地址是0
            gen(jpc,0,0);
            statement(fsys|elsesym);// 紧接着可能是else
            code[cx1].a=cx;      // 把跳转地址加上

            if (sym==elsesym)    // else子句
            {
                cx1=cx;          // 记录下跳转代码的位置
                gen(jmp,0,0);    // 如果有else,那么then后面部分执行完毕后要跳过这段
                getsym();
                statement(fsys);
                code[cx1].a=cx;
            }
        } break;
        case beginsym:           // begin语句
        {
            getsym();
            statement(fsys|semicolon|endsym);
            while(sym==semicolon||(sym&statbegsys))
            {
                if(sym==semicolon)
                {
                    getsym();
                }
                else
                {
                    error(10);
                }
                statement(fsys|semicolon|endsym);
            }
            if(sym==endsym) getsym();
            else error(17);
        } break;
        case whilesym:           // while语句
        {
            cx1=cx; getsym();
            expression(fsys|dosym);

            cx2=elx;                 // 记录下当前层开始的elx
            exitlist[elx]=cx;        // 将jpc的位置记录进exitlist
            elx++;
            
            gen(jpc,0,0);
            if(sym==dosym)
            {
                getsym();
            }
            else
            {
                error(18);
            }

            statement(fsys|exitsym);
            gen(jmp,0,cx1);

            while(elx>cx2)           // 将exitlist中记录下的跳转语句的地址补充完整
            {
                elx--;
                code[exitlist[elx]].a=cx;
            }
        } break;
        case elsesym:            // else语句
        {
            test(fsys,0,33);     // 具体的处理已经在if中完成了,这里只要判错即可
        } break;
        case exitsym:            // exit语句
        {
            test(fsys,0,34);     // exit语句报错处理 

            exitlist[elx]=cx;    // 将jpc的位置记录进exitlist
            elx++;
            gen(jmp,0,0);

            getsym();
        } break;
        case readsym:            // read语句
        {
            getsym();
            if (sym==lparen) getsym();
            else error(35);

            readata();

            while(sym==comma)
            {
                getsym();
                readata();
            }

            if (sym==rparen) getsym();
            else error(22);
        } break;
        case writesym:           // write语句
        {
            getsym();
            if (sym==lparen) getsym();
            else error(35);

            expression(fsys|comma|rparen);
            gen(opr,0,14);

            while(sym==comma)
            {
                getsym();
                expression(fsys|comma|rparen);
                gen(opr,0,14);
            }

            if (sym==rparen) getsym();
            else error(22);
        } break;
    }

    test(fsys,0,19);
}