ipc-thread.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <asm/mman.h>
  13. #include <asm/unistd.h>
  14. #include <asm/prctl.h>
  15. #include <errno.h>
  16. static int parse_ipc_thread_name (const char * name,
  17. const char ** next, int * next_len,
  18. const char ** nextnext)
  19. {
  20. const char * p = name;
  21. int 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. if (next_len)
  35. *next_len = p - *next;
  36. if (nextnext)
  37. *nextnext = (*(p++) == '/' && *p) ? p : NULL;
  38. } else {
  39. *next = NULL;
  40. }
  41. }
  42. return pid;
  43. }
  44. static int find_ipc_thread_link (const char * name, struct shim_qstr * link,
  45. struct shim_dentry ** dentptr)
  46. {
  47. const char * next, * nextnext;
  48. int next_len;
  49. int pid = parse_ipc_thread_name(name, &next, &next_len, &nextnext);
  50. if (pid < 0)
  51. return pid;
  52. struct shim_dentry * dent = NULL;
  53. enum pid_meta_code ipc_code;
  54. void * ipc_data = NULL;
  55. int ret = 0;
  56. if (!memcmp(next, "root", next_len)) {
  57. ipc_code = PID_META_ROOT;
  58. goto do_ipc;
  59. }
  60. if (!memcmp(next, "cwd", next_len)) {
  61. ipc_code = PID_META_CWD;
  62. goto do_ipc;
  63. }
  64. if (!memcmp(next, "exe", next_len)) {
  65. ipc_code = PID_META_EXEC;
  66. goto do_ipc;
  67. }
  68. ret = -ENOENT;
  69. goto out;
  70. do_ipc:
  71. ret = ipc_pid_getmeta_send(pid, ipc_code, &ipc_data);
  72. if (ret < 0)
  73. goto out;
  74. if (link)
  75. qstrsetstr(link, (char *) ipc_data, strlen((char *) ipc_data));
  76. if (dentptr) {
  77. ret = path_lookupat(NULL, (char *) ipc_data, 0, &dent);
  78. if (ret < 0)
  79. goto out;
  80. get_dentry(dent);
  81. *dentptr = dent;
  82. }
  83. out:
  84. if (dent)
  85. put_dentry(dent);
  86. return ret;
  87. }
  88. static int proc_ipc_thread_link_open (struct shim_handle * hdl,
  89. const char * name, int flags)
  90. {
  91. struct shim_dentry * dent;
  92. int ret = find_ipc_thread_link(name, NULL, &dent);
  93. if (ret < 0)
  94. return ret;
  95. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
  96. ret = -EACCES;
  97. goto out;
  98. }
  99. ret = dent->fs->d_ops->open(hdl, dent, flags);
  100. out:
  101. put_dentry(dent);
  102. return 0;
  103. }
  104. static int proc_ipc_thread_link_mode (const char * name, mode_t * mode)
  105. {
  106. struct shim_dentry * dent;
  107. int ret = find_ipc_thread_link(name, NULL, &dent);
  108. if (ret < 0)
  109. return ret;
  110. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode) {
  111. ret = -EACCES;
  112. goto out;
  113. }
  114. ret = dent->fs->d_ops->mode(dent, mode, true);
  115. out:
  116. put_dentry(dent);
  117. return ret;
  118. }
  119. static int proc_ipc_thread_link_stat (const char * name, struct stat * buf)
  120. {
  121. struct shim_dentry * dent;
  122. int ret = find_ipc_thread_link(name, NULL, &dent);
  123. if (ret < 0)
  124. return ret;
  125. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
  126. ret = -EACCES;
  127. goto out;
  128. }
  129. ret = dent->fs->d_ops->stat(dent, buf);
  130. out:
  131. put_dentry(dent);
  132. return ret;
  133. }
  134. static int proc_ipc_thread_link_follow_link (const char * name,
  135. struct shim_qstr * link)
  136. {
  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. int ref_count;
  147. bool dirty;
  148. int nstatus;
  149. struct pid_status * status;
  150. } * pid_status_cache;
  151. static LOCKTYPE status_lock;
  152. static int proc_match_ipc_thread (const char * name)
  153. {
  154. int pid = parse_ipc_thread_name(name, NULL, NULL, NULL);
  155. if (pid < 0)
  156. return 0;
  157. create_lock_runtime(&status_lock);
  158. lock(status_lock);
  159. if (pid_status_cache)
  160. for (int 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. {
  170. const char * next;
  171. int next_len;
  172. int pid = parse_ipc_thread_name(name, &next, &next_len, NULL);
  173. if (pid < 0)
  174. return 0;
  175. create_lock_runtime(&status_lock);
  176. lock(status_lock);
  177. if (pid_status_cache)
  178. for (int i = 0 ; i < pid_status_cache->nstatus ; i++)
  179. if (pid_status_cache->status[i].pid == pid) {
  180. unlock(status_lock);
  181. *mode = 0500;
  182. return 0;
  183. }
  184. unlock(status_lock);
  185. return -ENOENT;
  186. }
  187. static int proc_ipc_thread_dir_stat (const char * name, struct stat * buf)
  188. {
  189. const char * next;
  190. int next_len;
  191. int pid = parse_ipc_thread_name(name, &next, &next_len, NULL);
  192. if (pid < 0)
  193. return 0;
  194. create_lock_runtime(&status_lock);
  195. lock(status_lock);
  196. if (pid_status_cache)
  197. for (int i = 0 ; i < pid_status_cache->nstatus ; i++)
  198. if (pid_status_cache->status[i].pid == pid) {
  199. memset(buf, 0, sizeof(struct stat));
  200. buf->st_dev = buf->st_ino = 1;
  201. buf->st_mode = 0500|S_IFDIR;
  202. buf->st_uid = 0; /* XXX */
  203. buf->st_gid = 0; /* XXX */
  204. buf->st_size = 4096;
  205. unlock(status_lock);
  206. return 0;
  207. }
  208. unlock(status_lock);
  209. return -ENOENT;
  210. }
  211. int get_all_pid_status (struct pid_status ** status);
  212. static int proc_list_ipc_thread (const char * name, struct shim_dirent ** buf,
  213. int len)
  214. {
  215. struct pid_status_cache * status = NULL;
  216. int ret = 0;
  217. create_lock_runtime(&status_lock);
  218. lock(status_lock);
  219. if (pid_status_cache && !pid_status_cache->dirty) {
  220. status = pid_status_cache;
  221. status->ref_count++;
  222. }
  223. unlock(status_lock);
  224. if (!status) {
  225. status = malloc(sizeof(struct pid_status_cache));
  226. if (!status)
  227. return -ENOMEM;
  228. ret = get_all_pid_status(&status->status);
  229. if (ret < 0) {
  230. free(status);
  231. return ret;
  232. }
  233. status->nstatus = ret;
  234. status->ref_count = 1;
  235. status->dirty = false;
  236. lock(status_lock);
  237. if (pid_status_cache) {
  238. if (pid_status_cache->dirty) {
  239. if (!pid_status_cache->ref_count)
  240. free(pid_status_cache);
  241. pid_status_cache = status;
  242. } else {
  243. if (status->nstatus)
  244. free(status->status);
  245. free(status);
  246. status = pid_status_cache;
  247. status->ref_count++;
  248. }
  249. } else {
  250. pid_status_cache = status;
  251. }
  252. unlock(status_lock);
  253. }
  254. if (!status->nstatus)
  255. goto success;
  256. struct shim_dirent * ptr = (*buf);
  257. void * buf_end = (void *) ptr + len;
  258. for (int i = 0 ; i < status->nstatus ; i++) {
  259. if (status->status[i].pid != status->status[i].tgid)
  260. continue;
  261. IDTYPE pid = status->status[i].pid;
  262. int p = pid, l = 0;
  263. for ( ; p ; p /= 10, l++);
  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. ptr = ptr->next;
  275. }
  276. *buf = ptr;
  277. success:
  278. lock(status_lock);
  279. status->dirty = true;
  280. status->ref_count--;
  281. if (!status->ref_count && status != pid_status_cache)
  282. free(status);
  283. unlock(status_lock);
  284. return 0;
  285. err:
  286. lock(status_lock);
  287. status->ref_count--;
  288. if (!status->ref_count && status != pid_status_cache)
  289. free(status);
  290. unlock(status_lock);
  291. return ret;
  292. }
  293. const struct proc_nm_ops nm_ipc_thread = {
  294. .match_name = &proc_match_ipc_thread,
  295. .list_name = &proc_list_ipc_thread,
  296. };
  297. const struct proc_fs_ops fs_ipc_thread = {
  298. .mode = &proc_ipc_thread_dir_mode,
  299. .stat = &proc_ipc_thread_dir_stat,
  300. };
  301. const struct proc_dir dir_ipc_thread = { .size = 0, .ent = {
  302. { .name = "cwd", .fs_ops = &fs_ipc_thread_link, },
  303. { .name = "exe", .fs_ops = &fs_ipc_thread_link, },
  304. { .name = "root", .fs_ops = &fs_ipc_thread_link, },
  305. }, };