0

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!

Benesh
  • 3,398
  • 1
  • 18
  • 38

1 Answers1

2

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.

Grapsus
  • 2,714
  • 15
  • 14
  • Thanks! But I'm still not sure what to do when I need a "custom" message. Maybe my example was oversimplified. What should I do when I have several arguments, or when there are several reasons an argument is considered invalid and I'd like to inform the user of the exact reson? – Benesh May 10 '14 at 15:00
  • @Benesh can explain why do you need a custom message ? An `ioctl` is an order to the kernel "do something related to this file descriptor", either it succeeds or it fails. When it fails, it fails for reason A, B or C. That's why a set of common error codes is reused. Kernel code is usually spartan, it doesn't care about returning nice messages like "Oops, there is an error, the PID you provided is 42 but we only allow PIDs 1, 2 and 3". Instead it says `EINVAL` which is more like "get your PID right !". – Grapsus May 10 '14 at 15:15
  • The `fd` is only used in order to enable communication. It is entirely possible that I'm misusing it, but I've seen some examples of this practice. Anyway, I'll follow your advice. Thanks again! – Benesh May 10 '14 at 15:34