Skip to content

Instantly share code, notes, and snippets.

@amaciel81
Created August 27, 2018 01:05
Show Gist options
  • Save amaciel81/bcb03a50f843f0163f7b9f0e088c102e to your computer and use it in GitHub Desktop.
Save amaciel81/bcb03a50f843f0163f7b9f0e088c102e to your computer and use it in GitHub Desktop.
ioctl example
import fcntl
import os
CD_DEVICE = '/dev/cdrom'
CD_EJECT = 0x5309 # From https://github.com/torvalds/linux/blob/master/include/uapi/linux/cdrom.h
# You need to open the device, not the link
if os.path.islink(CD_DEVICE):
device_path = os.readlink(CD_DEVICE)
if not device_path.startswith('/'):
device_path = os.path.dirname(CD_DEVICE) + '/' + device_path
else:
device_path = CD_DEVICE
device_fd = os.open(device_path, os.O_RDONLY | os.O_NONBLOCK )
fcntl.ioctl(device_fd, CD_EJECT, 0)
os.close(device_fd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment