ipc-thread.c 9.5 KB

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