当前位置: 首页 > news >正文

15_sysfsLinux内核模块

01_basicLinux内核模块_the kernel was built by-CSDN博客文章浏览阅读989次,点赞3次,收藏3次。环境ID=ubuntuMakefilemodules:clean:basic.creturn 0;运行效果。_the kernel was built by https://blog.csdn.net/m0_37132481/article/details/136157384

环境

root@auto:/media/sf_D_DRIVE/kmodule/15_sysfs# cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.1 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
 

mysysfs.c

root@auto:/media/sf_D_DRIVE/kmodule/15_sysfs# cat mysysfs.c
#include <linux/module.h>
#include <linux/kobject.h>// implement ref:kernel/ksysfs.c
#define TAG "hello# "static struct kobject *hello_kobj;static ssize_t hello1_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{return sprintf(buf, "%s\n", __func__);
}static ssize_t hello1_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{return count;
}static struct kobj_attribute hello_attr1 = {.attr = {.name = "hello1",.mode = 0777},.show = hello1_show,.store = hello1_store
};static struct attribute * hello_attrs[] = {&hello_attr1.attr,{},
};static const struct attribute_group hello_attr_group = {.attrs = hello_attrs,
};static int mysysfs_init(void)
{int error;printk(TAG "%s called\n", __func__);hello_kobj = kobject_create_and_add("hello", NULL);if (!hello_kobj) {error = -ENOMEM;goto exit;}error = sysfs_create_group(hello_kobj, &hello_attr_group);if (error)goto kset_exit;return 0;
kset_exit:kobject_put(hello_kobj);
exit:return error;
}static void mysysfs_exit(void)
{printk(TAG "%s called\n", __func__);sysfs_remove_group(hello_kobj, &hello_attr_group);kobject_put(hello_kobj);
}module_init(mysysfs_init);
module_exit(mysysfs_exit);MODULE_LICENSE("GPL");

效果

http://www.xdnf.cn/news/332353.html

相关文章:

  • influxdb实战
  • CPU-GPU-NPU-TPU 概念
  • 统一返回JsonResult踩坑
  • 今日行情明日机会——20250507
  • 前端三大件--HTML
  • svn服务器迁移
  • BK精密电源操作软件 9130BA系列和手侧user manual
  • 文生图(Text-to-Image)的发展
  • 人工智能如何进行课堂管理?
  • 分布式爬虫去重:Python + Redis实现高效URL去重
  • 多账号管理、反追踪与自动化测试:我的浏览器实战笔记
  • string--OJ4
  • LeetCode106_从中序与后序遍历序列构造二叉树
  • 迈向AI辅助数据分析代码生成的透明性与知识共享
  • #黑马点评#(三)缓存穿透/雪崩/击穿
  • hadoop中的序列化和反序列化(1)
  • MySQL的information_schema在SQL注入中的关键作用与防御策略
  • 由浅入深谈Python书写规范
  • 【MySQL】-- 联合查询
  • Linux:进程控制1
  • 如何利用 QuickAPI 生成 PostgreSQL 样本测试数据:全面解析与实用指南
  • vue-qr生成的二维码增加下载功能
  • 【云备份】客户端开发
  • 百胜企业管理咨询:助力企业快速获得ecovadis认证
  • SecureCRT SFTP命令详解与实战
  • S32K3 HSE模块安装
  • 屏蔽力 | 在复杂世界中从内耗到成长的转变之道
  • STM32开发printf函数支持
  • LeetCode:二叉树的最大深度
  • React Native主题切换、字号调整:不用styled-components也能玩出花