1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  */
9 
10 #include "proc.h"
11 #include "procfs.h"
12 
13 #include <rthw.h>
14 #include <rtdbg.h>
15 
16 #include <fcntl.h>
17 #include <errno.h>
18 
19 #include <dfs_dentry.h>
20 #include <dfs_mnt.h>
21 
22 
mnt_flag(int flag)23 const char *mnt_flag(int flag)
24 {
25     /*if (flag & MNT_READONLY)
26     {
27         return "ro";
28     }*/
29 
30     return "rw";
31 }
32 
mnt_show(struct dfs_mnt * mnt,void * parameter)33 static struct dfs_mnt* mnt_show(struct dfs_mnt *mnt, void *parameter)
34 {
35     struct dfs_seq_file *seq = (struct dfs_seq_file *)parameter;
36 
37     if (mnt)
38     {
39         if (mnt->dev_id)
40         {
41             dfs_seq_printf(seq, "%s %s %s %s 0 0\n", mnt->dev_id->parent.name, mnt->fullpath,
42                             mnt->fs_ops->name, mnt_flag(mnt->flags));
43         }
44         else
45         {
46             dfs_seq_printf(seq, "%s %s %s %s 0 0\n", mnt->fs_ops->name, mnt->fullpath,
47                             mnt->fs_ops->name, mnt_flag(mnt->flags));
48         }
49     }
50 
51     return RT_NULL;
52 }
53 
seq_start(struct dfs_seq_file * seq,off_t * index)54 static void *seq_start(struct dfs_seq_file *seq, off_t *index)
55 {
56     off_t i = *index; // seq->index
57 
58     return NULL + (i == 0);
59 }
60 
seq_stop(struct dfs_seq_file * seq,void * data)61 static void seq_stop(struct dfs_seq_file *seq, void *data)
62 {
63 }
64 
seq_next(struct dfs_seq_file * seq,void * data,off_t * index)65 static void *seq_next(struct dfs_seq_file *seq, void *data, off_t *index)
66 {
67     /* data: The return value of the start or next*/
68     off_t i = *index + 1; // seq->index
69 
70     *index = i;
71 
72     return NULL;
73 }
74 
seq_show(struct dfs_seq_file * seq,void * data)75 static int seq_show(struct dfs_seq_file *seq, void *data)
76 {
77     /* data: The return value of the start or next*/
78     dfs_mnt_foreach(mnt_show, seq);
79 
80     return 0;
81 }
82 
83 static const struct dfs_seq_ops seq_ops = {
84     .start  = seq_start,
85     .stop   = seq_stop,
86     .next   = seq_next,
87     .show   = seq_show,
88 };
89 
proc_mounts_init(void)90 int proc_mounts_init(void)
91 {
92     struct proc_dentry *dentry = proc_create_data("mounts", 0, NULL, NULL, NULL);
93     if (dentry)
94     {
95         dentry->seq_ops = &seq_ops;
96     }
97     proc_release(dentry);
98 
99     return 0;
100 }
101 INIT_ENV_EXPORT(proc_mounts_init);
102