fs.c 9.6 KB

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