[Debburn-changes] r722 - in cdrkit/trunk: libusal wodim

Eduard Bloch blade at alioth.debian.org
Sun Apr 1 19:36:17 UTC 2007


Author: blade
Date: 2007-04-01 19:36:16 +0000 (Sun, 01 Apr 2007)
New Revision: 722

Modified:
   cdrkit/trunk/libusal/scsi-linux-sg.c
   cdrkit/trunk/libusal/scsiopen.c
   cdrkit/trunk/wodim/wodim.c
Log:
Some refactoring, moved linux specific code to proper places, disabled some uber-paranoid checks on device setup

Modified: cdrkit/trunk/libusal/scsi-linux-sg.c
===================================================================
--- cdrkit/trunk/libusal/scsi-linux-sg.c	2007-04-01 16:39:29 UTC (rev 721)
+++ cdrkit/trunk/libusal/scsi-linux-sg.c	2007-04-01 19:36:16 UTC (rev 722)
@@ -67,6 +67,7 @@
 #include <linux/version.h>
 #include <sys/types.h>
 #include <dirent.h>
+#include <sys/utsname.h>
 
 #ifndef LINUX_VERSION_CODE	/* Very old kernel? */
 #	define LINUX_VERSION_CODE 0
@@ -243,24 +244,29 @@
 #endif
 static	void	sg_settimeout(int f, int timeout);
 
-int    sg_open_excl(char *device, int mode);
+int    sg_open_excl(char *device, int mode, BOOL beQuiet);
 
 static BOOL get_max_secs(char *dirpath, int *outval);
 
-int sg_open_excl(char *device, int mode)
+static BOOL check_linux_26();
+
+int sg_open_excl(char *device, int mode, BOOL beQuiet)
 {
        int f;
        int i=0;
+       long interval = beQuiet ? 400000 : 1000000;
+
        f = open(device, mode|O_EXCL);
        /* try to reopen locked/busy devices up to five times */
        for (i = 0; (i < 5) && (f == -1 && errno == EBUSY); i++) {
-	       fprintf(stderr, "Error trying to open %s exclusively (%s)... %s\n",
-               device, strerror(errno), 
-               (i<4)?"retrying in 1 second.":"giving up.");
-	       usleep(1000000 + 100000.0 * rand()/(RAND_MAX+1.0));
+	       if(!beQuiet)
+		       fprintf(stderr, "Error trying to open %s exclusively (%s)... %s\n",
+				       device, strerror(errno), 
+				       (i<4)?"retrying in 1 second.":"giving up.");
+	       usleep(interval + interval * rand()/(RAND_MAX+1.0));
 	       f = open(device, mode|O_EXCL);
        }
-       if(i==5) {
+       if(i==5 && !beQuiet) {
 	       FILE *g = fopen("/proc/mounts", "r");
 	       if(g) {
 		       char buf[80];
@@ -357,19 +363,44 @@
 	return (0);
 }
 
+#define in_scanmode (busno < 0 && tgt < 0 && tlun < 0)
+
+#if 0
 static int
 usalo_open(SCSI *usalp, char *device)
 {
+	  if(!usalp && dev && strncmp("ATA", dev, 3) ) {
+     char *dalt;
+     int len=5+strlen(dev);
+
+     fprintf(stderr, "Unable to open, trying with ATA: prefix...\n");
+     dalt=calloc(len, sizeof(char));
+     strcat(dalt, "ATA:");
+     strcat(dalt+4, dev);
+     usalp = usal_open(dalt, errstr, sizeof (errstr),
+           debug, lverbose);
+  }
+
+
+}
+#endif
+
+/*
+ * b/t/l is chopped of the device string.
+ */
+static int
+usalo_open(SCSI *usalp, char *device)
+{
 		int	busno	= usal_scsibus(usalp);
 		int	tgt	= usal_target(usalp);
 		int	tlun	= usal_lun(usalp);
-	register int	f;
-	register int	i;
-	register int	b;
-	register int	t;
-	register int	l;
-	register int	nopen = 0;
-	char		devname[64];
+		register int	f;
+		register int	i;
+		register int	b;
+		register int	t;
+		register int	l;
+		register int	nopen = 0;
+		char		devname[64];
 		BOOL	use_ata = FALSE;
 
 
@@ -381,7 +412,31 @@
 				busno, tgt, tlun);
 		return (-1);
 	}
+
+	struct stat statbuf;
+	if(check_linux_26() && 0!=stat("/sys/kernel", &statbuf)) {
+		static int warn_sysfs=1;
+		if(warn_sysfs) {
+			warn_sysfs=0;
+			fprintf(stderr, "\nWarning, sysfs is not mounted on /sys!\n"
+					"It is recommended to mount sysfs to allow better device configuration\n");
+			sleep(5);
+		}
+	}
+
 	if (device != NULL && *device != '\0') {
+		if(check_linux_26()) {
+			if(0==strncmp(device, "ATAPI:", 6))
+			{
+				device+=6;
+				fprintf(stderr, "\nWarning, the ATAPI: method is considered deprecated on modern kernels!\n"
+						"Mapping device specification to dev=%s now.\n"
+						"To force the old ATAPI: method, replace ATAPI: with OLDATAPI:\n", device);
+			}
+			if(0==strncmp(device, "OLDATAPI:", 9))
+				device+=3;
+		}
+
 #ifdef	USE_OLD_ATAPI
 		if (strncmp(device, "ATAPI", 5) == 0) {
 			usalp->ops = &ata_ops;
@@ -393,6 +448,13 @@
 			device = NULL;
 		}
 	}
+	else if( ! in_scanmode ) {
+			fprintf(stderr, "\nWarning, the deprecated pseudo SCSI syntax found as device specification.\n"
+					"Support for that may cease in the future versions of wodim. For now,\n"
+					"the device will be mapped to a block device file where possible.\n"
+					"Run \"wodim --devices\" for details.\n" );
+			sleep(5);
+	}
 
 	if (usalp->local == NULL) {
 		usalp->local = malloc(sizeof (struct usal_local));
@@ -431,7 +493,7 @@
 		for (i=2*busno+tgt >= 0 ? 2*busno+tgt:0; i <= 25; i++) {
 			snprintf(devname, sizeof (devname), "/dev/hd%c", i+'a');
 			/* O_NONBLOCK is dangerous */
-			f = sg_open_excl(devname, O_RDWR | O_NONBLOCK);
+			f = sg_open_excl(devname, O_RDWR | O_NONBLOCK, in_scanmode);
 			if (f < 0) {
 				/*
 				 * Set up error string but let us clear it later
@@ -459,7 +521,7 @@
 				sg_clearnblock(f);	/* Be very proper about this */
 				if (sg_setup(usalp, f, busno, tgt, tlun, i))
 					return (++nopen);
-				if (busno < 0 && tgt < 0 && tlun < 0)
+				if (in_scanmode)
 					nopen++;
 			}
 		}
@@ -467,20 +529,24 @@
 		if(nopen==0)
 			return(0);
 	}
-  else {
-     fprintf(stderr, "ATAPI devices not scanned.\n"
-           "wodim: HINT : To surely see all drives try option: --devices\n"
-           "wodim: HINT : Or try options:               dev=ATA -scanbus\n"
-           );
-  }
+	/*
+	else if(in_scanmode) {
+		fprintf(stderr, "ATAPI devices not scanned.\n"
+				"wodim: HINT : To surely see all drives try option: --devices\n"
+				"wodim: HINT : Or try options:               dev=ATA -scanbus\n"
+		       );
+	}
+	*/
+
 	if (nopen > 0 && usalp->errstr)
 		usalp->errstr[0] = '\0';
 
-	if (nopen == 0) {
+	if(nopen == 0 && check_linux_26())
+	{
 		for (i = 0; i < 32; i++) {
 			snprintf(devname, sizeof (devname), "/dev/sr%d", i);
 			/* O_NONBLOCK is dangerous */
-			f = sg_open_excl(devname, O_RDWR | O_NONBLOCK);
+			f = sg_open_excl(devname, O_RDWR | O_NONBLOCK, in_scanmode);
 			if (f < 0) {
 				/*
 				 * Set up error string but let us clear it later
@@ -489,7 +555,9 @@
 				if (usalp->errstr)
 					snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
 							"Cannot open '%s'", devname);
-				if(errno == EACCES || errno==EPERM)
+				/*
+				 * Stop this paranoia, continue scanning.
+				if(errno == EACCES || errno==EPERM || errno == EBUSY)
 					continue;
 				if (errno != ENOENT && errno != ENXIO && errno != ENODEV) {
 					if (usalp->errstr)
@@ -497,11 +565,12 @@
 								"Cannot open '%s'", devname);
 					return (0);
 				}
+				*/
 			} else {
 				sg_clearnblock(f);	/* Be very proper about this */
 				if (sg_setup(usalp, f, busno, tgt, tlun, -1))
 					return (++nopen);
-				if (busno < 0 && tgt < 0 && tlun < 0)
+				if (in_scanmode)
 					nopen++;
 			}
 		}
@@ -509,11 +578,11 @@
 	if (nopen > 0 && usalp->errstr)
 		usalp->errstr[0] = '\0';
 
-	if (nopen == 0) { /* don't do it if sr driver is workin */
+	if (nopen == 0) { /* don't do it if sr driver is working */
 		for (i = 0; i < 32; i++) {
 			snprintf(devname, sizeof (devname), "/dev/sg%d", i);
 			/* O_NONBLOCK is dangerous */
-			f = sg_open_excl(devname, O_RDWR | O_NONBLOCK);
+			f = sg_open_excl(devname, O_RDWR | O_NONBLOCK, in_scanmode);
 			if (f < 0) {
 				/*
 				 * Set up error string but let us clear it later
@@ -522,7 +591,9 @@
 				if (usalp->errstr)
 					snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
 							"Cannot open '%s'", devname);
-				if(errno == EACCES || errno==EPERM)
+				/*
+				 * Stop this paranoia, continue scanning.
+				if(errno == EACCES || errno==EPERM || errno==EBUSY)
 					continue;
 				if (errno != ENOENT && errno != ENXIO && errno != ENODEV) {
 					if (usalp->errstr)
@@ -530,11 +601,12 @@
 								"Cannot open '%s'", devname);
 					return (0);
 				}
+				*/
 			} else {
 				sg_clearnblock(f);	/* Be very proper about this */
 				if (sg_setup(usalp, f, busno, tgt, tlun, -1))
 					return (++nopen);
-				if (busno < 0 && tgt < 0 && tlun < 0)
+				if (in_scanmode)
 					nopen++;
 			}
 		}
@@ -546,7 +618,7 @@
 		for (i = 0; i <= 25; i++) {
 			snprintf(devname, sizeof (devname), "/dev/sg%c", i+'a');
 			/* O_NONBLOCK is dangerous */
-			f = sg_open_excl(devname, O_RDWR | O_NONBLOCK);
+			f = sg_open_excl(devname, O_RDWR | O_NONBLOCK, in_scanmode);
 			if (f < 0) {
 				/*
 				 * Set up error string but let us clear it later
@@ -555,7 +627,9 @@
 				if (usalp->errstr)
 					snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
 							"Cannot open '%s'", devname);
-				if(errno == EACCES || errno==EPERM)
+				/*
+				 * Stop this paranoia, continue scanning.
+				if(errno == EACCES || errno==EPERM || errno=EBUSY)
 					continue;
 				if (errno != ENOENT && errno != ENXIO && errno != ENODEV) {
 					if (usalp->errstr)
@@ -563,11 +637,12 @@
 								"Cannot open '%s'", devname);
 					return (0);
 				}
+				*/
 			} else {
 				sg_clearnblock(f);	/* Be very proper about this */
 				if (sg_setup(usalp, f, busno, tgt, tlun, -1))
 					return (++nopen);
-				if (busno < 0 && tgt < 0 && tlun < 0)
+				if (in_scanmode)
 					nopen++;
 			}
 		}
@@ -593,7 +668,7 @@
        device=buf;
        }
      * */
-		f = sg_open_excl(device, O_RDWR | O_NONBLOCK);
+		f = sg_open_excl(device, O_RDWR | O_NONBLOCK, FALSE);
 /*		if (f < 0 && errno == ENOENT)*/
 /*			goto openpg;*/
 
@@ -650,6 +725,12 @@
 			}
 		}
 	}
+	if(nopen==0 && !use_ata) {
+		use_ata=TRUE;
+		fprintf(stderr, "Unable to open, trying with ATA: prefix...\n");
+		goto scanopen;
+	}
+
 	return (nopen);
 }
 
@@ -706,6 +787,11 @@
 	fcntl(f, F_SETFL, n);
 }
 
+/*!
+ *
+ * Return: TRUE when single target is chosen and was opened successfully, FALSE otherwise
+ */
+
 static BOOL
 sg_setup(SCSI *usalp, int f, int busno, int tgt, int tlun, int ataidx)
 {
@@ -734,10 +820,6 @@
 		onetarget = TRUE;
 
 	sg_mapdev(usalp, f, &Bus, &Target, &Lun, &Chan, &Ino, ataidx);
-
-  /*if(usallocal(usalp)) // skip it, already set up
-     return TRUE;
-*/
 	/*
 	 * For old kernels try to make the best guess.
 	 */
@@ -1756,3 +1838,8 @@
 	return (0);
 }
 
+static BOOL check_linux_26() {
+	int gen, tmp;
+	struct utsname buf;
+	return ( 0==uname( &buf ) && sscanf(buf.release, "%d.%d", &gen, &tmp)>1 && tmp>=6);
+}

Modified: cdrkit/trunk/libusal/scsiopen.c
===================================================================
--- cdrkit/trunk/libusal/scsiopen.c	2007-04-01 16:39:29 UTC (rev 721)
+++ cdrkit/trunk/libusal/scsiopen.c	2007-04-01 19:36:16 UTC (rev 722)
@@ -64,10 +64,9 @@
 #include <usal/scsitransp.h>
 
 #if    defined(linux) || defined(__linux) || defined(__linux__)
-#include <sys/utsname.h>
+extern BOOL check_linux_26();
 #endif
 
-
 #define	strbeg(s1, s2)	(strstr((s2), (s1)) == (s2))
 
 extern	int	lverbose;
@@ -125,42 +124,6 @@
 	}
 	usalp->debug = debug;
 	usalp->overbose = be_verbose;
-
-#ifdef __linux__
-	struct utsname buf; 
-	if(scsidev) {
-		int gen, tmp;
-		struct stat statbuf;
-		if( 0==uname( &buf ) &&
-				sscanf(buf.release, "%d.%d", &gen, &tmp)>1 &&
-				tmp>=6)
-		{
-			if(0==strncmp(scsidev, "ATAPI:", 6))
-			{
-				scsidev+=6;
-				fprintf(stderr, "\nWarning, the ATAPI: method is considered deprecated on modern kernels!\n"
-						"Mapping device specification to dev=%s now.\n"
-						"To force the old ATAPI: method, replace ATAPI: with OLDATAPI:\n", scsidev);
-			}
-			if(0!=stat("/sys/kernel", &statbuf)) {
-				fprintf(stderr, "\nWarning, sysfs is not mounted on /sys!\n"
-						"It is recommended to mount sysfs to allow better device configuration\n");
-				sleep(5);
-			}
-      /* some bitching about old style, ATA:b,t,l warning was displayed already. */
-			if(sscanf(scsidev, "%d,%d,%d", &tmp, &tmp, &tmp)>1) {
-				fprintf(stderr, "\nWarning, the deprecated pseudo SCSI syntax found as device specification.\n"
-						"Support for that may cease in the future versions of wodim. For now,\n"
-						"the device will be mapped to a block device file where possible.\n"
-						"Run \"wodim --devices\" for details.\n" );
-				sleep(5);
-			}
-		}
-		if(0==strncmp(scsidev, "OLDATAPI:", 9))
-			scsidev+=3;
-	}
-#endif
-
 	devname[0] = '\0';
 	if (scsidev != NULL && scsidev[0] != '\0') {
 		sdev = scsidev;

Modified: cdrkit/trunk/wodim/wodim.c
===================================================================
--- cdrkit/trunk/wodim/wodim.c	2007-04-01 16:39:29 UTC (rev 721)
+++ cdrkit/trunk/wodim/wodim.c	2007-04-01 19:36:16 UTC (rev 722)
@@ -406,22 +406,6 @@
 
   usalp = usal_open(dev, errstr, sizeof (errstr),
         debug, lverbose);
-  if(!usalp && dev) {
-     char *dalt;
-     int len=5+strlen(dev);
-
-     dalt=calloc(len, sizeof(char));
-     strcat(dalt, "ATA:");
-     strcat(dalt+4, dev);
-     usalp = usal_open(dalt, errstr, sizeof (errstr),
-           debug, lverbose);
-#ifdef __linux__
-     if(usalp && sscanf(dev, "%d,%d,%d", dalt, dalt, dalt)>1)
-        fprintf(stderr, "WARNING: mapped the pseudo SCSI syntax to ATA:%s. This method is considered"
-              "deprecated. Please specify the device by native path, run \"wodim --devices\".");
-#endif
-  }
-
   if(!usalp)
   {
      errmsg("\nCannot open SCSI driver!\n"




More information about the Debburn-changes mailing list