Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/miscutils/eject.c

    r1765 r2725  
    66 * Copyright (C) 2005  Tito Ragusa <farmatito@tiscali.it>
    77 *
    8  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
     8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    99 */
    1010
     
    1414 */
    1515
     16#include <sys/mount.h>
    1617#include "libbb.h"
     18/* Must be after libbb.h: they need size_t */
     19#include "fix_u32.h"
     20#include <scsi/sg.h>
     21#include <scsi/scsi.h>
    1722
    1823/* various defines swiped from linux/cdrom.h */
     
    2328#define CDS_TRAY_OPEN        2
    2429
     30#define dev_fd 3
     31
     32/* Code taken from the original eject (http://eject.sourceforge.net/),
     33 * refactored it a bit for busybox (ne-bb@nicoerfurth.de) */
     34
     35static void eject_scsi(const char *dev)
     36{
     37    static const char sg_commands[3][6] = {
     38        { ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0 },
     39        { START_STOP, 0, 0, 0, 1, 0 },
     40        { START_STOP, 0, 0, 0, 2, 0 }
     41    };
     42
     43    unsigned i;
     44    unsigned char sense_buffer[32];
     45    unsigned char inqBuff[2];
     46    sg_io_hdr_t io_hdr;
     47
     48    if ((ioctl(dev_fd, SG_GET_VERSION_NUM, &i) < 0) || (i < 30000))
     49        bb_error_msg_and_die("not a sg device or old sg driver");
     50
     51    memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
     52    io_hdr.interface_id = 'S';
     53    io_hdr.cmd_len = 6;
     54    io_hdr.mx_sb_len = sizeof(sense_buffer);
     55    io_hdr.dxfer_direction = SG_DXFER_NONE;
     56    /* io_hdr.dxfer_len = 0; */
     57    io_hdr.dxferp = inqBuff;
     58    io_hdr.sbp = sense_buffer;
     59    io_hdr.timeout = 2000;
     60
     61    for (i = 0; i < 3; i++) {
     62        io_hdr.cmdp = (void *)sg_commands[i];
     63        ioctl_or_perror_and_die(dev_fd, SG_IO, (void *)&io_hdr, "%s", dev);
     64    }
     65
     66    /* force kernel to reread partition table when new disc is inserted */
     67    ioctl(dev_fd, BLKRRPART);
     68}
     69
    2570#define FLAG_CLOSE  1
    2671#define FLAG_SMART  2
     72#define FLAG_SCSI   4
    2773
    28 int eject_main(int argc, char **argv);
    29 int eject_main(int argc, char **argv)
     74static void eject_cdrom(unsigned flags, const char *dev)
    3075{
    31     unsigned long flags;
     76    int cmd = CDROMEJECT;
     77
     78    if (flags & FLAG_CLOSE
     79     || ((flags & FLAG_SMART) && ioctl(dev_fd, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN)
     80    ) {
     81        cmd = CDROMCLOSETRAY;
     82    }
     83
     84    ioctl_or_perror_and_die(dev_fd, cmd, NULL, "%s", dev);
     85}
     86
     87int eject_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     88int eject_main(int argc UNUSED_PARAM, char **argv)
     89{
     90    unsigned flags;
    3291    const char *device;
    33     int dev, cmd;
    3492
    3593    opt_complementary = "?1:t--T:T--t";
    36     flags = getopt32(argv, "tT");
    37     device = argv[optind] ? : "/dev/cdrom";
     94    flags = getopt32(argv, "tT" IF_FEATURE_EJECT_SCSI("s"));
     95    device = argv[optind] ? argv[optind] : "/dev/cdrom";
    3896
    39     // We used to do "umount <device>" here, but it was buggy
    40     // if something was mounted OVER cdrom and
    41     // if cdrom is mounted many times.
    42     //
    43     // This works equally well (or better):
    44     // #!/bin/sh
    45     // umount /dev/cdrom
    46     // eject
     97    /* We used to do "umount <device>" here, but it was buggy
     98       if something was mounted OVER cdrom and
     99       if cdrom is mounted many times.
    47100
    48     dev = xopen(device, O_RDONLY|O_NONBLOCK);
    49     cmd = CDROMEJECT;
    50     if (flags & FLAG_CLOSE
    51      || (flags & FLAG_SMART && ioctl(dev, CDROM_DRIVE_STATUS) == CDS_TRAY_OPEN))
    52         cmd = CDROMCLOSETRAY;
     101       This works equally well (or better):
     102       #!/bin/sh
     103       umount /dev/cdrom
     104       eject /dev/cdrom
     105    */
    53106
    54     ioctl_or_perror_and_die(dev, cmd, NULL, "%s", device);
     107    xmove_fd(xopen_nonblocking(device), dev_fd);
     108
     109    if (ENABLE_FEATURE_EJECT_SCSI && (flags & FLAG_SCSI))
     110        eject_scsi(device);
     111    else
     112        eject_cdrom(flags, device);
    55113
    56114    if (ENABLE_FEATURE_CLEAN_UP)
    57         close(dev);
     115        close(dev_fd);
    58116
    59117    return EXIT_SUCCESS;
Note: See TracChangeset for help on using the changeset viewer.