Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use interpreted languages like Perl, Python, or Ruby to call an <code>ioctl</code> to the kernel for querying drive status.</p> <pre><code>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 &gt; 0 ? $r &amp; 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 &gt; 0 ? (r &amp; 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 &lt;= 0) else 0 if (r &amp; 1) else 1) ' "$1" } </code></pre> <p>You could also compile a code with gcc on runtime:</p> <pre><code>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 - &lt;&lt;EOF #include &lt;stdio.h&gt; #include &lt;sys/ioctl.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;linux/cdrom.h&gt; 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 &lt; 0) { fprintf(stderr, "Failed to open file %s.\n", file); return 2; } int r = ioctl(fd, CDROM_DRIVE_STATUS, 0); return (r &gt; 0 ? r &amp; 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" } </code></pre> <p>Wrapper function:</p> <pre><code>function check_disk_tray { if type -P perl &gt;/dev/null; then check_disk_tray_perl "$1" elif type -P ruby &gt;/dev/null; then check_disk_tray_ruby "$1" elif type -P python &gt;/dev/null; then check_disk_tray_python "$1" elif type -P gcc &gt;/dev/null; then check_disk_tray_gcc "$1" else echo "No tool usable for checking disk tray." &gt;&amp;2 return 2 fi } </code></pre> <p>Example usage:</p> <pre><code>#!/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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload