1d167cf6fSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1993 Jan-Simon Pendry 5df8bae1dSRodney W. Grimes * Copyright (c) 1993 6df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 9df8bae1dSRodney W. Grimes * Jan-Simon Pendry. 10df8bae1dSRodney W. Grimes * 11df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 12df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 13df8bae1dSRodney W. Grimes * are met: 14df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 16df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 18df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 19fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 20df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 21df8bae1dSRodney W. Grimes * without specific prior written permission. 22df8bae1dSRodney W. Grimes * 23df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33df8bae1dSRodney W. Grimes * SUCH DAMAGE. 34df8bae1dSRodney W. Grimes * 35996c772fSJohn Dyson * From: 36a21759a1SBruce Evans * $Id: procfs_status.c,v 3.1 1993/12/15 09:40:17 jsp Exp $ 37df8bae1dSRodney W. Grimes */ 38df8bae1dSRodney W. Grimes 39df8bae1dSRodney W. Grimes #include <sys/param.h> 4087ccef7bSDag-Erling Smørgrav #include <sys/kernel.h> 41df8bae1dSRodney W. Grimes #include <sys/systm.h> 42fb919e4dSMark Murray #include <sys/exec.h> 43fb919e4dSMark Murray #include <sys/lock.h> 44fb919e4dSMark Murray #include <sys/mutex.h> 4501137630SRobert Watson #include <sys/jail.h> 4601137630SRobert Watson #include <sys/malloc.h> 47f591779bSSeigo Tanimura #include <sys/mutex.h> 48f591779bSSeigo Tanimura #include <sys/sx.h> 49fb919e4dSMark Murray #include <sys/proc.h> 50df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 513a669c52SDag-Erling Smørgrav #include <sys/sbuf.h> 52fb919e4dSMark Murray #include <sys/tty.h> 53df8bae1dSRodney W. Grimes 5463a99273SMarcel Moolenaar #include <vm/vm.h> 5563a99273SMarcel Moolenaar #include <vm/pmap.h> 5663a99273SMarcel Moolenaar #include <vm/vm_param.h> 57fb919e4dSMark Murray 583a669c52SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs.h> 5999d300a1SRuslan Ermilov #include <fs/procfs/procfs.h> 6063a99273SMarcel Moolenaar 61df8bae1dSRodney W. Grimes int 623a669c52SDag-Erling Smørgrav procfs_doprocstatus(PFS_FILL_ARGS) 63df8bae1dSRodney W. Grimes { 64*47288801SMark Johnston struct timeval start, ut, st; 65df8bae1dSRodney W. Grimes struct session *sess; 66a21759a1SBruce Evans struct thread *tdfirst; 67df8bae1dSRodney W. Grimes struct tty *tp; 68df8bae1dSRodney W. Grimes struct ucred *cr; 69f3640361SJohn Baldwin const char *wmesg; 703a669c52SDag-Erling Smørgrav char *pc; 71df8bae1dSRodney W. Grimes char *sep; 72584b675eSKonstantin Belousov struct timeval boottime; 73df8bae1dSRodney W. Grimes int pid, ppid, pgid, sid; 74df8bae1dSRodney W. Grimes int i; 75df8bae1dSRodney W. Grimes 76df8bae1dSRodney W. Grimes pid = p->p_pid; 7730ac5d0fSJohn Baldwin PROC_LOCK(p); 78e0f9d286SPeter Wemm ppid = p->p_pptr ? p->p_pptr->p_pid : 0; 79df8bae1dSRodney W. Grimes pgid = p->p_pgrp->pg_id; 80df8bae1dSRodney W. Grimes sess = p->p_pgrp->pg_session; 81f591779bSSeigo Tanimura SESS_LOCK(sess); 82df8bae1dSRodney W. Grimes sid = sess->s_leader ? sess->s_leader->p_pid : 0; 83df8bae1dSRodney W. Grimes 8446d7d4a3SPoul-Henning Kamp /* comm pid ppid pgid sid tty ctty,sldr start ut st wmsg 8564606312SBrooks Davis euid ruid rgid,egid,groups[1 .. ngroups] 861d08058fSWolfram Schneider */ 87b8c8516aSEivind Eklund 883273a63eSMike Barcroft pc = p->p_comm; 893273a63eSMike Barcroft do { 903273a63eSMike Barcroft if (*pc < 33 || *pc > 126 || *pc == '\\') 913a669c52SDag-Erling Smørgrav sbuf_printf(sb, "\\%03o", *pc); 923273a63eSMike Barcroft else 933a669c52SDag-Erling Smørgrav sbuf_putc(sb, *pc); 943a669c52SDag-Erling Smørgrav } while (*++pc); 953a669c52SDag-Erling Smørgrav sbuf_printf(sb, " %d %d %d %d ", pid, ppid, pgid, sid); 96df8bae1dSRodney W. Grimes if ((p->p_flag & P_CONTROLT) && (tp = sess->s_ttyp)) 9746d7d4a3SPoul-Henning Kamp sbuf_printf(sb, "%s ", devtoname(tp->t_dev)); 98df8bae1dSRodney W. Grimes else 9946d7d4a3SPoul-Henning Kamp sbuf_printf(sb, "- "); 100df8bae1dSRodney W. Grimes 101df8bae1dSRodney W. Grimes sep = ""; 102df8bae1dSRodney W. Grimes if (sess->s_ttyvp) { 1033a669c52SDag-Erling Smørgrav sbuf_printf(sb, "%sctty", sep); 104df8bae1dSRodney W. Grimes sep = ","; 105df8bae1dSRodney W. Grimes } 106df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) { 1073a669c52SDag-Erling Smørgrav sbuf_printf(sb, "%ssldr", sep); 108df8bae1dSRodney W. Grimes sep = ","; 109df8bae1dSRodney W. Grimes } 110f591779bSSeigo Tanimura SESS_UNLOCK(sess); 111b8c8516aSEivind Eklund if (*sep != ',') { 1123a669c52SDag-Erling Smørgrav sbuf_printf(sb, "noflags"); 113b8c8516aSEivind Eklund } 114df8bae1dSRodney W. Grimes 115f3640361SJohn Baldwin tdfirst = FIRST_THREAD_IN_PROC(p); 116c8997bf0SKonstantin Belousov thread_lock(tdfirst); 117f3640361SJohn Baldwin if (tdfirst->td_wchan != NULL) { 118f3640361SJohn Baldwin KASSERT(tdfirst->td_wmesg != NULL, 119f3640361SJohn Baldwin ("wchan %p has no wmesg", tdfirst->td_wchan)); 120f3640361SJohn Baldwin wmesg = tdfirst->td_wmesg; 121f3640361SJohn Baldwin } else 122f3640361SJohn Baldwin wmesg = "nochan"; 123c8997bf0SKonstantin Belousov thread_unlock(tdfirst); 124f3640361SJohn Baldwin 1255c7bebf9SKonstantin Belousov PROC_STATLOCK(p); 12678c85e8dSJohn Baldwin calcru(p, &ut, &st); 1275c7bebf9SKonstantin Belousov PROC_STATUNLOCK(p); 12887ccef7bSDag-Erling Smørgrav start = p->p_stats->p_start; 129584b675eSKonstantin Belousov getboottime(&boottime); 13087ccef7bSDag-Erling Smørgrav timevaladd(&start, &boottime); 13109c00166STom Rhodes sbuf_printf(sb, " %jd,%ld %jd,%ld %jd,%ld", 13209c00166STom Rhodes (intmax_t)start.tv_sec, start.tv_usec, 13309c00166STom Rhodes (intmax_t)ut.tv_sec, ut.tv_usec, 13409c00166STom Rhodes (intmax_t)st.tv_sec, st.tv_usec); 135df8bae1dSRodney W. Grimes 136f3640361SJohn Baldwin sbuf_printf(sb, " %s", wmesg); 137df8bae1dSRodney W. Grimes 138df8bae1dSRodney W. Grimes cr = p->p_ucred; 139df8bae1dSRodney W. Grimes 1403a669c52SDag-Erling Smørgrav sbuf_printf(sb, " %lu %lu %lu", 141ac1e407bSBruce Evans (u_long)cr->cr_uid, 142b1fc0ec1SRobert Watson (u_long)cr->cr_ruid, 143b1fc0ec1SRobert Watson (u_long)cr->cr_rgid); 1441d08058fSWolfram Schneider 145b1fc0ec1SRobert Watson /* egid (cr->cr_svgid) is equal to cr_ngroups[0] 1461d08058fSWolfram Schneider see also getegid(2) in /sys/kern/kern_prot.c */ 1471d08058fSWolfram Schneider 148b8c8516aSEivind Eklund for (i = 0; i < cr->cr_ngroups; i++) { 1493a669c52SDag-Erling Smørgrav sbuf_printf(sb, ",%lu", (u_long)cr->cr_groups[i]); 150b8c8516aSEivind Eklund } 15175c13541SPoul-Henning Kamp 1520304c731SJamie Gritton if (jailed(cr)) { 1530304c731SJamie Gritton mtx_lock(&cr->cr_prison->pr_mtx); 1540304c731SJamie Gritton sbuf_printf(sb, " %s", 1550304c731SJamie Gritton prison_name(td->td_ucred->cr_prison, cr->cr_prison)); 1560304c731SJamie Gritton mtx_unlock(&cr->cr_prison->pr_mtx); 15701137630SRobert Watson } else { 1583a669c52SDag-Erling Smørgrav sbuf_printf(sb, " -"); 15901137630SRobert Watson } 160590ae816SJohn Baldwin PROC_UNLOCK(p); 1613a669c52SDag-Erling Smørgrav sbuf_printf(sb, "\n"); 162df8bae1dSRodney W. Grimes 1633a669c52SDag-Erling Smørgrav return (0); 164df8bae1dSRodney W. Grimes } 16575ba7757SPeter Wemm 16675ba7757SPeter Wemm int 1673a669c52SDag-Erling Smørgrav procfs_doproccmdline(PFS_FILL_ARGS) 16875ba7757SPeter Wemm { 16975ba7757SPeter Wemm 17075ba7757SPeter Wemm /* 1716153cb20SPoul-Henning Kamp * If we are using the ps/cmdline caching, use that. Otherwise 172beb7471bSMikolaj Golub * read argv from the process space. 17363a99273SMarcel Moolenaar * Note that if the argv is no longer available, we deliberately 17463a99273SMarcel Moolenaar * don't fall back on p->p_comm or return an error: the authentic 17563a99273SMarcel Moolenaar * Linux behaviour is to return zero-length in this case. 17675ba7757SPeter Wemm */ 17763a99273SMarcel Moolenaar 178e9b192b7SAlfred Perlstein PROC_LOCK(p); 179c5b7c33bSPawel Jakub Dawidek if (p->p_args && p_cansee(td, p) == 0) { 1803a669c52SDag-Erling Smørgrav sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length); 181e9b192b7SAlfred Perlstein PROC_UNLOCK(p); 182590ae816SJohn Baldwin return (0); 183590ae816SJohn Baldwin } 184beb7471bSMikolaj Golub 185beb7471bSMikolaj Golub if ((p->p_flag & P_SYSTEM) != 0) { 186590ae816SJohn Baldwin PROC_UNLOCK(p); 187beb7471bSMikolaj Golub return (0); 18863a99273SMarcel Moolenaar } 18975ba7757SPeter Wemm 190beb7471bSMikolaj Golub PROC_UNLOCK(p); 191beb7471bSMikolaj Golub 192fe7f89b7SMikolaj Golub return (proc_getargv(td, p, sb)); 19375ba7757SPeter Wemm } 194