1

I am developing a user space program that interacts with my linux kernel module. I know one of the ways to achieve this is by using netlink sockets. However, netlink sockets are not used by shell commands such as 'ls' for interaction with the kernel.

So how do various shell commands interact with the kernel? I tried browsing the code for ls command (ls.c) but wasn't able to comprehend this.

user2522685
  • 870
  • 1
  • 7
  • 8
  • `libc`, `glibc` and "system calls" should be sufficient to point you in the right direction. – twalberg Jan 24 '17 at 21:35
  • Can you be a little more specific please? – user2522685 Jan 24 '17 at 23:12
  • 1
    You have to choose most suitable ABI for your communications. There are: IOCTL on top of device node, sysfs file nodes, netlink, system call. Most popular and easiest one is sysfs. You may use it inside tiny environments. However, each of the listed above have pros and cons. – 0andriy Jan 24 '17 at 23:15
  • @user2522685 Not really. Your question is very open ended and broad. The kernel provides numerous interface points between user space and kernel space, including your netlink sockets, system calls, sysfs, procfs, character devices, block devices, and quite a few others. Many of these are wrapped in various system libraries including the C runtime library, where you find things like `readdir`, `stat`, etc... Way too much to describe here. – twalberg Jan 24 '17 at 23:38
  • I understand that the question has a broad scope. I was just trying to analyze how shell commands like 'ls' access the kernel. Browsed the code but could not find the place of interaction. Anyways, I will try to analyze other the other ways you mentioned for kernel interaction and then decide which one to use in my program. Thanks for the help!! – user2522685 Jan 25 '17 at 01:28

1 Answers1

2

I think @Ondriy and @twalberg has already given the answer. But still I am adding some explanation.

There are different way through which we can interact with kernel space.

Syscall: In Linux kernel each system call is assigned a unique syscall number. Like open() -> __NR_open,close() -> __NR_exit,read() -> __NR_read.

IOCTL: There are already predefined ictl number in the kernel. So by using these number you can interact with kernel space. e.g. ioctl(/dev/i2c-0,I2C_SMBUS,address);

Netlink Socket: Through netlink socket you can pass the message from kernel space to user space. e.g socket(), bind(), sendmsg().

Sysfs/procfs: You can communicate with the kernel through sysfs as well proc fs. (Have a look into /sys/* folder).

But if you are looking particular only for ls command then I think you can follow the below link.

How does the 'ls' command work in Linux/Unix?

Community
  • 1
  • 1
vinod maverick
  • 670
  • 4
  • 14