shim_handle.h 11 KB

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