2
* Copyright (c) 2004-2006 Douglas Gilbert.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37
#include <sys/ioctl.h>
38
#include <sys/types.h>
43
/* A utility program for the Linux OS SCSI subsystem.
45
* This program issues the SCSI PREVENT ALLOW MEDIUM REMOVAL command to the
49
static char * version_str = "1.04 20060623";
51
#define ME "sg_prevent: "
54
static struct option long_options[] = {
57
{"prevent", 1, 0, 'p'},
58
{"verbose", 0, 0, 'v'},
59
{"version", 0, 0, 'V'},
65
fprintf(stderr, "Usage: "
66
"sg_prevent [--allow] [--help] [--prevent=<n>] [--verbose] "
69
" where: --allow|-a allow media removal\n"
70
" --help|-h print out usage message\n"
71
" --prevent=<n>|-p <n> prevention level (def: 1 -> "
73
" 0 -> allow, 1 -> prevent\n"
74
" 2 -> persistent allow, 3 -> "
75
"persistent prevent\n"
76
" --verbose|-v increase verbosity\n"
77
" --version|-V print version string and exit\n\n"
78
"Performs a PREVENT ALLOW MEDIUM REMOVAL SCSI command\n"
83
int main(int argc, char * argv[])
89
char device_name[256];
92
memset(device_name, 0, sizeof device_name);
96
c = getopt_long(argc, argv, "ahp:vV", long_options,
110
prevent = sg_get_num(optarg);
111
if ((prevent < 0) || (prevent > 3)) {
112
fprintf(stderr, "bad argument to '--prevent'\n");
113
return SG_LIB_SYNTAX_ERROR;
120
fprintf(stderr, ME "version: %s\n", version_str);
123
fprintf(stderr, "unrecognised switch code 0x%x ??\n", c);
125
return SG_LIB_SYNTAX_ERROR;
129
if ('\0' == device_name[0]) {
130
strncpy(device_name, argv[optind], sizeof(device_name) - 1);
131
device_name[sizeof(device_name) - 1] = '\0';
135
for (; optind < argc; ++optind)
136
fprintf(stderr, "Unexpected extra argument: %s\n",
139
return SG_LIB_SYNTAX_ERROR;
142
if (0 == device_name[0]) {
143
fprintf(stderr, "missing device name!\n");
145
return SG_LIB_SYNTAX_ERROR;
147
if (allow && (prevent >= 0)) {
148
fprintf(stderr, "can't give both '--allow' and '--prevent='\n");
150
return SG_LIB_SYNTAX_ERROR;
154
else if (prevent < 0)
155
prevent = 1; /* default is to prevent, as utility name suggests */
157
sg_fd = sg_cmds_open_device(device_name, 0 /* rw */, verbose);
159
fprintf(stderr, ME "open error: %s: %s\n", device_name,
160
safe_strerror(-sg_fd));
161
return SG_LIB_FILE_ERROR;
163
res = sg_ll_prevent_allow(sg_fd, prevent, 1, verbose);
167
else if (SG_LIB_CAT_NOT_READY == res)
168
fprintf(stderr, "Device not ready\n");
169
else if (SG_LIB_CAT_UNIT_ATTENTION == res)
170
fprintf(stderr, "Unit attention\n");
171
else if (SG_LIB_CAT_INVALID_OP == res)
172
fprintf(stderr, "Prevent allow medium removal command not "
174
else if (SG_LIB_CAT_ILLEGAL_REQ == res)
175
fprintf(stderr, "Prevent allow medium removal, bad field in "
178
fprintf(stderr, "Prevent allow medium removal command failed\n");
180
res = sg_cmds_close_device(sg_fd);
182
fprintf(stderr, "close error: %s\n", safe_strerror(-res));
184
return SG_LIB_FILE_ERROR;
186
return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;