public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Stanislav Brabec <sbrabec@suse.cz>
To: util-linux@vger.kernel.org
Subject: [PATCH] eject: Check host_status and driver_status when using SG_IO.
Date: Fri, 14 Jun 2013 20:01:20 +0200	[thread overview]
Message-ID: <1371232880.11619.1.camel@oct.suse.cz> (raw)

I just reviewed patches from SUSE eject package. I found one which
probably still applies. It was written by Anna Bernathova (now
Maresova).

>From c8a15b1f325a20a33233f56815cf3c6e71b86031 Mon Sep 17 00:00:00 2001
From: Anna Bernathova <anicka@suse.cz>
Date: Tue, 27 May 2008 09:39:49 +0200
Subject: [PATCH] eject: Check host_status and driver_status when using SG_IO.

Detailed description from https://bugzilla.novell.com/show_bug.cgi?id=358033

Tejun Heo 2008-05-23 11:54:01 UTC:

ioctl(4, SG_IO, {'S', SG_DXFER_NONE, cmd[6]=[1b, 00, 00, 00, 02, 00],
mx_sb_len=32, iovec_count=0, dxfer_len=0, timeout=8000, flags=0, status=00,
masked_status=00, sb[0]=[], host_status=0x4, driver_status=0, resid=0,
duration=0, info=0x1}) = 0

The SG_IO succeeded with host_status=0x4 which is DID_BAD_TARGET meaning that
there was no such device.  SG_IO completion status is weird but still well
defined.  You'll need to check both host_status, driver_status and status to
determine that a command actually succeeded.

Please take a look at http://sg.torque.net/sg/sg_io.html and
include/scsi/scsi.h for details.

Signed-off-by: Anna Bernathova <anicka@suse.cz>
---
 sys-utils/eject.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/sys-utils/eject.c b/sys-utils/eject.c
index 4ec69e7..b319dd2 100644
--- a/sys-utils/eject.c
+++ b/sys-utils/eject.c
@@ -41,6 +41,7 @@
 #include <scsi/scsi.h>
 #include <scsi/sg.h>
 #include <scsi/scsi_ioctl.h>
+#include <scsi/sg_io_linux.h>
 #include <sys/time.h>
 
 #include <libmount.h>
@@ -604,17 +605,17 @@ static int eject_scsi(int fd)
 
 	io_hdr.cmdp = allowRmBlk;
 	status = ioctl(fd, SG_IO, (void *)&io_hdr);
-	if (status < 0)
+	if (status < 0 || io_hdr.host_status != DID_OK || io_hdr.driver_status != DRIVER_OK)
 		return 0;
 
 	io_hdr.cmdp = startStop1Blk;
 	status = ioctl(fd, SG_IO, (void *)&io_hdr);
-	if (status < 0)
+	if (status < 0 || io_hdr.host_status != DID_OK || io_hdr.driver_status != DRIVER_OK)
 		return 0;
 
 	io_hdr.cmdp = startStop2Blk;
 	status = ioctl(fd, SG_IO, (void *)&io_hdr);
-	if (status < 0)
+	if (status < 0 || io_hdr.host_status != DID_OK || io_hdr.driver_status != DRIVER_OK)
 		return 0;
 
 	/* force kernel to reread partition table when new disc inserted */
-- 
1.8.1.4

-- 
Best Regards / S pozdravem,

Stanislav Brabec
software developer
---------------------------------------------------------------------
SUSE LINUX, s. r. o.                          e-mail: sbrabec@suse.cz
Lihovarská 1060/12                            tel: +49 911 7405384547
190 00 Praha 9                                  fax: +420 284 028 951
Czech Republic                                    http://www.suse.cz/

-- 
Best Regards / S pozdravem,

Stanislav Brabec
software developer
---------------------------------------------------------------------
SUSE LINUX, s. r. o.                          e-mail: sbrabec@suse.cz
Lihovarská 1060/12                            tel: +49 911 7405384547
190 00 Praha 9                                  fax: +420 284 028 951
Czech Republic                                    http://www.suse.cz/


             reply	other threads:[~2013-06-14 18:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-14 18:01 Stanislav Brabec [this message]
2013-06-18 10:47 ` [PATCH] eject: Check host_status and driver_status when using SG_IO Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1371232880.11619.1.camel@oct.suse.cz \
    --to=sbrabec@suse.cz \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox