dtpstree.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. // DT PS Tree
  2. //
  3. // Douglas Thrift
  4. //
  5. // dtpstree.cpp
  6. /* Copyright 2010 Douglas Thrift
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include <cerrno>
  21. #include <climits>
  22. #include <cstdarg>
  23. #include <cstdio>
  24. #include <cstdlib>
  25. #include <cstring>
  26. #include <iostream>
  27. #include <map>
  28. #include <set>
  29. #include <sstream>
  30. #include <string>
  31. #include <vector>
  32. #ifdef __GLIBC__
  33. #include <bsd/stdlib.h>
  34. #else
  35. #include <libgen.h>
  36. #endif
  37. #include <curses.h>
  38. #include <err.h>
  39. #include <fcntl.h>
  40. #include <getopt.h>
  41. #include <kvm.h>
  42. #include <paths.h>
  43. #include <pwd.h>
  44. #include <sys/param.h>
  45. #include <sys/sysctl.h>
  46. #include <sys/user.h>
  47. #include <term.h>
  48. #include <vis.h>
  49. #include "foreach.hpp"
  50. #define DTPSTREE_PROGRAM "dtpstree"
  51. #define DTPSTREE_VERSION "1.0.1"
  52. class Proc;
  53. typedef std::map<pid_t, Proc *> PidMap;
  54. typedef std::multimap<std::string, Proc *> NameMap;
  55. enum Flags
  56. {
  57. Arguments = 0x0001,
  58. Ascii = 0x0002,
  59. NoCompact = 0x0004,
  60. Highlight = 0x0008,
  61. Vt100 = 0x0010,
  62. ShowKernel = 0x0020,
  63. Long = 0x0040,
  64. NumericSort = 0x0080,
  65. ShowPids = 0x0100,
  66. ShowTitles = 0x0200,
  67. UidChanges = 0x0400,
  68. Unicode = 0x0800,
  69. Version = 0x1000,
  70. Pid = 0x2000,
  71. User = 0x4000
  72. };
  73. enum Escape { None, BoxDrawing, Bright };
  74. struct Segment
  75. {
  76. size_t width_;
  77. Escape escape_;
  78. char *string_;
  79. inline Segment(size_t width, Escape escape, char *string) : width_(width), escape_(escape), string_(string) {}
  80. };
  81. struct Branch
  82. {
  83. std::string indentation_;
  84. bool done_;
  85. inline Branch(size_t indentation) : indentation_(indentation, ' '), done_(false) {}
  86. };
  87. class Tree
  88. {
  89. const uint16_t &flags_;
  90. bool vt100_;
  91. wchar_t horizontal_, vertical_, upAndRight_, verticalAndRight_, downAndHorizontal_;
  92. size_t maxWidth_, width_;
  93. bool max_, suppress_;
  94. std::vector<Segment> segments_;
  95. std::vector<Branch> branches_;
  96. bool first_, last_;
  97. size_t duplicate_;
  98. public:
  99. Tree(const uint16_t &flags) : flags_(flags), vt100_(false), maxWidth_(0), width_(0), max_(false), suppress_(false), duplicate_(0)
  100. {
  101. bool tty(isatty(1));
  102. if (flags & Ascii)
  103. {
  104. ascii:
  105. horizontal_ = L'-';
  106. vertical_ = L'|';
  107. upAndRight_ = L'`';
  108. verticalAndRight_ = L'|';
  109. downAndHorizontal_ = L'+';
  110. }
  111. else if (flags & Unicode)
  112. {
  113. unicode:
  114. if (!std::setlocale(LC_CTYPE, ""))
  115. goto vt100;
  116. horizontal_ = L'\x2500';
  117. vertical_ = L'\x2502';
  118. upAndRight_ = L'\x2514';
  119. verticalAndRight_ = L'\x251c';
  120. downAndHorizontal_ = L'\x252c';
  121. char *test;
  122. if (asprintf(&test, "%lc%lc%lc%lc%lc", horizontal_, vertical_, upAndRight_, verticalAndRight_, downAndHorizontal_) == -1)
  123. goto vt100;
  124. std::free(test);
  125. }
  126. else if (flags & Vt100)
  127. {
  128. vt100:
  129. vt100_ = true;
  130. horizontal_ = L'\x71';
  131. vertical_ = L'\x78';
  132. upAndRight_ = L'\x6d';
  133. verticalAndRight_ = L'\x74';
  134. downAndHorizontal_ = L'\x77';
  135. }
  136. else if (tty)
  137. goto unicode;
  138. else
  139. goto ascii;
  140. if (!(flags & Long) && tty)
  141. {
  142. int code;
  143. if (setupterm(NULL, 1, &code) == OK)
  144. {
  145. maxWidth_ = tigetnum("cols");
  146. if (tigetflag("am") && !tigetflag("xenl"))
  147. suppress_ = true;
  148. }
  149. else
  150. maxWidth_ = 80;
  151. }
  152. }
  153. void print(const std::string &string, bool highlight, size_t duplicate)
  154. {
  155. Escape escape(vt100_ ? BoxDrawing : None);
  156. if (!first_ || flags_ & Arguments)
  157. {
  158. size_t last(branches_.size() - 1);
  159. _foreach (std::vector<Branch>, branch, branches_)
  160. {
  161. size_t width(branch->indentation_.size() + 2);
  162. if (_index == last)
  163. {
  164. wchar_t line;
  165. if (last_)
  166. {
  167. branch->done_ = true;
  168. line = upAndRight_;
  169. }
  170. else
  171. line = verticalAndRight_;
  172. print(width, escape, "%s%lc%lc", branch->indentation_.c_str(), line, horizontal_);
  173. }
  174. else
  175. print(width, escape, "%s%lc ", branch->indentation_.c_str(), branch->done_ ? ' ' : vertical_);
  176. }
  177. }
  178. else if (branches_.size())
  179. {
  180. wchar_t line;
  181. if (last_)
  182. {
  183. branches_.back().done_ = true;
  184. line = horizontal_;
  185. }
  186. else
  187. line = downAndHorizontal_;
  188. print(3, escape, "%lc%lc%lc", horizontal_, line, horizontal_);
  189. }
  190. size_t size(0);
  191. if (duplicate)
  192. {
  193. std::ostringstream string;
  194. string << duplicate << "*[";
  195. size = string.str().size();
  196. print(size, None, "%s", string.str().c_str());
  197. ++duplicate_;
  198. }
  199. print(string.size(), highlight ? Bright : None, "%s", string.c_str());
  200. branches_.push_back(Branch(!(flags_ & Arguments) ? size + string.size() + 1 : 2));
  201. }
  202. inline void printArg(const std::string &arg, bool last)
  203. {
  204. if (max_)
  205. return;
  206. size_t width(arg.size() + 1);
  207. width_ += width;
  208. char *string;
  209. if (maxWidth_ && !(flags_ & Long))
  210. if (width_ > maxWidth_ || !last && width_ + 3 >= maxWidth_)
  211. {
  212. width -= width_ - maxWidth_;
  213. width_ = maxWidth_;
  214. max_ = true;
  215. ssize_t size(static_cast<ssize_t>(width) - 4);
  216. asprintf(&string, " %s...", size > 0 ? arg.substr(0, size).c_str() : "");
  217. }
  218. else
  219. goto print;
  220. else
  221. print:
  222. asprintf(&string, " %s", arg.c_str());
  223. segments_.push_back(Segment(width, None, string));
  224. }
  225. inline void pop(bool children)
  226. {
  227. branches_.pop_back();
  228. if (!(flags_ & Arguments) && !children)
  229. done();
  230. }
  231. void done()
  232. {
  233. if (duplicate_)
  234. {
  235. print(duplicate_, None, "%s", std::string(duplicate_, ']').c_str());
  236. duplicate_ = 0;
  237. }
  238. size_t last(segments_.size() - 1);
  239. _foreach (std::vector<Segment>, segment, segments_)
  240. {
  241. const char *begin, *end;
  242. switch (segment->escape_)
  243. {
  244. case BoxDrawing:
  245. begin = !_index || (segment - 1)->escape_ != BoxDrawing ? "\033(0\017" : "";
  246. end = _index == last || (segment + 1)->escape_ != BoxDrawing ? "\033(B\017" : "";
  247. break;
  248. case Bright:
  249. begin = "\033[1m";
  250. end = "\033[22m";
  251. break;
  252. default:
  253. begin = end = ""; break;
  254. }
  255. std::printf("%s%s%s", begin, segment->string_, end);
  256. std::free(segment->string_);
  257. }
  258. segments_.clear();
  259. if (suppress_ && width_ == maxWidth_)
  260. std::fflush(stdout);
  261. else
  262. std::printf("\n");
  263. width_ = 0;
  264. max_ = false;
  265. }
  266. inline Tree &operator()(bool first, bool last)
  267. {
  268. first_ = first;
  269. last_ = last;
  270. return *this;
  271. }
  272. private:
  273. void print(size_t width, Escape escape, const char * format, ...)
  274. {
  275. if (max_)
  276. return;
  277. std::va_list args;
  278. va_start(args, format);
  279. char *string;
  280. vasprintf(&string, format, args);
  281. va_end(args);
  282. width_ += width;
  283. if (maxWidth_ && !(flags_ & Long))
  284. if (width_ > maxWidth_)
  285. {
  286. width -= width_ - maxWidth_;
  287. width_ = maxWidth_;
  288. max_ = true;
  289. bool previous = !width;
  290. if (previous)
  291. {
  292. std::free(string);
  293. const Segment &segment(segments_.back());
  294. width = segment.width_;
  295. string = segment.string_;
  296. }
  297. std::wstring wide(width - 1, '\0');
  298. std::mbstowcs(const_cast<wchar_t *>(wide.data()), string, wide.size());
  299. std::free(string);
  300. asprintf(&string, "%ls+", wide.c_str());
  301. if (previous)
  302. {
  303. segments_.back().string_ = string;
  304. return;
  305. }
  306. }
  307. segments_.push_back(Segment(width, escape, string));
  308. }
  309. };
  310. class Proc
  311. {
  312. const uint16_t &flags_;
  313. kvm_t *kd_;
  314. kinfo_proc *proc_;
  315. mutable std::string name_, print_;
  316. Proc *parent_;
  317. PidMap childrenByPid_;
  318. NameMap childrenByName_;
  319. bool highlight_, root_;
  320. int8_t compact_;
  321. size_t duplicate_;
  322. public:
  323. inline Proc(const uint16_t &flags, kvm_t *kd, kinfo_proc *proc) : flags_(flags), kd_(kd), proc_(proc), parent_(NULL), highlight_(false), root_(false), compact_(-1), duplicate_(0) {}
  324. inline const std::string &name() const
  325. {
  326. if (name_.empty())
  327. name_ = visual(proc_->ki_comm);
  328. return name_;
  329. }
  330. inline pid_t parent() const { return proc_->ki_ppid; }
  331. inline pid_t pid() const { return proc_->ki_pid; }
  332. inline void child(Proc *proc)
  333. {
  334. if (proc == this)
  335. return;
  336. proc->parent_ = this;
  337. childrenByPid_[proc->pid()] = proc;
  338. childrenByName_.insert(NameMap::value_type(proc->name(), proc));
  339. }
  340. inline void highlight()
  341. {
  342. highlight_ = true;
  343. if (parent_)
  344. parent_->highlight();
  345. }
  346. inline bool compact()
  347. {
  348. if (compact_ == -1)
  349. compact_ = compact(childrenByName_);
  350. return compact_;
  351. }
  352. bool root(uid_t uid)
  353. {
  354. if (flags_ & User)
  355. {
  356. if (uid == this->uid())
  357. {
  358. Proc *parent(parent_);
  359. while (parent)
  360. {
  361. if (parent->uid() == uid)
  362. return false;
  363. parent = parent->parent_;
  364. }
  365. return root_ = true;
  366. }
  367. return false;
  368. }
  369. return root_ = !parent_;
  370. }
  371. void printByPid(Tree &tree) const
  372. {
  373. if (duplicate_ == 1)
  374. return;
  375. print(tree);
  376. size_t last(childrenByPid_.size() - 1);
  377. _foreach (const PidMap, child, childrenByPid_)
  378. child->second->printByPid(tree(!_index, _index == last));
  379. tree.pop(children());
  380. }
  381. void printByName(Tree &tree) const
  382. {
  383. if (duplicate_ == 1)
  384. return;
  385. print(tree);
  386. size_t last(childrenByName_.size() - 1);
  387. _foreach (const NameMap, child, childrenByName_)
  388. child->second->printByName(tree(!_index, _index == last));
  389. tree.pop(children());
  390. }
  391. static bool compact(NameMap &names)
  392. {
  393. Proc *previous(NULL);
  394. bool compact(true);
  395. _foreach (NameMap, name, names)
  396. {
  397. Proc *proc(name->second);
  398. if (proc->duplicate_)
  399. continue;
  400. size_t duplicate(proc->compact());
  401. if (!(duplicate && (!previous || proc->print() == previous->print())))
  402. compact = false;
  403. previous = proc;
  404. size_t count(names.count(name->first));
  405. if (!duplicate || count == 1)
  406. continue;
  407. _forall(NameMap::iterator, n4me, (++name)--, names.upper_bound(name->first))
  408. {
  409. Proc *pr0c(n4me->second);
  410. if (true)
  411. duplicate += ++pr0c->duplicate_;
  412. }
  413. if (duplicate != 1)
  414. proc->duplicate_ = duplicate;
  415. }
  416. return compact;
  417. }
  418. private:
  419. inline std::string visual(const char *string) const
  420. {
  421. std::string visual(std::strlen(string) * 4 + 1, '\0');
  422. visual.resize(strvis(const_cast<char *>(visual.data()), string, VIS_TAB | VIS_NL | VIS_NOSLASH));
  423. return visual;
  424. }
  425. void print(Tree &tree) const
  426. {
  427. tree.print(print(), highlight_, duplicate_);
  428. if (flags_ & Arguments)
  429. {
  430. char **argv(kvm_getargv(kd_, proc_, 0));
  431. if (argv && *argv)
  432. for (++argv; *argv; ++argv)
  433. tree.printArg(visual(*argv), !*(argv + 1));
  434. tree.done();
  435. }
  436. }
  437. const std::string &print() const
  438. {
  439. if (print_.empty())
  440. {
  441. std::ostringstream print;
  442. if (flags_ & ShowTitles)
  443. {
  444. char **argv(kvm_getargv(kd_, proc_, 0));
  445. if (argv)
  446. print << visual(*argv);
  447. else
  448. print << name();
  449. }
  450. else
  451. print << name();
  452. bool p1d(flags_ & ShowPids), args(flags_ & Arguments);
  453. bool change(flags_ & UidChanges && (root_ ? !(flags_ & User) && uid() : parent_ && uid() != parent_->uid()));
  454. bool parens((p1d || change) && !args);
  455. if (parens)
  456. print << '(';
  457. if (p1d)
  458. {
  459. if (!parens)
  460. print << ',';
  461. print << pid();
  462. }
  463. if (change)
  464. {
  465. if (!parens || p1d)
  466. print << ',';
  467. passwd *user(getpwuid(uid()));
  468. print << user->pw_name;
  469. }
  470. if (parens)
  471. print << ')';
  472. print_ = print.str();
  473. }
  474. return print_;
  475. }
  476. inline bool children() const { return childrenByName_.size(); }
  477. inline uid_t uid() const { return proc_->ki_ruid; }
  478. };
  479. static void help(const char *program, option options[], int code = 0)
  480. {
  481. std::printf("Usage: %s [options] [PID|USER]\n\nOptions:\n", basename(program));
  482. for (option *option(options); option->name; ++option)
  483. {
  484. std::string name(option->name);
  485. std::ostringstream arguments;
  486. switch (option->val)
  487. {
  488. case 'H':
  489. if (name != "highlight")
  490. continue;
  491. arguments << "-H[PID], --highlight[=PID]"; break;
  492. case 0:
  493. if (name == "pid")
  494. arguments << "PID, --pid=PID";
  495. else if (name == "user")
  496. arguments << "USER, --user=USER";
  497. else
  498. goto argument;
  499. break;
  500. case 'c':
  501. if (name != "no-compact")
  502. continue;
  503. default:
  504. arguments << '-' << static_cast<char>(option->val) << ", ";
  505. argument:
  506. arguments << "--" << name;
  507. }
  508. const char *description("");
  509. switch (option->val)
  510. {
  511. case 'a':
  512. description = "show command line arguments"; break;
  513. case 'A':
  514. description = "use ASCII line drawing characters"; break;
  515. case 'c':
  516. description = "don't compact identical subtrees"; break;
  517. case 'h':
  518. description = "show this help message and exit"; break;
  519. case 'H':
  520. description = "highlight the current process (or PID) and its\n ancestors"; break;
  521. case 'G':
  522. description = "use VT100 line drawing characters"; break;
  523. case 'k':
  524. description = "show kernel processes"; break;
  525. case 'l':
  526. description = "don't truncate long lines"; break;
  527. case 'n':
  528. description = "sort output by PID"; break;
  529. case 'p':
  530. description = "show PIDs; implies -c"; break;
  531. case 't':
  532. description = "show process titles"; break;
  533. case 'u':
  534. description = "show uid transitions"; break;
  535. case 'U':
  536. description = "use Unicode line drawing characters"; break;
  537. case 'V':
  538. description = "show version information and exit"; break;
  539. case 0:
  540. if (name == "pid")
  541. description = "show only the tree rooted at the process PID";
  542. else if (name == "user")
  543. description = "show only trees rooted at processes of USER";
  544. }
  545. std::printf(" %-27s %s\n", arguments.str().c_str(), description);
  546. }
  547. std::exit(code);
  548. }
  549. template <typename Type, long long minimum, long long maximum>
  550. static Type value(char *program, option options[], bool *success = NULL)
  551. {
  552. const char *error;
  553. long long value(strtonum(optarg, minimum, maximum, &error));
  554. if (error)
  555. if (success && errno == EINVAL)
  556. *success = false;
  557. else
  558. {
  559. warnx("Number is %s: \"%s\"", error, optarg);
  560. help(program, options, 1);
  561. }
  562. else if (success)
  563. *success = true;
  564. return value;
  565. }
  566. static uint16_t options(int argc, char *argv[], pid_t &hpid, pid_t &pid, char *&user)
  567. {
  568. option options[] = {
  569. { "arguments", no_argument, NULL, 'a' },
  570. { "ascii", no_argument, NULL, 'A' },
  571. { "compact", no_argument, NULL, 'c' },
  572. { "no-compact", no_argument, NULL, 'c' },
  573. { "help", no_argument, NULL, 'h' },
  574. { "highlight", optional_argument, NULL, 'H' },
  575. { "highlight-all", no_argument, NULL, 'H' },
  576. { "highlight-pid", required_argument, NULL, 'H' },
  577. { "vt100", no_argument, NULL, 'G' },
  578. { "show-kernel", no_argument, NULL, 'k' },
  579. { "long", no_argument, NULL, 'l' },
  580. { "numeric-sort", no_argument, NULL, 'n' },
  581. { "show-pids", no_argument, NULL, 'p' },
  582. { "show-titles", no_argument, NULL, 't' },
  583. { "uid-changes", no_argument, NULL, 'u' },
  584. { "unicode", no_argument, NULL, 'U' },
  585. { "version", no_argument, NULL, 'V' },
  586. { "pid", required_argument, NULL, 0 },
  587. { "user", required_argument, NULL, 0 },
  588. { NULL, 0, NULL, 0 }
  589. };
  590. int option, index;
  591. uint16_t flags(0);
  592. char *program(argv[0]);
  593. while ((option = getopt_long(argc, argv, "aAchH::GklnptuUV", options, &index)) != -1)
  594. switch (option)
  595. {
  596. case 'a':
  597. flags |= Arguments | NoCompact; break;
  598. case 'A':
  599. flags |= Ascii;
  600. flags &= ~Vt100;
  601. flags &= ~Unicode;
  602. break;
  603. case 'c':
  604. flags |= NoCompact; break;
  605. case 'h':
  606. help(program, options);
  607. case 'H':
  608. hpid = optarg ? value<pid_t, 0, INT_MAX>(program, options) : getpid();
  609. flags |= Highlight;
  610. break;
  611. case 'G':
  612. flags |= Vt100;
  613. flags &= ~Ascii;
  614. flags &= ~Unicode;
  615. break;
  616. case 'k':
  617. flags |= ShowKernel; break;
  618. case 'l':
  619. flags |= Long; break;
  620. case 'n':
  621. flags |= NumericSort; break;
  622. case 'p':
  623. flags |= NoCompact | ShowPids; break;
  624. case 't':
  625. flags |= ShowTitles; break;
  626. case 'u':
  627. flags |= UidChanges; break;
  628. case 'U':
  629. flags |= Unicode;
  630. flags &= ~Ascii;
  631. flags &= ~Vt100;
  632. break;
  633. case 'V':
  634. flags |= Version; break;
  635. case 0:
  636. {
  637. std::string option(options[index].name);
  638. if (option == "pid")
  639. {
  640. pid = value<pid_t, 0, INT_MAX>(program, options);
  641. flags |= Pid;
  642. flags &= ~User;
  643. }
  644. else if (option == "user")
  645. {
  646. std::free(user);
  647. user = strdup(optarg);
  648. flags |= User;
  649. flags &= ~Pid;
  650. }
  651. }
  652. break;
  653. case '?':
  654. help(program, options, 1);
  655. }
  656. _forall (int, index, optind, argc)
  657. {
  658. bool success;
  659. optarg = argv[index];
  660. pid = value<pid_t, 0, INT_MAX>(program, options, &success);
  661. if (success)
  662. {
  663. flags |= Pid;
  664. flags &= ~User;
  665. }
  666. else
  667. {
  668. std::free(user);
  669. user = strdup(optarg);
  670. flags |= User;
  671. flags &= ~Pid;
  672. }
  673. }
  674. return flags;
  675. }
  676. int main(int argc, char *argv[])
  677. {
  678. pid_t hpid(0), pid(0);
  679. char *user(NULL);
  680. uint16_t flags(options(argc, argv, hpid, pid, user));
  681. if (flags & Version)
  682. {
  683. std::printf(DTPSTREE_PROGRAM " " DTPSTREE_VERSION "\n");
  684. return 0;
  685. }
  686. uid_t uid(0);
  687. if (flags & User)
  688. {
  689. errno = 0;
  690. passwd *us3r(getpwnam(user));
  691. if (!us3r)
  692. errno ? err(1, NULL) : errx(1, "Unknown user: \"%s\"", user);
  693. uid = us3r->pw_uid;
  694. }
  695. char error[_POSIX2_LINE_MAX];
  696. kvm_t *kd(kvm_openfiles(NULL, _PATH_DEVNULL, NULL, O_RDONLY, error));
  697. if (!kd)
  698. errx(1, "%s", error);
  699. typedef kinfo_proc *InfoProc;
  700. int count;
  701. InfoProc procs(kvm_getprocs(kd, KERN_PROC_PROC, 0, &count));
  702. if (!procs)
  703. errx(1, "%s", kvm_geterr(kd));
  704. PidMap pids;
  705. _forall (InfoProc, proc, procs, procs + count)
  706. if (flags & ShowKernel || proc->ki_ppid != 0 || proc->ki_pid == 1)
  707. pids[proc->ki_pid] = new Proc(flags, kd, proc);
  708. enum { PidSort, NameSort } sort(flags & NumericSort ? PidSort : NameSort);
  709. _foreach (PidMap, pid, pids)
  710. {
  711. Proc *proc(pid->second);
  712. PidMap::iterator parent(pids.find(proc->parent()));
  713. if (parent != pids.end())
  714. parent->second->child(proc);
  715. }
  716. if (flags & Highlight)
  717. {
  718. PidMap::iterator pid(pids.find(hpid));
  719. if (pid != pids.end())
  720. pid->second->highlight();
  721. }
  722. Tree tree(flags);
  723. if (flags & Pid)
  724. {
  725. PidMap::iterator p1d(pids.find(pid));
  726. if (p1d != pids.end())
  727. {
  728. Proc *proc(p1d->second);
  729. if (!(flags & NoCompact))
  730. proc->compact();
  731. switch (sort)
  732. {
  733. case PidSort:
  734. proc->printByPid(tree);
  735. break;
  736. case NameSort:
  737. proc->printByName(tree);
  738. }
  739. }
  740. }
  741. else
  742. {
  743. NameMap names;
  744. _foreach (PidMap, pid, pids)
  745. {
  746. Proc *proc(pid->second);
  747. if (proc->root(uid))
  748. names.insert(NameMap::value_type(proc->name(), proc));
  749. }
  750. if (!(flags & NoCompact))
  751. Proc::compact(names);
  752. switch (sort)
  753. {
  754. case PidSort:
  755. _foreach (PidMap, pid, pids)
  756. {
  757. Proc *proc(pid->second);
  758. if (proc->root(uid))
  759. proc->printByPid(tree);
  760. }
  761. break;
  762. case NameSort:
  763. _foreach (NameMap, name, names)
  764. name->second->printByName(tree);
  765. }
  766. }
  767. _foreach (PidMap, pid, pids)
  768. delete pid->second;
  769. return 0;
  770. }
  771. // display a tree of processes