I am going to write a Lexical that is going to be part of a kernel module in which I will parse a file and return tokens. For that, I may need to use functions like fopen, getc,putc,fseek etc which obviously are user space functions. I have searched for alternatives to these functions in kernel space and I found functions like open, filp_open, sys_open etc which I guess would be OK for me. But what I want to know is whether functions like getc, putc, seek etc (which can be very handy in file operations), are available in kernel space?
Asked
Active
Viewed 3,857 times
6
-
2Why would you want to put this into the kernel in the first place? – Nobody moving away from SE Feb 27 '12 at 10:33
-
I think open is a system call. – Shiplu Mokaddim Feb 27 '12 at 10:35
-
@Nobody I am writing a kernel module which would require to parse a config file for some functionality. – awatan Feb 27 '12 at 10:43
-
7Maybe you should have a look on other modules solutions. It does not sound like a good solution to me to have a task like parsing a file in the kernel. You should not put anything inside that does not necessarily need the kernel power. Maybe you should design it as a module and an user space program that parses the config and hands it over to the module. – Nobody moving away from SE Feb 27 '12 at 10:50
-
4@JewelThief In that case, add a system call which has a defined API, and write a userland agent to convert the configuration file into the correct button pushes. There's a reason that userland exists. – tbert Feb 27 '12 at 10:51
-
1Alternatively, expose configuration knobs via the sysfs filesystem. – janneb Feb 29 '12 at 11:03
-
Do not add a system call. Do as janneb said, expose configuration knobs via sysfs, or via netlink, or implement ioctl()s for your device/driver. – Kristof Provost Nov 04 '12 at 16:21
-
Related: [How to read/write files within a Linux kernel module?](http://stackoverflow.com/questions/1184274/how-to-read-write-files-within-a-linux-kernel-module), http://stackoverflow.com/questions/12264291/is-there-a-c-function-like-sprintf-in-the-linux-kernel , http://stackoverflow.com/questions/13485271/why-cant-we-use-c-function-in-kernel-development , – Ciro Santilli OurBigBook.com May 13 '17 at 06:07
1 Answers
5
Don't.
Reading files, and especially complex configuration files, is not something which should be done from the kernel.
There's a lot of information about why this is a bad idea. The KernelNewbies FAQ is a good start.
Really, really don't do this. I integrate a lot of vendor (kernel) code, and this is one of the mistakes which keeps coming up and biting us. Learn to do things the right way from the start.

Kristof Provost
- 26,018
- 2
- 26
- 28
-
7So, after someone (... with decades of kernel experience ...) gets to the end of that reference and concludes that for their embedded product that the only way to do "it" is to ignore the "Don't" advice, how does that happen? Not to worry -- I can read the source. I'm just tired of answers like "Don't" here. Not at all helpful. I'd down-vote your answer but I'm too busy to click the "Down" button ... – Julie in Austin Oct 29 '12 at 18:20
-
See, I can agree that sometimes it's required (like for the NFS server), but I also think you agree that "Don't" is actually the answer Jewel Thief needed to hear. I'd add a link to a brief description, but anyone who *really* does need it will find it in 5 minutes, and it'd only encourage those who shouldn't do it to do it anyway. – Kristof Provost Oct 29 '12 at 20:32
-
4No, I don't agree that "Don't" is the correct answer here because that sounds too much like we're a bunch of Nannies trying to keep the OP from breaking their machine. Your justification that "Don't" is okay because we could find it in 5 minutes smacks of elitism. Well, what if I don't want to go spend 5 or 10 or 20 or ... minutes digging that up and I'm one of the "elite", because I really am -- did my first UNIX kernel work in 1981. – Julie in Austin Dec 03 '12 at 20:13