udev/cdrom_id: use random_u64_range() and tweak log messages

https://github.com/systemd/systemd/pull/19317#pullrequestreview-637765222
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2021-04-19 08:37:39 +02:00
parent 6604fb0207
commit 092c05717c

View File

@@ -308,17 +308,16 @@ static int cd_media_compat(Context *c) {
r = ioctl(c->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
if (r < 0)
return log_debug_errno(errno, "ioctl(CDROM_DRIVE_STATUS) failed: m");
if (r != CDS_DISC_OK) {
log_debug("ioctl(CDROM_DRIVE_STATUS) → %d (%s), ignoring",
r,
r == CDS_NO_INFO ? "no info" :
r == CDS_NO_DISC ? "no disc" :
r == CDS_TRAY_OPEN ? "tray open" :
r == CDS_DRIVE_NOT_READY ? "drive not ready" :
"unkown status");
return -ENOMEDIUM;
}
return log_debug_errno(errno, "ioctl(CDROM_DRIVE_STATUS) failed: %m");
if (r != CDS_DISC_OK)
log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
"ioctl(CDROM_DRIVE_STATUS) → %d (%s), ignoring.",
r,
r == CDS_NO_INFO ? "no info" :
r == CDS_NO_DISC ? "no disc" :
r == CDS_TRAY_OPEN ? "tray open" :
r == CDS_DRIVE_NOT_READY ? "drive not ready" :
"unkown status");
c->has_media = true;
return 0;
@@ -340,7 +339,7 @@ static int cd_inquiry(Context *c) {
return r;
if ((inq[0] & 0x1F) != 5)
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Not an MMC unit");
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Not an MMC unit.");
log_debug("INQUIRY: [%.8s][%.16s][%.4s]", inq + 8, inq + 16, inq + 32);
return 0;
@@ -450,7 +449,7 @@ static int cd_profiles(Context *c) {
log_debug("GET CONFIGURATION: size of features buffer %zu", len);
if (len > sizeof(features)) {
log_debug("cannot get features in a single query, truncating");
log_debug("Cannot get features in a single query, truncating.");
len = sizeof(features);
} else if (len <= 8)
len = sizeof(features);
@@ -470,7 +469,7 @@ static int cd_profiles(Context *c) {
log_debug("GET CONFIGURATION: size of features buffer %zu", len);
if (len > sizeof(features)) {
log_debug("cannot get features in a single query, truncating");
log_debug("Cannot get features in a single query, truncating.");
len = sizeof(features);
}
@@ -529,7 +528,7 @@ static int dvd_ram_media_update_state(Context *c) {
if (dvdstruct[4] & 0x02) {
c->media_state = MEDIA_STATE_COMPLETE;
log_debug("write-protected DVD-RAM media inserted");
log_debug("Write-protected DVD-RAM media inserted");
return 1;
}
@@ -545,24 +544,24 @@ static int dvd_ram_media_update_state(Context *c) {
len = format[3];
if (len & 7 || len < 16)
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"invalid format capacities length");
"Invalid format capacities length.");
switch(format[8] & 3) {
case 1:
/* This means that last format was interrupted or failed, blank dvd-ram discs are
* factory formatted. Take no action here as it takes quite a while to reformat a
* dvd-ram and it's not automatically started. */
log_debug("unformatted DVD-RAM media inserted");
log_debug("Unformatted DVD-RAM media inserted.");
return 1;
case 2:
log_debug("formatted DVD-RAM media inserted");
log_debug("Formatted DVD-RAM media inserted.");
return 0;
case 3:
c->has_media = false;
return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
"format capacities returned no media");
"Format capacities returned no media.");
}
return 0;
@@ -599,17 +598,17 @@ static int dvd_media_update_state(Context *c) {
for (size_t offset = 32768; offset < 32768 + 2048; offset++)
if (buffer[offset] != 0) {
log_debug("data in block 16, assuming complete");
log_debug("Data in block 16, assuming complete.");
return 0;
}
for (size_t offset = 0; offset < 2048; offset++)
if (buffer[offset] != 0) {
log_debug("data in block 0, assuming complete");
log_debug("Data in block 0, assuming complete.");
return 0;
}
log_debug("no data in blocks 0 or 16, assuming blank");
log_debug("No data in blocks 0 or 16, assuming blank.");
c->media_state = MEDIA_STATE_BLANK;
return 0;
}
@@ -755,7 +754,7 @@ static int open_drive(Context *c) {
if (++cnt >= 20 || errno != EBUSY)
return log_debug_errno(errno, "Unable to open '%s': %m", arg_node);
(void) usleep(100 * USEC_PER_MSEC + random_u64() % (100 * USEC_PER_MSEC));
(void) usleep(100 * USEC_PER_MSEC + random_u64_range(100 * USEC_PER_MSEC));
}
log_debug("probing: '%s'", arg_node);
@@ -831,7 +830,7 @@ static void print_feature(Feature feature, const char *prefix) {
found = typesafe_bsearch(&in, feature_to_string, ELEMENTSOF(feature_to_string), feature_to_string_compare_func);
if (!found)
return (void) log_debug("Unknown feature 0x%02x, ignoring", (unsigned) feature);
return (void) log_debug("Unknown feature 0x%02x, ignoring.", (unsigned) feature);
printf("%s_%s=1\n", prefix, found->str);
}