问题
I'm trying to unmount programmatically an USB drive (device /dev/sdb1
). If I run in a Linux terminal sudo umount /dev/sdb1
it works. However, if I gcc
compile and run the following C snippet as sudo
, it errors with ERRNO 22 = EINVAL (Invalid argument)
.
This is the code:
#include "unistd.h"
#include "sys/mount.h"
#include "errno.h"
int main()
{
int r = umount2("/dev/sdb1", MNT_FORCE);
if (r != 0) return errno;
else return 0;
}
The same applies for umount()
. MNT_FORCE
doesn't change anything.
The function works if I pass the mount point instead of device, yet the documentation says it works with both. I find this way more reliable than reading /etc/mtab
to get the mount point and use that.
Function: int umount2 (const char *file, int flags)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
umount2 unmounts a filesystem.
You can identify the filesystem to unmount either by the device special file that contains the filesystem or by the mount point. The effect is the same. Specify either as the string file.
What is wrong?
回答1:
Give your mount_path. It should work.
int r = umount2("/your_mount_path", MNT_FORCE);
来源:https://stackoverflow.com/questions/30547553/umount-doesnt-work-with-device-in-c-but-it-works-in-terminal