monitor.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* vccd/monitor.c
  2. *
  3. * This file is part of vccd.
  4. *
  5. * vccd is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vccd is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vccd. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <sys/times.h>
  24. #include <config.h>
  25. #include <vcc/vccd.h>
  26. int allocated, current;
  27. extern int nusers, system_fd;
  28. #if CONFIG_ADVANCED_AMALLOC
  29. struct klist_node amalloc_log = KLIST_NODE_INIT(&amalloc_log);
  30. #endif
  31. void *__amalloc(int nbytes, const char *fn) {
  32. void *p;
  33. current += nbytes;
  34. allocated += nbytes;
  35. if (unlikely(!(p = malloc(nbytes)))) {
  36. vcc_log("*** malloc() failed\n");
  37. amalloc_report(NULL);
  38. return NULL;
  39. }
  40. #if CONFIG_ADVANCED_AMALLOC
  41. struct klist_node *n;
  42. if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
  43. vcc_log("*** malloc() failed. \n");
  44. return NULL;
  45. }
  46. klist_init(n);
  47. n->data = (char *) fn;
  48. n->sending = (void *) p;
  49. n->value = nbytes;
  50. klist_add(&amalloc_log, n);
  51. #else
  52. (void) fn;
  53. #endif
  54. return p;
  55. }
  56. int afree(void *p, int nbytes) {
  57. current -= nbytes;
  58. free(p);
  59. #if CONFIG_ADVANCED_AMALLOC
  60. struct klist_node *n;
  61. for (n = amalloc_log.next; n != &amalloc_log; n = n->next)
  62. if (n->sending == p)
  63. break;
  64. if (unlikely(n == &amalloc_log)) {
  65. vcc_log("*** broken amalloc_log. \n");
  66. return 1;
  67. }
  68. klist_del(&amalloc_log, n);
  69. free(n);
  70. #endif
  71. return 0;
  72. }
  73. int amalloc_report(char *s) {
  74. (void) s;
  75. printf("amalloc report: \n");
  76. printf("\tallocated\tused\n");
  77. printf("\t%d\t\t%d\n", allocated, current);
  78. printf("\t%d KB\t\t%d KB\n", allocated >> 10, current >> 10);
  79. #if CONFIG_ADVANCED_AMALLOC
  80. struct klist_node *n;
  81. printf("\nadvanced amalloc report: \n");
  82. for (n = amalloc_log.next; n != &amalloc_log; n = n->next)
  83. printf("\t%s: %ld bytes %ld KB\n", (char *) n->data, n->value, (n->value >> 10));
  84. #endif
  85. printf("\ntotal: %d users online. \n", nusers);
  86. return 0;
  87. }
  88. int monitor_info(int fd, struct vcc_request *req) {
  89. char msg[128];
  90. if (fd != system_fd) {
  91. relay_send_msg(fd, "sysinfo: only root can do that. ", "system");
  92. vcc_log("%d:%s tried to get sysinfo. \n", fd, req->usrname);
  93. vcc_info("WSYSINFO %s %d\n", req->usrname, fd);
  94. return 1;
  95. }
  96. sprintf(msg, "\namalloc info: \n%d (%d KB) allocated, %d (%d KB) using\n", allocated, allocated >> 10,
  97. current, current >> 10);
  98. relay_send_msg(fd, msg, "system");
  99. vcc_log("sysinfo sent to %d. \n", fd);
  100. return 0;
  101. }