当前位置: 首页 > 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");

效果

相关文章:

  • 在 R 中,清除包含 NA(缺失值)的数据
  • AOP封装进行批量的数据查询并填充
  • 探索智能体的记忆:类型、策略和应用
  • MySQL优化-MySQL常用查询命令
  • MATLAB三维可视化技术解析
  • 引用的使用
  • 什么是跨域,如何解决跨域问题
  • 脑图谱:脑机接口的必由之路——技术突破与产业转化的系统性思考
  • CI/CD与DevOps流程流程简述(给小白运维提供思路)
  • element-plus中,vue3项目,el-input密码框禁止浏览器自动弹出浏览器历史密码提示框
  • 关于Stream
  • 使用ffmpeg截取MP3等音频片段
  • ubuntu 挂载硬盘
  • Notion Windows桌面端快捷键详解
  • Solana批量转账教程:提高代币持有地址和生态用户空投代币
  • 高防ip是怎么做到分布式防御的
  • Decode rpc invocation failed: null -> DecodeableRpcInvocation
  • 计算机网络中相比于RIP,路由器动态路由协议OSPF有什么优势?
  • 多线程的出现解决了什么问题?深入解析多线程的核心价值
  • Loly: 1靶场渗透
  • 铁肩担道义,历史鉴未来——中共中央政治局委员、外交部长王毅谈习近平主席对俄罗斯进行国事访问并出席纪念苏联伟大卫国战争胜利80周年庆典
  • 印度证实印巴已同意停火
  • 国际足联女子世界杯再次扩军,2031年起增至48支球队
  • 名帅大挪移提前开启,意属皇马的阿隆索会是齐达内第二吗
  • 泉州一家婚介机构广告牌越南新娘七天闪婚领证?市监部门介入
  • 中国驻俄大使张汉晖人民日报撰文:共襄和平伟业,续谱友谊新篇