eject

categories: code

A friend of mine recently gave me her Huawei E1550 umts modem and as many others it first presents itself to the operating system as a usb cdrom drive. It will only switch to serial modem mode when the cdrom is ejected. For this to happen you can fire the eject command and you are done. Since I was curious how linux would eject a cdrom and how eject really works I investigated and found that I can replicate the eject behavior with this C snippet.

#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <linux/cdrom.h>

int main() {
int ret, fd;

fd = open("/dev/sr0", O_NONBLOCK);
if (fd == -1) {
perror("open");
return EXIT_FAILURE;
}

ret = ioctl(fd, CDROMEJECT, 0);
if (ret == -1) {
perror("ioctl");
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

or alternatively with this python oneliner:

python -c "import fcntl, os; fcntl.ioctl(os.open('/dev/sr0', os.O_NONBLOCK), 0x5309, 0)"
View Comments
blog comments powered by Disqus