Hello. I'm trying to figure out how to lock the tray of an optical drive, so that the drive will not open the tray when the user presses the hardware "eject" button on the drive.
This seems straightforward, because there is a well-documented list of IOCTLs for optical drive control, and one of them is CDROM_LOCKDOOR. (See Edward A. Falk's "cdrom.txt" doc file, and also /usr/include/linux/cdrom.h.)
However, in my tests, the drive still ejects the disk even when I use the CDROM_LOCKDOOR IOCTL to lock it.
/* gcc - W -Wall -o lock lock.c */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/cdrom.h>
int main(int argc, char *argv[])
{
int i, lock = 1;
while ((i = getopt(argc, argv, "u")) != -1) {
if (i != 'u')
goto usage;
lock = 0;
}
if (optind >= argc) {
usage:
fprintf(stderr, "usage: %s [-u] <device>\n", argv[0]);
return EXIT_FAILURE;
}
i = open(argv[optind], O_RDONLY);
if (i < 0) {
perror("open");
return EXIT_FAILURE;
}
if (ioctl(i, CDROM_LOCKDOOR, lock) < 0) {
perror("ioctl");
return EXIT_FAILURE;
}
if (close(i) < 0) {
perror("close");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}