Linux: How to write in filesystem node from kernel for logging the kernel data

Posted on 2011-04-26
Last Modified: 2012-05-11
Hello experts,

Platform: Linux kernel: 2.6 +
Drivers dealing with: USB drivers, DMA driver

I have following requirement:

1. Create a node in filesystem (/proc or /dev) using proc filesystem or udev.
2. Write to this particular file from the driver mentioned above in all possible modes (Low power mode/ idle mode)
3. Avoid printf / kmsgs as it adds certain amount of delay while writing
4. Access file using cat command and read the data dumped by kernel components into mentioned file
5. During powerup of H/W, clear the file

I have gone through vast documentation available on internet but unable to find right solution to achieve above mentioned requirements. Please help me in identifying correct options of filesystem (udev / sysfs etc) which can be faster and easily accessible from both - kernel or userspace components.

Thanks in advance,

Question by:yogithite
    LVL 38

    Accepted Solution

    First, procfs is virtual filesystem (memory).

    How to Read and Write a /proc File

    Author Comment

    Hi Wesly_chen,

    Thank you for information. I want to know whether using proc file is the "best" way for logging details from kernel driver? Will it add any delay while logging data in filesystem? Is there any other option for writing data from kernel to any file (by not using kmsg/printf functionality)?

    LVL 12

    Expert Comment

    procfs is a pseudo file system, it is used within the kernel  to export access to the FS internal structure to the "user land". it is not created with the regular functions, but you need to write a kernel module (driver).
    One of the best books about linux devices is Alessandro Rubini's "Writing linux device drivers", available online here. Take a look. When you have learned all about drivers, you should be able to write your driver, as you request.
    BUT since what you ask is something to debug the kernel, why not using the kernel debugging infrastructure? You have many solutions: debugfs, kdbg, etc.
    LVL 38

    Expert Comment

    > whether using proc file is the "best" way for logging details from kernel driver?
    It depends.
    If they are driver parameters/setting (all possible modes :Low power mode/ idle mode), then yes.
    If they arre logs, then it'd better to put to /var for the record and tracing.
    The link HappyCactus mentioned "Linux device drivers" is good but old.
    Here is the newer version

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
    AWS Glacier is Amazons cheapest storage option and is their answer to a ‘Cold’ storage service.  Customers primarily use this service for archival purposes and storage of infrastructure backups.  Its unlimited storage potential and low storage cost …
    This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now