[BACK]Return to ps.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / bin / ps

Annotation of prex-old/usr/bin/ps/ps.c, Revision 1.1

1.1     ! nbrk        1: /*
        !             2:  * Copyright (c) 2007, Kohsuke Ohtani
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms, with or without
        !             6:  * modification, are permitted provided that the following conditions
        !             7:  * are met:
        !             8:  * 1. Redistributions of source code must retain the above copyright
        !             9:  *    notice, this list of conditions and the following disclaimer.
        !            10:  * 2. Redistributions in binary form must reproduce the above copyright
        !            11:  *    notice, this list of conditions and the following disclaimer in the
        !            12:  *    documentation and/or other materials provided with the distribution.
        !            13:  * 3. Neither the name of the author nor the names of any co-contributors
        !            14:  *    may be used to endorse or promote products derived from this software
        !            15:  *    without specific prior written permission.
        !            16:  *
        !            17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
        !            18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
        !            21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            27:  * SUCH DAMAGE.
        !            28:  */
        !            29:
        !            30: #include <prex/prex.h>
        !            31: #include <server/object.h>
        !            32: #include <server/stdmsg.h>
        !            33: #include <server/proc.h>
        !            34:
        !            35: #include <unistd.h>
        !            36: #include <stdlib.h>
        !            37: #include <errno.h>
        !            38: #include <stdio.h>
        !            39:
        !            40: #ifdef CMDBOX
        !            41: #define main(argc, argv)       ps_main(argc, argv)
        !            42: #endif
        !            43:
        !            44: #define PSFX   0x01
        !            45: #define PSFL   0x02
        !            46:
        !            47: struct info_proc {
        !            48:        pid_t   pid;
        !            49:        pid_t   ppid;
        !            50:        int     stat;
        !            51: };
        !            52:
        !            53: static object_t procobj;
        !            54:
        !            55: static int
        !            56: pstat(task_t task, struct info_proc *ip)
        !            57: {
        !            58:        static struct msg m;
        !            59:        int rc;
        !            60:
        !            61:        do {
        !            62:                m.hdr.code = PS_PSTAT;
        !            63:                m.data[0] = task;
        !            64:                rc = msg_send(procobj, &m, sizeof(m));
        !            65:        } while (rc == EINTR);
        !            66:
        !            67:        if (rc || m.hdr.status) {
        !            68:                ip->pid = -1;
        !            69:                ip->ppid = -1;
        !            70:                ip->stat = 1;
        !            71:                return -1;
        !            72:        }
        !            73:
        !            74:        ip->pid = m.data[0];
        !            75:        ip->ppid = m.data[1];
        !            76:        ip->stat = m.data[2];
        !            77:        return 0;
        !            78: }
        !            79:
        !            80: int
        !            81: main(int argc, char *argv[])
        !            82: {
        !            83:        int ch, rc, ps_flag = 0;
        !            84:        static struct info_thread it;
        !            85:        static struct info_proc ip;
        !            86:        pid_t last_pid = -2;
        !            87:        char stat[][2] = { "R", "Z", "S" };
        !            88:        char pol[][5] = { "FIFO", "RR  " };
        !            89:
        !            90:        while ((ch = getopt(argc, argv, "lx")) != -1)
        !            91:                switch(ch) {
        !            92:                case 'x':
        !            93:                        ps_flag |= PSFX;
        !            94:                        break;
        !            95:                case 'l':
        !            96:                        ps_flag |= PSFL;
        !            97:                        break;
        !            98:
        !            99:                case '?':
        !           100:                default:
        !           101:                        fprintf(stderr, "usage: ps [-lx]\n");
        !           102:                        exit(1);
        !           103:                }
        !           104:        argc -= optind;
        !           105:        argv += optind;
        !           106:
        !           107:        if (object_lookup(OBJNAME_PROC, &procobj))
        !           108:                exit(1);
        !           109:
        !           110:        if (ps_flag & PSFL)
        !           111:                printf("  PID  PPID PRI STAT POL  SUS     TIME WCHAN       CMD\n");
        !           112:        else
        !           113:                printf("  PID     TIME CMD\n");
        !           114:
        !           115:        rc = 0;
        !           116:        it.cookie = 0;
        !           117:        do {
        !           118:                /*
        !           119:                 * Get thread info from kernel.
        !           120:                 */
        !           121:                rc = sys_info(INFO_THREAD, &it);
        !           122:                if (!rc) {
        !           123:                        /*
        !           124:                         * Get process info from server.
        !           125:                         */
        !           126:                        if (pstat(it.task, &ip) && !(ps_flag & PSFX))
        !           127:                                continue;
        !           128:
        !           129:                        if (ps_flag & PSFL) {
        !           130:                                if (ip.pid == -1)
        !           131:                                        printf("    -     -"); /* kernel */
        !           132:                                else
        !           133:                                        printf("%5d %5d", ip.pid, ip.ppid);
        !           134:
        !           135:                                printf(" %3d %s    %s %3d %8d "
        !           136:                                       "%-11s %-11s\n",
        !           137:                                       it.prio, stat[ip.stat-1],
        !           138:                                       pol[it.policy], it.suspend_count,
        !           139:                                       it.total_ticks, it.sleep_event,
        !           140:                                       it.task_name);
        !           141:                        } else {
        !           142:                                if (!(ps_flag & PSFX) && (ip.pid == last_pid))
        !           143:                                        continue;
        !           144:                                if (ip.pid == -1)
        !           145:                                        printf("    -"); /* kernel */
        !           146:                                else
        !           147:                                        printf("%5d", ip.pid);
        !           148:
        !           149:                                printf(" %8d %-11s\n", it.total_ticks,
        !           150:                                       it.task_name);
        !           151:                                last_pid = ip.pid;
        !           152:                        }
        !           153:                }
        !           154:        } while (rc == 0);
        !           155:        exit(0);
        !           156: }

CVSweb