/* * ps_showallprocs functionality fix for FreeBSD 4.x. * * (c) 2003 Pawel Jakub Dawidek * * $Id: showprocs.c,v 1.2 2003/04/20 14:55:20 jules Exp $ */ #include #include #include #include #include #include #define FSP_CATCH 0 #define FSP_ORIG 1 extern int ps_showallprocs; int (*vsp_sysctl_kern_proc)(SYSCTL_HANDLER_ARGS); static int fsp_sysctl_kern_proc(SYSCTL_HANDLER_ARGS) { struct proc *p; int *name = (int *)arg1; u_int namelen = arg2; if (oidp->oid_number == KERN_PROC_PID) { if (namelen != 1) return (EINVAL); p = pfind((pid_t)name[0]); if (p == NULL) return (0); /* * Show a user only their processes. */ if (!ps_showallprocs && p_trespass(curproc, p)) return (0); } /* That's it, rest original sysctl_kern_proc will do. */ return (vsp_sysctl_kern_proc(oidp, arg1, arg2, req)); } static int fsp_sysctl_switch(int what) { struct sysctl_oid *oid; struct sysctl_req req; int name[3] = { CTL_KERN, KERN_PROC, KERN_PROC_PID }; int error; /* kern.proc.pid */ error = sysctl_find_oid(name, 3, &oid, NULL, &req); if (error != 0) { printf("showprocs: Unexpected error while looking for " "kern.proc.pid sysctl.\n"); return (error); } if (what == FSP_ORIG) oid->oid_handler = vsp_sysctl_kern_proc; else /* if (what == FSP_CATCH) */ { vsp_sysctl_kern_proc = oid->oid_handler; oid->oid_handler = fsp_sysctl_kern_proc; } return (0); } static int mod(struct module *module, int cmd, void *arg) { int error; error = 0; switch (cmd) { case MOD_LOAD: error = fsp_sysctl_switch(FSP_CATCH); if (error != 0) break; log(LOG_INFO, "ps_showallprocs fix loaded.\n"); break; case MOD_UNLOAD: fsp_sysctl_switch(FSP_ORIG); log(LOG_INFO, "ps_showallprocs fix unloaded.\n"); break; default: error = EINVAL; break; } return (error); } static moduledata_t showprocs_mod = { "showprocs", mod, NULL }; DECLARE_MODULE(showprocs, showprocs_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);