ipc-thread.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #define __KERNEL__
  2. #include <asm/fcntl.h>
  3. #include <asm/mman.h>
  4. #include <asm/prctl.h>
  5. #include <asm/unistd.h>
  6. #include <errno.h>
  7. #include <linux/fcntl.h>
  8. #include <linux/stat.h>
  9. #include <pal.h>
  10. #include <pal_error.h>
  11. #include <shim_fs.h>
  12. #include <shim_handle.h>
  13. #include <shim_internal.h>
  14. #include <shim_ipc.h>
  15. #include <shim_table.h>
  16. #include <shim_thread.h>
  17. #include <shim_utils.h>
  18. static int parse_ipc_thread_name(const char* name, IDTYPE* pidptr, const char** next,
  19. size_t* next_len, const char** nextnext) {
  20. const char* p = name;
  21. IDTYPE pid = 0;
  22. if (*p == '/')
  23. p++;
  24. for (; *p && *p != '/'; p++) {
  25. if (*p < '0' || *p > '9')
  26. return -ENOENT;
  27. pid = pid * 10 + *p - '0';
  28. }
  29. if (next) {
  30. if (*(p++) == '/' && *p) {
  31. *next = p;
  32. if (next_len || nextnext)
  33. for (; *p && *p != '/'; p++)
  34. ;
  35. if (next_len)
  36. *next_len = p - *next;
  37. if (nextnext)
  38. *nextnext = (*(p++) == '/' && *p) ? p : NULL;
  39. } else {
  40. *next = NULL;
  41. }
  42. }
  43. if (pidptr)
  44. *pidptr = pid;
  45. return 0;
  46. }
  47. static int find_ipc_thread_link(const char* name, struct shim_qstr* link,
  48. struct shim_dentry** dentptr) {
  49. const char *next;
  50. const char *nextnext;
  51. size_t next_len;
  52. IDTYPE pid;
  53. int ret = parse_ipc_thread_name(name, &pid, &next, &next_len, &nextnext);
  54. if (ret < 0)
  55. return ret;
  56. struct shim_dentry* dent = NULL;
  57. enum pid_meta_code ipc_code;
  58. void* ipc_data = NULL;
  59. if (!memcmp(next, "root", next_len)) {
  60. ipc_code = PID_META_ROOT;
  61. goto do_ipc;
  62. }
  63. if (!memcmp(next, "cwd", next_len)) {
  64. ipc_code = PID_META_CWD;
  65. goto do_ipc;
  66. }
  67. if (!memcmp(next, "exe", next_len)) {
  68. ipc_code = PID_META_EXEC;
  69. goto do_ipc;
  70. }
  71. ret = -ENOENT;
  72. goto out;
  73. do_ipc:
  74. ret = ipc_pid_getmeta_send(pid, ipc_code, &ipc_data);
  75. if (ret < 0)
  76. goto out;
  77. if (link)
  78. qstrsetstr(link, (char*)ipc_data, strlen((char*)ipc_data));
  79. if (dentptr) {
  80. /* XXX: Not sure how to handle this case yet */
  81. __abort();
  82. ret = path_lookupat(NULL, (char*)ipc_data, 0, &dent, NULL);
  83. if (ret < 0)
  84. goto out;
  85. get_dentry(dent);
  86. *dentptr = dent;
  87. }
  88. out:
  89. if (dent)
  90. put_dentry(dent);
  91. return ret;
  92. }
  93. static int proc_ipc_thread_link_open(struct shim_handle* hdl, const char* name, int flags) {
  94. struct shim_dentry* dent;
  95. int ret = find_ipc_thread_link(name, NULL, &dent);
  96. if (ret < 0)
  97. return ret;
  98. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
  99. ret = -EACCES;
  100. goto out;
  101. }
  102. ret = dent->fs->d_ops->open(hdl, dent, flags);
  103. out:
  104. put_dentry(dent);
  105. return 0;
  106. }
  107. static int proc_ipc_thread_link_mode(const char* name, mode_t* mode) {
  108. struct shim_dentry* dent;
  109. int ret = find_ipc_thread_link(name, NULL, &dent);
  110. if (ret < 0)
  111. return ret;
  112. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode) {
  113. ret = -EACCES;
  114. goto out;
  115. }
  116. ret = dent->fs->d_ops->mode(dent, mode);
  117. out:
  118. put_dentry(dent);
  119. return ret;
  120. }
  121. static int proc_ipc_thread_link_stat(const char* name, struct stat* buf) {
  122. struct shim_dentry* dent;
  123. int ret = find_ipc_thread_link(name, NULL, &dent);
  124. if (ret < 0)
  125. return ret;
  126. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
  127. ret = -EACCES;
  128. goto out;
  129. }
  130. ret = dent->fs->d_ops->stat(dent, buf);
  131. out:
  132. put_dentry(dent);
  133. return ret;
  134. }
  135. static int proc_ipc_thread_link_follow_link(const char* name, struct shim_qstr* link) {
  136. return find_ipc_thread_link(name, link, NULL);
  137. }
  138. static const struct proc_fs_ops fs_ipc_thread_link = {
  139. .open = &proc_ipc_thread_link_open,
  140. .mode = &proc_ipc_thread_link_mode,
  141. .stat = &proc_ipc_thread_link_stat,
  142. .follow_link = &proc_ipc_thread_link_follow_link,
  143. };
  144. static struct pid_status_cache {
  145. uint32_t ref_count;
  146. bool dirty;
  147. size_t nstatus;
  148. struct pid_status* status;
  149. } * pid_status_cache;
  150. static struct shim_lock status_lock;
  151. static int proc_match_ipc_thread(const char* name) {
  152. IDTYPE pid;
  153. if (parse_ipc_thread_name(name, &pid, NULL, NULL, NULL) < 0)
  154. return 0;
  155. if (!create_lock_runtime(&status_lock)) {
  156. return -ENOMEM;
  157. }
  158. lock(&status_lock);
  159. if (pid_status_cache)
  160. for (size_t i = 0; i < pid_status_cache->nstatus; i++)
  161. if (pid_status_cache->status[i].pid == pid) {
  162. unlock(&status_lock);
  163. return 1;
  164. }
  165. unlock(&status_lock);
  166. return 0;
  167. }
  168. static int proc_ipc_thread_dir_mode(const char* name, mode_t* mode) {
  169. const char* next;
  170. size_t next_len;
  171. IDTYPE pid;
  172. int ret = parse_ipc_thread_name(name, &pid, &next, &next_len, NULL);
  173. if (ret < 0)
  174. return ret;
  175. if (!create_lock_runtime(&status_lock)) {
  176. return -ENOMEM;
  177. }
  178. lock(&status_lock);
  179. if (pid_status_cache)
  180. for (size_t i = 0; i < pid_status_cache->nstatus; i++)
  181. if (pid_status_cache->status[i].pid == pid) {
  182. unlock(&status_lock);
  183. *mode = 0500;
  184. return 0;
  185. }
  186. unlock(&status_lock);
  187. return -ENOENT;
  188. }
  189. static int proc_ipc_thread_dir_stat(const char* name, struct stat* buf) {
  190. const char* next;
  191. size_t next_len;
  192. IDTYPE pid;
  193. int ret = parse_ipc_thread_name(name, &pid, &next, &next_len, NULL);
  194. if (ret < 0)
  195. return ret;
  196. if (!create_lock_runtime(&status_lock)) {
  197. return -ENOMEM;
  198. }
  199. lock(&status_lock);
  200. if (pid_status_cache)
  201. for (size_t i = 0; i < pid_status_cache->nstatus; i++)
  202. if (pid_status_cache->status[i].pid == pid) {
  203. memset(buf, 0, sizeof(struct stat));
  204. buf->st_dev = buf->st_ino = 1;
  205. buf->st_mode = 0500 | S_IFDIR;
  206. buf->st_uid = 0; /* XXX */
  207. buf->st_gid = 0; /* XXX */
  208. buf->st_size = 4096;
  209. unlock(&status_lock);
  210. return 0;
  211. }
  212. unlock(&status_lock);
  213. return -ENOENT;
  214. }
  215. int get_all_pid_status(struct pid_status** status);
  216. static int proc_list_ipc_thread(const char* name, struct shim_dirent** buf, int len) {
  217. // Only one valid name
  218. __UNUSED(name);
  219. struct pid_status_cache* status = NULL;
  220. int ret = 0;
  221. if (!create_lock_runtime(&status_lock)) {
  222. return -ENOMEM;
  223. }
  224. lock(&status_lock);
  225. if (pid_status_cache && !pid_status_cache->dirty) {
  226. status = pid_status_cache;
  227. status->ref_count++;
  228. }
  229. unlock(&status_lock);
  230. if (!status) {
  231. status = malloc(sizeof(struct pid_status_cache));
  232. if (!status)
  233. return -ENOMEM;
  234. ret = get_all_pid_status(&status->status);
  235. if (ret < 0) {
  236. free(status);
  237. return ret;
  238. }
  239. status->nstatus = ret;
  240. status->ref_count = 1;
  241. status->dirty = false;
  242. lock(&status_lock);
  243. if (pid_status_cache) {
  244. if (pid_status_cache->dirty) {
  245. if (!pid_status_cache->ref_count)
  246. free(pid_status_cache);
  247. pid_status_cache = status;
  248. } else {
  249. if (status->nstatus)
  250. free(status->status);
  251. free(status);
  252. status = pid_status_cache;
  253. status->ref_count++;
  254. }
  255. } else {
  256. pid_status_cache = status;
  257. }
  258. unlock(&status_lock);
  259. }
  260. if (!status->nstatus)
  261. goto success;
  262. struct shim_dirent* ptr = (*buf);
  263. void* buf_end = (void*)ptr + len;
  264. for (size_t i = 0; i < status->nstatus; i++) {
  265. if (status->status[i].pid != status->status[i].tgid)
  266. continue;
  267. IDTYPE pid = status->status[i].pid;
  268. int p = pid, l = 0;
  269. for (; p; p /= 10, l++)
  270. ;
  271. if ((void*)(ptr + 1) + l + 1 > buf_end) {
  272. ret = -ENOBUFS;
  273. goto err;
  274. }
  275. ptr->next = (void*)(ptr + 1) + l + 1;
  276. ptr->ino = 1;
  277. ptr->type = LINUX_DT_DIR;
  278. ptr->name[l--] = 0;
  279. for (p = pid; p; p /= 10) {
  280. ptr->name[l--] = p % 10 + '0';
  281. }
  282. ptr = ptr->next;
  283. }
  284. *buf = ptr;
  285. success:
  286. lock(&status_lock);
  287. status->dirty = true;
  288. status->ref_count--;
  289. if (!status->ref_count && status != pid_status_cache)
  290. free(status);
  291. unlock(&status_lock);
  292. return 0;
  293. err:
  294. lock(&status_lock);
  295. status->ref_count--;
  296. if (!status->ref_count && status != pid_status_cache)
  297. free(status);
  298. unlock(&status_lock);
  299. return ret;
  300. }
  301. const struct proc_nm_ops nm_ipc_thread = {
  302. .match_name = &proc_match_ipc_thread,
  303. .list_name = &proc_list_ipc_thread,
  304. };
  305. const struct proc_fs_ops fs_ipc_thread = {
  306. .mode = &proc_ipc_thread_dir_mode,
  307. .stat = &proc_ipc_thread_dir_stat,
  308. };
  309. const struct proc_dir dir_ipc_thread = {
  310. .size = 0,
  311. .ent =
  312. {
  313. {
  314. .name = "cwd",
  315. .fs_ops = &fs_ipc_thread_link,
  316. },
  317. {
  318. .name = "exe",
  319. .fs_ops = &fs_ipc_thread_link,
  320. },
  321. {
  322. .name = "root",
  323. .fs_ops = &fs_ipc_thread_link,
  324. },
  325. },
  326. };