sysinfo.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <strings.h>
  9. #define MEMINFO_FILE "/proc/meminfo"
  10. #define MTAB_FILE "/etc/mtab"
  11. #define BAD_OPEN_MESSAGE \
  12. "Error: /proc must be mounted\n" \
  13. " To mount /proc at boot you need an /etc/fstab line like:\n" \
  14. " /proc /proc proc defaults\n" \
  15. " In the meantime, run \"mount /proc /proc -t proc\"\n"
  16. /* This macro opens filename only if necessary and seeks to 0 so
  17. * that successive calls to the functions are more efficient.
  18. * It also reads the current contents of the file into the global buf.
  19. */
  20. #define FILE_TO_BUF(filename) do{ \
  21. static int fd, local_n; \
  22. if ((fd = open(filename, O_RDONLY)) == -1) { \
  23. fputs(BAD_OPEN_MESSAGE, stderr); \
  24. fflush(NULL); \
  25. _exit(102); \
  26. } \
  27. lseek(fd, 0L, SEEK_SET); \
  28. if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \
  29. perror(filename); \
  30. fflush(NULL); \
  31. _exit(103); \
  32. } \
  33. buf[local_n] = '\0'; \
  34. close(fd); \
  35. }while(0)
  36. typedef struct mem_table_struct {
  37. const char *name; /* memory type name */
  38. unsigned long *slot; /* slot in return struct */
  39. } mem_table_struct;
  40. static int compare_mem_table_structs(const void *a, const void *b){
  41. return strcmp(((const mem_table_struct*)a)->name,((const mem_table_struct*)b)->name);
  42. }
  43. size_t get_free_memory(void){
  44. char buf[4096];
  45. unsigned long kb_main_buffers, kb_main_cached, kb_main_free;
  46. char namebuf[16]; /* big enough to hold any row name */
  47. mem_table_struct findme = { namebuf, NULL};
  48. mem_table_struct *found;
  49. char *head;
  50. char *tail;
  51. const mem_table_struct mem_table[] = {
  52. {"Buffers", &kb_main_buffers}, // important
  53. {"Cached", &kb_main_cached}, // important
  54. {"MemFree", &kb_main_free}, // important
  55. };
  56. const int mem_table_count = sizeof(mem_table)/sizeof(mem_table_struct);
  57. FILE_TO_BUF(MEMINFO_FILE);
  58. head = buf;
  59. for(;;){
  60. tail = strchr(head, ':');
  61. if(!tail) break;
  62. *tail = '\0';
  63. if(strlen(head) >= sizeof(namebuf)){
  64. head = tail+1;
  65. goto nextline;
  66. }
  67. strcpy(namebuf,head);
  68. found = bsearch(&findme, mem_table, mem_table_count,
  69. sizeof(mem_table_struct), compare_mem_table_structs
  70. );
  71. head = tail+1;
  72. if(!found) goto nextline;
  73. *(found->slot) = strtoul(head,&tail,10);
  74. nextline:
  75. tail = strchr(head, '\n');
  76. if(!tail) break;
  77. head = tail+1;
  78. }
  79. return (kb_main_buffers + kb_main_cached + kb_main_free) * 1024;
  80. }
  81. int get_file_fs(const char *fname, size_t size, char *fs) {
  82. int err = 0;
  83. char buf[4096];
  84. char *fn;
  85. char *head;
  86. char *tail;
  87. size_t len, max = 0;
  88. struct stat st;
  89. if ((!fname)||(!fs)||(size < 3)) return -1;
  90. if (*fname == '/') {
  91. fn = (char*)fname;
  92. } else {
  93. if (!getcwd(buf, 4095)) return -1;
  94. fn = malloc(strlen(fname) + strlen(buf) + 2);
  95. if (!fn) return -1;
  96. sprintf(fn, "%s/%s", buf, fname);
  97. }
  98. if (!stat(fn, &st)) {
  99. if (S_ISBLK(st.st_mode)) {
  100. strcpy(fs, "raw");
  101. goto clean;
  102. }
  103. }
  104. FILE_TO_BUF(MTAB_FILE);
  105. head = buf;
  106. for(;;){
  107. head = strchr(head, ' ');
  108. if(!head) break;
  109. head += 1;
  110. tail = strchr(head, ' ');
  111. if(!tail) break;
  112. *tail = '\0';
  113. len = strlen(head);
  114. if((len <= max)||(strncmp(head, fn, len))) {
  115. head = tail+1;
  116. goto nextline;
  117. }
  118. head = tail + 1;
  119. tail = strchr(head, ' ');
  120. if(!tail) break;
  121. *tail = '\0';
  122. if (!strncasecmp(head,"root",4)) {
  123. head = tail+1;
  124. goto nextline;
  125. }
  126. max = len;
  127. if (strlen(head) >= size) err = -1;
  128. else {
  129. err = 0;
  130. strcpy(fs, head);
  131. }
  132. head = tail+1;
  133. nextline:
  134. tail = strchr(head, '\n');
  135. if(!tail) break;
  136. head = tail+1;
  137. }
  138. clean:
  139. if (fn != fname) free(fn);
  140. return err;
  141. }