/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1993 Jan-Simon Pendry4* Copyright (c) 19935* The Regents of the University of California. All rights reserved.6*7* This code is derived from software contributed to Berkeley by8* Jan-Simon Pendry.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the University nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*34* From:35* $Id: procfs_status.c,v 3.1 1993/12/15 09:40:17 jsp Exp $36*/3738#include <sys/param.h>39#include <sys/kernel.h>40#include <sys/systm.h>41#include <sys/exec.h>42#include <sys/lock.h>43#include <sys/mutex.h>44#include <sys/jail.h>45#include <sys/malloc.h>46#include <sys/mutex.h>47#include <sys/sx.h>48#include <sys/proc.h>49#include <sys/resourcevar.h>50#include <sys/sbuf.h>51#include <sys/tty.h>5253#include <vm/vm.h>54#include <vm/pmap.h>55#include <vm/vm_param.h>5657#include <fs/pseudofs/pseudofs.h>58#include <fs/procfs/procfs.h>5960int61procfs_doprocstatus(PFS_FILL_ARGS)62{63struct timeval start, ut, st;64struct session *sess;65struct thread *tdfirst;66struct tty *tp;67struct ucred *cr;68const char *wmesg;69char *pc;70char *sep;71struct timeval boottime;72int pid, ppid, pgid, sid;73int i;7475pid = p->p_pid;76PROC_LOCK(p);77ppid = p->p_pptr ? p->p_pptr->p_pid : 0;78pgid = p->p_pgrp->pg_id;79sess = p->p_pgrp->pg_session;80SESS_LOCK(sess);81sid = sess->s_leader ? sess->s_leader->p_pid : 0;8283/* comm pid ppid pgid sid tty ctty,sldr start ut st wmsg84euid ruid rgid,egid,groups[1 .. ngroups]85*/8687pc = p->p_comm;88do {89if (*pc < 33 || *pc > 126 || *pc == '\\')90sbuf_printf(sb, "\\%03o", *pc);91else92sbuf_putc(sb, *pc);93} while (*++pc);94sbuf_printf(sb, " %d %d %d %d ", pid, ppid, pgid, sid);95if ((p->p_flag & P_CONTROLT) && (tp = sess->s_ttyp))96sbuf_printf(sb, "%s ", devtoname(tp->t_dev));97else98sbuf_printf(sb, "- ");99100sep = "";101if (sess->s_ttyvp) {102sbuf_printf(sb, "%sctty", sep);103sep = ",";104}105if (SESS_LEADER(p)) {106sbuf_printf(sb, "%ssldr", sep);107sep = ",";108}109SESS_UNLOCK(sess);110if (*sep != ',') {111sbuf_printf(sb, "noflags");112}113114tdfirst = FIRST_THREAD_IN_PROC(p);115thread_lock(tdfirst);116if (tdfirst->td_wchan != NULL) {117KASSERT(tdfirst->td_wmesg != NULL,118("wchan %p has no wmesg", tdfirst->td_wchan));119wmesg = tdfirst->td_wmesg;120} else121wmesg = "nochan";122thread_unlock(tdfirst);123124PROC_STATLOCK(p);125calcru(p, &ut, &st);126PROC_STATUNLOCK(p);127start = p->p_stats->p_start;128getboottime(&boottime);129timevaladd(&start, &boottime);130sbuf_printf(sb, " %jd,%ld %jd,%ld %jd,%ld",131(intmax_t)start.tv_sec, start.tv_usec,132(intmax_t)ut.tv_sec, ut.tv_usec,133(intmax_t)st.tv_sec, st.tv_usec);134135sbuf_printf(sb, " %s", wmesg);136137cr = p->p_ucred;138139sbuf_printf(sb, " %lu %lu %lu",140(u_long)cr->cr_uid,141(u_long)cr->cr_ruid,142(u_long)cr->cr_rgid);143sbuf_printf(sb, ",%lu", (u_long)cr->cr_gid);144for (i = 0; i < cr->cr_ngroups; i++)145sbuf_printf(sb, ",%lu", (u_long)cr->cr_groups[i]);146147if (jailed(cr)) {148mtx_lock(&cr->cr_prison->pr_mtx);149sbuf_printf(sb, " %s",150prison_name(td->td_ucred->cr_prison, cr->cr_prison));151mtx_unlock(&cr->cr_prison->pr_mtx);152} else {153sbuf_printf(sb, " -");154}155PROC_UNLOCK(p);156sbuf_printf(sb, "\n");157158return (0);159}160161int162procfs_doproccmdline(PFS_FILL_ARGS)163{164165/*166* If we are using the ps/cmdline caching, use that. Otherwise167* read argv from the process space.168* Note that if the argv is no longer available, we deliberately169* don't fall back on p->p_comm or return an error: the authentic170* Linux behaviour is to return zero-length in this case.171*/172173PROC_LOCK(p);174if (p->p_args && p_cansee(td, p) == 0) {175sbuf_bcpy(sb, p->p_args->ar_args, p->p_args->ar_length);176PROC_UNLOCK(p);177return (0);178}179180if ((p->p_flag & P_SYSTEM) != 0) {181PROC_UNLOCK(p);182return (0);183}184185PROC_UNLOCK(p);186187return (proc_getargv(td, p, sb));188}189190191