#include <unistd.h>
#include "log.h"
+#include "memory-util.h"
#include "random-util.h"
#include "udev-util.h"
-#include "util.h"
/* device info */
-static unsigned int cd_cd_rom;
-static unsigned int cd_cd_r;
-static unsigned int cd_cd_rw;
-static unsigned int cd_dvd_rom;
-static unsigned int cd_dvd_r;
-static unsigned int cd_dvd_rw;
-static unsigned int cd_dvd_ram;
-static unsigned int cd_dvd_plus_r;
-static unsigned int cd_dvd_plus_rw;
-static unsigned int cd_dvd_plus_r_dl;
-static unsigned int cd_dvd_plus_rw_dl;
-static unsigned int cd_bd;
-static unsigned int cd_bd_r;
-static unsigned int cd_bd_re;
-static unsigned int cd_hddvd;
-static unsigned int cd_hddvd_r;
-static unsigned int cd_hddvd_rw;
-static unsigned int cd_mo;
-static unsigned int cd_mrw;
-static unsigned int cd_mrw_w;
+static unsigned cd_cd_rom;
+static unsigned cd_cd_r;
+static unsigned cd_cd_rw;
+static unsigned cd_dvd_rom;
+static unsigned cd_dvd_r;
+static unsigned cd_dvd_rw;
+static unsigned cd_dvd_ram;
+static unsigned cd_dvd_plus_r;
+static unsigned cd_dvd_plus_rw;
+static unsigned cd_dvd_plus_r_dl;
+static unsigned cd_dvd_plus_rw_dl;
+static unsigned cd_bd;
+static unsigned cd_bd_r;
+static unsigned cd_bd_re;
+static unsigned cd_hddvd;
+static unsigned cd_hddvd_r;
+static unsigned cd_hddvd_rw;
+static unsigned cd_mo;
+static unsigned cd_mrw;
+static unsigned 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;
-static unsigned int cd_media_dvd_rom;
-static unsigned int cd_media_dvd_r;
-static unsigned int cd_media_dvd_rw;
-static unsigned int cd_media_dvd_rw_ro; /* restricted overwrite mode */
-static unsigned int cd_media_dvd_rw_seq; /* sequential mode */
-static unsigned int cd_media_dvd_ram;
-static unsigned int cd_media_dvd_plus_r;
-static unsigned int cd_media_dvd_plus_rw;
-static unsigned int cd_media_dvd_plus_r_dl;
-static unsigned int cd_media_dvd_plus_rw_dl;
-static unsigned int cd_media_bd;
-static unsigned int cd_media_bd_r;
-static unsigned int cd_media_bd_re;
-static unsigned int cd_media_hddvd;
-static unsigned int cd_media_hddvd_r;
-static unsigned int cd_media_hddvd_rw;
-static unsigned int cd_media_mo;
-static unsigned int cd_media_mrw;
-static unsigned int cd_media_mrw_w;
+static unsigned cd_media;
+static unsigned cd_media_cd_rom;
+static unsigned cd_media_cd_r;
+static unsigned cd_media_cd_rw;
+static unsigned cd_media_dvd_rom;
+static unsigned cd_media_dvd_r;
+static unsigned cd_media_dvd_rw;
+static unsigned cd_media_dvd_rw_ro; /* restricted overwrite mode */
+static unsigned cd_media_dvd_rw_seq; /* sequential mode */
+static unsigned cd_media_dvd_ram;
+static unsigned cd_media_dvd_plus_r;
+static unsigned cd_media_dvd_plus_rw;
+static unsigned cd_media_dvd_plus_r_dl;
+static unsigned cd_media_dvd_plus_rw_dl;
+static unsigned cd_media_bd;
+static unsigned cd_media_bd_r;
+static unsigned cd_media_bd_re;
+static unsigned cd_media_hddvd;
+static unsigned cd_media_hddvd_r;
+static unsigned cd_media_hddvd_rw;
+static unsigned cd_media_mo;
+static unsigned cd_media_mrw;
+static unsigned cd_media_mrw_w;
static const char *cd_media_state = NULL;
-static unsigned int cd_media_session_next;
-static unsigned int cd_media_session_count;
-static unsigned int cd_media_track_count;
-static unsigned int cd_media_track_count_data;
-static unsigned int cd_media_track_count_audio;
+static unsigned cd_media_session_next;
+static unsigned cd_media_session_count;
+static unsigned cd_media_track_count;
+static unsigned cd_media_track_count_data;
+static unsigned cd_media_track_count_audio;
static unsigned long long int cd_media_session_last_offset;
#define ERRCODE(s) ((((s)[2] & 0x0F) << 16) | ((s)[12] << 8) | ((s)[13]))
return false;
fp = fopen("/proc/self/mountinfo", "re");
- if (fp == NULL)
+ if (!fp)
return false;
while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
if (makedev(maj, min) == statbuf.st_rdev) {
}
static void info_scsi_cmd_err(const char *cmd, int err) {
- if (err == -1) {
+ if (err == -1)
log_debug("%s failed", cmd);
- return;
- }
- log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd, SK(err), ASC(err), ASCQ(err));
+ else
+ log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd, SK(err), ASC(err), ASCQ(err));
}
struct scsi_cmd {
cmd->sg_io.dxferp = buf;
cmd->sg_io.dxfer_len = bufsize;
cmd->sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
- } else {
+ } else
cmd->sg_io.dxfer_direction = SG_DXFER_NONE;
- }
+
if (ioctl(fd, SG_IO, &cmd->sg_io))
return -1;
scsi_cmd_set(&sc, 4, 0x02);
scsi_cmd_set(&sc, 5, 0);
err = scsi_cmd_run(&sc, fd, NULL, 0);
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("START_STOP_UNIT", err);
return -1;
}
int capability;
capability = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
- if (capability < 0) {
- log_debug("CDROM_GET_CAPABILITY failed");
- return -1;
- }
+ if (capability < 0)
+ return log_debug_errno(errno, "CDROM_GET_CAPABILITY failed");
if (capability & CDC_CD_R)
cd_cd_r = 1;
}
static int cd_media_compat(int fd) {
- if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK) {
- log_debug("CDROM_DRIVE_STATUS != CDS_DISC_OK");
- return -1;
- }
+ if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) != CDS_DISC_OK)
+ return log_debug_errno(errno, "CDROM_DRIVE_STATUS != CDS_DISC_OK");
+
cd_media = 1;
return 0;
}
scsi_cmd_set(&sc, 4, 36);
scsi_cmd_set(&sc, 5, 0);
err = scsi_cmd_run(&sc, fd, inq, 36);
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("INQUIRY", err);
return -1;
}
- if ((inq[0] & 0x1F) != 5) {
- log_debug("not an MMC unit");
- return -1;
- }
+ if ((inq[0] & 0x1F) != 5)
+ 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;
}
static int feature_profiles(const unsigned char *profiles, size_t size) {
- unsigned int i;
+ unsigned i;
for (i = 0; i+4 <= size; i += 4) {
int profile;
scsi_cmd_set(&sc, 8, sizeof(header));
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, header, sizeof(header));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ DISC INFORMATION", err);
if (cd_media == 1) {
log_debug("no current profile, but disc is present; assuming CD-ROM");
cd_media_track_count = 1;
cd_media_track_count_data = 1;
return 0;
- } else {
- log_debug("no current profile, assuming no media");
- return -1;
- }
+ } else
+ return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
+ "no current profile, assuming no media");
};
cd_media = 1;
static int cd_profiles(int fd) {
struct scsi_cmd sc;
unsigned char features[65530];
- unsigned int cur_profile = 0;
- unsigned int len;
- unsigned int i;
+ unsigned cur_profile = 0;
+ unsigned len;
+ unsigned i;
int err;
int ret;
scsi_cmd_set(&sc, 8, 8);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, features, 8);
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("GET CONFIGURATION", err);
/* handle pre-MMC2 drives which do not support GET CONFIGURATION */
if (SK(err) == 0x5 && IN_SET(ASC(err), 0x20, 0x24)) {
log_debug("current profile 0x%02x", cur_profile);
feature_profile_media(cur_profile);
ret = 0; /* we have media */
- } else {
+ } else
log_debug("no current profile, assuming no media");
- }
len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len);
scsi_cmd_set(&sc, 8, len & 0xff);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, features, len);
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("GET CONFIGURATION", err);
return -1;
}
/* device features */
for (i = 8; i+4 < len; i += (4 + features[i+3])) {
- unsigned int feature;
+ unsigned feature;
feature = features[i] << 8 | features[i+1];
static int cd_media_info(int fd) {
struct scsi_cmd sc;
unsigned char header[32];
- static const char *media_status[] = {
+ static const char *const media_status[] = {
"blank",
"appendable",
"complete",
scsi_cmd_set(&sc, 8, sizeof(header) & 0xff);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, header, sizeof(header));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ DISC INFORMATION", err);
return -1;
};
if (!cd_media_cd_rom)
cd_media_state = media_status[header[2] & 3];
- /* fresh DVD-RW in restricted overwite mode reports itself as
+ /* fresh DVD-RW in restricted overwrite mode reports itself as
* "appendable"; change it to "blank" to make it consistent with what
* gets reported after blanking, and what userspace expects */
if (cd_media_dvd_rw_ro && (header[2] & 3) == 1)
scsi_cmd_set(&sc, 9, sizeof(dvdstruct));
scsi_cmd_set(&sc, 11, 0);
err = scsi_cmd_run(&sc, fd, dvdstruct, sizeof(dvdstruct));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ DVD STRUCTURE", err);
return -1;
}
scsi_cmd_set(&sc, 8, sizeof(format));
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, format, sizeof(format));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ DVD FORMAT CAPACITIES", err);
return -1;
}
len = format[3];
- if (len & 7 || len < 16) {
- log_debug("invalid format capacities length");
- return -1;
- }
+ if (len & 7 || len < 16)
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "invalid format capacities length");
switch(format[8] & 3) {
case 1:
case 3:
cd_media = 0; //return no media
- log_debug("format capacities returned no media");
- return -1;
+ return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
+ "format capacities returned no media");
}
}
scsi_cmd_set(&sc, 8, 32);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, buffer, sizeof(buffer));
- if ((err != 0)) {
+ if (err != 0) {
cd_media = 0;
info_scsi_cmd_err("READ FIRST 32 BLOCKS", err);
return -1;
struct scsi_cmd sc;
unsigned char header[12];
unsigned char toc[65536];
- unsigned int len, i, num_tracks;
+ unsigned len, i, num_tracks;
unsigned char *p;
int err;
scsi_cmd_set(&sc, 8, sizeof(header) & 0xff);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, header, sizeof(header));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ TOC", err);
return -1;
}
scsi_cmd_set(&sc, 8, len & 0xff);
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, toc, len);
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ TOC (tracks)", err);
return -1;
}
* the TOC, but also avoid going beyond the TOC length, just in case
* the last track number is invalidly large */
for (p = toc+4, i = 4; i < len-8 && num_tracks > 0; i += 8, p += 8, --num_tracks) {
- unsigned int block;
- unsigned int is_data_track;
+ unsigned block;
+ unsigned is_data_track;
is_data_track = (p[1] & 0x04) != 0;
scsi_cmd_set(&sc, 8, sizeof(header));
scsi_cmd_set(&sc, 9, 0);
err = scsi_cmd_run(&sc, fd, header, sizeof(header));
- if ((err != 0)) {
+ if (err != 0) {
info_scsi_cmd_err("READ TOC (multi session)", err);
return -1;
}
if (cd_media_hddvd_rw)
printf("ID_CDROM_MEDIA_HDDVD_RW=1\n");
- if (cd_media_state != NULL)
+ if (cd_media_state)
printf("ID_CDROM_MEDIA_STATE=%s\n", cd_media_state);
if (cd_media_session_next > 0)
printf("ID_CDROM_MEDIA_SESSION_NEXT=%u\n", cd_media_session_next);