ipc-thread.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. ret = path_lookupat(NULL, (char *) ipc_data, 0, &dent);
  81. if (ret < 0)
  82. goto out;
  83. get_dentry(dent);
  84. *dentptr = dent;
  85. }
  86. out:
  87. if (dent)
  88. put_dentry(dent);
  89. return ret;
  90. }
  91. static int proc_ipc_thread_link_open (struct shim_handle * hdl,
  92. const char * name, int flags)
  93. {
  94. struct shim_dentry * dent;
  95. int ret = find_ipc_thread_link(name, NULL, &dent);
  96. if (ret < 0)
  97. return ret;
  98. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->open) {
  99. ret = -EACCES;
  100. goto out;
  101. }
  102. ret = dent->fs->d_ops->open(hdl, dent, flags);
  103. out:
  104. put_dentry(dent);
  105. return 0;
  106. }
  107. static int proc_ipc_thread_link_mode (const char * name, mode_t * mode)
  108. {
  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, true);
  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. {
  124. struct shim_dentry * dent;
  125. int ret = find_ipc_thread_link(name, NULL, &dent);
  126. if (ret < 0)
  127. return ret;
  128. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->stat) {
  129. ret = -EACCES;
  130. goto out;
  131. }
  132. ret = dent->fs->d_ops->stat(dent, buf);
  133. out:
  134. put_dentry(dent);
  135. return ret;
  136. }
  137. static int proc_ipc_thread_link_follow_link (const char * name,
  138. struct shim_qstr * link)
  139. {
  140. return find_ipc_thread_link(name, link, NULL);
  141. }
  142. static const struct proc_fs_ops fs_ipc_thread_link = {
  143. .open = &proc_ipc_thread_link_open,
  144. .mode = &proc_ipc_thread_link_mode,
  145. .stat = &proc_ipc_thread_link_stat,
  146. .follow_link = &proc_ipc_thread_link_follow_link,
  147. };
  148. static struct pid_status_cache {
  149. int ref_count;
  150. bool dirty;
  151. int nstatus;
  152. struct pid_status * status;
  153. } * pid_status_cache;
  154. static LOCKTYPE status_lock;
  155. static int proc_match_ipc_thread (const char * name)
  156. {
  157. int pid = parse_ipc_thread_name(name, NULL, NULL, NULL);
  158. if (pid < 0)
  159. return 0;
  160. create_lock_runtime(&status_lock);
  161. lock(status_lock);
  162. if (pid_status_cache)
  163. for (int i = 0 ; i < pid_status_cache->nstatus ; i++)
  164. if (pid_status_cache->status[i].pid == pid) {
  165. unlock(status_lock);
  166. return 1;
  167. }
  168. unlock(status_lock);
  169. return 0;
  170. }
  171. static int proc_ipc_thread_dir_mode (const char * name, mode_t * mode)
  172. {
  173. const char * next;
  174. int next_len;
  175. int pid = parse_ipc_thread_name(name, &next, &next_len, NULL);
  176. if (pid < 0)
  177. return 0;
  178. create_lock_runtime(&status_lock);
  179. lock(status_lock);
  180. if (pid_status_cache)
  181. for (int i = 0 ; i < pid_status_cache->nstatus ; i++)
  182. if (pid_status_cache->status[i].pid == pid) {
  183. unlock(status_lock);
  184. *mode = 0500;
  185. return 0;
  186. }
  187. unlock(status_lock);
  188. return -ENOENT;
  189. }
  190. static int proc_ipc_thread_dir_stat (const char * name, struct stat * buf)
  191. {
  192. const char * next;
  193. int next_len;
  194. int pid = parse_ipc_thread_name(name, &next, &next_len, NULL);
  195. if (pid < 0)
  196. return 0;
  197. create_lock_runtime(&status_lock);
  198. lock(status_lock);
  199. if (pid_status_cache)
  200. for (int i = 0 ; i < pid_status_cache->nstatus ; i++)
  201. if (pid_status_cache->status[i].pid == pid) {
  202. memset(buf, 0, sizeof(struct stat));
  203. buf->st_dev = buf->st_ino = 1;
  204. buf->st_mode = 0500|S_IFDIR;
  205. buf->st_uid = 0; /* XXX */
  206. buf->st_gid = 0; /* XXX */
  207. buf->st_size = 4096;
  208. unlock(status_lock);
  209. return 0;
  210. }
  211. unlock(status_lock);
  212. return -ENOENT;
  213. }
  214. int get_all_pid_status (struct pid_status ** status);
  215. static int proc_list_ipc_thread (const char * name, struct shim_dirent ** buf,
  216. int len)
  217. {
  218. struct pid_status_cache * status = NULL;
  219. int ret = 0;
  220. create_lock_runtime(&status_lock);
  221. lock(status_lock);
  222. if (pid_status_cache && !pid_status_cache->dirty) {
  223. status = pid_status_cache;
  224. status->ref_count++;
  225. }
  226. unlock(status_lock);
  227. if (!status) {
  228. status = malloc(sizeof(struct pid_status_cache));
  229. if (!status)
  230. return -ENOMEM;
  231. ret = get_all_pid_status(&status->status);
  232. if (ret < 0) {
  233. free(status);
  234. return ret;
  235. }
  236. status->nstatus = ret;
  237. status->ref_count = 1;
  238. status->dirty = false;
  239. lock(status_lock);
  240. if (pid_status_cache) {
  241. if (pid_status_cache->dirty) {
  242. if (!pid_status_cache->ref_count)
  243. free(pid_status_cache);
  244. pid_status_cache = status;
  245. } else {
  246. if (status->nstatus)
  247. free(status->status);
  248. free(status);
  249. status = pid_status_cache;
  250. status->ref_count++;
  251. }
  252. } else {
  253. pid_status_cache = status;
  254. }
  255. unlock(status_lock);
  256. }
  257. if (!status->nstatus)
  258. goto success;
  259. struct shim_dirent * ptr = (*buf);
  260. void * buf_end = (void *) ptr + len;
  261. for (int i = 0 ; i < status->nstatus ; i++) {
  262. if (status->status[i].pid != status->status[i].tgid)
  263. continue;
  264. IDTYPE pid = status->status[i].pid;
  265. int p = pid, l = 0;
  266. for ( ; p ; p /= 10, l++);
  267. if ((void *) (ptr + 1) + l + 1 > buf_end) {
  268. ret = -ENOBUFS;
  269. goto err;
  270. }
  271. ptr->next = (void *) (ptr + 1) + l + 1;
  272. ptr->ino = 1;
  273. ptr->type = LINUX_DT_DIR;
  274. ptr->name[l--] = 0;
  275. for (p = pid ; p ; p /= 10)
  276. ptr->name[l--] = p % 10 + '0';
  277. ptr = ptr->next;
  278. }
  279. *buf = ptr;
  280. success:
  281. lock(status_lock);
  282. status->dirty = true;
  283. status->ref_count--;
  284. if (!status->ref_count && status != pid_status_cache)
  285. free(status);
  286. unlock(status_lock);
  287. return 0;
  288. err:
  289. lock(status_lock);
  290. status->ref_count--;
  291. if (!status->ref_count && status != pid_status_cache)
  292. free(status);
  293. unlock(status_lock);
  294. return ret;
  295. }
  296. const struct proc_nm_ops nm_ipc_thread = {
  297. .match_name = &proc_match_ipc_thread,
  298. .list_name = &proc_list_ipc_thread,
  299. };
  300. const struct proc_fs_ops fs_ipc_thread = {
  301. .mode = &proc_ipc_thread_dir_mode,
  302. .stat = &proc_ipc_thread_dir_stat,
  303. };
  304. const struct proc_dir dir_ipc_thread = { .size = 0, .ent = {
  305. { .name = "cwd", .fs_ops = &fs_ipc_thread_link, },
  306. { .name = "exe", .fs_ops = &fs_ipc_thread_link, },
  307. { .name = "root", .fs_ops = &fs_ipc_thread_link, },
  308. }, };