can a linux kernel module call libc functions?


 
Thread Tools Search this Thread
Top Forums Programming can a linux kernel module call libc functions?
# 8  
Old 11-16-2011
Thank you otheus.
very enlightening.

Again, thank you all.
# 9  
Old 11-16-2011
Quote:
Originally Posted by vistastar
I know it is the kernel which makes syscalls and libc possible.
Do you? You don't seem to want to accept the implications.

All the fuzzy details of how things work are laid bare inside the kernel, and it must keep track of a lot more stuff than you do. That's why it's done inside the kernel, so you don't have to.

So you want to open a file inside the kernel. Great. Do open("/path/to/file", O_RDWR) call inside the kernel -- whoops, there isn't one.

Which open would you like?

Code:
$ grep open System.map-2.6.34-gentoo-r6

ffffffff810077e0 t setup_data_open
ffffffff81010f7d t fake_panic_fops_open
ffffffff81010fc7 t mce_open
ffffffff8101286a t severities_coverage_open
ffffffff81013802 t mtrr_open
ffffffff81017a64 t msr_open
ffffffff81017d92 t cpuid_open
ffffffff8101f661 t microcode_open
ffffffff81026c26 t memtype_seq_open
ffffffff8102dfee t schedstat_open
ffffffff8103a80c t execdomains_proc_open
ffffffff8103f78c T open_softirq
ffffffff81040d82 t iomem_open
ffffffff81040db4 t ioports_open
ffffffff81058022 t pm_qos_power_open
ffffffff8105a110 t prof_cpu_mask_proc_open
ffffffff8105cca8 t timer_list_open
ffffffff8105ffd8 t tstats_open
ffffffff81064123 t proc_dma_open
ffffffff81065859 t modules_open
ffffffff81069cb1 t kallsyms_open
ffffffff8106dda8 t snapshot_open
ffffffff8106efac t acct_file_reopen
...

Assuming you can even find a real_ultimate_kernel_mode_open_which_acts_just_like_userspace_open() call somewhere in that list, how do you use it? When you call open() from a process, the kernel knows what process you're calling it from, so it can alter your process' open file table. You don't have a process. How's it supposed to track it?

Turns out the kernel has its own global file table, which is all well and good, but means your real_ultimate_kernel_mode_open_which_acts_just_like_userspace_open() call won't work -- without a process, many important data structures are missing. You need to alter the global table and avoid needing any userspace one.

So you'd need to find something which alters the kernel's own global file table instead. call it kernel_internal_open(). Okay, good. Now consider whether it's safe to call kernel_internal_open() from your device driver. Might the kernel want you to lock some mutex first? What if something else has already locked it -- is it really okay for your kernel code to sit waiting? Or is the other thing going to sit waiting forever, waiting for you while you're waiting for it? And what kind of waiting should you do? Is a spinlock good enough, or do you have to tell the kernel to pre-empt yourself somehow so other things can happen while you're waiting?

What if kernel_internal_open is already the function that's calling you? Wouldn't all the data structures you want already be busy? It might happen -- that's how device drivers work, big central kernel functions are switchboards for choosing which device driver callbacks to call...

If you have a process, this is an easy fix. The calling process is suspended until the kernel has the right conditions to deal with it, plus the kernel automatically knows all kinds of things about you. But when you don't have a process, suddenly there's all kinds of things you need to worry about.

The concept of "process" is an artificial thing. It pretends very hard to have ideal conditions all the time even when the kernel might be flailing like mad behind the scenes. It's a very tranquil and safe place. The kernel is not. That's why its the kernel.

Last edited by Corona688; 11-16-2011 at 10:48 AM..
This User Gave Thanks to Corona688 For This Post:
# 10  
Old 11-23-2011
Thank you Corona688

Thanks for your patient and zeal. I will study the kernel earnestly. One years ago, I know nothing about linux. It is UNIX.COM who accompanies my way to linux. Thank you all. Thanks for the great forum.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call functions from other scripts

i have a file that contains functions and i want the functions to be available in another script. of course, the ideal situation here would be to put the functions in the same script, but i dont own these scripts. so I have to call the functions file from a different script. how do i... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Linux

Unload kernel module at boot time (Debian Wheezy 7.2, 3.2.0-4-686-pae kernel)

Hi everyone, I am trying to prevent the ehci_hcd kernel module to load at boot time. Here's what I've tried so far: 1) Add the following line to /etc/modprobe.d/blacklist.conf (as suggested here): 2) Blacklisted the module by adding the following string to 3) Tried to blacklist the module... (0 Replies)
Discussion started by: gacanepa
0 Replies

3. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies

4. Programming

Make cannot generate .ko linux kernel module

cannot generate .ko file on my linux, although it can generate module.symvers. But when I copy .c file and Makefile to another linux computer, there's no problem. The strange thing is: make is successfuly executed, and returned 0; make output: make -C /lib/modules/2.6.18-92.el5xen/build ... (4 Replies)
Discussion started by: vistastar
4 Replies

5. Red Hat

Export functions in kernel module to user Programs

Hi all, I just started working on kernel modules. One query i'm not able to resolve how i can use call any of my function(take example testfunc() ) defined in my loadable kernel module (take example : test.ko) I want to export kernel module functions to user programs. Consider i have... (0 Replies)
Discussion started by: er.tarun.9986
0 Replies

6. Linux

How to convert Linux Kernel built-in module into a loadable module

Hi all, I am working on USB data monitoring on Fedora Core 9. Kernel 2.6.25 has a built-in module (the one that isn't loadable, but compiles and links statically with the kernel during compilation) to snoop USB data. It is in <kernel_source_code>/drivers/usb/mon/. I need to know if I can... (0 Replies)
Discussion started by: anitemp
0 Replies

7. UNIX for Dummies Questions & Answers

Linux kernel module parameters

Hi, Does anyone know if it is possible to know the current value of a kernel module parameters after the module is loaded. Are the values of the parameters advertised at some /proc or /sys location ? The only thing I know is modinfo, that actually looks a the module .ko and gives a... (3 Replies)
Discussion started by: macL
3 Replies

8. SuSE

max number of slabs per kernel module (kernel 2.6.17, suse)

Hi All, Is there a max number of slabs that can be used per kernel module? I'm having a tough time finding out that kind of information, but the array 'node_zonelists' (mmzone.h) has a size of 5. I just want to avoid buffer overruns and other bad stuff. Cheers, Brendan (4 Replies)
Discussion started by: Brendan Kennedy
4 Replies

9. Programming

kernel-kernel call communication

hi all! i have developed a mechanism in system.c to count how many times each kernel call is called. The results are held in an array in system.c . What i want to do is to create a new kernel call which will print this array. I need help in passing the array from system.c to the new kernel call. ... (5 Replies)
Discussion started by: aureliano
5 Replies

10. Linux

Making Socket System Call From Linux Kernel Module?

Hi Everyone! How can we make a socket() system call from a linux module executing in kernel space? If any one knows, kindly tell me. It will be great. I want to use the socket interface in linux kernel space for sending raw packets over the network. Hamayun (0 Replies)
Discussion started by: mian_m_hamayun
0 Replies
Login or Register to Ask a Question