5
5
#include <time.h>
6
6
#include <stdint.h>
7
7
#include <stdlib.h>
8
+ #include <errno.h>
8
9
#include <string.h>
9
10
#include <sys/resource.h>
10
11
#include <bpf/libbpf.h>
14
15
#include <inttypes.h>
15
16
#include <linux/fs.h>
16
17
#include <errno.h>
18
+ #include <fcntl.h> // 包含文件打开标志宏
17
19
#include <argp.h>
18
20
#include "fs/fs_watcher/open.skel.h"
19
21
#include "fs/fs_watcher/read.skel.h"
@@ -105,15 +107,13 @@ static struct env{
105
107
bool disk_io_visit ;
106
108
bool block_rq_issue ;
107
109
bool CacheTrack ;
108
- pid_t pid ;
109
110
}env = {
110
111
.open = false,
111
112
.read = false,
112
113
.write = false,
113
114
.disk_io_visit = false,
114
115
.block_rq_issue = false,
115
116
.CacheTrack = false,
116
- .pid = -1 ,
117
117
};
118
118
119
119
static const struct argp_option opts [] = {
@@ -123,7 +123,6 @@ static const struct argp_option opts[] = {
123
123
{"disk_io_visit" , 'd' , 0 , 0 , "Print disk I/O visit report" },
124
124
{"block_rq_issue" , 'b' , 0 , 0 , "Print block I/O request submission events. Reports when block I/O requests are submitted to device drivers." },
125
125
{"CacheTrack" , 't' , 0 ,0 , "WriteBack dirty lagency and other information" },
126
- {"pid" , 'p' , "PID" , 0 , "Specify pid number when report weite. Only support for write report now" },
127
126
{0 } // 结束标记,用于指示选项列表的结束
128
127
};
129
128
@@ -142,19 +141,7 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) {
142
141
env .block_rq_issue = true;break ;
143
142
case 't' :
144
143
env .CacheTrack = true;break ;
145
- case 'p' :
146
- if (arg ) {
147
- env .pid = atoi (arg );
148
- if (env .pid <= 0 ) {
149
- fprintf (stderr , "Invalid PID value: %s\n" , arg );
150
- argp_usage (state );
151
- }
152
- } else {
153
- fprintf (stderr , "-p option requires an argument\n" );
154
- argp_usage (state );
155
- }
156
- break ;
157
- default :
144
+ default :
158
145
return ARGP_ERR_UNKNOWN ;
159
146
}
160
147
return 0 ;
@@ -244,38 +231,65 @@ int main(int argc,char **argv){
244
231
}
245
232
}
246
233
234
+ const char * flags_to_str (int flags ) {
235
+ static char str [256 ];
236
+ str [0 ] = '\0' ; // 清空字符串
237
+
238
+ if (flags & O_RDONLY ) strcat (str , "O_RDONLY " );
239
+ if (flags & O_WRONLY ) strcat (str , "O_WRONLY " );
240
+ if (flags & O_RDWR ) strcat (str , "O_RDWR " );
241
+ if (flags & O_CREAT ) strcat (str , "O_CREAT " );
242
+ if (flags & O_EXCL ) strcat (str , "O_EXCL " );
243
+ if (flags & O_TRUNC ) strcat (str , "O_TRUNC " );
244
+ if (flags & O_APPEND ) strcat (str , "O_APPEND " );
245
+ if (flags & O_NOFOLLOW ) strcat (str , "O_NOFOLLOW " );
246
+ if (flags & O_CLOEXEC ) strcat (str , "O_CLOEXEC " );
247
+ if (flags & O_NONBLOCK ) strcat (str , "O_NONBLOCK " );
248
+ if (flags & O_SYNC ) strcat (str , "O_SYNC " );
249
+ if (flags & O_DSYNC ) strcat (str , "O_DSYNC " );
250
+ if (flags & O_RSYNC ) strcat (str , "O_RSYNC " );
251
+ if (flags & O_DIRECTORY ) strcat (str , "O_DIRECTORY " );
252
+
253
+ // 条件编译部分:如果系统定义了 O_NOATIME 和 O_PATH
254
+ #ifdef O_NOATIME
255
+ if (flags & O_NOATIME ) strcat (str , "O_NOATIME " );
256
+ #endif
257
+
258
+ #ifdef O_PATH
259
+ if (flags & O_PATH ) strcat (str , "O_PATH " );
260
+ #endif
261
+
262
+ // 如果没有匹配到标志,返回 "Unknown"
263
+ if (str [0 ] == '\0' ) {
264
+ return "Unknown" ;
265
+ }
266
+
267
+ return str ;
268
+ }
269
+
247
270
static int handle_event_open (void * ctx , void * data , size_t data_sz )
248
271
{
249
- struct event_open * e = (struct event_open * )data ;
250
- char * filename = strrchr (e -> path_name_ , '/' );
251
- ++ filename ;
252
-
253
- char fd_path [path_size ];
254
- char actual_path [path_size ];
255
- char comm [TASK_COMM_LEN ];
256
- int i = 0 ;
257
- int map_fd = * (int * )ctx ;//传递map得文件描述符
258
-
259
-
260
- for (; i < e -> n_ ; ++ i ) {
261
- snprintf (fd_path , sizeof (fd_path ), "/proc/%d/fd/%d" , e -> pid_ ,
262
- i );
263
- ssize_t len =
264
- readlink (fd_path , actual_path , sizeof (actual_path ) - 1 );
265
- if (len != -1 ) {
266
- actual_path [len ] = '\0' ;
267
- int result = strcmp (e -> path_name_ , actual_path );
268
- if (result == 0 ) {
269
- if (bpf_map_lookup_elem (map_fd ,& e -> pid_ ,& comm )== 0 ){
270
- printf ("%-60s %-8d %-8d %-8s\n" ,
271
- e -> path_name_ , i ,e -> pid_ ,comm );
272
- }else {
273
- fprintf (stderr , "Failed to lookup value for key %d\n" , e -> pid_ );
274
- }
275
-
276
- }
277
- }
278
- }
272
+ const struct event_open * e = data ;
273
+ struct tm * tm ;
274
+ char ts [32 ];
275
+ time_t t ;
276
+
277
+ time (& t );
278
+ tm = localtime (& t );
279
+ strftime (ts , sizeof (ts ), "%H:%M:%S" , tm );
280
+ const char * ret_str ;
281
+ // 如果返回值是负数,则是错误码,使用 strerror
282
+ if (e -> ret < 0 ) {
283
+ ret_str = strerror (- e -> ret ); // 负数表示错误码
284
+ } else {
285
+ // 正数表示文件描述符,直接打印文件描述符
286
+ ret_str = "Success" ; // 如果是文件描述符,表示成功
287
+ }
288
+
289
+ const char * flags_str = flags_to_str (e -> flags );
290
+
291
+ printf ("%-8s %-8d %-8d %-100s %-8s %-8d %-8s %-8s\n" ,
292
+ ts , e -> dfd , e -> pid ,e -> filename , flags_str , e -> fd , ret_str , e -> is_created ? "true" : "false" );
279
293
return 0 ;
280
294
}
281
295
@@ -352,7 +366,7 @@ static int process_open(struct open_bpf *skel_open){
352
366
353
367
LOAD_AND_ATTACH_SKELETON_MAP (skel_open ,open );
354
368
355
- printf ("%-60s %-8s %-8s %-8s \n" ,"filenamename " ,"fd" ,"pid" , "comm " );
369
+ printf ("%-8s %-8s %-8s %-100s %-8s %-8s %-8s %-8s \n" , "TIME " ,"dfd" , "PID" , "filename" , "flags" , " fd" , "ret" , "is_created " );
356
370
POLL_RING_BUFFER (rb , 1000 , err );
357
371
358
372
open_cleanup :
@@ -381,22 +395,9 @@ static int process_read(struct read_bpf *skel_read){
381
395
static int process_write (struct write_bpf * skel_write ){
382
396
int err ;
383
397
struct ring_buffer * rb ;
384
- int arg_index = 0 ;
385
-
386
- struct dist_args d_args = {-1 };
387
-
388
398
389
399
LOAD_AND_ATTACH_SKELETON (skel_write ,write );
390
400
391
- d_args .pid = env .pid ;
392
- struct bpf_map * arg_map = bpf_object__find_map_by_name ((const struct bpf_object * )* (skel_write -> skeleton -> obj ), "args_map" );
393
- err = bpf_map__update_elem (arg_map , & arg_index , sizeof (arg_index ), & d_args , sizeof (d_args ), BPF_ANY );
394
-
395
- if (err < 0 ) {
396
- fprintf (stderr , "ERROR: failed to update args map\n" );
397
- goto write_cleanup ;
398
- }
399
-
400
401
printf ("%-8s %-8s %-8s %-8s %-8s\n" ,"ds" ,"inode_number" ,"pid" ,"real_count" ,"count" );
401
402
POLL_RING_BUFFER (rb , 1000 , err );
402
403
0 commit comments