X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=extras%2Fcdrom_id%2Fcdrom_id.c;h=d4156ce7ac310586f90e5c28ef924353d3bb9f6a;hb=bcee964977fe6a85e1c7e60f6a58ec61843e8921;hp=dbe80ea7e3709f53f4847a2a103d169d257d06ed;hpb=7d563a17f3967890331daf08d43f2f005418139b;p=elogind.git diff --git a/extras/cdrom_id/cdrom_id.c b/extras/cdrom_id/cdrom_id.c index dbe80ea7e..d4156ce7a 100644 --- a/extras/cdrom_id/cdrom_id.c +++ b/extras/cdrom_id/cdrom_id.c @@ -3,19 +3,18 @@ * * Copyright (C) 2008 Kay Sievers * - * 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 version 2 of the License. + * 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., - * 675 Mass Ave, Cambridge, MA 02139, USA. + * 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, see . */ #ifndef _GNU_SOURCE @@ -38,11 +37,8 @@ #include #include -#include "../../udev/udev.h" - -#ifndef ARRAY_SIZE -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -#endif +#include "libudev.h" +#include "libudev-private.h" static int debug; @@ -81,6 +77,7 @@ static unsigned int cd_mrw; static unsigned int cd_mrw_w; /* media info */ +static unsigned int cd_media; static unsigned int cd_media_cd_rom; static unsigned int cd_media_cd_r; static unsigned int cd_media_cd_rw; @@ -201,6 +198,16 @@ static int cd_capability_compat(struct udev *udev, int fd) return 0; } +static int cd_media_compat(struct udev *udev, int fd) +{ + if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) { + info(udev, "CDROM_DRIVE_STATUS != CDS_DISC_OK\n"); + return -1; + } + cd_media = 1; + return 0; +} + static int cd_inquiry(struct udev *udev, int fd) { struct scsi_cmd sc; unsigned char inq[128]; @@ -330,6 +337,8 @@ static int cd_profiles(struct udev *udev, int fd) return -1; } + cd_media = 1; + switch (cur_profile) { case 0x03: case 0x04: @@ -418,8 +427,10 @@ static int cd_media_info(struct udev *udev, int fd) info(udev, "disk type %02x\n", header[8]); - if ((header[2] & 3) < 4) + /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */ + if (!cd_media_cd_rom && (header[2] & 3) < 4) cd_media_state = media_status[header[2] & 3]; + if ((header[2] & 3) != 2) cd_media_session_next = header[10] << 8 | header[5]; cd_media_session_count = header[9] << 8 | header[4]; @@ -504,9 +515,9 @@ int main(int argc, char *argv[]) { struct udev *udev; static const struct option options[] = { - { "export", 0, NULL, 'x' }, - { "debug", 0, NULL, 'd' }, - { "help", 0, NULL, 'h' }, + { "export", no_argument, NULL, 'x' }, + { "debug", no_argument, NULL, 'd' }, + { "help", no_argument, NULL, 'h' }, {} }; const char *node = NULL; @@ -518,7 +529,7 @@ int main(int argc, char *argv[]) if (udev == NULL) goto exit; - logging_init("cdrom_id"); + udev_log_init("cdrom_id"); udev_set_log_fn(udev, log_fn); while (1) { @@ -560,6 +571,7 @@ int main(int argc, char *argv[]) fd = open(node, O_RDONLY | O_NONBLOCK); if (fd < 0) { info(udev, "unable to open '%s'\n", node); + fprintf(stderr, "unable to open '%s'\n", node); rc = 1; goto exit; } @@ -571,11 +583,13 @@ int main(int argc, char *argv[]) goto exit; } - /* check drive */ - if (cd_inquiry(udev, fd) < 0) { - rc = 2; - goto exit; - } + /* check for media */ + if (cd_media_compat(udev, fd) < 0) + goto print; + + /* check if drive talks MMC */ + if (cd_inquiry(udev, fd) < 0) + goto print; /* read drive and possibly current profile */ if (cd_profiles(udev, fd) < 0) @@ -632,6 +646,8 @@ print: if (cd_mrw_w) printf("ID_CDROM_MRW_W=1\n"); + if (cd_media) + printf("ID_CDROM_MEDIA=1\n"); if (cd_media_mo) printf("ID_CDROM_MEDIA_MO=1\n"); if (cd_media_mrw) @@ -691,7 +707,7 @@ exit: if (fd >= 0) close(fd); udev_unref(udev); - logging_close(); + udev_log_close(); return rc; }