source: mainline/uspace/lib/c/generic/vfs/vfs.c@ 5bcd5b7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5bcd5b7 was 778d26d, checked in by Jiri Zarevucky <zarevucky.jiri@…>, 12 years ago

Relativize rename().

  • Property mode set to 100644
File size: 18.0 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/canonify.h>
36#include <vfs/vfs.h>
37#include <vfs/vfs_sess.h>
38#include <macros.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include <dirent.h>
42#include <fcntl.h>
43#include <stdio.h>
44#include <sys/stat.h>
45#include <sys/types.h>
46#include <ipc/services.h>
47#include <ns.h>
48#include <async.h>
49#include <fibril_synch.h>
50#include <errno.h>
51#include <assert.h>
52#include <str.h>
53#include <loc.h>
54#include <ipc/vfs.h>
55#include <ipc/loc.h>
56
57static FIBRIL_MUTEX_INITIALIZE(vfs_mutex);
58static async_sess_t *vfs_sess = NULL;
59
60static FIBRIL_MUTEX_INITIALIZE(cwd_mutex);
61
62static int cwd_fd = -1;
63static char *cwd_path = NULL;
64static size_t cwd_size = 0;
65
66/** Start an async exchange on the VFS session.
67 *
68 * @return New exchange.
69 *
70 */
71async_exch_t *vfs_exchange_begin(void)
72{
73 fibril_mutex_lock(&vfs_mutex);
74
75 while (vfs_sess == NULL)
76 vfs_sess = service_connect_blocking(EXCHANGE_PARALLEL, SERVICE_VFS,
77 0, 0);
78
79 fibril_mutex_unlock(&vfs_mutex);
80
81 return async_exchange_begin(vfs_sess);
82}
83
84/** Finish an async exchange on the VFS session.
85 *
86 * @param exch Exchange to be finished.
87 *
88 */
89void vfs_exchange_end(async_exch_t *exch)
90{
91 async_exchange_end(exch);
92}
93
94int _vfs_walk(int parent, const char *path, int flags)
95{
96 async_exch_t *exch = vfs_exchange_begin();
97
98 ipc_call_t answer;
99 aid_t req = async_send_2(exch, VFS_IN_WALK, parent, flags, &answer);
100 sysarg_t rc = async_data_write_start(exch, path, str_size(path));
101 vfs_exchange_end(exch);
102
103 sysarg_t rc_orig;
104 async_wait_for(req, &rc_orig);
105
106 if (rc_orig != EOK) {
107 return (int) rc_orig;
108 }
109
110 if (rc != EOK) {
111 return (int) rc;
112 }
113
114 return (int) IPC_GET_ARG1(answer);
115}
116
117int _vfs_open(int fildes, int mode)
118{
119 async_exch_t *exch = vfs_exchange_begin();
120 sysarg_t rc = async_req_2_0(exch, VFS_IN_OPEN2, fildes, mode);
121 vfs_exchange_end(exch);
122
123 return (int) rc;
124}
125
126char *absolutize(const char *path, size_t *retlen)
127{
128 char *ncwd_path;
129 char *ncwd_path_nc;
130
131 fibril_mutex_lock(&cwd_mutex);
132 size_t size = str_size(path);
133 if (*path != '/') {
134 if (!cwd_path) {
135 fibril_mutex_unlock(&cwd_mutex);
136 return NULL;
137 }
138 ncwd_path_nc = malloc(cwd_size + 1 + size + 1);
139 if (!ncwd_path_nc) {
140 fibril_mutex_unlock(&cwd_mutex);
141 return NULL;
142 }
143 str_cpy(ncwd_path_nc, cwd_size + 1 + size + 1, cwd_path);
144 ncwd_path_nc[cwd_size] = '/';
145 ncwd_path_nc[cwd_size + 1] = '\0';
146 } else {
147 ncwd_path_nc = malloc(size + 1);
148 if (!ncwd_path_nc) {
149 fibril_mutex_unlock(&cwd_mutex);
150 return NULL;
151 }
152 ncwd_path_nc[0] = '\0';
153 }
154 str_append(ncwd_path_nc, cwd_size + 1 + size + 1, path);
155 ncwd_path = canonify(ncwd_path_nc, retlen);
156 if (!ncwd_path) {
157 fibril_mutex_unlock(&cwd_mutex);
158 free(ncwd_path_nc);
159 return NULL;
160 }
161 /*
162 * We need to clone ncwd_path because canonify() works in-place and thus
163 * the address in ncwd_path need not be the same as ncwd_path_nc, even
164 * though they both point into the same dynamically allocated buffer.
165 */
166 ncwd_path = str_dup(ncwd_path);
167 free(ncwd_path_nc);
168 if (!ncwd_path) {
169 fibril_mutex_unlock(&cwd_mutex);
170 return NULL;
171 }
172 fibril_mutex_unlock(&cwd_mutex);
173 return ncwd_path;
174}
175
176int mount(const char *fs_name, const char *mp, const char *fqsn,
177 const char *opts, unsigned int flags, unsigned int instance)
178{
179 int null_id = -1;
180 char null[LOC_NAME_MAXLEN];
181
182 if (str_cmp(fqsn, "") == 0) {
183 /* No device specified, create a fresh
184 null/%d device instead */
185 null_id = loc_null_create();
186
187 if (null_id == -1)
188 return ENOMEM;
189
190 snprintf(null, LOC_NAME_MAXLEN, "null/%d", null_id);
191 fqsn = null;
192 }
193
194 service_id_t service_id;
195 int res = loc_service_get_id(fqsn, &service_id, flags);
196 if (res != EOK) {
197 if (null_id != -1)
198 loc_null_destroy(null_id);
199
200 return res;
201 }
202
203 size_t mpa_size;
204 char *mpa = absolutize(mp, &mpa_size);
205 if (!mpa) {
206 if (null_id != -1)
207 loc_null_destroy(null_id);
208
209 return ENOMEM;
210 }
211
212 async_exch_t *exch = vfs_exchange_begin();
213
214 sysarg_t rc_orig;
215 aid_t req = async_send_3(exch, VFS_IN_MOUNT, service_id, flags,
216 instance, NULL);
217 sysarg_t rc = async_data_write_start(exch, (void *) mpa, mpa_size);
218 if (rc != EOK) {
219 vfs_exchange_end(exch);
220 free(mpa);
221 async_wait_for(req, &rc_orig);
222
223 if (null_id != -1)
224 loc_null_destroy(null_id);
225
226 if (rc_orig == EOK)
227 return (int) rc;
228 else
229 return (int) rc_orig;
230 }
231
232 rc = async_data_write_start(exch, (void *) opts, str_size(opts));
233 if (rc != EOK) {
234 vfs_exchange_end(exch);
235 free(mpa);
236 async_wait_for(req, &rc_orig);
237
238 if (null_id != -1)
239 loc_null_destroy(null_id);
240
241 if (rc_orig == EOK)
242 return (int) rc;
243 else
244 return (int) rc_orig;
245 }
246
247 rc = async_data_write_start(exch, (void *) fs_name, str_size(fs_name));
248 if (rc != EOK) {
249 vfs_exchange_end(exch);
250 free(mpa);
251 async_wait_for(req, &rc_orig);
252
253 if (null_id != -1)
254 loc_null_destroy(null_id);
255
256 if (rc_orig == EOK)
257 return (int) rc;
258 else
259 return (int) rc_orig;
260 }
261
262 /* Ask VFS whether it likes fs_name. */
263 rc = async_req_0_0(exch, VFS_IN_PING);
264 if (rc != EOK) {
265 vfs_exchange_end(exch);
266 free(mpa);
267 async_wait_for(req, &rc_orig);
268
269 if (null_id != -1)
270 loc_null_destroy(null_id);
271
272 if (rc_orig == EOK)
273 return (int) rc;
274 else
275 return (int) rc_orig;
276 }
277
278 vfs_exchange_end(exch);
279 free(mpa);
280 async_wait_for(req, &rc);
281
282 if ((rc != EOK) && (null_id != -1))
283 loc_null_destroy(null_id);
284
285 return (int) rc;
286}
287
288int unmount(const char *mp)
289{
290 sysarg_t rc;
291 sysarg_t rc_orig;
292 aid_t req;
293 size_t mpa_size;
294 char *mpa;
295
296 mpa = absolutize(mp, &mpa_size);
297 if (!mpa)
298 return ENOMEM;
299
300 async_exch_t *exch = vfs_exchange_begin();
301
302 req = async_send_0(exch, VFS_IN_UNMOUNT, NULL);
303 rc = async_data_write_start(exch, (void *) mpa, mpa_size);
304 if (rc != EOK) {
305 vfs_exchange_end(exch);
306 free(mpa);
307 async_wait_for(req, &rc_orig);
308 if (rc_orig == EOK)
309 return (int) rc;
310 else
311 return (int) rc_orig;
312 }
313
314
315 vfs_exchange_end(exch);
316 free(mpa);
317 async_wait_for(req, &rc);
318
319 return (int) rc;
320}
321
322static int walk_flags(int oflags)
323{
324 int flags = 0;
325 if (oflags & O_CREAT) {
326 if (oflags & O_EXCL) {
327 flags |= WALK_MUST_CREATE;
328 } else {
329 flags |= WALK_MAY_CREATE;
330 }
331 }
332 return flags;
333}
334
335int open(const char *path, int oflag, ...)
336{
337 // FIXME: Some applications call this incorrectly.
338 if ((oflag & (O_RDONLY|O_WRONLY|O_RDWR)) == 0) {
339 oflag |= O_RDWR;
340 }
341
342 assert((((oflag & O_RDONLY) != 0) + ((oflag & O_WRONLY) != 0) + ((oflag & O_RDWR) != 0)) == 1);
343
344 size_t abs_size;
345 char *abs = absolutize(path, &abs_size);
346 if (!abs) {
347 return ENOMEM;
348 }
349
350 int ret = _vfs_walk(-1, abs, walk_flags(oflag) | WALK_REGULAR);
351 if (ret < 0) {
352 return ret;
353 }
354
355 int mode =
356 ((oflag & O_RDWR) ? MODE_READ|MODE_WRITE : 0) |
357 ((oflag & O_RDONLY) ? MODE_READ : 0) |
358 ((oflag & O_WRONLY) ? MODE_WRITE : 0) |
359 ((oflag & O_APPEND) ? MODE_APPEND : 0);
360
361 int rc = _vfs_open(ret, mode);
362 if (rc < 0) {
363 // _vfs_put(ret);
364 close(ret);
365 return rc;
366 }
367
368 if (oflag & O_TRUNC) {
369 assert(oflag & O_WRONLY || oflag & O_RDWR);
370 assert(!(oflag & O_APPEND));
371
372 // _vfs_resize
373 (void) ftruncate(ret, 0);
374 }
375
376 return ret;
377}
378
379int close(int fildes)
380{
381 sysarg_t rc;
382
383 async_exch_t *exch = vfs_exchange_begin();
384 rc = async_req_1_0(exch, VFS_IN_CLOSE, fildes);
385 vfs_exchange_end(exch);
386
387 return (int) rc;
388}
389
390// TODO: Versioning for read.
391
392ssize_t read(int fildes, void *buf, size_t nbyte)
393{
394 sysarg_t rc;
395 ipc_call_t answer;
396 aid_t req;
397
398 async_exch_t *exch = vfs_exchange_begin();
399
400 req = async_send_1(exch, VFS_IN_READ, fildes, &answer);
401 rc = async_data_read_start(exch, (void *)buf, nbyte);
402 if (rc != EOK) {
403 vfs_exchange_end(exch);
404
405 sysarg_t rc_orig;
406 async_wait_for(req, &rc_orig);
407
408 if (rc_orig == EOK)
409 return (ssize_t) rc;
410 else
411 return (ssize_t) rc_orig;
412 }
413 vfs_exchange_end(exch);
414 async_wait_for(req, &rc);
415 if (rc == EOK)
416 return (ssize_t) IPC_GET_ARG1(answer);
417 else
418 return rc;
419}
420
421ssize_t write(int fildes, const void *buf, size_t nbyte)
422{
423 sysarg_t rc;
424 ipc_call_t answer;
425 aid_t req;
426
427 async_exch_t *exch = vfs_exchange_begin();
428
429 req = async_send_1(exch, VFS_IN_WRITE, fildes, &answer);
430 rc = async_data_write_start(exch, (void *)buf, nbyte);
431 if (rc != EOK) {
432 vfs_exchange_end(exch);
433
434 sysarg_t rc_orig;
435 async_wait_for(req, &rc_orig);
436
437 if (rc_orig == EOK)
438 return (ssize_t) rc;
439 else
440 return (ssize_t) rc_orig;
441 }
442 vfs_exchange_end(exch);
443 async_wait_for(req, &rc);
444 if (rc == EOK)
445 return (ssize_t) IPC_GET_ARG1(answer);
446 else
447 return -1;
448}
449
450/** Read entire buffer.
451 *
452 * In face of short reads this function continues reading until either
453 * the entire buffer is read or no more data is available (at end of file).
454 *
455 * @param fildes File descriptor
456 * @param buf Buffer, @a nbytes bytes long
457 * @param nbytes Number of bytes to read
458 *
459 * @return On success, positive number of bytes read.
460 * On failure, negative error code from read().
461 */
462ssize_t read_all(int fildes, void *buf, size_t nbyte)
463{
464 ssize_t cnt = 0;
465 size_t nread = 0;
466 uint8_t *bp = (uint8_t *) buf;
467
468 do {
469 bp += cnt;
470 nread += cnt;
471 cnt = read(fildes, bp, nbyte - nread);
472 } while (cnt > 0 && (nbyte - nread - cnt) > 0);
473
474 if (cnt < 0)
475 return cnt;
476
477 return nread + cnt;
478}
479
480/** Write entire buffer.
481 *
482 * This function fails if it cannot write exactly @a len bytes to the file.
483 *
484 * @param fildes File descriptor
485 * @param buf Data, @a nbytes bytes long
486 * @param nbytes Number of bytes to write
487 *
488 * @return EOK on error, return value from write() if writing
489 * failed.
490 */
491ssize_t write_all(int fildes, const void *buf, size_t nbyte)
492{
493 ssize_t cnt = 0;
494 ssize_t nwritten = 0;
495 const uint8_t *bp = (uint8_t *) buf;
496
497 do {
498 bp += cnt;
499 nwritten += cnt;
500 cnt = write(fildes, bp, nbyte - nwritten);
501 } while (cnt > 0 && ((ssize_t )nbyte - nwritten - cnt) > 0);
502
503 if (cnt < 0)
504 return cnt;
505
506 if ((ssize_t)nbyte - nwritten - cnt > 0)
507 return EIO;
508
509 return nbyte;
510}
511
512int fsync(int fildes)
513{
514 async_exch_t *exch = vfs_exchange_begin();
515 sysarg_t rc = async_req_1_0(exch, VFS_IN_SYNC, fildes);
516 vfs_exchange_end(exch);
517
518 return (int) rc;
519}
520
521off64_t lseek(int fildes, off64_t offset, int whence)
522{
523 async_exch_t *exch = vfs_exchange_begin();
524
525 sysarg_t newoff_lo;
526 sysarg_t newoff_hi;
527 sysarg_t rc = async_req_4_2(exch, VFS_IN_SEEK, fildes,
528 LOWER32(offset), UPPER32(offset), whence,
529 &newoff_lo, &newoff_hi);
530
531 vfs_exchange_end(exch);
532
533 if (rc != EOK)
534 return (off64_t) -1;
535
536 return (off64_t) MERGE_LOUP32(newoff_lo, newoff_hi);
537}
538
539int ftruncate(int fildes, aoff64_t length)
540{
541 sysarg_t rc;
542
543 async_exch_t *exch = vfs_exchange_begin();
544 rc = async_req_3_0(exch, VFS_IN_TRUNCATE, fildes,
545 LOWER32(length), UPPER32(length));
546 vfs_exchange_end(exch);
547
548 return (int) rc;
549}
550
551int fstat(int fildes, struct stat *stat)
552{
553 sysarg_t rc;
554 aid_t req;
555
556 async_exch_t *exch = vfs_exchange_begin();
557
558 req = async_send_1(exch, VFS_IN_FSTAT, fildes, NULL);
559 rc = async_data_read_start(exch, (void *) stat, sizeof(struct stat));
560 if (rc != EOK) {
561 vfs_exchange_end(exch);
562
563 sysarg_t rc_orig;
564 async_wait_for(req, &rc_orig);
565
566 if (rc_orig == EOK)
567 return (ssize_t) rc;
568 else
569 return (ssize_t) rc_orig;
570 }
571 vfs_exchange_end(exch);
572 async_wait_for(req, &rc);
573
574 return rc;
575}
576
577int stat(const char *path, struct stat *stat)
578{
579 size_t pa_size;
580 char *pa = absolutize(path, &pa_size);
581 if (!pa) {
582 return ENOMEM;
583 }
584
585 int fd = _vfs_walk(-1, pa, 0);
586 if (fd < 0) {
587 return fd;
588 }
589
590 int rc = fstat(fd, stat);
591 close(fd);
592 return rc;
593}
594
595DIR *opendir(const char *dirname)
596{
597 DIR *dirp = malloc(sizeof(DIR));
598 if (!dirp) {
599 errno = ENOMEM;
600 return NULL;
601 }
602
603 size_t abs_size;
604 char *abs = absolutize(dirname, &abs_size);
605 if (!abs) {
606 free(dirp);
607 errno = ENOMEM;
608 return NULL;
609 }
610
611 int ret = _vfs_walk(-1, abs, WALK_DIRECTORY);
612 free(abs);
613
614 if (ret < 0) {
615 free(dirp);
616 errno = ret;
617 return NULL;
618 }
619
620 int rc = _vfs_open(ret, MODE_READ);
621 if (rc < 0) {
622 free(dirp);
623 close(ret);
624 errno = rc;
625 return NULL;
626 }
627
628 dirp->fd = ret;
629 return dirp;
630}
631
632struct dirent *readdir(DIR *dirp)
633{
634 ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
635 if (len <= 0)
636 return NULL;
637 return &dirp->res;
638}
639
640void rewinddir(DIR *dirp)
641{
642 (void) lseek(dirp->fd, 0, SEEK_SET);
643}
644
645int closedir(DIR *dirp)
646{
647 (void) close(dirp->fd);
648 free(dirp);
649 return 0;
650}
651
652int mkdir(const char *path, mode_t mode)
653{
654 size_t pa_size;
655 char *pa = absolutize(path, &pa_size);
656 if (!pa) {
657 return ENOMEM;
658 }
659
660 int ret = _vfs_walk(-1, pa, WALK_MUST_CREATE | WALK_DIRECTORY);
661 if (ret < 0) {
662 return ret;
663 }
664
665 close(ret);
666 return EOK;
667}
668
669static int _vfs_unlink2(int parent, const char *path, int expect, int wflag)
670{
671 sysarg_t rc;
672 aid_t req;
673
674 async_exch_t *exch = vfs_exchange_begin();
675
676 req = async_send_3(exch, VFS_IN_UNLINK2, parent, expect, wflag, NULL);
677 rc = async_data_write_start(exch, path, str_size(path));
678
679 vfs_exchange_end(exch);
680
681 sysarg_t rc_orig;
682 async_wait_for(req, &rc_orig);
683
684 if (rc_orig != EOK) {
685 return (int) rc_orig;
686 }
687 return rc;
688}
689
690int unlink(const char *path)
691{
692 size_t pa_size;
693 char *pa = absolutize(path, &pa_size);
694 if (!pa) {
695 return ENOMEM;
696 }
697
698 return _vfs_unlink2(-1, pa, -1, 0);
699}
700
701int rmdir(const char *path)
702{
703 size_t pa_size;
704 char *pa = absolutize(path, &pa_size);
705 if (!pa) {
706 return ENOMEM;
707 }
708
709 return _vfs_unlink2(-1, pa, -1, WALK_DIRECTORY);
710}
711
712int rename(const char *old, const char *new)
713{
714 sysarg_t rc;
715 sysarg_t rc_orig;
716 aid_t req;
717
718 size_t olda_size;
719 char *olda = absolutize(old, &olda_size);
720 if (!olda)
721 return ENOMEM;
722
723 size_t newa_size;
724 char *newa = absolutize(new, &newa_size);
725 if (!newa) {
726 free(olda);
727 return ENOMEM;
728 }
729
730 async_exch_t *exch = vfs_exchange_begin();
731
732 req = async_send_1(exch, VFS_IN_RENAME, -1, NULL);
733 rc = async_data_write_start(exch, olda, olda_size);
734 if (rc != EOK) {
735 vfs_exchange_end(exch);
736 free(olda);
737 free(newa);
738 async_wait_for(req, &rc_orig);
739 if (rc_orig == EOK)
740 return (int) rc;
741 else
742 return (int) rc_orig;
743 }
744 rc = async_data_write_start(exch, newa, newa_size);
745 if (rc != EOK) {
746 vfs_exchange_end(exch);
747 free(olda);
748 free(newa);
749 async_wait_for(req, &rc_orig);
750 if (rc_orig == EOK)
751 return (int) rc;
752 else
753 return (int) rc_orig;
754 }
755 vfs_exchange_end(exch);
756 free(olda);
757 free(newa);
758 async_wait_for(req, &rc);
759 return rc;
760}
761
762int chdir(const char *path)
763{
764 size_t abs_size;
765 char *abs = absolutize(path, &abs_size);
766 if (!abs)
767 return ENOMEM;
768
769 int fd = _vfs_walk(-1, abs, WALK_DIRECTORY);
770 if (fd < 0) {
771 free(abs);
772 return ENOENT;
773 }
774
775 fibril_mutex_lock(&cwd_mutex);
776
777 if (cwd_fd >= 0)
778 close(cwd_fd);
779
780
781 if (cwd_path)
782 free(cwd_path);
783
784 cwd_fd = fd;
785 cwd_path = abs;
786 cwd_size = abs_size;
787
788 fibril_mutex_unlock(&cwd_mutex);
789 return EOK;
790}
791
792char *getcwd(char *buf, size_t size)
793{
794 if (size == 0)
795 return NULL;
796
797 fibril_mutex_lock(&cwd_mutex);
798
799 if ((cwd_size == 0) || (size < cwd_size + 1)) {
800 fibril_mutex_unlock(&cwd_mutex);
801 return NULL;
802 }
803
804 str_cpy(buf, size, cwd_path);
805 fibril_mutex_unlock(&cwd_mutex);
806
807 return buf;
808}
809
810async_sess_t *fd_session(exch_mgmt_t mgmt, int fildes)
811{
812 struct stat stat;
813 int rc = fstat(fildes, &stat);
814 if (rc != 0) {
815 errno = rc;
816 return NULL;
817 }
818
819 if (!stat.service) {
820 errno = ENOENT;
821 return NULL;
822 }
823
824 return loc_service_connect(mgmt, stat.service, 0);
825}
826
827int dup2(int oldfd, int newfd)
828{
829 async_exch_t *exch = vfs_exchange_begin();
830
831 sysarg_t ret;
832 sysarg_t rc = async_req_2_1(exch, VFS_IN_DUP, oldfd, newfd, &ret);
833
834 vfs_exchange_end(exch);
835
836 if (rc == EOK)
837 return (int) ret;
838
839 return (int) rc;
840}
841
842int fd_wait(void)
843{
844 async_exch_t *exch = vfs_exchange_begin();
845
846 sysarg_t ret;
847 sysarg_t rc = async_req_0_1(exch, VFS_IN_WAIT_HANDLE, &ret);
848
849 vfs_exchange_end(exch);
850
851 if (rc == EOK)
852 return (int) ret;
853
854 return (int) rc;
855}
856
857int get_mtab_list(list_t *mtab_list)
858{
859 sysarg_t rc;
860 aid_t req;
861 size_t i;
862 sysarg_t num_mounted_fs;
863
864 async_exch_t *exch = vfs_exchange_begin();
865
866 req = async_send_0(exch, VFS_IN_MTAB_GET, NULL);
867
868 /* Ask VFS how many filesystems are mounted */
869 rc = async_req_0_1(exch, VFS_IN_PING, &num_mounted_fs);
870 if (rc != EOK)
871 goto exit;
872
873 for (i = 0; i < num_mounted_fs; ++i) {
874 mtab_ent_t *mtab_ent;
875
876 mtab_ent = malloc(sizeof(mtab_ent_t));
877 if (!mtab_ent) {
878 rc = ENOMEM;
879 goto exit;
880 }
881
882 memset(mtab_ent, 0, sizeof(mtab_ent_t));
883
884 rc = async_data_read_start(exch, (void *) mtab_ent->mp,
885 MAX_PATH_LEN);
886 if (rc != EOK)
887 goto exit;
888
889 rc = async_data_read_start(exch, (void *) mtab_ent->opts,
890 MAX_MNTOPTS_LEN);
891 if (rc != EOK)
892 goto exit;
893
894 rc = async_data_read_start(exch, (void *) mtab_ent->fs_name,
895 FS_NAME_MAXLEN);
896 if (rc != EOK)
897 goto exit;
898
899 sysarg_t p[2];
900
901 rc = async_req_0_2(exch, VFS_IN_PING, &p[0], &p[1]);
902 if (rc != EOK)
903 goto exit;
904
905 mtab_ent->instance = p[0];
906 mtab_ent->service_id = p[1];
907
908 link_initialize(&mtab_ent->link);
909 list_append(&mtab_ent->link, mtab_list);
910 }
911
912exit:
913 async_wait_for(req, &rc);
914 vfs_exchange_end(exch);
915 return rc;
916}
917
918/** @}
919 */
Note: See TracBrowser for help on using the repository browser.