info.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <shim_internal.h>
  4. #include <shim_fs.h>
  5. #include <pal.h>
  6. #include <pal_error.h>
  7. #include <asm/fcntl.h>
  8. #include <asm/mman.h>
  9. #include <asm/unistd.h>
  10. #include <asm/prctl.h>
  11. #include <errno.h>
  12. #define MEMINFO_READ_PASSTHROUGH 1
  13. #define CPUINFO_READ_PASSTHROUGH 1
  14. static int proc_info_mode (const char * name, mode_t * mode)
  15. {
  16. *mode = 0444;
  17. return 0;
  18. }
  19. static int proc_info_stat (const char * name, struct stat * buf)
  20. {
  21. memset(buf, 0, sizeof(struct stat));
  22. buf->st_dev = buf->st_ino = 1;
  23. buf->st_mode = 0444|S_IFDIR;
  24. buf->st_uid = 0;
  25. buf->st_gid = 0;
  26. buf->st_size = 0;
  27. return 0;
  28. }
  29. #if MEMINFO_READ_PASSTHROUGH == 1 || CPUINFO_READ_PASSTHROUGH == 1
  30. # define DEFAULT_BUFFER_SIZE 256
  31. static int proc_info_read_passthrough (const char * uri, char ** strptr)
  32. {
  33. int size = DEFAULT_BUFFER_SIZE;
  34. char * strbuf = malloc(size);
  35. int bytes = 0, ret = 0;
  36. if (!strbuf) {
  37. ret = -ENOMEM;
  38. goto out;
  39. }
  40. PAL_HANDLE hdl = DkStreamOpen(uri, PAL_ACCESS_RDONLY, 0, 0, 0);
  41. if (!hdl)
  42. return -PAL_ERRNO;
  43. retry:
  44. ret = DkStreamRead(hdl, bytes, size - bytes, strbuf + bytes, NULL, 0);
  45. if (!ret) {
  46. ret = -PAL_ERRNO;
  47. goto out_free;
  48. }
  49. bytes += ret;
  50. if (bytes == size) {
  51. char * newbuf = malloc(size * 2);
  52. memcpy(newbuf, strbuf, size);
  53. free(strbuf);
  54. strbuf = newbuf;
  55. size *= 2;
  56. goto retry;
  57. }
  58. ret = bytes;
  59. *strptr = strbuf;
  60. goto out;
  61. out_free:
  62. free(strbuf);
  63. out:
  64. DkObjectClose(hdl);
  65. return ret;
  66. }
  67. #endif
  68. static int proc_meminfo_open (struct shim_handle * hdl, const char * name,
  69. int flags)
  70. {
  71. if (flags & (O_WRONLY|O_RDWR))
  72. return -EACCES;
  73. char * str = NULL;
  74. int ret = 0, len = 0;
  75. #if MEMINFO_READ_PASSTHROUGH == 1
  76. ret = proc_info_read_passthrough("file:/proc/meminfo", &str);
  77. if (ret >= 0) {
  78. len = ret;
  79. ret = 0;
  80. }
  81. #else
  82. ret = -EACCES;
  83. #endif
  84. if (ret < 0)
  85. return ret;
  86. struct shim_str_data * data = malloc(sizeof(struct shim_str_data));
  87. if (!data) {
  88. free(str);
  89. return -ENOMEM;
  90. }
  91. memset(data, 0, sizeof(struct shim_str_data));
  92. data->str = str;
  93. data->len = len;
  94. hdl->type = TYPE_STR;
  95. hdl->flags = flags & ~O_RDONLY;
  96. hdl->acc_mode = MAY_READ;
  97. hdl->info.str.data = data;
  98. return 0;
  99. }
  100. static int proc_cpuinfo_open (struct shim_handle * hdl, const char * name,
  101. int flags)
  102. {
  103. if (flags & (O_WRONLY|O_RDWR))
  104. return -EACCES;
  105. char * str = NULL;
  106. int ret = 0, len = 0;
  107. #if CPUINFO_READ_PASSTHROUGH == 1
  108. ret = proc_info_read_passthrough("file:/proc/cpuinfo", &str);
  109. if (ret >= 0) {
  110. len = ret;
  111. ret = 0;
  112. }
  113. #else
  114. ret = -EACCES;
  115. #endif
  116. if (ret < 0)
  117. return ret;
  118. struct shim_str_data * data = malloc(sizeof(struct shim_str_data));
  119. if (!data) {
  120. free(str);
  121. return -ENOMEM;
  122. }
  123. memset(data, 0, sizeof(struct shim_str_data));
  124. data->str = str;
  125. data->len = len;
  126. hdl->type = TYPE_STR;
  127. hdl->flags = flags & ~O_RDONLY;
  128. hdl->acc_mode = MAY_READ;
  129. hdl->info.str.data = data;
  130. return 0;
  131. }
  132. struct proc_fs_ops fs_meminfo = {
  133. .mode = &proc_info_mode,
  134. .stat = &proc_info_stat,
  135. .open = &proc_meminfo_open,
  136. };
  137. struct proc_fs_ops fs_cpuinfo = {
  138. .mode = &proc_info_mode,
  139. .stat = &proc_info_stat,
  140. .open = &proc_cpuinfo_open,
  141. };