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.
 
 
 
 
 
 

2221 lines
59 KiB

  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 - 2010 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. aquired 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 aquired
  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 allows 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. This initial setting may be overridden in sg_select_device_family() by
  135. settings made via burn_preset_device_open().
  136. */
  137. static char linux_sg_device_family[80] = {"/dev/sg%d"};
  138. /* Set this to 1 if you want the default linux_sg_device_family chosen
  139. depending on kernel release: sg for <2.6 , sr for >=2.6
  140. */
  141. static int linux_sg_auto_family = 1;
  142. /* Set this to 1 in order to accept any TYPE_* (see scsi/scsi.h) */
  143. /* But try with 0 first. There is hope via CDROM_DRIVE_STATUS. */
  144. /* !!! DO NOT SET TO 1 UNLESS YOU PROTECTED ALL INDISPENSIBLE DEVICES
  145. chmod -rw !!! */
  146. static int linux_sg_accept_any_type = 0;
  147. /* The device file family to use for SCSI transport over ATA.
  148. This must be a printf formatter with one single placeholder for a
  149. _single_ char in the range of 'a' to 'z'. This placeholder _must_ be
  150. at the end of the formatter string.
  151. E.g. "/dev/hd%c"
  152. */
  153. static char linux_ata_device_family[80] = {"/dev/hd%c"};
  154. /* Set this to 1 in order to get on stderr messages from ata_enumerate()
  155. */
  156. static int linux_ata_enumerate_verbous = 0;
  157. /* The waiting time before eventually retrying a failed SCSI command.
  158. Before each retry wait Libburn_sg_linux_retry_incR longer than with
  159. the previous one.
  160. */
  161. #define Libburn_sg_linux_retry_usleeP 100000
  162. #define Libburn_sg_linux_retry_incR 100000
  163. /** PORTING : ------ libburn portable headers and definitions ----- */
  164. #include "libburn.h"
  165. #include "transport.h"
  166. #include "drive.h"
  167. #include "sg.h"
  168. #include "spc.h"
  169. #include "mmc.h"
  170. #include "sbc.h"
  171. #include "debug.h"
  172. #include "toc.h"
  173. #include "util.h"
  174. #include "libdax_msgs.h"
  175. extern struct libdax_msgs *libdax_messenger;
  176. /* ts A51221 */
  177. int burn_drive_is_banned(char *device_address);
  178. /* ------------------------------------------------------------------------ */
  179. /* PORTING: Private definitions. Port only if needed by public functions. */
  180. /* (Public functions are listed below) */
  181. /* ------------------------------------------------------------------------ */
  182. static void enumerate_common(char *fname, int bus_no, int host_no,
  183. int channel_no, 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[160];
  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. sprintf(msg, "%s substitutes for non-existent %s", scd, fname);
  275. libdax_msgs_submit(libdax_messenger, -1, 0x00000002,
  276. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH, msg, 0, 0);
  277. strcpy(fname, scd);
  278. return 1;
  279. }
  280. static int sgio_test(int fd)
  281. {
  282. unsigned char test_ops[] = { 0, 0, 0, 0, 0, 0 };
  283. sg_io_hdr_t s;
  284. memset(&s, 0, sizeof(sg_io_hdr_t));
  285. s.interface_id = 'S';
  286. s.dxfer_direction = SG_DXFER_NONE;
  287. s.cmd_len = 6;
  288. s.cmdp = test_ops;
  289. s.timeout = 12345;
  290. return ioctl(fd, SG_IO, &s);
  291. }
  292. /* ts A60924 */
  293. static int sg_handle_busy_device(char *fname, int os_errno)
  294. {
  295. char msg[4096];
  296. struct stat stbuf;
  297. int looks_like_hd= 0, fd, ret;
  298. /* ts A80713 :
  299. check existence of /dev/hdX1 as hint for hard disk rather than CD
  300. Hint by Giulio Orsero: check /proc/ide/hdX/media for "disk"
  301. */
  302. if (strncmp(fname, "/dev/hd", 7)==0) {
  303. sprintf(msg, "%s1", fname);
  304. if (stat(msg, &stbuf) != -1)
  305. looks_like_hd= 1;
  306. sprintf(msg, "/proc/ide/hd%c/media", fname[7]);
  307. fd = open(msg, O_RDONLY);
  308. if (fd != -1) {
  309. ret = read(fd, msg, 10);
  310. if (ret < 0)
  311. ret = 0;
  312. msg[ret]= 0;
  313. close(fd);
  314. if (strncmp(msg, "disk\n", 5) == 0 ||
  315. strcmp(msg, "disk") == 0)
  316. looks_like_hd= 2;
  317. else if (strncmp(msg, "cdrom\n", 6) == 0 ||
  318. strcmp(msg, "cdrom") == 0)
  319. looks_like_hd= 0;
  320. }
  321. }
  322. /* ts A60814 : i saw no way to do this more nicely */
  323. if (burn_sg_open_abort_busy) {
  324. fprintf(stderr,
  325. "\nlibburn: FATAL : Application triggered abort on busy device '%s'\n",
  326. fname);
  327. /* ts A61007 */
  328. abort();
  329. /* a ssert("drive busy" == "non fatal"); */
  330. }
  331. /* ts A60924 : now reporting to libdax_msgs */
  332. if (looks_like_hd == 2) { /* is surely hard disk */
  333. ;
  334. } else if (looks_like_hd) {
  335. sprintf(msg, "Could not examine busy device '%s'", fname);
  336. libdax_msgs_submit(libdax_messenger, -1, 0x0002015a,
  337. LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_LOW,
  338. msg, os_errno, 0);
  339. sprintf(msg,
  340. "Busy '%s' seems to be a hard disk, as '%s1' exists. But better check.",
  341. fname, fname);
  342. libdax_msgs_submit(libdax_messenger, -1, 0x0002015b,
  343. LIBDAX_MSGS_SEV_HINT, LIBDAX_MSGS_PRIO_LOW,
  344. msg, 0, 0);
  345. } else {
  346. sprintf(msg, "Cannot open busy device '%s'", fname);
  347. libdax_msgs_submit(libdax_messenger, -1, 0x00020001,
  348. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_LOW,
  349. msg, os_errno, 0);
  350. }
  351. return 1;
  352. }
  353. /* ts A60925 : ticket 74 */
  354. static int sg_close_drive_fd(char *fname, int driveno, int *fd, int sorry)
  355. {
  356. int ret, os_errno, sevno= LIBDAX_MSGS_SEV_DEBUG;
  357. char msg[4096+100];
  358. if(*fd < 0)
  359. return(0);
  360. #ifdef CDROM_MEDIA_CHANGED_disabled_because_not_helpful
  361. #ifdef CDSL_CURRENT
  362. /* ts A80217 : wondering whether the os knows about our activities */
  363. ret = ioctl(*fd, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
  364. sprintf(msg, "ioctl(CDROM_MEDIA_CHANGED) == %d", ret);
  365. libdax_msgs_submit(libdax_messenger, driveno, 0x00000002,
  366. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH, msg, 0, 0);
  367. #ifdef BLKFLSBUF_disabled_because_not_helpful
  368. ret = ioctl(*fd, BLKFLSBUF, 0);
  369. sprintf(msg, "ioctl(BLKFLSBUF) == %d", ret);
  370. os_errno = 0;
  371. if(ret == -1)
  372. os_errno = errno;
  373. libdax_msgs_submit(libdax_messenger, driveno, 0x00000002,
  374. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH, msg, os_errno,0);
  375. #endif /* BLKFLSBUF */
  376. #endif /* CDSL_CURRENT */
  377. #endif /* CDROM_MEDIA_CHANGED */
  378. ret = close(*fd);
  379. *fd = -1337;
  380. if(ret != -1) {
  381. /* ts A70409 : DDLP-B */
  382. /* >>> release single lock on fname */
  383. return 1;
  384. }
  385. os_errno= errno;
  386. sprintf(msg, "Encountered error when closing drive '%s'", fname);
  387. if (sorry)
  388. sevno = LIBDAX_MSGS_SEV_SORRY;
  389. libdax_msgs_submit(libdax_messenger, driveno, 0x00020002,
  390. sevno, LIBDAX_MSGS_PRIO_HIGH, msg, os_errno, 0);
  391. return 0;
  392. }
  393. /* ts A70401 :
  394. fcntl() has the unappealing property to work only after open().
  395. So libburn will by default use open(O_EXCL) first and afterwards
  396. as second assertion will use fcntl(F_SETLK). One lock more should not harm.
  397. */
  398. static int sg_fcntl_lock(int *fd, char *fd_name, int l_type, int verbous)
  399. {
  400. struct flock lockthing;
  401. char msg[81];
  402. int ret;
  403. if (!burn_sg_fcntl_f_setlk)
  404. return 1;
  405. memset(&lockthing, 0, sizeof(lockthing));
  406. lockthing.l_type = l_type;
  407. lockthing.l_whence = SEEK_SET;
  408. lockthing.l_start = 0;
  409. lockthing.l_len = 0;
  410. /*
  411. fprintf(stderr,"LIBBURN_EXPERIMENTAL: fcntl(%d, F_SETLK, %s)\n",
  412. *fd, l_type == F_WRLCK ? "F_WRLCK" : "F_RDLCK");
  413. */
  414. ret = fcntl(*fd, F_SETLK, &lockthing);
  415. if (ret == -1) {
  416. if (verbous) {
  417. sprintf(msg, "Device busy. Failed to fcntl-lock '%s'",
  418. fd_name);
  419. libdax_msgs_submit(libdax_messenger, -1, 0x00020008,
  420. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
  421. msg, errno, 0);
  422. }
  423. close(*fd);
  424. *fd = -1;
  425. /* ts A70409 : DDLP-B */
  426. /* >>> release single lock on fd_name */
  427. return(0);
  428. }
  429. return 1;
  430. }
  431. /* ts A60926 */
  432. static int sg_open_drive_fd(char *fname, int scan_mode)
  433. {
  434. int open_mode = O_RDWR, fd, tries= 0;
  435. char msg[81];
  436. /* ts A70409 : DDLP-B */
  437. /* >>> obtain single lock on fname */
  438. /* ts A60813 - A60927
  439. O_EXCL with devices is a non-POSIX feature
  440. of Linux kernels. Possibly introduced 2002.
  441. Mentioned in "The Linux SCSI Generic (sg) HOWTO" */
  442. if(burn_sg_open_o_excl)
  443. open_mode |= O_EXCL;
  444. /* ts A60813
  445. O_NONBLOCK was already hardcoded in ata_ but not in sg_.
  446. There must be some reason for this. So O_NONBLOCK is
  447. default mode for both now. Disable on own risk.
  448. ts A70411
  449. Switched to O_NDELAY for LKML statement 2007/4/11/141 by Alan Cox:
  450. "open() has side effects. The CD layer allows you to open
  451. with O_NDELAY if you want to avoid them."
  452. */
  453. if(burn_sg_open_o_nonblock)
  454. open_mode |= O_NDELAY;
  455. /* <<< debugging
  456. fprintf(stderr,
  457. "\nlibburn: experimental: o_excl= %d , o_nonblock= %d, abort_on_busy= %d\n",
  458. burn_sg_open_o_excl,burn_sg_open_o_nonblock,burn_sg_open_abort_busy);
  459. fprintf(stderr,
  460. "libburn: experimental: O_EXCL= %d , O_NDELAY= %d\n",
  461. !!(open_mode&O_EXCL),!!(open_mode&O_NDELAY));
  462. */
  463. try_open:;
  464. fd = open(fname, open_mode);
  465. if (fd == -1) {
  466. /* <<< debugging
  467. fprintf(stderr,
  468. "\nlibburn: experimental: fname= %s , errno= %d\n",
  469. fname,errno);
  470. */
  471. if (errno == EBUSY) {
  472. tries++;
  473. /* <<< debugging
  474. fprintf(stderr,
  475. "\nlibburn_DEBUG: EBUSY , tries= %d\n", tries);
  476. */
  477. if (tries < 4) {
  478. usleep(2000000);
  479. goto try_open;
  480. }
  481. sg_handle_busy_device(fname, errno);
  482. return -1;
  483. }
  484. if (scan_mode)
  485. return -1;
  486. sprintf(msg, "Failed to open device '%s'",fname);
  487. libdax_msgs_submit(libdax_messenger, -1, 0x00020005,
  488. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
  489. msg, errno, 0);
  490. return -1;
  491. }
  492. sg_fcntl_lock(&fd, fname, F_WRLCK, 1);
  493. return fd;
  494. }
  495. /* ts A60926 */
  496. static int sg_release_siblings(int sibling_fds[],
  497. char sibling_fnames[][BURN_OS_SG_MAX_NAMELEN],
  498. int *sibling_count)
  499. {
  500. int i;
  501. char msg[81];
  502. for(i= 0; i < *sibling_count; i++)
  503. sg_close_drive_fd(sibling_fnames[i], -1, &(sibling_fds[i]), 0);
  504. if(*sibling_count > 0) {
  505. sprintf(msg, "Closed %d O_EXCL scsi siblings", *sibling_count);
  506. libdax_msgs_submit(libdax_messenger, -1, 0x00020007,
  507. LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_HIGH, msg, 0,0);
  508. }
  509. *sibling_count = 0;
  510. return 1;
  511. }
  512. /* ts A60926 */
  513. static int sg_close_drive(struct burn_drive *d)
  514. {
  515. int ret;
  516. if (!burn_drive_is_open(d))
  517. return 0;
  518. sg_release_siblings(d->sibling_fds, d->sibling_fnames,
  519. &(d->sibling_count));
  520. ret = sg_close_drive_fd(d->devname, d->global_index, &(d->fd), 0);
  521. return ret;
  522. }
  523. /* ts A60926 */
  524. static int sg_open_scsi_siblings(char *path, int driveno,
  525. int sibling_fds[],
  526. char sibling_fnames[][BURN_OS_SG_MAX_NAMELEN],
  527. int *sibling_count,
  528. int host_no, int channel_no, int id_no, int lun_no)
  529. {
  530. int tld, i, ret, fd, i_bus_no = -1;
  531. int i_host_no = -1, i_channel_no = -1, i_target_no = -1, i_lun_no = -1;
  532. char msg[161], fname[81];
  533. struct stat stbuf;
  534. dev_t last_rdev = 0, path_rdev;
  535. static char tldev[][81]= {"/dev/sr%d", "/dev/scd%d", "/dev/sg%d", ""};
  536. /* ts A70609: removed "/dev/st%d" */
  537. if(stat(path, &stbuf) == -1)
  538. return 0;
  539. path_rdev = stbuf.st_rdev;
  540. sg_select_device_family();
  541. if (linux_sg_device_family[0] == 0)
  542. return 1;
  543. if(host_no < 0 || id_no < 0 || channel_no < 0 || lun_no < 0)
  544. return(2);
  545. if(*sibling_count > 0)
  546. sg_release_siblings(sibling_fds, sibling_fnames,
  547. sibling_count);
  548. for (tld = 0; tldev[tld][0] != 0; tld++) {
  549. if (strcmp(tldev[tld], linux_sg_device_family)==0)
  550. continue;
  551. for (i = 0; i < 32; i++) {
  552. sprintf(fname, tldev[tld], i);
  553. if(stat(fname, &stbuf) == -1)
  554. continue;
  555. if (path_rdev == stbuf.st_rdev)
  556. continue;
  557. if (*sibling_count > 0 && last_rdev == stbuf.st_rdev)
  558. continue;
  559. ret = sg_obtain_scsi_adr(fname, &i_bus_no, &i_host_no,
  560. &i_channel_no, &i_target_no, &i_lun_no);
  561. if (ret <= 0)
  562. continue;
  563. if (i_host_no != host_no || i_channel_no != channel_no)
  564. continue;
  565. if (i_target_no != id_no || i_lun_no != lun_no)
  566. continue;
  567. fd = sg_open_drive_fd(fname, 0);
  568. if (fd < 0)
  569. goto failed;
  570. if (*sibling_count>=BURN_OS_SG_MAX_SIBLINGS) {
  571. sprintf(msg, "Too many scsi siblings of '%s'",
  572. path);
  573. libdax_msgs_submit(libdax_messenger,
  574. driveno, 0x00020006,
  575. LIBDAX_MSGS_SEV_FATAL,
  576. LIBDAX_MSGS_PRIO_HIGH, msg, 0, 0);
  577. goto failed;
  578. }
  579. sprintf(msg, "Opened O_EXCL scsi sibling '%s' of '%s'",
  580. fname, path);
  581. libdax_msgs_submit(libdax_messenger, driveno,
  582. 0x00020004,
  583. LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_HIGH,
  584. msg, 0, 0);
  585. sibling_fds[*sibling_count] = fd;
  586. strcpy(sibling_fnames[*sibling_count], fname);
  587. (*sibling_count)++;
  588. last_rdev= stbuf.st_rdev;
  589. }
  590. }
  591. return 1;
  592. failed:;
  593. sg_release_siblings(sibling_fds, sibling_fnames, sibling_count);
  594. return 0;
  595. }
  596. #define Libburn_drive_new_deaL 1
  597. #ifdef Libburn_drive_new_deaL
  598. /* ts A80731 */
  599. static int is_ata_drive(char *fname)
  600. {
  601. int fd;
  602. struct hd_driveid tm;
  603. fd = sg_open_drive_fd(fname, 1);
  604. if (fd == -1) {
  605. if (linux_ata_enumerate_verbous)
  606. fprintf(stderr,"open failed, errno=%d '%s'\n",
  607. errno, strerror(errno));
  608. return 0;
  609. }
  610. memset(&tm, 0, sizeof(tm));
  611. ioctl(fd, HDIO_GET_IDENTITY, &tm);
  612. /* not atapi */
  613. if (!(tm.config & 0x8000) || (tm.config & 0x4000)) {
  614. if (linux_ata_enumerate_verbous)
  615. fprintf(stderr, "not marked as ATAPI\n");
  616. sg_close_drive_fd(fname, -1, &fd, 0);
  617. return 0;
  618. }
  619. /* if SG_IO fails on an atapi device, we should stop trying to
  620. use hd* devices */
  621. if (sgio_test(fd) == -1) {
  622. if (linux_ata_enumerate_verbous)
  623. fprintf(stderr,
  624. "FATAL: sgio_test() failed: errno=%d '%s'\n",
  625. errno, strerror(errno));
  626. sg_close_drive_fd(fname, -1, &fd, 0);
  627. return 0;
  628. }
  629. if (sg_close_drive_fd(fname, -1, &fd, 1) <= 0) {
  630. if (linux_ata_enumerate_verbous)
  631. fprintf(stderr,
  632. "cannot close properly, errno=%d '%s'\n",
  633. errno, strerror(errno));
  634. return 0;
  635. }
  636. return 1;
  637. }
  638. static int is_scsi_drive(char *fname, int *bus_no, int *host_no,
  639. int *channel_no, int *target_no, int *lun_no)
  640. {
  641. int fd, sid_ret = 0, ret;
  642. struct sg_scsi_id sid;
  643. int sibling_fds[BURN_OS_SG_MAX_SIBLINGS], sibling_count= 0;
  644. char sibling_fnames[BURN_OS_SG_MAX_SIBLINGS][BURN_OS_SG_MAX_NAMELEN];
  645. fd = sg_open_drive_fd(fname, 1);
  646. if (fd == -1) {
  647. if (linux_sg_enumerate_debug)
  648. fprintf(stderr, "open failed, errno=%d '%s'\n",
  649. errno, strerror(errno));
  650. return 0;
  651. }
  652. sid_ret = ioctl(fd, SG_GET_SCSI_ID, &sid);
  653. if (sid_ret == -1) {
  654. sid.scsi_id = -1; /* mark SCSI address as invalid */
  655. if(linux_sg_enumerate_debug)
  656. fprintf(stderr,
  657. "ioctl(SG_GET_SCSI_ID) failed, errno=%d '%s' , ",
  658. errno, strerror(errno));
  659. if (sgio_test(fd) == -1) {
  660. if (linux_sg_enumerate_debug)
  661. fprintf(stderr,
  662. "FATAL: sgio_test() failed: errno=%d '%s'",
  663. errno, strerror(errno));
  664. sg_close_drive_fd(fname, -1, &fd, 0);
  665. return 0;
  666. }
  667. #ifdef CDROM_DRIVE_STATUS
  668. /* http://developer.osdl.org/dev/robustmutexes/
  669. src/fusyn.hg/Documentation/ioctl/cdrom.txt */
  670. sid_ret = ioctl(fd, CDROM_DRIVE_STATUS, 0);
  671. if(linux_sg_enumerate_debug)
  672. fprintf(stderr,
  673. "ioctl(CDROM_DRIVE_STATUS) = %d , ",
  674. sid_ret);
  675. if (sid_ret != -1 && sid_ret != CDS_NO_INFO)
  676. sid.scsi_type = TYPE_ROM;
  677. else
  678. sid_ret = -1;
  679. #endif /* CDROM_DRIVE_STATUS */
  680. }
  681. #ifdef SCSI_IOCTL_GET_BUS_NUMBER
  682. /* Hearsay A61005 */
  683. if (ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, bus_no) == -1)
  684. *bus_no = -1;
  685. #endif
  686. if (sg_close_drive_fd(fname, -1, &fd,
  687. sid.scsi_type == TYPE_ROM ) <= 0) {
  688. if (linux_sg_enumerate_debug)
  689. fprintf(stderr,
  690. "cannot close properly, errno=%d '%s'\n",
  691. errno, strerror(errno));
  692. return 0;
  693. }
  694. if ( (sid_ret == -1 || sid.scsi_type != TYPE_ROM)
  695. && !linux_sg_accept_any_type) {
  696. if (linux_sg_enumerate_debug)
  697. fprintf(stderr, "sid.scsi_type = %d (!= TYPE_ROM)\n",
  698. sid.scsi_type);
  699. return 0;
  700. }
  701. if (sid_ret == -1 || sid.scsi_id < 0) {
  702. /* ts A61211 : employ a more general ioctl */
  703. ret = sg_obtain_scsi_adr(fname, bus_no, host_no,
  704. channel_no, target_no, lun_no);
  705. if (ret>0) {
  706. sid.host_no = *host_no;
  707. sid.channel = *channel_no;
  708. sid.scsi_id = *target_no;
  709. sid.lun = *lun_no;
  710. } else {
  711. if (linux_sg_enumerate_debug)
  712. fprintf(stderr,
  713. "sg_obtain_scsi_adr() failed\n");
  714. return 0;
  715. }
  716. }
  717. /* ts A60927 : trying to do locking with growisofs */
  718. if(burn_sg_open_o_excl>1) {
  719. ret = sg_open_scsi_siblings(
  720. fname, -1, sibling_fds, sibling_fnames,
  721. &sibling_count,
  722. sid.host_no, sid.channel,
  723. sid.scsi_id, sid.lun);
  724. if (ret<=0) {
  725. if (linux_sg_enumerate_debug)
  726. fprintf(stderr, "cannot lock siblings\n");
  727. sg_handle_busy_device(fname, 0);
  728. return 0;
  729. }
  730. /* the final occupation will be done in sg_grab() */
  731. sg_release_siblings(sibling_fds, sibling_fnames,
  732. &sibling_count);
  733. }
  734. #ifdef SCSI_IOCTL_GET_BUS_NUMBER
  735. if(*bus_no == -1)
  736. *bus_no = 1000 * (sid.host_no + 1) + sid.channel;
  737. #else
  738. *bus_no = sid.host_no;
  739. #endif
  740. *host_no= sid.host_no;
  741. *channel_no= sid.channel;
  742. *target_no= sid.scsi_id;
  743. *lun_no= sid.lun;
  744. return 1;
  745. }
  746. #endif /* Libburn_drive_new_deaL */
  747. /** Speciality of GNU/Linux: detect non-SCSI ATAPI (EIDE) which will from
  748. then on used used via generic SCSI as is done with (emulated) SCSI drives */
  749. static void ata_enumerate(void)
  750. {
  751. #ifdef Libburn_drive_new_deaL
  752. int ret;
  753. #else
  754. struct hd_driveid tm;
  755. int fd;
  756. #endif
  757. int i;
  758. char fname[10];
  759. if (linux_ata_enumerate_verbous)
  760. fprintf(stderr, "libburn_debug: linux_ata_device_family = %s\n",
  761. linux_ata_device_family);
  762. if (linux_ata_device_family[0] == 0)
  763. return;
  764. for (i = 0; i < 26; i++) {
  765. sprintf(fname, linux_ata_device_family, 'a' + i);
  766. if (linux_ata_enumerate_verbous)
  767. fprintf(stderr, "libburn_debug: %s : ", fname);
  768. /* ts A51221 */
  769. if (burn_drive_is_banned(fname)) {
  770. if (linux_ata_enumerate_verbous)
  771. fprintf(stderr, "not in whitelist\n");
  772. continue;
  773. }
  774. #ifdef Libburn_drive_new_deaL
  775. ret = is_ata_drive(fname);
  776. if (ret < 0)
  777. break;
  778. if (ret == 0)
  779. continue;
  780. #else /* Libburn_drive_new_deaL */
  781. fd = sg_open_drive_fd(fname, 1);
  782. if (fd == -1) {
  783. if (linux_ata_enumerate_verbous)
  784. fprintf(stderr,"open failed, errno=%d '%s'\n",
  785. errno, strerror(errno));
  786. continue;
  787. }
  788. /* found a drive */
  789. ioctl(fd, HDIO_GET_IDENTITY, &tm);
  790. /* not atapi */
  791. if (!(tm.config & 0x8000) || (tm.config & 0x4000)) {
  792. if (linux_ata_enumerate_verbous)
  793. fprintf(stderr, "not marked as ATAPI\n");
  794. sg_close_drive_fd(fname, -1, &fd, 0);
  795. continue;
  796. }
  797. /* if SG_IO fails on an atapi device, we should stop trying to
  798. use hd* devices */
  799. if (sgio_test(fd) == -1) {
  800. if (linux_ata_enumerate_verbous)
  801. fprintf(stderr,
  802. "FATAL: sgio_test() failed: errno=%d '%s'\n",
  803. errno, strerror(errno));
  804. sg_close_drive_fd(fname, -1, &fd, 0);
  805. return;
  806. }
  807. if (sg_close_drive_fd(fname, -1, &fd, 1) <= 0) {
  808. if (linux_ata_enumerate_verbous)
  809. fprintf(stderr,
  810. "cannot close properly, errno=%d '%s'\n",
  811. errno, strerror(errno));
  812. continue;
  813. }
  814. #endif /* Libburn_drive_new_deaL */
  815. if (linux_ata_enumerate_verbous)
  816. fprintf(stderr, "accepting as drive without SCSI address\n");
  817. enumerate_common(fname, -1, -1, -1, -1, -1);
  818. }
  819. }
  820. /** Detects (probably emulated) SCSI drives */
  821. static void sg_enumerate(void)
  822. {
  823. #ifdef Libburn_drive_new_deaL
  824. #else
  825. struct sg_scsi_id sid;
  826. int fd, sibling_fds[BURN_OS_SG_MAX_SIBLINGS], sibling_count= 0;
  827. char sibling_fnames[BURN_OS_SG_MAX_SIBLINGS][BURN_OS_SG_MAX_NAMELEN];
  828. int sid_ret = 0;
  829. #endif
  830. int i, ret;
  831. int bus_no= -1, host_no= -1, channel_no= -1, target_no= -1, lun_no= -1;
  832. char fname[17];
  833. sg_select_device_family();
  834. if (linux_sg_enumerate_debug)
  835. fprintf(stderr, "libburn_debug: linux_sg_device_family = %s\n",
  836. linux_sg_device_family);
  837. if (linux_sg_device_family[0] == 0)
  838. return;
  839. for (i = 0; i < 32; i++) {
  840. sprintf(fname, linux_sg_device_family, i);
  841. /* ts A80702 */
  842. sg_exchange_scd_for_sr(fname, 0);
  843. if (linux_sg_enumerate_debug)
  844. fprintf(stderr, "libburn_debug: %s : ", fname);
  845. /* ts A51221 */
  846. if (burn_drive_is_banned(fname)) {
  847. if (linux_sg_enumerate_debug)
  848. fprintf(stderr, "not in whitelist\n");
  849. continue;
  850. }
  851. #ifdef Libburn_drive_new_deaL
  852. ret = is_scsi_drive(fname, &bus_no, &host_no, &channel_no,
  853. &target_no, &lun_no);
  854. if (ret < 0)
  855. break;
  856. if (ret == 0)
  857. continue;
  858. if (linux_sg_enumerate_debug)
  859. fprintf(stderr, "accepting as SCSI %d,%d,%d,%d bus=%d\n",
  860. host_no, channel_no, target_no, lun_no, bus_no);
  861. enumerate_common(fname, bus_no, host_no, channel_no,
  862. target_no, lun_no);
  863. #else /* Libburn_drive_new_deaL */
  864. /* ts A60927 */
  865. fd = sg_open_drive_fd(fname, 1);
  866. if (fd == -1) {
  867. if (linux_sg_enumerate_debug)
  868. fprintf(stderr, "open failed, errno=%d '%s'\n",
  869. errno, strerror(errno));
  870. continue;
  871. }
  872. /* found a drive */
  873. sid_ret = ioctl(fd, SG_GET_SCSI_ID, &sid);
  874. if (sid_ret == -1) {
  875. sid.scsi_id = -1; /* mark SCSI address as invalid */
  876. if(linux_sg_enumerate_debug)
  877. fprintf(stderr,
  878. "ioctl(SG_GET_SCSI_ID) failed, errno=%d '%s' , ",
  879. errno, strerror(errno));
  880. if (sgio_test(fd) == -1) {
  881. if (linux_sg_enumerate_debug)
  882. fprintf(stderr,
  883. "FATAL: sgio_test() failed: errno=%d '%s'",
  884. errno, strerror(errno));
  885. sg_close_drive_fd(fname, -1, &fd, 0);
  886. continue;
  887. }
  888. #ifdef CDROM_DRIVE_STATUS
  889. /* ts A61211 : not widening old acceptance range */
  890. if (strcmp(linux_sg_device_family,"/dev/sg%d") != 0) {
  891. /* http://developer.osdl.org/dev/robustmutexes/
  892. src/fusyn.hg/Documentation/ioctl/cdrom.txt */
  893. sid_ret = ioctl(fd, CDROM_DRIVE_STATUS, 0);
  894. if(linux_sg_enumerate_debug)
  895. fprintf(stderr,
  896. "ioctl(CDROM_DRIVE_STATUS) = %d , ",
  897. sid_ret);
  898. if (sid_ret != -1 && sid_ret != CDS_NO_INFO)
  899. sid.scsi_type = TYPE_ROM;
  900. else
  901. sid_ret = -1;
  902. }
  903. #endif /* CDROM_DRIVE_STATUS */
  904. }
  905. #ifdef SCSI_IOCTL_GET_BUS_NUMBER
  906. /* Hearsay A61005 */
  907. if (ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &bus_no) == -1)
  908. bus_no = -1;
  909. #endif
  910. if (sg_close_drive_fd(fname, -1, &fd,
  911. sid.scsi_type == TYPE_ROM ) <= 0) {
  912. if (linux_sg_enumerate_debug)
  913. fprintf(stderr,
  914. "cannot close properly, errno=%d '%s'\n",
  915. errno, strerror(errno));
  916. continue;
  917. }
  918. if ( (sid_ret == -1 || sid.scsi_type != TYPE_ROM)
  919. && !linux_sg_accept_any_type) {
  920. if (linux_sg_enumerate_debug)
  921. fprintf(stderr, "sid.scsi_type = %d (!= TYPE_ROM)\n",
  922. sid.scsi_type);
  923. continue;
  924. }
  925. if (sid_ret == -1 || sid.scsi_id < 0) {
  926. /* ts A61211 : employ a more general ioctl */
  927. ret = sg_obtain_scsi_adr(fname, &bus_no, &host_no,
  928. &channel_no, &target_no, &lun_no);
  929. if (ret>0) {
  930. sid.host_no = host_no;
  931. sid.channel = channel_no;
  932. sid.scsi_id = target_no;
  933. sid.lun = lun_no;
  934. } else {
  935. if (linux_sg_enumerate_debug)
  936. fprintf(stderr,
  937. "sg_obtain_scsi_adr() failed\n");
  938. continue;
  939. }
  940. }
  941. /* ts A60927 : trying to do locking with growisofs */
  942. if(burn_sg_open_o_excl>1) {
  943. ret = sg_open_scsi_siblings(
  944. fname, -1, sibling_fds, sibling_fnames,
  945. &sibling_count,
  946. sid.host_no, sid.channel,
  947. sid.scsi_id, sid.lun);
  948. if (ret<=0) {
  949. if (linux_sg_enumerate_debug)
  950. fprintf(stderr, "cannot lock siblings\n");
  951. sg_handle_busy_device(fname, 0);
  952. continue;
  953. }
  954. /* the final occupation will be done in sg_grab() */
  955. sg_release_siblings(sibling_fds, sibling_fnames,
  956. &sibling_count);
  957. }
  958. #ifdef SCSI_IOCTL_GET_BUS_NUMBER
  959. if(bus_no == -1)
  960. bus_no = 1000 * (sid.host_no + 1) + sid.channel;
  961. #else
  962. bus_no = sid.host_no;
  963. #endif
  964. if (linux_sg_enumerate_debug)
  965. fprintf(stderr, "accepting as SCSI %d,%d,%d,%d bus=%d\n",
  966. sid.host_no, sid.channel, sid.scsi_id, sid.lun,
  967. bus_no);
  968. enumerate_common(fname, bus_no, sid.host_no, sid.channel,
  969. sid.scsi_id, sid.lun);
  970. #endif /* Libburn_drive_new_deaL */
  971. }
  972. }
  973. #ifdef Libburn_drive_new_deaL
  974. /* ts A80805 : eventually produce the other official name of a device file */
  975. static int fname_other_name(char *fname, char other_name[80], int flag)
  976. {
  977. if(strncmp(fname, "/dev/sr", 7) == 0 &&
  978. (fname[7] >= '0' && fname[7] <= '9') &&
  979. (fname[8] == 0 ||
  980. (fname[8] >= '0' && fname[8] <= '9' && fname[9] == 0))) {
  981. sprintf(other_name, "/dev/scd%s", fname + 7);
  982. return 1;
  983. }
  984. if(strncmp(fname, "/dev/scd", 8) == 0 &&
  985. (fname[8] >= '0' && fname[8] <= '9') &&
  986. (fname[9] == 0 ||
  987. (fname[9] >= '0' && fname[9] <= '9' && fname[10] == 0))) {
  988. sprintf(other_name, "/dev/sr%s", fname + 8);
  989. return 1;
  990. }
  991. return 0;
  992. }
  993. /* ts A80805 */
  994. static int fname_drive_is_listed(char *fname, int flag)
  995. {
  996. char other_fname[80];
  997. if (burn_drive_is_listed(fname, NULL, 0))
  998. return 1;
  999. if (fname_other_name(fname, other_fname, 0) > 0)
  1000. if (burn_drive_is_listed(other_fname, NULL, 0))
  1001. return 2;
  1002. return 0;
  1003. }
  1004. /* ts A80731 : Directly open the given address.
  1005. @param flag bit0= do not complain about missing file
  1006. bit1= do not check whether drive is already listed
  1007. */
  1008. static int fname_enumerate(char *fname, int flag)
  1009. {
  1010. int is_ata= 0, is_scsi= 0;
  1011. int bus_no= -1, host_no= -1, channel_no= -1, target_no= -1, lun_no= -1;
  1012. char msg[BURN_DRIVE_ADR_LEN + 80];
  1013. struct stat stbuf;
  1014. if (!(flag & 2))
  1015. if (fname_drive_is_listed(fname, 0))
  1016. return 2;
  1017. if (stat(fname, &stbuf) == -1) {
  1018. sprintf(msg, "File object '%s' not found", fname);
  1019. if (!(flag & 1))
  1020. libdax_msgs_submit(libdax_messenger, -1, 0x0002000b,
  1021. LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  1022. msg, 0, 0);
  1023. return -1;
  1024. }
  1025. is_ata = is_ata_drive(fname);
  1026. if (is_ata < 0)
  1027. return -1;
  1028. if (!is_ata)
  1029. is_scsi = is_scsi_drive(fname, &bus_no, &host_no, &channel_no,
  1030. &target_no, &lun_no);
  1031. if (is_scsi < 0)
  1032. return -1;
  1033. if (is_ata == 0 && is_scsi == 0)
  1034. return 0;
  1035. if (linux_sg_enumerate_debug)
  1036. fprintf(stderr,
  1037. "(single) accepting as SCSI %d,%d,%d,%d bus=%d\n",
  1038. host_no, channel_no, target_no, lun_no, bus_no);
  1039. enumerate_common(fname, bus_no, host_no, channel_no,
  1040. target_no, lun_no);
  1041. return 1;
  1042. }
  1043. /* ts A80731 : Directly open the given address from a single-item whitlist */
  1044. static int single_enumerate(int flag)
  1045. {
  1046. int ret, wl_count;
  1047. char *fname, msg[BURN_DRIVE_ADR_LEN + 80];
  1048. wl_count= burn_drive_whitelist_count();
  1049. if (wl_count != 1)
  1050. return 0;
  1051. fname= burn_drive_whitelist_item(0, 0);
  1052. if (fname == NULL)
  1053. return 0;
  1054. ret = fname_enumerate(fname, 2);
  1055. if (ret <= 0) {
  1056. sprintf(msg, "Cannot access '%s' as SG_IO CDROM drive", fname);
  1057. libdax_msgs_submit(libdax_messenger, -1, 0x0002000a,
  1058. LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  1059. msg, 0, 0);
  1060. ret = -1;
  1061. }
  1062. return ret;
  1063. }
  1064. /* ts A80801 : looking up drives listed in /proc/sys/dev/cdrom/info line like:
  1065. drive name: sr1 hdc hda sr0
  1066. @parm flag bit0= release list memory and exit
  1067. */
  1068. static int proc_sys_dev_cdrom_info(char ***list, int *count, int flag)
  1069. {
  1070. FILE *fp;
  1071. char line[1024], fname[1024 + 5], *cpt, *retpt, *list_data;
  1072. int maxl= 0, pass, i;
  1073. if (*list != NULL) {
  1074. if ((*list)[0] != NULL)
  1075. free((*list)[0]);
  1076. free(*list);
  1077. *list = NULL;
  1078. *count = 0;
  1079. }
  1080. if (flag & 1)
  1081. return 1;
  1082. *count = 0;
  1083. sg_evaluate_kernel();
  1084. if (sg_kernel_age < 2) /* addresses are not suitable for kernel 2.4 */
  1085. return 1;
  1086. fp = fopen("/proc/sys/dev/cdrom/info", "r");
  1087. if (fp == NULL)
  1088. return 0;
  1089. while (1) {
  1090. retpt = fgets(line, sizeof(line), fp);
  1091. if (retpt == NULL)
  1092. break;
  1093. if(strncmp(line, "drive name:", 11) == 0)
  1094. break;
  1095. }
  1096. fclose(fp);
  1097. if (retpt == NULL)
  1098. return 0;
  1099. strcpy(fname, "/dev/");
  1100. for(pass = 0; pass < 2; pass++) {
  1101. *count = 0;
  1102. cpt = line + 11;
  1103. while (*cpt != 0) {
  1104. for(; *cpt == ' ' || *cpt == '\t'; cpt++);
  1105. if (*cpt == 0 || *cpt == '\n')
  1106. break;
  1107. sscanf(cpt, "%s", fname + 5);
  1108. if (strlen(fname) > maxl)
  1109. maxl = strlen(fname);
  1110. if (pass == 1)
  1111. strcpy((*list)[*count], fname);
  1112. (*count)++;
  1113. for(cpt++; *cpt != ' ' && *cpt != '\t'
  1114. && *cpt != 0 && *cpt != '\n'; cpt++);
  1115. }
  1116. if (pass == 0) {
  1117. list_data = calloc(*count + 1, maxl+1);
  1118. *list = calloc(*count + 1, sizeof(char *));
  1119. if(list_data == NULL || *list == NULL) {
  1120. libdax_msgs_submit(libdax_messenger, -1,
  1121. 0x00000003,
  1122. LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH,
  1123. "Out of virtual memory", 0, 0);
  1124. if (list_data != NULL)
  1125. free(list_data);
  1126. if (*list != NULL)
  1127. free((char *) *list);
  1128. return -1;
  1129. }
  1130. for (i = 0; i <= *count; i++)
  1131. (*list)[i] = list_data + i * (maxl + 1);
  1132. }
  1133. }
  1134. return 1;
  1135. }
  1136. static int add_proc_info_drives(int flag)
  1137. {
  1138. int ret, list_count, count = 0, i;
  1139. char **list= NULL;
  1140. ret = proc_sys_dev_cdrom_info(&list, &list_count, 0);
  1141. if (ret <= 0)
  1142. return ret;
  1143. for (i = 0; i < list_count; i++) {
  1144. if (burn_drive_is_banned(list[i]))
  1145. continue;
  1146. ret = fname_enumerate(list[i], 1);
  1147. if (ret == 1)
  1148. count++;
  1149. }
  1150. proc_sys_dev_cdrom_info(&list, &list_count, 1); /* free memory */
  1151. return 1 + count;
  1152. }
  1153. #endif /* Libburn_drive_new_deaL */
  1154. /* ts A61115 */
  1155. /* ----------------------------------------------------------------------- */
  1156. /* PORTING: Private functions which contain publicly needed functionality. */
  1157. /* Their portable part must be performed. So it is probably best */
  1158. /* to replace the non-portable part and to call these functions */
  1159. /* in your port, too. */
  1160. /* ----------------------------------------------------------------------- */
  1161. /** Wraps a detected drive into libburn structures and hands it over to
  1162. libburn drive list.
  1163. */
  1164. /* ts A60923 - A61005 : introduced new SCSI parameters */
  1165. /* ts A61021 : moved non os-specific code to spc,sbc,mmc,drive */
  1166. static void enumerate_common(char *fname, int bus_no, int host_no,
  1167. int channel_no, int target_no, int lun_no)
  1168. {
  1169. int ret, i;
  1170. struct burn_drive out;
  1171. /* General libburn drive setup */
  1172. burn_setup_drive(&out, fname);
  1173. /* This transport adapter uses SCSI-family commands and models
  1174. (seems the adapter would know better than its boss, if ever) */
  1175. ret = burn_scsi_setup_drive(&out, bus_no, host_no, channel_no,
  1176. target_no, lun_no, 0);
  1177. if (ret<=0)
  1178. return;
  1179. /* PORTING: ------------------- non portable part --------------- */
  1180. /* Operating system adapter is GNU/Linux Generic SCSI (sg) */
  1181. /* Adapter specific handles and data */
  1182. out.fd = -1337;
  1183. out.sibling_count = 0;
  1184. for(i= 0; i<BURN_OS_SG_MAX_SIBLINGS; i++)
  1185. out.sibling_fds[i] = -1337;
  1186. /* PORTING: ---------------- end of non portable part ------------ */
  1187. /* Adapter specific functions with standardized names */
  1188. out.grab = sg_grab;
  1189. out.release = sg_release;
  1190. out.drive_is_open= sg_drive_is_open;
  1191. out.issue_command = sg_issue_command;
  1192. /* Finally register drive and inquire drive information */
  1193. burn_drive_finish_enum(&out);
  1194. }
  1195. /* ts A61115 */
  1196. /* ------------------------------------------------------------------------ */
  1197. /* PORTING: Public functions. These MUST be ported. */
  1198. /* ------------------------------------------------------------------------ */
  1199. /** Returns the id string of the SCSI transport adapter and eventually
  1200. needed operating system facilities.
  1201. This call is usable even if sg_initialize() was not called yet. In that
  1202. case a preliminary constant message might be issued if detailed info is
  1203. not available yet.
  1204. @param msg returns id string
  1205. @param flag unused yet, submit 0
  1206. @return 1 = success, <=0 = failure
  1207. */
  1208. int sg_id_string(char msg[1024], int flag)
  1209. {
  1210. strcpy(msg, "internal GNU/Linux SG_IO adapter sg-linux");
  1211. return 1;
  1212. }
  1213. /** Performs global initialization of the SCSI transport adapter and eventually
  1214. needed operating system facilities. Checks for compatibility supporting
  1215. software components.
  1216. @param msg returns ids and/or error messages of eventual helpers
  1217. @param flag unused yet, submit 0
  1218. @return 1 = success, <=0 = failure
  1219. */
  1220. int sg_initialize(char msg[1024], int flag)
  1221. {
  1222. return sg_id_string(msg, 0);
  1223. }
  1224. /** Performs global finalization of the SCSI transport adapter and eventually
  1225. needed operating system facilities. Releases globally aquired resources.
  1226. @param flag unused yet, submit 0
  1227. @return 1 = success, <=0 = failure
  1228. */
  1229. int sg_shutdown(int flag)
  1230. {
  1231. return 1;
  1232. }
  1233. /** Finalizes BURN_OS_TRANSPORT_DRIVE_ELEMENTS, the components of
  1234. struct burn_drive which are defined in os-*.h.
  1235. The eventual initialization of those components was made underneath
  1236. scsi_enumerate_drives().
  1237. This will be called when a burn_drive gets disposed.
  1238. @param d the drive to be finalized
  1239. @param flag unused yet, submit 0
  1240. @return 1 = success, <=0 = failure
  1241. */
  1242. int sg_dispose_drive(struct burn_drive *d, int flag)
  1243. {
  1244. return 1;
  1245. }
  1246. /** PORTING:
  1247. In this GNU/Linux implementation, this function mirrors the enumeration
  1248. done in sg_enumerate and ata_enumerate(). It would be better to base those
  1249. functions on this sg_give_next_adr() but the situation is not inviting.
  1250. */
  1251. /* ts A60922 ticket 33 : called from drive.c */
  1252. /** Returns the next index number and the next enumerated drive address.
  1253. The enumeration has to cover all available and accessible drives. It is
  1254. allowed to return addresses of drives which are not available but under
  1255. some (even exotic) circumstances could be available. It is on the other
  1256. hand allowed, only to hand out addresses which can really be used right
  1257. in the moment of this call. (This implementation chooses the former.)
  1258. @param idx An opaque handle. Make no own theories about it.
  1259. @param adr Takes the reply
  1260. @param adr_size Gives maximum size of reply including final 0
  1261. @param initialize 1 = start new,
  1262. 0 = continue, use no other values for now
  1263. -1 = finish
  1264. @return 1 = reply is a valid address , 0 = no further address available
  1265. -1 = severe error (e.g. adr_size too small)
  1266. */
  1267. int sg_give_next_adr(burn_drive_enumerator_t *idx,
  1268. char adr[], int adr_size, int initialize)
  1269. {
  1270. /* os-linux.h : typedef int burn_drive_enumerator_t; */
  1271. static int sg_limit = 32, ata_limit = 26;
  1272. int baseno = 0, i;
  1273. char other_name[80];
  1274. if (initialize == -1) {
  1275. proc_sys_dev_cdrom_info(&(idx->info_list), &(idx->info_count),
  1276. 1);
  1277. return 0;
  1278. }
  1279. sg_select_device_family();
  1280. if (linux_sg_device_family[0] == 0)
  1281. sg_limit = 0;
  1282. if (linux_ata_device_family[0] == 0)
  1283. ata_limit = 0;
  1284. if (initialize == 1) {
  1285. idx->pos = -1;
  1286. idx->info_count= 0;
  1287. idx->info_list= NULL;
  1288. proc_sys_dev_cdrom_info(&(idx->info_list), &(idx->info_count),
  1289. 0);
  1290. }
  1291. (idx->pos)++;
  1292. if (idx->pos >= sg_limit)
  1293. goto next_ata;
  1294. if (adr_size < 11)
  1295. return -1;
  1296. sprintf(adr, linux_sg_device_family, idx->pos);
  1297. sg_exchange_scd_for_sr(adr, 0);
  1298. goto return_1_pre_proc;
  1299. next_ata:;
  1300. baseno += sg_limit;
  1301. if (idx->pos - baseno >= ata_limit)
  1302. goto next_proc_info;
  1303. if (adr_size < 9)
  1304. return -1;
  1305. sprintf(adr, linux_ata_device_family, 'a' + (idx->pos - baseno));
  1306. goto return_1_pre_proc;
  1307. next_proc_info:;
  1308. baseno += ata_limit;
  1309. for (i = 0; i < idx->info_count; i++) {
  1310. if ((idx->info_list)[i][0] == 0)
  1311. continue;
  1312. if (baseno == idx->pos) {
  1313. if (adr_size < strlen((idx->info_list)[i]) + 1)
  1314. return -1;
  1315. strcpy(adr, (idx->info_list)[i]);
  1316. return 1;
  1317. }
  1318. baseno++;
  1319. }
  1320. return 0;
  1321. return_1_pre_proc:;
  1322. for (i = 0; i < idx->info_count; i++) {
  1323. if (strcmp((idx->info_list)[i], adr) == 0)
  1324. (idx->info_list)[i][0] = 0;
  1325. if (fname_other_name(adr, other_name, 0) > 0)
  1326. if (strcmp((idx->info_list)[i], other_name) == 0)
  1327. (idx->info_list)[i][0] = 0;
  1328. }
  1329. return 1;
  1330. }
  1331. /** Brings all available, not-whitelist-banned, and accessible drives into
  1332. libburn's list of drives.
  1333. */
  1334. /** PORTING:
  1335. If not stricken with an incompletely unified situation like in GNU/Linux
  1336. one would rather implement this by a loop calling sg_give_next_adr().
  1337. If needed with your sg_give_next_adr() results, do a test for existence
  1338. and accessability. If burn activities are prone to external interference
  1339. on your system it is also necessary to obtain exclusive access locks on
  1340. the drives.
  1341. Hand over each accepted drive to enumerate_common() resp. its replacement
  1342. within your port.
  1343. See FreeBSD port sketch sg-freebsd-port.c for such an implementation.
  1344. */
  1345. /* ts A61115: replacing call to sg-implementation internals from drive.c */
  1346. int scsi_enumerate_drives(void)
  1347. {
  1348. #ifdef Libburn_drive_new_deaL
  1349. int ret;
  1350. #endif
  1351. #ifdef Libburn_drive_new_deaL
  1352. /* Direct examination of eventually single whitelisted name */
  1353. ret = single_enumerate(0);
  1354. if (ret < 0)
  1355. return -1;
  1356. if (ret > 0)
  1357. return 1;
  1358. #endif /* Libburn_drive_new_deaL */
  1359. sg_enumerate();
  1360. ata_enumerate();
  1361. #ifdef Libburn_drive_new_deaL
  1362. add_proc_info_drives(0);
  1363. #endif /* Libburn_drive_new_deaL */
  1364. return 1;
  1365. }
  1366. /** Tells wether libburn has the given drive in use or exclusively reserved.
  1367. If it is "open" then libburn will eventually call sg_release() on it when
  1368. it is time to give up usage resp. reservation.
  1369. */
  1370. /** Published as burn_drive.drive_is_open() */
  1371. int sg_drive_is_open(struct burn_drive * d)
  1372. {
  1373. /* a bit more detailed case distinction than needed */
  1374. if (d->fd == -1337)
  1375. return 0;
  1376. if (d->fd < 0)
  1377. return 0;
  1378. return 1;
  1379. }
  1380. /** Opens the drive for SCSI commands and - if burn activities are prone
  1381. to external interference on your system - obtains an exclusive access lock
  1382. on the drive. (Note: this is not physical tray locking.)
  1383. A drive that has been opened with sg_grab() will eventually be handed
  1384. over to sg_release() for closing and unreserving.
  1385. */
  1386. int sg_grab(struct burn_drive *d)
  1387. {
  1388. int fd, os_errno= 0, ret;
  1389. int max_tries = 3, tries = 0;
  1390. /* ts A60813 */
  1391. int open_mode = O_RDWR;
  1392. /* ts A60821
  1393. <<< debug: for tracing calls which might use open drive fds */
  1394. if (mmc_function_spy(d, "sg_grab") <= 0)
  1395. return 0;
  1396. /* ts A60813 - A60927
  1397. O_EXCL with devices is a non-POSIX feature
  1398. of Linux kernels. Possibly introduced 2002.
  1399. Mentioned in "The Linux SCSI Generic (sg) HOWTO".
  1400. */
  1401. if(burn_sg_open_o_excl)
  1402. open_mode |= O_EXCL;
  1403. /* ts A60813
  1404. O_NONBLOCK was hardcoded here. So it should stay default mode.
  1405. ts A70411
  1406. Switched to O_NDELAY for LKML statement 2007/4/11/141
  1407. */
  1408. if(burn_sg_open_o_nonblock)
  1409. open_mode |= O_NDELAY;
  1410. /* ts A60813 - A60822
  1411. After enumeration the drive fd is probably still open.
  1412. -1337 is the initial value of burn_drive.fd and the value after
  1413. relase of drive. Unclear why not the official error return
  1414. value -1 of open(2) war used. */
  1415. if(! burn_drive_is_open(d)) {
  1416. try_open:;
  1417. /* ts A60821
  1418. <<< debug: for tracing calls which might use open drive fds */
  1419. mmc_function_spy(NULL, "sg_grab ----------- opening");
  1420. /* ts A70409 : DDLP-B */
  1421. /* >>> obtain single lock on d->devname */
  1422. /* ts A60926 */
  1423. if(burn_sg_open_o_excl>1) {
  1424. fd = -1;
  1425. ret = sg_open_scsi_siblings(d->devname,
  1426. d->global_index,d->sibling_fds,
  1427. d->sibling_fnames,&(d->sibling_count),
  1428. d->host, d->channel, d->id, d->lun);
  1429. if(ret <= 0)
  1430. goto drive_is_in_use;
  1431. }
  1432. fd = open(d->devname, open_mode);
  1433. os_errno = errno;
  1434. if (fd >= 0) {
  1435. sg_fcntl_lock(&fd, d->devname, F_WRLCK, 1);
  1436. if (fd < 0)
  1437. goto drive_is_in_use;
  1438. }
  1439. } else
  1440. fd= d->fd;
  1441. if (fd >= 0) {
  1442. d->fd = fd;
  1443. fcntl(fd, F_SETOWN, getpid());
  1444. d->released = 0;
  1445. return 1;
  1446. } else if (errno == EBUSY)
  1447. goto drive_is_in_use;
  1448. libdax_msgs_submit(libdax_messenger, d->global_index, 0x00020003,
  1449. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
  1450. "Could not grab drive", os_errno, 0);
  1451. return 0;
  1452. drive_is_in_use:;
  1453. tries++;
  1454. if (tries < max_tries) {
  1455. usleep(2000000);
  1456. goto try_open;
  1457. }
  1458. libdax_msgs_submit(libdax_messenger, d->global_index,
  1459. 0x00020003,
  1460. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
  1461. "Could not grab drive - already in use", 0, 0);
  1462. sg_close_drive(d);
  1463. d->fd = -1337;
  1464. return 0;
  1465. }
  1466. /** PORTING: Is mainly about the call to sg_close_drive() and wether it
  1467. implements the demanded functionality.
  1468. */
  1469. /** Gives up the drive for SCSI commands and releases eventual access locks.
  1470. (Note: this is not physical tray locking.)
  1471. */
  1472. int sg_release(struct burn_drive *d)
  1473. {
  1474. /* ts A60821
  1475. <<< debug: for tracing calls which might use open drive fds */
  1476. if (mmc_function_spy(d, "sg_release") <= 0)
  1477. return 0;
  1478. if (d->fd < 1) {
  1479. burn_print(1, "release an ungrabbed drive. die\n");
  1480. return 0;
  1481. }
  1482. /* ts A60821
  1483. <<< debug: for tracing calls which might use open drive fds */
  1484. mmc_function_spy(NULL, "sg_release ----------- closing");
  1485. sg_close_drive(d);
  1486. return 0;
  1487. }
  1488. /** Sends a SCSI command to the drive, receives reply and evaluates wether
  1489. the command succeeded or shall be retried or finally failed.
  1490. Returned SCSI errors shall not lead to a return value indicating failure.
  1491. The callers get notified by c->error. An SCSI failure which leads not to
  1492. a retry shall be notified via scsi_notify_error().
  1493. @return: 1 success , <=0 failure
  1494. */
  1495. int sg_issue_command(struct burn_drive *d, struct command *c)
  1496. {
  1497. int done = 0, no_c_page = 0, usleep_time, i;
  1498. int err;
  1499. time_t start_time;
  1500. sg_io_hdr_t s;
  1501. /* ts A61030 */
  1502. static FILE *fp= NULL;
  1503. /* <<< ts A60821
  1504. debug: for tracing calls which might use open drive fds */
  1505. char buf[161];
  1506. sprintf(buf,"sg_issue_command d->fd= %d d->released= %d\n",
  1507. d->fd,d->released);
  1508. mmc_function_spy(NULL, buf);
  1509. /* ts A61030 */
  1510. if (burn_sg_log_scsi & 1) {
  1511. if (fp == NULL) {
  1512. fp= fopen("/tmp/libburn_sg_command_log", "a");
  1513. fprintf(fp,
  1514. "\n-----------------------------------------\n");
  1515. }
  1516. }
  1517. if (burn_sg_log_scsi & 3)
  1518. scsi_log_cmd(c,fp,0);
  1519. /* ts A61010 : with no fd there is no chance to send an ioctl */
  1520. if (d->fd < 0) {
  1521. c->error = 1;
  1522. return 0;
  1523. }
  1524. c->error = 0;
  1525. memset(&s, 0, sizeof(sg_io_hdr_t));
  1526. s.interface_id = 'S';
  1527. #ifdef Libburn_sgio_as_growisofS
  1528. /* ??? ts A91112 : does this speed up USB ? (from growisofs)
  1529. --- did not help
  1530. */
  1531. s.flags = SG_FLAG_DIRECT_IO;
  1532. #endif /* Libburn_sgio_as_growisofS */
  1533. if (c->dir == TO_DRIVE)
  1534. s.dxfer_direction = SG_DXFER_TO_DEV;
  1535. else if (c->dir == FROM_DRIVE)
  1536. s.dxfer_direction = SG_DXFER_FROM_DEV;
  1537. else if (c->dir == NO_TRANSFER) {
  1538. s.dxfer_direction = SG_DXFER_NONE;
  1539. /* ts A61007 */
  1540. /* a ssert(!c->page); */
  1541. no_c_page = 1;
  1542. }
  1543. s.cmd_len = c->oplen;
  1544. s.cmdp = c->opcode;
  1545. s.mx_sb_len = 32;
  1546. s.sbp = c->sense;
  1547. memset(c->sense, 0, sizeof(c->sense));
  1548. s.timeout = 200000;
  1549. if (c->page && !no_c_page) {
  1550. s.dxferp = c->page->data;
  1551. if (c->dir == FROM_DRIVE) {
  1552. /* ts A70519 : kernel 2.4 usb-storage seems to
  1553. expect exact dxfer_len for data
  1554. fetching commands.
  1555. */
  1556. if (c->dxfer_len >= 0)
  1557. s.dxfer_len = c->dxfer_len;
  1558. else
  1559. s.dxfer_len = BUFFER_SIZE;
  1560. /* touch page so we can use valgrind */
  1561. memset(c->page->data, 0, BUFFER_SIZE);
  1562. } else {
  1563. /* ts A61010 */
  1564. /* a ssert(c->page->bytes > 0); */
  1565. if (c->page->bytes <= 0) {
  1566. c->error = 1;
  1567. return 0;
  1568. }
  1569. s.dxfer_len = c->page->bytes;
  1570. }
  1571. } else {
  1572. s.dxferp = NULL;
  1573. s.dxfer_len = 0;
  1574. }
  1575. s.usr_ptr = c;
  1576. start_time = time(NULL);
  1577. for(i = 0; !done; i++) {
  1578. err = ioctl(d->fd, SG_IO, &s);
  1579. /* ts A61010 */
  1580. /* a ssert(err != -1); */
  1581. if (err == -1) {
  1582. libdax_msgs_submit(libdax_messenger,
  1583. d->global_index, 0x0002010c,
  1584. LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH,
  1585. "Failed to transfer command to drive",
  1586. errno, 0);
  1587. sg_close_drive(d);
  1588. d->released = 1;
  1589. d->busy = BURN_DRIVE_IDLE;
  1590. c->error = 1;
  1591. return -1;
  1592. }
  1593. #ifdef NIX
  1594. /* <<< */
  1595. if(0){
  1596. static int erst= 1;
  1597. static unsigned char b00_sense[22]= {
  1598. 0x72, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E,
  1599. 0x09, 0x0C, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
  1600. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  1601. if (erst > 0) {
  1602. s.sb_len_wr= 22;
  1603. memcpy(s.sbp, b00_sense, s.sb_len_wr);
  1604. erst--;
  1605. }
  1606. }
  1607. /* <<< */
  1608. #endif /* NIX */
  1609. if (s.sb_len_wr) {
  1610. if (!c->retry) {
  1611. c->error = 1;
  1612. /* A61106: rather than : return 1 */
  1613. goto ex;
  1614. }
  1615. switch (scsi_error(d, s.sbp, s.sb_len_wr)) {
  1616. case RETRY:
  1617. done = 0;
  1618. if (burn_sg_log_scsi & 3) {
  1619. scsi_log_err(c, fp, s.sbp, s.sb_len_wr,
  1620. s.duration, 1);
  1621. scsi_log_cmd(c,fp,0);
  1622. }
  1623. break;
  1624. case FAIL:
  1625. done = 1;
  1626. c->error = 1;
  1627. break;
  1628. }
  1629. /* ts A90921 :
  1630. Calming down retries and breaking up endless cycle
  1631. */
  1632. usleep_time = Libburn_sg_linux_retry_usleeP +
  1633. i * Libburn_sg_linux_retry_incR;
  1634. if (time(NULL) + usleep_time / 1000000 - start_time >
  1635. s.timeout / 1000 + 1) {
  1636. c->error = 1;
  1637. goto ex;
  1638. }
  1639. usleep(usleep_time);
  1640. } else {
  1641. done = 1;
  1642. }
  1643. }
  1644. /* ts A61106 */
  1645. ex:;
  1646. if (c->error) {
  1647. scsi_notify_error(d, c, s.sbp, s.sb_len_wr, 0);
  1648. } else if (s.host_status != Libburn_sg_host_oK ||
  1649. s.driver_status != Libburn_sg_driver_oK) {
  1650. char msg[161];
  1651. sprintf(msg,
  1652. "SCSI command %2.2Xh indicates host or driver error:",
  1653. (unsigned int) c->opcode[0]);
  1654. sprintf(msg+strlen(msg),
  1655. " host_status= %xh , driver_status= %xh",
  1656. (unsigned int) s.host_status,
  1657. (unsigned int) s.driver_status);
  1658. libdax_msgs_submit(libdax_messenger, d->global_index,
  1659. 0x0002013b,
  1660. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  1661. msg, 0, 0);
  1662. }
  1663. if (burn_sg_log_scsi & 3)
  1664. scsi_log_err(c, fp, s.sbp, s.sb_len_wr,
  1665. s.duration, c->error != 0);
  1666. return 1;
  1667. }
  1668. /* ts A60922 */
  1669. /** Tries to obtain SCSI address parameters.
  1670. @return 1 is success , 0 is failure
  1671. */
  1672. int sg_obtain_scsi_adr(char *path, int *bus_no, int *host_no, int *channel_no,
  1673. int *target_no, int *lun_no)
  1674. {
  1675. int fd, ret, l, open_mode = O_RDONLY;
  1676. struct my_scsi_idlun {
  1677. int x;
  1678. int host_unique_id;
  1679. };
  1680. struct my_scsi_idlun idlun;
  1681. /* valgrind called idlun unitialized because it is blind for ioctl */
  1682. memset(&idlun, 0, sizeof(struct my_scsi_idlun));
  1683. l = strlen(linux_ata_device_family) - 2;
  1684. if (l > 0 && strncmp(path, linux_ata_device_family, l) == 0
  1685. && path[7] >= 'a' && path[7] <= 'z' && path[8] == 0)
  1686. return 0; /* on RIP 14 all hdx return SCSI adr 0,0,0,0 */
  1687. /* ts A70409 : DDLP-B */
  1688. /* >>> obtain single lock on path */
  1689. if(burn_sg_open_o_nonblock)
  1690. open_mode |= O_NDELAY;
  1691. if(burn_sg_open_o_excl) {
  1692. /* O_EXCL | O_RDONLY does not work with /dev/sg* on
  1693. SuSE 9.0 (kernel 2.4) and SuSE 9.3 (kernel 2.6) */
  1694. /* so skip it for now */;
  1695. }
  1696. fd = open(path, open_mode);
  1697. if(fd < 0)
  1698. return 0;
  1699. sg_fcntl_lock(&fd, path, F_RDLCK, 0);
  1700. if(fd < 0)
  1701. return 0;
  1702. #ifdef SCSI_IOCTL_GET_BUS_NUMBER
  1703. /* Hearsay A61005 */
  1704. if (ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, bus_no) == -1)
  1705. *bus_no = -1;
  1706. #endif
  1707. /* http://www.tldp.org/HOWTO/SCSI-Generic-HOWTO/scsi_g_idlun.html */
  1708. ret = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &idlun);
  1709. sg_close_drive_fd(path, -1, &fd, 0);
  1710. if (ret == -1)
  1711. return(0);
  1712. *host_no= (idlun.x>>24)&255;
  1713. *channel_no= (idlun.x>>16)&255;
  1714. *target_no= (idlun.x)&255;
  1715. *lun_no= (idlun.x>>8)&255;
  1716. #ifdef SCSI_IOCTL_GET_BUS_NUMBER
  1717. if(*bus_no == -1)
  1718. *bus_no = 1000 * (*host_no + 1) + *channel_no;
  1719. #else
  1720. *bus_no= *host_no;
  1721. #endif
  1722. return 1;
  1723. }
  1724. /* ts A60922 ticket 33 : called from drive.c */
  1725. /** Tells wether a text is a persistent address as listed by the enumeration
  1726. functions.
  1727. */
  1728. int sg_is_enumerable_adr(char *adr)
  1729. {
  1730. char fname[4096];
  1731. int ret = 0, first = 1;
  1732. burn_drive_enumerator_t idx;
  1733. while (1) {
  1734. ret= sg_give_next_adr(&idx, fname, sizeof(fname), first);
  1735. if(ret <= 0)
  1736. break;
  1737. first = 0;
  1738. if (strcmp(adr, fname) == 0) {
  1739. sg_give_next_adr(&idx, fname, sizeof(fname), -1);
  1740. return 1;
  1741. }
  1742. }
  1743. if (first == 0)
  1744. sg_give_next_adr(&idx, fname, sizeof(fname), -1);
  1745. return(0);
  1746. }
  1747. /* ts B00115 */
  1748. /* Return 1 if the given path leads to a regular file or a device that can be
  1749. seeked, read, and possibly written with 2 kB granularity.
  1750. */
  1751. int burn_os_is_2k_seekrw(char *path, int flag)
  1752. {
  1753. struct stat stbuf;
  1754. if (stat(path, &stbuf) == -1)
  1755. return 0;
  1756. if (S_ISREG(stbuf.st_mode))
  1757. return 1;
  1758. if (S_ISBLK(stbuf.st_mode))
  1759. return 1;
  1760. return 0;
  1761. }
  1762. /* ts A70909 */
  1763. /** Estimate the potential payload capacity of a file address.
  1764. @param path The address of the file to be examined. If it does not
  1765. exist yet, then the directory will be inquired.
  1766. @param bytes The pointed value gets modified, but only if an estimation is
  1767. possible.
  1768. @return -2 = cannot perform necessary operations on file object
  1769. -1 = neither path nor dirname of path exist
  1770. 0 = could not estimate size capacity of file object
  1771. 1 = estimation has been made, bytes was set
  1772. */
  1773. int burn_os_stdio_capacity(char *path, off_t *bytes)
  1774. {
  1775. struct stat stbuf;
  1776. struct statvfs vfsbuf;
  1777. char testpath[4096], *cpt;
  1778. long blocks;
  1779. int open_mode = O_RDONLY, fd, ret;
  1780. off_t add_size = 0;
  1781. testpath[0] = 0;
  1782. blocks = *bytes / 512;
  1783. if (stat(path, &stbuf) == -1) {
  1784. strcpy(testpath, path);
  1785. cpt = strrchr(testpath, '/');
  1786. if(cpt == NULL)
  1787. strcpy(testpath, ".");
  1788. else if(cpt == testpath)
  1789. testpath[1] = 0;
  1790. else
  1791. *cpt = 0;
  1792. if (stat(testpath, &stbuf) == -1)
  1793. return -1;
  1794. } else if(S_ISBLK(stbuf.st_mode)) {
  1795. fd = open(path, open_mode);
  1796. if (fd == -1)
  1797. return -2;
  1798. ret = ioctl(fd, BLKGETSIZE, &blocks);
  1799. close(fd);
  1800. if (ret == -1)
  1801. return -2;
  1802. *bytes = ((off_t) blocks) * (off_t) 512;
  1803. } else if(S_ISREG(stbuf.st_mode)) {
  1804. add_size = stbuf.st_blocks * (off_t) 512;
  1805. strcpy(testpath, path);
  1806. } else
  1807. return 0;
  1808. if (testpath[0]) {
  1809. if (statvfs(testpath, &vfsbuf) == -1)
  1810. return -2;
  1811. *bytes = add_size + ((off_t) vfsbuf.f_frsize) *
  1812. (off_t) vfsbuf.f_bavail;
  1813. }
  1814. return 1;
  1815. }
  1816. /* ts A91122 : an interface to open(O_DIRECT) or similar OS tricks. */
  1817. #ifdef PROT_READ
  1818. #ifdef PROT_WRITE
  1819. #ifdef MAP_SHARED
  1820. #ifdef MAP_ANONYMOUS
  1821. #ifdef MAP_FAILED
  1822. #define Libburn_linux_do_mmaP 1
  1823. #endif
  1824. #endif
  1825. #endif
  1826. #endif
  1827. #endif
  1828. #ifdef Libburn_read_o_direcT
  1829. #ifdef O_DIRECT
  1830. #define Libburn_linux_do_o_direcT 1
  1831. #endif
  1832. #endif /* Libburn_read_o_direcT */
  1833. int burn_os_open_track_src(char *path, int open_flags, int flag)
  1834. {
  1835. int fd;
  1836. #ifdef Libburn_linux_do_o_direcT
  1837. libdax_msgs_submit(libdax_messenger, -1, 0x00000002,
  1838. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  1839. "Opening track source with O_DIRECT" , 0, 0);
  1840. fd = open(path, open_flags | O_DIRECT);
  1841. #else
  1842. fd = open(path, open_flags);
  1843. #endif
  1844. return fd;
  1845. }
  1846. void *burn_os_alloc_buffer(size_t amount, int flag)
  1847. {
  1848. void *buf = NULL;
  1849. #ifdef Libburn_linux_do_mmaP
  1850. /* >>> check whether size is suitable */;
  1851. libdax_msgs_submit(libdax_messenger, -1, 0x00000002,
  1852. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  1853. "Allocating buffer via mmap()" , 0, 0);
  1854. buf = mmap(NULL, amount, PROT_READ | PROT_WRITE,
  1855. MAP_SHARED | MAP_ANONYMOUS, -1, (off_t) 0);
  1856. if (buf == MAP_FAILED)
  1857. buf = NULL;
  1858. else
  1859. memset(buf, 0, amount);
  1860. #else
  1861. buf = calloc(1, amount);
  1862. #endif /* ! Libburn_linux_do_mmaP */
  1863. return buf;
  1864. }
  1865. int burn_os_free_buffer(void *buffer, size_t amount, int flag)
  1866. {
  1867. int ret = 0;
  1868. if (buffer == NULL)
  1869. return 0;
  1870. #ifdef Libburn_linux_do_mmaP
  1871. ret = munmap(buffer, amount);
  1872. #else
  1873. free(buffer);
  1874. #endif
  1875. return (ret == 0);
  1876. }