umount不适用于C中的设备(但适用于Terminal) [英] umount doesn't work with device in C (but it works in Terminal)
问题描述
我正在尝试以编程方式卸载USB驱动器(设备/dev/sdb1
).如果我在Linux终端sudo umount /dev/sdb1
中运行,则可以运行.但是,如果我gcc
编译并以sudo
身份运行以下C代码段,则ERRNO 22 = EINVAL (Invalid argument)
会出错.
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)
.
这是代码:
#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;
}
umount()
也是如此. MNT_FORCE
没有任何改变.
The same applies for umount()
. MNT_FORCE
doesn't change anything.
如果我通过安装点而不是设备,但该函数有效,但该文档说它可以与这两者一起使用.我发现这种方法比读取/etc/mtab
获取安装点并使用它更可靠.
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.
功能:int umount2(const char *文件,int标志)
Function: int umount2 (const char *file, int flags)
初步:| MT安全| AS安全交流安全|请参阅POSIX安全概念.
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
umount2卸载文件系统.
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.
怎么了?
推荐答案
提供您的mount_path.它应该工作.
Give your mount_path. It should work.
int r = umount2("/your_mount_path", MNT_FORCE);
这篇关于umount不适用于C中的设备(但适用于Terminal)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!