HOMEVIDEOSSTUDENTSSPONSORSDONATIONSEVENTSTUTORIALSLINKSNEWSCONTACT


VIDEOS 》 Linux Kernel /sysfs Interface

/sysfs is one of the most popular kernel to user-space interface which you can leverage to add an interface to your Kernel code such as Kernel modules, Kernel Device Drivers, etc. Although personally I prefer /proc interface than other alternatives such as /sysfs, ioctl() and so on for my personal Kernel modules/stack.

So here is my detailed multi-episode Youtube video series on /sysfs Interface.

I also conduct sessions/classes on Systems and Network software programming and architecture. If you are interested, click HERE for more details.

Download this episode my entire kernel module sample code, make file, clean script HERE. And here is the same source code for a quick reference.

/* sysfs_example.c
 * Author: Kiran Kankipati
 * Updated: 14-May-2022
 * Release notes: this is the first version of Sample /sysfs read access file.
 * compatible with: 5.13.0-41-generic (Ubuntu 21.10) - as well tested with: 5.15.0-27-generic (Ubuntu 22.04 LTS)
 */
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/module.h> 
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>  
#include <linux/udp.h>
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/socket.h>
#include <linux/sockios.h>
#include <linux/inet.h>
#include <linux/inetdevice.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/if_arp.h>
#include <linux/icmp.h>
#include <linux/proc_fs.h>
#include <linux/netlink.h>
#include <linux/mroute.h>
#include <net/checksum.h>
#include <net/inet_ecn.h>
#include <net/xfrm.h>
#include <net/route.h>
#include <net/sock.h>
#include <net/ip.h>
#include <net/tcp.h>
#include <net/arp.h>
#include <net/udp.h>
#include <net/icmp.h>
#include <net/inetpeer.h>
#include <net/protocol.h>
#include <net/flow.h>
#include <asm/types.h>
#include <asm/string.h>

static struct kobject *register_kobj;
static char *sysfs_buf;
#define SYSFS_BUF_MAX_SIZE 1024

static ssize_t sysfs_read_value(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{ return sprintf(buf, "%s\n", sysfs_buf); }


static ssize_t __used sysfs_write_value(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count)
{ strcpy(sysfs_buf, buf); return count; }

static struct kobj_attribute sysfs_val_attribute = __ATTR( my_sysfs_file, 0660, sysfs_read_value, sysfs_write_value);

static int sysfs_module_init(void)
{
    sysfs_buf = kzalloc(SYSFS_BUF_MAX_SIZE, GFP_KERNEL); if(sysfs_buf==NULL) { return 0; }

    register_kobj = kobject_create_and_add("my_sysfs", kernel_kobj); { if(!register_kobj) { return -ENOMEM; } }


    if(sysfs_create_file(register_kobj, &sysfs_val_attribute.attr)) { printk("failed to create the my_sysfs_file file in /sys/kernel/my_sysfs/my_sysfs_file \n"); } 

    return 0;
}

static void sysfs_module_exit(void)
{
    kfree(sysfs_buf);
    kobject_put(register_kobj);
}


module_init(sysfs_module_init);
module_exit(sysfs_module_exit);

MODULE_DESCRIPTION("Sample /sysfs read/write access file");
MODULE_AUTHOR("Copyright(c) 2022 - Kiran Kankipati");
MODULE_LICENSE("GPL v2");



Suggested Topics:


Video Episodes :: Linux Kernel programming

Linux ioctl() API interface ↗
Saturday' 13-Mar-2021
Watch detailed videos and read topics on Linux Kernel Programming and Linux ioctl() API interface

Linux Operating System - User-space Processes ↗
Saturday' 14-May-2022

Linux Kernel Architecture ↗
Wednesday' 18-May-2022

Linux Kernel FileSystems Subsystem ↗
Saturday' 13-Mar-2021

Linux Kernel /proc Interface ↗
Wednesday' 18-May-2022
/proc is one of the most popular kernel to user-space interface which you can leverage to add an interface to your Kernel code such as Kernel modules, Kernel Device Drivers, etc. Personally I prefer /proc interface than other alternatives such as /sysfs, ioctl() and so on for my personal Kernel modules/stack. So here is my detailed multi-episode Youtube video series on /sysfs Interface. I also conduct sessions/classes on Systems and Network software programming and architecture.

What is purpose of Kernel Development - Example SMOAD Networks SDWAN Orchestrator Firewall Kernel Engine ↗
Monday' 18-Jul-2022
Often aspiring students may have this question, that what is the purpose of Linux Kernel Development. Since Linux Kernel is very mature and it has almost everything one would need. Usually, we need custom kernel development in the case of any new driver development for new upcoming hardware. And this happens on and on. But at times we may also come across few features/modules/components which are already provided by the Linux Kernel which are not adequate or atleast not the way we exactly intended to use. So, this is the real-world example, sometimes no matter what Linux Kernel provides as a part of stock Kernel/OS features, sometimes we have to write our own custom kernel stack or module(s) which can specifically cater our exact needs.

Linux Kernel /sysfs Interface ↗
Saturday' 14-May-2022
/sysfs is one of the most popular kernel to user-space interface which you can leverage to add an interface to your Kernel code such as Kernel modules, Kernel Device Drivers, etc. Although personally I prefer /proc interface than other alternatives such as /sysfs, ioctl() and so on for my personal Kernel modules/stack. So here is my detailed multi-episode Youtube video series on /sysfs Interface.

Linux Kernel Compilation ↗
Wednesday' 18-May-2022

Linux Kernel Programming ↗
Saturday' 13-Mar-2021

Linux Kernel - Containers and Namespaces ↗
Saturday' 13-Mar-2021

Linux Kernel Programming - Device Drivers ↗
Saturday' 13-Mar-2021
Watch detailed videos and read topics on Linux Kernel Programming - Device Drivers

Join The Linux Channel :: Facebook Group ↗

Visit The Linux Channel :: on Youtube ↗


💗 Help shape the future: Sponsor/Donate


Recommended Topics:
Featured Video:
Watch on Youtube - [490//0] QA - Selecting Linux Kernel preemption model based on your CPU ↗

What is a Linux Kernel Module - a Big Picture ↗
Saturday' 01-Jan-2022
Learning Linux Kernel Programming is always fascinating and yet challenging. So generally you may tend to learn Kernel Module programming, since such a module can be dynamically plugged into running Linux Kernel. But this will lead to confusion, and many assume kernel source is mostly a collection of these modules. Which in reality is not. Not just that, when we say Kernel Module, its a vaguely defined term. The term Module (as we know) is nothing but a collection of APIs, bunch of variables and associated data-structures. Which may or may not be a plugable kernel module. If you ask me, I am a fan of wiring Linux Kernel Modules, which may not be necessarily a pluggable kernel module. It all boils down to the address space at which these modules function inside a monolithic Linux Kernel. Which is nothing but Linux Kernel's address space. Hence here is my detailed multi-episode Youtube video series on Linux Kernel modules, a big picture and the significance of the

Linux Kernel Network stack and architecture ↗
Saturday' 13-Mar-2021

Bluefish IDE - a simple versatile light-weight best editor to edit kernel source files, html files, c source code, text files, SQL schema and so on ↗
Saturday' 13-Mar-2021

Linux Kernel struct udphdr data-structure ↗
Saturday' 13-Mar-2021

Linux Kernel TCP Congestion Control Algorithms ↗
Saturday' 13-Mar-2021

Linux Kernel struct socket and struct sock data-structure ↗
Saturday' 13-Mar-2021

Linux Kernel FileSystems Subsystem ↗
Saturday' 13-Mar-2021

C code to read a /proc file in Linux user-space - Live Demo and Example ↗
Saturday' 13-Mar-2021

Raspberry Pi GPIO Pins - Raw Kernel Access via sysfs - GPIO Linux Kernel Drivers ↗
Saturday' 01-Jan-2022
Here is a my multi-episode Youtube video series of me doing a live code (Code with Kiran Series). Linux Kernel GPIO drivers of Raspberry Pi offers access/control directly via sysfs filesystem interface. But often people don't see this aspect, instead as a part of IoT, they assume these GPIO pins can be only accessed by some Python, Java or C/C++ code libraries. But in the real case those libraries are just an abstract layer written around the core Kernel GPIO driver's sysfs filesystem. So here is an attempt where we learn this aspect in systems architecture point of view. And then later try to attempt writing such user-space GPIO control code without any third-party libraries. Since the sysfs driver access is via regular files (user-space context), we can now use any language of our choice and access the same like any files. In that process we can attempt writing our own bunch of abstract APIs resembling a typical GPIO access library.

Data Visualization and Analytics ↗
Saturday' 13-Mar-2021


Trending Video:
Watch on Youtube - [492//0] 0x208 TCP Sequence Numbers | why not in UDP ? | FAQ | Network Architecture | The Linux Channel ↗

What is a BSP | Board Support Package | Big Picture | Embedded Systems Software Development ↗
Saturday' 13-Mar-2021



Recommended Video:
Watch on Youtube - [459//0] 271 Linux Kernel TCP Congestion Control - tcp_cong.c - tcp_ca_find(), tcp_cong_list, tcp_init_congestion_control(), tcp_get_allowed_congestion_control() - Ep3 ↗