X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fudev%2Fcdrom_id%2Fcdrom_id.c;h=5fe3fb535b40bf56405e6671509dc9baba606455;hp=f90d52ec9c487d5193b0c5efff4c4b00daab232e;hb=ef309a681f4c761503e4cd4cc6884d7d6ef70436;hpb=3e2147858f21943d5f4a781c60f33ac22c6096ed diff --git a/src/udev/cdrom_id/cdrom_id.c b/src/udev/cdrom_id/cdrom_id.c index f90d52ec9..5fe3fb535 100644 --- a/src/udev/cdrom_id/cdrom_id.c +++ b/src/udev/cdrom_id/cdrom_id.c @@ -1,7 +1,7 @@ /* * cdrom_id - optical drive and media information prober * - * Copyright (C) 2008-2010 Kay Sievers + * Copyright (C) 2008-2010 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 @@ -17,10 +17,6 @@ * along with this program. If not, see . */ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE 1 -#endif - #include #include #include @@ -41,18 +37,12 @@ #include "libudev.h" #include "libudev-private.h" -static bool debug; - +_printf_(6,0) static void log_fn(struct udev *udev, int priority, const char *file, int line, const char *fn, const char *format, va_list args) { - if (debug) { - fprintf(stderr, "%s: ", fn); - vfprintf(stderr, format, args); - } else { - vsyslog(priority, format, args); - } + log_metav(priority, file, line, fn, format, args); } /* device info */ @@ -125,7 +115,7 @@ static bool is_mounted(const char *device) if (stat(device, &statbuf) < 0) return -ENODEV; - fp = fopen("/proc/self/mountinfo", "r"); + fp = fopen("/proc/self/mountinfo", "re"); if (fp == NULL) return -ENOSYS; while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) { @@ -138,13 +128,13 @@ static bool is_mounted(const char *device) return mounted; } -static void info_scsi_cmd_err(struct udev *udev, char *cmd, int err) +static void info_scsi_cmd_err(struct udev *udev, const char *cmd, int err) { if (err == -1) { - info(udev, "%s failed\n", cmd); + log_debug("%s failed", cmd); return; } - info(udev, "%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err)); + log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd, SK(err), ASC(err), ASCQ(err)); } struct scsi_cmd { @@ -158,7 +148,7 @@ struct scsi_cmd { static void scsi_cmd_init(struct udev *udev, struct scsi_cmd *cmd) { - memset(cmd, 0x00, sizeof(struct scsi_cmd)); + memzero(cmd, sizeof(struct scsi_cmd)); cmd->cgc.quiet = 1; cmd->cgc.sense = &cmd->_sense.s; cmd->sg_io.interface_id = 'S'; @@ -209,11 +199,11 @@ static int media_lock(struct udev *udev, int fd, bool lock) /* disable the kernel's lock logic */ err = ioctl(fd, CDROM_CLEAR_OPTIONS, CDO_LOCK); if (err < 0) - info(udev, "CDROM_CLEAR_OPTIONS, CDO_LOCK failed\n"); + log_debug("CDROM_CLEAR_OPTIONS, CDO_LOCK failed"); err = ioctl(fd, CDROM_LOCKDOOR, lock ? 1 : 0); if (err < 0) - info(udev, "CDROM_LOCKDOOR failed\n"); + log_debug("CDROM_LOCKDOOR failed"); return err; } @@ -241,7 +231,7 @@ static int cd_capability_compat(struct udev *udev, int fd) capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL); if (capability < 0) { - info(udev, "CDROM_GET_CAPABILITY failed\n"); + log_debug("CDROM_GET_CAPABILITY failed"); return -1; } @@ -265,7 +255,7 @@ static int cd_capability_compat(struct udev *udev, int fd) 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"); + log_debug("CDROM_DRIVE_STATUS != CDS_DISC_OK"); return -1; } cd_media = 1; @@ -289,11 +279,11 @@ static int cd_inquiry(struct udev *udev, int fd) } if ((inq[0] & 0x1F) != 5) { - info(udev, "not an MMC unit\n"); + log_debug("not an MMC unit"); return -1; } - info(udev, "INQUIRY: [%.8s][%.16s][%.4s]\n", inq + 8, inq + 16, inq + 32); + log_debug("INQUIRY: [%.8s][%.16s][%.4s]", inq + 8, inq + 16, inq + 32); return 0; } @@ -303,105 +293,105 @@ static void feature_profile_media(struct udev *udev, int cur_profile) case 0x03: case 0x04: case 0x05: - info(udev, "profile 0x%02x \n", cur_profile); + log_debug("profile 0x%02x ", cur_profile); cd_media = 1; cd_media_mo = 1; break; case 0x08: - info(udev, "profile 0x%02x media_cd_rom\n", cur_profile); + log_debug("profile 0x%02x media_cd_rom", cur_profile); cd_media = 1; cd_media_cd_rom = 1; break; case 0x09: - info(udev, "profile 0x%02x media_cd_r\n", cur_profile); + log_debug("profile 0x%02x media_cd_r", cur_profile); cd_media = 1; cd_media_cd_r = 1; break; case 0x0a: - info(udev, "profile 0x%02x media_cd_rw\n", cur_profile); + log_debug("profile 0x%02x media_cd_rw", cur_profile); cd_media = 1; cd_media_cd_rw = 1; break; case 0x10: - info(udev, "profile 0x%02x media_dvd_ro\n", cur_profile); + log_debug("profile 0x%02x media_dvd_ro", cur_profile); cd_media = 1; cd_media_dvd_rom = 1; break; case 0x11: - info(udev, "profile 0x%02x media_dvd_r\n", cur_profile); + log_debug("profile 0x%02x media_dvd_r", cur_profile); cd_media = 1; cd_media_dvd_r = 1; break; case 0x12: - info(udev, "profile 0x%02x media_dvd_ram\n", cur_profile); + log_debug("profile 0x%02x media_dvd_ram", cur_profile); cd_media = 1; cd_media_dvd_ram = 1; break; case 0x13: - info(udev, "profile 0x%02x media_dvd_rw_ro\n", cur_profile); + log_debug("profile 0x%02x media_dvd_rw_ro", cur_profile); cd_media = 1; cd_media_dvd_rw = 1; cd_media_dvd_rw_ro = 1; break; case 0x14: - info(udev, "profile 0x%02x media_dvd_rw_seq\n", cur_profile); + log_debug("profile 0x%02x media_dvd_rw_seq", cur_profile); cd_media = 1; cd_media_dvd_rw = 1; cd_media_dvd_rw_seq = 1; break; case 0x1B: - info(udev, "profile 0x%02x media_dvd_plus_r\n", cur_profile); + log_debug("profile 0x%02x media_dvd_plus_r", cur_profile); cd_media = 1; cd_media_dvd_plus_r = 1; break; case 0x1A: - info(udev, "profile 0x%02x media_dvd_plus_rw\n", cur_profile); + log_debug("profile 0x%02x media_dvd_plus_rw", cur_profile); cd_media = 1; cd_media_dvd_plus_rw = 1; break; case 0x2A: - info(udev, "profile 0x%02x media_dvd_plus_rw_dl\n", cur_profile); + log_debug("profile 0x%02x media_dvd_plus_rw_dl", cur_profile); cd_media = 1; cd_media_dvd_plus_rw_dl = 1; break; case 0x2B: - info(udev, "profile 0x%02x media_dvd_plus_r_dl\n", cur_profile); + log_debug("profile 0x%02x media_dvd_plus_r_dl", cur_profile); cd_media = 1; cd_media_dvd_plus_r_dl = 1; break; case 0x40: - info(udev, "profile 0x%02x media_bd\n", cur_profile); + log_debug("profile 0x%02x media_bd", cur_profile); cd_media = 1; cd_media_bd = 1; break; case 0x41: case 0x42: - info(udev, "profile 0x%02x media_bd_r\n", cur_profile); + log_debug("profile 0x%02x media_bd_r", cur_profile); cd_media = 1; cd_media_bd_r = 1; break; case 0x43: - info(udev, "profile 0x%02x media_bd_re\n", cur_profile); + log_debug("profile 0x%02x media_bd_re", cur_profile); cd_media = 1; cd_media_bd_re = 1; break; case 0x50: - info(udev, "profile 0x%02x media_hddvd\n", cur_profile); + log_debug("profile 0x%02x media_hddvd", cur_profile); cd_media = 1; cd_media_hddvd = 1; break; case 0x51: - info(udev, "profile 0x%02x media_hddvd_r\n", cur_profile); + log_debug("profile 0x%02x media_hddvd_r", cur_profile); cd_media = 1; cd_media_hddvd_r = 1; break; case 0x52: - info(udev, "profile 0x%02x media_hddvd_rw\n", cur_profile); + log_debug("profile 0x%02x media_hddvd_rw", cur_profile); cd_media = 1; cd_media_hddvd_rw = 1; break; default: - info(udev, "profile 0x%02x \n", cur_profile); + log_debug("profile 0x%02x ", cur_profile); break; } } @@ -418,77 +408,77 @@ static int feature_profiles(struct udev *udev, const unsigned char *profiles, si case 0x03: case 0x04: case 0x05: - info(udev, "profile 0x%02x mo\n", profile); + log_debug("profile 0x%02x mo", profile); cd_mo = 1; break; case 0x08: - info(udev, "profile 0x%02x cd_rom\n", profile); + log_debug("profile 0x%02x cd_rom", profile); cd_cd_rom = 1; break; case 0x09: - info(udev, "profile 0x%02x cd_r\n", profile); + log_debug("profile 0x%02x cd_r", profile); cd_cd_r = 1; break; case 0x0A: - info(udev, "profile 0x%02x cd_rw\n", profile); + log_debug("profile 0x%02x cd_rw", profile); cd_cd_rw = 1; break; case 0x10: - info(udev, "profile 0x%02x dvd_rom\n", profile); + log_debug("profile 0x%02x dvd_rom", profile); cd_dvd_rom = 1; break; case 0x12: - info(udev, "profile 0x%02x dvd_ram\n", profile); + log_debug("profile 0x%02x dvd_ram", profile); cd_dvd_ram = 1; break; case 0x13: case 0x14: - info(udev, "profile 0x%02x dvd_rw\n", profile); + log_debug("profile 0x%02x dvd_rw", profile); cd_dvd_rw = 1; break; case 0x1B: - info(udev, "profile 0x%02x dvd_plus_r\n", profile); + log_debug("profile 0x%02x dvd_plus_r", profile); cd_dvd_plus_r = 1; break; case 0x1A: - info(udev, "profile 0x%02x dvd_plus_rw\n", profile); + log_debug("profile 0x%02x dvd_plus_rw", profile); cd_dvd_plus_rw = 1; break; case 0x2A: - info(udev, "profile 0x%02x dvd_plus_rw_dl\n", profile); + log_debug("profile 0x%02x dvd_plus_rw_dl", profile); cd_dvd_plus_rw_dl = 1; break; case 0x2B: - info(udev, "profile 0x%02x dvd_plus_r_dl\n", profile); + log_debug("profile 0x%02x dvd_plus_r_dl", profile); cd_dvd_plus_r_dl = 1; break; case 0x40: cd_bd = 1; - info(udev, "profile 0x%02x bd\n", profile); + log_debug("profile 0x%02x bd", profile); break; case 0x41: case 0x42: cd_bd_r = 1; - info(udev, "profile 0x%02x bd_r\n", profile); + log_debug("profile 0x%02x bd_r", profile); break; case 0x43: cd_bd_re = 1; - info(udev, "profile 0x%02x bd_re\n", profile); + log_debug("profile 0x%02x bd_re", profile); break; case 0x50: cd_hddvd = 1; - info(udev, "profile 0x%02x hddvd\n", profile); + log_debug("profile 0x%02x hddvd", profile); break; case 0x51: cd_hddvd_r = 1; - info(udev, "profile 0x%02x hddvd_r\n", profile); + log_debug("profile 0x%02x hddvd_r", profile); break; case 0x52: cd_hddvd_rw = 1; - info(udev, "profile 0x%02x hddvd_rw\n", profile); + log_debug("profile 0x%02x hddvd_rw", profile); break; default: - info(udev, "profile 0x%02x \n", profile); + log_debug("profile 0x%02x ", profile); break; } } @@ -511,11 +501,13 @@ static int cd_profiles_old_mmc(struct udev *udev, int fd) if ((err != 0)) { info_scsi_cmd_err(udev, "READ DISC INFORMATION", err); if (cd_media == 1) { - info(udev, "no current profile, but disc is present; assuming CD-ROM\n"); + log_debug("no current profile, but disc is present; assuming CD-ROM"); cd_media_cd_rom = 1; + cd_media_track_count = 1; + cd_media_track_count_data = 1; return 0; } else { - info(udev, "no current profile, assuming no media\n"); + log_debug("no current profile, assuming no media"); return -1; } }; @@ -524,13 +516,13 @@ static int cd_profiles_old_mmc(struct udev *udev, int fd) if (header[2] & 16) { cd_media_cd_rw = 1; - info(udev, "profile 0x0a media_cd_rw\n"); + log_debug("profile 0x0a media_cd_rw"); } else if ((header[2] & 3) < 2 && cd_cd_r) { cd_media_cd_r = 1; - info(udev, "profile 0x09 media_cd_r\n"); + log_debug("profile 0x09 media_cd_r"); } else { cd_media_cd_rom = 1; - info(udev, "profile 0x08 media_cd_rom\n"); + log_debug("profile 0x08 media_cd_rom"); } return 0; } @@ -557,9 +549,9 @@ static int cd_profiles(struct udev *udev, int fd) if ((err != 0)) { info_scsi_cmd_err(udev, "GET CONFIGURATION", err); /* handle pre-MMC2 drives which do not support GET CONFIGURATION */ - if (SK(err) == 0x5 && ASC(err) == 0x20) { - info(udev, "drive is pre-MMC2 and does not support 46h get configuration command\n"); - info(udev, "trying to work around the problem\n"); + if (SK(err) == 0x5 && (ASC(err) == 0x20 || ASC(err) == 0x24)) { + log_debug("drive is pre-MMC2 and does not support 46h get configuration command"); + log_debug("trying to work around the problem"); ret = cd_profiles_old_mmc(udev, fd); } goto out; @@ -567,18 +559,18 @@ static int cd_profiles(struct udev *udev, int fd) cur_profile = features[6] << 8 | features[7]; if (cur_profile > 0) { - info(udev, "current profile 0x%02x\n", cur_profile); + log_debug("current profile 0x%02x", cur_profile); feature_profile_media (udev, cur_profile); ret = 0; /* we have media */ } else { - info(udev, "no current profile, assuming no media\n"); + log_debug("no current profile, assuming no media"); } len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3]; - info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len); + log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len); if (len > sizeof(features)) { - info(udev, "can not get features in a single query, truncating\n"); + log_debug("can not get features in a single query, truncating"); len = sizeof(features); } else if (len <= 8) { len = sizeof(features); @@ -598,10 +590,10 @@ static int cd_profiles(struct udev *udev, int fd) /* parse the length once more, in case the drive decided to have other features suddenly :) */ len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3]; - info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len); + log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len); if (len > sizeof(features)) { - info(udev, "can not get features in a single query, truncating\n"); + log_debug("can not get features in a single query, truncating"); len = sizeof(features); } @@ -613,11 +605,11 @@ static int cd_profiles(struct udev *udev, int fd) switch (feature) { case 0x00: - info(udev, "GET CONFIGURATION: feature 'profiles', with %i entries\n", features[i+3] / 4); - feature_profiles(udev, &features[i]+4, features[i+3]); + log_debug("GET CONFIGURATION: feature 'profiles', with %i entries", features[i+3] / 4); + feature_profiles(udev, &features[i]+4, MIN(features[i+3], len - i - 4)); break; default: - info(udev, "GET CONFIGURATION: feature 0x%04x , with 0x%02x bytes\n", feature, features[i+3]); + log_debug("GET CONFIGURATION: feature 0x%04x , with 0x%02x bytes", feature, features[i+3]); break; } } @@ -648,8 +640,8 @@ static int cd_media_info(struct udev *udev, int fd) }; cd_media = 1; - info(udev, "disk type %02x\n", header[8]); - info(udev, "hardware reported media status: %s\n", media_status[header[2] & 3]); + log_debug("disk type %02x", header[8]); + log_debug("hardware reported media status: %s", media_status[header[2] & 3]); /* exclude plain CDROM, some fake cdroms return 0 for "blank" media here */ if (!cd_media_cd_rom) @@ -687,7 +679,7 @@ static int cd_media_info(struct udev *udev, int fd) } if (dvdstruct[4] & 0x02) { cd_media_state = media_status[2]; - info(udev, "write-protected DVD-RAM media inserted\n"); + log_debug("write-protected DVD-RAM media inserted"); goto determined; } @@ -704,13 +696,13 @@ static int cd_media_info(struct udev *udev, int fd) len = format[3]; if (len & 7 || len < 16) { - info(udev, "invalid format capacities length\n"); + log_debug("invalid format capacities length"); return -1; } switch(format[8] & 3) { case 1: - info(udev, "unformatted DVD-RAM media inserted\n"); + log_debug("unformatted DVD-RAM media inserted"); /* This means that last format was interrupted * or failed, blank dvd-ram discs are factory * formatted. Take no action here as it takes @@ -719,12 +711,12 @@ static int cd_media_info(struct udev *udev, int fd) goto determined; case 2: - info(udev, "formatted DVD-RAM media inserted\n"); + log_debug("formatted DVD-RAM media inserted"); break; case 3: cd_media = 0; //return no media - info(udev, "format capacities returned no media\n"); + log_debug("format capacities returned no media"); return -1; } } @@ -760,9 +752,9 @@ static int cd_media_info(struct udev *udev, int fd) if (!result) { cd_media_state = media_status[0]; - info(udev, "no data in blocks 0 or 16, assuming blank\n"); + log_debug("no data in blocks 0 or 16, assuming blank"); } else { - info(udev, "data in blocks 0 or 16, assuming complete\n"); + log_debug("data in blocks 0 or 16, assuming complete"); } } @@ -798,7 +790,7 @@ static int cd_media_toc(struct udev *udev, int fd) } len = (header[0] << 8 | header[1]) + 2; - info(udev, "READ TOC: len: %d, start track: %d, end track: %d\n", len, header[2], header[3]); + log_debug("READ TOC: len: %d, start track: %d, end track: %d", len, header[2], header[3]); if (len > sizeof(toc)) return -1; if (len < 2) @@ -832,7 +824,7 @@ static int cd_media_toc(struct udev *udev, int fd) is_data_track = (p[1] & 0x04) != 0; block = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7]; - info(udev, "track=%u info=0x%x(%s) start_block=%u\n", + log_debug("track=%u info=0x%x(%s) start_block=%u", p[2], p[1] & 0x0f, is_data_track ? "data":"audio", block); if (is_data_track) @@ -852,7 +844,7 @@ static int cd_media_toc(struct udev *udev, int fd) return -1; } len = header[4+4] << 24 | header[4+5] << 16 | header[4+6] << 8 | header[4+7]; - info(udev, "last track %u starts at block %u\n", header[4+2], len); + log_debug("last track %u starts at block %u", header[4+2], len); cd_media_session_last_offset = (unsigned long long int)len * 2048; return 0; } @@ -876,11 +868,13 @@ int main(int argc, char *argv[]) int cnt; int rc = 0; + log_parse_environment(); + log_open(); + udev = udev_new(); if (udev == NULL) goto exit; - udev_log_init("cdrom_id"); udev_set_log_fn(udev, log_fn); while (1) { @@ -901,9 +895,10 @@ int main(int argc, char *argv[]) eject = true; break; case 'd': - debug = true; - if (udev_get_log_priority(udev) < LOG_INFO) - udev_set_log_priority(udev, LOG_INFO); + log_set_target(LOG_TARGET_CONSOLE); + log_set_max_level(LOG_DEBUG); + udev_set_log_priority(udev, LOG_DEBUG); + log_open(); break; case 'h': printf("Usage: cdrom_id [options] \n" @@ -921,17 +916,17 @@ int main(int argc, char *argv[]) node = argv[optind]; if (!node) { - err(udev, "no device\n"); + log_error("no device"); fprintf(stderr, "no device\n"); rc = 1; goto exit; } - srand((unsigned int)getpid()); + initialize_srand(); for (cnt = 20; cnt > 0; cnt--) { struct timespec duration; - fd = open(node, O_RDONLY|O_NONBLOCK|(is_mounted(node) ? 0 : O_EXCL)); + fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC|(is_mounted(node) ? 0 : O_EXCL)); if (fd >= 0 || errno != EBUSY) break; duration.tv_sec = 0; @@ -939,12 +934,12 @@ int main(int argc, char *argv[]) nanosleep(&duration, NULL); } if (fd < 0) { - info(udev, "unable to open '%s'\n", node); + log_debug("unable to open '%s'", node); fprintf(stderr, "unable to open '%s'\n", node); rc = 1; goto exit; } - info(udev, "probing: '%s'\n", node); + log_debug("probing: '%s'", node); /* same data as original cdrom_id */ if (cd_capability_compat(udev, fd) < 0) { @@ -975,19 +970,19 @@ int main(int argc, char *argv[]) work: /* lock the media, so we enable eject button events */ if (lock && cd_media) { - info(udev, "PREVENT_ALLOW_MEDIUM_REMOVAL (lock)\n"); + log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (lock)"); media_lock(udev, fd, true); } if (unlock && cd_media) { - info(udev, "PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)\n"); + log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)"); media_lock(udev, fd, false); } if (eject) { - info(udev, "PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)\n"); + log_debug("PREVENT_ALLOW_MEDIUM_REMOVAL (unlock)"); media_lock(udev, fd, false); - info(udev, "START_STOP_UNIT (eject)\n"); + log_debug("START_STOP_UNIT (eject)"); media_eject(udev, fd); } @@ -1094,6 +1089,6 @@ exit: if (fd >= 0) close(fd); udev_unref(udev); - udev_log_close(); + log_close(); return rc; }