ipc-thread.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 <shim_ipc.h>
  10. #include <pal.h>
  11. #include <pal_error.h>
  12. #include <errno.h>
  13. #include <linux/stat.h>
  14. #include <linux/fcntl.h>
  15. #include <asm/fcntl.h>
  16. #include <asm/mman.h>
  17. #include <asm/unistd.h>
  18. #include <asm/prctl.h>
  19. static int parse_ipc_thread_name (const char * name,
  20. const char ** next, int * next_len,
  21. const char ** nextnext)
  22. {
  23. const char * p = name;
  24. int pid = 0;
  25. if (*p == '/')
  26. p++;
  27. for ( ; *p && *p != '/' ; p++) {
  28. if (*p < '0' || *p > '9')
  29. return -ENOENT;
  30. pid = pid * 10 + *p - '0';
  31. }
  32. if (next) {
  33. if (*(p++) == '/' && *p) {
  34. *next = p;
  35. if (next_len || nextnext)
  36. for ( ; *p && *p != '/' ; p++);
  37. if (next_len)
  38. *next_len = p - *next;
  39. if (nextnext)
  40. *nextnext = (*(p++) == '/' && *p) ? p : NULL;
  41. } else {
  42. *next = NULL;
  43. }
  44. }
  45. return pid;
  46. }
  47. static int find_ipc_thread_link (const char * name, struct shim_qstr * link,
  48. struct shim_dentry ** dentptr)
  49. {
  50. const char * next, * nextnext;
  51. int next_len;
  52. int pid = parse_ipc_thread_name(name, &next, &next_len, &nextnext);
  53. if (pid < 0)
  54. return pid;
  55. struct shim_dentry * dent = NULL;
  56. enum pid_meta_code ipc_code;
  57. void * ipc_data = NULL;
  58. int ret = 0;
  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. assert (0);
  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,
  94. const char * name, int flags)
  95. {
  96. struct shim_dentry * dent;
  97. int ret = find_ipc_thread_link(name, NULL, &dent);
  98. if (ret < 0)
  99. return ret;
  100. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
  101. ret = -EACCES;
  102. goto out;
  103. }
  104. ret = dent->fs->d_ops->open(hdl, dent, flags);
  105. out:
  106. put_dentry(dent);
  107. return 0;
  108. }
  109. static int proc_ipc_thread_link_mode (const char * name, mode_t * mode)
  110. {
  111. struct shim_dentry * dent;
  112. int ret = find_ipc_thread_link(name, NULL, &dent);
  113. if (ret < 0)
  114. return ret;
  115. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode) {
  116. ret = -EACCES;
  117. goto out;
  118. }
  119. ret = dent->fs->d_ops->mode(dent, mode, true);
  120. out:
  121. put_dentry(dent);
  122. return ret;
  123. }
  124. static int proc_ipc_thread_link_stat (const char * name, struct stat * buf)
  125. {
  126. struct shim_dentry * dent;
  127. int ret = find_ipc_thread_link(name, NULL, &dent);
  128. if (ret < 0)
  129. return ret;
  130. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
  131. ret = -EACCES;
  132. goto out;
  133. }
  134. ret = dent->fs->d_ops->stat(dent, buf);
  135. out:
  136. put_dentry(dent);
  137. return ret;
  138. }
  139. static int proc_ipc_thread_link_follow_link (const char * name,
  140. struct shim_qstr * link)
  141. {
  142. return find_ipc_thread_link(name, link, NULL);
  143. }
  144. static const struct proc_fs_ops fs_ipc_thread_link = {
  145. .open = &proc_ipc_thread_link_open,
  146. .mode = &proc_ipc_thread_link_mode,
  147. .stat = &proc_ipc_thread_link_stat,
  148. .follow_link = &proc_ipc_thread_link_follow_link,
  149. };
  150. static struct pid_status_cache {
  151. int ref_count;
  152. bool dirty;
  153. int nstatus;
  154. struct pid_status * status;
  155. } * pid_status_cache;
  156. static LOCKTYPE status_lock;
  157. static int proc_match_ipc_thread (const char * name)
  158. {
  159. int pid = parse_ipc_thread_name(name, NULL, NULL, NULL);
  160. if (pid < 0)
  161. return 0;
  162. create_lock_runtime(&status_lock);
  163. lock(status_lock);
  164. if (pid_status_cache)
  165. for (int i = 0 ; i < pid_status_cache->nstatus ; i++)
  166. if (pid_status_cache->status[i].pid == pid) {
  167. unlock(status_lock);
  168. return 1;
  169. }
  170. unlock(status_lock);
  171. return 0;
  172. }
  173. static int proc_ipc_thread_dir_mode (const char * name, mode_t * mode)
  174. {
  175. const char * next;
  176. int next_len;
  177. int pid = parse_ipc_thread_name(name, &next, &next_len, NULL);
  178. if (pid < 0)
  179. return 0;
  180. create_lock_runtime(&status_lock);
  181. lock(status_lock);
  182. if (pid_status_cache)
  183. for (int 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. int next_len;
  196. int pid = parse_ipc_thread_name(name, &next, &next_len, NULL);
  197. if (pid < 0)
  198. return 0;
  199. create_lock_runtime(&status_lock);
  200. lock(status_lock);
  201. if (pid_status_cache)
  202. for (int i = 0 ; i < pid_status_cache->nstatus ; i++)
  203. if (pid_status_cache->status[i].pid == pid) {
  204. memset(buf, 0, sizeof(struct stat));
  205. buf->st_dev = buf->st_ino = 1;
  206. buf->st_mode = 0500|S_IFDIR;
  207. buf->st_uid = 0; /* XXX */
  208. buf->st_gid = 0; /* XXX */
  209. buf->st_size = 4096;
  210. unlock(status_lock);
  211. return 0;
  212. }
  213. unlock(status_lock);
  214. return -ENOENT;
  215. }
  216. int get_all_pid_status (struct pid_status ** status);
  217. static int proc_list_ipc_thread (const char * name, struct shim_dirent ** buf,
  218. int len)
  219. {
  220. struct pid_status_cache * status = NULL;
  221. int ret = 0;
  222. create_lock_runtime(&status_lock);
  223. lock(status_lock);
  224. if (pid_status_cache && !pid_status_cache->dirty) {
  225. status = pid_status_cache;
  226. status->ref_count++;
  227. }
  228. unlock(status_lock);
  229. if (!status) {
  230. status = malloc(sizeof(struct pid_status_cache));
  231. if (!status)
  232. return -ENOMEM;
  233. ret = get_all_pid_status(&status->status);
  234. if (ret < 0) {
  235. free(status);
  236. return ret;
  237. }
  238. status->nstatus = ret;
  239. status->ref_count = 1;
  240. status->dirty = false;
  241. lock(status_lock);
  242. if (pid_status_cache) {
  243. if (pid_status_cache->dirty) {
  244. if (!pid_status_cache->ref_count)
  245. free(pid_status_cache);
  246. pid_status_cache = status;
  247. } else {
  248. if (status->nstatus)
  249. free(status->status);
  250. free(status);
  251. status = pid_status_cache;
  252. status->ref_count++;
  253. }
  254. } else {
  255. pid_status_cache = status;
  256. }
  257. unlock(status_lock);
  258. }
  259. if (!status->nstatus)
  260. goto success;
  261. struct shim_dirent * ptr = (*buf);
  262. void * buf_end = (void *) ptr + len;
  263. for (int i = 0 ; i < status->nstatus ; i++) {
  264. if (status->status[i].pid != status->status[i].tgid)
  265. continue;
  266. IDTYPE pid = status->status[i].pid;
  267. int p = pid, l = 0;
  268. for ( ; p ; p /= 10, l++);
  269. if ((void *) (ptr + 1) + l + 1 > buf_end) {
  270. ret = -ENOBUFS;
  271. goto err;
  272. }
  273. ptr->next = (void *) (ptr + 1) + l + 1;
  274. ptr->ino = 1;
  275. ptr->type = LINUX_DT_DIR;
  276. ptr->name[l--] = 0;
  277. for (p = pid ; p ; p /= 10)
  278. ptr->name[l--] = p % 10 + '0';
  279. ptr = ptr->next;
  280. }
  281. *buf = ptr;
  282. success:
  283. lock(status_lock);
  284. status->dirty = true;
  285. status->ref_count--;
  286. if (!status->ref_count && status != pid_status_cache)
  287. free(status);
  288. unlock(status_lock);
  289. return 0;
  290. err:
  291. lock(status_lock);
  292. status->ref_count--;
  293. if (!status->ref_count && status != pid_status_cache)
  294. free(status);
  295. unlock(status_lock);
  296. return ret;
  297. }
  298. const struct proc_nm_ops nm_ipc_thread = {
  299. .match_name = &proc_match_ipc_thread,
  300. .list_name = &proc_list_ipc_thread,
  301. };
  302. const struct proc_fs_ops fs_ipc_thread = {
  303. .mode = &proc_ipc_thread_dir_mode,
  304. .stat = &proc_ipc_thread_dir_stat,
  305. };
  306. const struct proc_dir dir_ipc_thread = { .size = 0, .ent = {
  307. { .name = "cwd", .fs_ops = &fs_ipc_thread_link, },
  308. { .name = "exe", .fs_ops = &fs_ipc_thread_link, },
  309. { .name = "root", .fs_ops = &fs_ipc_thread_link, },
  310. }, };