/release/picobsd/tinyware/sps/sps.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 122 lines · 86 code · 5 blank · 31 comment · 19 complexity · 3b7c2b6e753361f712cd22fd7b6db2cb MD5 · raw file

  1. /*-
  2. * Copyright (c) 1998 Andrzej Bialecki
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * $FreeBSD$
  27. */
  28. /*
  29. * Small replacement for ps(1) - uses only sysctl(3) to retrieve info
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <sys/param.h>
  35. #include <sys/sysctl.h>
  36. #include <sys/stat.h>
  37. #include <sys/user.h>
  38. char p_stat[] = "?iRSTZWM";
  39. int
  40. main(int argc, char *argv[])
  41. {
  42. int mib[4], i, num, len, j, plen;
  43. char buf[MAXPATHLEN], vty[5], pst[5], wmesg[10];
  44. struct kinfo_proc *ki;
  45. char *t;
  46. int ma, mi;
  47. mib[0] = CTL_KERN;
  48. mib[1] = KERN_PROC;
  49. mib[2] = KERN_PROC_ALL;
  50. if (sysctl(mib, 3, NULL, &len, NULL, 0) != 0) {
  51. perror("sysctl sizing");
  52. exit(1);
  53. }
  54. t = (char *)malloc(len);
  55. if (sysctl(mib, 3, t, &len, NULL, 0) != 0) {
  56. perror("sysctl info");
  57. exit(1);
  58. }
  59. mib[2] = KERN_PROC_ARGS;
  60. num = len / KINFO_PROC_SIZE;
  61. i = 0;
  62. printf("USERNAME PID PPID PRI NICE TTY STAT WCHAN COMMAND\n");
  63. while(i < num) {
  64. ki = (struct kinfo_proc *)(t + (num - i - 1) * KINFO_PROC_SIZE);
  65. mib[3] = ki->ki_pid;
  66. plen = MAXPATHLEN;
  67. if (sysctl(mib, 4, buf, &plen, NULL, 0) != 0) {
  68. perror("sysctl cmd info");
  69. exit(1);
  70. }
  71. if (plen == 0) {
  72. sprintf(buf, "(%s)", ki->ki_comm);
  73. } else {
  74. for (j = 0; j < plen - 1; j++) {
  75. if (buf[j] == '\0') buf[j] = ' ';
  76. }
  77. }
  78. if (strcmp(ki->ki_wmesg, "") == 0) {
  79. sprintf(wmesg, "-");
  80. } else {
  81. strcpy(wmesg, ki->ki_wmesg);
  82. }
  83. ma = major(ki->ki_tdev);
  84. mi = minor(ki->ki_tdev);
  85. switch(ma) {
  86. case 255:
  87. strcpy(vty, "??");
  88. break;
  89. case 12:
  90. if(mi != 255) {
  91. sprintf(vty, "v%d", mi);
  92. break;
  93. }
  94. /* FALLTHROUGH */
  95. case 0:
  96. strcpy(vty, "con");
  97. break;
  98. case 5:
  99. sprintf(vty, "p%d", mi);
  100. break;
  101. }
  102. sprintf(pst, "%c", p_stat[ki->ki_stat]);
  103. printf("%8s %5u %5u %3d %4d %3s %-4s %-7s %s\n",
  104. ki->ki_login,
  105. ki->ki_pid,
  106. ki->ki_ppid,
  107. ki->ki_pri.pri_level, /* XXX check this */
  108. ki->ki_nice,
  109. vty,
  110. pst,
  111. wmesg,
  112. buf);
  113. i++;
  114. }
  115. free((void *)t);
  116. exit(0);
  117. }