fs.c 9.8 KB

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