From 12ad9100ffd77e051ebc44a9ee1586d8ff84f6db Mon Sep 17 00:00:00 2001 From: Thomas Schmitt Date: Mon, 25 Sep 2006 14:09:14 +0000 Subject: [PATCH] Implemented sg_close_drive_fd (ticket 74) --- cdrskin/cdrskin_timestamp.h | 2 +- libburn/drive.c | 3 +- libburn/libdax_msgs.c | 13 ++++++-- libburn/libdax_msgs.h | 4 +++ libburn/sg.c | 65 ++++++++++++++++++++++++++++--------- libburn/sg.h | 3 ++ 6 files changed, 69 insertions(+), 21 deletions(-) diff --git a/cdrskin/cdrskin_timestamp.h b/cdrskin/cdrskin_timestamp.h index 0974d1d..c450580 100644 --- a/cdrskin/cdrskin_timestamp.h +++ b/cdrskin/cdrskin_timestamp.h @@ -1 +1 @@ -#define Cdrskin_timestamP "2006.09.25.104629" +#define Cdrskin_timestamP "2006.09.25.141035" diff --git a/libburn/drive.c b/libburn/drive.c index 4819d40..fd57e22 100644 --- a/libburn/drive.c +++ b/libburn/drive.c @@ -42,7 +42,7 @@ void burn_drive_free(struct burn_drive *d) return; /* ts A60822 : close open fds before forgetting them */ if (burn_drive_is_open(d)) - close(d->fd); + sg_close_drive_fd(d->devname, d->global_index, &(d->fd), 0); free((void *) d->idata); free((void *) d->mdata); free((void *) d->toc_entry); @@ -879,7 +879,6 @@ int burn_drive_convert_fs_adr(char *path, char adr[]) { int ret; struct stat stbuf; - char msg[4096]; burn_drive_adr_debug_msg("burn_drive_convert_fs_adr( %s )", path); if(burn_drive_is_enumerable_adr(path)) { diff --git a/libburn/libdax_msgs.c b/libburn/libdax_msgs.c index 43b578f..6bd72c7 100644 --- a/libburn/libdax_msgs.c +++ b/libburn/libdax_msgs.c @@ -284,9 +284,12 @@ int libdax_msgs_submit(struct libdax_msgs *m, int driveno, int error_code, sprintf(sev_text,"%s : ",sev_name); fprintf(stderr,"%s%s%s\n",m->print_id,sev_text,textpt); - if(os_errno!=0) + if(os_errno!=0) { + error_buf[0]= 0; + strerror_r(os_errno, error_buf,1024); fprintf(stderr,"%s( Most recent system error: %d '%s' )\n", - m->print_id,os_errno,strerror_r(os_errno, error_buf,1024)); + m->print_id,os_errno,error_buf); + } } if(severity < m->queue_severity) @@ -312,6 +315,12 @@ int libdax_msgs_submit(struct libdax_msgs *m, int driveno, int error_code, m->youngest= item; m->count++; libdax_msgs_unlock(m,0); + +/* +fprintf(stderr,"libdax_experimental: message submitted to queue (now %d)\n", + m->count); +*/ + return(1); failed:; libdax_msgs_item_destroy(&item,0); diff --git a/libburn/libdax_msgs.h b/libburn/libdax_msgs.h index 83ead66..904d6fd 100644 --- a/libburn/libdax_msgs.h +++ b/libburn/libdax_msgs.h @@ -278,6 +278,7 @@ Range "libdax_msgs" : 0x00000000 to 0x0000ffff 0x00000000 (ALL,ZERO) = Initial setting in new libdax_msgs_item 0x00000001 (DEBUG,ZERO) = Test error message + 0x00000002 (DEBUG,ZERO) = Debugging message ------------------------------------------------------------------------------ @@ -289,6 +290,9 @@ Range "elmom" : 0x00010000 to 0x0001ffff Range "scdbackup" : 0x00020000 to 0x0002ffff 0x00020001 (SORRY,LOW) = Cannot open busy drive + 0x00020002 (SORRY,HIGH) = Encountered error when closing drive + 0x00020003 (FATAL,HIGH) = Could not grab drive + ------------------------------------------------------------------------------ diff --git a/libburn/sg.c b/libburn/sg.c index e2139cc..a0769ff 100644 --- a/libburn/sg.c +++ b/libburn/sg.c @@ -61,6 +61,34 @@ static int sgio_test(int fd) } +/* ts A60925 : ticket 74 */ +int sg_close_drive_fd(char *fname, int driveno, int *fd, int sorry) +{ + int ret, os_errno, sevno= LIBDAX_MSGS_SEV_DEBUG; + char msg[4096+100]; + + if(*fd < 0) + return(0); + ret = close(*fd); + *fd = -1337; + if(ret != -1) + return 1; + os_errno= errno; + + if (fname != NULL) + sprintf(msg, "Encountered error when closing drive '%s'", + fname); + else + sprintf(msg, "Encountered error when closing drive"); + + if (sorry) + sevno = LIBDAX_MSGS_SEV_SORRY; + libdax_msgs_submit(libdax_messenger, driveno, 0x00020002, + sevno, LIBDAX_MSGS_PRIO_HIGH, msg, os_errno, 0); + return 0; +} + + /* ts A60922 ticket 33 */ /** Returns the next index number and the next enumerated drive address. @param idx An opaque number handle. Make no own theories about it. @@ -185,17 +213,18 @@ void ata_enumerate(void) /* not atapi */ if (!(tm.config & 0x8000) || (tm.config & 0x4000)) { - close(fd); + sg_close_drive_fd(fname, -1, &fd, 0); continue; } /* if SG_IO fails on an atapi device, we should stop trying to use hd* devices */ if (sgio_test(fd) == -1) { - close(fd); + sg_close_drive_fd(fname, -1, &fd, 0); return; } - close(fd); + if (sg_close_drive_fd(fname, -1, &fd, 1) <= 0) + continue; enumerate_common(fname, -1, -1, -1, -1); } } @@ -254,13 +283,11 @@ void sg_enumerate(void) } /* found a drive */ ioctl(fd, SG_GET_SCSI_ID, &sid); - close(fd); + if (sg_close_drive_fd(fname, -1, &fd, + sid.scsi_type == TYPE_ROM ) <= 0) + continue; if (sid.scsi_type != TYPE_ROM) continue; -/* <<< ts A60922 (use SCSI_IOCTL_GET_IDLUN on block devices) - fprintf(stderr,"libburn experimental: SCSI triple: %d,%d,%d\n",sid.host_no,sid.scsi_id,sid.lun); -*/ - enumerate_common(fname, sid.host_no, sid.channel, sid.scsi_id, sid.lun); } @@ -346,7 +373,7 @@ static void enumerate_common(char *fname, int host_no, int channel_no, */ int sg_grab(struct burn_drive *d) { - int fd, count; + int fd, count, os_errno= 0; /* ts A60813 */ int open_mode = O_RDWR; @@ -383,6 +410,7 @@ int sg_grab(struct burn_drive *d) mmc_function_spy("sg_grab ----------- opening"); fd = open(d->devname, open_mode); + os_errno = errno; } else fd= d->fd; @@ -400,11 +428,17 @@ int sg_grab(struct burn_drive *d) d->released = 0; return 1; } - burn_print(1, "could not acquire drive - already open\n"); - close(fd); + libdax_msgs_submit(libdax_messenger, d->global_index, + 0x00020003, + LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH, + "Could not grab drive - already in use", 0, 0); + sg_close_drive_fd(d->devname, d->global_index, &fd, 0); + d->fd = -1337; return 0; } - burn_print(1, "could not acquire drive\n"); + libdax_msgs_submit(libdax_messenger, d->global_index, 0x00020003, + LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH, + "Could not grab drive", os_errno, 0); return 0; } @@ -428,8 +462,7 @@ int sg_release(struct burn_drive *d) <<< debug: for tracing calls which might use open drive fds */ mmc_function_spy("sg_release ----------- closing"); - close(d->fd); - d->fd = -1337; + sg_close_drive_fd(d->devname, d->global_index, &(d->fd), 0); return 0; } @@ -598,8 +631,8 @@ int sg_obtain_scsi_adr(char *path, int *host_no, int *channel_no, /* http://www.tldp.org/HOWTO/SCSI-Generic-HOWTO/scsi_g_idlun.html */ ret= ioctl(fd, SCSI_IOCTL_GET_IDLUN, &idlun); - close(fd); - if(ret == -1) + sg_close_drive_fd(path, -1, &fd, 0); + if (ret == -1) return(0); *host_no= (idlun.x>>24)&255; *channel_no= (idlun.x>>16)&255; diff --git a/libburn/sg.h b/libburn/sg.h index cc9870a..40f19c3 100644 --- a/libburn/sg.h +++ b/libburn/sg.h @@ -9,6 +9,9 @@ struct command; enum response { RETRY, FAIL }; +/* ts A60925 : ticket 74 */ +int sg_close_drive_fd(char *fname, int driveno, int *fd, int sorry); + /* ts A60922 ticket 33 */ int sg_give_next_adr(int *idx, char adr[], int adr_size, int initialize); int sg_is_enumerable_adr(char *adr);