C + + implementation of Linux eject U disk method _c language

Source: Internet
Author: User
Tags goto

This article describes the C + + implementation of Linux eject U disk method. Share to everyone for your reference. Specifically as follows:

In Linux, for USB devices, we are generally used on mount, do not use when umount off on it.

In the ubuntu10.04, when we insert the U disk, will appear U disk equipment, when I click on this device can mount U disk, and read the file inside, when we do not use, we again click on this device can eject this device, if you want to use the U disk again, then you have to plug the U plate again before you can.

Umount and eject u disk is different, after umount we can again mount on the use, our U disk device is still in, but after the U disk, we want to use the Insert U disk before you can. For example, I have a U disk, the device is SDB, there is a partition sdb1, after the U disk is ejected, we will not see the SDB device when we use FDISK to list the disk.

Eject the U disk under Linux we can use the following command (for example, my U disk device is SDB1):

Copy Code code as follows:
sudo eject-s/dev/sdb1

Here you can look at the eject code and extract it:

main.cpp files are as follows:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include < sys/ioctl.h> #include <fcntl.h> #include <string.h> #include <linux/fd.h> #include <sys/ mount.h> #include <scsi/scsi.h> #include <scsi/sg.h> #include <scsi/scsi_ioctl.h> int main (int
  ARGC, Char *argv[]) {int FD =-1;
  Char *device;
    if (argc!= 2) {printf ("usage:usb-s/dev/sde1");
  return-1;
  } device = StrDup (argv[1]); if (FD = open (device, o_rdonly|
    O_nonblock)) < 0) {printf ("Open device%s failed!\n", device);
    Free (device);
  return-1;
  int status, K;
  sg_io_hdr_t Io_hdr;
  unsigned char allowrmblk[6] = {allow_medium_removal, 0, 0, 0, 0, 0};
  unsigned char startstop1blk[6] = {start_stop, 0, 0, 0, 1, 0};
  unsigned char startstop2blk[6] = {start_stop, 0, 0, 0, 2, 0};
  unsigned char inqbuff[2];
  unsigned char sense_buffer[32]; if (IOCTL (FD, Sg_get_version_num, &k) < 0) | | (K < 30000))
   {printf ("Not a SG device, or old SG driver\n");
  Goto out;
  } memset (&AMP;IO_HDR, 0, sizeof (sg_io_hdr_t));
  io_hdr.interface_id = ' S ';
  Io_hdr.cmd_len = 6;
  Io_hdr.mx_sb_len = sizeof (Sense_buffer);
  Io_hdr.dxfer_direction = Sg_dxfer_none;
  Io_hdr.dxfer_len = 0;
  Io_hdr.dxferp = Inqbuff;
  IO_HDR.SBP = Sense_buffer;
  Io_hdr.timeout = 10000;
  IO_HDR.CMDP = allowrmblk;
  Status = IOCTL (FD, Sg_io, (void *) &AMP;IO_HDR);
  if (Status < 0) {goto out;
  } IO_HDR.CMDP = startstop1blk;
  Status = IOCTL (FD, Sg_io, (void *) &AMP;IO_HDR);
  if (Status < 0) {goto out;
  } IO_HDR.CMDP = startstop2blk;
  Status = IOCTL (FD, Sg_io, (void *) &AMP;IO_HDR);
  if (Status < 0) {goto out;
}/* Force kernel to reread partition table when new disc/status = inserted (FD, IOCTL);
  Out:close (FD);
  Free (device);
return 0;

 }

Compile and run:

Compile:

Copy Code code as follows:
g++-g-wall Main.cpp-o usb-s


Now we're going to pop the SDB1 u disk and that's it.
Copy Code code as follows:
sudo usb-s/dev/sdb1

I hope this article will help you with the C + + program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.