xref: /freebsd/sys/fs/procfs/procfs_status.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
1 /*
2  * Copyright (c) 1993 Jan-Simon Pendry
3  * Copyright (c) 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. 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  * From:
40  *	$Id: procfs_status.c,v 1.9 1997/03/24 11:24:42 bde Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/tty.h>
48 #include <sys/resourcevar.h>
49 #include <miscfs/procfs/procfs.h>
50 
51 int
52 procfs_dostatus(curp, p, pfs, uio)
53 	struct proc *curp;
54 	struct proc *p;
55 	struct pfsnode *pfs;
56 	struct uio *uio;
57 {
58 	struct session *sess;
59 	struct tty *tp;
60 	struct ucred *cr;
61 	char *ps;
62 	char *sep;
63 	int pid, ppid, pgid, sid;
64 	int i;
65 	int xlen;
66 	int error;
67 	char psbuf[256];		/* XXX - conservative */
68 
69 	if (uio->uio_rw != UIO_READ)
70 		return (EOPNOTSUPP);
71 
72 	pid = p->p_pid;
73 	ppid = p->p_pptr ? p->p_pptr->p_pid : 0,
74 	pgid = p->p_pgrp->pg_id;
75 	sess = p->p_pgrp->pg_session;
76 	sid = sess->s_leader ? sess->s_leader->p_pid : 0;
77 
78 /* comm pid ppid pgid sid maj,min ctty,sldr start ut st wmsg
79                                 euid ruid rgid,egid,groups[1 .. NGROUPS]
80 */
81 	ps = psbuf;
82 	bcopy(p->p_comm, ps, MAXCOMLEN);
83 	ps[MAXCOMLEN] = '\0';
84 	ps += strlen(ps);
85 	ps += sprintf(ps, " %d %d %d %d ", pid, ppid, pgid, sid);
86 
87 	if ((p->p_flag&P_CONTROLT) && (tp = sess->s_ttyp))
88 		ps += sprintf(ps, "%d,%d ", major(tp->t_dev), minor(tp->t_dev));
89 	else
90 		ps += sprintf(ps, "%d,%d ", -1, -1);
91 
92 	sep = "";
93 	if (sess->s_ttyvp) {
94 		ps += sprintf(ps, "%sctty", sep);
95 		sep = ",";
96 	}
97 	if (SESS_LEADER(p)) {
98 		ps += sprintf(ps, "%ssldr", sep);
99 		sep = ",";
100 	}
101 	if (*sep != ',')
102 		ps += sprintf(ps, "noflags");
103 
104 	if (p->p_flag & P_INMEM)
105 		ps += sprintf(ps, " %ld,%ld",
106 			p->p_stats->p_start.tv_sec,
107 			p->p_stats->p_start.tv_usec);
108 	else
109 		ps += sprintf(ps, " -1,-1");
110 
111 	{
112 		struct timeval ut, st;
113 
114 		calcru(p, &ut, &st, (void *) 0);
115 		ps += sprintf(ps, " %ld,%ld %ld,%ld",
116 			ut.tv_sec,
117 			ut.tv_usec,
118 			st.tv_sec,
119 			st.tv_usec);
120 	}
121 
122 	ps += sprintf(ps, " %s",
123 		(p->p_wchan && p->p_wmesg) ? p->p_wmesg : "nochan");
124 
125 	cr = p->p_ucred;
126 
127 	ps += sprintf(ps, " %ld %ld %ld",
128 		      cr->cr_uid,               /* euid */
129 		      p->p_cred->p_ruid,        /* ruid */
130 		      p->p_cred->p_rgid);       /* rgid */
131 
132 	/* egid (p->p_cred->p_svgid) is equal to cr_ngroups[0]
133 	   see also getegid(2) in /sys/kern/kern_prot.c */
134 
135 	for (i = 0; i < cr->cr_ngroups; i++)
136 		ps += sprintf(ps, ",%ld", cr->cr_groups[i]);
137 	ps += sprintf(ps, "\n");
138 
139 	xlen = ps - psbuf;
140 	xlen -= uio->uio_offset;
141 	ps = psbuf + uio->uio_offset;
142 	xlen = imin(xlen, uio->uio_resid);
143 	if (xlen <= 0)
144 		error = 0;
145 	else
146 		error = uiomove(ps, xlen, uio);
147 
148 	return (error);
149 }
150