db_files.c 16 KB

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