db_files.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. * db_files.c
  17. *
  18. * This file contains operands to handle streams with URIs that start with
  19. * "file:" or "dir:".
  20. */
  21. #include "pal_defs.h"
  22. #include "pal_linux_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_linux.h"
  26. #include "pal_debug.h"
  27. #include "pal_error.h"
  28. #include "api.h"
  29. #include <linux/types.h>
  30. typedef __kernel_pid_t pid_t;
  31. #undef __GLIBC__
  32. #include <linux/stat.h>
  33. #include <asm/stat.h>
  34. #include <asm/fcntl.h>
  35. #include <asm-errno.h>
  36. /* 'open' operation for file streams */
  37. static int file_open (PAL_HANDLE * handle, const char * type, const char * uri,
  38. int access, int share, int create, int options)
  39. {
  40. /* try to do the real open */
  41. int ret = INLINE_SYSCALL(open, 3, uri,
  42. access|create|options|O_CLOEXEC,
  43. share);
  44. if (IS_ERR(ret))
  45. return unix_to_pal_error(ERRNO(ret));
  46. /* if try_create_path succeeded, prepare for the file handle */
  47. int len = strlen(uri);
  48. PAL_HANDLE hdl = malloc(HANDLE_SIZE(file) + len + 1);
  49. SET_HANDLE_TYPE(hdl, file);
  50. hdl->__in.flags |= RFD(0)|WFD(0)|WRITEABLE(0);
  51. hdl->file.fd = ret;
  52. hdl->file.offset = 0;
  53. hdl->file.append = 0;
  54. hdl->file.pass = 0;
  55. char * path = (void *) hdl + HANDLE_SIZE(file);
  56. memcpy(path, uri, len + 1);
  57. hdl->file.realpath = path;
  58. *handle = hdl;
  59. return 0;
  60. }
  61. #ifndef SEEK_SET
  62. # define SEEK_SET 0
  63. #endif
  64. /* 'read' operation for file streams. */
  65. static int file_read (PAL_HANDLE handle, int offset, int count,
  66. void * buffer)
  67. {
  68. int fd = handle->file.fd;
  69. int ret;
  70. if (handle->file.offset != offset) {
  71. ret = INLINE_SYSCALL(lseek, 3, fd, offset, SEEK_SET);
  72. if (IS_ERR(ret))
  73. return -PAL_ERROR_DENIED;
  74. handle->file.offset = offset;
  75. }
  76. ret = INLINE_SYSCALL(read, 3, fd, buffer, count);
  77. if (IS_ERR(ret))
  78. return unix_to_pal_error(ERRNO(ret));
  79. handle->file.offset = offset + ret;
  80. return ret;
  81. }
  82. /* 'write' operation for file streams. */
  83. static int file_write (PAL_HANDLE handle, int offset, int count,
  84. const void * buffer)
  85. {
  86. int fd = handle->file.fd;
  87. int ret;
  88. if (handle->file.offset != offset) {
  89. ret = INLINE_SYSCALL(lseek, 3, fd, offset, SEEK_SET);
  90. if (IS_ERR(ret))
  91. return -PAL_ERROR_DENIED;
  92. handle->file.offset = offset;
  93. }
  94. ret = INLINE_SYSCALL(write, 3, fd, buffer, count);
  95. if (IS_ERR(ret))
  96. return unix_to_pal_error(ERRNO(ret));
  97. handle->file.offset = offset + ret;
  98. return ret;
  99. }
  100. /* 'close' operation for file streams. In this case, it will only
  101. close the file withou deleting it. */
  102. static int file_close (PAL_HANDLE handle)
  103. {
  104. int fd = handle->file.fd;
  105. int ret = INLINE_SYSCALL(close, 1, fd);
  106. if (handle->file.realpath &&
  107. handle->file.realpath != (void *) handle + HANDLE_SIZE(file))
  108. free((void *) handle->file.realpath);
  109. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  110. }
  111. /* 'delete' operation for file streams. It will actually delete
  112. the file if we can successfully close it. */
  113. static int file_delete (PAL_HANDLE handle, int access)
  114. {
  115. if (access)
  116. return -PAL_ERROR_INVAL;
  117. INLINE_SYSCALL(unlink, 1, handle->file.realpath);
  118. return 0;
  119. }
  120. /* 'map' operation for file stream. */
  121. static int file_map (PAL_HANDLE handle, void ** addr, int prot,
  122. int offset, int size)
  123. {
  124. int fd = handle->file.fd;
  125. void * mem = *addr;
  126. int flags = MAP_FILE|HOST_FLAGS(0, prot)|(mem ? MAP_FIXED : 0);
  127. prot = HOST_PROT(prot);
  128. /* The memory will always allocated with flag MAP_PRIVATE
  129. and MAP_FILE */
  130. mem = (void *) ARCH_MMAP(mem, size, prot, flags, fd, offset);
  131. if (IS_ERR_P(mem))
  132. return -PAL_ERROR_DENIED;
  133. *addr = mem;
  134. return 0;
  135. }
  136. /* 'setlength' operation for file stream. */
  137. static int file_setlength (PAL_HANDLE handle, int length)
  138. {
  139. int ret = INLINE_SYSCALL(ftruncate, 2, handle->file.fd, length);
  140. if (IS_ERR(ret))
  141. return (ERRNO(ret) == EINVAL || ERRNO(ret) == EBADF) ?
  142. -PAL_ERROR_BADHANDLE : -PAL_ERROR_DENIED;
  143. return length;
  144. }
  145. /* 'flush' operation for file stream. */
  146. static int file_flush (PAL_HANDLE handle)
  147. {
  148. int ret = INLINE_SYSCALL(fsync, 1, handle->file.fd);
  149. if (IS_ERR(ret))
  150. return (ERRNO(ret) == EINVAL || ERRNO(ret) == EBADF) ?
  151. -PAL_ERROR_BADHANDLE : -PAL_ERROR_DENIED;
  152. return 0;
  153. }
  154. static inline int file_stat_type (struct stat * stat)
  155. {
  156. if (S_ISREG(stat->st_mode))
  157. return pal_type_file;
  158. if (S_ISDIR(stat->st_mode))
  159. return pal_type_dir;
  160. if (S_ISCHR(stat->st_mode))
  161. return pal_type_dev;
  162. if (S_ISFIFO(stat->st_mode))
  163. return pal_type_pipe;
  164. if (S_ISSOCK(stat->st_mode))
  165. return pal_type_dev;
  166. return 0;
  167. }
  168. /* copy attr content from POSIX stat struct to PAL_STREAM_ATTR */
  169. static inline void
  170. file_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat)
  171. {
  172. memset(attr, 0, sizeof(PAL_STREAM_ATTR));
  173. attr->type = file_stat_type(stat);
  174. attr->file_id = stat->st_ino;
  175. attr->size = stat->st_size;
  176. attr->access_time = stat->st_atime;
  177. attr->change_time = stat->st_mtime;
  178. attr->create_time = stat->st_ctime;
  179. attr->readable = stataccess(stat, ACCESS_R);
  180. attr->writeable = stataccess(stat, ACCESS_W);
  181. attr->runnable = stataccess(stat, ACCESS_X);
  182. attr->share_flags = stat->st_mode;
  183. }
  184. /* 'attrquery' operation for file streams */
  185. static int file_attrquery (const char * type, const char * uri,
  186. PAL_STREAM_ATTR * attr)
  187. {
  188. struct stat stat_buf;
  189. /* try to do the real open */
  190. int ret = INLINE_SYSCALL(stat, 2, uri, &stat_buf);
  191. /* if it failed, return the right error code */
  192. if (IS_ERR(ret))
  193. return unix_to_pal_error(ERRNO(ret));
  194. file_attrcopy(attr, &stat_buf);
  195. return 0;
  196. }
  197. /* 'attrquerybyhdl' operation for file streams */
  198. static int file_attrquerybyhdl (PAL_HANDLE handle,
  199. PAL_STREAM_ATTR * attr)
  200. {
  201. int fd = handle->__in.fds[0];
  202. struct stat stat_buf;
  203. int ret = INLINE_SYSCALL(fstat, 2, fd, &stat_buf);
  204. if (IS_ERR(ret))
  205. return unix_to_pal_error(ERRNO(ret));
  206. file_attrcopy(attr, &stat_buf);
  207. return 0;
  208. }
  209. static int file_attrsetbyhdl (PAL_HANDLE handle,
  210. PAL_STREAM_ATTR * attr)
  211. {
  212. int fd = handle->__in.fds[0], ret;
  213. ret = INLINE_SYSCALL(fchmod, 2, fd, attr->share_flags);
  214. if (IS_ERR(ret))
  215. return unix_to_pal_error(ERRNO(ret));
  216. return 0;
  217. }
  218. static int file_rename (PAL_HANDLE handle, const char * type,
  219. const char * uri)
  220. {
  221. int ret = INLINE_SYSCALL(rename, 2, handle->file.realpath, uri);
  222. if (IS_ERR(ret))
  223. return unix_to_pal_error(ERRNO(ret));
  224. handle->file.realpath = remalloc(uri, strlen(uri));
  225. return 0;
  226. }
  227. static int file_getname (PAL_HANDLE handle, char * buffer, int count)
  228. {
  229. if (!handle->file.realpath)
  230. return 0;
  231. int len = strlen(handle->file.realpath);
  232. if (len + 5 >= count)
  233. return -PAL_ERROR_TOOLONG;
  234. memcpy(buffer, "file:", 5);
  235. memcpy(buffer + 5, handle->file.realpath, len + 1);
  236. return len + 5;
  237. }
  238. const char * file_getrealpath (PAL_HANDLE handle)
  239. {
  240. return handle->file.realpath;
  241. }
  242. struct handle_ops file_ops = {
  243. .getname = &file_getname,
  244. .getrealpath = &file_getrealpath,
  245. .open = &file_open,
  246. .read = &file_read,
  247. .write = &file_write,
  248. .close = &file_close,
  249. .delete = &file_delete,
  250. .map = &file_map,
  251. .setlength = &file_setlength,
  252. .flush = &file_flush,
  253. .attrquery = &file_attrquery,
  254. .attrquerybyhdl = &file_attrquerybyhdl,
  255. .attrsetbyhdl = &file_attrsetbyhdl,
  256. .rename = &file_rename,
  257. };
  258. /* 'open' operation for directory stream. Directory stream does not have a
  259. specific type prefix, its URI looks the same file streams, plus it
  260. ended with slashes. dir_open will be called by file_open. */
  261. static int dir_open (PAL_HANDLE * handle, const char * type, const char * uri,
  262. int access, int share, int create, int options)
  263. {
  264. int ret;
  265. if (create & PAL_CREAT_TRY) {
  266. ret = INLINE_SYSCALL(mkdir, 2, uri, share);
  267. if (IS_ERR(ret) && ERRNO(ret) == EEXIST &&
  268. create & PAL_CREAT_ALWAYS)
  269. return -PAL_ERROR_STREAMEXIST;
  270. }
  271. ret = INLINE_SYSCALL(open, 3, uri, O_DIRECTORY|options|O_CLOEXEC, 0);
  272. if (IS_ERR(ret))
  273. return unix_to_pal_error(ERRNO(ret));
  274. int len = strlen(uri);
  275. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dir) + len + 1);
  276. SET_HANDLE_TYPE(hdl, dir);
  277. hdl->__in.flags |= RFD(0);
  278. hdl->dir.fd = ret;
  279. char * path = (void *) hdl + HANDLE_SIZE(dir);
  280. memcpy(path, uri, len + 1);
  281. hdl->dir.realpath = path;
  282. hdl->dir.buf = NULL;
  283. hdl->dir.ptr = NULL;
  284. hdl->dir.end = NULL;
  285. hdl->dir.endofstream = false;
  286. *handle = hdl;
  287. return 0;
  288. }
  289. struct linux_dirent64 {
  290. unsigned long d_ino;
  291. unsigned long d_off;
  292. unsigned short d_reclen;
  293. unsigned char d_type;
  294. char d_name[];
  295. };
  296. #define DT_UNKNOWN 0
  297. #define DT_FIFO 1
  298. #define DT_CHR 2
  299. #define DT_DIR 4
  300. #define DT_BLK 6
  301. #define DT_REG 8
  302. #define DT_LNK 10
  303. #define DT_SOCK 12
  304. #define DT_WHT 14
  305. #define DIRBUF_SIZE 1024
  306. /* 'read' operation for directory stream. Directory stream will not
  307. need a 'write' operat4on. */
  308. int dir_read (PAL_HANDLE handle, int offset, int count, void * buf)
  309. {
  310. void * dent_buf = handle->dir.buf ? : __alloca(DIRBUF_SIZE);
  311. void * ptr = handle->dir.ptr;
  312. void * end = handle->dir.end;
  313. int bytes = 0;
  314. if (ptr && ptr < end)
  315. goto output;
  316. do {
  317. if (handle->dir.endofstream)
  318. break;
  319. int size = INLINE_SYSCALL(getdents64, 3, handle->dir.fd, dent_buf,
  320. DIRBUF_SIZE);
  321. if (IS_ERR(size))
  322. return -PAL_ERROR_DENIED;
  323. if (size == 0) {
  324. handle->dir.endofstream = PAL_TRUE;
  325. break;
  326. }
  327. ptr = dent_buf;
  328. end = dent_buf + size;
  329. output:
  330. while (ptr < end) {
  331. struct linux_dirent64 * d = (struct linux_dirent64 *) ptr;
  332. if (d->d_name[0] == '.' &&
  333. (!d->d_name[1] || d->d_name[1] == '.'))
  334. goto next;
  335. bool isdir = (d->d_type == DT_DIR);
  336. int len = strlen(d->d_name);
  337. if (len + (isdir ? 2 : 1) > count)
  338. break;
  339. memcpy(buf, d->d_name, len);
  340. if (isdir)
  341. ((char *) buf)[len++] = '/';
  342. ((char *) buf)[len++] = '\0';
  343. bytes += len;
  344. buf += len;
  345. count -= len;
  346. next:
  347. ptr += d->d_reclen;
  348. }
  349. } while (ptr == end);
  350. if (ptr < end) {
  351. if (!handle->dir.buf)
  352. handle->dir.buf = malloc(DIRBUF_SIZE);
  353. if (handle->dir.buf != ptr) {
  354. memmove(handle->dir.buf, ptr, end - ptr);
  355. end = handle->dir.buf + (end - ptr);
  356. ptr = handle->dir.buf;
  357. }
  358. if (!bytes)
  359. return -PAL_ERROR_OVERFLOW;
  360. }
  361. return bytes ? : -PAL_ERROR_ENDOFSTREAM;
  362. }
  363. /* 'close' operation of directory streams */
  364. static int dir_close (PAL_HANDLE handle)
  365. {
  366. int fd = handle->dir.fd;
  367. int ret = INLINE_SYSCALL(close, 1, fd);
  368. if (handle->dir.buf) {
  369. free(handle->dir.buf);
  370. handle->dir.buf = handle->dir.ptr = handle->dir.end = NULL;
  371. }
  372. if (handle->dir.realpath &&
  373. handle->dir.realpath != (void *) handle + HANDLE_SIZE(dir))
  374. free((void *) handle->dir.realpath);
  375. if (IS_ERR(ret))
  376. return -PAL_ERROR_BADHANDLE;
  377. return 0;
  378. }
  379. /* 'delete' operation of directoy streams */
  380. static int dir_delete (PAL_HANDLE handle, int access)
  381. {
  382. if (access)
  383. return -PAL_ERROR_INVAL;
  384. int ret = dir_close(handle);
  385. if (ret < 0)
  386. return ret;
  387. ret = INLINE_SYSCALL(rmdir, 1, handle->dir.realpath);
  388. return (IS_ERR(ret) && ERRNO(ret) != ENOENT) ?
  389. -PAL_ERROR_DENIED : 0;
  390. }
  391. static int dir_rename (PAL_HANDLE handle, const char * type,
  392. const char * uri)
  393. {
  394. int ret = INLINE_SYSCALL(rename, 2, handle->dir.realpath, uri);
  395. if (IS_ERR(ret))
  396. return unix_to_pal_error(ERRNO(ret));
  397. handle->dir.realpath = remalloc(uri, strlen(uri));
  398. return 0;
  399. }
  400. static int dir_getname (PAL_HANDLE handle, char * buffer, int count)
  401. {
  402. if (!handle->dir.realpath)
  403. return 0;
  404. int len = strlen(handle->dir.realpath);
  405. if (len + 6 >= count)
  406. return -PAL_ERROR_TOOLONG;
  407. memcpy(buffer, "file:", 5);
  408. memcpy(buffer + 5, handle->dir.realpath, len);
  409. buffer[len + 5] = '/';
  410. buffer[len + 6] = 0;
  411. return len + 6;
  412. }
  413. static const char * dir_getrealpath (PAL_HANDLE handle)
  414. {
  415. return handle->dir.realpath;
  416. }
  417. struct handle_ops dir_ops = {
  418. .getname = &dir_getname,
  419. .getrealpath = &dir_getrealpath,
  420. .open = &dir_open,
  421. .read = &dir_read,
  422. .close = &dir_close,
  423. .delete = &dir_delete,
  424. .attrquery = &file_attrquery,
  425. .attrquerybyhdl = &file_attrquerybyhdl,
  426. .attrsetbyhdl = &file_attrsetbyhdl,
  427. .rename = &dir_rename,
  428. };