~ubuntu-branches/ubuntu/intrepid/sg3-utils/intrepid

« back to all changes in this revision

Viewing changes to sg_prevent.c

  • Committer: Bazaar Package Importer
  • Author(s): Luk Claes
  • Date: 2006-11-05 17:23:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.2 edgy)
  • Revision ID: james.westby@ubuntu.com-20061105172329-l4loha00sk36qz6k
* Non-maintainer upload.
* Fix FTBFS due to old syscall usage (Closes: #395512).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2004-2006 Douglas Gilbert.
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
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.
 
15
 *
 
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
 
26
 * SUCH DAMAGE.
 
27
 *
 
28
 */
 
29
 
 
30
#include <unistd.h>
 
31
#include <fcntl.h>
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
#include <errno.h>
 
36
#include <getopt.h>
 
37
#include <sys/ioctl.h>
 
38
#include <sys/types.h>
 
39
#include <sys/stat.h>
 
40
#include "sg_lib.h"
 
41
#include "sg_cmds.h"
 
42
 
 
43
/* A utility program for the Linux OS SCSI subsystem.
 
44
 *
 
45
 * This program issues the SCSI PREVENT ALLOW MEDIUM REMOVAL command to the
 
46
 * given SCSI device.
 
47
 */
 
48
 
 
49
static char * version_str = "1.04 20060623";
 
50
 
 
51
#define ME "sg_prevent: "
 
52
 
 
53
 
 
54
static struct option long_options[] = {
 
55
        {"allow", 0, 0, 'a'},
 
56
        {"help", 0, 0, 'h'},
 
57
        {"prevent", 1, 0, 'p'},
 
58
        {"verbose", 0, 0, 'v'},
 
59
        {"version", 0, 0, 'V'},
 
60
        {0, 0, 0, 0},
 
61
};
 
62
 
 
63
static void usage()
 
64
{
 
65
    fprintf(stderr, "Usage: "
 
66
          "sg_prevent [--allow] [--help] [--prevent=<n>] [--verbose] "
 
67
          "[--version]\n"
 
68
          "                   <scsi_device>\n"
 
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 -> "
 
72
          "prevent)\n"
 
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"
 
79
          );
 
80
 
 
81
}
 
82
 
 
83
int main(int argc, char * argv[])
 
84
{
 
85
    int sg_fd, res, c;
 
86
    int allow = 0;
 
87
    int prevent = -1;
 
88
    int verbose = 0;
 
89
    char device_name[256];
 
90
    int ret = 0;
 
91
 
 
92
    memset(device_name, 0, sizeof device_name);
 
93
    while (1) {
 
94
        int option_index = 0;
 
95
 
 
96
        c = getopt_long(argc, argv, "ahp:vV", long_options,
 
97
                        &option_index);
 
98
        if (c == -1)
 
99
            break;
 
100
 
 
101
        switch (c) {
 
102
        case 'a':
 
103
            allow = 1;
 
104
            break;
 
105
        case 'h':
 
106
        case '?':
 
107
            usage();
 
108
            return 0;
 
109
        case 'p':
 
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;
 
114
            }
 
115
            break;
 
116
        case 'v':
 
117
            ++verbose;
 
118
            break;
 
119
        case 'V':
 
120
            fprintf(stderr, ME "version: %s\n", version_str);
 
121
            return 0;
 
122
        default:
 
123
            fprintf(stderr, "unrecognised switch code 0x%x ??\n", c);
 
124
            usage();
 
125
            return SG_LIB_SYNTAX_ERROR;
 
126
        }
 
127
    }
 
128
    if (optind < argc) {
 
129
        if ('\0' == device_name[0]) {
 
130
            strncpy(device_name, argv[optind], sizeof(device_name) - 1);
 
131
            device_name[sizeof(device_name) - 1] = '\0';
 
132
            ++optind;
 
133
        }
 
134
        if (optind < argc) {
 
135
            for (; optind < argc; ++optind)
 
136
                fprintf(stderr, "Unexpected extra argument: %s\n",
 
137
                        argv[optind]);
 
138
            usage();
 
139
            return SG_LIB_SYNTAX_ERROR;
 
140
        }
 
141
    }
 
142
    if (0 == device_name[0]) {
 
143
        fprintf(stderr, "missing device name!\n");
 
144
        usage();
 
145
        return SG_LIB_SYNTAX_ERROR;
 
146
    }
 
147
    if (allow && (prevent >= 0)) {
 
148
        fprintf(stderr, "can't give both '--allow' and '--prevent='\n");
 
149
        usage();
 
150
        return SG_LIB_SYNTAX_ERROR;
 
151
    }
 
152
    if (allow)
 
153
        prevent = 0;
 
154
    else if (prevent < 0)
 
155
        prevent = 1;    /* default is to prevent, as utility name suggests */
 
156
 
 
157
    sg_fd = sg_cmds_open_device(device_name, 0 /* rw */, verbose);
 
158
    if (sg_fd < 0) {
 
159
        fprintf(stderr, ME "open error: %s: %s\n", device_name,
 
160
                safe_strerror(-sg_fd));
 
161
        return SG_LIB_FILE_ERROR;
 
162
    }
 
163
    res = sg_ll_prevent_allow(sg_fd, prevent, 1, verbose);
 
164
    ret = res;
 
165
    if (0 == res)
 
166
        ;
 
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 "
 
173
                "supported\n");
 
174
    else if (SG_LIB_CAT_ILLEGAL_REQ == res)
 
175
        fprintf(stderr, "Prevent allow medium removal, bad field in "
 
176
                "command\n");
 
177
    else
 
178
        fprintf(stderr, "Prevent allow medium removal command failed\n");
 
179
 
 
180
    res = sg_cmds_close_device(sg_fd);
 
181
    if (res < 0) {
 
182
        fprintf(stderr, "close error: %s\n", safe_strerror(-res));
 
183
        if (0 == ret)
 
184
            return SG_LIB_FILE_ERROR;
 
185
    }
 
186
    return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
 
187
}