Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / prex-0.9.0 / usr / bin / ps / ps.c @ 03e9c04a

History | View | Annotate | Download (3.7 KB)

1
/*
2
 * Copyright (c) 2007, Kohsuke Ohtani
3
 * All rights reserved.
4
x *
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 <sys/prex.h>
31
#include <ipc/ipc.h>
32
#include <ipc/proc.h>
33

    
34
#include <unistd.h>
35
#include <stdlib.h>
36
#include <errno.h>
37
#include <stdio.h>
38

    
39
#ifdef CMDBOX
40
#define main(argc, argv)        ps_main(argc, argv)
41
#endif
42

    
43
#define PSFX        0x01
44
#define PSFL        0x02
45

    
46
struct procinfo {
47
        pid_t        pid;
48
        pid_t        ppid;
49
        int        stat;
50
};
51

    
52
static object_t procobj;
53

    
54
static int
55
pstat(task_t task, struct procinfo *pi)
56
{
57
        static struct msg m;
58
        int rc;
59

    
60
        do {
61
                m.hdr.code = PS_PSTAT;
62
                m.data[0] = (int)task;
63
                rc = msg_send(procobj, &m, sizeof(m));
64
        } while (rc == EINTR);
65

    
66
        if (rc || m.hdr.status) {
67
                pi->pid = -1;
68
                pi->ppid = -1;
69
                pi->stat = 1;
70
                return -1;
71
        }
72

    
73
        pi->pid = m.data[0];
74
        pi->ppid = m.data[1];
75
        pi->stat = m.data[2];
76
        return 0;
77
}
78

    
79
int
80
main(int argc, char *argv[])
81
{
82
        static const char stat[][2] = { "R", "Z", "S" };
83
        static const char pol[][5] = { "FIFO", "RR  " };
84
        static struct threadinfo ti;
85
        static struct procinfo pi;
86
        int ch, rc, ps_flag = 0;
87
        pid_t last_pid = -2;
88

    
89
        while ((ch = getopt(argc, argv, "lx")) != -1)
90
                switch(ch) {
91
                case 'x':
92
                        ps_flag |= PSFX;
93
                        break;
94
                case 'l':
95
                        ps_flag |= PSFL;
96
                        break;
97

    
98
                case '?':
99
                default:
100
                        fprintf(stderr, "usage: ps [-lx]\n");
101
                        exit(1);
102
                }
103
        argc -= optind;
104
        argv += optind;
105

    
106
        if (object_lookup("!proc", &procobj))
107
                exit(1);
108

    
109
        if (ps_flag & PSFL)
110
                printf("  PID  PPID PRI STAT POL      TIME WCHAN       CMD\n");
111
        else
112
                printf("  PID     TIME CMD\n");
113

    
114
        rc = 0;
115
        ti.cookie = 0;
116
        do {
117
                /*
118
                 * Get thread info from kernel.
119
                 */
120
                rc = sys_info(INFO_THREAD, &ti);
121
                if (!rc) {
122
                        /*
123
                         * Get process info from server.
124
                         */
125
                        if (pstat(ti.task, &pi) && !(ps_flag & PSFX))
126
                                continue;
127

    
128
                        if (ps_flag & PSFL) {
129
                                if (pi.pid == -1)
130
                                        printf("    -     -"); /* kernel */
131
                                else
132
                                        printf("%5d %5d", pi.pid, pi.ppid);
133

    
134
                                printf(" %3d %s    %s %8d "
135
                                       "%-11s %-11s\n",
136
                                       ti.priority, stat[pi.stat-1],
137
                                       pol[ti.policy],
138
                                       ti.time, ti.slpevt, ti.taskname);
139
                        } else {
140
                                if (!(ps_flag & PSFX) && (pi.pid == last_pid))
141
                                        continue;
142
                                if (pi.pid == -1)
143
                                        printf("    -"); /* kernel */
144
                                else
145
                                        printf("%5d", pi.pid);
146

    
147
                                printf(" %8d %-11s\n", ti.time, ti.taskname);
148
                                last_pid = pi.pid;
149
                        }
150
                }
151
        } while (rc == 0);
152
        exit(0);
153
}