1 /** $MirOS: src/sys/miscfs/procfs/procfs_status.c,v 1.2 2005/03/06 21:28:12 tg Exp $ */
2 /* $OpenBSD: procfs_status.c,v 1.8 2004/05/05 23:52:10 tedu Exp $ */
3 /* $NetBSD: procfs_status.c,v 1.11 1996/03/16 23:52:50 christos Exp $ */
4
5 /*
6 * Copyright (c) 1993 Jan-Simon Pendry
7 * Copyright (c) 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)procfs_status.c 8.4 (Berkeley) 6/15/94
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/time.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/ioctl.h>
48 #include <sys/tty.h>
49 #include <sys/resource.h>
50 #include <sys/resourcevar.h>
51 #include <miscfs/procfs/procfs.h>
52
53 int procfs_stat_gen(struct proc *, char *s, int);
54
55 #define COUNTORCAT(s, l, ps, n) do { \
56 if (s) \
57 strlcat(s, ps, l); \
58 else \
59 n += strlen(ps); \
60 } while (0)
61
62 /* Generates:
63 * comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg uid gid groups
64 */
65 int
procfs_stat_gen(p,s,l)66 procfs_stat_gen(p, s, l)
67 struct proc *p;
68 char *s;
69 int l;
70 {
71 struct session *sess;
72 struct tty *tp;
73 struct ucred *cr;
74 int pid, ppid, pgid, sid;
75 struct timeval ut, st;
76 char ps[256], *sep;
77 int i, n;
78
79 pid = p->p_pid;
80 ppid = p->p_pptr ? p->p_pptr->p_pid : 0;
81 pgid = p->p_pgrp->pg_id;
82 sess = p->p_pgrp->pg_session;
83 sid = sess->s_leader ? sess->s_leader->p_pid : 0;
84
85 n = 0;
86 if (s)
87 bzero(s, l);
88
89 bcopy(p->p_comm, ps, MAXCOMLEN-1);
90 ps[MAXCOMLEN] = '\0';
91 COUNTORCAT(s, l, ps, n);
92
93 (void) snprintf(ps, sizeof(ps), " %d %d %d %d ",
94 pid, ppid, pgid, sid);
95 COUNTORCAT(s, l, ps, n);
96
97 if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
98 snprintf(ps, sizeof(ps), "%d,%d ",
99 major(tp->t_dev), minor(tp->t_dev));
100 else
101 snprintf(ps, sizeof(ps), "%d,%d ",
102 -1, -1);
103 COUNTORCAT(s, l, ps, n);
104
105 sep = "";
106 if (sess->s_ttyvp) {
107 snprintf(ps, sizeof(ps), "%sctty", sep);
108 sep = ",";
109 COUNTORCAT(s, l, ps, n);
110 }
111
112 if (SESS_LEADER(p)) {
113 snprintf(ps, sizeof(ps), "%ssldr", sep);
114 sep = ",";
115 COUNTORCAT(s, l, ps, n);
116 }
117
118 if (*sep != ',') {
119 snprintf(ps, sizeof(ps), "noflags");
120 COUNTORCAT(s, l, ps, n);
121 }
122
123 if (p->p_flag & P_INMEM)
124 snprintf(ps, sizeof(ps), " %lld,%ld",
125 (int64_t)p->p_stats->p_start.tv_sec,
126 p->p_stats->p_start.tv_usec);
127 else
128 snprintf(ps, sizeof(ps), " -1,-1");
129 COUNTORCAT(s, l, ps, n);
130
131 calcru(p, &ut, &st, (void *) 0);
132 snprintf(ps, sizeof(ps), " %lld,%ld %lld,%ld",
133 (int64_t)ut.tv_sec, ut.tv_usec,
134 (int64_t)st.tv_sec, st.tv_usec);
135 COUNTORCAT(s, l, ps, n);
136
137 snprintf(ps, sizeof(ps), " %s",
138 (p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
139 COUNTORCAT(s, l, ps, n);
140
141 cr = p->p_ucred;
142
143 snprintf(ps, sizeof(ps), " %u, %u", cr->cr_uid, cr->cr_gid);
144 COUNTORCAT(s, l, ps, n);
145 for (i = 0; i < cr->cr_ngroups; i++) {
146 snprintf(ps, sizeof(ps), ",%u", cr->cr_groups[i]);
147 COUNTORCAT(s, l, ps, n);
148 }
149
150 snprintf(ps, sizeof(ps), "\n");
151 COUNTORCAT(s, l, ps, n);
152
153 return (s != NULL ? strlen(s) + 1 : n + 1);
154 }
155
156 int
procfs_dostatus(curp,p,pfs,uio)157 procfs_dostatus(curp, p, pfs, uio)
158 struct proc *curp;
159 struct proc *p;
160 struct pfsnode *pfs;
161 struct uio *uio;
162 {
163 char *ps;
164 int error, len;
165
166 if (uio->uio_rw != UIO_READ)
167 return (EOPNOTSUPP);
168
169 len = procfs_stat_gen(p, NULL, 0);
170 ps = malloc(len, M_TEMP, M_WAITOK);
171 len = procfs_stat_gen(p, ps, len);
172
173 if (len <= uio->uio_offset)
174 error = 0;
175 else {
176 len -= uio->uio_offset;
177 len = imin(len, uio->uio_resid);
178 error = uiomove(ps + uio->uio_offset, len, uio);
179 }
180
181 free(ps, M_TEMP);
182 return (error);
183 }
184