HardenedBSD src tree https://hardenedbsd.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

377 lines
9.3 KiB

  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1982, 1986, 1990, 1991, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. * (c) UNIX System Laboratories, Inc.
  7. * All or some portions of this file are derived from material licensed
  8. * to the University of California by American Telephone and Telegraph
  9. * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  10. * the permission of UNIX System Laboratories, Inc.
  11. *
  12. * Copyright (c) 2002 Networks Associates Technologies, Inc.
  13. * All rights reserved.
  14. *
  15. * Portions of this software were developed for the FreeBSD Project by
  16. * ThinkSec AS and NAI Labs, the Security Research Division of Network
  17. * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
  18. * ("CBOSS"), as part of the DARPA CHATS research program.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. * 3. Neither the name of the University nor the names of its contributors
  29. * may be used to endorse or promote products derived from this software
  30. * without specific prior written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  33. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  36. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  38. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  42. * SUCH DAMAGE.
  43. */
  44. #include <sys/cdefs.h>
  45. __FBSDID("$FreeBSD$");
  46. #include "opt_stack.h"
  47. #include <sys/param.h>
  48. #include <sys/cons.h>
  49. #include <sys/kdb.h>
  50. #include <sys/lock.h>
  51. #include <sys/malloc.h>
  52. #include <sys/mutex.h>
  53. #include <sys/proc.h>
  54. #include <sys/resourcevar.h>
  55. #include <sys/sbuf.h>
  56. #include <sys/sched.h>
  57. #include <sys/stack.h>
  58. #include <sys/sysctl.h>
  59. #include <sys/systm.h>
  60. #include <sys/tty.h>
  61. #include <vm/vm.h>
  62. #include <vm/pmap.h>
  63. #include <vm/vm_map.h>
  64. /*
  65. * Returns 1 if p2 is "better" than p1
  66. *
  67. * The algorithm for picking the "interesting" process is thus:
  68. *
  69. * 1) Only foreground processes are eligible - implied.
  70. * 2) Runnable processes are favored over anything else. The runner
  71. * with the highest cpu utilization is picked (p_estcpu). Ties are
  72. * broken by picking the highest pid.
  73. * 3) The sleeper with the shortest sleep time is next. With ties,
  74. * we pick out just "short-term" sleepers (P_SINTR == 0).
  75. * 4) Further ties are broken by picking the highest pid.
  76. */
  77. #define TESTAB(a, b) ((a)<<1 | (b))
  78. #define ONLYA 2
  79. #define ONLYB 1
  80. #define BOTH 3
  81. static int
  82. proc_sum(struct proc *p, fixpt_t *estcpup)
  83. {
  84. struct thread *td;
  85. int estcpu;
  86. int val;
  87. val = 0;
  88. estcpu = 0;
  89. FOREACH_THREAD_IN_PROC(p, td) {
  90. thread_lock(td);
  91. if (TD_ON_RUNQ(td) ||
  92. TD_IS_RUNNING(td))
  93. val = 1;
  94. estcpu += sched_pctcpu(td);
  95. thread_unlock(td);
  96. }
  97. *estcpup = estcpu;
  98. return (val);
  99. }
  100. static int
  101. thread_compare(struct thread *td, struct thread *td2)
  102. {
  103. int runa, runb;
  104. int slpa, slpb;
  105. fixpt_t esta, estb;
  106. if (td == NULL)
  107. return (1);
  108. /*
  109. * Fetch running stats, pctcpu usage, and interruptable flag.
  110. */
  111. thread_lock(td);
  112. runa = TD_IS_RUNNING(td) | TD_ON_RUNQ(td);
  113. slpa = td->td_flags & TDF_SINTR;
  114. esta = sched_pctcpu(td);
  115. thread_unlock(td);
  116. thread_lock(td2);
  117. runb = TD_IS_RUNNING(td2) | TD_ON_RUNQ(td2);
  118. estb = sched_pctcpu(td2);
  119. slpb = td2->td_flags & TDF_SINTR;
  120. thread_unlock(td2);
  121. /*
  122. * see if at least one of them is runnable
  123. */
  124. switch (TESTAB(runa, runb)) {
  125. case ONLYA:
  126. return (0);
  127. case ONLYB:
  128. return (1);
  129. case BOTH:
  130. break;
  131. }
  132. /*
  133. * favor one with highest recent cpu utilization
  134. */
  135. if (estb > esta)
  136. return (1);
  137. if (esta > estb)
  138. return (0);
  139. /*
  140. * favor one sleeping in a non-interruptible sleep
  141. */
  142. switch (TESTAB(slpa, slpb)) {
  143. case ONLYA:
  144. return (0);
  145. case ONLYB:
  146. return (1);
  147. case BOTH:
  148. break;
  149. }
  150. return (td < td2);
  151. }
  152. static int
  153. proc_compare(struct proc *p1, struct proc *p2)
  154. {
  155. int runa, runb;
  156. fixpt_t esta, estb;
  157. if (p1 == NULL)
  158. return (1);
  159. /*
  160. * Fetch various stats about these processes. After we drop the
  161. * lock the information could be stale but the race is unimportant.
  162. */
  163. PROC_LOCK(p1);
  164. runa = proc_sum(p1, &esta);
  165. PROC_UNLOCK(p1);
  166. PROC_LOCK(p2);
  167. runb = proc_sum(p2, &estb);
  168. PROC_UNLOCK(p2);
  169. /*
  170. * see if at least one of them is runnable
  171. */
  172. switch (TESTAB(runa, runb)) {
  173. case ONLYA:
  174. return (0);
  175. case ONLYB:
  176. return (1);
  177. case BOTH:
  178. break;
  179. }
  180. /*
  181. * favor one with highest recent cpu utilization
  182. */
  183. if (estb > esta)
  184. return (1);
  185. if (esta > estb)
  186. return (0);
  187. /*
  188. * weed out zombies
  189. */
  190. switch (TESTAB(p1->p_state == PRS_ZOMBIE, p2->p_state == PRS_ZOMBIE)) {
  191. case ONLYA:
  192. return (1);
  193. case ONLYB:
  194. return (0);
  195. case BOTH:
  196. break;
  197. }
  198. return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
  199. }
  200. static int
  201. sbuf_tty_drain(void *a, const char *d, int len)
  202. {
  203. struct tty *tp;
  204. int rc;
  205. tp = a;
  206. if (kdb_active) {
  207. cnputsn(d, len);
  208. return (len);
  209. }
  210. if (tp != NULL && !KERNEL_PANICKED()) {
  211. rc = tty_putstrn(tp, d, len);
  212. if (rc != 0)
  213. return (-ENXIO);
  214. return (len);
  215. }
  216. return (-ENXIO);
  217. }
  218. #ifdef STACK
  219. static bool tty_info_kstacks = false;
  220. SYSCTL_BOOL(_kern, OID_AUTO, tty_info_kstacks, CTLFLAG_RWTUN,
  221. &tty_info_kstacks, 0,
  222. "Enable printing kernel stack(9) traces on ^T (tty info)");
  223. #endif
  224. /*
  225. * Report on state of foreground process group.
  226. */
  227. void
  228. tty_info(struct tty *tp)
  229. {
  230. struct timeval rtime, utime, stime;
  231. #ifdef STACK
  232. struct stack stack;
  233. int sterr;
  234. #endif
  235. struct proc *p, *ppick;
  236. struct thread *td, *tdpick;
  237. const char *stateprefix, *state;
  238. struct sbuf sb;
  239. long rss;
  240. int load, pctcpu;
  241. pid_t pid;
  242. char comm[MAXCOMLEN + 1];
  243. struct rusage ru;
  244. tty_assert_locked(tp);
  245. if (tty_checkoutq(tp) == 0)
  246. return;
  247. (void)sbuf_new(&sb, tp->t_prbuf, tp->t_prbufsz, SBUF_FIXEDLEN);
  248. sbuf_set_drain(&sb, sbuf_tty_drain, tp);
  249. /* Print load average. */
  250. load = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
  251. sbuf_printf(&sb, "%sload: %d.%02d ", tp->t_column == 0 ? "" : "\n",
  252. load / 100, load % 100);
  253. if (tp->t_session == NULL) {
  254. sbuf_printf(&sb, "not a controlling terminal\n");
  255. goto out;
  256. }
  257. if (tp->t_pgrp == NULL) {
  258. sbuf_printf(&sb, "no foreground process group\n");
  259. goto out;
  260. }
  261. PGRP_LOCK(tp->t_pgrp);
  262. if (LIST_EMPTY(&tp->t_pgrp->pg_members)) {
  263. PGRP_UNLOCK(tp->t_pgrp);
  264. sbuf_printf(&sb, "empty foreground process group\n");
  265. goto out;
  266. }
  267. /*
  268. * Pick the most interesting process and copy some of its
  269. * state for printing later. This operation could rely on stale
  270. * data as we can't hold the proc slock or thread locks over the
  271. * whole list. However, we're guaranteed not to reference an exited
  272. * thread or proc since we hold the tty locked.
  273. */
  274. p = NULL;
  275. LIST_FOREACH(ppick, &tp->t_pgrp->pg_members, p_pglist)
  276. if (proc_compare(p, ppick))
  277. p = ppick;
  278. PROC_LOCK(p);
  279. PGRP_UNLOCK(tp->t_pgrp);
  280. td = NULL;
  281. FOREACH_THREAD_IN_PROC(p, tdpick)
  282. if (thread_compare(td, tdpick))
  283. td = tdpick;
  284. stateprefix = "";
  285. thread_lock(td);
  286. if (TD_IS_RUNNING(td))
  287. state = "running";
  288. else if (TD_ON_RUNQ(td) || TD_CAN_RUN(td))
  289. state = "runnable";
  290. else if (TD_IS_SLEEPING(td)) {
  291. /* XXX: If we're sleeping, are we ever not in a queue? */
  292. if (TD_ON_SLEEPQ(td))
  293. state = td->td_wmesg;
  294. else
  295. state = "sleeping without queue";
  296. } else if (TD_ON_LOCK(td)) {
  297. state = td->td_lockname;
  298. stateprefix = "*";
  299. } else if (TD_IS_SUSPENDED(td))
  300. state = "suspended";
  301. else if (TD_AWAITING_INTR(td))
  302. state = "intrwait";
  303. else if (p->p_state == PRS_ZOMBIE)
  304. state = "zombie";
  305. else
  306. state = "unknown";
  307. pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
  308. #ifdef STACK
  309. if (tty_info_kstacks) {
  310. if (TD_IS_SWAPPED(td))
  311. sterr = ENOENT;
  312. else
  313. sterr = stack_save_td(&stack, td);
  314. }
  315. #endif
  316. thread_unlock(td);
  317. if (p->p_state == PRS_NEW || p->p_state == PRS_ZOMBIE)
  318. rss = 0;
  319. else
  320. rss = pgtok(vmspace_resident_count(p->p_vmspace));
  321. microuptime(&rtime);
  322. timevalsub(&rtime, &p->p_stats->p_start);
  323. rufetchcalc(p, &ru, &utime, &stime);
  324. pid = p->p_pid;
  325. strlcpy(comm, p->p_comm, sizeof comm);
  326. PROC_UNLOCK(p);
  327. /* Print command, pid, state, rtime, utime, stime, %cpu, and rss. */
  328. sbuf_printf(&sb,
  329. " cmd: %s %d [%s%s] %ld.%02ldr %ld.%02ldu %ld.%02lds %d%% %ldk\n",
  330. comm, pid, stateprefix, state,
  331. (long)rtime.tv_sec, rtime.tv_usec / 10000,
  332. (long)utime.tv_sec, utime.tv_usec / 10000,
  333. (long)stime.tv_sec, stime.tv_usec / 10000,
  334. pctcpu / 100, rss);
  335. #ifdef STACK
  336. if (tty_info_kstacks && sterr == 0)
  337. stack_sbuf_print_flags(&sb, &stack, M_NOWAIT);
  338. #endif
  339. out:
  340. sbuf_finish(&sb);
  341. sbuf_delete(&sb);
  342. }