umount doesn't work with device in C (but it works in Terminal)

回眸只為那壹抹淺笑 提交于 2019-12-08 09:45:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!