ipc-thread.c 9.3 KB

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