1 /*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "proc_file.h"
32
33 #include <sys/statfs.h>
34 #include <stdlib.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37
38 #include "fs/dirent_fs.h"
39 #include "fs/mount.h"
40 #include "fs/fs.h"
41 #include "los_tables.h"
42 #include "internal.h"
43
44 #ifdef LOSCFG_FS_PROC
45 static struct VnodeOps g_procfsVops;
46 static struct file_operations_vfs g_procfsFops;
47
VnodeToEntry(struct Vnode * node)48 static struct ProcDirEntry *VnodeToEntry(struct Vnode *node)
49 {
50 return (struct ProcDirEntry *)(node->data);
51 }
52
EntryToVnode(struct ProcDirEntry * entry)53 static struct Vnode *EntryToVnode(struct ProcDirEntry *entry)
54 {
55 struct Vnode *node = NULL;
56
57 (void)VnodeAlloc(&g_procfsVops, &node);
58 node->fop = &g_procfsFops;
59 node->data = entry;
60 node->type = entry->type;
61 node->uid = entry->uid;
62 node->gid = entry->gid;
63 node->mode = entry->mode;
64 return node;
65 }
66
EntryMatch(const char * name,int len,const struct ProcDirEntry * pn)67 static int EntryMatch(const char *name, int len, const struct ProcDirEntry *pn)
68 {
69 if (len != pn->nameLen) {
70 return 0;
71 }
72 return !strncmp(name, pn->name, len);
73 }
74
VfsProcfsTruncate(struct Vnode * pVnode,off_t len)75 int VfsProcfsTruncate(struct Vnode *pVnode, off_t len)
76 {
77 return 0;
78 }
79
VfsProcfsCreate(struct Vnode * parent,const char * name,int mode,struct Vnode ** vnode)80 int VfsProcfsCreate(struct Vnode* parent, const char *name, int mode, struct Vnode **vnode)
81 {
82 int ret;
83 struct Vnode *vp = NULL;
84 struct ProcDirEntry *curEntry = NULL;
85
86 struct ProcDirEntry *parentEntry = VnodeToEntry(parent);
87 if (parentEntry == NULL) {
88 return -ENODATA;
89 }
90
91 ret = VnodeAlloc(&g_procfsVops, &vp);
92 if (ret != 0) {
93 return -ENOMEM;
94 }
95
96 curEntry = ProcCreate(name, mode, parentEntry, NULL);
97 if (curEntry == NULL) {
98 VnodeFree(vp);
99 return -ENODATA;
100 }
101
102 vp->data = curEntry;
103 vp->type = curEntry->type;
104 if (vp->type == VNODE_TYPE_DIR) {
105 vp->mode = S_IFDIR | PROCFS_DEFAULT_MODE;
106 } else {
107 vp->mode = S_IFREG | PROCFS_DEFAULT_MODE;
108 }
109
110 vp->vop = parent->vop;
111 vp->fop = parent->fop;
112 vp->parent = parent;
113 vp->originMount = parent->originMount;
114
115 *vnode = vp;
116
117 return LOS_OK;
118 }
119
VfsProcfsRead(struct file * filep,char * buffer,size_t buflen)120 int VfsProcfsRead(struct file *filep, char *buffer, size_t buflen)
121 {
122 ssize_t size;
123 struct ProcDirEntry *entry = NULL;
124 if ((filep == NULL) || (filep->f_vnode == NULL) || (buffer == NULL)) {
125 return -EINVAL;
126 }
127
128 entry = VnodeToEntry(filep->f_vnode);
129 size = (ssize_t)ReadProcFile(entry, (void *)buffer, buflen);
130 filep->f_pos = entry->pf->fPos;
131
132 return size;
133 }
134
VfsProcfsWrite(struct file * filep,const char * buffer,size_t buflen)135 int VfsProcfsWrite(struct file *filep, const char *buffer, size_t buflen)
136 {
137 ssize_t size;
138 struct ProcDirEntry *entry = NULL;
139 if ((filep == NULL) || (filep->f_vnode == NULL) || (buffer == NULL)) {
140 return -EINVAL;
141 }
142
143 entry = VnodeToEntry(filep->f_vnode);
144 size = (ssize_t)WriteProcFile(entry, (void *)buffer, buflen);
145 filep->f_pos = entry->pf->fPos;
146
147 return size;
148 }
149
VfsProcfsLookup(struct Vnode * parent,const char * name,int len,struct Vnode ** vpp)150 int VfsProcfsLookup(struct Vnode *parent, const char *name, int len, struct Vnode **vpp)
151 {
152 if (parent == NULL || name == NULL || len <= 0 || vpp == NULL) {
153 return -EINVAL;
154 }
155 struct ProcDirEntry *entry = VnodeToEntry(parent);
156 if (entry == NULL) {
157 return -ENODATA;
158 }
159 entry = entry->subdir;
160 while (1) {
161 if (entry == NULL) {
162 return -ENOENT;
163 }
164 if (EntryMatch(name, len, entry)) {
165 break;
166 }
167 entry = entry->next;
168 }
169
170 *vpp = EntryToVnode(entry);
171 if ((*vpp) == NULL) {
172 return -ENOMEM;
173 }
174 (*vpp)->originMount = parent->originMount;
175 (*vpp)->parent = parent;
176 return LOS_OK;
177 }
178
VfsProcfsMount(struct Mount * mnt,struct Vnode * device,const void * data)179 int VfsProcfsMount(struct Mount *mnt, struct Vnode *device, const void *data)
180 {
181 struct Vnode *vp = NULL;
182 int ret;
183
184 spin_lock_init(&procfsLock);
185 procfsInit = true;
186
187 ret = VnodeAlloc(&g_procfsVops, &vp);
188 if (ret != 0) {
189 return -ENOMEM;
190 }
191
192 struct ProcDirEntry *root = GetProcRootEntry();
193 vp->data = root;
194 vp->originMount = mnt;
195 vp->fop = &g_procfsFops;
196 mnt->data = NULL;
197 mnt->vnodeCovered = vp;
198 vp->type = root->type;
199 if (vp->type == VNODE_TYPE_DIR) {
200 vp->mode = S_IFDIR | PROCFS_DEFAULT_MODE;
201 } else {
202 vp->mode = S_IFREG | PROCFS_DEFAULT_MODE;
203 }
204
205 return LOS_OK;
206 }
207
VfsProcfsUnmount(void * handle,struct Vnode ** blkdriver)208 int VfsProcfsUnmount(void *handle, struct Vnode **blkdriver)
209 {
210 (void)handle;
211 (void)blkdriver;
212 return -EPERM;
213 }
214
VfsProcfsStat(struct Vnode * node,struct stat * buf)215 int VfsProcfsStat(struct Vnode *node, struct stat *buf)
216 {
217 struct ProcDirEntry *entry = VnodeToEntry(node);
218
219 (void)memset_s(buf, sizeof(struct stat), 0, sizeof(struct stat));
220 buf->st_mode = entry->mode;
221
222 return LOS_OK;
223 }
224
VfsProcfsReaddir(struct Vnode * node,struct fs_dirent_s * dir)225 int VfsProcfsReaddir(struct Vnode *node, struct fs_dirent_s *dir)
226 {
227 int result;
228 char *buffer = NULL;
229 int buflen = NAME_MAX;
230 unsigned int min_size;
231 unsigned int dst_name_size;
232 struct ProcDirEntry *pde = NULL;
233 int i = 0;
234
235 if (dir == NULL) {
236 return -EINVAL;
237 }
238 if (node->type != VNODE_TYPE_DIR) {
239 return -ENOTDIR;
240 }
241 pde = VnodeToEntry(node);
242
243 while (i < dir->read_cnt) {
244 buffer = (char *)zalloc(sizeof(char) * NAME_MAX);
245 if (buffer == NULL) {
246 PRINT_ERR("malloc failed\n");
247 return -ENOMEM;
248 }
249
250 result = ReadProcFile(pde, (void *)buffer, buflen);
251 if (result != ENOERR) {
252 free(buffer);
253 break;
254 }
255 dst_name_size = sizeof(dir->fd_dir[i].d_name);
256 min_size = (dst_name_size < NAME_MAX) ? dst_name_size : NAME_MAX;
257 result = strncpy_s(dir->fd_dir[i].d_name, dst_name_size, buffer, min_size);
258 if (result != EOK) {
259 free(buffer);
260 return -ENAMETOOLONG;
261 }
262 dir->fd_dir[i].d_name[dst_name_size - 1] = '\0';
263 dir->fd_position++;
264 dir->fd_dir[i].d_off = dir->fd_position;
265 dir->fd_dir[i].d_reclen = (uint16_t)sizeof(struct dirent);
266
267 i++;
268 free(buffer);
269 }
270
271 return i;
272 }
273
VfsProcfsOpendir(struct Vnode * node,struct fs_dirent_s * dir)274 int VfsProcfsOpendir(struct Vnode *node, struct fs_dirent_s *dir)
275 {
276 struct ProcDirEntry *pde = VnodeToEntry(node);
277 if (pde == NULL) {
278 return -EINVAL;
279 }
280 pde->pdirCurrent = pde->subdir;
281 pde->pf->fPos = 0;
282
283 return LOS_OK;
284 }
285
VfsProcfsOpen(struct file * filep)286 int VfsProcfsOpen(struct file *filep)
287 {
288 if (filep == NULL) {
289 return -EINVAL;
290 }
291 struct Vnode *node = filep->f_vnode;
292 struct ProcDirEntry *pde = VnodeToEntry(node);
293 if (ProcOpen(pde->pf) != OK) {
294 return -ENOMEM;
295 }
296 if (S_ISREG(pde->mode) && (pde->procFileOps != NULL) && (pde->procFileOps->open != NULL)) {
297 (void)pde->procFileOps->open((struct Vnode *)pde, pde->pf);
298 }
299 if (S_ISDIR(pde->mode)) {
300 pde->pdirCurrent = pde->subdir;
301 pde->pf->fPos = 0;
302 }
303 filep->f_priv = (void *)pde;
304 return LOS_OK;
305 }
306
VfsProcfsClose(struct file * filep)307 int VfsProcfsClose(struct file *filep)
308 {
309 int result = 0;
310 if (filep == NULL) {
311 return -EINVAL;
312 }
313 struct Vnode *node = filep->f_vnode;
314 struct ProcDirEntry *pde = VnodeToEntry(node);
315 pde->pf->fPos = 0;
316 if ((pde->procFileOps != NULL) && (pde->procFileOps->release != NULL)) {
317 result = pde->procFileOps->release((struct Vnode *)pde, pde->pf);
318 }
319 LosBufRelease(pde->pf->sbuf);
320 pde->pf->sbuf = NULL;
321
322 return result;
323 }
324
VfsProcfsStatfs(struct Mount * mnt,struct statfs * buf)325 int VfsProcfsStatfs(struct Mount *mnt, struct statfs *buf)
326 {
327 (void)memset_s(buf, sizeof(struct statfs), 0, sizeof(struct statfs));
328 buf->f_type = PROCFS_MAGIC;
329
330 return LOS_OK;
331 }
332
VfsProcfsClosedir(struct Vnode * vp,struct fs_dirent_s * dir)333 int VfsProcfsClosedir(struct Vnode *vp, struct fs_dirent_s *dir)
334 {
335 return LOS_OK;
336 }
337
338 const struct MountOps procfs_operations = {
339 .Mount = VfsProcfsMount,
340 .Unmount = NULL,
341 .Statfs = VfsProcfsStatfs,
342 };
343
344 static struct VnodeOps g_procfsVops = {
345 .Lookup = VfsProcfsLookup,
346 .Getattr = VfsProcfsStat,
347 .Readdir = VfsProcfsReaddir,
348 .Opendir = VfsProcfsOpendir,
349 .Closedir = VfsProcfsClosedir,
350 .Truncate = VfsProcfsTruncate
351 };
352
353 static struct file_operations_vfs g_procfsFops = {
354 .read = VfsProcfsRead,
355 .write = VfsProcfsWrite,
356 .open = VfsProcfsOpen,
357 .close = VfsProcfsClose
358 };
359
360 FSMAP_ENTRY(procfs_fsmap, "procfs", procfs_operations, FALSE, FALSE);
361 #endif
362