source: mainline/uspace/lib/libc/generic/vfs/vfs.c@ a47d49f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a47d49f was 2b88074b, checked in by Martin Decky <martin@…>, 16 years ago

vfs: file descriptors housekeeping changes

  • add support for allocating new file descriptors from the end of the fd range (O_DESC)
  • add support for assigning of an already opened file to a free file descriptor

vfs: VFS_OUT_CLOSE is called only when the file reference count is about to drop to zero
vfs: implement VFS_IN_DUP

libc: optimize current working directory housekeeping

  • using opendir() was an overkill
  • allocate current working directory file descriptor from the end of the fd range using O_DESC (so it doesn't mess with the well-known descriptors 0, 1 and 2)

libc: implement dup2() (via VFS_IN_DUP)

getvc: change stdin/stdout/stderr by a slightly more elegant way (using dup2())

  • Property mode set to 100644
File size: 14.8 KB
Line 
1/*
2 * Copyright (c) 2008 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35#include <vfs/vfs.h>
36#include <vfs/canonify.h>
37#include <stdlib.h>
38#include <unistd.h>
39#include <dirent.h>
40#include <fcntl.h>
41#include <stdio.h>
42#include <sys/stat.h>
43#include <sys/types.h>
44#include <ipc/ipc.h>
45#include <ipc/services.h>
46#include <async.h>
47#include <atomic.h>
48#include <futex.h>
49#include <errno.h>
50#include <string.h>
51#include <devmap.h>
52#include <ipc/vfs.h>
53#include <ipc/devmap.h>
54
55static int vfs_phone = -1;
56static futex_t vfs_phone_futex = FUTEX_INITIALIZER;
57static futex_t cwd_futex = FUTEX_INITIALIZER;
58
59static int cwd_fd = -1;
60static char *cwd_path = NULL;
61static size_t cwd_size = 0;
62
63char *absolutize(const char *path, size_t *retlen)
64{
65 char *ncwd_path;
66 char *ncwd_path_nc;
67
68 futex_down(&cwd_futex);
69 size_t size = str_size(path);
70 if (*path != '/') {
71 if (!cwd_path) {
72 futex_up(&cwd_futex);
73 return NULL;
74 }
75 ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
76 if (!ncwd_path_nc) {
77 futex_up(&cwd_futex);
78 return NULL;
79 }
80 str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
81 ncwd_path_nc[cwd_size] = '/';
82 ncwd_path_nc[cwd_size + 1] = '\0';
83 } else {
84 ncwd_path_nc = malloc(size + 1);
85 if (!ncwd_path_nc) {
86 futex_up(&cwd_futex);
87 return NULL;
88 }
89 ncwd_path_nc[0] = '\0';
90 }
91 str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
92 ncwd_path = canonify(ncwd_path_nc, retlen);
93 if (!ncwd_path) {
94 futex_up(&cwd_futex);
95 free(ncwd_path_nc);
96 return NULL;
97 }
98 /*
99 * We need to clone ncwd_path because canonify() works in-place and thus
100 * the address in ncwd_path need not be the same as ncwd_path_nc, even
101 * though they both point into the same dynamically allocated buffer.
102 */
103 ncwd_path = str_dup(ncwd_path);
104 free(ncwd_path_nc);
105 if (!ncwd_path) {
106 futex_up(&cwd_futex);
107 return NULL;
108 }
109 futex_up(&cwd_futex);
110 return ncwd_path;
111}
112
113static void vfs_connect(void)
114{
115 while (vfs_phone < 0)
116 vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0);
117}
118
119int mount(const char *fs_name, const char *mp, const char *dev,
120 const char *opts, unsigned int flags)
121{
122 int res;
123 ipcarg_t rc;
124 ipcarg_t rc_orig;
125 aid_t req;
126 dev_handle_t dev_handle;
127
128 res = devmap_device_get_handle(dev, &dev_handle, flags);
129 if (res != EOK)
130 return res;
131
132 size_t mpa_size;
133 char *mpa = absolutize(mp, &mpa_size);
134 if (!mpa)
135 return ENOMEM;
136
137 futex_down(&vfs_phone_futex);
138 async_serialize_start();
139 vfs_connect();
140
141 req = async_send_2(vfs_phone, VFS_IN_MOUNT, dev_handle, flags, NULL);
142 rc = async_data_write_start(vfs_phone, (void *) mpa, mpa_size);
143 if (rc != EOK) {
144 async_wait_for(req, &rc_orig);
145 async_serialize_end();
146 futex_up(&vfs_phone_futex);
147 free(mpa);
148 if (rc_orig == EOK)
149 return (int) rc;
150 else
151 return (int) rc_orig;
152 }
153
154 rc = async_data_write_start(vfs_phone, (void *) opts, str_size(opts));
155 if (rc != EOK) {
156 async_wait_for(req, &rc_orig);
157 async_serialize_end();
158 futex_up(&vfs_phone_futex);
159 free(mpa);
160 if (rc_orig == EOK)
161 return (int) rc;
162 else
163 return (int) rc_orig;
164 }
165
166 rc = async_data_write_start(vfs_phone, (void *) fs_name, str_size(fs_name));
167 if (rc != EOK) {
168 async_wait_for(req, &rc_orig);
169 async_serialize_end();
170 futex_up(&vfs_phone_futex);
171 free(mpa);
172 if (rc_orig == EOK)
173 return (int) rc;
174 else
175 return (int) rc_orig;
176 }
177
178 /* Ask VFS whether it likes fs_name. */
179 rc = async_req_0_0(vfs_phone, IPC_M_PING);
180 if (rc != EOK) {
181 async_wait_for(req, &rc_orig);
182 async_serialize_end();
183 futex_up(&vfs_phone_futex);
184 free(mpa);
185 if (rc_orig == EOK)
186 return (int) rc;
187 else
188 return (int) rc_orig;
189 }
190
191 async_wait_for(req, &rc);
192 async_serialize_end();
193 futex_up(&vfs_phone_futex);
194 free(mpa);
195
196 return (int) rc;
197}
198
199static int open_internal(const char *abs, size_t abs_size, int lflag, int oflag)
200{
201 futex_down(&vfs_phone_futex);
202 async_serialize_start();
203 vfs_connect();
204
205 ipc_call_t answer;
206 aid_t req = async_send_3(vfs_phone, VFS_IN_OPEN, lflag, oflag, 0, &answer);
207 ipcarg_t rc = async_data_write_start(vfs_phone, abs, abs_size);
208
209 if (rc != EOK) {
210 ipcarg_t rc_orig;
211 async_wait_for(req, &rc_orig);
212
213 async_serialize_end();
214 futex_up(&vfs_phone_futex);
215
216 if (rc_orig == EOK)
217 return (int) rc;
218 else
219 return (int) rc_orig;
220 }
221
222 async_wait_for(req, &rc);
223 async_serialize_end();
224 futex_up(&vfs_phone_futex);
225
226 if (rc != EOK)
227 return (int) rc;
228
229 return (int) IPC_GET_ARG1(answer);
230}
231
232int open(const char *path, int oflag, ...)
233{
234 size_t abs_size;
235 char *abs = absolutize(path, &abs_size);
236 if (!abs)
237 return ENOMEM;
238
239 int ret = open_internal(abs, abs_size, L_FILE, oflag);
240 free(abs);
241
242 return ret;
243}
244
245int open_node(fdi_node_t *node, int oflag)
246{
247 futex_down(&vfs_phone_futex);
248 async_serialize_start();
249 vfs_connect();
250
251 ipc_call_t answer;
252 aid_t req = async_send_4(vfs_phone, VFS_IN_OPEN_NODE, node->fs_handle,
253 node->dev_handle, node->index, oflag, &answer);
254
255 ipcarg_t rc;
256 async_wait_for(req, &rc);
257 async_serialize_end();
258 futex_up(&vfs_phone_futex);
259
260 if (rc != EOK)
261 return (int) rc;
262
263 return (int) IPC_GET_ARG1(answer);
264}
265
266int close(int fildes)
267{
268 ipcarg_t rc;
269
270 futex_down(&vfs_phone_futex);
271 async_serialize_start();
272 vfs_connect();
273
274 rc = async_req_1_0(vfs_phone, VFS_IN_CLOSE, fildes);
275
276 async_serialize_end();
277 futex_up(&vfs_phone_futex);
278
279 return (int)rc;
280}
281
282ssize_t read(int fildes, void *buf, size_t nbyte)
283{
284 ipcarg_t rc;
285 ipc_call_t answer;
286 aid_t req;
287
288 futex_down(&vfs_phone_futex);
289 async_serialize_start();
290 vfs_connect();
291
292 req = async_send_1(vfs_phone, VFS_IN_READ, fildes, &answer);
293 rc = async_data_read_start(vfs_phone, (void *)buf, nbyte);
294 if (rc != EOK) {
295 ipcarg_t rc_orig;
296
297 async_wait_for(req, &rc_orig);
298 async_serialize_end();
299 futex_up(&vfs_phone_futex);
300 if (rc_orig == EOK)
301 return (ssize_t) rc;
302 else
303 return (ssize_t) rc_orig;
304 }
305 async_wait_for(req, &rc);
306 async_serialize_end();
307 futex_up(&vfs_phone_futex);
308 if (rc == EOK)
309 return (ssize_t) IPC_GET_ARG1(answer);
310 else
311 return rc;
312}
313
314ssize_t write(int fildes, const void *buf, size_t nbyte)
315{
316 ipcarg_t rc;
317 ipc_call_t answer;
318 aid_t req;
319
320 futex_down(&vfs_phone_futex);
321 async_serialize_start();
322 vfs_connect();
323
324 req = async_send_1(vfs_phone, VFS_IN_WRITE, fildes, &answer);
325 rc = async_data_write_start(vfs_phone, (void *)buf, nbyte);
326 if (rc != EOK) {
327 ipcarg_t rc_orig;
328
329 async_wait_for(req, &rc_orig);
330 async_serialize_end();
331 futex_up(&vfs_phone_futex);
332 if (rc_orig == EOK)
333 return (ssize_t) rc;
334 else
335 return (ssize_t) rc_orig;
336 }
337 async_wait_for(req, &rc);
338 async_serialize_end();
339 futex_up(&vfs_phone_futex);
340 if (rc == EOK)
341 return (ssize_t) IPC_GET_ARG1(answer);
342 else
343 return -1;
344}
345
346int fsync(int fildes)
347{
348 futex_down(&vfs_phone_futex);
349 async_serialize_start();
350 vfs_connect();
351
352 ipcarg_t rc = async_req_1_0(vfs_phone, VFS_IN_SYNC, fildes);
353
354 async_serialize_end();
355 futex_up(&vfs_phone_futex);
356
357 return (int) rc;
358}
359
360off_t lseek(int fildes, off_t offset, int whence)
361{
362 ipcarg_t rc;
363
364 futex_down(&vfs_phone_futex);
365 async_serialize_start();
366 vfs_connect();
367
368 ipcarg_t newoffs;
369 rc = async_req_3_1(vfs_phone, VFS_IN_SEEK, fildes, offset, whence,
370 &newoffs);
371
372 async_serialize_end();
373 futex_up(&vfs_phone_futex);
374
375 if (rc != EOK)
376 return (off_t) -1;
377
378 return (off_t) newoffs;
379}
380
381int ftruncate(int fildes, off_t length)
382{
383 ipcarg_t rc;
384
385 futex_down(&vfs_phone_futex);
386 async_serialize_start();
387 vfs_connect();
388
389 rc = async_req_2_0(vfs_phone, VFS_IN_TRUNCATE, fildes, length);
390 async_serialize_end();
391 futex_up(&vfs_phone_futex);
392 return (int) rc;
393}
394
395int fstat(int fildes, struct stat *stat)
396{
397 ipcarg_t rc;
398 aid_t req;
399
400 futex_down(&vfs_phone_futex);
401 async_serialize_start();
402 vfs_connect();
403
404 req = async_send_1(vfs_phone, VFS_IN_FSTAT, fildes, NULL);
405 rc = async_data_read_start(vfs_phone, (void *)stat, sizeof(struct stat));
406 if (rc != EOK) {
407 ipcarg_t rc_orig;
408
409 async_wait_for(req, &rc_orig);
410 async_serialize_end();
411 futex_up(&vfs_phone_futex);
412 if (rc_orig == EOK)
413 return (ssize_t) rc;
414 else
415 return (ssize_t) rc_orig;
416 }
417 async_wait_for(req, &rc);
418 async_serialize_end();
419 futex_up(&vfs_phone_futex);
420
421 return rc;
422}
423
424int stat(const char *path, struct stat *stat)
425{
426 ipcarg_t rc;
427 ipcarg_t rc_orig;
428 aid_t req;
429
430 size_t pa_size;
431 char *pa = absolutize(path, &pa_size);
432 if (!pa)
433 return ENOMEM;
434
435 futex_down(&vfs_phone_futex);
436 async_serialize_start();
437 vfs_connect();
438
439 req = async_send_0(vfs_phone, VFS_IN_STAT, NULL);
440 rc = async_data_write_start(vfs_phone, pa, pa_size);
441 if (rc != EOK) {
442 async_wait_for(req, &rc_orig);
443 async_serialize_end();
444 futex_up(&vfs_phone_futex);
445 free(pa);
446 if (rc_orig == EOK)
447 return (int) rc;
448 else
449 return (int) rc_orig;
450 }
451 rc = async_data_read_start(vfs_phone, stat, sizeof(struct stat));
452 if (rc != EOK) {
453 async_wait_for(req, &rc_orig);
454 async_serialize_end();
455 futex_up(&vfs_phone_futex);
456 free(pa);
457 if (rc_orig == EOK)
458 return (int) rc;
459 else
460 return (int) rc_orig;
461 }
462 async_wait_for(req, &rc);
463 async_serialize_end();
464 futex_up(&vfs_phone_futex);
465 free(pa);
466 return rc;
467}
468
469DIR *opendir(const char *dirname)
470{
471 DIR *dirp = malloc(sizeof(DIR));
472 if (!dirp)
473 return NULL;
474
475 size_t abs_size;
476 char *abs = absolutize(dirname, &abs_size);
477 if (!abs) {
478 free(dirp);
479 return ENOMEM;
480 }
481
482 int ret = open_internal(abs, abs_size, L_DIRECTORY, 0);
483 free(abs);
484
485 if (ret < 0) {
486 free(dirp);
487 return NULL;
488 }
489
490 dirp->fd = ret;
491 return dirp;
492}
493
494struct dirent *readdir(DIR *dirp)
495{
496 ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
497 if (len <= 0)
498 return NULL;
499 return &dirp->res;
500}
501
502void rewinddir(DIR *dirp)
503{
504 (void) lseek(dirp->fd, 0, SEEK_SET);
505}
506
507int closedir(DIR *dirp)
508{
509 (void) close(dirp->fd);
510 free(dirp);
511 return 0;
512}
513
514int mkdir(const char *path, mode_t mode)
515{
516 ipcarg_t rc;
517 aid_t req;
518
519 size_t pa_size;
520 char *pa = absolutize(path, &pa_size);
521 if (!pa)
522 return ENOMEM;
523
524 futex_down(&vfs_phone_futex);
525 async_serialize_start();
526 vfs_connect();
527
528 req = async_send_1(vfs_phone, VFS_IN_MKDIR, mode, NULL);
529 rc = async_data_write_start(vfs_phone, pa, pa_size);
530 if (rc != EOK) {
531 ipcarg_t rc_orig;
532
533 async_wait_for(req, &rc_orig);
534 async_serialize_end();
535 futex_up(&vfs_phone_futex);
536 free(pa);
537 if (rc_orig == EOK)
538 return (int) rc;
539 else
540 return (int) rc_orig;
541 }
542 async_wait_for(req, &rc);
543 async_serialize_end();
544 futex_up(&vfs_phone_futex);
545 free(pa);
546 return rc;
547}
548
549static int _unlink(const char *path, int lflag)
550{
551 ipcarg_t rc;
552 aid_t req;
553
554 size_t pa_size;
555 char *pa = absolutize(path, &pa_size);
556 if (!pa)
557 return ENOMEM;
558
559 futex_down(&vfs_phone_futex);
560 async_serialize_start();
561 vfs_connect();
562
563 req = async_send_0(vfs_phone, VFS_IN_UNLINK, NULL);
564 rc = async_data_write_start(vfs_phone, pa, pa_size);
565 if (rc != EOK) {
566 ipcarg_t rc_orig;
567
568 async_wait_for(req, &rc_orig);
569 async_serialize_end();
570 futex_up(&vfs_phone_futex);
571 free(pa);
572 if (rc_orig == EOK)
573 return (int) rc;
574 else
575 return (int) rc_orig;
576 }
577 async_wait_for(req, &rc);
578 async_serialize_end();
579 futex_up(&vfs_phone_futex);
580 free(pa);
581 return rc;
582}
583
584int unlink(const char *path)
585{
586 return _unlink(path, L_NONE);
587}
588
589int rmdir(const char *path)
590{
591 return _unlink(path, L_DIRECTORY);
592}
593
594int rename(const char *old, const char *new)
595{
596 ipcarg_t rc;
597 ipcarg_t rc_orig;
598 aid_t req;
599
600 size_t olda_size;
601 char *olda = absolutize(old, &olda_size);
602 if (!olda)
603 return ENOMEM;
604
605 size_t newa_size;
606 char *newa = absolutize(new, &newa_size);
607 if (!newa) {
608 free(olda);
609 return ENOMEM;
610 }
611
612 futex_down(&vfs_phone_futex);
613 async_serialize_start();
614 vfs_connect();
615
616 req = async_send_0(vfs_phone, VFS_IN_RENAME, NULL);
617 rc = async_data_write_start(vfs_phone, olda, olda_size);
618 if (rc != EOK) {
619 async_wait_for(req, &rc_orig);
620 async_serialize_end();
621 futex_up(&vfs_phone_futex);
622 free(olda);
623 free(newa);
624 if (rc_orig == EOK)
625 return (int) rc;
626 else
627 return (int) rc_orig;
628 }
629 rc = async_data_write_start(vfs_phone, newa, newa_size);
630 if (rc != EOK) {
631 async_wait_for(req, &rc_orig);
632 async_serialize_end();
633 futex_up(&vfs_phone_futex);
634 free(olda);
635 free(newa);
636 if (rc_orig == EOK)
637 return (int) rc;
638 else
639 return (int) rc_orig;
640 }
641 async_wait_for(req, &rc);
642 async_serialize_end();
643 futex_up(&vfs_phone_futex);
644 free(olda);
645 free(newa);
646 return rc;
647}
648
649int chdir(const char *path)
650{
651 size_t abs_size;
652 char *abs = absolutize(path, &abs_size);
653 if (!abs)
654 return ENOMEM;
655
656 int fd = open_internal(abs, abs_size, L_DIRECTORY, O_DESC);
657
658 if (fd < 0) {
659 free(abs);
660 return ENOENT;
661 }
662
663 futex_down(&cwd_futex);
664
665 if (cwd_fd >= 0)
666 close(cwd_fd);
667
668
669 if (cwd_path)
670 free(cwd_path);
671
672 cwd_fd = fd;
673 cwd_path = abs;
674 cwd_size = abs_size;
675
676 futex_up(&cwd_futex);
677 return EOK;
678}
679
680char *getcwd(char *buf, size_t size)
681{
682 if (size == 0)
683 return NULL;
684
685 futex_down(&cwd_futex);
686
687 if ((cwd_size == 0) || (size < cwd_size + 1)) {
688 futex_up(&cwd_futex);
689 return NULL;
690 }
691
692 str_cpy(buf, size, cwd_path);
693 futex_up(&cwd_futex);
694
695 return buf;
696}
697
698int fd_phone(int fildes)
699{
700 struct stat stat;
701 int rc;
702
703 rc = fstat(fildes, &stat);
704
705 if (!stat.devfs_stat.device)
706 return -1;
707
708 return devmap_device_connect(stat.devfs_stat.device, 0);
709}
710
711int fd_node(int fildes, fdi_node_t *node)
712{
713 struct stat stat;
714 int rc;
715
716 rc = fstat(fildes, &stat);
717
718 if (rc == EOK) {
719 node->fs_handle = stat.fs_handle;
720 node->dev_handle = stat.dev_handle;
721 node->index = stat.index;
722 }
723
724 return rc;
725}
726
727int dup2(int oldfd, int newfd)
728{
729 futex_down(&vfs_phone_futex);
730 async_serialize_start();
731 vfs_connect();
732
733 ipcarg_t ret;
734 ipcarg_t rc = async_req_2_1(vfs_phone, VFS_IN_DUP, oldfd, newfd, &ret);
735
736 async_serialize_end();
737 futex_up(&vfs_phone_futex);
738
739 if (rc == EOK)
740 return (int) ret;
741
742 return (int) rc;
743}
744
745/** @}
746 */
Note: See TracBrowser for help on using the repository browser.