shim_handle.h 12 KB

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