0

我有一个程序,其中需要对未安装或以任何其他方式使用的驱动器进行降速。

我注意到在我关闭文件描述符后驱动器会自动启动。

我还没有找到任何信息为什么会这样,有什么办法可以禁用它吗?

这是一个自己测试的简短程序。任何帮助或指示将不胜感激

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <scsi/sg.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    sg_io_hdr_t io_hdr;
    const unsigned char     stopcmdblk[6]        =     { 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00 };

    int fd = open(argv[1], O_RDWR);
    if (fd == -1) {
        perror("couldn't open device");
        exit(1);
    }

    memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
    io_hdr.interface_id = 'S';
    io_hdr.cmd_len = 6;
    io_hdr.mx_sb_len = 32;
    io_hdr.dxfer_direction = SG_DXFER_NONE;
    io_hdr.dxfer_len = 0;
    io_hdr.dxferp = NULL;
    io_hdr.cmdp = malloc(6);
    io_hdr.sbp = calloc(32, 1);

    memcpy(io_hdr.cmdp, stopcmdblk, 6);

    errno = 0;
    int ret = ioctl(fd, SG_IO, &io_hdr);
    if (ret < 0) { 
        perror("ioctl error");
        exit(1);
    }

    if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
        printf("SCSI err\n");
        exit(1);
    }

    printf("finished spindown\n");

    sleep(30);

    printf("close file now\n");

    close(fd);
    printf("file closed\n");

    exit(0);

}
4

1 回答 1

1

我猜系统想在文件关闭时将一些元数据写入磁盘。例如,文件大小 - 每次write调用后更新文件大小似乎没有意义。所以它在close通话中更新。

我认为您需要sync系统调用。还有一种fsync变体只适用于一个文件;但是,这与您的文件描述符无关;您要删除驱动器,因此应注意所有文件描述符。

于 2014-12-18T19:10:25.123 回答