fs.c 10.0 KB

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