0

I am developing a Linux Kernel Module and I need to use an equivalent of fprintf function to print some informations in a file.

I wonder if there is an equivalent of this function in kernel mode.

Thank you!

gaston
  • 405
  • 5
  • 22
  • 2
    Yes, `printk()`. It's used many many times all over the kernel code, I wonder how you couldn't have been stumbled across it yet – Ctx Dec 11 '19 at 13:42
  • 2
    N.B.: [Read/write files within a Linux kernel module](https://stackoverflow.com/questions/1184274/read-write-files-within-a-linux-kernel-module), [Things You Never Should Do in the Kernel](https://www.linuxjournal.com/article/8110) – red0ct Dec 11 '19 at 13:46
  • @Ctx I think that `printk` is equivalent for `printf` ( for just printing message on the console ). Does it used also as an equiavalent for `fprintf` ? – gaston Dec 11 '19 at 13:48
  • 1
    @gaston No, most messages from `printk()` do _not_ end up in the console (that would be a mess!) but is distributed to files according to the rules of the syslog daemon (i.e. most ends up in /var/log/kern.log) – Ctx Dec 11 '19 at 13:49
  • @Ctx Yes you are right. However, FYI, `printk` does not work here. – gaston Dec 11 '19 at 13:57
  • You can print to a buffer first (e.g. using `snprintf` or `scnprintf`) and then write the buffer contents to a file. – Ian Abbott Dec 11 '19 at 14:26
  • @IanAbbott Which function I must use to write the buffer contents to a file kernel mode ? – gaston Dec 11 '19 at 14:36
  • @gaston Did you read the first post from my comment? There are some examples in it (depending on Linux kernel version). – red0ct Dec 11 '19 at 14:38
  • @red0ct Yes I am reading it now thank you. – gaston Dec 11 '19 at 14:45
  • You shouldn't do that to begin with. If you need some information to be in the file, use DebugFS. – 0andriy Dec 11 '19 at 15:42

0 Answers0