Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining when cd drawer has been closed

Tags:

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?

I am running Ubuntu 12.04 server.

like image 490
user2740987 Avatar asked Oct 03 '22 01:10

user2740987


1 Answers

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"
}

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"
}

Wrapper function:

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
}

Example usage:

#!/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
like image 132
konsolebox Avatar answered Oct 11 '22 12:10

konsolebox