Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Wardriving: Need to lock cd-drive without a cd in it
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Gruffi
Apprentice
Apprentice


Joined: 15 Aug 2003
Posts: 209
Location: Antwerpen - Flanders - Belgium

PostPosted: Fri May 28, 2004 7:49 am    Post subject: Wardriving: Need to lock cd-drive without a cd in it Reply with quote

Hello there,

I like warbiking with my trusty gentoo laptop with kismet and gpsdrive.
Problem is that when i put my laptop in my backpack the cddrive is at the bottom and opens sometimes. I want to lock it without mounting a cd, is this possible?
_________________
... and we will show Microsoft, that they cannot take whatever they want. And that Free Software is our software!
Back to top
View user's profile Send private message
hw-tph
l33t
l33t


Joined: 08 Jan 2004
Posts: 768
Location: Uppsala, Sweden

PostPosted: Fri May 28, 2004 9:09 am    Post subject: Reply with quote

Not very constructive perhaps, but I feel the need to post it:

Doesn't it go without saying that warbiking and duct tape go hand in hand? Just tape that sucker up real good and you won't have any problems. :D


Håkan
Back to top
View user's profile Send private message
Gruffi
Apprentice
Apprentice


Joined: 15 Aug 2003
Posts: 209
Location: Antwerpen - Flanders - Belgium

PostPosted: Fri May 28, 2004 11:45 am    Post subject: Reply with quote

Did I mention that I don't want to demolish my laptop?
_________________
... and we will show Microsoft, that they cannot take whatever they want. And that Free Software is our software!
Back to top
View user's profile Send private message
PowerFactor
Veteran
Veteran


Joined: 30 Jan 2003
Posts: 1693
Location: out of it

PostPosted: Fri May 28, 2004 6:45 pm    Post subject: Reply with quote

Apparently it is possible. I haven't seen a program to do it though so you might have to write your own.
Back to top
View user's profile Send private message
jmercer
Tux's lil' helper
Tux's lil' helper


Joined: 15 Nov 2002
Posts: 95
Location: Newfoundland, Canada

PostPosted: Sat May 29, 2004 1:44 am    Post subject: Reply with quote

Hi there, based on the info in the link PowerFactor gave I wrote the following code:

Code:

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

int main(int argc, char** argv)
{
  if(argc != 2)
  {
    printf("error: must supply one argument which is the device\n");
    return -1;
  }

  int cdrom = open(argv[1], O_RDONLY|O_NONBLOCK);
  if(cdrom == -1)
  {
    printf("failed to open %s\n", argv[1]);
    return -1;
  }


  if(ioctl(cdrom, CDROM_LOCKDOOR, 1) == -1)
  {
        printf("Something went wrong\n");
        return -1;
  }

  printf("Drive locked, happy hunting\n");

  return 0;
}


compile it with g++ like this:

Code:

g++ lock.cpp -o lock


and run it like this:

Code:

./lock /dev/cdroms/cdrom0


You may have to check your fstab for the proper cdrom device.

I'm proud to report my cdrom is locked without a cd in it. But I can't unlock it...

Wait, running eject as root seems to do the trick.
Back to top
View user's profile Send private message
PowerFactor
Veteran
Veteran


Joined: 30 Jan 2003
Posts: 1693
Location: out of it

PostPosted: Sat May 29, 2004 2:08 am    Post subject: Reply with quote

For unlocking it
Code:
ioctl(cdrom, CDROM_LOCKDOOR, 0)
works for me.
Back to top
View user's profile Send private message
Gruffi
Apprentice
Apprentice


Joined: 15 Aug 2003
Posts: 209
Location: Antwerpen - Flanders - Belgium

PostPosted: Sat May 29, 2004 9:11 pm    Post subject: Reply with quote

w00t, thanks, i'll try it as soon as possible! :D
_________________
... and we will show Microsoft, that they cannot take whatever they want. And that Free Software is our software!
Back to top
View user's profile Send private message
darkstar808
n00b
n00b


Joined: 30 May 2004
Posts: 8

PostPosted: Sun May 30, 2004 7:46 am    Post subject: Reply with quote

Here is a version in c that includes both locking and unlocking
compile with
Code:
gcc ulcdrom.c -o ulcdrom

Run with -l to lock or -u to unlock. One caveat, for some reason you have to be superuser to unlock the drive but not to lock it. You could chmod it suid or su -c it as a quickfix (any input on how to resolve this would be appreciated). Here is the code, make sure to change CDROM_DEV to point to your CDROM device (not a symlink).

EDIT: I fixed some UID issues in the code (I should really read my own posts :oops: )

Meethune Bhowmick


Code:

/* ulcdrom.c - toggle lock ioctl for cdrom
Copyright (C) 2004 Meethune Bhowmick

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
  USAGE: ulcdrom -[l|u]
*/
#include <fcntl.h>
#include <linux/cdrom.h>
#include <sys/ioctl.h>
#include <strings.h>

/*Change CDROM_DEV to your cdrom device
  it HAS to be a device, NOT a symlink
  to a device */
#define CDROM_DEV "/dev/hdc"

/*Error Strings*/
#define ERROR_DEV "Unable to open cdrom!\n"
#define ERROR_U "Unable to unlock cdrom door\n"
#define ERROR_L "Unable to lock cdrom door\n"
#define SUCCESS_L "Locked cdrom door!\n"
#define SUCCESS_U "Unlocked cdrom door!\n"

void printusage(char *progname)
{

    printf("description: lock/unlock cdrom drive\n");
    printf("usage: %s -[l|u]\n\n", progname);
    printf("-l  lock cdrom drive\n");
    printf("-u  unlock cdrom drive\n");
}

int processparam(int argc, char *argv[])
{
    int lockunlock = 1;
    if (argc != 2)
    {
        printusage(argv[0]);
        return -1;
    }
    if (strcmp(argv[1], "-l") == 0)
        lockunlock = 1;
    else if (strcmp(argv[1], "-u") == 0)
        lockunlock = 0;
    else
    {
        printusage(argv[0]);
        return -1;
    }
    return lockunlock;
}
   
int main(int argc, char *argv[])
{
    int cdrom;
    int lockunlock = processparam(argc,argv);
    if (lockunlock == -1) return 1;
   
    if ((cdrom = open(CDROM_DEV, O_RDONLY | O_NONBLOCK)) == -1)
    {
        printf(ERROR_DEV);
        return 1;
    }
    if (ioctl(cdrom, CDROM_LOCKDOOR, lockunlock) == -1)
    {
        if (lockunlock == 1) printf(ERROR_L); else printf(ERROR_U);
        return 1;
    }
    if (lockunlock == 1) printf(SUCCESS_L); else printf(SUCCESS_U);
    return 0;
}




Last edited by darkstar808 on Wed Jun 02, 2004 1:46 am; edited 1 time in total
Back to top
View user's profile Send private message
PowerFactor
Veteran
Veteran


Joined: 30 Jan 2003
Posts: 1693
Location: out of it

PostPosted: Sun May 30, 2004 8:14 am    Post subject: Reply with quote

darkstar808 wrote:
One caveat, for some reason you have to be superuser to unlock the drive but not to lock it.

Hmm, that's odd. I can both lock and unlock mine as non root. I am running a 2.6 kernel though. Are you both running 2.4?
Back to top
View user's profile Send private message
darkstar808
n00b
n00b


Joined: 30 May 2004
Posts: 8

PostPosted: Sun May 30, 2004 8:30 am    Post subject: Reply with quote

I'm using mm sources 2.6.7_rc1 w/ udev and a dvdrom/cdrw. It just may be that my permissions are set funky for my cdrom. I didn't really try to figure out the cause. Maybe setting the effective gid (setegid) to cdrw will work, hmmm. I'll try in the morning.

Meethune

EDIT: I figured out the permission issue. In udev, /dev/cdrom/* are symlinks to /dev/hd? (whatever your cdrom device is). Changing CDROM_DEV to point to the actual device (that has user permissions of course) will resolve any uid/gid issues. You can then also remove all uid checking. I'll edit my code above to reflect the changes.
Back to top
View user's profile Send private message
SonWon
n00b
n00b


Joined: 29 Oct 2006
Posts: 5

PostPosted: Sun Oct 29, 2006 2:07 pm    Post subject: Reply with quote

This nice program almost does exactly what I want. It does lock the eject button when there is no CD but I need it to lock the eject button with a CD in the tray. Any ideas how to add that functionality? I should add, I still want eject to work from the popup menu but not the hardware button.

Thank you,
SonWon
Back to top
View user's profile Send private message
UncleOwen
Veteran
Veteran


Joined: 27 Feb 2003
Posts: 1493
Location: Germany, Hamburg

PostPosted: Sun Oct 29, 2006 2:29 pm    Post subject: Reply with quote

SonWon wrote:
I need it to lock the eject button with a CD in the tray.
Simply mount the cd.
Quote:
I still want eject to work from the popup menu but not the hardware button.
I have no idea what you're talking about.
Back to top
View user's profile Send private message
SonWon
n00b
n00b


Joined: 29 Oct 2006
Posts: 5

PostPosted: Sun Oct 29, 2006 3:01 pm    Post subject: Reply with quote

Thank you for the reply.

I have been trying to get the following behaviour from the CDROM drive.

Insert CD auto mount and lock the hardware eject button.
Right clicking on the CDROM icon on the desktop produces a popup menu with eject as one of the choices. Choosing eject ejects the CD.

The hardware eject button cannot be used to remove a CD.

Everything works but the hardware eject button is not disabled (locked) when the CD is inserted into the drive.

Thanks again
SonWon
Back to top
View user's profile Send private message
beatryder
Veteran
Veteran


Joined: 08 Apr 2005
Posts: 1138

PostPosted: Sun Oct 29, 2006 3:28 pm    Post subject: Reply with quote

SonWon wrote:
Thank you for the reply.

I have been trying to get the following behaviour from the CDROM drive.

Insert CD auto mount and lock the hardware eject button.
Right clicking on the CDROM icon on the desktop produces a popup menu with eject as one of the choices. Choosing eject ejects the CD.

The hardware eject button cannot be used to remove a CD.

Everything works but the hardware eject button is not disabled (locked) when the CD is inserted into the drive.

Thanks again
SonWon


One idea would be to disable Hal/Dbus, which provide the functionality of being able to eject a CD without un-mounting it first.
_________________
Dont make it idiot proof, make it work.
Neucode.org
<suppressed key>
Back to top
View user's profile Send private message
SonWon
n00b
n00b


Joined: 29 Oct 2006
Posts: 5

PostPosted: Sun Oct 29, 2006 4:44 pm    Post subject: Reply with quote

Thank you for the reply.

I have only been using Linux since August but I am learning fast. How would I disable Hal/Dbus? I see Dbus processes running but it doesn't seem safe to stop them?

I would guess I somehow need to capture the signal from the hardware eject button before it reaches the program that issues the umount command.

Thanks,
SonWon
Back to top
View user's profile Send private message
fisherking
Tux's lil' helper
Tux's lil' helper


Joined: 12 Nov 2003
Posts: 111

PostPosted: Sun Oct 29, 2006 5:01 pm    Post subject: Reply with quote

Hi, great program... I have a son that always keeps ejecting my cdroms and therefor had a need to modigy your program a bit.

now, you can say give which drive to lock or unlock as a argument to the program. And you can enter multiple devices at the same time!

Sometime, when I have some time to spare (...!...) I will make a daemon of it and lock it if it unlocks (by eject, for example)....

Code:

/* ulcdrom.c - toggle lock ioctl for cdrom
Copyright (C) 2004 Meethune Bhowmick

Slightly modified 2006 by Anders Mac Key (anders.mackey(a)gmail.com)


This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
  USAGE: ulcdrom -[l|u] device
*/
#include <fcntl.h>
#include <linux/cdrom.h>
#include <sys/ioctl.h>
#include <strings.h>
#include <getopt.h>

/*Change CDROM_DEV to your cdrom device
  it HAS to be a device, NOT a symlink
  to a device */
//#define CDROM_DEV "/dev/hdc"

/*Error Strings*/
#define ERROR_DEV "Unable to open cdrom!\n"
#define ERROR_U "Unable to unlock cdrom door\n"
#define ERROR_L "Unable to lock cdrom door\n"
#define SUCCESS_L "Locked cdrom door!\n"
#define SUCCESS_U "Unlocked cdrom door!\n"

void printusage(char *progname)
{

    printf("description: lock/unlock cdrom drive\n");
    printf("usage: %s -[l|u] device\n\n", progname);
    printf("-l device\tlock cdrom drive\n");
    printf("-u device\tunlock cdrom drive\n");
    exit(0);
}

 
int lock(int lockunlock, char* device)
{

    //int lockunlock = processparam(argc,argv);   
   int cdrom;
       
    if (lockunlock == -1) return 1;
   
    if ((cdrom = open(device/*CDROM_DEV*/, O_RDONLY | O_NONBLOCK)) == -1)
    {
        printf(ERROR_DEV);
        return 1;
    }
    if (ioctl(cdrom, CDROM_LOCKDOOR, lockunlock) == -1)
    {
        if (lockunlock == 1) printf(ERROR_L); else printf(ERROR_U);
        return 1;
    }
    if (lockunlock == 1) printf(SUCCESS_L); else printf(SUCCESS_U);




 
int main(int argc, char *argv[])
{

     if(argc <= 1)
   printusage(argv[0]);
   
   int next_opt;
   const char* const sopt = "hl:u:sv";
   const struct option lopt[] =  {
   {"help",     0, NULL, 'h'},     // Outputs usage information
    {"lock",   1, NULL, 'l'},        // locks the drive
    {"unlock",  1, NULL, 'u'},     // unlocks it
 //   {"silent",     0, NULL, 's'},    // silent mode!
//    {"version",0,NULL,'v'},       // prints out verson information!
    { NULL,      0, NULL, 0  }      
     }; 


   do{
          next_opt = getopt_long(argc, argv, sopt, lopt, NULL);
   
      switch(next_opt)
      {
         case 'l':
            lock(1, optarg);
            break;
         case 'u':
            lock(0, optarg);
            break;
         case 'h':
             printusage(argv[0]);
             break;
         }
      }while(next_opt != -1);   
      


   return 0;

}
 



hups, usage was printed every time the program was run, always... fixed
Back to top
View user's profile Send private message
SonWon
n00b
n00b


Joined: 29 Oct 2006
Posts: 5

PostPosted: Sun Oct 29, 2006 5:30 pm    Post subject: Reply with quote

Compiling gives:

Code:
ulcdrom.c: In function ‘printusage’:
ulcdrom.c:47: warning: incompatible implicit declaration of built-in function ‘printf’
ulcdrom.c:51: warning: incompatible implicit declaration of built-in function ‘exit’
ulcdrom.c: In function ‘lock’:
ulcdrom.c:65: warning: incompatible implicit declaration of built-in function ‘printf’
ulcdrom.c:71: warning: incompatible implicit declaration of built-in function ‘printf’
ulcdrom.c:75: warning: incompatible implicit declaration of built-in function ‘printf’
ulcdrom.c: In function ‘main’:
ulcdrom.c:90: error: ‘NULL’ undeclared (first use in this function)
ulcdrom.c:90: error: (Each undeclared identifier is reported only once
ulcdrom.c:90: error: for each function it appears in.)
ulcdrom.c:100: warning: passing argument 5 of ‘getopt_long’ from incompatible pointer type


I added,

Code:
#define NULL 0


And got it to compile but it does not lock the cdrom drive?

Hmm, I went back and tried the orginal code and it is also not locking?

SonWon
[/quote]
Back to top
View user's profile Send private message
fisherking
Tux's lil' helper
Tux's lil' helper


Joined: 12 Nov 2003
Posts: 111

PostPosted: Sun Oct 29, 2006 6:13 pm    Post subject: Reply with quote

the program really needs some work!

hmm. I didn't have that problem, but the NULL problem, isn't that a C++ problem? Well, it doesn't hurt anyway, I hope, to add #define NULL 0,

Well, there is a bug... The drive isn't locked when you have a cd in the drive and using automount! Is that what didn't work?
Back to top
View user's profile Send private message
SonWon
n00b
n00b


Joined: 29 Oct 2006
Posts: 5

PostPosted: Sun Oct 29, 2006 6:35 pm    Post subject: Reply with quote

Quote:
The drive isn't locked when you have a cd in the drive and using automount!


Yes, this was my orginal problem with the code.

Now it does not even work with no CD. I think a reboot would change the behaviour. Something to do with the automount functions in the kernel, I think.

SonWon
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum