This source file includes following definitions.
- procfs_stat_gen
- procfs_dostatus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/time.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/ioctl.h>
47 #include <sys/tty.h>
48 #include <sys/resource.h>
49 #include <sys/resourcevar.h>
50 #include <miscfs/procfs/procfs.h>
51
52 int procfs_stat_gen(struct proc *, char *s, int);
53
54 #define COUNTORCAT(s, l, ps, n) do { \
55 if (s) \
56 strlcat(s, ps, l); \
57 else \
58 n += strlen(ps); \
59 } while (0)
60
61
62
63
64 int
65 procfs_stat_gen(struct proc *p, char *s, int l)
66 {
67 struct session *sess;
68 struct tty *tp;
69 struct ucred *cr;
70 int pid, ppid, pgid, sid;
71 struct timeval ut, st;
72 char ps[256], *sep;
73 int i, n;
74
75 pid = p->p_pid;
76 ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
77 pgid = p->p_pgrp->pg_id;
78 sess = p->p_pgrp->pg_session;
79 sid = sess->s_leader ? sess->s_leader->p_pid : 0;
80
81 n = 0;
82 if (s)
83 bzero(s, l);
84
85 bcopy(p->p_comm, ps, MAXCOMLEN-1);
86 ps[MAXCOMLEN] = '\0';
87 COUNTORCAT(s, l, ps, n);
88
89 (void) snprintf(ps, sizeof(ps), " %d %d %d %d ",
90 pid, ppid, pgid, sid);
91 COUNTORCAT(s, l, ps, n);
92
93 if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
94 snprintf(ps, sizeof(ps), "%d,%d ",
95 major(tp->t_dev), minor(tp->t_dev));
96 else
97 snprintf(ps, sizeof(ps), "%d,%d ",
98 -1, -1);
99 COUNTORCAT(s, l, ps, n);
100
101 sep = "";
102 if (sess->s_ttyvp) {
103 snprintf(ps, sizeof(ps), "%sctty", sep);
104 sep = ",";
105 COUNTORCAT(s, l, ps, n);
106 }
107
108 if (SESS_LEADER(p)) {
109 snprintf(ps, sizeof(ps), "%ssldr", sep);
110 sep = ",";
111 COUNTORCAT(s, l, ps, n);
112 }
113
114 if (*sep != ',') {
115 snprintf(ps, sizeof(ps), "noflags");
116 COUNTORCAT(s, l, ps, n);
117 }
118
119 snprintf(ps, sizeof(ps), " %ld,%ld",
120 p->p_stats->p_start.tv_sec, p->p_stats->p_start.tv_usec);
121 COUNTORCAT(s, l, ps, n);
122
123 calcru(p, &ut, &st, (void *) 0);
124 snprintf(ps, sizeof(ps), " %ld,%ld %ld,%ld",
125 ut.tv_sec, ut.tv_usec, st.tv_sec, st.tv_usec);
126 COUNTORCAT(s, l, ps, n);
127
128 snprintf(ps, sizeof(ps), " %s",
129 (p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
130 COUNTORCAT(s, l, ps, n);
131
132 cr = p->p_ucred;
133
134 snprintf(ps, sizeof(ps), " %u, %u", cr->cr_uid, cr->cr_gid);
135 COUNTORCAT(s, l, ps, n);
136 for (i = 0; i < cr->cr_ngroups; i++) {
137 snprintf(ps, sizeof(ps), ",%u", cr->cr_groups[i]);
138 COUNTORCAT(s, l, ps, n);
139 }
140
141 snprintf(ps, sizeof(ps), "\n");
142 COUNTORCAT(s, l, ps, n);
143
144 return (s != NULL ? strlen(s) + 1 : n + 1);
145 }
146
147 int
148 procfs_dostatus(struct proc *curp, struct proc *p, struct pfsnode *pfs, struct uio *uio)
149 {
150 char *ps;
151 int error, len;
152
153 if (uio->uio_rw != UIO_READ)
154 return (EOPNOTSUPP);
155
156 len = procfs_stat_gen(p, NULL, 0);
157 ps = malloc(len, M_TEMP, M_WAITOK);
158 len = procfs_stat_gen(p, ps, len);
159
160 if (len <= uio->uio_offset)
161 error = 0;
162 else {
163 len -= uio->uio_offset;
164 len = imin(len, uio->uio_resid);
165 error = uiomove(ps + uio->uio_offset, len, uio);
166 }
167
168 free(ps, M_TEMP);
169 return (error);
170 }