- if ((m = find_mount_point(device, bb_path_mtab_file))) {
- if (umount(m->mnt_dir)) {
- bb_error_msg_and_die("Can't umount");
- } else if (ENABLE_FEATURE_MTAB_SUPPORT) {
- erase_mtab(m->mnt_fsname);
- }
- }
- if (ioctl(bb_xopen(device, (O_RDONLY | O_NONBLOCK)),
- (flags ? CDROMCLOSETRAY : CDROMEJECT))) {
- bb_perror_msg_and_die(device);
+ // We used to do "umount <device>" here, but it was buggy
+ // if something was mounted OVER cdrom and
+ // if cdrom is mounted many times.
+ //
+ // This works equally well (or better):
+ // #!/bin/sh
+ // umount /dev/cdrom
+ // eject
+
+ dev = xopen(device, O_RDONLY|O_NONBLOCK);
+ cmd = CDROMEJECT;
+ if (flags & FLAG_CLOSE
+ || (flags & FLAG_SMART && ioctl(dev, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN))
+ cmd = CDROMCLOSETRAY;
+ if (ioctl(dev, cmd)) {
+ bb_perror_msg_and_die("%s", device);