http://www.perlmonks.org/index.pl?node_id=194848

Description:
Eject, close, lock and unlock your cdrom with Inline C.
Great for playing mind-games on friends. The example will leave your
cdrom locked, so you need to run the script again to unlock.
Need to run this as root, unless your cdrom permissions are loose.
#!/usr/bin/perl -w
use warnings;
use Inline C;
use strict;

my $cd = '/dev/cdrom';

cdlock($cd,0); #unlocks cdrom 
cdeject($cd);  #ejects cdrom 
cdclose($cd);  #closes cdrom 
cdlock($cd,1); #locks cdrom  

exit;

__END__ 
__C__ 
#include <stdio.h> 
#include <sys/ioctl.h> 
#include <linux/cdrom.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <stdlib.h> 
 
/* In all functions 'device' means name of CD-ROM device, 
* for example /dev/cdrom 
*/ 
 
/* Close CD-ROM tray */ 
int cdclose(char *device) 
{ 
int fd = open(device, O_RDONLY|O_NONBLOCK); 
if (fd == -1) 
return -1; 
if (ioctl(fd, CDROMCLOSETRAY) == -1) 
return -1; 
close(fd); 
return 0; 
} 
 
/* Eject CD-ROM tray */ 
int cdeject(char *device) 
{ 
int fd = open(device, O_RDONLY|O_NONBLOCK); 
if (fd == -1) 
return -1; 
if (ioctl(fd, CDROMEJECT) == -1) 
return -1; 
close(fd); 
return 0; 
} 

/* Lock (if lock==1) or unlock (if lock==0) CD-ROM tray */ 
int cdlock(char *device, int lock) 
{ 
int fd; 
fd = open(device, O_RDONLY|O_NONBLOCK); 
if (fd == -1) 
return -1; 
if (ioctl(fd, CDROM_LOCKDOOR, lock) == -1) 
return -1; 
close(fd); 
return 0; 
} 


comment on cdromp
Download Code

Back to Snippets Section