You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2523 lines
69 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. /* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */
  2. /* Copyright (c) 2004 - 2006 Derek Foreman, Ben Jansens
  3. Copyright (c) 2006 - 2014 Thomas Schmitt <scdbackup@gmx.net>
  4. Provided under GPL version 2 or later.
  5. */
  6. /* <<< ts A91112 : experiments to get better speed with USB
  7. #define Libburn_sgio_as_growisofS 1
  8. */
  9. /*
  10. This is the main operating system dependent SCSI part of libburn. It implements
  11. the transport level aspects of SCSI control and command i/o.
  12. Present implementation: GNU/Linux SCSI Generic (sg)
  13. PORTING:
  14. Porting libburn typically will consist of adding a new operating system case
  15. to the following switcher files:
  16. os.h Operating system specific libburn definitions and declarations.
  17. sg.c Operating system dependent transport level modules.
  18. and of deriving the following system specific files from existing examples:
  19. os-*.h Included by os.h. You will need some general system knowledge
  20. about signals and knowledge about the storage object needs of your
  21. transport level module sg-*.c.
  22. sg-*.c This source module. You will need special system knowledge about
  23. how to detect all potentially available drives, how to open them,
  24. eventually how to exclusively reserve them, how to perform
  25. SCSI transactions, how to inquire the (pseudo-)SCSI driver.
  26. You will not need to care about CD burning, MMC or other high-level
  27. SCSI aspects.
  28. Said sg-*.c operations are defined by a public function interface, which has
  29. to be implemented in a way that provides libburn with the desired services:
  30. sg_id_string() returns an id string of the SCSI transport adapter.
  31. It may be called before initialization but then may
  32. return only a preliminary id.
  33. sg_initialize() performs global initialization of the SCSI transport
  34. adapter and eventually needed operating system
  35. facilities. Checks for compatibility of supporting
  36. software components.
  37. sg_shutdown() performs global finalizations and releases golbally
  38. acquired resources.
  39. sg_give_next_adr() iterates over the set of potentially useful drive
  40. address strings.
  41. scsi_enumerate_drives() brings all available, not-whitelist-banned, and
  42. accessible drives into libburn's list of drives.
  43. sg_dispose_drive() finalizes adapter specifics of struct burn_drive
  44. on destruction. Releases resources which were acquired
  45. underneath scsi_enumerate_drives().
  46. sg_drive_is_open() tells wether libburn has the given drive in use.
  47. sg_grab() opens the drive for SCSI commands and ensures
  48. undisturbed access.
  49. sg_release() closes a drive opened by sg_grab()
  50. sg_issue_command() sends a SCSI command to the drive, receives reply,
  51. and evaluates wether the command succeeded or shall
  52. be retried or finally failed.
  53. sg_obtain_scsi_adr() tries to obtain SCSI address parameters.
  54. burn_os_is_2k_seekrw() tells whether the given path leads to a file object
  55. that can be used in 2 kB granularity by lseek(2) and
  56. read(2), and possibly write(2) if not read-only.
  57. E.g. a USB stick or a hard disk.
  58. burn_os_stdio_capacity() estimates the emulated media space of stdio-drives.
  59. burn_os_open_track_src() opens a disk file in a way that offers best
  60. throughput with file reading and/or SCSI write command
  61. transmission.
  62. burn_os_alloc_buffer() allocates a memory area that is suitable for file
  63. descriptors issued by burn_os_open_track_src().
  64. The buffer size may be rounded up for alignment
  65. reasons.
  66. burn_os_free_buffer() delete a buffer obtained by burn_os_alloc_buffer().
  67. Porting hints are marked by the text "PORTING:".
  68. Send feedback to libburn-hackers@pykix.org .
  69. Hint: You should also look into sg-freebsd-port.c, which is a younger and
  70. in some aspects more straightforward implementation of this interface.
  71. */
  72. #ifdef HAVE_CONFIG_H
  73. #include "../config.h"
  74. #endif
  75. /** PORTING : ------- OS dependent headers and definitions ------ */
  76. #ifdef Libburn_read_o_direcT
  77. # ifndef _GNU_SOURCE
  78. # define _GNU_SOURCE
  79. # endif
  80. #endif /* Libburn_read_o_direcT */
  81. #include <errno.h>
  82. #include <unistd.h>
  83. #include <stdio.h>
  84. #include <sys/types.h>
  85. #include <sys/stat.h>
  86. #include <fcntl.h>
  87. #include <sys/ioctl.h>
  88. #include <string.h>
  89. #include <sys/poll.h>
  90. #include <linux/hdreg.h>
  91. #include <stdlib.h>
  92. #include <sys/utsname.h>
  93. #include <scsi/scsi.h>
  94. #include <sys/statvfs.h>
  95. /* for ioctl(BLKGETSIZE) */
  96. #include <linux/fs.h>
  97. /* for mmap() */
  98. #include <sys/mman.h>
  99. #include <scsi/sg.h>
  100. /* Values within sg_io_hdr_t indicating success after ioctl(SG_IO) : */
  101. /* .host_status : from http://tldp.org/HOWTO/SCSI-Generic-HOWTO/x291.html */
  102. #define Libburn_sg_host_oK 0
  103. /* .driver_status : from http://tldp.org/HOWTO/SCSI-Generic-HOWTO/x322.html */
  104. #define Libburn_sg_driver_oK 0
  105. /* ts A61211 : to eventually recognize CD devices on /dev/sr* */
  106. #include <limits.h>
  107. #include <linux/cdrom.h>
  108. /** Indication of the Linux kernel this software is running on */
  109. /* -1 = not evaluated , 0 = unrecognizable , 1 = 2.4 , 2 = 2.6 */
  110. static int sg_kernel_age = -1;
  111. /** PORTING : Device file families for bus scanning and drive access.
  112. Both device families must support the following ioctls:
  113. SG_IO,
  114. SG_GET_SCSI_ID
  115. SCSI_IOCTL_GET_BUS_NUMBER
  116. SCSI_IOCTL_GET_IDLUN
  117. as well as mutual exclusively locking with open(O_EXCL).
  118. If a device family is left empty, then it will not be used.
  119. To avoid misunderstandings: both families are used via identical
  120. transport methods as soon as a device file is accepted as CD drive
  121. by the family specific function <family>_enumerate().
  122. One difference remains throughout usage: Host,Channel,Id,Lun and Bus
  123. address parameters of ATA devices are considered invalid.
  124. */
  125. /* Set this to 1 in order to get on stderr messages from sg_enumerate() */
  126. static int linux_sg_enumerate_debug = 0;
  127. /* The device file family to use for (emulated) generic SCSI transport.
  128. This must be a printf formatter with one single placeholder for int
  129. in the range of 0 to 31 . The resulting addresses must provide SCSI
  130. address parameters Host, Channel, Id, Lun and also Bus.
  131. E.g.: "/dev/sg%d"
  132. sr%d is supposed to map only CD-ROM style devices. Additionally a test
  133. with ioctl(CDROM_DRIVE_STATUS) is made to assert that it is such a drive,
  134. If no such assertion is made, then this adapter performs INQUIRE and
  135. looks for first reply byte 0x05.
  136. This initial setting may be overridden in sg_select_device_family() by
  137. settings made via burn_preset_device_open().
  138. */
  139. static char linux_sg_device_family[80] = {"/dev/sg%d"};
  140. /* Set this to 1 if you want the default linux_sg_device_family chosen
  141. depending on kernel release: sg for <2.6 , sr for >=2.6
  142. */
  143. static int linux_sg_auto_family = 1;
  144. /* Set this to 1 in order to accept any TYPE_* (see scsi/scsi.h) */
  145. /* But try with 0 first. There is hope via CDROM_DRIVE_STATUS. */
  146. /* !!! DO NOT SET TO 1 UNLESS YOU PROTECTED ALL INDISPENSABLE DEVICES
  147. chmod -rw !!! */
  148. static int linux_sg_accept_any_type = 0;
  149. /* The device file family to use for SCSI transport over ATA.
  150. This must be a printf formatter with one single placeholder for a
  151. _single_ char in the range of 'a' to 'z'. This placeholder _must_ be
  152. at the end of the formatter string.
  153. E.g. "/dev/hd%c"
  154. */
  155. static char linux_ata_device_family[80] = {"/dev/hd%c"};
  156. /* Set this to 1 in order to get on stderr messages from ata_enumerate()
  157. */
  158. static int linux_ata_enumerate_verbous = 0;
  159. /** PORTING : ------ libburn portable headers and definitions ----- */
  160. #include "libburn.h"
  161. #include "transport.h"
  162. #include "drive.h"
  163. #include "sg.h"
  164. #include "spc.h"
  165. #include "mmc.h"
  166. #include "sbc.h"
  167. #include "debug.h"
  168. #include "toc.h"
  169. #include "util.h"
  170. #include "init.h"
  171. #include "libdax_msgs.h"
  172. extern struct libdax_msgs *libdax_messenger;
  173. /* ts A51221 */
  174. int burn_drive_is_banned(char *device_address);
  175. /* ------------------------------------------------------------------------ */
  176. /* PORTING: Private definitions. Port only if needed by public functions. */
  177. /* (Public functions are listed below) */
  178. /* ------------------------------------------------------------------------ */
  179. static void enumerate_common(char *fname, int fd_in, int bus_no, int host_no,
  180. int channel_no, int target_no, int lun_no);
  181. static int sg_obtain_scsi_adr_fd(char *path, int fd_in,
  182. int *bus_no, int *host_no, int *channel_no,
  183. int *target_no, int *lun_no);
  184. /* ts A60813 : storage objects are in libburn/init.c
  185. whether to use O_EXCL with open(2) of devices
  186. whether to use fcntl(,F_SETLK,) after open(2) of devices
  187. what device family to use : 0=default, 1=sr, 2=scd, (3=st), 4=sg
  188. whether to use O_NOBLOCK with open(2) on devices
  189. whether to take O_EXCL rejection as fatal error
  190. */
  191. extern int burn_sg_open_o_excl;
  192. extern int burn_sg_fcntl_f_setlk;
  193. extern int burn_sg_use_family;
  194. extern int burn_sg_open_o_nonblock;
  195. extern int burn_sg_open_abort_busy;
  196. /* ts A91111 :
  197. whether to log SCSI commands:
  198. bit0= log in /tmp/libburn_sg_command_log
  199. bit1= log to stderr
  200. bit2= flush every line
  201. */
  202. extern int burn_sg_log_scsi;
  203. /* ts A60821
  204. debug: for tracing calls which might use open drive fds
  205. or for catching SCSI usage of emulated drives. */
  206. int mmc_function_spy(struct burn_drive *d, char * text);
  207. /* ------------------------------------------------------------------------ */
  208. /* PORTING: Private functions. Port only if needed by public functions */
  209. /* (Public functions are listed below) */
  210. /* ------------------------------------------------------------------------ */
  211. /* ts A70413 */
  212. /* This finds out wether the software is running on kernel >= 2.6
  213. */
  214. static void sg_evaluate_kernel(void)
  215. {
  216. struct utsname buf;
  217. if (sg_kernel_age >= 0)
  218. return;
  219. sg_kernel_age = 0;
  220. if (uname(&buf) == -1)
  221. return;
  222. sg_kernel_age = 1;
  223. if (strcmp(buf.release, "2.6") >= 0)
  224. sg_kernel_age = 2;
  225. }
  226. /* ts A70314 */
  227. /* This installs the device file family if one was chosen explicitely
  228. by burn_preset_device_open()
  229. */
  230. static void sg_select_device_family(void)
  231. {
  232. /* >>> ??? do we need a mutex here ? */
  233. /* >>> (It might be concurrent but is supposed to have always
  234. the same effect. Any race condition should be harmless.) */
  235. if (burn_sg_use_family == 1)
  236. strcpy(linux_sg_device_family, "/dev/sr%d");
  237. else if (burn_sg_use_family == 2)
  238. strcpy(linux_sg_device_family, "/dev/scd%d");
  239. else if (burn_sg_use_family == 3)
  240. strcpy(linux_sg_device_family, "/dev/st%d");
  241. else if (burn_sg_use_family == 4)
  242. strcpy(linux_sg_device_family, "/dev/sg%d");
  243. else if (linux_sg_auto_family) {
  244. sg_evaluate_kernel();
  245. if (sg_kernel_age >= 2)
  246. strcpy(linux_sg_device_family, "/dev/sr%d");
  247. else
  248. strcpy(linux_sg_device_family, "/dev/sg%d");
  249. linux_sg_auto_family = 0;
  250. }
  251. }
  252. /* ts A80701 */
  253. /* This cares for the case that no /dev/srNN but only /dev/scdNN exists.
  254. A theoretical case which has its complement in SuSE 10.2 having
  255. /dev/sr but not /dev/scd.
  256. */
  257. static int sg_exchange_scd_for_sr(char *fname, int flag)
  258. {
  259. struct stat stbuf;
  260. char scd[17], *msg = NULL;
  261. if (burn_sg_use_family != 0 || strncmp(fname, "/dev/sr", 7)!=0 ||
  262. strlen(fname)>9 || strlen(fname)<8)
  263. return 2;
  264. if (fname[7] < '0' || fname[7] > '9')
  265. return 2;
  266. if (fname [8] != 0 && (fname[7] < '0' || fname[7] > '9'))
  267. return 2;
  268. if (stat(fname, &stbuf) != -1)
  269. return 2;
  270. strcpy(scd, "/dev/scd");
  271. strcpy(scd + 8, fname + 7);
  272. if (stat(scd, &stbuf) == -1)
  273. return 2;
  274. msg = calloc(strlen(scd) + strlen(fname) + 80, 1);
  275. if (msg != NULL) {
  276. sprintf(msg, "%s substitutes for non-existent %s", scd, fname);
  277. libdax_msgs_submit(libdax_messenger, -1, 0x00000002,
  278. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  279. msg, 0, 0);
  280. free(msg);
  281. }
  282. strcpy(fname, scd);
  283. return 1;
  284. }
  285. /* ts B11110 */
  286. /* This is an early stage version of scsi_log_cmd.
  287. >>> It will become obsolete when the /tmp file handler is moved into
  288. >>> scsi_log_command().
  289. */
  290. static int sgio_log_cmd(unsigned char *cmd, int cmd_len, FILE *fp_in, int flag)
  291. {
  292. FILE *fp = fp_in;
  293. int ret = 0;
  294. /* >>> ts B11110 : move this into scsi_log_command() */
  295. if (fp == NULL && (burn_sg_log_scsi & 1)) {
  296. fp= fopen("/tmp/libburn_sg_command_log", "a");
  297. if (fp != NULL)
  298. fprintf(fp,
  299. "\n=========================================\n");
  300. }
  301. if (fp != NULL)
  302. ret = scsi_log_command(cmd, cmd_len, NO_TRANSFER, NULL, 0,
  303. fp, flag);
  304. if (fp_in == NULL && fp != NULL)
  305. fclose(fp);
  306. return ret;
  307. }
  308. /* ts B11110 */
  309. static int sgio_log_reply(unsigned char *opcode, int data_dir,
  310. unsigned char *data, int dxfer_len,
  311. void *fp_in, unsigned char sense[18], int sense_len,
  312. double duration, int flag)
  313. {
  314. int ret;
  315. ret = scsi_log_reply(opcode, data_dir, data, dxfer_len, fp_in,
  316. sense, sense_len, duration, flag);
  317. return ret;
  318. }
  319. static int sgio_test(int fd)
  320. {
  321. unsigned char test_ops[] = { 0, 0, 0, 0, 0, 0 };
  322. sg_io_hdr_t s;
  323. int ret;
  324. double c_start_time, c_end_time;
  325. memset(&s, 0, sizeof(sg_io_hdr_t));
  326. s.interface_id = 'S';
  327. s.dxfer_direction = SG_DXFER_NONE;
  328. s.cmd_len = 6;
  329. s.cmdp = test_ops;
  330. s.timeout = 12345;
  331. sgio_log_cmd(s.cmdp, s.cmd_len, NULL, 0);
  332. c_start_time = burn_get_time(0);
  333. ret= ioctl(fd, SG_IO, &s);
  334. c_end_time = burn_get_time(0);
  335. sgio_log_reply(s.cmdp, NO_TRANSFER, NULL, 0, NULL,
  336. (unsigned char *) (s.sbp),
  337. s.sb_len_wr, c_end_time - c_start_time, 0);
  338. return ret;
  339. }
  340. static int sgio_inquiry_cd_drive(int fd, char *fname)
  341. {
  342. unsigned char test_ops[] = { 0x12, 0, 0, 0, 36, 0 };
  343. sg_io_hdr_t s;
  344. struct buffer *buf = NULL;
  345. unsigned char *sense = NULL;
  346. char *msg = NULL, *msg_pt;
  347. int ret = 0, i;
  348. double c_start_time, c_end_time;
  349. BURN_ALLOC_MEM(buf, struct buffer, 1);
  350. BURN_ALLOC_MEM(sense, unsigned char, 128);
  351. BURN_ALLOC_MEM(msg, char, strlen(fname) + 1024);
  352. memset(&s, 0, sizeof(sg_io_hdr_t));
  353. s.interface_id = 'S';
  354. s.dxfer_direction = SG_DXFER_FROM_DEV;
  355. s.cmd_len = 6;
  356. s.cmdp = test_ops;
  357. s.mx_sb_len = 32;
  358. s.sbp = sense;
  359. s.timeout = 30000;
  360. s.dxferp = buf;
  361. s.dxfer_len = 36;
  362. s.usr_ptr = NULL;
  363. sgio_log_cmd(s.cmdp, s.cmd_len, NULL, 0);
  364. c_start_time = burn_get_time(0);
  365. ret = ioctl(fd, SG_IO, &s);
  366. c_end_time = burn_get_time(0);
  367. if (ret == -1) {
  368. sprintf(msg,
  369. "INQUIRY on '%s' : ioctl(SG_IO) failed , errno= %d",
  370. fname, errno);
  371. libdax_msgs_submit(libdax_messenger, -1, 0x00000002,
  372. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  373. msg, 0, 0);
  374. goto ex;
  375. }
  376. sgio_log_reply(s.cmdp, FROM_DRIVE, buf->data, s.dxfer_len, NULL,
  377. (unsigned char *) (s.sbp),
  378. s.sb_len_wr, c_end_time - c_start_time, 0);
  379. if (s.sb_len_wr > 0 || s.host_status != Libburn_sg_host_oK ||
  380. s.driver_status != Libburn_sg_driver_oK) {
  381. sprintf(msg, "INQUIRY failed on '%s' : host_status= %hd , driver_status= %hd", fname, s.host_status, s.driver_status);
  382. if (s.sb_len_wr > 0) {
  383. sprintf(msg + strlen(msg), " , sense data=");
  384. msg_pt = msg + strlen(msg);
  385. for (i = 0 ; i < s.sb_len_wr; i++)
  386. sprintf(msg_pt + i * 3, " %2.2X",
  387. ((unsigned char *) (s.sbp))[i]);
  388. }
  389. libdax_msgs_submit(libdax_messenger, -1, 0x00000002,
  390. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  391. msg, 0, 0);
  392. ret = -1;
  393. goto ex;
  394. }
  395. ret = 0;
  396. if (buf->data[0] == 0x5) {
  397. /* Peripheral qualifier 0, device type 0x5 = CD/DVD device.
  398. SPC-3 tables 82 and 83 */
  399. ret = 1;
  400. } else {
  401. sprintf(msg, "INQUIRY on '%s' : byte 0 = 0x%2.2X",
  402. fname, buf->data[0]);
  403. libdax_msgs_submit(libdax_messenger, -1, 0x00000002,
  404. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  405. msg, 0, 0);
  406. }
  407. ex:;
  408. BURN_FREE_MEM(msg);
  409. BURN_FREE_MEM(sense);
  410. BURN_FREE_MEM(buf);
  411. return ret;
  412. }
  413. /* ts A60924 */
  414. static int sg_handle_busy_device(char *fname, int os_errno)
  415. {
  416. char *msg = NULL;
  417. struct stat stbuf;
  418. int looks_like_hd= 0, fd, ret;
  419. BURN_ALLOC_MEM(msg, char, 4096);
  420. /* ts A80713 :
  421. check existence of /dev/hdX1 as hint for hard disk rather than CD
  422. Hint by Giulio Orsero: check /proc/ide/hdX/media for "disk"
  423. */
  424. if (strncmp(fname, "/dev/hd", 7)==0) {
  425. sprintf(msg, "%s1", fname);
  426. if (stat(msg, &stbuf) != -1)
  427. looks_like_hd= 1;
  428. sprintf(msg, "/proc/ide/hd%c/media", fname[7]);
  429. fd = open(msg, O_RDONLY);
  430. if (fd != -1) {
  431. ret = read(fd, msg, 10);
  432. if (ret < 0)
  433. ret = 0;
  434. msg[ret]= 0;
  435. close(fd);
  436. if (strncmp(msg, "disk\n", 5) == 0 ||
  437. strcmp(msg, "disk") == 0)
  438. looks_like_hd= 2;
  439. else if (strncmp(msg, "cdrom\n", 6) == 0 ||
  440. strcmp(msg, "cdrom") == 0)
  441. looks_like_hd= 0;
  442. }
  443. }
  444. /* ts A60814 : i saw no way to do this more nicely */
  445. if (burn_sg_open_abort_busy) {
  446. fprintf(stderr,
  447. "\nlibburn: FATAL : Application triggered abort on busy device '%s'\n",
  448. fname);
  449. /* ts A61007 */
  450. abort();
  451. /* a ssert("drive busy" == "non fatal"); */
  452. }
  453. /* ts A60924 : now reporting to libdax_msgs */
  454. if (looks_like_hd == 2) { /* is surely hard disk */
  455. ;
  456. } else if (looks_like_hd) {
  457. sprintf(msg, "Could not examine busy device '%s'", fname);
  458. libdax_msgs_submit(libdax_messenger, -1, 0x0002015a,
  459. LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_LOW,
  460. msg, os_errno, 0);
  461. sprintf(msg,
  462. "Busy '%s' seems to be a hard disk, as '%s1' exists. But better check.",
  463. fname, fname);
  464. libdax_msgs_submit(libdax_messenger, -1, 0x0002015b,
  465. LIBDAX_MSGS_SEV_HINT, LIBDAX_MSGS_PRIO_LOW,
  466. msg, 0, 0);
  467. } else {
  468. sprintf(msg, "Cannot open busy device '%s'", fname);
  469. libdax_msgs_submit(libdax_messenger, -1, 0x00020001,
  470. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_LOW,
  471. msg, os_errno, 0);
  472. }
  473. ret = 1;
  474. ex:;
  475. BURN_FREE_MEM(msg);
  476. return ret;
  477. }
  478. /* ts A60925 : ticket 74 */
  479. static int sg_close_drive_fd(char *fname, int driveno, int *fd, int sorry)
  480. {
  481. int ret, os_errno, sevno= LIBDAX_MSGS_SEV_DEBUG;
  482. char *msg = NULL;
  483. if(*fd < 0)
  484. {ret = 0; goto ex;}
  485. BURN_ALLOC_MEM(msg, char, 4096 + 100);
  486. #ifdef CDROM_MEDIA_CHANGED_disabled_because_not_helpful
  487. #ifdef CDSL_CURRENT
  488. /* ts A80217 : wondering whether the os knows about our activities */
  489. ret = ioctl(*fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
  490. sprintf(msg, "ioctl(CDROM_MEDIA_CHANGED) == %d", ret);
  491. libdax_msgs_submit(libdax_messenger, driveno, 0x00000002,
  492. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH, msg, 0, 0);
  493. #ifdef BLKFLSBUF_disabled_because_not_helpful
  494. ret = ioctl(*fd, BLKFLSBUF, 0);
  495. sprintf(msg, "ioctl(BLKFLSBUF) == %d", ret);
  496. os_errno = 0;
  497. if(ret == -1)
  498. os_errno = errno;
  499. libdax_msgs_submit(libdax_messenger, driveno, 0x00000002,
  500. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH, msg, os_errno,0);
  501. #endif /* BLKFLSBUF */
  502. #endif /* CDSL_CURRENT */
  503. #endif /* CDROM_MEDIA_CHANGED */
  504. ret = close(*fd);
  505. *fd = -1337;
  506. if(ret != -1) {
  507. /* ts A70409 : DDLP-B */
  508. /* >>> release single lock on fname */
  509. {ret = 1; goto ex;}
  510. }
  511. os_errno= errno;
  512. sprintf(msg, "Encountered error when closing drive '%s'", fname);
  513. if (sorry)
  514. sevno = LIBDAX_MSGS_SEV_SORRY;
  515. libdax_msgs_submit(libdax_messenger, driveno, 0x00020002,
  516. sevno, LIBDAX_MSGS_PRIO_HIGH, msg, os_errno, 0);
  517. ret = 0;
  518. ex:;
  519. BURN_FREE_MEM(msg);
  520. return ret;
  521. }
  522. /* ts A70401 :
  523. fcntl() has the unappealing property to work only after open().
  524. So libburn will by default use open(O_EXCL) first and afterwards
  525. as second assertion will use fcntl(F_SETLK). One lock more should not harm.
  526. */
  527. static int sg_fcntl_lock(int *fd, char *fd_name, int l_type, int verbous)
  528. {
  529. struct flock lockthing;
  530. char msg[81];
  531. int ret;
  532. if (!burn_sg_fcntl_f_setlk)
  533. return 1;
  534. memset(&lockthing, 0, sizeof(lockthing));
  535. lockthing.l_type = l_type;
  536. lockthing.l_whence = SEEK_SET;
  537. lockthing.l_start = 0;
  538. lockthing.l_len = 0;
  539. /*
  540. fprintf(stderr,"LIBBURN_EXPERIMENTAL: fcntl(%d, F_SETLK, %s)\n",
  541. *fd, l_type == F_WRLCK ? "F_WRLCK" : "F_RDLCK");
  542. */
  543. ret = fcntl(*fd, F_SETLK, &lockthing);
  544. if (ret == -1) {
  545. if (verbous) {
  546. sprintf(msg, "Device busy. Failed to fcntl-lock '%s'",
  547. fd_name);
  548. libdax_msgs_submit(libdax_messenger, -1, 0x00020008,
  549. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
  550. msg, errno, 0);
  551. }
  552. close(*fd);
  553. *fd = -1;
  554. /* ts A70409 : DDLP-B */
  555. /* >>> release single lock on fd_name */
  556. return(0);
  557. }
  558. return 1;
  559. }
  560. /* ts A60926 */
  561. /* @param scan_mode 0= open for drivce aquiration
  562. 1= open for scanning with guessed names
  563. 2= open for scanning with /proc/sys/dev/cdrom/info names
  564. */
  565. static int sg_open_drive_fd(char *fname, int scan_mode)
  566. {
  567. int open_mode = O_RDWR, fd, tries= 0, is_std_adr, report_as_note = 0;
  568. char msg[81];
  569. struct stat stbuf;
  570. /* ts A70409 : DDLP-B */
  571. /* >>> obtain single lock on fname */
  572. /* ts A60813 - A60927
  573. O_EXCL with devices is a non-POSIX feature
  574. of Linux kernels. Possibly introduced 2002.
  575. Mentioned in "The Linux SCSI Generic (sg) HOWTO" */
  576. if(burn_sg_open_o_excl)
  577. open_mode |= O_EXCL;
  578. /* ts A60813
  579. O_NONBLOCK was already hardcoded in ata_ but not in sg_.
  580. There must be some reason for this. So O_NONBLOCK is
  581. default mode for both now. Disable on own risk.
  582. ts B10904: O_NONBLOCK is prescribed by <linux/cdrom.h>
  583. ts A70411
  584. Switched to O_NDELAY for LKML statement 2007/4/11/141 by Alan Cox:
  585. "open() has side effects. The CD layer allows you to open
  586. with O_NDELAY if you want to avoid them."
  587. */
  588. if(burn_sg_open_o_nonblock)
  589. open_mode |= O_NDELAY;
  590. /* <<< debugging
  591. fprintf(stderr,
  592. "\nlibburn: experimental: o_excl= %d , o_nonblock= %d, abort_on_busy= %d\n",
  593. burn_sg_open_o_excl,burn_sg_open_o_nonblock,burn_sg_open_abort_busy);
  594. fprintf(stderr,
  595. "libburn: experimental: O_EXCL= %d , O_NDELAY= %d\n",
  596. !!(open_mode&O_EXCL),!!(open_mode&O_NDELAY));
  597. */
  598. try_open:;
  599. fd = open(fname, open_mode);
  600. if (fd == -1) {
  601. /* <<< debugging
  602. fprintf(stderr,
  603. "\nlibburn: experimental: fname= %s , errno= %d\n",
  604. fname,errno);
  605. */
  606. if (errno == EBUSY) {
  607. tries++;
  608. /* <<< debugging
  609. fprintf(stderr,
  610. "\nlibburn_DEBUG: EBUSY , tries= %d\n", tries);
  611. */
  612. if (tries < 4) {
  613. usleep(2000000);
  614. goto try_open;
  615. }
  616. sg_handle_busy_device(fname, errno);
  617. return -1;
  618. }
  619. sprintf(msg, "Failed to open device '%s'",fname);
  620. if (scan_mode) {
  621. is_std_adr = (strncmp(fname, "/dev/sr", 7) == 0 ||
  622. strncmp(fname, "/dev/scd", 8) == 0);
  623. if(scan_mode == 1 && is_std_adr &&
  624. stat(fname, &stbuf) != -1)
  625. report_as_note = 1;
  626. else if(scan_mode == 2 && (!is_std_adr) &&
  627. stat(fname, &stbuf) != -1)
  628. report_as_note = 1;
  629. if (report_as_note)
  630. libdax_msgs_submit(libdax_messenger, -1,
  631. 0x0002000e,
  632. LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_HIGH,
  633. msg, errno, 0);
  634. } else {
  635. libdax_msgs_submit(libdax_messenger, -1, 0x00020005,
  636. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
  637. msg, errno, 0);
  638. }
  639. return -1;
  640. }
  641. sg_fcntl_lock(&fd, fname, F_WRLCK, 1);
  642. return fd;
  643. }
  644. /* ts A60926 */
  645. static int sg_release_siblings(int sibling_fds[],
  646. char sibling_fnames[][BURN_OS_SG_MAX_NAMELEN],
  647. int *sibling_count)
  648. {
  649. int i;
  650. char msg[81];
  651. for(i= 0; i < *sibling_count; i++)
  652. sg_close_drive_fd(sibling_fnames[i], -1, &(sibling_fds[i]), 0);
  653. if(*sibling_count > 0) {
  654. sprintf(msg, "Closed %d O_EXCL scsi siblings", *sibling_count);
  655. libdax_msgs_submit(libdax_messenger, -1, 0x00020007,
  656. LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_HIGH, msg, 0,0);
  657. }
  658. *sibling_count = 0;
  659. return 1;
  660. }
  661. /* ts A60926 */
  662. static int sg_close_drive(struct burn_drive *d)
  663. {
  664. int ret;
  665. if (!burn_drive_is_open(d))
  666. return 0;
  667. sg_release_siblings(d->sibling_fds, d->sibling_fnames,
  668. &(d->sibling_count));
  669. ret = sg_close_drive_fd(d->devname, d->global_index, &(d->fd), 0);
  670. return ret;
  671. }
  672. /* ts A60926 */
  673. static int sg_open_scsi_siblings(char *path, int driveno,
  674. int sibling_fds[],
  675. char sibling_fnames[][BURN_OS_SG_MAX_NAMELEN],
  676. int *sibling_count,
  677. int host_no, int channel_no, int id_no, int lun_no)
  678. {
  679. int tld, i, ret, fd, i_bus_no = -1;
  680. int i_host_no = -1, i_channel_no = -1, i_target_no = -1, i_lun_no = -1;
  681. char *msg = NULL, fname[40];
  682. struct stat stbuf;
  683. dev_t last_rdev = 0, path_rdev;
  684. static char tldev[][20]= {"/dev/sr%d", "/dev/scd%d", "/dev/sg%d", ""};
  685. /* ts A70609: removed "/dev/st%d" */
  686. if (strlen(path) > BURN_MSGS_MESSAGE_LEN - 160)
  687. {ret = 0; goto ex;}
  688. BURN_ALLOC_MEM(msg, char, BURN_MSGS_MESSAGE_LEN);
  689. if(stat(path, &stbuf) == -1)
  690. {ret = 0; goto ex;}
  691. path_rdev = stbuf.st_rdev;
  692. sg_select_device_family();
  693. if (linux_sg_device_family[0] == 0)
  694. {ret = 1; goto ex;}
  695. if(host_no < 0 || id_no < 0 || channel_no < 0 || lun_no < 0)
  696. {ret = 2; goto ex;}
  697. if(*sibling_count > 0)
  698. sg_release_siblings(sibling_fds, sibling_fnames,
  699. sibling_count);
  700. for (tld = 0; tldev[tld][0] != 0; tld++) {
  701. if (strcmp(tldev[tld], linux_sg_device_family)==0)
  702. continue;
  703. for (i = 0; i < 32; i++) {
  704. sprintf(fname, tldev[tld], i);
  705. if(stat(fname, &stbuf) == -1)
  706. continue;
  707. if (path_rdev == stbuf.st_rdev)
  708. continue;
  709. if (*sibling_count > 0 && last_rdev == stbuf.st_rdev)
  710. continue;
  711. ret = sg_obtain_scsi_adr(fname, &i_bus_no, &i_host_no,
  712. &i_channel_no, &i_target_no, &i_lun_no);
  713. if (ret <= 0)
  714. continue;
  715. if (i_host_no != host_no || i_channel_no != channel_no)
  716. continue;
  717. if (i_target_no != id_no || i_lun_no != lun_no)
  718. continue;
  719. fd = sg_open_drive_fd(fname, 0);
  720. if (fd < 0)
  721. goto failed;
  722. if (*sibling_count>=BURN_OS_SG_MAX_SIBLINGS) {
  723. sprintf(msg, "Too many scsi siblings of '%s'",
  724. path);
  725. libdax_msgs_submit(libdax_messenger,
  726. driveno, 0x00020006,
  727. LIBDAX_MSGS_SEV_FATAL,
  728. LIBDAX_MSGS_PRIO_HIGH, msg, 0, 0);
  729. goto failed;
  730. }
  731. sprintf(msg, "Opened O_EXCL scsi sibling '%s' of '%s'",
  732. fname, path);
  733. libdax_msgs_submit(libdax_messenger, driveno,
  734. 0x00020004,
  735. LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_HIGH,
  736. msg, 0, 0);
  737. sibling_fds[*sibling_count] = fd;
  738. strcpy(sibling_fnames[*sibling_count], fname);
  739. (*sibling_count)++;
  740. last_rdev= stbuf.st_rdev;
  741. }
  742. }
  743. ret = 1;
  744. ex:;
  745. BURN_FREE_MEM(msg);
  746. return ret;
  747. failed:;
  748. sg_release_siblings(sibling_fds, sibling_fnames, sibling_count);
  749. ret = 0;
  750. goto ex;
  751. }
  752. /* ts A80731 */
  753. static int is_ata_drive(char *fname, int fd_in)
  754. {
  755. int fd;
  756. struct hd_driveid tm;
  757. if (fd_in >= 0)
  758. fd = fd_in;
  759. else
  760. fd = sg_open_drive_fd(fname, 1);
  761. if (fd == -1) {
  762. if (linux_ata_enumerate_verbous)
  763. fprintf(stderr,"open failed, errno=%d '%s'\n",
  764. errno, strerror(errno));
  765. return 0;
  766. }
  767. memset(&tm, 0, sizeof(tm));
  768. ioctl(fd, HDIO_GET_IDENTITY, &tm);
  769. /* not atapi */
  770. if (!(tm.config & 0x8000) || (tm.config & 0x4000)) {
  771. if (linux_ata_enumerate_verbous)
  772. fprintf(stderr, "not marked as ATAPI\n");
  773. if (fd_in < 0)
  774. sg_close_drive_fd(fname, -1, &fd, 0);
  775. return 0;
  776. }
  777. /* if SG_IO fails on an atapi device, we should stop trying to
  778. use hd* devices */
  779. if (sgio_test(fd) == -1) {
  780. if (linux_ata_enumerate_verbous)
  781. fprintf(stderr,
  782. "FATAL: sgio_test() failed: errno=%d '%s'\n",
  783. errno, strerror(errno));
  784. if (fd_in < 0)
  785. sg_close_drive_fd(fname, -1, &fd, 0);
  786. return 0;
  787. }
  788. if (fd_in >= 0)
  789. return 1;
  790. if (sg_close_drive_fd(fname, -1, &fd, 1) <= 0) {
  791. if (linux_ata_enumerate_verbous)
  792. fprintf(stderr,
  793. "cannot close properly, errno=%d '%s'\n",
  794. errno, strerror(errno));
  795. return 0;
  796. }
  797. return 1;
  798. }
  799. static int is_scsi_drive(char *fname, int fd_in, int *bus_no, int *host_no,
  800. int *channel_no, int *target_no, int *lun_no)
  801. {
  802. int fd = -1, sid_ret = 0, ret, fail_sev_sorry = 0;
  803. struct sg_scsi_id sid;
  804. int *sibling_fds = NULL, sibling_count= 0;
  805. typedef char burn_sg_sibling_fname[BURN_OS_SG_MAX_NAMELEN];
  806. burn_sg_sibling_fname *sibling_fnames = NULL;
  807. BURN_ALLOC_MEM(sibling_fds, int, BURN_OS_SG_MAX_SIBLINGS);
  808. BURN_ALLOC_MEM(sibling_fnames, burn_sg_sibling_fname,
  809. BURN_OS_SG_MAX_SIBLINGS);
  810. if (fd_in >= 0)
  811. fd = fd_in;
  812. else
  813. fd = sg_open_drive_fd(fname, 1);
  814. if (fd == -1) {
  815. if (linux_sg_enumerate_debug)
  816. fprintf(stderr, "open failed, errno=%d '%s'\n",
  817. errno, strerror(errno));
  818. {ret = 0; goto ex;}
  819. }
  820. sid_ret = ioctl(fd, SG_GET_SCSI_ID, &sid);
  821. if (sid_ret == -1) {
  822. sid.scsi_id = -1; /* mark SCSI address as invalid */
  823. if(linux_sg_enumerate_debug)
  824. fprintf(stderr,
  825. "ioctl(SG_GET_SCSI_ID) failed, errno=%d '%s' , ",
  826. errno, strerror(errno));
  827. if (sgio_test(fd) == -1) {
  828. if (linux_sg_enumerate_debug)
  829. fprintf(stderr,
  830. "FATAL: sgio_test() failed: errno=%d '%s'",
  831. errno, strerror(errno));
  832. {ret = 0; goto ex;}
  833. }
  834. #ifdef CDROM_DRIVE_STATUS
  835. /* http://developer.osdl.org/dev/robustmutexes/
  836. src/fusyn.hg/Documentation/ioctl/cdrom.txt */
  837. sid_ret = ioctl(fd, CDROM_DRIVE_STATUS, 0);
  838. if(linux_sg_enumerate_debug)
  839. fprintf(stderr,
  840. "ioctl(CDROM_DRIVE_STATUS) = %d , ",
  841. sid_ret);
  842. if (sid_ret != -1 && sid_ret != CDS_NO_INFO)
  843. sid.scsi_type = TYPE_ROM;
  844. else
  845. sid_ret = -1;
  846. #endif /* CDROM_DRIVE_STATUS */
  847. }
  848. if (sid_ret == -1) {
  849. /* ts B11109 : Try device type from INQUIRY byte 0 */
  850. if (sgio_inquiry_cd_drive(fd, fname) == 1) {
  851. sid_ret = 0;
  852. sid.scsi_type = TYPE_ROM;
  853. }
  854. }
  855. #ifdef SCSI_IOCTL_GET_BUS_NUMBER
  856. /* Hearsay A61005 */
  857. if (ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, bus_no) == -1)
  858. *bus_no = -1;
  859. #endif
  860. fail_sev_sorry = (sid.scsi_type == TYPE_ROM);
  861. if ( (sid_ret == -1 || sid.scsi_type != TYPE_ROM)
  862. && !linux_sg_accept_any_type) {
  863. if (linux_sg_enumerate_debug)
  864. fprintf(stderr, "sid.scsi_type = %d (!= TYPE_ROM)\n",
  865. sid.scsi_type);
  866. {ret = 0; goto ex;}
  867. }
  868. if (sid_ret == -1 || sid.scsi_id < 0) {
  869. /* ts A61211 : employ a more general ioctl */
  870. /* ts B11001 : re-use fd */
  871. ret = sg_obtain_scsi_adr_fd(fname, fd, bus_no, host_no,
  872. channel_no, target_no, lun_no);
  873. if (ret>0) {
  874. sid.host_no = *host_no;
  875. sid.channel = *channel_no;
  876. sid.scsi_id = *target_no;
  877. sid.lun = *lun_no;
  878. } else {
  879. if (linux_sg_enumerate_debug)
  880. fprintf(stderr,
  881. "sg_obtain_scsi_adr_fd() failed\n");
  882. {ret = 0; goto ex;}
  883. }
  884. }
  885. /* ts A60927 : trying to do locking with growisofs */
  886. if(burn_sg_open_o_excl>1) {
  887. ret = sg_open_scsi_siblings(
  888. fname, -1, sibling_fds, sibling_fnames,
  889. &sibling_count,
  890. sid.host_no, sid.channel,
  891. sid.scsi_id, sid.lun);
  892. if (ret<=0) {
  893. if (linux_sg_enumerate_debug)
  894. fprintf(stderr, "cannot lock siblings\n");
  895. sg_handle_busy_device(fname, 0);
  896. {ret = 0; goto ex;}
  897. }
  898. /* the final occupation will be done in sg_grab() */
  899. sg_release_siblings(sibling_fds, sibling_fnames,
  900. &sibling_count);
  901. }
  902. #ifdef SCSI_IOCTL_GET_BUS_NUMBER
  903. if(*bus_no == -1)
  904. *bus_no = 1000 * (sid.host_no + 1) + sid.channel;
  905. #else
  906. *bus_no = sid.host_no;
  907. #endif
  908. *host_no= sid.host_no;
  909. *channel_no= sid.channel;
  910. *target_no= sid.scsi_id;
  911. *lun_no= sid.lun;
  912. ret = 1;
  913. ex:;
  914. if (fd_in < 0 && fd >= 0) {
  915. if (sg_close_drive_fd(fname, -1, &fd, fail_sev_sorry) <= 0) {
  916. if (linux_sg_enumerate_debug)
  917. fprintf(stderr,
  918. "cannot close properly, errno=%d '%s'\n",
  919. errno, strerror(errno));
  920. if (ret > 0)
  921. ret = 0;
  922. }
  923. }
  924. BURN_FREE_MEM(sibling_fds);
  925. BURN_FREE_MEM(sibling_fnames);
  926. return ret;
  927. }
  928. /* @param flag bit0= do not complain about failure to open /dev/sr /dev/scd */
  929. static int sg_open_for_enumeration(char *fname, int flag)
  930. {
  931. int fd;
  932. fd = sg_open_drive_fd(fname, 1 + (flag & 1));
  933. if (fd < 0) {
  934. if (linux_sg_enumerate_debug || linux_ata_enumerate_verbous)
  935. fprintf(stderr, "open failed, errno=%d '%s'\n",
  936. errno, strerror(errno));
  937. return -1;
  938. }
  939. return fd;
  940. }
  941. /** Speciality of GNU/Linux: detect non-SCSI ATAPI (EIDE) which will from
  942. then on used used via generic SCSI as is done with (emulated) SCSI drives */
  943. static void ata_enumerate(void)
  944. {
  945. int ret, i, fd = -1;
  946. char fname[10];
  947. if (linux_ata_enumerate_verbous)
  948. fprintf(stderr, "libburn_debug: linux_ata_device_family = %s\n",
  949. linux_ata_device_family);
  950. if (linux_ata_device_family[0] == 0)
  951. return;
  952. for (i = 0; i < 26; i++) {
  953. sprintf(fname, linux_ata_device_family, 'a' + i);
  954. if (linux_ata_enumerate_verbous)
  955. fprintf(stderr, "libburn_debug: %s : ", fname);
  956. /* ts A51221 */
  957. if (burn_drive_is_banned(fname)) {
  958. if (linux_ata_enumerate_verbous)
  959. fprintf(stderr, "not in whitelist\n");
  960. continue;
  961. }
  962. fd = sg_open_for_enumeration(fname, 0);
  963. if (fd < 0)
  964. continue;
  965. ret = is_ata_drive(fname, fd);
  966. if (ret < 0)
  967. break;
  968. if (ret == 0)
  969. continue;
  970. if (linux_ata_enumerate_verbous)
  971. fprintf(stderr, "accepting as drive without SCSI address\n");
  972. enumerate_common(fname, fd, -1, -1, -1, -1, -1);
  973. }
  974. }
  975. /** Detects (probably emulated) SCSI drives */
  976. static void sg_enumerate(void)
  977. {
  978. int i, ret, fd = -1;
  979. int bus_no= -1, host_no= -1, channel_no= -1, target_no= -1, lun_no= -1;
  980. char fname[17];
  981. sg_select_device_family();
  982. if (linux_sg_enumerate_debug)
  983. fprintf(stderr, "libburn_debug: linux_sg_device_family = %s\n",
  984. linux_sg_device_family);
  985. if (linux_sg_device_family[0] == 0)
  986. return;
  987. for (i = 0; i < 32; i++) {
  988. sprintf(fname, linux_sg_device_family, i);
  989. /* ts A80702 */
  990. sg_exchange_scd_for_sr(fname, 0);
  991. if (linux_sg_enumerate_debug)
  992. fprintf(stderr, "libburn_debug: %s : ", fname);
  993. /* ts A51221 */
  994. if (burn_drive_is_banned(fname)) {
  995. if (linux_sg_enumerate_debug)
  996. fprintf(stderr, "not in whitelist\n");
  997. continue;
  998. }
  999. fd = sg_open_for_enumeration(fname, 0);
  1000. if (fd < 0)
  1001. continue;
  1002. ret = is_scsi_drive(fname, fd, &bus_no, &host_no, &channel_no,
  1003. &target_no, &lun_no);
  1004. if (ret < 0)
  1005. break;
  1006. if (ret == 0)
  1007. continue;
  1008. if (linux_sg_enumerate_debug)
  1009. fprintf(stderr, "accepting as SCSI %d,%d,%d,%d bus=%d\n",
  1010. host_no, channel_no, target_no, lun_no, bus_no);
  1011. enumerate_common(fname, fd, bus_no, host_no, channel_no,
  1012. target_no, lun_no);
  1013. }
  1014. }
  1015. /* ts A80805 : eventually produce the other official name of a device file */
  1016. static int fname_other_name(char *fname, char other_name[80], int flag)
  1017. {
  1018. if(strncmp(fname, "/dev/sr", 7) == 0 &&
  1019. (fname[7] >= '0' && fname[7] <= '9') &&
  1020. (fname[8] == 0 ||
  1021. (fname[8] >= '0' && fname[8] <= '9' && fname[9] == 0))) {
  1022. sprintf(other_name, "/dev/scd%s", fname + 7);
  1023. return 1;
  1024. }
  1025. if(strncmp(fname, "/dev/scd", 8) == 0 &&
  1026. (fname[8] >= '0' && fname[8] <= '9') &&
  1027. (fname[9] == 0 ||
  1028. (fname[9] >= '0' && fname[9] <= '9' && fname[10] == 0))) {
  1029. sprintf(other_name, "/dev/sr%s", fname + 8);
  1030. return 1;
  1031. }
  1032. return 0;
  1033. }
  1034. /* ts A80805 */
  1035. static int fname_drive_is_listed(char *fname, int flag)
  1036. {
  1037. char other_fname[80];
  1038. if (burn_drive_is_listed(fname, NULL, 0))
  1039. return 1;
  1040. if (fname_other_name(fname, other_fname, 0) > 0)
  1041. if (burn_drive_is_listed(other_fname, NULL, 0))
  1042. return 2;
  1043. return 0;
  1044. }
  1045. /* ts A80731 : Directly open the given address.
  1046. @param flag bit0= do not complain about missing file
  1047. bit1= do not check whether drive is already listed
  1048. bit2= do not complain about failure to open /dev/sr /dev/scd
  1049. */
  1050. static int fname_enumerate(char *fname, int flag)
  1051. {
  1052. int is_ata= 0, is_scsi= 0, ret, fd = -1;
  1053. int bus_no= -1, host_no= -1, channel_no= -1, target_no= -1, lun_no= -1;
  1054. char *msg = NULL;
  1055. struct stat stbuf;
  1056. BURN_ALLOC_MEM(msg, char, BURN_DRIVE_ADR_LEN + 80);
  1057. if (!(flag & 2))
  1058. if (fname_drive_is_listed(fname, 0))
  1059. {ret = 2; goto ex;}
  1060. if (stat(fname, &stbuf) == -1) {
  1061. sprintf(msg, "File object '%s' not found", fname);
  1062. if (!(flag & 1))
  1063. libdax_msgs_submit(libdax_messenger, -1, 0x0002000b,
  1064. LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  1065. msg, 0, 0);
  1066. {ret = -1; goto ex;}
  1067. }
  1068. fd = sg_open_for_enumeration(fname, !!(flag & 4));
  1069. if (fd < 0)
  1070. {ret = 0; goto ex;}
  1071. is_ata = is_ata_drive(fname, fd);
  1072. if (is_ata < 0)
  1073. {ret = -1; goto ex;}
  1074. if (!is_ata)
  1075. is_scsi = is_scsi_drive(fname, fd, &bus_no, &host_no,
  1076. &channel_no, &target_no, &lun_no);
  1077. if (is_scsi < 0)
  1078. {ret = -1; goto ex;}
  1079. if (is_ata == 0 && is_scsi == 0)
  1080. {ret = 0; goto ex;}
  1081. if (linux_sg_enumerate_debug)
  1082. fprintf(stderr,
  1083. "(single) accepting as SCSI %d,%d,%d,%d bus=%d\n",
  1084. host_no, channel_no, target_no, lun_no, bus_no);
  1085. enumerate_common(fname, fd, bus_no, host_no, channel_no,
  1086. target_no, lun_no);
  1087. ret = 1;
  1088. ex:;
  1089. BURN_FREE_MEM(msg);
  1090. return ret;
  1091. }
  1092. /* ts A80731 : Directly open the given address from a single-item whitlist */
  1093. static int single_enumerate(int flag)
  1094. {
  1095. int ret, wl_count;
  1096. char *fname, *msg = NULL;
  1097. wl_count= burn_drive_whitelist_count();
  1098. if (wl_count != 1)
  1099. {ret = 0; goto ex;}
  1100. fname= burn_drive_whitelist_item(0, 0);
  1101. if (fname == NULL)
  1102. {ret = 0; goto ex;}
  1103. ret = fname_enumerate(fname, 2);
  1104. if (ret <= 0) {
  1105. BURN_ALLOC_MEM(msg, char, BURN_DRIVE_ADR_LEN + 80);
  1106. sprintf(msg, "Cannot access '%s' as SG_IO CDROM drive", fname);
  1107. libdax_msgs_submit(libdax_messenger, -1, 0x0002000a,
  1108. LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  1109. msg, 0, 0);
  1110. ret = -1;
  1111. }
  1112. ex:;
  1113. BURN_FREE_MEM(msg);
  1114. return ret;
  1115. }
  1116. /* ts A80801 : looking up drives listed in /proc/sys/dev/cdrom/info line like:
  1117. drive name: sr1 hdc hda sr0
  1118. @parm flag bit0= release list memory and exit
  1119. */
  1120. static int proc_sys_dev_cdrom_info(char ***list, int *count, int flag)
  1121. {
  1122. FILE *fp;
  1123. char *line = NULL, *fname = NULL, *cpt, *retpt, *list_data;
  1124. int maxl= 0, pass, i, line_size = 1024, ret;
  1125. BURN_ALLOC_MEM(line, char, line_size);
  1126. BURN_ALLOC_MEM(fname, char, line_size + 5);
  1127. if (*list != NULL) {
  1128. if ((*list)[0] != NULL)
  1129. free((*list)[0]);
  1130. free(*list);
  1131. *list = NULL;
  1132. *count = 0;
  1133. }
  1134. if (flag & 1)
  1135. {ret = 1; goto ex;}
  1136. *count = 0;
  1137. sg_evaluate_kernel();
  1138. if (sg_kernel_age < 2) /* addresses are not suitable for kernel 2.4 */
  1139. {ret = 1; goto ex;}
  1140. fp = fopen("/proc/sys/dev/cdrom/info", "r");
  1141. if (fp == NULL)
  1142. {ret = 0; goto ex;}
  1143. while (1) {
  1144. retpt = fgets(line, line_size, fp);
  1145. if (retpt == NULL)
  1146. break;
  1147. if(strncmp(line, "drive name:", 11) == 0)
  1148. break;
  1149. }
  1150. fclose(fp);
  1151. if (retpt == NULL)
  1152. {ret = 0; goto ex;}
  1153. strcpy(fname, "/dev/");
  1154. for(pass = 0; pass < 2; pass++) {
  1155. *count = 0;
  1156. cpt = line + 11;
  1157. while (*cpt != 0) {
  1158. for(; *cpt == ' ' || *cpt == '\t'; cpt++);
  1159. if (*cpt == 0 || *cpt == '\n')
  1160. break;
  1161. sscanf(cpt, "%s", fname + 5);
  1162. if ((int) strlen(fname) > maxl)
  1163. maxl = strlen(fname);
  1164. if (pass == 1)
  1165. strcpy((*list)[*count], fname);
  1166. (*count)++;
  1167. for(cpt++; *cpt != ' ' && *cpt != '\t'
  1168. && *cpt != 0 && *cpt != '\n'; cpt++);
  1169. }
  1170. if (pass == 0) {
  1171. list_data = calloc(*count + 1, maxl+1);
  1172. *list = calloc(*count + 1, sizeof(char *));
  1173. if(list_data == NULL || *list == NULL) {
  1174. libdax_msgs_submit(libdax_messenger, -1,
  1175. 0x00000003,
  1176. LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH,
  1177. "Out of virtual memory", 0, 0);
  1178. if (list_data != NULL)
  1179. free(list_data);
  1180. if (*list != NULL)
  1181. free((char *) *list);
  1182. {ret = -1; goto ex;}
  1183. }
  1184. for (i = 0; i <= *count; i++)
  1185. (*list)[i] = list_data + i * (maxl + 1);
  1186. }
  1187. }
  1188. ret = 1;
  1189. ex:;
  1190. BURN_FREE_MEM(line);
  1191. BURN_FREE_MEM(fname);
  1192. return ret;
  1193. }
  1194. static int add_proc_info_drives(int flag)
  1195. {
  1196. int ret, list_count, count = 0, i;
  1197. char **list= NULL;
  1198. if (burn_sg_use_family != 0)
  1199. return(1); /* Looking only for sr , scd , sg */
  1200. ret = proc_sys_dev_cdrom_info(&list, &list_count, 0);
  1201. if (ret <= 0)
  1202. return ret;
  1203. for (i = 0; i < list_count; i++) {
  1204. if (burn_drive_is_banned(list[i]))
  1205. continue;
  1206. ret = fname_enumerate(list[i], 1 | 4);
  1207. if (ret == 1)
  1208. count++;
  1209. }
  1210. proc_sys_dev_cdrom_info(&list, &list_count, 1); /* free memory */
  1211. return 1 + count;
  1212. }
  1213. /* ts A61115 */
  1214. /* ----------------------------------------------------------------------- */
  1215. /* PORTING: Private functions which contain publicly needed functionality. */
  1216. /* Their portable part must be performed. So it is probably best */
  1217. /* to replace the non-portable part and to call these functions */
  1218. /* in your port, too. */
  1219. /* ----------------------------------------------------------------------- */
  1220. /** Wraps a detected drive into libburn structures and hands it over to
  1221. libburn drive list.
  1222. */
  1223. /* ts A60923 - A61005 : introduced new SCSI parameters */
  1224. /* ts A61021 : moved non os-specific code to spc,sbc,mmc,drive */
  1225. static void enumerate_common(char *fname, int fd_in, int bus_no, int host_no,
  1226. int channel_no, int target_no, int lun_no)
  1227. {
  1228. int ret, i;
  1229. struct burn_drive out;
  1230. /* General libburn drive setup */
  1231. burn_setup_drive(&out, fname);
  1232. /* This transport adapter uses SCSI-family commands and models
  1233. (seems the adapter would know better than its boss, if ever) */
  1234. ret = burn_scsi_setup_drive(&out, bus_no, host_no, channel_no,
  1235. target_no, lun_no, 0);
  1236. if (ret<=0)
  1237. return;
  1238. /* PORTING: ------------------- non portable part --------------- */
  1239. /* Operating system adapter is GNU/Linux Generic SCSI (sg) */
  1240. /* Adapter specific handles and data */
  1241. out.fd = -1337;
  1242. out.sibling_count = 0;
  1243. for(i= 0; i<BURN_OS_SG_MAX_SIBLINGS; i++)
  1244. out.sibling_fds[i] = -1337;
  1245. /* PORTING: ---------------- end of non portable part ------------ */
  1246. /* Adapter specific functions with standardized names */
  1247. out.grab = sg_grab;
  1248. out.release = sg_release;
  1249. out.drive_is_open= sg_drive_is_open;
  1250. out.issue_command = sg_issue_command;
  1251. if (fd_in >= 0)
  1252. out.fd = fd_in;
  1253. /* Finally register drive and inquire drive information.
  1254. out is an invalid copy afterwards. Do not use it for anything.
  1255. */
  1256. burn_drive_finish_enum(&out);
  1257. }
  1258. /* ts A61115 */
  1259. /* ------------------------------------------------------------------------ */
  1260. /* PORTING: Public functions. These MUST be ported. */
  1261. /* ------------------------------------------------------------------------ */
  1262. /** Returns the id string of the SCSI transport adapter and eventually
  1263. needed operating system facilities.
  1264. This call is usable even if sg_initialize() was not called yet. In that
  1265. case a preliminary constant message might be issued if detailed info is
  1266. not available yet.
  1267. @param msg returns id string
  1268. @param flag unused yet, submit 0
  1269. @return 1 = success, <=0 = failure
  1270. */
  1271. int sg_id_string(char msg[1024], int flag)
  1272. {
  1273. strcpy(msg, "internal GNU/Linux SG_IO adapter sg-linux");
  1274. return 1;
  1275. }
  1276. /** Performs global initialization of the SCSI transport adapter and eventually
  1277. needed operating system facilities. Checks for compatibility supporting
  1278. software components.
  1279. @param msg returns ids and/or error messages of eventual helpers
  1280. @param flag unused yet, submit 0
  1281. @return 1 = success, <=0 = failure
  1282. */
  1283. int sg_initialize(char msg[1024], int flag)
  1284. {
  1285. return sg_id_string(msg, 0);
  1286. }
  1287. /** Performs global finalization of the SCSI transport adapter and eventually
  1288. needed operating system facilities. Releases globally acquired resources.
  1289. @param flag unused yet, submit 0
  1290. @return 1 = success, <=0 = failure
  1291. */
  1292. int sg_shutdown(int flag)
  1293. {
  1294. return 1;
  1295. }
  1296. /** Finalizes BURN_OS_TRANSPORT_DRIVE_ELEMENTS, the components of
  1297. struct burn_drive which are defined in os-*.h.
  1298. The eventual initialization of those components was made underneath
  1299. scsi_enumerate_drives().
  1300. This will be called when a burn_drive gets disposed.
  1301. @param d the drive to be finalized
  1302. @param flag unused yet, submit 0
  1303. @return 1 = success, <=0 = failure
  1304. */
  1305. int sg_dispose_drive(struct burn_drive *d, int flag)
  1306. {
  1307. return 1;
  1308. }
  1309. /** PORTING:
  1310. In this GNU/Linux implementation, this function mirrors the enumeration
  1311. done in sg_enumerate and ata_enumerate(). It would be better to base those
  1312. functions on this sg_give_next_adr() but the situation is not inviting.
  1313. */
  1314. /* ts A60922 ticket 33 : called from drive.c */
  1315. /** Returns the next index number and the next enumerated drive address.
  1316. The enumeration has to cover all available and accessible drives. It is
  1317. allowed to return addresses of drives which are not available but under
  1318. some (even exotic) circumstances could be available. It is on the other
  1319. hand allowed, only to hand out addresses which can really be used right
  1320. in the moment of this call. (This implementation chooses the former.)
  1321. @param idx An opaque handle. Make no own theories about it.
  1322. @param adr Takes the reply
  1323. @param adr_size Gives maximum size of reply including final 0
  1324. @param initialize 1 = start new,
  1325. 0 = continue, use no other values for now
  1326. -1 = finish
  1327. @return 1 = reply is a valid address , 0 = no further address available
  1328. -1 = severe error (e.g. adr_size too small)
  1329. */
  1330. int sg_give_next_adr(burn_drive_enumerator_t *idx,
  1331. char adr[], int adr_size, int initialize)
  1332. {
  1333. /* os-linux.h : typedef int burn_drive_enumerator_t; */
  1334. static int sg_limit = 32, ata_limit = 26;
  1335. int baseno = 0, i;
  1336. char other_name[80];
  1337. if (initialize == -1) {
  1338. proc_sys_dev_cdrom_info(&(idx->info_list), &(idx->info_count),
  1339. 1);
  1340. return 0;
  1341. }
  1342. sg_select_device_family();
  1343. if (linux_sg_device_family[0] == 0)
  1344. sg_limit = 0;
  1345. if (linux_ata_device_family[0] == 0)
  1346. ata_limit = 0;
  1347. if (initialize == 1) {
  1348. idx->pos = -1;
  1349. idx->info_count= 0;
  1350. idx->info_list= NULL;
  1351. proc_sys_dev_cdrom_info(&(idx->info_list), &(idx->info_count),
  1352. 0);
  1353. }
  1354. (idx->pos)++;
  1355. if (idx->pos >= sg_limit)
  1356. goto next_ata;
  1357. if (adr_size < 11)
  1358. return -1;
  1359. sprintf(adr, linux_sg_device_family, idx->pos);
  1360. sg_exchange_scd_for_sr(adr, 0);
  1361. goto return_1_pre_proc;
  1362. next_ata:;
  1363. baseno += sg_limit;
  1364. if (idx->pos - baseno >= ata_limit)
  1365. goto next_proc_info;
  1366. if (adr_size < 9)
  1367. return -1;
  1368. sprintf(adr, linux_ata_device_family, 'a' + (idx->pos - baseno));
  1369. goto return_1_pre_proc;
  1370. next_proc_info:;
  1371. baseno += ata_limit;
  1372. if (burn_sg_use_family != 0) /* Only with default enumeration */
  1373. return 0;
  1374. for (i = 0; i < idx->info_count; i++) {
  1375. if ((idx->info_list)[i][0] == 0)
  1376. continue;
  1377. if (baseno == idx->pos) {
  1378. if (adr_size < (int) strlen((idx->info_list)[i]) + 1)
  1379. return -1;
  1380. strcpy(adr, (idx->info_list)[i]);
  1381. return 1;
  1382. }
  1383. baseno++;
  1384. }
  1385. return 0;
  1386. return_1_pre_proc:;
  1387. for (i = 0; i < idx->info_count; i++) {
  1388. if (strcmp((idx->info_list)[i], adr) == 0)
  1389. (idx->info_list)[i][0] = 0;
  1390. if (fname_other_name(adr, other_name, 0) > 0)
  1391. if (strcmp((idx->info_list)[i], other_name) == 0)
  1392. (idx->info_list)[i][0] = 0;
  1393. }
  1394. return 1;
  1395. }
  1396. /** Brings all available, not-whitelist-banned, and accessible drives into
  1397. libburn's list of drives.
  1398. */
  1399. /** PORTING:
  1400. If not stricken with an incompletely unified situation like in GNU/Linux
  1401. one would rather implement this by a loop calling sg_give_next_adr().
  1402. If needed with your sg_give_next_adr() results, do a test for existence
  1403. and accessability. If burn activities are prone to external interference
  1404. on your system it is also necessary to obtain exclusive access locks on
  1405. the drives.
  1406. Hand over each accepted drive to enumerate_common() or its replacement
  1407. within your port.
  1408. See FreeBSD port sketch sg-freebsd-port.c for such an implementation.
  1409. */
  1410. /* ts A61115: replacing call to sg-implementation internals from drive.c */
  1411. int scsi_enumerate_drives(void)
  1412. {
  1413. int ret;
  1414. /* Direct examination of eventually single whitelisted name */
  1415. ret = single_enumerate(0);
  1416. if (ret < 0)
  1417. return -1;
  1418. if (ret > 0)
  1419. return 1;
  1420. sg_enumerate();
  1421. ata_enumerate();
  1422. add_proc_info_drives(0);
  1423. return 1;
  1424. }
  1425. /** Tells wether libburn has the given drive in use or exclusively reserved.
  1426. If it is "open" then libburn will eventually call sg_release() on it when
  1427. it is time to give up usage and reservation.
  1428. */
  1429. /** Published as burn_drive.drive_is_open() */
  1430. int sg_drive_is_open(struct burn_drive * d)
  1431. {
  1432. /* a bit more detailed case distinction than needed */
  1433. if (d->fd == -1337)
  1434. return 0;
  1435. if (d->fd < 0)
  1436. return 0;
  1437. return 1;
  1438. }
  1439. /** Opens the drive for SCSI commands and - if burn activities are prone
  1440. to external interference on your system - obtains an exclusive access lock
  1441. on the drive. (Note: this is not physical tray locking.)
  1442. A drive that has been opened with sg_grab() will eventually be handed
  1443. over to sg_release() for closing and unreserving.
  1444. */
  1445. int sg_grab(struct burn_drive *d)
  1446. {
  1447. int fd, os_errno= 0, ret;
  1448. int max_tries = 3, tries = 0;
  1449. /* ts A60813 */
  1450. int open_mode = O_RDWR;
  1451. /* ts A60821
  1452. <<< debug: for tracing calls which might use open drive fds */
  1453. if (mmc_function_spy(d, "sg_grab") <= 0)
  1454. return 0;
  1455. /* ts A60813 - A60927
  1456. O_EXCL with devices is a non-POSIX feature
  1457. of Linux kernels. Possibly introduced 2002.
  1458. Mentioned in "The Linux SCSI Generic (sg) HOWTO".
  1459. */
  1460. if(burn_sg_open_o_excl)
  1461. open_mode |= O_EXCL;
  1462. /* ts A60813
  1463. O_NONBLOCK was hardcoded here. So it should stay default mode.
  1464. ts A70411
  1465. Switched to O_NDELAY for LKML statement 2007/4/11/141
  1466. */
  1467. if(burn_sg_open_o_nonblock)
  1468. open_mode |= O_NDELAY;
  1469. /* ts A60813 - A60822
  1470. After enumeration the drive fd is probably still open.
  1471. -1337 is the initial value of burn_drive.fd and the value after
  1472. relase of drive. Unclear why not the official error return
  1473. value -1 of open(2) war used. */
  1474. if(! burn_drive_is_open(d)) {
  1475. char msg[120];
  1476. /* >>> SINGLE_OPEN : This case should be impossible now, since enumeration
  1477. transfers the fd from scanning to drive.
  1478. So if close-wait-open is