db_files.c 16 KB

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