db_files.c 16 KB

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