当CD的抽屉已经被关闭确定 [英] Determining when cd drawer has been closed

查看:118
本文介绍了当CD的抽屉已经被关闭确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,需要观察被手动关闭CD托盘一个bash脚本。该驱动器是超薄因此它没有打开/关闭马达。什么我需要监控,将显示抽屉该国或它的关闭的事件?

I am working on a bash script that needs to watch for the CD drawer being manually closed. The drive is slimline so it has no open/close motor. What do I need to monitor that would show either the state of the drawer or the event of its closure?

我运行Ubuntu 12.04服务器。

I am running Ubuntu 12.04 server.

推荐答案

您可以使用跨preTED语言如Perl,Python或Ruby来调用的ioctl 内核用于查询驱动器状态。

You can use interpreted languages like Perl, Python, or Ruby to call an ioctl to the kernel for querying drive status.

function check_disk_tray_perl {
    perl -e 'use POSIX; sysopen(FD, $ARGV[0], O_RDONLY|O_NONBLOCK) || die "Failed to open device ${ARGV[0]}\n"; my $r = ioctl(FD, 0x5326, 0); exit ($r > 0 ? $r & 0x01 ? 0 : 1 : 2);' "$1"
}

function check_disk_tray_ruby {
    ruby -e 'require "fcntl"; dev = ARGV[0]; begin fd = IO::sysopen(dev, Fcntl::O_RDONLY|Fcntl::O_NONBLOCK); io = IO.new(fd); rescue; $stderr.puts "Failed to open device #{dev}."; exit 2; end; r = io.ioctl(0x5326, 0); exit (r > 0 ? (r & 0x01) == 1 ? 0 : 1 : 2);' "$1"
}

function check_disk_tray_python {
    python -c 'import os, sys, fcntl
dev = str(sys.argv[1])
try:
    fd = os.open(dev, os.O_RDONLY|os.O_NONBLOCK)
except:
    sys.stderr.write("Failed to open device " + dev + ".\n")
    exit(2)
r = fcntl.ioctl(fd, 0x5326, 0)
os.close(fd)
exit(2 if (r <= 0) else 0 if (r & 1) else 1)
' "$1"
}

您也可以编译code与海湾合作委员会的运行时:

You could also compile a code with gcc on runtime:

function check_disk_tray_gcc {
    local OUTPUT_BINARY="/tmp/check_disk_tray_gcc_$((RANDOM))"
    local FLAGS=(-O2 -pipe -fomit-frame-pointer)
    local R

    gcc "${FLAGS[@]}" -o "$OUTPUT_BINARY" -xc - <<EOF
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/cdrom.h>

int main(int argc, char** argv)
{
    if (argc != 2) {
        fprintf(stderr, "Invalid number of arguments.\n");
        return 2;
    }

    char* file = argv[1];

    int fd = open(file, O_RDONLY|O_NONBLOCK);

    if (fd < 0) {
        fprintf(stderr, "Failed to open file %s.\n", file);
        return 2;
    }

    int r = ioctl(fd, CDROM_DRIVE_STATUS, 0);

    return (r > 0 ? r & 1 ? 0 : 1 : 2);
}
EOF

    if [[ $? -ne 0 || ! -f "$OUTPUT_BINARY" ]]; then
        R=2
    else
        "$OUTPUT_BINARY" "$1"
        R=$?
    fi

    rm -f "$OUTPUT_BINARY"

    return "$R"
}

包装函数:

function check_disk_tray {
    if type -P perl >/dev/null; then
        check_disk_tray_perl "$1"
    elif type -P ruby >/dev/null; then
        check_disk_tray_ruby "$1"
    elif type -P python >/dev/null; then
        check_disk_tray_python "$1"
    elif type -P gcc >/dev/null; then
        check_disk_tray_gcc "$1"
    else
        echo "No tool usable for checking disk tray." >&2
        return 2
    fi
}

实例:

#!/bin/bash

# ---- Place the above codes here. ----

check_disk_tray "$(readlink -f /dev/cdrom)"
case "$?" in
0)
    echo "Tray is closed."
    ;;
1)
    echo "Tray is open."
    ;;
2|*)
    echo "General failure."
    ;;
esac

这篇关于当CD的抽屉已经被关闭确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆