shim_handle.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. * shim_handle.h
  15. *
  16. * Definitions of types and functions for file/handle bookkeeping.
  17. */
  18. #ifndef _SHIM_HANDLE_H_
  19. #define _SHIM_HANDLE_H_
  20. #include <shim_types.h>
  21. #include <shim_defs.h>
  22. #include <shim_sysv.h>
  23. #include <pal.h>
  24. #include <list.h>
  25. #include <linux/shm.h>
  26. #include <linux/in.h>
  27. #include <linux/in6.h>
  28. #include <linux/un.h>
  29. #include <asm/fcntl.h>
  30. #include <asm/resource.h>
  31. /* start definition of shim handle */
  32. enum shim_handle_type {
  33. TYPE_FILE,
  34. TYPE_DEV,
  35. TYPE_PIPE,
  36. TYPE_SOCK,
  37. TYPE_DIR,
  38. TYPE_SHM,
  39. TYPE_SEM,
  40. TYPE_MSG,
  41. TYPE_FUTEX,
  42. TYPE_STR,
  43. TYPE_EPOLL,
  44. };
  45. struct shim_handle;
  46. struct shim_thread;
  47. struct shim_vma;
  48. enum shim_file_type {
  49. FILE_UNKNOWN,
  50. FILE_REGULAR,
  51. FILE_DIR,
  52. FILE_DEV,
  53. FILE_TTY,
  54. };
  55. struct shim_file_data {
  56. struct shim_lock lock;
  57. struct atomic_int version;
  58. bool queried;
  59. enum shim_file_type type;
  60. mode_t mode;
  61. struct atomic_int size;
  62. struct shim_qstr host_uri;
  63. unsigned long atime;
  64. unsigned long mtime;
  65. unsigned long ctime;
  66. unsigned long nlink;
  67. };
  68. struct shim_file_handle {
  69. unsigned int version;
  70. struct shim_file_data * data;
  71. enum shim_file_type type;
  72. uint64_t size;
  73. uint64_t marker;
  74. enum { FILEBUF_MAP, FILEBUF_NONE } buf_type;
  75. uint64_t mapsize;
  76. uint64_t mapoffset;
  77. void * mapbuf;
  78. };
  79. #define FILE_HANDLE_DATA(hdl) ((hdl)->info.file.data)
  80. #define FILE_DENTRY_DATA(dent) ((struct shim_file_data*)(dent)->data)
  81. struct shim_dev_ops {
  82. /* open: provide a filename relative to the mount point and flags,
  83. modify the shim handle */
  84. int (*open) (struct shim_handle * hdl, const char * name, int flags);
  85. /* close: clean up the file state inside the handle */
  86. int (*close) (struct shim_handle * hdl);
  87. /* read: the content from the file opened as handle */
  88. int (*read) (struct shim_handle * hdl, void * buf, size_t count);
  89. /* write: the content from the file opened as handle */
  90. int (*write) (struct shim_handle * hdl, const void * buf, size_t count);
  91. /* flush: flush out user buffer */
  92. int (*flush) (struct shim_handle * hdl);
  93. /* seek: the content from the file opened as handle */
  94. int (*seek) (struct shim_handle * hdl, off_t offset, int wence);
  95. int (*truncate) (struct shim_handle * hdl, uint64_t len);
  96. int (*mode) (const char * name, mode_t * mode);
  97. /* stat, hstat: get status of the file */
  98. int (*stat) (const char * name, struct stat * buf);
  99. int (*hstat) (struct shim_handle * hdl, struct stat * buf);
  100. };
  101. struct shim_dev_handle {
  102. struct shim_dev_ops dev_ops;
  103. };
  104. struct shim_pipe_handle {
  105. #if USE_SIMPLE_PIPE == 1
  106. struct shim_handle * pair;
  107. #else
  108. IDTYPE pipeid;
  109. #endif
  110. };
  111. #define SOCK_STREAM 1
  112. #define SOCK_DGRAM 2
  113. #define SOCK_NONBLOCK 04000
  114. #define SOCK_CLOEXEC 02000000
  115. #define SOL_TCP 6
  116. #define PF_LOCAL 1
  117. #define PF_UNIX PF_LOCAL
  118. #define PF_FILE PF_LOCAL
  119. #define PF_INET 2
  120. #define PF_INET6 10
  121. #define AF_UNIX PF_UNIX
  122. #define AF_INET PF_INET
  123. #define AF_INET6 PF_INET6
  124. enum shim_sock_state {
  125. SOCK_CREATED,
  126. SOCK_BOUND,
  127. SOCK_CONNECTED,
  128. SOCK_BOUNDCONNECTED,
  129. SOCK_LISTENED,
  130. SOCK_ACCEPTED,
  131. SOCK_SHUTDOWN,
  132. };
  133. struct shim_unix_data {
  134. unsigned int pipeid;
  135. };
  136. struct shim_sock_handle {
  137. int domain;
  138. int sock_type;
  139. int protocol;
  140. int error;
  141. enum shim_sock_state sock_state;
  142. union shim_sock_addr {
  143. // INET addr
  144. struct {
  145. struct addr_inet {
  146. unsigned short port;
  147. unsigned short ext_port;
  148. union {
  149. struct in_addr v4;
  150. struct in6_addr v6;
  151. } addr;
  152. } bind, conn;
  153. } in;
  154. // UNIX addr
  155. struct addr_unix {
  156. struct shim_dentry * dentry;
  157. unsigned int pipeid;
  158. struct shim_unix_data * data;
  159. } un;
  160. } addr;
  161. struct shim_sock_option {
  162. struct shim_sock_option * next;
  163. int level;
  164. int optname;
  165. int optlen;
  166. char optval[];
  167. } * pending_options;
  168. };
  169. struct shim_dirent {
  170. struct shim_dirent * next;
  171. unsigned long ino; /* Inode number */
  172. unsigned char type;
  173. char name[]; /* File name (null-terminated) */
  174. };
  175. struct shim_dir_handle {
  176. int offset;
  177. struct shim_dentry * dotdot;
  178. struct shim_dentry * dot;
  179. struct shim_dentry ** buf;
  180. struct shim_dentry ** ptr;
  181. };
  182. struct shim_shm_handle {
  183. /* XXX: need to implement */
  184. void * __reserved;
  185. };
  186. struct msg_type;
  187. struct msg_item;
  188. struct msg_client;
  189. #define MAX_SYSV_CLIENTS 32
  190. DEFINE_LIST(shim_msg_handle);
  191. struct shim_msg_handle {
  192. unsigned long msqkey; /* msg queue key from user */
  193. IDTYPE msqid; /* msg queue identifier */
  194. bool owned; /* owned by current process */
  195. struct shim_ipc_info * owner;
  196. LEASETYPE lease;
  197. int perm; /* access permissions */
  198. bool deleted; /* marking the queue deleted */
  199. int nmsgs; /* number of msgs */
  200. int currentsize; /* current size in bytes */
  201. struct msg_qobj * queue;
  202. int queuesize;
  203. int queueused;
  204. struct msg_qobj * freed;
  205. PAL_HANDLE event; /* event for waiting */
  206. int ntypes;
  207. int maxtypes;
  208. struct msg_type * types;
  209. struct sysv_score scores[MAX_SYSV_CLIENTS];
  210. LIST_TYPE(shim_msg_handle) list;
  211. LIST_TYPE(shim_msg_handle) key_hlist;
  212. LIST_TYPE(shim_msg_handle) qid_hlist;
  213. };
  214. struct sem_objs;
  215. DEFINE_LIST(shim_sem_handle);
  216. struct shim_sem_handle {
  217. unsigned long semkey;
  218. IDTYPE semid;
  219. bool owned;
  220. struct shim_ipc_info * owner;
  221. LEASETYPE lease;
  222. int perm;
  223. bool deleted;
  224. PAL_HANDLE event;
  225. int nsems;
  226. struct sem_obj * sems;
  227. int nreqs;
  228. struct sysv_score scores[MAX_SYSV_CLIENTS];
  229. LISTP_TYPE(sem_ops) migrated;
  230. LIST_TYPE(shim_sem_handle) list;
  231. LIST_TYPE(shim_sem_handle) key_hlist;
  232. LIST_TYPE(shim_sem_handle) sid_hlist;
  233. };
  234. DEFINE_LIST(futex_waiter);
  235. DEFINE_LISTP(futex_waiter);
  236. DEFINE_LIST(shim_futex_handle);
  237. struct shim_futex_handle {
  238. int * uaddr;
  239. LISTP_TYPE(futex_waiter) waiters;
  240. struct shim_vma * vma;
  241. LIST_TYPE(shim_futex_handle) list;
  242. };
  243. struct shim_str_data {
  244. REFTYPE ref_count;
  245. char * str;
  246. size_t len;
  247. size_t buf_size;
  248. bool dirty;
  249. int (*update) (struct shim_handle * hdl);
  250. int (*modify) (struct shim_handle * hdl);
  251. };
  252. struct shim_str_handle {
  253. struct shim_str_data * data; /* inode is stored in dentry, too.
  254. store pointer here for efficiency */
  255. char * ptr;
  256. };
  257. DEFINE_LIST(shim_epoll_fd);
  258. DEFINE_LISTP(shim_epoll_fd);
  259. struct shim_epoll_handle {
  260. int maxfds;
  261. int nfds;
  262. LISTP_TYPE(shim_epoll_fd) fds; /* this list contains all the
  263. * shim_epoll_fd objects in correspondence
  264. * with the registered handles. */
  265. FDTYPE * pal_fds;
  266. PAL_HANDLE * pal_handles;
  267. int npals;
  268. int nread;
  269. int nwaiters;
  270. AEVENTTYPE event;
  271. };
  272. struct shim_mount;
  273. struct shim_qstr;
  274. struct shim_dentry;
  275. /* The epolls list links to the back field of the shim_epoll_fd structure
  276. */
  277. struct shim_handle {
  278. enum shim_handle_type type;
  279. REFTYPE ref_count;
  280. char fs_type[8];
  281. struct shim_mount * fs;
  282. struct shim_qstr path;
  283. struct shim_dentry * dentry;
  284. /* If this handle is registered for any epoll handle, this list contains
  285. * a shim_epoll_fd object in correspondence with the epoll handle. */
  286. LISTP_TYPE(shim_epoll_fd) epolls;
  287. struct shim_qstr uri; /* URI representing this handle, it is not
  288. * necessary to be set. */
  289. PAL_HANDLE pal_handle;
  290. union {
  291. struct shim_file_handle file;
  292. struct shim_dev_handle dev;
  293. struct shim_pipe_handle pipe;
  294. struct shim_sock_handle sock;
  295. struct shim_dir_handle dir;
  296. struct shim_shm_handle shm;
  297. struct shim_msg_handle msg;
  298. struct shim_sem_handle sem;
  299. struct shim_futex_handle futex;
  300. struct shim_str_handle str;
  301. struct shim_epoll_handle epoll;
  302. } info;
  303. int flags;
  304. int acc_mode;
  305. IDTYPE owner;
  306. REFTYPE opened;
  307. struct shim_lock lock;
  308. };
  309. /* allocating / manage handle */
  310. struct shim_handle * get_new_handle (void);
  311. void flush_handle (struct shim_handle * hdl);
  312. void open_handle (struct shim_handle * hdl);
  313. void close_handle (struct shim_handle * hdl);
  314. void get_handle (struct shim_handle * hdl);
  315. void put_handle (struct shim_handle * hdl);
  316. /* file descriptor table */
  317. struct shim_fd_handle {
  318. FDTYPE vfd; /* virtual file descriptor */
  319. int flags; /* file descriptor flags, only FD_CLOEXEC */
  320. struct shim_handle * handle;
  321. };
  322. struct shim_handle_map {
  323. /* the top of created file descriptors */
  324. FDTYPE fd_size;
  325. FDTYPE fd_top;
  326. /* refrence count and lock */
  327. REFTYPE ref_count;
  328. struct shim_lock lock;
  329. /* An array of file descriptor belong to this mapping */
  330. struct shim_fd_handle ** map;
  331. };
  332. /* allocating file descriptors */
  333. #define FD_NULL ((FDTYPE)-1)
  334. #define HANDLE_ALLOCATED(fd_handle) ((fd_handle) && (fd_handle)->vfd != FD_NULL)
  335. struct shim_handle * __get_fd_handle (FDTYPE fd, int * flags,
  336. struct shim_handle_map * map);
  337. struct shim_handle * get_fd_handle (FDTYPE fd, int * flags,
  338. struct shim_handle_map * map);
  339. int set_new_fd_handle (struct shim_handle * hdl, int flags,
  340. struct shim_handle_map * map);
  341. int set_new_fd_handle_by_fd (FDTYPE fd, struct shim_handle * hdl,
  342. int flags, struct shim_handle_map * map);
  343. struct shim_handle *
  344. __detach_fd_handle (struct shim_fd_handle * fd, int * flags,
  345. struct shim_handle_map * map);
  346. struct shim_handle * detach_fd_handle (FDTYPE fd, int * flags,
  347. struct shim_handle_map * map);
  348. /* manage handle mapping */
  349. int dup_handle_map (struct shim_handle_map ** new_map,
  350. struct shim_handle_map * old_map);
  351. int flush_handle_map (struct shim_handle_map * map);
  352. void get_handle_map (struct shim_handle_map * map);
  353. void put_handle_map (struct shim_handle_map * map);
  354. int walk_handle_map (int (*callback) (struct shim_fd_handle *,
  355. struct shim_handle_map *, void *),
  356. struct shim_handle_map * map, void * arg);
  357. int init_handle (void);
  358. int init_important_handles (void);
  359. ssize_t get_file_size (struct shim_handle * file);
  360. int do_handle_read (struct shim_handle * hdl, void * buf, int count);
  361. int do_handle_write (struct shim_handle * hdl, const void * buf, int count);
  362. extern struct __kernel_rlimit __rlim[RLIM_NLIMITS];
  363. #endif /* _SHIM_HANDLE_H_ */