文件系统-4-struct file_operations 结构
一、各回调简介
1. flush和close回调的关系
实测,dev设备节点每次 close(fd) 都会调用一次 fops->flush() 回调,当 file->f_count 减为0后调用 fops->close() 回调; 而proc文件节点则不会调用 fops->flush() 回调,只会在 file->f_count 减为0后调用 fops->close() 回调。
实验代码:
#include <signal.h> #include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/prctl.h> #include <fcntl.h> #include <stdlib.h> #include <errno.h> #include <string.h> int proc_file_node_test(int argc, char *argv[]) { int fd, fd1, fd2, fd3, ret; char *str_val = "4"; if (argc < 2 || atoi(argv[1]) != 1) return -1; if (argc == 3) str_val = argv[2]; fd = open("/proc/my_debug", O_RDWR); if (fd < 0) { fprintf(stderr,"%s: open failed: %d:%s\n", __func__, fd, strerror(errno)); return -1; } fd1 = dup(fd); fd2 = dup(fd); fd3 = dup(fd); ret = write(fd, str_val, strlen(str_val)); if (ret < 0) { fprintf(stderr,"%s: write failed: %d:%s\n", __func__, ret, strerror(errno)); return -1; } close(fd3); close(fd2); close(fd1); close(fd); return 0; } int dev_node_test(int argc, char *argv[]) { int fd, fd1, fd2, fd3; char *str_val = "4"; if (argc < 2 || atoi(argv[1]) != 2) return -1; prctl(PR_SET_NAME, "ham_dev_test", NULL, NULL, NULL); if (argc == 3) { str_val = argv[2]; fprintf(stderr,"%s: str_val=%s\n", __func__, str_val); } fd = open("/dev/binder", O_RDWR); if (fd < 0) { fprintf(stderr,"%s: open failed: %d:%s\n", __func__, fd, strerror(errno)); return -1; } fd1 = dup(fd); fd2 = dup(fd); fd3 = dup(fd); close(fd3); close(fd2); close(fd1); close(fd); return 0; } int main(int argc, char *argv[]) { proc_file_node_test(argc, argv); dev_node_test(argc, argv); return 0; }
测试结果:
8155:/data/local/tmp # ./my_dup_test 1 8155:/data/local/tmp # dmesg -c | grep HAM [ 155.472931] (6)[7323:my_dup_test]HAM: proc_my_debug_open called [ 155.479231] (6)[7323:my_dup_test]HAM: proc_my_debug_write: set debug=4 [ 155.507279] (6)[7323:my_dup_test]HAM: proc_my_debug_release called 8155:/data/local/tmp # ./my_dup_test 2 8155:/data/local/tmp # dmesg -c | grep HAM [ 90.478820] (4)[6984:ham_dev_test]HAM: binder_open called. [ 90.478932] (6)[6984:ham_dev_test]HAM: binder_flush called. [ 90.485913] (6)[6984:ham_dev_test]HAM: binder_flush called. [ 90.491850] (6)[6984:ham_dev_test]HAM: binder_flush called. [ 90.497757] (6)[6984:ham_dev_test]HAM: binder_flush called. [ 90.503616] (3)[5269:OtaUeIoThd]HAM: binder_release called.
posted on 2025-05-26 21:28 Hello-World3 阅读(28) 评论(0) 收藏 举报