1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
*/
#include "proc.h"
#include "procfs.h"
#include <rthw.h>
#include <rtdbg.h>
#include <fcntl.h>
#include <errno.h>
#include <dfs_dentry.h>
#include <dfs_mnt.h>
static void mnt_escape(const char *source, char *target, rt_size_t target_size)
{
rt_size_t offset = 0;
const char *cursor = source ? source : "";
if (target_size == 0)
{
return;
}
while (*cursor != '\0')
{
const char *escape = RT_NULL;
rt_size_t need = 1;
if (*cursor == ' ')
{
escape = "\\040";
need = 4;
}
else if (*cursor == '\t')
{
escape = "\\011";
need = 4;
}
else if (*cursor == '\n')
{
escape = "\\012";
need = 4;
}
else if (*cursor == '\\')
{
escape = "\\134";
need = 4;
}
if (offset + need >= target_size)
{
break;
}
if (escape)
{
target[offset++] = escape[0];
target[offset++] = escape[1];
target[offset++] = escape[2];
target[offset++] = escape[3];
}
else
{
target[offset++] = *cursor;
}
cursor++;
}
target[offset] = '\0';
}
const char *mnt_flag(int flag)
{
if (flag & MNT_RDONLY)
{
return "ro";
}
return "rw";
}
static struct dfs_mnt* mnt_show(struct dfs_mnt *mnt, void *parameter)
{
struct dfs_seq_file *seq = (struct dfs_seq_file *)parameter;
char source[DFS_PATH_MAX];
char target[DFS_PATH_MAX];
const char *source_name;
const char *filesystem_name;
if (mnt && mnt->fs_ops && mnt->fullpath)
{
filesystem_name = mnt->fs_ops->name ? mnt->fs_ops->name : "unknown";
source_name = filesystem_name;
if (mnt->dev_id && mnt->dev_id->parent.name[0] != '\0')
{
source_name = mnt->dev_id->parent.name;
}
mnt_escape(source_name, source, sizeof(source));
mnt_escape(mnt->fullpath, target, sizeof(target));
dfs_seq_printf(seq, "%s %s %s %s 0 0\n", source, target,
filesystem_name, mnt_flag(mnt->flags));
}
return RT_NULL;
}
static void *seq_start(struct dfs_seq_file *seq, off_t *index)
{
off_t i = *index; // seq->index
return NULL + (i == 0);
}
static void seq_stop(struct dfs_seq_file *seq, void *data)
{
}
static void *seq_next(struct dfs_seq_file *seq, void *data, off_t *index)
{
/* data: The return value of the start or next*/
off_t i = *index + 1; // seq->index
*index = i;
return NULL;
}
static int seq_show(struct dfs_seq_file *seq, void *data)
{
/* data: The return value of the start or next*/
dfs_mnt_foreach(mnt_show, seq);
return 0;
}
static const struct dfs_seq_ops seq_ops = {
.start = seq_start,
.stop = seq_stop,
.next = seq_next,
.show = seq_show,
};
int proc_mounts_init(void)
{
struct proc_dentry *dentry = proc_create_data("mounts", 0, NULL, NULL, NULL);
if (dentry)
{
dentry->seq_ops = &seq_ops;
}
proc_release(dentry);
return 0;
}
INIT_ENV_EXPORT(proc_mounts_init);
|