Search code examples
debianmountusb-drive

USB umount stats: inappropriate ioctl for device


I'm writing some code for my program to mount a flash drive using

mount("/dev/sda", "/media", "vfat", MS_NOATIME, NULL)

and unmount using

status = umount2("/media",MNT_DETACH);
qDebug("USB unmount - status = %s\n", strerror(errno));

The message says: USB unmount - status = Inappropriate ioctl for device when in fact the flash was successfully unmounted. Am I doing anything wrong here?

The target system is debian and I'm using C++ with Qt


Solution

  • What you're seeing - a zero return from a system call interface function, indicating success, but a nonzero errno - is normal. This is because, in general, system call interface functions, and a few other C functions, only set errno when there's a failure, and otherwise leave it unchanged. (Strictly speaking, the value of errno after a successful function call is unspecified.)

    Here's a quote from the POSIX standard:

    The value of errno shall be defined only after a call to a function for which it is explicitly stated to be set and until it is changed by the next function call or if the application assigns it a value.

    The value of errno should only be examined when it is indicated to be valid by a function's return value.

    The setting of errno after a successful call to a function is unspecified unless the description of that function specifies that errno shall not be modified.

    Looking at the documentation for umount2:

    On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

    So your call to umount2 succeeded, and the value of errno is unspecified. It is likely that errno was set by some other recently-called function that failed. You can run strace on your program to see which system call failed; since the error was ioctl-related, the function was probably part of the stdio infrastructure probing the attributes of stdout, and it's normal for that to happen.