@@ -16,6 +16,7 @@
#include <string.h>
#include <ctype.h>
#include <getopt.h>
+#include <sys/ioctl.h>
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>
@@ -26,6 +27,7 @@
#include "sg_lib.h"
#include "sg_cmds_basic.h"
#include "sg_cmds_extra.h"
+#include "sg_io_linux.h"
static const char * version_str = "0.44 20140202";
@@ -1029,6 +1031,8 @@
struct sg_simple_inquiry_resp inq_resp;
const char * cp;
struct opts_t opts;
+ int omode = 0;
+ const char *omode_desc = "rw";
memset(&opts, 0, sizeof(opts));
opts.prin = 1;
@@ -1292,10 +1296,31 @@
sg_cmds_close_device(sg_fd);
}
- if ((sg_fd = sg_cmds_open_device(device_name, 0 /* rw */,
+#ifdef SG_LIB_LINUX
+ /*
+ * PRIN commands do not provoke device changes, so we should avoid to
+ * open the device read-write so that the Linux kernel does not
generate
+ * a change event.
+ * Older kernel do not support PRIN commands submission on a read-only
+ * opened device, so don't try in this case.
+ */
+ int v;
+ if (opts.prin) {
+ sg_fd = sg_cmds_open_device(device_name, 1, opts.verbose);
+ if (sg_fd >= 0) {
+ if (ioctl(sg_fd, SG_GET_VERSION_NUM, &v) >= 0 && v >= 30000) {
+ omode = 1;
+ omode_desc = "ro";
+ }
+ sg_cmds_close_device(sg_fd);
+ }
+ }
+#endif
+
+ if ((sg_fd = sg_cmds_open_device(device_name, omode,
opts.verbose)) < 0) {
- pr2serr("sg_persist: error opening file (rw): %s: %s\n",
device_name,
- safe_strerror(-sg_fd));
+ pr2serr("sg_persist: error opening file (%s): %s: %s\n",
omode_desc,
+ device_name, safe_strerror(-sg_fd));