fs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 <asm/mman.h>
  28. #include <asm/unistd.h>
  29. #include <asm/prctl.h>
  30. #include <fcntl.h>
  31. #include <errno.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. *mode = 0555;
  54. return 0;
  55. }
  56. static int proc_root_stat (const char * name, struct stat * buf)
  57. {
  58. memset(buf, 0, sizeof(struct stat));
  59. buf->st_dev = buf->st_ino = 1;
  60. buf->st_mode = 0555|S_IFDIR;
  61. buf->st_uid = 0;
  62. buf->st_gid = 0;
  63. buf->st_size = 4096;
  64. return 0;
  65. }
  66. struct proc_fs_ops fs_proc_root = {
  67. .mode = &proc_root_mode,
  68. .stat = &proc_root_stat,
  69. };
  70. const struct proc_ent proc_root_ent =
  71. { .name = "", .fs_ops = &fs_proc_root, .dir = &proc_root, };
  72. static inline int token_len (const char * str, const char ** next_str)
  73. {
  74. const char * t = str;
  75. while (*t && *t != '/')
  76. t++;
  77. if (next_str)
  78. *next_str = *t ? t + 1 : NULL;
  79. return t - str;
  80. }
  81. static int proc_match_name (const char * trim_name,
  82. const struct proc_ent ** ent)
  83. {
  84. if (!trim_name || !trim_name[0]) {
  85. *ent = &proc_root_ent;
  86. return 0;
  87. }
  88. const char * token = trim_name, * next_token;
  89. const struct proc_ent * tmp = proc_root.ent;
  90. const struct proc_ent * last = NULL;
  91. if (*token == '/')
  92. token++;
  93. while (token) {
  94. int tlen = token_len(token, &next_token);
  95. for ( ; tmp->name || tmp->nm_ops ; tmp++) {
  96. if (tmp->name && !memcmp(tmp->name, token, tlen))
  97. goto found;
  98. if (tmp->nm_ops && tmp->nm_ops->match_name &&
  99. tmp->nm_ops->match_name(trim_name))
  100. goto found;
  101. }
  102. return -ENOENT;
  103. found:
  104. if (!tmp->dir && next_token)
  105. return -ENOENT;
  106. last = tmp;
  107. tmp = tmp->dir->ent;
  108. token = next_token;
  109. }
  110. *ent = last;
  111. return 0;
  112. }
  113. static int proc_mode (struct shim_dentry * dent, mode_t * mode, bool force)
  114. {
  115. if (qstrempty(&dent->rel_path)) {
  116. dent->ino = PROC_INO_BASE;
  117. *mode = 0555|S_IFDIR;
  118. return 0;
  119. }
  120. /* don't care about forced or not */
  121. const char * rel_path = qstrgetstr(&dent->rel_path);
  122. const struct proc_ent * ent;
  123. int ret = proc_match_name(rel_path, &ent);
  124. if (ret < 0)
  125. return ret;
  126. if (!ent->fs_ops || !ent->fs_ops->mode)
  127. return -EACCES;
  128. return ent->fs_ops->mode(rel_path, mode);
  129. }
  130. static int proc_lookup (struct shim_dentry * dent, bool force)
  131. {
  132. if (qstrempty(&dent->rel_path)) {
  133. dent->ino = PROC_INO_BASE;
  134. dent->state |= DENTRY_ISDIRECTORY;
  135. return 0;
  136. }
  137. /* don't care about forced or not */
  138. const struct proc_ent * ent;
  139. int ret = proc_match_name(qstrgetstr(&dent->rel_path), &ent);
  140. if (!ret && ent->dir)
  141. dent->state |= DENTRY_ISDIRECTORY;
  142. return ret;
  143. }
  144. static int proc_mount (const char * uri, const char * root, void ** mount_data)
  145. {
  146. /* do nothing */
  147. return 0;
  148. }
  149. static int proc_unmount (void * mount_data)
  150. {
  151. /* do nothing */
  152. return 0;
  153. }
  154. static int proc_open (struct shim_handle * hdl, struct shim_dentry * dent,
  155. int flags)
  156. {
  157. const char * rel_path = qstrgetstr(&dent->rel_path);
  158. if (flags & (O_CREAT|O_EXCL))
  159. return -EACCES;
  160. const struct proc_ent * ent;
  161. int ret;
  162. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  163. return ret;
  164. if (ent->dir)
  165. return -EISDIR;
  166. if (!ent->fs_ops || !ent->fs_ops->open)
  167. return -EACCES;
  168. hdl->flags = flags;
  169. return ent->fs_ops->open(hdl, rel_path, flags);
  170. }
  171. static int proc_readdir (struct shim_dentry * dent,
  172. struct shim_dirent ** dirent)
  173. {
  174. const char * rel_path = qstrgetstr(&dent->rel_path);
  175. const struct proc_ent * ent;
  176. int ret;
  177. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  178. return ret;
  179. if (!ent->dir)
  180. return -ENOTDIR;
  181. const struct proc_ent * tmp = ent->dir->ent;
  182. const struct proc_ent * end = tmp + ent->dir->size;
  183. HASHTYPE self_hash = hash_path(rel_path,
  184. dent->rel_path.len, NULL);
  185. HASHTYPE new_hash;
  186. struct shim_dirent * buf, * ptr;
  187. int buf_size = MAX_PATH;
  188. retry:
  189. buf = malloc(buf_size);
  190. *dirent = ptr = buf;
  191. struct shim_dirent ** last = dirent;
  192. for ( ; tmp < end ; tmp++) {
  193. if (tmp->name) {
  194. int name_len = strlen(tmp->name);
  195. if ((void *) (ptr + 1) + name_len + 1 > (void *) buf + buf_size)
  196. goto enlarge;
  197. new_hash = rehash_name(self_hash,
  198. tmp->name, name_len);
  199. ptr->next = (void *) (ptr + 1) + name_len + 1;
  200. ptr->ino = new_hash;
  201. ptr->type = tmp->dir ? LINUX_DT_DIR : (
  202. tmp->fs_ops && tmp->fs_ops->follow_link ?
  203. LINUX_DT_LNK : LINUX_DT_REG);
  204. memcpy(ptr->name, tmp->name, name_len + 1);
  205. last = &ptr->next;
  206. ptr = *last;
  207. continue;
  208. }
  209. if (tmp->nm_ops && tmp->nm_ops->list_name) {
  210. struct shim_dirent * d = ptr;
  211. int ret = tmp->nm_ops->list_name(rel_path,
  212. &ptr,
  213. (void *) buf + buf_size -
  214. (void *) ptr);
  215. if (ret == -ENOBUFS)
  216. goto enlarge;
  217. if (ret < 0)
  218. ptr = d;
  219. else
  220. for ( ; d != ptr ; d = d->next)
  221. last = &d->next;
  222. continue;
  223. }
  224. }
  225. *last = NULL;
  226. if (!(*dirent))
  227. free(buf);
  228. return 0;
  229. enlarge:
  230. buf_size *= 2;
  231. free(buf);
  232. goto retry;
  233. }
  234. static int proc_stat (struct shim_dentry * dent, struct stat * buf)
  235. {
  236. const char * rel_path = qstrgetstr(&dent->rel_path);
  237. const struct proc_ent * ent;
  238. int ret;
  239. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  240. return ret;
  241. if (!ent->fs_ops || !ent->fs_ops->stat)
  242. return -EACCES;
  243. return ent->fs_ops->stat(rel_path, buf);
  244. }
  245. static int proc_follow_link (struct shim_dentry * dent,
  246. struct shim_qstr * link)
  247. {
  248. const char * rel_path = qstrgetstr(&dent->rel_path);
  249. const struct proc_ent * ent;
  250. int ret;
  251. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  252. return ret;
  253. if (!ent->fs_ops || !ent->fs_ops->follow_link)
  254. return -EINVAL;
  255. return ent->fs_ops->follow_link(rel_path, link);
  256. }
  257. static int proc_hstat (struct shim_handle * hdl, struct stat * buf)
  258. {
  259. struct shim_dentry * dent = hdl->dentry;
  260. assert(dent);
  261. const char * rel_path = qstrgetstr(&dent->rel_path);
  262. const struct proc_ent * ent;
  263. int ret;
  264. if ((ret = proc_match_name(rel_path, &ent)) < 0)
  265. return ret;
  266. if (!ent->fs_ops || !ent->fs_ops->stat)
  267. return -EACCES;
  268. return ent->fs_ops->stat(rel_path, buf);
  269. }
  270. struct shim_fs_ops proc_fs_ops = {
  271. .mount = &proc_mount,
  272. .unmount = &proc_unmount,
  273. .close = &str_close,
  274. .read = &str_read,
  275. .write = &str_write,
  276. .seek = &str_seek,
  277. .flush = &str_flush,
  278. .hstat = &proc_hstat,
  279. };
  280. struct shim_d_ops proc_d_ops = {
  281. .open = &proc_open,
  282. .stat = &proc_stat,
  283. .mode = &proc_mode,
  284. .lookup = &proc_lookup,
  285. .follow_link = &proc_follow_link,
  286. .readdir = &proc_readdir,
  287. };