cfreebsddvdcd-rom

Random seek on CD-ROM drive


This works on Linux, but not (as I would like) on FreeBSD:

I wish to exercise my CD-ROM drive, to keep the dust off the lens. On Linux I run (as root) a C program I wrote which seeks back and forth, reading a single block each time as it goes. On FreeBSD this program doesn't get too far. I can open the device and seek to (say) block 1. But when I try to read the block, I get error 22 (EINVAL). It fails on the first read, at block 1, whether or not the device is mounted (-t cd9660). How do I proceed?

Full program is here. The relevant snippet:

  lo_fd=Open(ar_argv[1],
             O_RDONLY,
             0
            );

  lo_high_bit=1;

  while(lo_high_bit>0)
  {
    if(lseek(lo_fd,
             lo_high_bit,
             SEEK_SET
            )
       ==
       (off_t)-1
      )
    {
      lo_high_bit>>=1;

      break;
    }

    if(read(lo_fd,
            lo_buffer,
            1
           )
       !=
       1
      )
    {
      lo_high_bit>>=1;

      break;
    }

    lo_high_bit<<=1;
  }

Solution

  • It turns out that I was making two errors: trying to read a byte at a time, and lseek()ing to byte 1. fstat() on the device shows st_blksize of 4096.

    Seeking to 4096 and reading 4096 bytes works.

    Seeking to 2048 and reading 2048 bytes works.

    Seeking to 2048 and reading 1024 bytes gives EINVAL on the read().

    Seeking to 1024 and reading 2048 bytes gives EINVAL on the read().