thread.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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_table.h>
  5. #include <shim_thread.h>
  6. #include <shim_handle.h>
  7. #include <shim_fs.h>
  8. #include <shim_utils.h>
  9. #include <pal.h>
  10. #include <pal_error.h>
  11. #include <asm/fcntl.h>
  12. #include <asm/mman.h>
  13. #include <asm/unistd.h>
  14. #include <asm/prctl.h>
  15. #include <errno.h>
  16. #define DEFAULT_BUFFER_SIZE 256
  17. static int parse_thread_name (const char * name,
  18. const char ** next, int * next_len,
  19. const char ** nextnext)
  20. {
  21. const char * p = name;
  22. int pid = 0;
  23. if (*p == '/')
  24. p++;
  25. if (!memcmp(p, "self", 4) && (!*(p + 4) || *(p + 4) == '/')) {
  26. p += 4;
  27. pid = get_cur_tid();
  28. } else {
  29. for ( ; *p && *p != '/' ; p++) {
  30. if (*p < '0' || *p > '9')
  31. return -ENOENT;
  32. pid = pid * 10 + *p - '0';
  33. }
  34. }
  35. if (next) {
  36. if (*(p++) == '/' && *p) {
  37. *next = p;
  38. if (next_len || nextnext)
  39. for ( ; *p && *p != '/' ; p++);
  40. if (next_len)
  41. *next_len = p - *next;
  42. if (nextnext)
  43. *nextnext = (*(p++) == '/' && *p) ? p : NULL;
  44. } else {
  45. *next = NULL;
  46. }
  47. }
  48. return pid;
  49. }
  50. static int find_thread_link (const char * name, struct shim_qstr * link,
  51. struct shim_dentry ** dentptr,
  52. struct shim_thread ** threadptr)
  53. {
  54. const char * next, * nextnext;
  55. int next_len;
  56. int pid = parse_thread_name(name, &next, &next_len, &nextnext);
  57. if (pid < 0)
  58. return pid;
  59. struct shim_thread * thread = lookup_thread(pid);
  60. struct shim_dentry * dent = NULL;
  61. int ret = 0;
  62. if (!thread)
  63. return -ENOENT;
  64. if (!thread->in_vm) {
  65. ret = -ENOENT;
  66. goto out;
  67. }
  68. lock(thread->lock);
  69. if (next_len == 4 && !memcmp(next, "root", next_len)) {
  70. dent = thread->root;
  71. get_dentry(dent);
  72. }
  73. if (next_len == 3 && !memcmp(next, "cwd", next_len)) {
  74. dent = thread->cwd;
  75. get_dentry(dent);
  76. }
  77. if (next_len == 3 && !memcmp(next, "exe", next_len)) {
  78. struct shim_handle * exec = thread->exec;
  79. if (!exec->dentry) {
  80. unlock(thread->lock);
  81. ret = -ENOENT;
  82. goto out;
  83. }
  84. dent = exec->dentry;
  85. get_dentry(dent);
  86. }
  87. unlock(thread->lock);
  88. if (nextnext) {
  89. struct shim_dentry * next_dent = NULL;
  90. ret = path_lookupat(dent, nextnext, 0, &next_dent);
  91. if (ret < 0)
  92. goto out;
  93. put_dentry(dent);
  94. dent = next_dent;
  95. }
  96. if (link) {
  97. int size;
  98. char * path = dentry_get_path(dent, true, &size);
  99. qstrsetstr(link, path, size);
  100. }
  101. if (dentptr) {
  102. get_dentry(dent);
  103. *dentptr = dent;
  104. }
  105. if (threadptr) {
  106. get_thread(thread);
  107. *threadptr = thread;
  108. }
  109. ret = 0;
  110. out:
  111. if (dent)
  112. put_dentry(dent);
  113. if (thread)
  114. put_thread(thread);
  115. return ret;
  116. }
  117. static int proc_thread_link_open (struct shim_handle * hdl,
  118. const char * name, int flags)
  119. {
  120. struct shim_dentry * dent;
  121. int ret = find_thread_link(name, NULL, &dent, NULL);
  122. if (ret < 0)
  123. return ret;
  124. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
  125. ret = -EACCES;
  126. goto out;
  127. }
  128. ret = dent->fs->d_ops->open(hdl, dent, flags);
  129. out:
  130. put_dentry(dent);
  131. return 0;
  132. }
  133. static int proc_thread_link_mode (const char * name, mode_t * mode)
  134. {
  135. struct shim_dentry * dent;
  136. int ret = find_thread_link(name, NULL, &dent, NULL);
  137. if (ret < 0)
  138. return ret;
  139. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode) {
  140. ret = -EACCES;
  141. goto out;
  142. }
  143. ret = dent->fs->d_ops->mode(dent, mode, true);
  144. out:
  145. put_dentry(dent);
  146. return ret;
  147. }
  148. static int proc_thread_link_stat (const char * name, struct stat * buf)
  149. {
  150. struct shim_dentry * dent;
  151. int ret = find_thread_link(name, NULL, &dent, NULL);
  152. if (ret < 0)
  153. return ret;
  154. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
  155. ret = -EACCES;
  156. goto out;
  157. }
  158. ret = dent->fs->d_ops->stat(dent, buf);
  159. out:
  160. put_dentry(dent);
  161. return ret;
  162. }
  163. static int proc_thread_link_follow_link (const char * name,
  164. struct shim_qstr * link)
  165. {
  166. return find_thread_link(name, link, NULL, NULL);
  167. }
  168. static const struct proc_fs_ops fs_thread_link = {
  169. .open = &proc_thread_link_open,
  170. .mode = &proc_thread_link_mode,
  171. .stat = &proc_thread_link_stat,
  172. .follow_link = &proc_thread_link_follow_link,
  173. };
  174. static int parse_thread_fd (const char * name, const char ** rest,
  175. struct shim_handle ** phdl)
  176. {
  177. const char * next, * nextnext;
  178. int next_len;
  179. int pid = parse_thread_name(name, &next, &next_len, &nextnext);
  180. if (!pid)
  181. return pid;
  182. if (!next || !nextnext || memcmp(next, "fd", next_len))
  183. return -EINVAL;
  184. const char * p = nextnext;
  185. int fd = 0;
  186. for ( ; *p && *p != '/' ; p++) {
  187. if (*p < '0' || *p > '9')
  188. return -ENOENT;
  189. fd = fd * 10 + *p - '0';
  190. if (fd >= MAX_FDS)
  191. return -ENOENT;
  192. }
  193. struct shim_thread * thread = lookup_thread(pid);
  194. if (!thread)
  195. return -ENOENT;
  196. struct shim_handle_map * handle_map = get_cur_handle_map(thread);
  197. lock(handle_map->lock);
  198. if (fd >= handle_map->fd_top ||
  199. handle_map->map[fd] == NULL ||
  200. handle_map->map[fd]->handle == NULL) {
  201. unlock(handle_map->lock);
  202. return -ENOENT;
  203. }
  204. if (phdl)
  205. *phdl = handle_map->map[fd]->handle;
  206. unlock(handle_map->lock);
  207. if (rest)
  208. *rest = *p ? p + 1 : NULL;
  209. return 0;
  210. }
  211. static int proc_match_thread_each_fd (const char * name)
  212. {
  213. return parse_thread_fd(name, NULL, NULL) == 0 ? 1 : 0;
  214. }
  215. static int proc_list_thread_each_fd (const char * name,
  216. struct shim_dirent ** buf, int count)
  217. {
  218. const char * next;
  219. int next_len;
  220. int pid = parse_thread_name(name, &next, &next_len, NULL);
  221. if (!pid)
  222. return pid;
  223. if (!next || memcmp(next, "fd", next_len))
  224. return -EINVAL;
  225. struct shim_thread * thread = lookup_thread(pid);
  226. if (!thread)
  227. return -ENOENT;
  228. struct shim_handle_map * handle_map = get_cur_handle_map(thread);
  229. int err = 0, bytes = 0;
  230. struct shim_dirent * dirent = *buf, ** last = NULL;
  231. lock(handle_map->lock);
  232. for (int i = 0 ; i < handle_map->fd_size ; i++, dirent = dirent->next)
  233. if (handle_map->map[i] &&
  234. handle_map->map[i]->handle) {
  235. int d = i, l = 0;
  236. for ( ; d ; d /= 10, l++);
  237. l = l ? : 1;
  238. bytes += sizeof(struct shim_dirent) + l + 1;
  239. if (bytes > count) {
  240. err = -ENOMEM;
  241. break;
  242. }
  243. dirent->next = (void *) (dirent + 1) + l + 1;
  244. dirent->ino = 1;
  245. dirent->type = LINUX_DT_LNK;
  246. dirent->name[0] = '0';
  247. dirent->name[l--] = 0;
  248. for (d = i ; d ; d /= 10)
  249. dirent->name[l--] = '0' + d % 10;
  250. last = &dirent->next;
  251. }
  252. unlock(handle_map->lock);
  253. put_thread(thread);
  254. if (last)
  255. *last = NULL;
  256. *buf = dirent;
  257. return err;
  258. }
  259. static const struct proc_nm_ops nm_thread_each_fd = {
  260. .match_name = &proc_match_thread_each_fd,
  261. .list_name = &proc_list_thread_each_fd,
  262. };
  263. static int find_thread_each_fd (const char * name, struct shim_qstr * link,
  264. struct shim_dentry ** dentptr)
  265. {
  266. const char * rest;
  267. struct shim_handle * handle;
  268. struct shim_dentry * dent = NULL;
  269. int ret;
  270. if ((ret = parse_thread_fd(name, &rest, &handle)) < 0)
  271. return ret;
  272. lock(handle->lock);
  273. if (handle->dentry) {
  274. dent = handle->dentry;
  275. get_dentry(dent);
  276. }
  277. unlock(handle->lock);
  278. if (!dent) {
  279. ret = -ENOENT;
  280. goto out;
  281. }
  282. if (rest) {
  283. struct shim_dentry * next_dent = NULL;
  284. ret = path_lookupat(dent, rest, 0, &next_dent);
  285. if (ret < 0)
  286. goto out;
  287. put_dentry(dent);
  288. dent = next_dent;
  289. }
  290. if (link) {
  291. int size;
  292. char * path = dentry_get_path(dent, true, &size);
  293. qstrsetstr(link, path, size);
  294. }
  295. if (dentptr) {
  296. get_dentry(dent);
  297. *dentptr = dent;
  298. }
  299. out:
  300. if (dent)
  301. put_dentry(dent);
  302. put_handle(handle);
  303. return ret;
  304. }
  305. static int proc_thread_each_fd_open (struct shim_handle * hdl,
  306. const char * name, int flags)
  307. {
  308. struct shim_dentry * dent;
  309. int ret = find_thread_each_fd(name, NULL, &dent);
  310. if (ret < 0)
  311. return ret;
  312. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
  313. ret = -EACCES;
  314. goto out;
  315. }
  316. ret = dent->fs->d_ops->open(hdl, dent, flags);
  317. out:
  318. put_dentry(dent);
  319. return 0;
  320. }
  321. static int proc_thread_each_fd_mode (const char * name, mode_t * mode)
  322. {
  323. struct shim_dentry * dent;
  324. int ret = find_thread_each_fd(name, NULL, &dent);
  325. if (ret < 0)
  326. return ret;
  327. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode) {
  328. ret = -EACCES;
  329. goto out;
  330. }
  331. ret = dent->fs->d_ops->mode(dent, mode, true);
  332. out:
  333. put_dentry(dent);
  334. return 0;
  335. }
  336. static int proc_thread_each_fd_stat (const char * name, struct stat * buf)
  337. {
  338. struct shim_dentry * dent;
  339. int ret = find_thread_each_fd(name, NULL, &dent);
  340. if (ret < 0)
  341. return ret;
  342. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
  343. ret = -EACCES;
  344. goto out;
  345. }
  346. ret = dent->fs->d_ops->stat(dent, buf);
  347. out:
  348. put_dentry(dent);
  349. return 0;
  350. }
  351. static int proc_thread_each_fd_follow_link (const char * name,
  352. struct shim_qstr * link)
  353. {
  354. return find_thread_each_fd(name, link, NULL);
  355. }
  356. static const struct proc_fs_ops fs_thread_each_fd = {
  357. .open = &proc_thread_each_fd_open,
  358. .mode = &proc_thread_each_fd_mode,
  359. .stat = &proc_thread_each_fd_stat,
  360. .follow_link = &proc_thread_each_fd_follow_link,
  361. };
  362. static const struct proc_dir dir_fd = { .size = 1, .ent = { {
  363. .nm_ops = &nm_thread_each_fd, .fs_ops = &fs_thread_each_fd,
  364. }, }, };
  365. static int proc_thread_maps_open (struct shim_handle * hdl,
  366. const char * name, int flags)
  367. {
  368. if (flags & (O_WRONLY|O_RDWR))
  369. return -EACCES;
  370. const char * next;
  371. int next_len;
  372. int pid = parse_thread_name(name, &next, &next_len, NULL);
  373. if (pid < 0)
  374. return pid;
  375. struct shim_thread * thread = lookup_thread(pid);
  376. if (!thread)
  377. return -ENOENT;
  378. int size = DEFAULT_BUFFER_SIZE;
  379. char * strbuf = malloc(size);
  380. int ret = 0, len = 0;
  381. if (!strbuf) {
  382. ret = -ENOMEM;
  383. goto out;
  384. }
  385. retry:
  386. ret = dump_all_vmas(thread, strbuf, size);
  387. if (ret == -EOVERFLOW) {
  388. char * newbuf = malloc(size * 2);
  389. if (!newbuf) {
  390. ret = -ENOMEM;
  391. goto err;
  392. }
  393. free(strbuf);
  394. strbuf = newbuf;
  395. size *= 2;
  396. goto retry;
  397. }
  398. if (ret < 0)
  399. goto err;
  400. len = ret;
  401. struct shim_str_data * data = malloc(sizeof(struct shim_str_data));
  402. if (!data) {
  403. ret = -ENOMEM;
  404. goto err;
  405. }
  406. memset(data, 0, sizeof(struct shim_str_data));
  407. data->str = strbuf;
  408. data->len = len;
  409. hdl->type = TYPE_STR;
  410. hdl->flags = flags & ~O_RDONLY;
  411. hdl->acc_mode = MAY_READ;
  412. hdl->info.str.data = data;
  413. ret = 0;
  414. out:
  415. put_thread(thread);
  416. return ret;
  417. err:
  418. free(strbuf);
  419. goto out;
  420. }
  421. static int proc_thread_maps_mode (const char * name, mode_t * mode)
  422. {
  423. *mode = 0400;
  424. return 0;
  425. }
  426. static int proc_thread_maps_stat (const char * name, struct stat * buf)
  427. {
  428. memset(buf, 0, sizeof(struct stat));
  429. buf->st_dev = buf->st_ino = 1;
  430. buf->st_mode = 0400|S_IFREG;
  431. buf->st_uid = 0;
  432. buf->st_gid = 0;
  433. buf->st_size = 0;
  434. return 0;
  435. }
  436. static const struct proc_fs_ops fs_thread_maps = {
  437. .open = &proc_thread_maps_open,
  438. .mode = &proc_thread_maps_mode,
  439. .stat = &proc_thread_maps_stat,
  440. };
  441. static int proc_thread_dir_mode (const char * name, mode_t * mode)
  442. {
  443. const char * next;
  444. int next_len;
  445. int pid = parse_thread_name(name, &next, &next_len, NULL);
  446. if (pid < 0)
  447. return pid;
  448. *mode = 0500;
  449. return 0;
  450. }
  451. static int proc_thread_dir_stat (const char * name, struct stat * buf)
  452. {
  453. const char * next;
  454. int next_len;
  455. int pid = parse_thread_name(name, &next, &next_len, NULL);
  456. if (pid < 0)
  457. return pid;
  458. struct shim_thread * thread = lookup_thread(pid);
  459. if (!thread)
  460. return -ENOENT;
  461. memset(buf, 0, sizeof(struct stat));
  462. buf->st_dev = buf->st_ino = 1;
  463. buf->st_mode = 0500|S_IFDIR;
  464. lock(thread->lock);
  465. buf->st_uid = thread->uid;
  466. buf->st_gid = thread->gid;
  467. unlock(thread->lock);
  468. buf->st_size = 4096;
  469. return 0;
  470. }
  471. static const struct proc_fs_ops fs_thread_fd = {
  472. .mode = &proc_thread_dir_mode,
  473. .stat = &proc_thread_dir_stat,
  474. };
  475. static int proc_match_thread (const char * name)
  476. {
  477. int pid = parse_thread_name(name, NULL, NULL, NULL);
  478. if (pid < 0)
  479. return 0;
  480. struct shim_thread * thread = lookup_thread(pid);
  481. return thread ? 1 : 0;
  482. }
  483. static int proc_list_thread (const char * name, struct shim_dirent ** buf,
  484. int len)
  485. {
  486. struct walk_thread_arg {
  487. struct shim_dirent * buf, * buf_end;
  488. } args = {
  489. .buf = *buf, .buf_end = (void *) *buf + len,
  490. };
  491. int walk_cb (struct shim_thread * thread, void * arg, bool * unlocked) {
  492. struct walk_thread_arg * args = (struct walk_thread_arg *) arg;
  493. IDTYPE pid = thread->tid;
  494. int p = pid, l = 0;
  495. for ( ; p ; p /= 10, l++);
  496. if ((void *) (args->buf + 1) + l + 1 > (void *) args->buf_end)
  497. return -ENOBUFS;
  498. struct shim_dirent * buf = args->buf;
  499. buf->next = (void *) (buf + 1) + l + 1;
  500. buf->ino = 1;
  501. buf->type = LINUX_DT_DIR;
  502. buf->name[l--] = 0;
  503. for (p = pid ; p ; p /= 10)
  504. buf->name[l--] = p % 10 + '0';
  505. args->buf = buf->next;
  506. return 1;
  507. }
  508. int ret = walk_thread_list(&walk_cb, &args, false);
  509. if (ret < 0)
  510. return ret;
  511. *buf = args.buf;
  512. return 0;
  513. }
  514. const struct proc_nm_ops nm_thread = {
  515. .match_name = &proc_match_thread,
  516. .list_name = &proc_list_thread,
  517. };
  518. const struct proc_fs_ops fs_thread = {
  519. .mode = &proc_thread_dir_mode,
  520. .stat = &proc_thread_dir_stat,
  521. };
  522. const struct proc_dir dir_thread = { .size = 5, .ent = {
  523. { .name = "cwd", .fs_ops = &fs_thread_link, },
  524. { .name = "exe", .fs_ops = &fs_thread_link, },
  525. { .name = "root", .fs_ops = &fs_thread_link, },
  526. { .name = "fd", .dir = &dir_fd, .fs_ops = &fs_thread_fd, },
  527. { .name = "maps", .fs_ops = &fs_thread_maps, },
  528. }, };