2016-04-05 01:14:38 +03:00
|
|
|
/* $NetBSD: kvm_proc.c,v 1.92 2016/04/04 22:14:38 christos Exp $ */
|
1998-08-15 13:16:27 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Charles M. Hannum.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
1996-03-19 01:33:07 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1989, 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software developed by the Computer Systems
|
|
|
|
* Engineering group at Lawrence Berkeley Laboratory under DARPA contract
|
|
|
|
* BG 91-66 and contributed to Berkeley.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:42:00 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-09 07:15:36 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-08-15 06:21:56 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-05-09 07:15:36 +04:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
1996-03-19 01:33:07 +03:00
|
|
|
#if 0
|
1994-05-09 07:15:36 +04:00
|
|
|
static char sccsid[] = "@(#)kvm_proc.c 8.3 (Berkeley) 9/23/93";
|
1996-03-19 01:33:07 +03:00
|
|
|
#else
|
2016-04-05 01:14:38 +03:00
|
|
|
__RCSID("$NetBSD: kvm_proc.c,v 1.92 2016/04/04 22:14:38 christos Exp $");
|
1996-03-19 01:33:07 +03:00
|
|
|
#endif
|
1994-05-09 07:15:36 +04:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Proc traversal interface for kvm. ps and w are (probably) the exclusive
|
|
|
|
* users of this code, so we've factored it out into a separate module.
|
|
|
|
* Thus, we keep this grunge out of the other kvm applications (i.e.,
|
|
|
|
* most other applications are interested only in open/close/read/nlist).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2003-01-18 13:32:11 +03:00
|
|
|
#include <sys/lwp.h>
|
2016-04-05 01:14:38 +03:00
|
|
|
#include <sys/wait.h>
|
1994-05-09 07:15:36 +04:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/tty.h>
|
2006-05-11 16:00:20 +04:00
|
|
|
#include <sys/resourcevar.h>
|
2007-02-24 23:41:33 +03:00
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/specificdata.h>
|
Change kvm_pa2off() and kvm_kvatop() prototypes (private to kvm(3)):
-int _kvm_kvatop(kvm_t *, u_long, u_long *);
-off_t _kvm_pa2off(kvm_t *, u_long);
+int _kvm_kvatop(kvm_t *, vaddr_t, paddr_t *);
+off_t _kvm_pa2off(kvm_t *, paddr_t);
Basically, use vaddr_t for VA and paddr_t for PA. In addition, for variables
representing addresses, use paddr_t or vaddr_t, depending on the context.
For most arches, vaddr_t and paddr_t are equivalent to unsigned long. However,
the change was needed for exotic situations, like i386 PAE, were unsigned long
is not suitable for PA which are 64 bits long. As this required a complete
change of the function prototypes, all arches had to be adapted accordingly.
Core files from before this commit should still work with the new code; I did
not see any direct dependency between core's structure and kvatop/pa2off.
The change was compile tested for all arches, as it impacts all of them.
See also:
http://mail-index.netbsd.org/current-users/2010/09/07/msg014249.html
2010-09-21 03:23:16 +04:00
|
|
|
#include <sys/types.h>
|
2007-02-10 01:08:48 +03:00
|
|
|
|
2006-12-31 19:20:18 +03:00
|
|
|
#include <errno.h>
|
1994-10-19 06:03:39 +03:00
|
|
|
#include <stdlib.h>
|
2003-03-21 01:53:12 +03:00
|
|
|
#include <stddef.h>
|
1995-05-16 18:21:08 +04:00
|
|
|
#include <string.h>
|
1994-05-09 07:15:36 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <kvm.h>
|
|
|
|
|
1998-02-12 09:55:29 +03:00
|
|
|
#include <uvm/uvm_extern.h>
|
- add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.
- adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.
- add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)
- patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)
- patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.
- update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)
this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.
tested on i386 and sparc64, build tested on several other platforms.
thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
2009-03-29 05:02:48 +04:00
|
|
|
#include <uvm/uvm_param.h>
|
1999-01-25 06:38:57 +03:00
|
|
|
#include <uvm/uvm_amap.h>
|
2010-11-12 07:52:08 +03:00
|
|
|
#include <uvm/uvm_page.h>
|
1998-02-12 09:55:29 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <db.h>
|
|
|
|
#include <paths.h>
|
|
|
|
|
|
|
|
#include "kvm_private.h"
|
|
|
|
|
2000-05-26 06:42:21 +04:00
|
|
|
/*
|
|
|
|
* Common info from kinfo_proc and kinfo_proc2 used by helper routines.
|
|
|
|
*/
|
|
|
|
struct miniproc {
|
|
|
|
struct vmspace *p_vmspace;
|
|
|
|
char p_stat;
|
|
|
|
struct proc *p_paddr;
|
|
|
|
pid_t p_pid;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert from struct proc and kinfo_proc{,2} to miniproc.
|
|
|
|
*/
|
|
|
|
#define PTOMINI(kp, p) \
|
2003-02-27 02:04:37 +03:00
|
|
|
do { \
|
2000-05-26 06:42:21 +04:00
|
|
|
(p)->p_stat = (kp)->p_stat; \
|
|
|
|
(p)->p_pid = (kp)->p_pid; \
|
|
|
|
(p)->p_paddr = NULL; \
|
|
|
|
(p)->p_vmspace = (kp)->p_vmspace; \
|
|
|
|
} while (/*CONSTCOND*/0);
|
|
|
|
|
|
|
|
#define KPTOMINI(kp, p) \
|
2003-02-27 02:04:37 +03:00
|
|
|
do { \
|
2000-05-26 06:42:21 +04:00
|
|
|
(p)->p_stat = (kp)->kp_proc.p_stat; \
|
|
|
|
(p)->p_pid = (kp)->kp_proc.p_pid; \
|
|
|
|
(p)->p_paddr = (kp)->kp_eproc.e_paddr; \
|
|
|
|
(p)->p_vmspace = (kp)->kp_proc.p_vmspace; \
|
|
|
|
} while (/*CONSTCOND*/0);
|
|
|
|
|
|
|
|
#define KP2TOMINI(kp, p) \
|
2003-02-27 02:04:37 +03:00
|
|
|
do { \
|
2000-05-26 06:42:21 +04:00
|
|
|
(p)->p_stat = (kp)->p_stat; \
|
|
|
|
(p)->p_pid = (kp)->p_pid; \
|
|
|
|
(p)->p_paddr = (void *)(long)(kp)->p_paddr; \
|
|
|
|
(p)->p_vmspace = (void *)(long)(kp)->p_vmspace; \
|
|
|
|
} while (/*CONSTCOND*/0);
|
|
|
|
|
2007-02-24 23:41:33 +03:00
|
|
|
/*
|
|
|
|
* NetBSD uses kauth(9) to manage credentials, which are stored in kauth_cred_t,
|
|
|
|
* a kernel-only opaque type. This is an embedded version which is *INTERNAL* to
|
|
|
|
* kvm(3) so dumps can be read properly.
|
|
|
|
*
|
|
|
|
* Whenever NetBSD starts exporting credentials to userland consistently (using
|
|
|
|
* 'struct uucred', or something) this will have to be updated again.
|
|
|
|
*/
|
|
|
|
struct kvm_kauth_cred {
|
|
|
|
u_int cr_refcnt; /* reference count */
|
2008-02-05 18:56:01 +03:00
|
|
|
uint8_t cr_pad[CACHE_LINE_SIZE - sizeof(u_int)];
|
2007-02-24 23:41:33 +03:00
|
|
|
uid_t cr_uid; /* user id */
|
|
|
|
uid_t cr_euid; /* effective user id */
|
|
|
|
uid_t cr_svuid; /* saved effective user id */
|
|
|
|
gid_t cr_gid; /* group id */
|
|
|
|
gid_t cr_egid; /* effective group id */
|
|
|
|
gid_t cr_svgid; /* saved effective group id */
|
|
|
|
u_int cr_ngroups; /* number of groups */
|
|
|
|
gid_t cr_groups[NGROUPS]; /* group memberships */
|
|
|
|
specificdata_reference cr_sd; /* specific data */
|
|
|
|
};
|
|
|
|
|
2000-05-26 06:42:21 +04:00
|
|
|
/* XXX: What uses these two functions? */
|
2010-09-19 06:07:00 +04:00
|
|
|
char *_kvm_uread(kvm_t *, const struct proc *, u_long, u_long *);
|
|
|
|
ssize_t kvm_uread(kvm_t *, const struct proc *, u_long, char *,
|
|
|
|
size_t);
|
|
|
|
|
|
|
|
static char *_kvm_ureadm(kvm_t *, const struct miniproc *, u_long,
|
|
|
|
u_long *);
|
|
|
|
static ssize_t kvm_ureadm(kvm_t *, const struct miniproc *, u_long,
|
|
|
|
char *, size_t);
|
|
|
|
|
|
|
|
static char **kvm_argv(kvm_t *, const struct miniproc *, u_long, int, int);
|
|
|
|
static int kvm_deadprocs(kvm_t *, int, int, u_long, u_long, int);
|
|
|
|
static char **kvm_doargv(kvm_t *, const struct miniproc *, int,
|
|
|
|
void (*)(struct ps_strings *, u_long *, int *));
|
|
|
|
static char **kvm_doargv2(kvm_t *, pid_t, int, int);
|
|
|
|
static int kvm_proclist(kvm_t *, int, int, struct proc *,
|
|
|
|
struct kinfo_proc *, int);
|
|
|
|
static int proc_verify(kvm_t *, u_long, const struct miniproc *);
|
|
|
|
static void ps_str_a(struct ps_strings *, u_long *, int *);
|
|
|
|
static void ps_str_e(struct ps_strings *, u_long *, int *);
|
1994-05-24 02:22:24 +04:00
|
|
|
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
static char *
|
2010-09-19 06:07:00 +04:00
|
|
|
_kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long va, u_long *cnt)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
u_long addr, head;
|
|
|
|
u_long offset;
|
1994-05-09 07:15:36 +04:00
|
|
|
struct vm_map_entry vme;
|
1998-02-12 09:55:29 +03:00
|
|
|
struct vm_amap amap;
|
|
|
|
struct vm_anon *anonp, anon;
|
|
|
|
struct vm_page pg;
|
1998-09-27 22:15:58 +04:00
|
|
|
u_long slot;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->swapspc == NULL) {
|
2006-02-16 23:48:42 +03:00
|
|
|
kd->swapspc = _kvm_malloc(kd, (size_t)kd->nbpg);
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->swapspc == NULL)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-09-18 04:22:22 +04:00
|
|
|
}
|
1995-01-09 11:59:25 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
/*
|
|
|
|
* Look through the address map for the memory object
|
|
|
|
* that corresponds to the given virtual address.
|
|
|
|
* The header just has the entire valid range.
|
|
|
|
*/
|
1995-01-09 11:59:25 +03:00
|
|
|
head = (u_long)&p->p_vmspace->vm_map.header;
|
1994-05-09 07:15:36 +04:00
|
|
|
addr = head;
|
2007-07-10 02:28:13 +04:00
|
|
|
for (;;) {
|
1994-05-24 02:22:24 +04:00
|
|
|
if (KREAD(kd, addr, &vme))
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
|
1998-02-12 09:55:29 +03:00
|
|
|
if (va >= vme.start && va < vme.end &&
|
|
|
|
vme.aref.ar_amap != NULL)
|
|
|
|
break;
|
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
addr = (u_long)vme.next;
|
1994-05-24 02:22:24 +04:00
|
|
|
if (addr == head)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
1998-02-12 09:55:29 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* we found the map entry, now to find the object...
|
|
|
|
*/
|
|
|
|
if (vme.aref.ar_amap == NULL)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1998-02-12 09:55:29 +03:00
|
|
|
|
|
|
|
addr = (u_long)vme.aref.ar_amap;
|
|
|
|
if (KREAD(kd, addr, &amap))
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1998-02-12 09:55:29 +03:00
|
|
|
|
|
|
|
offset = va - vme.start;
|
1999-01-25 06:38:57 +03:00
|
|
|
slot = offset / kd->nbpg + vme.aref.ar_pageoff;
|
1998-02-12 09:55:29 +03:00
|
|
|
/* sanity-check slot number */
|
2003-02-27 02:04:37 +03:00
|
|
|
if (slot > amap.am_nslot)
|
|
|
|
return (NULL);
|
1998-02-12 09:55:29 +03:00
|
|
|
|
|
|
|
addr = (u_long)amap.am_anon + (offset / kd->nbpg) * sizeof(anonp);
|
|
|
|
if (KREAD(kd, addr, &anonp))
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-24 02:22:24 +04:00
|
|
|
|
1998-02-12 09:55:29 +03:00
|
|
|
addr = (u_long)anonp;
|
|
|
|
if (KREAD(kd, addr, &anon))
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1998-02-12 09:55:29 +03:00
|
|
|
|
2005-05-11 21:41:52 +04:00
|
|
|
addr = (u_long)anon.an_page;
|
1998-02-12 09:55:29 +03:00
|
|
|
if (addr) {
|
|
|
|
if (KREAD(kd, addr, &pg))
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1998-02-12 09:55:29 +03:00
|
|
|
|
2008-01-15 16:57:41 +03:00
|
|
|
if (_kvm_pread(kd, kd->pmfd, kd->swapspc, (size_t)kd->nbpg,
|
1998-07-01 00:29:39 +04:00
|
|
|
(off_t)pg.phys_addr) != kd->nbpg)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
|
|
|
} else {
|
2005-07-30 20:32:29 +04:00
|
|
|
if (kd->swfd < 0 ||
|
2008-01-15 16:57:41 +03:00
|
|
|
_kvm_pread(kd, kd->swfd, kd->swapspc, (size_t)kd->nbpg,
|
1998-07-01 00:29:39 +04:00
|
|
|
(off_t)(anon.an_swslot * kd->nbpg)) != kd->nbpg)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1998-02-12 09:55:29 +03:00
|
|
|
}
|
1995-01-09 11:59:25 +03:00
|
|
|
|
1994-05-24 02:22:24 +04:00
|
|
|
/* Found the page. */
|
1994-09-18 05:52:01 +04:00
|
|
|
offset %= kd->nbpg;
|
|
|
|
*cnt = kd->nbpg - offset;
|
1998-09-27 22:15:58 +04:00
|
|
|
return (&kd->swapspc[(size_t)offset]);
|
1994-05-24 02:22:24 +04:00
|
|
|
}
|
|
|
|
|
2000-05-26 06:42:21 +04:00
|
|
|
char *
|
2010-09-19 06:07:00 +04:00
|
|
|
_kvm_uread(kvm_t *kd, const struct proc *p, u_long va, u_long *cnt)
|
2000-05-26 06:42:21 +04:00
|
|
|
{
|
|
|
|
struct miniproc mp;
|
|
|
|
|
|
|
|
PTOMINI(p, &mp);
|
|
|
|
return (_kvm_ureadm(kd, &mp, va, cnt));
|
|
|
|
}
|
|
|
|
|
2007-02-07 00:21:51 +03:00
|
|
|
/*
|
|
|
|
* Convert credentials located in kernel space address 'cred' and store
|
|
|
|
* them in the appropriate members of 'eproc'.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
_kvm_convertcred(kvm_t *kd, u_long cred, struct eproc *eproc)
|
|
|
|
{
|
2007-02-24 23:41:33 +03:00
|
|
|
struct kvm_kauth_cred kauthcred;
|
2007-02-18 18:22:44 +03:00
|
|
|
struct ki_pcred *pc = &eproc->e_pcred;
|
|
|
|
struct ki_ucred *uc = &eproc->e_ucred;
|
2007-02-07 00:21:51 +03:00
|
|
|
|
|
|
|
if (KREAD(kd, cred, &kauthcred) != 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* inlined version of kauth_cred_to_pcred, see kauth(9). */
|
|
|
|
pc->p_ruid = kauthcred.cr_uid;
|
|
|
|
pc->p_svuid = kauthcred.cr_svuid;
|
|
|
|
pc->p_rgid = kauthcred.cr_gid;
|
|
|
|
pc->p_svgid = kauthcred.cr_svgid;
|
|
|
|
pc->p_refcnt = kauthcred.cr_refcnt;
|
2007-02-18 18:22:44 +03:00
|
|
|
pc->p_pad = NULL;
|
2007-02-07 00:21:51 +03:00
|
|
|
|
|
|
|
/* inlined version of kauth_cred_to_ucred(), see kauth(9). */
|
|
|
|
uc->cr_ref = kauthcred.cr_refcnt;
|
|
|
|
uc->cr_uid = kauthcred.cr_euid;
|
|
|
|
uc->cr_gid = kauthcred.cr_egid;
|
2007-07-06 18:25:42 +04:00
|
|
|
uc->cr_ngroups = (uint32_t)MIN(kauthcred.cr_ngroups,
|
2007-02-07 00:21:51 +03:00
|
|
|
sizeof(uc->cr_groups) / sizeof(uc->cr_groups[0]));
|
|
|
|
memcpy(uc->cr_groups, kauthcred.cr_groups,
|
|
|
|
uc->cr_ngroups * sizeof(uc->cr_groups[0]));
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
/*
|
|
|
|
* Read proc's from memory file into buffer bp, which has space to hold
|
|
|
|
* at most maxcnt procs.
|
|
|
|
*/
|
|
|
|
static int
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_proclist(kvm_t *kd, int what, int arg, struct proc *p,
|
|
|
|
struct kinfo_proc *bp, int maxcnt)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
int cnt = 0;
|
2003-01-18 13:32:11 +03:00
|
|
|
int nlwps;
|
|
|
|
struct kinfo_lwp *kl;
|
1994-05-09 07:15:36 +04:00
|
|
|
struct eproc eproc;
|
|
|
|
struct pgrp pgrp;
|
|
|
|
struct session sess;
|
|
|
|
struct tty tty;
|
|
|
|
struct proc proc;
|
|
|
|
|
1994-08-30 21:39:11 +04:00
|
|
|
for (; cnt < maxcnt && p != NULL; p = proc.p_list.le_next) {
|
1994-05-09 07:15:36 +04:00
|
|
|
if (KREAD(kd, (u_long)p, &proc)) {
|
2000-10-04 20:11:27 +04:00
|
|
|
_kvm_err(kd, kd->program, "can't read proc at %p", p);
|
1994-05-09 07:15:36 +04:00
|
|
|
return (-1);
|
|
|
|
}
|
2007-02-07 00:21:51 +03:00
|
|
|
if (_kvm_convertcred(kd, (u_long)proc.p_cred, &eproc) != 0) {
|
|
|
|
_kvm_err(kd, kd->program,
|
|
|
|
"can't read proc credentials at %p", p);
|
|
|
|
return (-1);
|
|
|
|
}
|
1994-05-09 07:15:36 +04:00
|
|
|
|
2003-02-27 02:04:37 +03:00
|
|
|
switch (what) {
|
1999-07-02 19:28:49 +04:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
case KERN_PROC_PID:
|
|
|
|
if (proc.p_pid != (pid_t)arg)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KERN_PROC_UID:
|
|
|
|
if (eproc.e_ucred.cr_uid != (uid_t)arg)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KERN_PROC_RUID:
|
|
|
|
if (eproc.e_pcred.p_ruid != (uid_t)arg)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We're going to add another proc to the set. If this
|
|
|
|
* will overflow the buffer, assume the reason is because
|
|
|
|
* nprocs (or the proc list) is corrupt and declare an error.
|
|
|
|
*/
|
|
|
|
if (cnt >= maxcnt) {
|
|
|
|
_kvm_err(kd, kd->program, "nprocs corrupt");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* gather eproc
|
|
|
|
*/
|
|
|
|
eproc.e_paddr = p;
|
|
|
|
if (KREAD(kd, (u_long)proc.p_pgrp, &pgrp)) {
|
2000-10-04 20:11:27 +04:00
|
|
|
_kvm_err(kd, kd->program, "can't read pgrp at %p",
|
2003-02-27 02:04:37 +03:00
|
|
|
proc.p_pgrp);
|
1994-05-09 07:15:36 +04:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
eproc.e_sess = pgrp.pg_session;
|
|
|
|
eproc.e_pgid = pgrp.pg_id;
|
|
|
|
eproc.e_jobc = pgrp.pg_jobc;
|
|
|
|
if (KREAD(kd, (u_long)pgrp.pg_session, &sess)) {
|
2000-10-04 20:11:27 +04:00
|
|
|
_kvm_err(kd, kd->program, "can't read session at %p",
|
2003-02-27 02:04:37 +03:00
|
|
|
pgrp.pg_session);
|
1994-05-09 07:15:36 +04:00
|
|
|
return (-1);
|
|
|
|
}
|
2007-02-10 01:08:48 +03:00
|
|
|
if ((proc.p_lflag & PL_CONTROLT) && sess.s_ttyp != NULL) {
|
1994-05-09 07:15:36 +04:00
|
|
|
if (KREAD(kd, (u_long)sess.s_ttyp, &tty)) {
|
|
|
|
_kvm_err(kd, kd->program,
|
2003-02-27 02:04:37 +03:00
|
|
|
"can't read tty at %p", sess.s_ttyp);
|
1994-05-09 07:15:36 +04:00
|
|
|
return (-1);
|
|
|
|
}
|
2008-12-28 22:49:26 +03:00
|
|
|
eproc.e_tdev = (uint32_t)tty.t_dev;
|
1994-05-09 07:15:36 +04:00
|
|
|
eproc.e_tsess = tty.t_session;
|
|
|
|
if (tty.t_pgrp != NULL) {
|
|
|
|
if (KREAD(kd, (u_long)tty.t_pgrp, &pgrp)) {
|
|
|
|
_kvm_err(kd, kd->program,
|
2003-02-27 02:04:37 +03:00
|
|
|
"can't read tpgrp at %p",
|
|
|
|
tty.t_pgrp);
|
1994-05-09 07:15:36 +04:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
eproc.e_tpgid = pgrp.pg_id;
|
|
|
|
} else
|
|
|
|
eproc.e_tpgid = -1;
|
|
|
|
} else
|
2008-12-28 22:49:26 +03:00
|
|
|
eproc.e_tdev = (uint32_t)NODEV;
|
1994-05-09 07:15:36 +04:00
|
|
|
eproc.e_flag = sess.s_ttyvp ? EPROC_CTTY : 0;
|
2000-04-15 19:52:52 +04:00
|
|
|
eproc.e_sid = sess.s_sid;
|
1994-05-09 07:15:36 +04:00
|
|
|
if (sess.s_leader == p)
|
|
|
|
eproc.e_flag |= EPROC_SLEADER;
|
2003-02-27 02:04:37 +03:00
|
|
|
/*
|
|
|
|
* Fill in the old-style proc.p_wmesg by copying the wmesg
|
2003-09-29 13:50:21 +04:00
|
|
|
* from the first available LWP.
|
2003-01-18 13:32:11 +03:00
|
|
|
*/
|
2003-02-02 05:29:59 +03:00
|
|
|
kl = kvm_getlwps(kd, proc.p_pid,
|
2004-02-19 06:57:56 +03:00
|
|
|
(u_long)PTRTOUINT64(eproc.e_paddr),
|
2003-01-18 13:32:11 +03:00
|
|
|
sizeof(struct kinfo_lwp), &nlwps);
|
|
|
|
if (kl) {
|
|
|
|
if (nlwps > 0) {
|
|
|
|
strcpy(eproc.e_wmesg, kl[0].l_wmesg);
|
|
|
|
}
|
|
|
|
}
|
2000-05-26 06:42:21 +04:00
|
|
|
(void)kvm_read(kd, (u_long)proc.p_vmspace, &eproc.e_vm,
|
|
|
|
sizeof(eproc.e_vm));
|
1995-01-12 19:30:52 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
eproc.e_xsize = eproc.e_xrssize = 0;
|
|
|
|
eproc.e_xccount = eproc.e_xswrss = 0;
|
|
|
|
|
|
|
|
switch (what) {
|
|
|
|
|
|
|
|
case KERN_PROC_PGRP:
|
|
|
|
if (eproc.e_pgid != (pid_t)arg)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KERN_PROC_TTY:
|
2007-02-10 01:08:48 +03:00
|
|
|
if ((proc.p_lflag & PL_CONTROLT) == 0 ||
|
2003-02-27 02:04:37 +03:00
|
|
|
eproc.e_tdev != (dev_t)arg)
|
1994-05-09 07:15:36 +04:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
1998-08-10 06:43:08 +04:00
|
|
|
memcpy(&bp->kp_proc, &proc, sizeof(proc));
|
|
|
|
memcpy(&bp->kp_eproc, &eproc, sizeof(eproc));
|
1994-05-09 07:15:36 +04:00
|
|
|
++bp;
|
|
|
|
++cnt;
|
|
|
|
}
|
|
|
|
return (cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build proc info array by reading in proc list from a crash dump.
|
|
|
|
* Return number of procs read. maxcnt is the max we will read.
|
|
|
|
*/
|
|
|
|
static int
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_deadprocs(kvm_t *kd, int what, int arg, u_long a_allproc,
|
|
|
|
u_long a_zombproc, int maxcnt)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
struct kinfo_proc *bp = kd->procbase;
|
2003-03-28 17:01:32 +03:00
|
|
|
int acnt, zcnt;
|
1994-05-09 07:15:36 +04:00
|
|
|
struct proc *p;
|
|
|
|
|
|
|
|
if (KREAD(kd, a_allproc, &p)) {
|
|
|
|
_kvm_err(kd, kd->program, "cannot read allproc");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
acnt = kvm_proclist(kd, what, arg, p, bp, maxcnt);
|
|
|
|
if (acnt < 0)
|
|
|
|
return (acnt);
|
|
|
|
|
|
|
|
if (KREAD(kd, a_zombproc, &p)) {
|
|
|
|
_kvm_err(kd, kd->program, "cannot read zombproc");
|
|
|
|
return (-1);
|
|
|
|
}
|
1998-09-09 04:31:25 +04:00
|
|
|
zcnt = kvm_proclist(kd, what, arg, p, bp + acnt,
|
2003-03-28 17:01:32 +03:00
|
|
|
maxcnt - acnt);
|
1994-05-09 07:15:36 +04:00
|
|
|
if (zcnt < 0)
|
|
|
|
zcnt = 0;
|
|
|
|
|
|
|
|
return (acnt + zcnt);
|
|
|
|
}
|
|
|
|
|
2000-05-26 06:42:21 +04:00
|
|
|
struct kinfo_proc2 *
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_getproc2(kvm_t *kd, int op, int arg, size_t esize, int *cnt)
|
2000-05-26 06:42:21 +04:00
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
int mib[6], st, nprocs;
|
2003-01-18 13:32:11 +03:00
|
|
|
struct pstats pstats;
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
if (ISSYSCTL(kd)) {
|
|
|
|
size = 0;
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROC2;
|
|
|
|
mib[2] = op;
|
|
|
|
mib[3] = arg;
|
2003-03-21 01:53:12 +03:00
|
|
|
mib[4] = (int)esize;
|
2006-12-31 19:20:18 +03:00
|
|
|
again:
|
2000-05-26 06:42:21 +04:00
|
|
|
mib[5] = 0;
|
2003-03-21 01:53:12 +03:00
|
|
|
st = sysctl(mib, 6, NULL, &size, NULL, (size_t)0);
|
2000-05-26 06:42:21 +04:00
|
|
|
if (st == -1) {
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getproc2");
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2000-05-26 06:42:21 +04:00
|
|
|
}
|
|
|
|
|
2003-03-21 01:53:12 +03:00
|
|
|
mib[5] = (int) (size / esize);
|
2006-02-16 23:48:42 +03:00
|
|
|
KVM_ALLOC(kd, procbase2, size);
|
2003-03-21 01:53:12 +03:00
|
|
|
st = sysctl(mib, 6, kd->procbase2, &size, NULL, (size_t)0);
|
2000-05-26 06:42:21 +04:00
|
|
|
if (st == -1) {
|
2006-12-31 19:20:18 +03:00
|
|
|
if (errno == ENOMEM) {
|
|
|
|
goto again;
|
|
|
|
}
|
2000-05-26 06:42:21 +04:00
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getproc2");
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2000-05-26 06:42:21 +04:00
|
|
|
}
|
2003-03-21 01:53:12 +03:00
|
|
|
nprocs = (int) (size / esize);
|
2000-05-26 06:42:21 +04:00
|
|
|
} else {
|
|
|
|
char *kp2c;
|
|
|
|
struct kinfo_proc *kp;
|
|
|
|
struct kinfo_proc2 kp2, *kp2p;
|
2003-01-18 13:32:11 +03:00
|
|
|
struct kinfo_lwp *kl;
|
|
|
|
int i, nlwps;
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
kp = kvm_getprocs(kd, op, arg, &nprocs);
|
|
|
|
if (kp == NULL)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2006-02-16 23:48:42 +03:00
|
|
|
size = nprocs * esize;
|
|
|
|
KVM_ALLOC(kd, procbase2, size);
|
2000-07-16 06:04:11 +04:00
|
|
|
kp2c = (char *)(void *)kd->procbase2;
|
2000-05-26 06:42:21 +04:00
|
|
|
kp2p = &kp2;
|
|
|
|
for (i = 0; i < nprocs; i++, kp++) {
|
2007-12-22 04:22:03 +03:00
|
|
|
struct timeval tv;
|
|
|
|
|
2003-02-27 02:04:37 +03:00
|
|
|
kl = kvm_getlwps(kd, kp->kp_proc.p_pid,
|
2004-02-19 06:57:56 +03:00
|
|
|
(u_long)PTRTOUINT64(kp->kp_eproc.e_paddr),
|
2003-01-18 13:32:11 +03:00
|
|
|
sizeof(struct kinfo_lwp), &nlwps);
|
2007-01-29 00:31:13 +03:00
|
|
|
|
2008-11-29 21:24:58 +03:00
|
|
|
if (kl == NULL) {
|
|
|
|
_kvm_syserr(kd, NULL,
|
|
|
|
"kvm_getlwps() failed on process %u\n",
|
|
|
|
kp->kp_proc.p_pid);
|
|
|
|
if (nlwps == 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
/* We use kl[0] as the "representative" LWP */
|
2000-05-26 06:42:21 +04:00
|
|
|
memset(kp2p, 0, sizeof(kp2));
|
2003-01-18 13:32:11 +03:00
|
|
|
kp2p->p_forw = kl[0].l_forw;
|
|
|
|
kp2p->p_back = kl[0].l_back;
|
2004-02-19 06:57:56 +03:00
|
|
|
kp2p->p_paddr = PTRTOUINT64(kp->kp_eproc.e_paddr);
|
2003-01-18 13:32:11 +03:00
|
|
|
kp2p->p_addr = kl[0].l_addr;
|
2004-02-19 06:57:56 +03:00
|
|
|
kp2p->p_fd = PTRTOUINT64(kp->kp_proc.p_fd);
|
|
|
|
kp2p->p_cwdi = PTRTOUINT64(kp->kp_proc.p_cwdi);
|
|
|
|
kp2p->p_stats = PTRTOUINT64(kp->kp_proc.p_stats);
|
|
|
|
kp2p->p_limit = PTRTOUINT64(kp->kp_proc.p_limit);
|
|
|
|
kp2p->p_vmspace = PTRTOUINT64(kp->kp_proc.p_vmspace);
|
|
|
|
kp2p->p_sigacts = PTRTOUINT64(kp->kp_proc.p_sigacts);
|
|
|
|
kp2p->p_sess = PTRTOUINT64(kp->kp_eproc.e_sess);
|
2000-05-26 06:42:21 +04:00
|
|
|
kp2p->p_tsess = 0;
|
2007-05-01 10:58:08 +04:00
|
|
|
#if 1 /* XXX: dsl - p_ru was only ever non-zero for zombies */
|
|
|
|
kp2p->p_ru = 0;
|
|
|
|
#else
|
|
|
|
kp2p->p_ru = PTRTOUINT64(pstats.p_ru);
|
|
|
|
#endif
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
kp2p->p_eflag = 0;
|
|
|
|
kp2p->p_exitsig = kp->kp_proc.p_exitsig;
|
|
|
|
kp2p->p_flag = kp->kp_proc.p_flag;
|
|
|
|
|
|
|
|
kp2p->p_pid = kp->kp_proc.p_pid;
|
|
|
|
|
|
|
|
kp2p->p_ppid = kp->kp_eproc.e_ppid;
|
|
|
|
kp2p->p_sid = kp->kp_eproc.e_sid;
|
|
|
|
kp2p->p__pgid = kp->kp_eproc.e_pgid;
|
|
|
|
|
2003-03-19 14:36:32 +03:00
|
|
|
kp2p->p_tpgid = -1 /* XXX NO_PGID! */;
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
kp2p->p_uid = kp->kp_eproc.e_ucred.cr_uid;
|
|
|
|
kp2p->p_ruid = kp->kp_eproc.e_pcred.p_ruid;
|
2003-03-01 08:41:55 +03:00
|
|
|
kp2p->p_svuid = kp->kp_eproc.e_pcred.p_svuid;
|
2000-05-26 06:42:21 +04:00
|
|
|
kp2p->p_gid = kp->kp_eproc.e_ucred.cr_gid;
|
|
|
|
kp2p->p_rgid = kp->kp_eproc.e_pcred.p_rgid;
|
2003-03-01 08:41:55 +03:00
|
|
|
kp2p->p_svgid = kp->kp_eproc.e_pcred.p_svgid;
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2000-07-16 06:04:11 +04:00
|
|
|
/*CONSTCOND*/
|
2000-05-26 06:42:21 +04:00
|
|
|
memcpy(kp2p->p_groups, kp->kp_eproc.e_ucred.cr_groups,
|
2003-02-27 02:04:37 +03:00
|
|
|
MIN(sizeof(kp2p->p_groups),
|
|
|
|
sizeof(kp->kp_eproc.e_ucred.cr_groups)));
|
2000-05-26 06:42:21 +04:00
|
|
|
kp2p->p_ngroups = kp->kp_eproc.e_ucred.cr_ngroups;
|
|
|
|
|
|
|
|
kp2p->p_jobc = kp->kp_eproc.e_jobc;
|
|
|
|
kp2p->p_tdev = kp->kp_eproc.e_tdev;
|
|
|
|
kp2p->p_tpgid = kp->kp_eproc.e_tpgid;
|
2004-02-19 06:57:56 +03:00
|
|
|
kp2p->p_tsess = PTRTOUINT64(kp->kp_eproc.e_tsess);
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2007-11-06 04:46:08 +03:00
|
|
|
kp2p->p_estcpu = 0;
|
2007-12-22 04:22:03 +03:00
|
|
|
bintime2timeval(&kp->kp_proc.p_rtime, &tv);
|
|
|
|
kp2p->p_rtime_sec = (uint32_t)tv.tv_sec;
|
|
|
|
kp2p->p_rtime_usec = (uint32_t)tv.tv_usec;
|
2007-05-18 01:42:32 +04:00
|
|
|
kp2p->p_cpticks = kl[0].l_cpticks;
|
2000-05-26 06:42:21 +04:00
|
|
|
kp2p->p_pctcpu = kp->kp_proc.p_pctcpu;
|
2003-01-18 13:32:11 +03:00
|
|
|
kp2p->p_swtime = kl[0].l_swtime;
|
|
|
|
kp2p->p_slptime = kl[0].l_slptime;
|
2000-05-27 05:03:00 +04:00
|
|
|
#if 0 /* XXX thorpej */
|
2000-05-26 06:42:21 +04:00
|
|
|
kp2p->p_schedflags = kp->kp_proc.p_schedflags;
|
2000-05-27 05:03:00 +04:00
|
|
|
#else
|
|
|
|
kp2p->p_schedflags = 0;
|
|
|
|
#endif
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
kp2p->p_uticks = kp->kp_proc.p_uticks;
|
|
|
|
kp2p->p_sticks = kp->kp_proc.p_sticks;
|
|
|
|
kp2p->p_iticks = kp->kp_proc.p_iticks;
|
|
|
|
|
2004-02-19 06:57:56 +03:00
|
|
|
kp2p->p_tracep = PTRTOUINT64(kp->kp_proc.p_tracep);
|
2000-05-26 06:42:21 +04:00
|
|
|
kp2p->p_traceflag = kp->kp_proc.p_traceflag;
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
kp2p->p_holdcnt = kl[0].l_holdcnt;
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2003-02-27 02:04:37 +03:00
|
|
|
memcpy(&kp2p->p_siglist,
|
2007-02-10 01:08:48 +03:00
|
|
|
&kp->kp_proc.p_sigpend.sp_set,
|
2003-02-27 02:04:37 +03:00
|
|
|
sizeof(ki_sigset_t));
|
2007-02-10 01:08:48 +03:00
|
|
|
memset(&kp2p->p_sigmask, 0,
|
2003-02-27 02:04:37 +03:00
|
|
|
sizeof(ki_sigset_t));
|
|
|
|
memcpy(&kp2p->p_sigignore,
|
|
|
|
&kp->kp_proc.p_sigctx.ps_sigignore,
|
|
|
|
sizeof(ki_sigset_t));
|
|
|
|
memcpy(&kp2p->p_sigcatch,
|
|
|
|
&kp->kp_proc.p_sigctx.ps_sigcatch,
|
|
|
|
sizeof(ki_sigset_t));
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2007-01-29 00:31:13 +03:00
|
|
|
kp2p->p_stat = kl[0].l_stat;
|
2003-01-18 13:32:11 +03:00
|
|
|
kp2p->p_priority = kl[0].l_priority;
|
2007-11-06 04:46:08 +03:00
|
|
|
kp2p->p_usrpri = kl[0].l_priority;
|
2000-05-26 06:42:21 +04:00
|
|
|
kp2p->p_nice = kp->kp_proc.p_nice;
|
|
|
|
|
2016-04-05 00:08:44 +03:00
|
|
|
kp2p->p_xstat = P_WAITSTATUS(&kp->kp_proc);
|
2000-05-26 06:42:21 +04:00
|
|
|
kp2p->p_acflag = kp->kp_proc.p_acflag;
|
|
|
|
|
2000-07-16 06:04:11 +04:00
|
|
|
/*CONSTCOND*/
|
2000-05-26 06:42:21 +04:00
|
|
|
strncpy(kp2p->p_comm, kp->kp_proc.p_comm,
|
2003-02-27 02:04:37 +03:00
|
|
|
MIN(sizeof(kp2p->p_comm),
|
|
|
|
sizeof(kp->kp_proc.p_comm)));
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2003-02-27 02:04:37 +03:00
|
|
|
strncpy(kp2p->p_wmesg, kp->kp_eproc.e_wmesg,
|
|
|
|
sizeof(kp2p->p_wmesg));
|
2003-01-18 13:32:11 +03:00
|
|
|
kp2p->p_wchan = kl[0].l_wchan;
|
2003-02-27 02:04:37 +03:00
|
|
|
strncpy(kp2p->p_login, kp->kp_eproc.e_login,
|
|
|
|
sizeof(kp2p->p_login));
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
kp2p->p_vm_rssize = kp->kp_eproc.e_xrssize;
|
|
|
|
kp2p->p_vm_tsize = kp->kp_eproc.e_vm.vm_tsize;
|
|
|
|
kp2p->p_vm_dsize = kp->kp_eproc.e_vm.vm_dsize;
|
|
|
|
kp2p->p_vm_ssize = kp->kp_eproc.e_vm.vm_ssize;
|
2012-06-05 12:23:05 +04:00
|
|
|
kp2p->p_vm_vsize = kp->kp_eproc.e_vm.vm_map.size
|
|
|
|
/ kd->nbpg;
|
- add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.
- adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.
- add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)
- patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)
- patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.
- update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)
this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.
tested on i386 and sparc64, build tested on several other platforms.
thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
2009-03-29 05:02:48 +04:00
|
|
|
/* Adjust mapped size */
|
|
|
|
kp2p->p_vm_msize =
|
|
|
|
(kp->kp_eproc.e_vm.vm_map.size / kd->nbpg) -
|
|
|
|
kp->kp_eproc.e_vm.vm_issize +
|
|
|
|
kp->kp_eproc.e_vm.vm_ssize;
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2000-07-16 06:04:11 +04:00
|
|
|
kp2p->p_eflag = (int32_t)kp->kp_eproc.e_flag;
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
kp2p->p_realflag = kp->kp_proc.p_flag;
|
|
|
|
kp2p->p_nlwps = kp->kp_proc.p_nlwps;
|
|
|
|
kp2p->p_nrlwps = kp->kp_proc.p_nrlwps;
|
|
|
|
kp2p->p_realstat = kp->kp_proc.p_stat;
|
|
|
|
|
2003-02-27 02:04:37 +03:00
|
|
|
if (P_ZOMBIE(&kp->kp_proc) ||
|
2003-01-18 13:32:11 +03:00
|
|
|
kp->kp_proc.p_stats == NULL ||
|
2003-02-27 02:04:37 +03:00
|
|
|
KREAD(kd, (u_long)kp->kp_proc.p_stats, &pstats)) {
|
2000-05-26 06:42:21 +04:00
|
|
|
kp2p->p_uvalid = 0;
|
|
|
|
} else {
|
|
|
|
kp2p->p_uvalid = 1;
|
|
|
|
|
2000-07-16 06:04:11 +04:00
|
|
|
kp2p->p_ustart_sec = (u_int32_t)
|
2003-01-18 13:32:11 +03:00
|
|
|
pstats.p_start.tv_sec;
|
2000-07-16 06:04:11 +04:00
|
|
|
kp2p->p_ustart_usec = (u_int32_t)
|
2003-01-18 13:32:11 +03:00
|
|
|
pstats.p_start.tv_usec;
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2000-07-16 06:04:11 +04:00
|
|
|
kp2p->p_uutime_sec = (u_int32_t)
|
2003-01-18 13:32:11 +03:00
|
|
|
pstats.p_ru.ru_utime.tv_sec;
|
2000-07-16 06:04:11 +04:00
|
|
|
kp2p->p_uutime_usec = (u_int32_t)
|
2003-01-18 13:32:11 +03:00
|
|
|
pstats.p_ru.ru_utime.tv_usec;
|
2000-07-16 06:04:11 +04:00
|
|
|
kp2p->p_ustime_sec = (u_int32_t)
|
2003-01-18 13:32:11 +03:00
|
|
|
pstats.p_ru.ru_stime.tv_sec;
|
2000-07-16 06:04:11 +04:00
|
|
|
kp2p->p_ustime_usec = (u_int32_t)
|
2003-01-18 13:32:11 +03:00
|
|
|
pstats.p_ru.ru_stime.tv_usec;
|
|
|
|
|
|
|
|
kp2p->p_uru_maxrss = pstats.p_ru.ru_maxrss;
|
|
|
|
kp2p->p_uru_ixrss = pstats.p_ru.ru_ixrss;
|
|
|
|
kp2p->p_uru_idrss = pstats.p_ru.ru_idrss;
|
|
|
|
kp2p->p_uru_isrss = pstats.p_ru.ru_isrss;
|
|
|
|
kp2p->p_uru_minflt = pstats.p_ru.ru_minflt;
|
|
|
|
kp2p->p_uru_majflt = pstats.p_ru.ru_majflt;
|
|
|
|
kp2p->p_uru_nswap = pstats.p_ru.ru_nswap;
|
|
|
|
kp2p->p_uru_inblock = pstats.p_ru.ru_inblock;
|
|
|
|
kp2p->p_uru_oublock = pstats.p_ru.ru_oublock;
|
|
|
|
kp2p->p_uru_msgsnd = pstats.p_ru.ru_msgsnd;
|
|
|
|
kp2p->p_uru_msgrcv = pstats.p_ru.ru_msgrcv;
|
|
|
|
kp2p->p_uru_nsignals = pstats.p_ru.ru_nsignals;
|
|
|
|
kp2p->p_uru_nvcsw = pstats.p_ru.ru_nvcsw;
|
|
|
|
kp2p->p_uru_nivcsw = pstats.p_ru.ru_nivcsw;
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2000-07-16 06:04:11 +04:00
|
|
|
kp2p->p_uctime_sec = (u_int32_t)
|
2003-01-18 13:32:11 +03:00
|
|
|
(pstats.p_cru.ru_utime.tv_sec +
|
|
|
|
pstats.p_cru.ru_stime.tv_sec);
|
2000-07-16 06:04:11 +04:00
|
|
|
kp2p->p_uctime_usec = (u_int32_t)
|
2003-01-18 13:32:11 +03:00
|
|
|
(pstats.p_cru.ru_utime.tv_usec +
|
|
|
|
pstats.p_cru.ru_stime.tv_usec);
|
2000-05-26 06:42:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(kp2c, &kp2, esize);
|
|
|
|
kp2c += esize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*cnt = nprocs;
|
|
|
|
return (kd->procbase2);
|
|
|
|
}
|
|
|
|
|
2003-01-18 13:32:11 +03:00
|
|
|
struct kinfo_lwp *
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_getlwps(kvm_t *kd, int pid, u_long paddr, size_t esize, int *cnt)
|
2003-01-18 13:32:11 +03:00
|
|
|
{
|
|
|
|
size_t size;
|
2003-03-21 01:53:12 +03:00
|
|
|
int mib[5], nlwps;
|
|
|
|
ssize_t st;
|
2003-01-18 13:32:11 +03:00
|
|
|
struct kinfo_lwp *kl;
|
|
|
|
|
|
|
|
if (ISSYSCTL(kd)) {
|
|
|
|
size = 0;
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_LWP;
|
|
|
|
mib[2] = pid;
|
2003-03-21 01:53:12 +03:00
|
|
|
mib[3] = (int)esize;
|
2003-01-18 13:32:11 +03:00
|
|
|
mib[4] = 0;
|
2007-07-06 18:25:42 +04:00
|
|
|
again:
|
2003-03-21 01:53:12 +03:00
|
|
|
st = sysctl(mib, 5, NULL, &size, NULL, (size_t)0);
|
2003-01-18 13:32:11 +03:00
|
|
|
if (st == -1) {
|
2007-07-06 18:25:42 +04:00
|
|
|
switch (errno) {
|
2007-07-07 22:27:26 +04:00
|
|
|
case ESRCH: /* Treat this as a soft error; see kvm.c */
|
|
|
|
_kvm_syserr(kd, NULL, "kvm_getlwps");
|
2007-07-06 18:25:42 +04:00
|
|
|
return NULL;
|
|
|
|
default:
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getlwps");
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
2003-03-21 01:53:12 +03:00
|
|
|
mib[4] = (int) (size / esize);
|
2006-02-16 23:48:42 +03:00
|
|
|
KVM_ALLOC(kd, lwpbase, size);
|
2003-03-21 01:53:12 +03:00
|
|
|
st = sysctl(mib, 5, kd->lwpbase, &size, NULL, (size_t)0);
|
2003-01-18 13:32:11 +03:00
|
|
|
if (st == -1) {
|
2007-07-06 18:25:42 +04:00
|
|
|
switch (errno) {
|
2007-07-07 22:27:26 +04:00
|
|
|
case ESRCH: /* Treat this as a soft error; see kvm.c */
|
|
|
|
_kvm_syserr(kd, NULL, "kvm_getlwps");
|
2007-07-06 18:25:42 +04:00
|
|
|
return NULL;
|
|
|
|
case ENOMEM:
|
|
|
|
goto again;
|
|
|
|
default:
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getlwps");
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
2003-03-21 01:53:12 +03:00
|
|
|
nlwps = (int) (size / esize);
|
2003-01-18 13:32:11 +03:00
|
|
|
} else {
|
|
|
|
/* grovel through the memory image */
|
|
|
|
struct proc p;
|
|
|
|
struct lwp l;
|
|
|
|
u_long laddr;
|
2007-05-18 01:42:32 +04:00
|
|
|
void *back;
|
2003-01-18 13:32:11 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
st = kvm_read(kd, paddr, &p, sizeof(p));
|
|
|
|
if (st == -1) {
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getlwps");
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nlwps = p.p_nlwps;
|
2006-02-16 23:48:42 +03:00
|
|
|
size = nlwps * sizeof(*kd->lwpbase);
|
|
|
|
KVM_ALLOC(kd, lwpbase, size);
|
2004-02-19 06:57:56 +03:00
|
|
|
laddr = (u_long)PTRTOUINT64(p.p_lwps.lh_first);
|
2003-01-18 13:32:11 +03:00
|
|
|
for (i = 0; (i < nlwps) && (laddr != 0); i++) {
|
|
|
|
st = kvm_read(kd, laddr, &l, sizeof(l));
|
|
|
|
if (st == -1) {
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getlwps");
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
kl = &kd->lwpbase[i];
|
|
|
|
kl->l_laddr = laddr;
|
2007-05-18 01:42:32 +04:00
|
|
|
kl->l_forw = PTRTOUINT64(l.l_runq.tqe_next);
|
|
|
|
laddr = (u_long)PTRTOUINT64(l.l_runq.tqe_prev);
|
|
|
|
st = kvm_read(kd, laddr, &back, sizeof(back));
|
|
|
|
if (st == -1) {
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getlwps");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
kl->l_back = PTRTOUINT64(back);
|
2004-02-19 06:57:56 +03:00
|
|
|
kl->l_addr = PTRTOUINT64(l.l_addr);
|
2003-01-18 13:32:11 +03:00
|
|
|
kl->l_lid = l.l_lid;
|
|
|
|
kl->l_flag = l.l_flag;
|
|
|
|
kl->l_swtime = l.l_swtime;
|
|
|
|
kl->l_slptime = l.l_slptime;
|
|
|
|
kl->l_schedflags = 0; /* XXX */
|
2009-10-22 01:11:57 +04:00
|
|
|
kl->l_holdcnt = 0;
|
2003-01-18 13:32:11 +03:00
|
|
|
kl->l_priority = l.l_priority;
|
2007-11-06 04:46:08 +03:00
|
|
|
kl->l_usrpri = l.l_priority;
|
2003-01-18 13:32:11 +03:00
|
|
|
kl->l_stat = l.l_stat;
|
2004-02-19 06:57:56 +03:00
|
|
|
kl->l_wchan = PTRTOUINT64(l.l_wchan);
|
2003-01-18 13:32:11 +03:00
|
|
|
if (l.l_wmesg)
|
|
|
|
(void)kvm_read(kd, (u_long)l.l_wmesg,
|
2003-03-21 01:53:12 +03:00
|
|
|
kl->l_wmesg, (size_t)WMESGLEN);
|
2003-01-18 13:32:11 +03:00
|
|
|
kl->l_cpuid = KI_NOCPU;
|
2004-02-19 06:57:56 +03:00
|
|
|
laddr = (u_long)PTRTOUINT64(l.l_sibling.le_next);
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*cnt = nlwps;
|
2003-02-27 02:04:37 +03:00
|
|
|
return (kd->lwpbase);
|
2003-01-18 13:32:11 +03:00
|
|
|
}
|
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
struct kinfo_proc *
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_getprocs(kvm_t *kd, int op, int arg, int *cnt)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1994-10-19 06:03:39 +03:00
|
|
|
size_t size;
|
|
|
|
int mib[4], st, nprocs;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
2009-05-16 15:56:47 +04:00
|
|
|
if (ISALIVE(kd)) {
|
1994-05-09 07:15:36 +04:00
|
|
|
size = 0;
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROC;
|
|
|
|
mib[2] = op;
|
|
|
|
mib[3] = arg;
|
2003-03-21 01:53:12 +03:00
|
|
|
st = sysctl(mib, 4, NULL, &size, NULL, (size_t)0);
|
1994-05-09 07:15:36 +04:00
|
|
|
if (st == -1) {
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getprocs");
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
2006-02-16 23:48:42 +03:00
|
|
|
KVM_ALLOC(kd, procbase, size);
|
2003-03-21 01:53:12 +03:00
|
|
|
st = sysctl(mib, 4, kd->procbase, &size, NULL, (size_t)0);
|
1994-05-09 07:15:36 +04:00
|
|
|
if (st == -1) {
|
|
|
|
_kvm_syserr(kd, kd->program, "kvm_getprocs");
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
|
|
|
if (size % sizeof(struct kinfo_proc) != 0) {
|
|
|
|
_kvm_err(kd, kd->program,
|
2000-10-05 07:21:01 +04:00
|
|
|
"proc size mismatch (%lu total, %lu chunks)",
|
|
|
|
(u_long)size, (u_long)sizeof(struct kinfo_proc));
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
2003-03-21 01:53:12 +03:00
|
|
|
nprocs = (int) (size / sizeof(struct kinfo_proc));
|
1994-05-09 07:15:36 +04:00
|
|
|
} else {
|
2003-03-28 17:01:32 +03:00
|
|
|
struct nlist nl[4], *p;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
2003-11-17 23:41:54 +03:00
|
|
|
(void)memset(nl, 0, sizeof(nl));
|
1994-05-09 07:15:36 +04:00
|
|
|
nl[0].n_name = "_nprocs";
|
|
|
|
nl[1].n_name = "_allproc";
|
2003-03-28 17:01:32 +03:00
|
|
|
nl[2].n_name = "_zombproc";
|
|
|
|
nl[3].n_name = NULL;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
if (kvm_nlist(kd, nl) != 0) {
|
|
|
|
for (p = nl; p->n_type != 0; ++p)
|
2003-02-27 02:04:37 +03:00
|
|
|
continue;
|
1994-05-09 07:15:36 +04:00
|
|
|
_kvm_err(kd, kd->program,
|
2003-02-27 02:04:37 +03:00
|
|
|
"%s: no such symbol", p->n_name);
|
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
|
|
|
if (KREAD(kd, nl[0].n_value, &nprocs)) {
|
|
|
|
_kvm_err(kd, kd->program, "can't read nprocs");
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
2006-02-16 23:48:42 +03:00
|
|
|
size = nprocs * sizeof(*kd->procbase);
|
|
|
|
KVM_ALLOC(kd, procbase, size);
|
1994-05-09 07:15:36 +04:00
|
|
|
nprocs = kvm_deadprocs(kd, op, arg, nl[1].n_value,
|
2003-03-28 17:01:32 +03:00
|
|
|
nl[2].n_value, nprocs);
|
2000-01-15 22:16:32 +03:00
|
|
|
if (nprocs < 0)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
#ifdef notdef
|
|
|
|
size = nprocs * sizeof(struct kinfo_proc);
|
|
|
|
(void)realloc(kd->procbase, size);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
*cnt = nprocs;
|
|
|
|
return (kd->procbase);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2010-09-19 06:07:00 +04:00
|
|
|
_kvm_realloc(kvm_t *kd, void *p, size_t n)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
2000-05-26 06:42:21 +04:00
|
|
|
void *np = realloc(p, n);
|
1994-05-09 07:15:36 +04:00
|
|
|
|
2000-06-05 03:03:27 +04:00
|
|
|
if (np == NULL)
|
1994-05-09 07:15:36 +04:00
|
|
|
_kvm_err(kd, kd->program, "out of memory");
|
|
|
|
return (np);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read in an argument vector from the user address space of process p.
|
1999-07-02 19:28:49 +04:00
|
|
|
* addr if the user-space base address of narg null-terminated contiguous
|
1994-05-09 07:15:36 +04:00
|
|
|
* strings. This is used to read in both the command arguments and
|
|
|
|
* environment strings. Read at most maxcnt characters of strings.
|
|
|
|
*/
|
|
|
|
static char **
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_argv(kvm_t *kd, const struct miniproc *p, u_long addr, int narg,
|
|
|
|
int maxcnt)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
char *np, *cp, *ep, *ap;
|
1998-09-27 22:15:58 +04:00
|
|
|
u_long oaddr = (u_long)~0L;
|
|
|
|
u_long len;
|
|
|
|
size_t cc;
|
1998-02-03 22:12:13 +03:00
|
|
|
char **argv;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
/*
|
2004-05-12 21:20:01 +04:00
|
|
|
* Check that there aren't an unreasonable number of arguments,
|
1994-05-09 07:15:36 +04:00
|
|
|
* and that the address is in user space.
|
|
|
|
*/
|
1997-08-12 20:30:15 +04:00
|
|
|
if (narg > ARG_MAX || addr < kd->min_uva || addr >= kd->max_uva)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argv == NULL) {
|
1994-05-09 07:15:36 +04:00
|
|
|
/*
|
|
|
|
* Try to avoid reallocs.
|
|
|
|
*/
|
|
|
|
kd->argc = MAX(narg + 1, 32);
|
2006-02-16 23:48:42 +03:00
|
|
|
kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv));
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argv == NULL)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
} else if (narg + 1 > kd->argc) {
|
|
|
|
kd->argc = MAX(2 * kd->argc, narg + 1);
|
2006-02-16 23:48:42 +03:00
|
|
|
kd->argv = _kvm_realloc(kd, kd->argv, kd->argc *
|
2003-02-27 02:04:37 +03:00
|
|
|
sizeof(*kd->argv));
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argv == NULL)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argspc == NULL) {
|
2006-02-16 23:48:42 +03:00
|
|
|
kd->argspc = _kvm_malloc(kd, (size_t)kd->nbpg);
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argspc == NULL)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2006-02-16 23:48:42 +03:00
|
|
|
kd->argspc_len = kd->nbpg;
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argbuf == NULL) {
|
2006-02-16 23:48:42 +03:00
|
|
|
kd->argbuf = _kvm_malloc(kd, (size_t)kd->nbpg);
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argbuf == NULL)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1995-05-16 18:21:08 +04:00
|
|
|
}
|
|
|
|
cc = sizeof(char *) * narg;
|
2000-05-26 06:42:21 +04:00
|
|
|
if (kvm_ureadm(kd, p, addr, (void *)kd->argv, cc) != cc)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1995-05-16 18:21:08 +04:00
|
|
|
ap = np = kd->argspc;
|
1994-05-09 07:15:36 +04:00
|
|
|
argv = kd->argv;
|
|
|
|
len = 0;
|
|
|
|
/*
|
|
|
|
* Loop over pages, filling in the argument vector.
|
|
|
|
*/
|
2000-06-05 03:03:27 +04:00
|
|
|
while (argv < kd->argv + narg && *argv != NULL) {
|
1995-05-16 18:21:08 +04:00
|
|
|
addr = (u_long)*argv & ~(kd->nbpg - 1);
|
|
|
|
if (addr != oaddr) {
|
2000-05-26 06:42:21 +04:00
|
|
|
if (kvm_ureadm(kd, p, addr, kd->argbuf,
|
1998-09-27 22:15:58 +04:00
|
|
|
(size_t)kd->nbpg) != kd->nbpg)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1995-05-16 18:21:08 +04:00
|
|
|
oaddr = addr;
|
|
|
|
}
|
|
|
|
addr = (u_long)*argv & (kd->nbpg - 1);
|
1998-09-27 22:15:58 +04:00
|
|
|
cp = kd->argbuf + (size_t)addr;
|
|
|
|
cc = kd->nbpg - (size_t)addr;
|
|
|
|
if (maxcnt > 0 && cc > (size_t)(maxcnt - len))
|
|
|
|
cc = (size_t)(maxcnt - len);
|
1995-05-16 18:21:08 +04:00
|
|
|
ep = memchr(cp, '\0', cc);
|
2000-06-05 03:03:27 +04:00
|
|
|
if (ep != NULL)
|
1995-05-16 18:21:08 +04:00
|
|
|
cc = ep - cp + 1;
|
2006-02-16 23:48:42 +03:00
|
|
|
if (len + cc > kd->argspc_len) {
|
2003-03-21 01:53:12 +03:00
|
|
|
ptrdiff_t off;
|
1998-02-03 22:12:13 +03:00
|
|
|
char **pp;
|
|
|
|
char *op = kd->argspc;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
2006-02-16 23:48:42 +03:00
|
|
|
kd->argspc_len *= 2;
|
|
|
|
kd->argspc = _kvm_realloc(kd, kd->argspc,
|
|
|
|
kd->argspc_len);
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argspc == NULL)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
/*
|
|
|
|
* Adjust argv pointers in case realloc moved
|
|
|
|
* the string space.
|
|
|
|
*/
|
|
|
|
off = kd->argspc - op;
|
1995-05-18 17:59:25 +04:00
|
|
|
for (pp = kd->argv; pp < argv; pp++)
|
1994-05-09 07:15:36 +04:00
|
|
|
*pp += off;
|
1995-05-18 16:21:54 +04:00
|
|
|
ap += off;
|
|
|
|
np += off;
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
1995-05-16 18:21:08 +04:00
|
|
|
memcpy(np, cp, cc);
|
|
|
|
np += cc;
|
1994-05-09 07:15:36 +04:00
|
|
|
len += cc;
|
2000-06-05 03:03:27 +04:00
|
|
|
if (ep != NULL) {
|
1995-05-16 18:21:08 +04:00
|
|
|
*argv++ = ap;
|
|
|
|
ap = np;
|
|
|
|
} else
|
|
|
|
*argv += cc;
|
1994-05-09 07:15:36 +04:00
|
|
|
if (maxcnt > 0 && len >= maxcnt) {
|
|
|
|
/*
|
|
|
|
* We're stopping prematurely. Terminate the
|
1995-05-16 18:21:08 +04:00
|
|
|
* current string.
|
1994-05-09 07:15:36 +04:00
|
|
|
*/
|
2000-06-05 03:03:27 +04:00
|
|
|
if (ep == NULL) {
|
1995-05-16 18:21:08 +04:00
|
|
|
*np = '\0';
|
1995-05-25 07:03:42 +04:00
|
|
|
*argv++ = ap;
|
1995-05-16 18:21:08 +04:00
|
|
|
}
|
|
|
|
break;
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
|
|
|
}
|
1995-05-16 18:21:08 +04:00
|
|
|
/* Make sure argv is terminated. */
|
2000-06-05 03:03:27 +04:00
|
|
|
*argv = NULL;
|
1995-05-16 18:21:08 +04:00
|
|
|
return (kd->argv);
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-09-19 06:07:00 +04:00
|
|
|
ps_str_a(struct ps_strings *p, u_long *addr, int *n)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
2003-02-27 02:04:37 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
*addr = (u_long)p->ps_argvstr;
|
|
|
|
*n = p->ps_nargvstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-09-19 06:07:00 +04:00
|
|
|
ps_str_e(struct ps_strings *p, u_long *addr, int *n)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
2003-02-27 02:04:37 +03:00
|
|
|
|
1994-05-09 07:15:36 +04:00
|
|
|
*addr = (u_long)p->ps_envstr;
|
|
|
|
*n = p->ps_nenvstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine if the proc indicated by p is still active.
|
|
|
|
* This test is not 100% foolproof in theory, but chances of
|
|
|
|
* being wrong are very low.
|
|
|
|
*/
|
|
|
|
static int
|
2010-09-19 06:07:00 +04:00
|
|
|
proc_verify(kvm_t *kd, u_long kernp, const struct miniproc *p)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
|
|
|
struct proc kernproc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just read in the whole proc. It's not that big relative
|
|
|
|
* to the cost of the read system call.
|
|
|
|
*/
|
2000-05-26 06:42:21 +04:00
|
|
|
if (kvm_read(kd, kernp, &kernproc, sizeof(kernproc)) !=
|
1994-05-09 07:15:36 +04:00
|
|
|
sizeof(kernproc))
|
2003-02-27 02:04:37 +03:00
|
|
|
return (0);
|
1994-05-09 07:15:36 +04:00
|
|
|
return (p->p_pid == kernproc.p_pid &&
|
2003-02-27 02:04:37 +03:00
|
|
|
(kernproc.p_stat != SZOMB || p->p_stat == SZOMB));
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_doargv(kvm_t *kd, const struct miniproc *p, int nchr,
|
|
|
|
void (*info)(struct ps_strings *, u_long *, int *))
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
char **ap;
|
1994-05-09 07:15:36 +04:00
|
|
|
u_long addr;
|
|
|
|
int cnt;
|
|
|
|
struct ps_strings arginfo;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pointers are stored at the top of the user stack.
|
|
|
|
*/
|
1997-08-12 20:30:15 +04:00
|
|
|
if (p->p_stat == SZOMB)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2003-03-21 01:53:12 +03:00
|
|
|
cnt = (int)kvm_ureadm(kd, p, kd->usrstack - sizeof(arginfo),
|
1998-09-27 22:15:58 +04:00
|
|
|
(void *)&arginfo, sizeof(arginfo));
|
1997-08-12 20:30:15 +04:00
|
|
|
if (cnt != sizeof(arginfo))
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
(*info)(&arginfo, &addr, &cnt);
|
1994-08-13 14:15:02 +04:00
|
|
|
if (cnt == 0)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
1994-05-09 07:15:36 +04:00
|
|
|
ap = kvm_argv(kd, p, addr, cnt, nchr);
|
|
|
|
/*
|
|
|
|
* For live kernels, make sure this process didn't go away.
|
|
|
|
*/
|
2000-06-05 03:03:27 +04:00
|
|
|
if (ap != NULL && ISALIVE(kd) &&
|
2000-05-26 06:42:21 +04:00
|
|
|
!proc_verify(kd, (u_long)p->p_paddr, p))
|
2000-06-05 03:03:27 +04:00
|
|
|
ap = NULL;
|
1994-05-09 07:15:36 +04:00
|
|
|
return (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the command args. This code is now machine independent.
|
|
|
|
*/
|
|
|
|
char **
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_getargv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
2000-05-26 06:42:21 +04:00
|
|
|
struct miniproc p;
|
|
|
|
|
|
|
|
KPTOMINI(kp, &p);
|
|
|
|
return (kvm_doargv(kd, &p, nchr, ps_str_a));
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_getenvv(kvm_t *kd, const struct kinfo_proc *kp, int nchr)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
2000-05-26 06:42:21 +04:00
|
|
|
struct miniproc p;
|
|
|
|
|
|
|
|
KPTOMINI(kp, &p);
|
|
|
|
return (kvm_doargv(kd, &p, nchr, ps_str_e));
|
|
|
|
}
|
|
|
|
|
|
|
|
static char **
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_doargv2(kvm_t *kd, pid_t pid, int type, int nchr)
|
2000-05-26 06:42:21 +04:00
|
|
|
{
|
|
|
|
size_t bufs;
|
2000-07-16 06:04:11 +04:00
|
|
|
int narg, mib[4];
|
2006-02-16 23:48:42 +03:00
|
|
|
size_t newargspc_len;
|
2000-05-26 06:42:21 +04:00
|
|
|
char **ap, *bp, *endp;
|
|
|
|
|
|
|
|
/*
|
2004-05-12 21:20:01 +04:00
|
|
|
* Check that there aren't an unreasonable number of arguments.
|
2000-05-26 06:42:21 +04:00
|
|
|
*/
|
|
|
|
if (nchr > ARG_MAX)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
if (nchr == 0)
|
|
|
|
nchr = ARG_MAX;
|
|
|
|
|
|
|
|
/* Get number of strings in argv */
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROC_ARGS;
|
|
|
|
mib[2] = pid;
|
|
|
|
mib[3] = type == KERN_PROC_ARGV ? KERN_PROC_NARGV : KERN_PROC_NENV;
|
|
|
|
bufs = sizeof(narg);
|
2003-03-21 01:53:12 +03:00
|
|
|
if (sysctl(mib, 4, &narg, &bufs, NULL, (size_t)0) == -1)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2000-05-26 06:42:21 +04:00
|
|
|
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argv == NULL) {
|
2000-05-26 06:42:21 +04:00
|
|
|
/*
|
|
|
|
* Try to avoid reallocs.
|
|
|
|
*/
|
|
|
|
kd->argc = MAX(narg + 1, 32);
|
2006-02-16 23:48:42 +03:00
|
|
|
kd->argv = _kvm_malloc(kd, kd->argc * sizeof(*kd->argv));
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argv == NULL)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2000-05-26 06:42:21 +04:00
|
|
|
} else if (narg + 1 > kd->argc) {
|
|
|
|
kd->argc = MAX(2 * kd->argc, narg + 1);
|
2006-02-16 23:48:42 +03:00
|
|
|
kd->argv = _kvm_realloc(kd, kd->argv, kd->argc *
|
2003-02-27 02:04:37 +03:00
|
|
|
sizeof(*kd->argv));
|
2000-06-05 03:03:27 +04:00
|
|
|
if (kd->argv == NULL)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2000-05-26 06:42:21 +04:00
|
|
|
}
|
|
|
|
|
2006-02-16 23:48:42 +03:00
|
|
|
newargspc_len = MIN(nchr, ARG_MAX);
|
|
|
|
KVM_ALLOC(kd, argspc, newargspc_len);
|
|
|
|
memset(kd->argspc, 0, (size_t)kd->argspc_len); /* XXX necessary? */
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROC_ARGS;
|
|
|
|
mib[2] = pid;
|
|
|
|
mib[3] = type;
|
2006-02-16 23:48:42 +03:00
|
|
|
bufs = kd->argspc_len;
|
2003-03-21 01:53:12 +03:00
|
|
|
if (sysctl(mib, 4, kd->argspc, &bufs, NULL, (size_t)0) == -1)
|
2003-02-27 02:04:37 +03:00
|
|
|
return (NULL);
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
bp = kd->argspc;
|
2006-02-16 23:48:42 +03:00
|
|
|
bp[kd->argspc_len-1] = '\0'; /* make sure the string ends with nul */
|
2000-05-26 06:42:21 +04:00
|
|
|
ap = kd->argv;
|
|
|
|
endp = bp + MIN(nchr, bufs);
|
|
|
|
|
|
|
|
while (bp < endp) {
|
|
|
|
*ap++ = bp;
|
2003-02-27 02:04:37 +03:00
|
|
|
/*
|
|
|
|
* XXX: don't need following anymore, or stick check
|
|
|
|
* for max argc in above while loop?
|
|
|
|
*/
|
2000-05-26 06:42:21 +04:00
|
|
|
if (ap >= kd->argv + kd->argc) {
|
|
|
|
kd->argc *= 2;
|
|
|
|
kd->argv = _kvm_realloc(kd, kd->argv,
|
|
|
|
kd->argc * sizeof(*kd->argv));
|
2001-03-24 12:54:52 +03:00
|
|
|
ap = kd->argv;
|
2000-05-26 06:42:21 +04:00
|
|
|
}
|
|
|
|
bp += strlen(bp) + 1;
|
|
|
|
}
|
|
|
|
*ap = NULL;
|
2003-02-27 02:04:37 +03:00
|
|
|
|
2000-05-26 06:42:21 +04:00
|
|
|
return (kd->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_getargv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr)
|
2000-05-26 06:42:21 +04:00
|
|
|
{
|
2003-02-27 02:04:37 +03:00
|
|
|
|
2000-05-26 06:42:21 +04:00
|
|
|
return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ARGV, nchr));
|
|
|
|
}
|
|
|
|
|
|
|
|
char **
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_getenvv2(kvm_t *kd, const struct kinfo_proc2 *kp, int nchr)
|
2000-05-26 06:42:21 +04:00
|
|
|
{
|
2003-02-27 02:04:37 +03:00
|
|
|
|
2000-05-26 06:42:21 +04:00
|
|
|
return (kvm_doargv2(kd, kp->p_pid, KERN_PROC_ENV, nchr));
|
1994-05-09 07:15:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read from user space. The user context is given by p.
|
|
|
|
*/
|
2000-05-26 06:42:21 +04:00
|
|
|
static ssize_t
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_ureadm(kvm_t *kd, const struct miniproc *p, u_long uva,
|
|
|
|
char *buf, size_t len)
|
1994-05-09 07:15:36 +04:00
|
|
|
{
|
1998-02-03 22:12:13 +03:00
|
|
|
char *cp;
|
1994-05-09 07:15:36 +04:00
|
|
|
|
|
|
|
cp = buf;
|
|
|
|
while (len > 0) {
|
1998-09-27 22:15:58 +04:00
|
|
|
size_t cc;
|
1998-02-03 22:12:13 +03:00
|
|
|
char *dp;
|
1995-07-04 07:03:30 +04:00
|
|
|
u_long cnt;
|
1995-01-09 11:59:25 +03:00
|
|
|
|
2000-05-26 06:42:21 +04:00
|
|
|
dp = _kvm_ureadm(kd, p, uva, &cnt);
|
2000-06-05 03:03:27 +04:00
|
|
|
if (dp == NULL) {
|
2000-10-04 20:11:27 +04:00
|
|
|
_kvm_err(kd, 0, "invalid address (%lx)", uva);
|
2003-02-27 02:04:37 +03:00
|
|
|
return (0);
|
1995-01-09 11:59:25 +03:00
|
|
|
}
|
1998-09-27 22:15:58 +04:00
|
|
|
cc = (size_t)MIN(cnt, len);
|
1998-08-10 06:43:08 +04:00
|
|
|
memcpy(cp, dp, cc);
|
1994-05-09 07:15:36 +04:00
|
|
|
cp += cc;
|
|
|
|
uva += cc;
|
|
|
|
len -= cc;
|
|
|
|
}
|
|
|
|
return (ssize_t)(cp - buf);
|
|
|
|
}
|
2000-05-26 06:42:21 +04:00
|
|
|
|
|
|
|
ssize_t
|
2010-09-19 06:07:00 +04:00
|
|
|
kvm_uread(kvm_t *kd, const struct proc *p, u_long uva, char *buf, size_t len)
|
2000-05-26 06:42:21 +04:00
|
|
|
{
|
|
|
|
struct miniproc mp;
|
|
|
|
|
|
|
|
PTOMINI(p, &mp);
|
|
|
|
return (kvm_ureadm(kd, &mp, uva, buf, len));
|
|
|
|
}
|