8000 minix/minix/servers/mib/proc.c at master · Double0101/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1288 lines (1115 loc) · 34.1 KB

proc.c

File metadata and controls

1288 lines (1115 loc) · 34.1 KB
p->p_realflag = p->p_flag;
p->p_nlwps = (zombie) ? 0 : 1;
p->p_svuid = mp->mp_svuid;
p->p_svgid = mp->mp_svgid;
p->p_stat = get_lwp_stat(mslot, &p->p_wchan, p->p_wmesg,
sizeof(p->p_wmesg), &p->p_flag);
switch (p->p_stat) {
case LSRUN:
p->p_realstat = SACTIVE;
p->p_nrlwps = 1;
break;
case LSSLEEP:
p->p_realstat = SACTIVE;
if (p->p_flag & L_SINTR)
p->p_realflag |= P_SINTR;
break;
case LSSTOP:
p->p_realstat = SSTOP;
break;
case LSZOMB:
p->p_realstat = SZOMB;
break;
case LSDEAD:
p->p_stat = LSZOMB; /* ps(1) STAT does not know LSDEAD */
p->p_realstat = SDEAD;
break;
default:
assert(0);
}
if (!zombie)
fill_proc2_common(p, kslot);
}
/*
* Implementation of CTL_KERN KERN_PROC2.
*/
ssize_t
mib_kern_proc2(struct mib_call * call, struct mib_node * node __unused,
struct mib_oldp * oldp, struct mib_newp * newp __unused)
{
struct kinfo_proc2 proc2;
struct mproc *mp;
size_t copysz;
ssize_t off;
dev_t tty;
int r, req, arg, elsz, elmax, kmatch, zombie, mslot;
if (call->call_namelen != 4)
return EINVAL;
req = call->call_name[0];
arg = call->call_name[1];
elsz = call->call_name[2];
elmax = call->call_name[3]; /* redundant with the given oldlen.. */
/*
* The kernel is special, in that it does not have a slot in the PM or
* VFS tables. As such, it is dealt with separately. While checking
* arguments, we might as well check whether the kernel is matched.
*/
switch (req) {
case KERN_PROC_ALL:
kmatch = TRUE;
break;
case KERN_PROC_PID:
case KERN_PROC_SESSION:
case KERN_PROC_PGRP:
case KERN_PROC_UID:
case KERN_PROC_RUID:
case KERN_PROC_GID:
case KERN_PROC_RGID:
kmatch = (arg == 0);
break;
case KERN_PROC_TTY:
kmatch = ((dev_t)arg == KERN_PROC_TTY_NODEV);
break;
default:
return EINVAL;
}
if (elsz <= 0 || elmax < 0)
return EINVAL;
if (!update_tables())
return EINVAL;
off = 0;
copysz = MIN((size_t)elsz, sizeof(proc2));
if (kmatch) {
if (mib_inrange(oldp, off) && elmax > 0) {
fill_proc2_kern(&proc2);
if ((r = mib_copyout(oldp, off, &proc2, copysz)) < 0)
return r;
elmax--;
}
off += elsz;
}
for (mslot = 0; mslot < NR_PROCS; mslot++) {
mp = &mproc_tab[mslot];
if (!(mp->mp_flags & IN_USE))
continue;
switch (req) {
case KERN_PROC_PID:
if ((pid_t)arg != mp->mp_pid)
continue;
break;
case KERN_PROC_SESSION: /* TODO: job control support */
case KERN_PROC_PGRP:
if ((pid_t)arg != mp->mp_procgrp)
continue;
break;
case KERN_PROC_TTY:
if ((dev_t)arg == KERN_PROC_TTY_REVOKE)
continue; /* TODO: revoke(2) support */
/* Do not access the fproc_tab slot of zombies. */
zombie = (mp->mp_flags & (TRACE_ZOMBIE | ZOMBIE));
tty = (zombie) ? fproc_tab[mslot].fpl_tty : NO_DEV;
if ((dev_t)arg == KERN_PROC_TTY_NODEV) {
if (tty != NO_DEV)
continue;
} else if ((dev_t)arg == NO_DEV || (dev_t)arg != tty)
continue;
break;
case KERN_PROC_UID:
if ((uid_t)arg != mp->mp_effuid)
continue;
break;
case KERN_PROC_RUID:
if ((uid_t)arg != mp->mp_realuid)
continue;
break;
case KERN_PROC_GID:
if ((gid_t)arg != mp->mp_effgid)
continue;
break;
case KERN_PROC_RGID:
if ((gid_t)arg != mp->mp_realgid)
continue;
break;
}
if (mib_inrange(oldp, off) && elmax > 0) {
fill_proc2_user(&proc2, mslot);
if ((r = mib_copyout(oldp, off, &proc2, copysz)) < 0)
return r;
elmax--;
}
off += elsz;
}
if (oldp == NULL && req != KERN_PROC_PID)
off += EXTRA_PROCS * elsz;
return off;
}
/*
* Implementation of CTL_KERN KERN_PROC_ARGS.
*/
ssize_t
mib_kern_proc_args(struct mib_call * call, struct mib_node * node __unused,
struct mib_oldp * oldp, struct mib_newp * newp __unused)
{
char vbuf[PAGE_SIZE], sbuf[PAGE_SIZE], obuf[PAGE_SIZE];
struct ps_strings pss;
struct mproc *mp;
char *buf, *p, *q, *pptr;
vir_bytes vaddr, vpage, spage, paddr, ppage;
size_t max, off, olen, oleft, oldlen, bytes, pleft;
unsigned int copybudget;
pid_t pid;
int req, mslot, count, aborted, ended;
ssize_t r;
if (call->call_namelen != 2)
return EINVAL;
pid = call->call_name[0];
req = call->call_name[1];
switch (req) {
case KERN_PROC_ARGV:
case KERN_PROC_ENV:
case KERN_PROC_NARGV:
case KERN_PROC_NENV:
break;
default:
return EOPNOTSUPP;
}
if (!update_tables())
return EINVAL;
if ((mslot = get_mslot(pid)) == NO_SLOT)
return ESRCH;
mp = &mproc_tab[mslot];
if (mp->mp_flags & (TRACE_ZOMBIE | ZOMBIE))
return ESRCH;
/* We can return the count field size without copying in any data. */
if (oldp == NULL && (req == KERN_PROC_NARGV || req == KERN_PROC_NENV))
return sizeof(count);
if (sys_datacopy(mp->mp_endpoint,
mp->mp_frame_addr + mp->mp_frame_len - sizeof(pss),
SELF, (vir_bytes)&pss, sizeof(pss)) != OK)
return EINVAL;
/*
* Determine the upper size limit of the requested data. Not only may
* the size never exceed ARG_MAX, it may also not exceed the frame
* length as given in its original exec call. In fact, the frame
* length should be substantially larger: all strings for both the
* arguments and the environment are in there, along with other stuff,
* and there must be no overlap between strings. It is possible that
* the application called setproctitle(3), in which case the ps_strings
* pointers refer to data outside the frame altogether. However, this
* data should not exceed 2048 bytes, and we cover this by rounding up
* the frame length to a multiple of the page size. Anyhow, NetBSD
* blindly returns ARG_MAX when asked for a size estimate, so with this
* maximum we are already quite a bit more accurate.
*/
max = roundup(MIN(mp->mp_frame_len, ARG_MAX), PAGE_SIZE);
switch (req) {
case KERN_PROC_NARGV:
count = pss.ps_nargvstr;
return mib_copyout(oldp, 0, &count, sizeof(count));
case KERN_PROC_NENV:
count = pss.ps_nenvstr;
return mib_copyout(oldp, 0, &count, sizeof(count));
case KERN_PROC_ARGV:
if (oldp == NULL)
return max;
vaddr = (vir_bytes)pss.ps_argvstr;
count = pss.ps_nargvstr;
break;
case KERN_PROC_ENV:
if (oldp == NULL)
return max;
vaddr = (vir_bytes)pss.ps_envstr;
count = pss.ps_nenvstr;
0