diff options
author | Kay Sievers <kay.sievers@vrfy.org> | 2010-03-18 11:14:32 +0100 |
---|---|---|
committer | Kay Sievers <kay.sievers@vrfy.org> | 2010-03-18 11:14:32 +0100 |
commit | 38a3cde11bc77af49a96245b8a8a0f2b583a344c (patch) | |
tree | a627fdcc1c109c233a22d43c7066a2822e1171de | |
parent | 23325a66ccf3a2f90a4f5d1702f60408187de28a (diff) |
cdrom_id: open non-mounted optical media with O_EXCL
This should prevent confusing drives during CD burning sessions. Based
on a patch from Harald Hoyer.
-rw-r--r-- | extras/cdrom_id/cdrom_id.c | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/extras/cdrom_id/cdrom_id.c b/extras/cdrom_id/cdrom_id.c index 2380b158a0..e485768de4 100644 --- a/extras/cdrom_id/cdrom_id.c +++ b/extras/cdrom_id/cdrom_id.c @@ -112,6 +112,30 @@ static unsigned long long int cd_media_session_last_offset; #define ASC(errcode) (((errcode) >> 8) & 0xFF) #define ASCQ(errcode) ((errcode) & 0xFF) +static int is_mounted(const char *device) +{ + struct stat statbuf; + FILE *fp; + int maj, min; + int mounted = 0; + + if (stat(device, &statbuf) < 0) + return -ENODEV; + + fp = fopen("/proc/self/mountinfo", "r"); + if (fp == NULL) + return -ENOSYS; + while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) { + printf("got %u %u\n", maj, min); + if (makedev(maj, min) == statbuf.st_rdev) { + mounted = 1; + break; + } + } + fclose(fp); + return mounted; +} + static void info_scsi_cmd_err(struct udev *udev, char *cmd, int err) { if (err == -1) { @@ -568,7 +592,7 @@ int main(int argc, char *argv[]) goto exit; } - fd = open(node, O_RDONLY | O_NONBLOCK); + fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL)); if (fd < 0) { info(udev, "unable to open '%s'\n", node); fprintf(stderr, "unable to open '%s'\n", node); |