1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup rtems_fstab File System Mount Support |
---|
7 | * |
---|
8 | * @brief File System Mount Functions |
---|
9 | * |
---|
10 | * This file contains the fsmount functions. These functions |
---|
11 | * are used to mount a list of filesystems (and create their mount |
---|
12 | * points before). |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * Copyright (c) 2003 IMD Ingenieurbuero fuer Microcomputertechnik |
---|
17 | * All rights reserved. |
---|
18 | * |
---|
19 | * Redistribution and use in source and binary forms, with or without |
---|
20 | * modification, are permitted provided that the following conditions |
---|
21 | * are met: |
---|
22 | * 1. Redistributions of source code must retain the above copyright |
---|
23 | * notice, this list of conditions and the following disclaimer. |
---|
24 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
25 | * notice, this list of conditions and the following disclaimer in the |
---|
26 | * documentation and/or other materials provided with the distribution. |
---|
27 | * |
---|
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
32 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
33 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
34 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
35 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
36 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
37 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
38 | * POSSIBILITY OF SUCH DAMAGE. |
---|
39 | */ |
---|
40 | |
---|
41 | #ifndef HAVE_CONFIG_H |
---|
42 | #include "config.h" |
---|
43 | #endif |
---|
44 | |
---|
45 | #include <rtems.h> |
---|
46 | #include <rtems/fsmount.h> |
---|
47 | #include <rtems/libio.h> |
---|
48 | #include <stdio.h> |
---|
49 | #include <stdlib.h> |
---|
50 | #include <string.h> |
---|
51 | #include <errno.h> |
---|
52 | |
---|
53 | /*=========================================================================*\ |
---|
54 | | Function: | |
---|
55 | \*-------------------------------------------------------------------------*/ |
---|
56 | int rtems_fsmount |
---|
57 | ( |
---|
58 | /*-------------------------------------------------------------------------*\ |
---|
59 | | Purpose: | |
---|
60 | | This function will create the mount points listed and mount the file | |
---|
61 | | systems listed in the calling parameters | |
---|
62 | +---------------------------------------------------------------------------+ |
---|
63 | | Input Parameters: | |
---|
64 | \*-------------------------------------------------------------------------*/ |
---|
65 | const rtems_fstab_entry *fstab_ptr, |
---|
66 | size_t fstab_count, |
---|
67 | size_t *fail_idx |
---|
68 | ) |
---|
69 | /*-------------------------------------------------------------------------*\ |
---|
70 | | Return Value: | |
---|
71 | | 0, if success, -1 and errno if failed | |
---|
72 | \*=========================================================================*/ |
---|
73 | { |
---|
74 | int rc = 0; |
---|
75 | int tmp_rc; |
---|
76 | size_t fstab_idx = 0; |
---|
77 | bool terminate = false; |
---|
78 | |
---|
79 | /* |
---|
80 | * scan through all fstab entries; |
---|
81 | */ |
---|
82 | while (!terminate && |
---|
83 | (fstab_idx < fstab_count)) { |
---|
84 | tmp_rc = 0; |
---|
85 | /* |
---|
86 | * create mount point |
---|
87 | */ |
---|
88 | if (tmp_rc == 0) { |
---|
89 | tmp_rc = rtems_mkdir(fstab_ptr->target, S_IRWXU | S_IRWXG | S_IRWXO); |
---|
90 | if (tmp_rc != 0) { |
---|
91 | if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNTPNT_CRTERR)) { |
---|
92 | fprintf(stdout,"fsmount: creation of mount point \"%s\" failed: %s\n", |
---|
93 | fstab_ptr->target, |
---|
94 | strerror(errno)); |
---|
95 | } |
---|
96 | if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNTPNT_CRTERR)) { |
---|
97 | terminate = true; |
---|
98 | rc = tmp_rc; |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | /* |
---|
103 | * mount device to given mount point |
---|
104 | */ |
---|
105 | if (tmp_rc == 0) { |
---|
106 | tmp_rc = mount(fstab_ptr->source, |
---|
107 | fstab_ptr->target, |
---|
108 | fstab_ptr->type, |
---|
109 | fstab_ptr->options, |
---|
110 | NULL); |
---|
111 | if (tmp_rc != 0) { |
---|
112 | if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNT_FAILED)) { |
---|
113 | fprintf(stdout,"fsmount: mounting of \"%s\" to" |
---|
114 | " \"%s\" failed: %s\n", |
---|
115 | fstab_ptr->source, |
---|
116 | fstab_ptr->target, |
---|
117 | strerror(errno)); |
---|
118 | } |
---|
119 | if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNT_FAILED)) { |
---|
120 | terminate = true; |
---|
121 | rc = tmp_rc; |
---|
122 | } |
---|
123 | } |
---|
124 | else { |
---|
125 | if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNT_OK)) { |
---|
126 | fprintf(stdout,"fsmount: mounting of \"%s\" to" |
---|
127 | " \"%s\" succeeded\n", |
---|
128 | fstab_ptr->source, |
---|
129 | fstab_ptr->target); |
---|
130 | } |
---|
131 | if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNT_OK)) { |
---|
132 | terminate = true; |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | /* |
---|
137 | * proceed to next entry |
---|
138 | */ |
---|
139 | if (!terminate) { |
---|
140 | fstab_ptr++; |
---|
141 | fstab_idx++; |
---|
142 | } |
---|
143 | } |
---|
144 | if (fail_idx != NULL) { |
---|
145 | *fail_idx = fstab_idx; |
---|
146 | } |
---|
147 | return rc; |
---|
148 | } |
---|