fs.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * fs.c
  17. *
  18. * This file contains codes for implementation of 'proc' filesystem.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_thread.h>
  22. #include <shim_handle.h>
  23. #include <shim_fs.h>
  24. #include <shim_utils.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <errno.h>
  28. #include <linux/stat.h>
  29. #include <linux/fcntl.h>
  30. #include <asm/fcntl.h>
  31. #include <asm/mman.h>
  32. #include <asm/unistd.h>
  33. #include <asm/prctl.h>
  34. extern const struct proc_nm_ops nm_thread;
  35. extern const struct proc_fs_ops fs_thread;
  36. extern const struct proc_dir dir_thread;
  37. extern const struct proc_nm_ops nm_ipc_thread;
  38. extern const struct proc_fs_ops fs_ipc_thread;
  39. extern const struct proc_dir dir_ipc_thread;
  40. extern const struct proc_fs_ops fs_meminfo;
  41. extern const struct proc_fs_ops fs_cpuinfo;
  42. const struct proc_dir proc_root = {
  43. .size = 5,
  44. .ent = {
  45. { .name = "self", .fs_ops = &fs_thread, .dir = &dir_thread, },
  46. { .nm_ops = &nm_thread, .fs_ops = &fs_thread, .dir = &dir_thread, },
  47. { .nm_ops = &nm_ipc_thread, .fs_ops = &fs_ipc_thread,
  48. .dir = &dir_ipc_thread, },
  49. { .name = "meminfo", .fs_ops = &fs_meminfo, },
  50. { .name = "cpuinfo", .fs_ops = &fs_cpuinfo, },
  51. }, };
  52. #define PROC_INO_BASE 1
  53. static int proc_root_mode (const char * name, mode_t * mode)
  54. {
  55. *mode = 0555;
  56. return 0;
  57. }
  58. static int proc_root_stat (const char * name, struct stat * buf)
  59. {
  60. memset(buf, 0, sizeof(struct stat));
  61. buf->st_dev = buf->st_ino = 1;
  62. buf->st_mode = 0555|S_IFDIR;
  63. buf->st_uid = 0;
  64. buf->st_gid = 0;
  65. buf->st_size = 4096;
  66. return 0;
  67. }
  68. struct proc_fs_ops fs_proc_root = {
  69. .mode = &proc_root_mode,
  70. .stat = &proc_root_stat,
  71. };
  72. const struct proc_ent proc_root_ent =
  73. { .name = "", .fs_ops = &fs_proc_root, .dir = &proc_root, };
  74. static inline int token_len (const char * str, const char ** next_str)
  75. {
  76. const char * t = str;
  77. while (*t && *t != '/')
  78. t++;
  79. if (next_str)
  80. *next_str = *t ? t + 1 : NULL;
  81. return t - str;
  82. }
  83. static int proc_match_name (const char * trim_name,
  84. const struct proc_ent ** ent)
  85. {
  86. if (!trim_name || !trim_name[0]) {
  87. *ent = &proc_root_ent;
  88. return 0;
  89. }
  90. const char * token = trim_name, * next_token;
  91. const struct proc_ent * tmp = proc_root.ent;
  92. const struct proc_ent * last = NULL;
  93. if (*token == '/')
  94. token++;
  95. while (token) {
  96. int tlen = token_len(token, &next_token);
  97. for ( ; tmp->name || tmp->nm_ops ; tmp++) {
  98. if (tmp->name && !memcmp(tmp->name, token, tlen))
  99. goto found;
  100. if (tmp->nm_ops && tmp->nm_ops->match_name &&
  101. tmp->nm_ops->match_name(trim_name))
  102. goto found;
  103. }
  104. return -ENOENT;
  105. found:
  106. if (!tmp->dir && next_token)
  107. return -ENOENT;
  108. last = tmp;
  109. tmp = tmp->dir->ent;
  110. token = next_token;
  111. }
  112. *ent = last;
  113. return 0;
  114. }
  115. static int proc_mode (struct shim_dentry * dent, mode_t * mode, bool force)
  116. {
  117. if (qstrempty(&dent->rel_path)) {
  118. dent->ino = PROC_INO_BASE;
  119. *mode = 0555|S_IFDIR;
  120. return 0;
  121. }
  122. /* don't care about forced or not */
  123. const char * rel_path = qstrgetstr(&dent->rel_path);
  124. const struct proc_ent * ent;
  125. int ret = proc_match_name(rel_path, &ent);
  126. if (ret < 0)
  127. return ret;
  128. if (!ent->fs_ops || !ent->fs_ops->mode)
  129. return -EACCES;
  130. return ent->fs_ops->mode(rel_path, mode);
  131. }
  132. static int proc_lookup (struct shim_dentry * dent, bool force)
  133. {
  134. if (qstrempty(&dent->rel_path)) {
  135. dent->ino = PROC_INO_BASE;
  136. dent->state |= DENTRY_ISDIRECTORY;
  137. return 0;
  138. }
  139. /* don't care about forced or not */
  140. const struct proc_ent * ent = NULL;
  141. int ret = proc_match_name(qstrgetstr(&dent->rel_path), &ent);
  142. if (!ret && ent->dir)
  143. dent->state |= DENTRY_ISDIRECTORY;
  144. if (ent && ent->fs_ops && ent->fs_ops->follow_link)
  145. dent->state |= DENTRY_ISLINK;
  146. return ret;
  147. }
  148. static int proc_mount (const char * uri, const char * root, void ** mount_data)
  149. {
  150. /* do nothing */
  151. return 0;
  152. }
  153. static int proc_unmount (void * mount_data)
  154. {
  155. /* do nothing */
  156. return 0;
  157. }
  158. static int proc_open (struct shim_handle * hdl, struct shim_dentry * dent,
  159. int flags)
  160. {
  161. const char * rel_path = qstrgetstr(&dent->rel_path);
  162. if (flags & (O_CREAT|O_EXCL))
  163. return -EACCES;
  164. const struct proc_ent * ent;
  165. int ret;
  166. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  167. return ret;
  168. if (ent->dir)
  169. return -EISDIR;
  170. if (!ent->fs_ops || !ent->fs_ops->open)
  171. return -EACCES;
  172. hdl->flags = flags;
  173. return ent->fs_ops->open(hdl, rel_path, flags);
  174. }
  175. static int proc_readdir (struct shim_dentry * dent,
  176. struct shim_dirent ** dirent)
  177. {
  178. const char * rel_path = qstrgetstr(&dent->rel_path);
  179. const struct proc_ent * ent;
  180. int ret;
  181. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  182. return ret;
  183. if (!ent->dir)
  184. return -ENOTDIR;
  185. const struct proc_ent * tmp = ent->dir->ent;
  186. const struct proc_ent * end = tmp + ent->dir->size;
  187. HASHTYPE self_hash = hash_path(rel_path,
  188. dent->rel_path.len, NULL);
  189. HASHTYPE new_hash;
  190. struct shim_dirent * buf, * ptr;
  191. int buf_size = MAX_PATH;
  192. retry:
  193. buf = malloc(buf_size);
  194. *dirent = ptr = buf;
  195. struct shim_dirent ** last = dirent;
  196. for ( ; tmp < end ; tmp++) {
  197. if (tmp->name) {
  198. int name_len = strlen(tmp->name);
  199. if ((void *) (ptr + 1) + name_len + 1 > (void *) buf + buf_size)
  200. goto enlarge;
  201. new_hash = rehash_name(self_hash,
  202. tmp->name, name_len);
  203. ptr->next = (void *) (ptr + 1) + name_len + 1;
  204. ptr->ino = new_hash;
  205. ptr->type = tmp->dir ? LINUX_DT_DIR : (
  206. tmp->fs_ops && tmp->fs_ops->follow_link ?
  207. LINUX_DT_LNK : LINUX_DT_REG);
  208. memcpy(ptr->name, tmp->name, name_len + 1);
  209. last = &ptr->next;
  210. ptr = *last;
  211. continue;
  212. }
  213. if (tmp->nm_ops && tmp->nm_ops->list_name) {
  214. struct shim_dirent * d = ptr;
  215. int ret = tmp->nm_ops->list_name(rel_path,
  216. &ptr,
  217. (void *) buf + buf_size -
  218. (void *) ptr);
  219. if (ret == -ENOBUFS)
  220. goto enlarge;
  221. if (ret < 0)
  222. ptr = d;
  223. else
  224. for ( ; d && d != ptr ; d = d->next)
  225. last = &d->next;
  226. continue;
  227. }
  228. }
  229. *last = NULL;
  230. if (!(*dirent))
  231. free(buf);
  232. return 0;
  233. enlarge:
  234. buf_size *= 2;
  235. free(buf);
  236. goto retry;
  237. }
  238. static int proc_stat (struct shim_dentry * dent, struct stat * buf)
  239. {
  240. const char * rel_path = qstrgetstr(&dent->rel_path);
  241. const struct proc_ent * ent;
  242. int ret;
  243. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  244. return ret;
  245. if (!ent->fs_ops || !ent->fs_ops->stat)
  246. return -EACCES;
  247. return ent->fs_ops->stat(rel_path, buf);
  248. }
  249. static int proc_follow_link (struct shim_dentry * dent,
  250. struct shim_qstr * link)
  251. {
  252. const char * rel_path = qstrgetstr(&dent->rel_path);
  253. const struct proc_ent * ent;
  254. int ret;
  255. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  256. return ret;
  257. if (!ent->fs_ops || !ent->fs_ops->follow_link)
  258. return -EINVAL;
  259. return ent->fs_ops->follow_link(rel_path, link);
  260. }
  261. static int proc_hstat (struct shim_handle * hdl, struct stat * buf)
  262. {
  263. struct shim_dentry * dent = hdl->dentry;
  264. assert(dent);
  265. const char * rel_path = qstrgetstr(&dent->rel_path);
  266. const struct proc_ent * ent;
  267. int ret;
  268. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  269. return ret;
  270. if (!ent->fs_ops || !ent->fs_ops->stat)
  271. return -EACCES;
  272. return ent->fs_ops->stat(rel_path, buf);
  273. }
  274. struct shim_fs_ops proc_fs_ops = {
  275. .mount = &proc_mount,
  276. .unmount = &proc_unmount,
  277. .close = &str_close,
  278. .read = &str_read,
  279. .write = &str_write,
  280. .seek = &str_seek,
  281. .flush = &str_flush,
  282. .hstat = &proc_hstat,
  283. };
  284. struct shim_d_ops proc_d_ops = {
  285. .open = &proc_open,
  286. .stat = &proc_stat,
  287. .mode = &proc_mode,
  288. .lookup = &proc_lookup,
  289. .follow_link = &proc_follow_link,
  290. .readdir = &proc_readdir,
  291. };