本文介绍了卸载C ++中的USB驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发用于Linux(Ubuntu)的Qt应用程序,其中使用USB驱动器备份一些内容.复制内容后,应用程序应卸载目标驱动器.我有一个udev规则文件,用于通过ENV{mount_options}="relatime,users,umask=0,uid=user,gid=user"将USB挂载到特定位置,其中用户代表我的用户名.

I am developing a Qt application for Linux (Ubuntu) in which I use USB drive to backup some content. Application should unmount the target drive after copying stuff. I have a udev rule file to mount USB at a specific location with ENV{mount_options}="relatime,users,umask=0,uid=user,gid=user" where user represents my user name.

我尝试使用它没有任何运气.

I tried using this without any luck.

const char* usb = "/mnt/mountpoint/usbdrive";
if (!umount(usb))
{
  qDebug() << "Device unmounted";
}
else
{
  qDebug() << "Can't unmount" << strerror(errno); //this prints Operation not permitted
}

有人可以在这里帮助我吗?我在使用umount正确吗?

Could someone please help me here? Am I using umount right?

谢谢.

推荐答案

每个 umount 都可以.但是,您需要特权才能卸载设备.

Per umount the code is fine. However you need privilege to umount devices.

CAP_SYS_ADMIN 功能允许进程执行各种管理任务,即:调用mount() ,umount().这里有两篇有关 capabilities 的有价值的文章:

The CAP_SYS_ADMIN capability allows a process to perform various administrative tasks, namely: calling mount(), umount(). There are two worth articles about capabilities here:

  • CAP_SYS_ADMIN: the new root
  • Overview of Linux capabilities (List of capabilities)

这篇关于卸载C ++中的USB驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:58