xref: /freebsd/sys/fs/procfs/procfs_status.c (revision 996c772f581f5624846dcd8470ca6860c1678b7c)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1993 Jan-Simon Pendry
3df8bae1dSRodney W. Grimes  * Copyright (c) 1993
4df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
7df8bae1dSRodney W. Grimes  * Jan-Simon Pendry.
8df8bae1dSRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
10df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
11df8bae1dSRodney W. Grimes  * are met:
12df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
14df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
15df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
16df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
17df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
18df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
19df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
20df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
21df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
37996c772fSJohn Dyson  *	@(#)procfs_status.c	8.4 (Berkeley) 6/15/94
38df8bae1dSRodney W. Grimes  *
39996c772fSJohn Dyson  * From:
401130b656SJordan K. Hubbard  *	$FreeBSD$
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45df8bae1dSRodney W. Grimes #include <sys/time.h>
46df8bae1dSRodney W. Grimes #include <sys/kernel.h>
47df8bae1dSRodney W. Grimes #include <sys/proc.h>
48df8bae1dSRodney W. Grimes #include <sys/vnode.h>
49df8bae1dSRodney W. Grimes #include <sys/ioctl.h>
50df8bae1dSRodney W. Grimes #include <sys/tty.h>
51df8bae1dSRodney W. Grimes #include <sys/resource.h>
52df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
53df8bae1dSRodney W. Grimes #include <miscfs/procfs/procfs.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes int
56df8bae1dSRodney W. Grimes procfs_dostatus(curp, p, pfs, uio)
57df8bae1dSRodney W. Grimes 	struct proc *curp;
58df8bae1dSRodney W. Grimes 	struct proc *p;
59df8bae1dSRodney W. Grimes 	struct pfsnode *pfs;
60df8bae1dSRodney W. Grimes 	struct uio *uio;
61df8bae1dSRodney W. Grimes {
62df8bae1dSRodney W. Grimes 	struct session *sess;
63df8bae1dSRodney W. Grimes 	struct tty *tp;
64df8bae1dSRodney W. Grimes 	struct ucred *cr;
65df8bae1dSRodney W. Grimes 	char *ps;
66df8bae1dSRodney W. Grimes 	char *sep;
67df8bae1dSRodney W. Grimes 	int pid, ppid, pgid, sid;
68df8bae1dSRodney W. Grimes 	int i;
69df8bae1dSRodney W. Grimes 	int xlen;
70df8bae1dSRodney W. Grimes 	int error;
71df8bae1dSRodney W. Grimes 	char psbuf[256];		/* XXX - conservative */
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes 	if (uio->uio_rw != UIO_READ)
74df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes 	pid = p->p_pid;
77df8bae1dSRodney W. Grimes 	ppid = p->p_pptr ? p->p_pptr->p_pid : 0,
78df8bae1dSRodney W. Grimes 	pgid = p->p_pgrp->pg_id;
79df8bae1dSRodney W. Grimes 	sess = p->p_pgrp->pg_session;
80df8bae1dSRodney W. Grimes 	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
81df8bae1dSRodney W. Grimes 
821d08058fSWolfram Schneider /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
831d08058fSWolfram Schneider                                 euid ruid rgid,egid,groups[1 .. NGROUPS]
841d08058fSWolfram Schneider */
85df8bae1dSRodney W. Grimes 	ps = psbuf;
86df8bae1dSRodney W. Grimes 	bcopy(p->p_comm, ps, MAXCOMLEN);
87df8bae1dSRodney W. Grimes 	ps[MAXCOMLEN] = '\0';
88df8bae1dSRodney W. Grimes 	ps += strlen(ps);
89df8bae1dSRodney W. Grimes 	ps += sprintf(ps, " %d %d %d %d ", pid, ppid, pgid, sid);
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes 	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
92df8bae1dSRodney W. Grimes 		ps += sprintf(ps, "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
93df8bae1dSRodney W. Grimes 	else
94df8bae1dSRodney W. Grimes 		ps += sprintf(ps, "%d,%d ", -1, -1);
95df8bae1dSRodney W. Grimes 
96df8bae1dSRodney W. Grimes 	sep = "";
97df8bae1dSRodney W. Grimes 	if (sess->s_ttyvp) {
98df8bae1dSRodney W. Grimes 		ps += sprintf(ps, "%sctty", sep);
99df8bae1dSRodney W. Grimes 		sep = ",";
100df8bae1dSRodney W. Grimes 	}
101df8bae1dSRodney W. Grimes 	if (SESS_LEADER(p)) {
102df8bae1dSRodney W. Grimes 		ps += sprintf(ps, "%ssldr", sep);
103df8bae1dSRodney W. Grimes 		sep = ",";
104df8bae1dSRodney W. Grimes 	}
105df8bae1dSRodney W. Grimes 	if (*sep != ',')
106df8bae1dSRodney W. Grimes 		ps += sprintf(ps, "noflags");
107df8bae1dSRodney W. Grimes 
108df8bae1dSRodney W. Grimes 	if (p->p_flag & P_INMEM)
1093a773ad0SPoul-Henning Kamp 		ps += sprintf(ps, " %ld,%ld",
110df8bae1dSRodney W. Grimes 			p->p_stats->p_start.tv_sec,
111df8bae1dSRodney W. Grimes 			p->p_stats->p_start.tv_usec);
112df8bae1dSRodney W. Grimes 	else
113df8bae1dSRodney W. Grimes 		ps += sprintf(ps, " -1,-1");
114df8bae1dSRodney W. Grimes 
115df8bae1dSRodney W. Grimes 	{
116df8bae1dSRodney W. Grimes 		struct timeval ut, st;
117df8bae1dSRodney W. Grimes 
118df8bae1dSRodney W. Grimes 		calcru(p, &ut, &st, (void *) 0);
1193a773ad0SPoul-Henning Kamp 		ps += sprintf(ps, " %ld,%ld %ld,%ld",
120df8bae1dSRodney W. Grimes 			ut.tv_sec,
121df8bae1dSRodney W. Grimes 			ut.tv_usec,
122df8bae1dSRodney W. Grimes 			st.tv_sec,
123df8bae1dSRodney W. Grimes 			st.tv_usec);
124df8bae1dSRodney W. Grimes 	}
125df8bae1dSRodney W. Grimes 
126df8bae1dSRodney W. Grimes 	ps += sprintf(ps, " %s",
127df8bae1dSRodney W. Grimes 		(p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
128df8bae1dSRodney W. Grimes 
129df8bae1dSRodney W. Grimes 	cr = p->p_ucred;
130df8bae1dSRodney W. Grimes 
1311d08058fSWolfram Schneider 	ps += sprintf(ps, " %ld %ld %ld",
1321d08058fSWolfram Schneider 		      cr->cr_uid,               /* euid */
1331d08058fSWolfram Schneider 		      p->p_cred->p_ruid,        /* ruid */
1341d08058fSWolfram Schneider 		      p->p_cred->p_rgid);       /* rgid */
1351d08058fSWolfram Schneider 
1361d08058fSWolfram Schneider 	/* egid (p->p_cred->p_svgid) is equal to cr_ngroups[0]
1371d08058fSWolfram Schneider 	   see also getegid(2) in /sys/kern/kern_prot.c */
1381d08058fSWolfram Schneider 
139df8bae1dSRodney W. Grimes 	for (i = 0; i < cr->cr_ngroups; i++)
1403a773ad0SPoul-Henning Kamp 		ps += sprintf(ps, ",%ld", cr->cr_groups[i]);
141df8bae1dSRodney W. Grimes 	ps += sprintf(ps, "\n");
142df8bae1dSRodney W. Grimes 
143df8bae1dSRodney W. Grimes 	xlen = ps - psbuf;
144df8bae1dSRodney W. Grimes 	xlen -= uio->uio_offset;
145df8bae1dSRodney W. Grimes 	ps = psbuf + uio->uio_offset;
146996c772fSJohn Dyson 	xlen = imin(xlen, uio->uio_resid);
147df8bae1dSRodney W. Grimes 	if (xlen <= 0)
148df8bae1dSRodney W. Grimes 		error = 0;
149df8bae1dSRodney W. Grimes 	else
150df8bae1dSRodney W. Grimes 		error = uiomove(ps, xlen, uio);
151df8bae1dSRodney W. Grimes 
152df8bae1dSRodney W. Grimes 	return (error);
153df8bae1dSRodney W. Grimes }
154