Question

I'm writing a kernel module that communicates with a user application via ioctl. The communication is one-way only - that is, under normal circumstances no output is required. My question is - how should I report errors to the user application?

For example, my module accepts a process ID as an input. Let's say the PID is invalid. I've found no matching error code in errno.h, so I need an alternative way of reporting this. I can think of several ways:

  1. Define a (positive) constant INVALID_PID in a common header file, return -INVALID_PID as the return value of the ioctl call, and handle it in the user application
  2. Print a meaningful message to the kernel log and return a generic error code
  3. The ioctl receives an argument from the user application - I can store the error message there
  4. Establish some two-way communication with the user application

What is the standard way of doing this?

Thanks!

Was it helpful?

Solution

Usually when ioctls fail, they use a standard code from include/uapi/asm-generic/errno-base.h. The meaning of each error is then documented for each specific ioctl.

In your case, you will probably want to return ENOENT or EINVAL. If the argument of your ioctl is a PID it will be obvious enough that ENOENT means that this PID doesn't exist. And you'd better document this behavior somewhere. A good example can be found in man console_ioctl.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top