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.

1013 lines
27 KiB

  1. /* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */
  2. /*
  3. Copyright (c) 2010 - 2016 Thomas Schmitt <scdbackup@gmx.net>
  4. Provided under GPL version 2 or later.
  5. */
  6. /*
  7. This is the main operating system dependent SCSI part of libburn. It implements
  8. the transport level aspects of SCSI control and command i/o.
  9. Present implementation: Solaris uscsi, e.g. for SunOS 5.11
  10. PORTING:
  11. Porting libburn typically will consist of adding a new operating system case
  12. to the following switcher files:
  13. os.h Operating system specific libburn definitions and declarations.
  14. sg.c Operating system dependent transport level modules.
  15. and of deriving the following system specific files from existing examples:
  16. os-*.h Included by os.h. You will need some general system knowledge
  17. about signals and knowledge about the storage object needs of your
  18. transport level module sg-*.c.
  19. sg-*.c This source module. You will need special system knowledge about
  20. how to detect all potentially available drives, how to open them,
  21. eventually how to exclusively reserve them, how to perform
  22. SCSI transactions, how to inquire the (pseudo-)SCSI driver.
  23. You will not need to care about CD burning, MMC or other high-level
  24. SCSI aspects.
  25. Said sg-*.c operations are defined by a public function interface, which has
  26. to be implemented in a way that provides libburn with the desired services:
  27. sg_id_string() returns an id string of the SCSI transport adapter.
  28. It may be called before initialization but then may
  29. return only a preliminary id.
  30. sg_initialize() performs global initialization of the SCSI transport
  31. adapter and eventually needed operating system
  32. facilities. Checks for compatibility of supporting
  33. software components.
  34. sg_shutdown() performs global finalizations and releases golbally
  35. acquired resources.
  36. sg_give_next_adr() iterates over the set of potentially useful drive
  37. address strings.
  38. scsi_enumerate_drives() brings all available, not-whitelist-banned, and
  39. accessible drives into libburn's list of drives.
  40. sg_dispose_drive() finalizes adapter specifics of struct burn_drive
  41. on destruction. Releases resources which were acquired
  42. underneath scsi_enumerate_drives().
  43. sg_drive_is_open() tells wether libburn has the given drive in use.
  44. sg_grab() opens the drive for SCSI commands and ensures
  45. undisturbed access.
  46. sg_release() closes a drive opened by sg_grab()
  47. sg_issue_command() sends a SCSI command to the drive, receives reply,
  48. and evaluates wether the command succeeded or shall
  49. be retried or finally failed.
  50. sg_obtain_scsi_adr() tries to obtain SCSI address parameters.
  51. burn_os_is_2k_seekrw() tells whether the given path leads to a file object
  52. that can be used in 2 kB granularity by lseek(2),
  53. read(2), and possibly write(2) if not read-only..
  54. E.g. a USB stick or a hard disk.
  55. burn_os_stdio_capacity() estimates the emulated media space of stdio-drives.
  56. burn_os_open_track_src() opens a disk file in a way that offers best
  57. throughput with file reading and/or SCSI write command
  58. transmission.
  59. burn_os_alloc_buffer() allocates a memory area that is suitable for file
  60. descriptors issued by burn_os_open_track_src().
  61. The buffer size may be rounded up for alignment
  62. reasons.
  63. burn_os_free_buffer() delete a buffer obtained by burn_os_alloc_buffer().
  64. Porting hints are marked by the text "PORTING:".
  65. Send feedback to libburn-hackers@pykix.org .
  66. */
  67. #ifdef HAVE_CONFIG_H
  68. #include "../config.h"
  69. #endif
  70. /** PORTING : ------- OS dependent headers and definitions ------ */
  71. #include <unistd.h>
  72. #include <stropts.h>
  73. #include <stdio.h>
  74. #include <sys/types.h>
  75. #include <errno.h>
  76. #include <fcntl.h>
  77. #include <sys/stat.h>
  78. #include <string.h>
  79. #include <stdlib.h>
  80. #include <dirent.h>
  81. #ifdef Libburn_os_has_statvfS
  82. #include <sys/statvfs.h>
  83. #endif /* Libburn_os_has_stavtfS */
  84. #include <volmgt.h>
  85. #include <sys/dkio.h>
  86. #include <sys/vtoc.h>
  87. #include <sys/scsi/impl/uscsi.h>
  88. /* The waiting time before eventually retrying a failed SCSI command.
  89. Before each retry wait Libburn_sg_linux_retry_incR longer than with
  90. the previous one.
  91. */
  92. #define Libburn_sg_solaris_retry_usleeP 100000
  93. #define Libburn_sg_solaris_retry_incR 100000
  94. /** PORTING : ------ libburn portable headers and definitions ----- */
  95. #include "transport.h"
  96. #include "drive.h"
  97. #include "sg.h"
  98. #include "spc.h"
  99. #include "sbc.h"
  100. #include "debug.h"
  101. #include "toc.h"
  102. #include "util.h"
  103. #include "init.h"
  104. #include "libdax_msgs.h"
  105. extern struct libdax_msgs *libdax_messenger;
  106. /* is in portable part of libburn */
  107. int burn_drive_is_banned(char *device_address);
  108. int burn_drive_resolve_link(char *path, char adr[],
  109. int *recursion_count, int flag); /* drive.c */
  110. /* Whether to log SCSI commands:
  111. bit0= log in /tmp/libburn_sg_command_log
  112. bit1= log to stderr
  113. bit2= flush every line
  114. */
  115. extern int burn_sg_log_scsi;
  116. /* ------------------------------------------------------------------------ */
  117. /* PORTING: Private definitions. Port only if needed by public functions. */
  118. /* (Public functions are listed below) */
  119. /* ------------------------------------------------------------------------ */
  120. /* Storage object is in libburn/init.c
  121. whether to strive for exclusive access to the drive
  122. */
  123. extern int burn_sg_open_o_excl;
  124. /* ------------------------------------------------------------------------ */
  125. /* PORTING: Private functions. Port only if needed by public functions */
  126. /* (Public functions are listed below) */
  127. /* ------------------------------------------------------------------------ */
  128. static int sg_close_drive(struct burn_drive * d)
  129. {
  130. if (d->fd != -1) {
  131. close(d->fd);
  132. d->fd = -1;
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. static int decode_btl_number(char **cpt, int stopper, int *no)
  138. {
  139. *no = 0;
  140. for ((*cpt)++; **cpt != stopper; (*cpt)++) {
  141. if (**cpt < '0' || **cpt > '9')
  142. return 0;
  143. *no = *no * 10 + **cpt - '0';
  144. }
  145. return 1;
  146. }
  147. /* Read bus, target, lun from name "cXtYdZs2" or "cXtYdZ/...".
  148. Return 0 if name is not of the desired form.
  149. */
  150. static int decode_btl_solaris(char *name, int *busno, int *tgtno, int *lunno,
  151. int flag)
  152. {
  153. char *cpt, *cpt_mem;
  154. int ret;
  155. *busno = *tgtno = *lunno = -1;
  156. cpt = name;
  157. if (*cpt != 'c')
  158. return 0;
  159. ret = decode_btl_number(&cpt, 't', busno);
  160. if (ret <= 0)
  161. return ret;
  162. ret = decode_btl_number(&cpt, 'd', tgtno);
  163. if (ret <= 0)
  164. return ret;
  165. cpt_mem = cpt;
  166. ret = decode_btl_number(&cpt, 's', lunno);
  167. if (ret <= 0) {
  168. cpt = cpt_mem;
  169. ret = decode_btl_number(&cpt, '/', lunno);
  170. if (ret <= 0)
  171. return ret;
  172. return(1);
  173. }
  174. cpt++;
  175. if (*cpt != '2' || *(cpt + 1) != 0)
  176. return 0;
  177. return 1;
  178. }
  179. static int start_enum_cXtYdZs2(burn_drive_enumerator_t *idx, int flag)
  180. {
  181. DIR *dir;
  182. idx->dir = NULL;
  183. dir = opendir("/dev/rdsk");
  184. if (dir == NULL) {
  185. libdax_msgs_submit(libdax_messenger, -1,
  186. 0x0002000c, LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  187. "Cannot start device file enumeration. opendir(\"/dev/rdsk\") failed.",
  188. errno, 0);
  189. return 0;
  190. }
  191. idx->dir = dir;
  192. return 1;
  193. }
  194. static int sg_solaris_convert_devname(char *path, char **dev_to_open, int flag)
  195. {
  196. char *sym_name = NULL, *media_name = NULL, *curr_name, *msg = NULL;
  197. int ret;
  198. BURN_ALLOC_MEM(msg, char, 4096);
  199. BURN_FREE_MEM(*dev_to_open);
  200. *dev_to_open = NULL;
  201. curr_name = path;
  202. if (! volmgt_running())
  203. goto set_name;
  204. sym_name = volmgt_symname(path);
  205. sprintf(msg, "Volume Management symbolic name: '%s' -> %s",
  206. path, sym_name == NULL ? "NULL" : sym_name);
  207. libdax_msgs_submit(libdax_messenger, -1,
  208. 0x00000002,
  209. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  210. msg, 0, 0);
  211. if (sym_name != NULL)
  212. media_name = media_findname(sym_name);
  213. else
  214. media_name = media_findname(path);
  215. if (media_name != NULL)
  216. curr_name = media_name;
  217. sprintf(msg, "Media name: %s -> %s",
  218. sym_name == NULL ? path : sym_name,
  219. media_name == NULL ? "NULL" : media_name);
  220. libdax_msgs_submit(libdax_messenger, -1,
  221. 0x00000002,
  222. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  223. msg, 0, 0);
  224. set_name:
  225. BURN_ALLOC_MEM(*dev_to_open, char, strlen(curr_name) + 1);
  226. strcpy(*dev_to_open, curr_name);
  227. ret = 1;
  228. ex:
  229. if (media_name != NULL)
  230. free(media_name);
  231. if (sym_name != NULL)
  232. free(sym_name);
  233. BURN_FREE_MEM(msg);
  234. return(ret);
  235. }
  236. static int next_enum_cXtYdZs2(burn_drive_enumerator_t *idx,
  237. char adr[], int adr_size, int flag)
  238. {
  239. int busno, tgtno, lunno, ret, fd = -1, volpath_size = 160, os_errno;
  240. char *volpath = NULL, *msg = NULL, *dev_to_open = NULL;
  241. struct dirent *entry;
  242. struct dk_cinfo cinfo;
  243. DIR *dir;
  244. BURN_ALLOC_MEM(volpath, char, volpath_size);
  245. BURN_ALLOC_MEM(msg, char, 4096);
  246. dir = idx->dir;
  247. while (1) {
  248. errno = 0;
  249. entry = readdir(dir);
  250. if (entry == NULL) {
  251. if (errno) {
  252. libdax_msgs_submit(libdax_messenger,
  253. -1, 0x0002000d,
  254. LIBDAX_MSGS_SEV_FAILURE, LIBDAX_MSGS_PRIO_HIGH,
  255. "Cannot enumerate next device. readdir() from \"/dev/rdsk\" failed.",
  256. errno, 0);
  257. {ret = -1; goto ex;}
  258. }
  259. break;
  260. }
  261. if (strlen(entry->d_name) > (size_t) (volpath_size - 11))
  262. continue;
  263. ret = decode_btl_solaris(entry->d_name,
  264. &busno, &tgtno, &lunno, 0);
  265. if (ret <= 0)
  266. continue; /* not cXtYdZs2 */
  267. sprintf(volpath, "/dev/rdsk/%s", entry->d_name);
  268. if (burn_drive_is_banned(volpath))
  269. continue;
  270. ret = sg_solaris_convert_devname(volpath, &dev_to_open, 0);
  271. if (ret <= 0)
  272. continue;
  273. fd = open(dev_to_open, O_RDONLY | O_NDELAY);
  274. if (fd < 0) {
  275. os_errno = errno;
  276. sprintf(msg, "Could not open '%s' , errno = %d",
  277. dev_to_open, os_errno);
  278. libdax_msgs_submit(libdax_messenger, -1,
  279. 0x00000002,
  280. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  281. msg, os_errno, 0);
  282. continue;
  283. }
  284. /* See man dkio */
  285. ret = ioctl(fd, DKIOCINFO, &cinfo);
  286. close(fd);
  287. if (ret < 0) {
  288. os_errno = errno;
  289. sprintf(msg,
  290. "ioctl(DKIOCINFO) failed on drive '%s', errno = %d",
  291. volpath, os_errno);
  292. libdax_msgs_submit(libdax_messenger, -1,
  293. 0x00000002,
  294. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  295. msg, os_errno, 0);
  296. continue;
  297. }
  298. if (cinfo.dki_ctype != DKC_CDROM) {
  299. sprintf(msg,
  300. "ioctl(DKIOCINFO) classifies drive '%s' as dki_ctype %ld, not as DKC_CDROM = %ld",
  301. volpath, (long int) cinfo.dki_ctype,
  302. (long int) DKC_CDROM);
  303. libdax_msgs_submit(libdax_messenger, -1,
  304. 0x00000002,
  305. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  306. msg, 0, 0);
  307. continue;
  308. }
  309. if (adr_size <= (int) strlen(volpath)) {
  310. sprintf(msg,
  311. "Device path '%s' too long. (Max. %d)",
  312. volpath, adr_size - 1);
  313. libdax_msgs_submit(libdax_messenger, -1,
  314. 0x00000002,
  315. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  316. msg, 0, 0);
  317. {ret = -1; goto ex;}
  318. }
  319. strcpy(adr, volpath);
  320. sprintf(msg, "Accepted as valid drive '%s'", volpath);
  321. libdax_msgs_submit(libdax_messenger, -1,
  322. 0x00000002,
  323. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  324. msg, 0, 0);
  325. {ret = 1; goto ex;}
  326. }
  327. ret = 0;
  328. ex:;
  329. BURN_FREE_MEM(dev_to_open);
  330. BURN_FREE_MEM(msg);
  331. BURN_FREE_MEM(volpath);
  332. return ret;
  333. }
  334. static int end_enum_cXtYdZs2(burn_drive_enumerator_t *idx, int flag)
  335. {
  336. DIR *dir;
  337. dir = idx->dir;
  338. if(dir != NULL)
  339. closedir(dir);
  340. idx->dir = NULL;
  341. return 1;
  342. }
  343. /* ----------------------------------------------------------------------- */
  344. /* PORTING: Private functions which contain publicly needed functionality. */
  345. /* Their portable part must be performed. So it is probably best */
  346. /* to replace the non-portable part and to call these functions */
  347. /* in your port, too. */
  348. /* ----------------------------------------------------------------------- */
  349. /** Wraps a detected drive into libburn structures and hands it over to
  350. libburn drive list.
  351. */
  352. static void enumerate_common(char *fname,
  353. int bus_no, int host_no,
  354. int channel_no, int target_no, int lun_no)
  355. {
  356. int ret;
  357. struct burn_drive out;
  358. /* General libburn drive setup */
  359. burn_setup_drive(&out, fname);
  360. /* This transport adapter uses SCSI-family commands and models
  361. (seems the adapter would know better than its boss, if ever) */
  362. ret = burn_scsi_setup_drive(&out, bus_no, host_no, channel_no,
  363. target_no, lun_no, 0);
  364. if (ret <= 0)
  365. return;
  366. /* PORTING: ------------------- non portable part --------------- */
  367. /* Transport adapter is Solaris uscsi */
  368. /* Adapter specific handles and data */
  369. out.fd = -1;
  370. /* PORTING: ---------------- end of non portable part ------------ */
  371. /* Adapter specific functions with standardized names */
  372. out.grab = sg_grab;
  373. out.release = sg_release;
  374. out.drive_is_open = sg_drive_is_open;
  375. out.issue_command = sg_issue_command;
  376. /* Finally register drive and inquire drive information */
  377. burn_drive_finish_enum(&out);
  378. }
  379. /* ------------------------------------------------------------------------ */
  380. /* PORTING: Public functions. These MUST be ported. */
  381. /* ------------------------------------------------------------------------ */
  382. /** Returns the id string of the SCSI transport adapter and eventually
  383. needed operating system facilities.
  384. This call is usable even if sg_initialize() was not called yet. In that
  385. case a preliminary constant message might be issued if detailed info is
  386. not available yet.
  387. @param msg returns id string
  388. @param flag unused yet, submit 0
  389. @return 1 = success, <=0 = failure
  390. */
  391. int sg_id_string(char msg[1024], int flag)
  392. {
  393. sprintf(msg, "internal Solaris uscsi adapter sg-solaris");
  394. return 1;
  395. }
  396. /** Performs global initialization of the SCSI transport adapter and eventually
  397. needed operating system facilities. Checks for compatibility of supporting
  398. software components.
  399. @param msg returns ids and/or error messages of eventual helpers
  400. @param flag unused yet, submit 0
  401. @return 1 = success, <=0 = failure
  402. */
  403. int sg_initialize(char msg[1024], int flag)
  404. {
  405. return sg_id_string(msg, 0);
  406. }
  407. /** Performs global finalization of the SCSI transport adapter and eventually
  408. needed operating system facilities. Releases globally acquired resources.
  409. @param flag unused yet, submit 0
  410. @return 1 = success, <=0 = failure
  411. */
  412. int sg_shutdown(int flag)
  413. {
  414. return 1;
  415. }
  416. /** Finalizes BURN_OS_TRANSPORT_DRIVE_ELEMENTS, the components of
  417. struct burn_drive which are defined in os-*.h.
  418. The eventual initialization of those components was made underneath
  419. scsi_enumerate_drives().
  420. This will be called when a burn_drive gets disposed.
  421. @param d the drive to be finalized
  422. @param flag unused yet, submit 0
  423. @return 1 = success, <=0 = failure
  424. */
  425. int sg_dispose_drive(struct burn_drive *d, int flag)
  426. {
  427. return 1;
  428. }
  429. /** Returns the next index number and the next enumerated drive address.
  430. The enumeration has to cover all available and accessible drives. It is
  431. allowed to return addresses of drives which are not available but under
  432. some (even exotic) circumstances could be available. It is on the other
  433. hand allowed, only to hand out addresses which can really be used right
  434. in the moment of this call. (This implementation chooses the latter.)
  435. @param idx An opaque handle. Make no own theories about it.
  436. @param adr Takes the reply
  437. @param adr_size Gives maximum size of reply including final 0
  438. @param initialize 1 = start new,
  439. 0 = continue, use no other values for now
  440. -1 = finish
  441. @return 1 = reply is a valid address , 0 = no further address available
  442. -1 = severe error (e.g. adr_size too small)
  443. */
  444. int sg_give_next_adr(burn_drive_enumerator_t *idx,
  445. char adr[], int adr_size, int initialize)
  446. {
  447. int ret;
  448. if (initialize == 1) {
  449. ret = start_enum_cXtYdZs2(idx, 0);
  450. if (ret <= 0)
  451. return ret;
  452. } else if (initialize == -1) {
  453. ret = end_enum_cXtYdZs2(idx, 0);
  454. return 0;
  455. }
  456. ret = next_enum_cXtYdZs2(idx, adr, adr_size, 0);
  457. return ret;
  458. }
  459. /** Brings all available, not-whitelist-banned, and accessible drives into
  460. libburn's list of drives.
  461. */
  462. int scsi_enumerate_drives(void)
  463. {
  464. burn_drive_enumerator_t idx;
  465. int initialize = 1, ret, i_bus_no = -1, buf_size = 4096;
  466. int i_host_no = -1, i_channel_no = -1, i_target_no = -1, i_lun_no = -1;
  467. char *buf = NULL;
  468. BURN_ALLOC_MEM(buf, char, buf_size);
  469. while(1) {
  470. ret = sg_give_next_adr(&idx, buf, buf_size, initialize);
  471. initialize = 0;
  472. if (ret <= 0)
  473. break;
  474. if (burn_drive_is_banned(buf))
  475. continue;
  476. sg_obtain_scsi_adr(buf, &i_bus_no, &i_host_no,
  477. &i_channel_no, &i_target_no, &i_lun_no);
  478. enumerate_common(buf,
  479. i_bus_no, i_host_no, i_channel_no,
  480. i_target_no, i_lun_no);
  481. }
  482. sg_give_next_adr(&idx, buf, buf_size, -1);
  483. ret = 1;
  484. ex:;
  485. BURN_FREE_MEM(buf);
  486. return ret;
  487. }
  488. /** Tells whether libburn has the given drive in use or exclusively reserved.
  489. If it is "open" then libburn will eventually call sg_release() on it when
  490. it is time to give up usage and reservation.
  491. */
  492. /** Published as burn_drive.drive_is_open() */
  493. int sg_drive_is_open(struct burn_drive * d)
  494. {
  495. return (d->fd != -1);
  496. }
  497. /** Opens the drive for SCSI commands and - if burn activities are prone
  498. to external interference on your system - obtains an exclusive access lock
  499. on the drive. (Note: this is not physical tray locking.)
  500. A drive that has been opened with sg_grab() will eventually be handed
  501. over to sg_release() for closing and unreserving.
  502. */
  503. int sg_grab(struct burn_drive *d)
  504. {
  505. char *msg = NULL, *dev_to_open = NULL;
  506. int os_errno, ret;
  507. struct dk_cinfo cinfo;
  508. BURN_ALLOC_MEM(msg, char, 4096);
  509. if (d->fd != -1) {
  510. d->released = 0;
  511. {ret = 1; goto ex;}
  512. }
  513. ret = sg_solaris_convert_devname(d->devname, &dev_to_open, 0);
  514. if (ret <= 0)
  515. goto ex;
  516. d->fd = open(dev_to_open, O_RDONLY | O_NDELAY);
  517. if (d->fd == -1) {
  518. os_errno = errno;
  519. sprintf(msg, "Could not grab drive '%s'",
  520. d->devname);
  521. if (strcmp(d->devname, dev_to_open))
  522. sprintf(msg + strlen(msg), " via '%s'", dev_to_open);
  523. libdax_msgs_submit(libdax_messenger, d->global_index,
  524. 0x00020003,
  525. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
  526. msg, os_errno, 0);
  527. {ret = 0; goto ex;}
  528. }
  529. ret = ioctl(d->fd, DKIOCINFO, &cinfo);
  530. if (ret < 0) {
  531. os_errno = errno;
  532. sprintf(msg, "ioctl(DKIOCINFO) failed on drive '%s'",
  533. d->devname);
  534. libdax_msgs_submit(libdax_messenger, d->global_index,
  535. 0x00000002,
  536. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  537. msg, os_errno, 0);
  538. goto revoke;
  539. }
  540. if (cinfo.dki_ctype != DKC_CDROM) {
  541. sprintf(msg,
  542. "ioctl(DKIOCINFO) classifies drive '%s' as dki_ctype %ld, not as DKC_CDROM = %ld",
  543. d->devname, (long int) cinfo.dki_ctype,
  544. (long int) DKC_CDROM);
  545. libdax_msgs_submit(libdax_messenger, d->global_index,
  546. 0x00000002,
  547. LIBDAX_MSGS_SEV_DEBUG, LIBDAX_MSGS_PRIO_HIGH,
  548. msg, 0, 0);
  549. goto revoke;
  550. }
  551. /* >>> obtain eventual locks */;
  552. d->released = 0;
  553. {ret = 1; goto ex;}
  554. revoke:;
  555. sprintf(msg, "Could not grab drive '%s'. Not a CDROM device.",
  556. d->devname);
  557. libdax_msgs_submit(libdax_messenger, d->global_index,
  558. 0x00020003,
  559. LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
  560. msg, 0, 0);
  561. ret = 0;
  562. ex:;
  563. BURN_FREE_MEM(dev_to_open);
  564. BURN_FREE_MEM(msg);
  565. return ret;
  566. }
  567. /** PORTING: Is mainly about the call to sg_close_drive() and whether it
  568. implements the demanded functionality.
  569. */
  570. /** Gives up the drive for SCSI commands and releases eventual access locks.
  571. (Note: this is not physical tray locking.)
  572. */
  573. int sg_release(struct burn_drive *d)
  574. {
  575. if (d->fd < 0)
  576. return 0;
  577. sg_close_drive(d);
  578. return 0;
  579. }
  580. /** Sends a SCSI command to the drive, receives reply and evaluates wether
  581. the command succeeded or shall be retried or finally failed.
  582. Returned SCSI errors shall not lead to a return value indicating failure.
  583. The callers get notified by c->error. An SCSI failure which leads not to
  584. a retry shall be notified via scsi_notify_error().
  585. The Libburn_log_sg_commandS facility might be of help when problems with
  586. a drive have to be examined. It shall stay disabled for normal use.
  587. @return: 1 success , <=0 failure
  588. */
  589. int sg_issue_command(struct burn_drive *d, struct command *c)
  590. {
  591. int i, timeout_ms, ret, key, asc, ascq, done = 0, sense_len;
  592. time_t start_time;
  593. struct uscsi_cmd cgc;
  594. char msg[80];
  595. static FILE *fp = NULL;
  596. c->error = 0;
  597. memset(c->sense, 0, sizeof(c->sense));
  598. if (d->fd == -1)
  599. return 0;
  600. if (burn_sg_log_scsi & 1) {
  601. if (fp == NULL) {
  602. fp= fopen("/tmp/libburn_sg_command_log", "a");
  603. fprintf(fp,
  604. "\n-----------------------------------------\n");
  605. }
  606. }
  607. if (burn_sg_log_scsi & 3)
  608. scsi_log_cmd(c,fp,0);
  609. if (c->timeout > 0)
  610. timeout_ms = c->timeout;
  611. else
  612. timeout_ms = 200000;
  613. memset (&cgc, 0, sizeof (struct uscsi_cmd));
  614. /* No error messages, no retries,
  615. do not execute with other commands, request sense data
  616. */
  617. cgc.uscsi_flags = USCSI_SILENT | USCSI_DIAGNOSE | USCSI_ISOLATE
  618. | USCSI_RQENABLE;
  619. cgc.uscsi_timeout = timeout_ms / 1000;
  620. cgc.uscsi_cdb = (caddr_t) c->opcode;
  621. cgc.uscsi_bufaddr = (caddr_t) c->page->data;
  622. if (c->dir == TO_DRIVE) {
  623. cgc.uscsi_flags |= USCSI_WRITE;
  624. cgc.uscsi_buflen = c->page->bytes;
  625. } else if (c->dir == FROM_DRIVE) {
  626. cgc.uscsi_flags |= USCSI_READ;
  627. if (c->dxfer_len >= 0)
  628. cgc.uscsi_buflen = c->dxfer_len;
  629. else
  630. cgc.uscsi_buflen = BUFFER_SIZE;
  631. /* touch page so we can use valgrind */
  632. memset(c->page->data, 0, BUFFER_SIZE);
  633. } else {
  634. cgc.uscsi_buflen = 0;
  635. }
  636. cgc.uscsi_cdblen = c->oplen;
  637. cgc.uscsi_rqlen = sizeof(c->sense);
  638. cgc.uscsi_rqbuf = (caddr_t) c->sense;
  639. /* retry-loop */
  640. start_time = time(NULL);
  641. for(i = 0; !done; i++) {
  642. memset(c->sense, 0, sizeof(c->sense));
  643. c->start_time = burn_get_time(0);
  644. ret = ioctl(d->fd, USCSICMD, &cgc);
  645. c->end_time = burn_get_time(0);
  646. /* For cgc.uscsi_status see SAM-3 5.3.1, Table 22
  647. 0 = GOOD , 2 = CHECK CONDITION : Sense Data are delivered
  648. 8 = BUSY
  649. */
  650. if (ret != 0 && cgc.uscsi_status != 2) {
  651. sprintf(msg,
  652. "Failed to transfer command to drive. (uscsi_status = 0x%X)",
  653. (unsigned int) cgc.uscsi_status),
  654. libdax_msgs_submit(libdax_messenger,
  655. d->global_index, 0x0002010c,
  656. LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH,
  657. msg, errno, 0);
  658. sg_close_drive(d);
  659. d->released = 1;
  660. d->busy = BURN_DRIVE_IDLE;
  661. c->error = 1;
  662. return -1;
  663. }
  664. /* >>> Should replace "18" by realistic sense length.
  665. What's about following older remark ?
  666. */
  667. /* >>> valid sense: cgc.uscsi_rqlen - cgc.uscsi_rqresid */;
  668. spc_decode_sense(c->sense, 0, &key, &asc, &ascq);
  669. if (key || asc || ascq)
  670. sense_len = 18;
  671. else
  672. sense_len = 0;
  673. done = scsi_eval_cmd_outcome(d, c, fp, c->sense, sense_len,
  674. start_time, timeout_ms, i, 0);
  675. if (d->cancel)
  676. done = 1;
  677. } /* end of retry-loop */
  678. return 1;
  679. }
  680. /** Tries to obtain SCSI address parameters.
  681. @return 1 is success , 0 is failure
  682. */
  683. int sg_obtain_scsi_adr(char *path, int *bus_no, int *host_no, int *channel_no,
  684. int *target_no, int *lun_no)
  685. {
  686. int ret;
  687. /* Try to guess from path */
  688. if (strncmp("/dev/rdsk/", path, 10) == 0) {
  689. ret = decode_btl_solaris(path + 10,
  690. bus_no, target_no, lun_no, 0);
  691. if (ret > 0) {
  692. *host_no = *bus_no;
  693. *channel_no = 0;
  694. return 1;
  695. }
  696. }
  697. *bus_no = *host_no = *channel_no = *target_no = *lun_no = -1;
  698. /* >>> Could need a ioctl which gives SCSI numbers */;
  699. return (0);
  700. }
  701. /** Tells wether a text is a persistent address as listed by the enumeration
  702. functions.
  703. */
  704. #ifndef NIX
  705. int sg_is_enumerable_adr(char* path)
  706. {
  707. int ret;
  708. int bus_no, target_no, lun_no;
  709. struct stat stbuf;
  710. if (strncmp("/dev/rdsk/", path, 10) != 0)
  711. return 0;
  712. ret = decode_btl_solaris(path + 10, &bus_no, &target_no, &lun_no, 0);
  713. if (ret <= 0)
  714. return 0;
  715. if (stat(path, &stbuf) == -1)
  716. return 0;
  717. return 1;
  718. }
  719. #else /* ! NIX */
  720. int sg_is_enumerable_adr(char* adr)
  721. {
  722. burn_drive_enumerator_t idx;
  723. int initialize = 1, ret;
  724. char buf[64];
  725. while(1) {
  726. ret = sg_give_next_adr(&idx, buf, sizeof(buf), initialize);
  727. initialize = 0;
  728. if (ret <= 0)
  729. break;
  730. if (strcmp(adr, buf) == 0) {
  731. sg_give_next_adr(&idx, buf, sizeof(buf), -1);
  732. return 1;
  733. }
  734. }
  735. sg_give_next_adr(&idx, buf, sizeof(buf), -1);
  736. return (0);
  737. }
  738. #endif /* NIX */
  739. /* Return 1 if the given path leads to a regular file or a device that can be
  740. fseeked, read, and possibly written with 2 kB granularity.
  741. */
  742. int burn_os_is_2k_seekrw(char *path, int flag)
  743. {
  744. struct stat stbuf;
  745. if (stat(path, &stbuf) == -1)
  746. return 0;
  747. if (S_ISREG(stbuf.st_mode))
  748. return 1;
  749. if (S_ISBLK(stbuf.st_mode))
  750. return 1;
  751. return 0;
  752. }
  753. /** Estimate the potential payload capacity of a file address.
  754. @param path The address of the file to be examined. If it does not
  755. exist yet, then the directory will be inquired.
  756. @param bytes The pointed value gets modified, but only if an estimation is
  757. possible.
  758. @return -2 = cannot perform necessary operations on file object
  759. -1 = neither path nor dirname of path exist
  760. 0 = could not estimate size capacity of file object
  761. 1 = estimation has been made, bytes was set
  762. */
  763. int burn_os_stdio_capacity(char *path, off_t write_start, off_t *bytes)
  764. {
  765. struct stat stbuf;
  766. int ret;
  767. #ifdef Libburn_os_has_statvfS
  768. struct statvfs vfsbuf;
  769. #endif
  770. char *testpath = NULL, *cpt;
  771. off_t add_size = 0;
  772. BURN_ALLOC_MEM(testpath, char, 4096);
  773. testpath[0] = 0;
  774. if (stat(path, &stbuf) == -1) {
  775. strcpy(testpath, path);
  776. cpt = strrchr(testpath, '/');
  777. if(cpt == NULL)
  778. strcpy(testpath, ".");
  779. else if(cpt == testpath)
  780. testpath[1] = 0;
  781. else
  782. *cpt = 0;
  783. if (stat(testpath, &stbuf) == -1)
  784. {ret = -1; goto ex;}
  785. } else if(S_ISBLK(stbuf.st_mode)) {
  786. int open_mode = O_RDONLY, fd;
  787. fd = open(path, open_mode);
  788. if (fd == -1)
  789. {ret = -2; goto ex;}
  790. *bytes = lseek(fd, 0, SEEK_END);
  791. close(fd);
  792. if (*bytes == -1) {
  793. *bytes = 0;
  794. {ret = 0; goto ex;}
  795. }
  796. } else if(S_ISREG(stbuf.st_mode)) {
  797. add_size = burn_sparse_file_addsize(write_start, &stbuf);
  798. strcpy(testpath, path);
  799. } else
  800. {ret = 0; goto ex;}
  801. if (testpath[0]) {
  802. #ifdef Libburn_os_has_statvfS
  803. if (statvfs(testpath, &vfsbuf) == -1)
  804. {ret = -2; goto ex;}
  805. *bytes = add_size + ((off_t) vfsbuf.f_frsize) *
  806. (off_t) vfsbuf.f_bavail;
  807. #else /* Libburn_os_has_statvfS */
  808. {ret = 0; goto ex;}
  809. #endif /* ! Libburn_os_has_stavtfS */
  810. }
  811. ret = 1;
  812. ex:;
  813. BURN_FREE_MEM(testpath);
  814. return ret;
  815. }
  816. /* ts A91122 : an interface to open(O_DIRECT) or similar OS tricks. */
  817. #ifdef Libburn_read_o_direcT
  818. /* No special O_DIRECT-like precautions are implemented here */
  819. #endif /* Libburn_read_o_direcT */
  820. int burn_os_open_track_src(char *path, int open_flags, int flag)
  821. {
  822. int fd;
  823. fd = open(path, open_flags);
  824. return fd;
  825. }
  826. void *burn_os_alloc_buffer(size_t amount, int flag)
  827. {
  828. void *buf = NULL;
  829. buf = calloc(1, amount);
  830. return buf;
  831. }
  832. int burn_os_free_buffer(void *buffer, size_t amount, int flag)
  833. {
  834. if (buffer == NULL)
  835. return 0;
  836. free(buffer);
  837. return 1;
  838. }