~ubuntu-branches/ubuntu/hardy/sg3-utils/hardy

« back to all changes in this revision

Viewing changes to sg_rbuf.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-09-19 21:00:55 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060919210055-j9deachd976os9my
Tags: 1.21-1ubuntu1
* Sync with Debian
* debian/control:
  + Let the -dev package depend on the runtime package
* llseek.c:
  + Fix syntax errors. Patch taken from Gentoo:
    http://mir2.ovh.net/gentoo-portage/sys-apps/sg3_utils/files/sg3_utils-llseek.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
 
46
46
#define ME "sg_rbuf: "
47
47
 
48
 
static char * version_str = "4.84 20060311";
 
48
static char * version_str = "4.85 20060623";
49
49
 
50
50
static void usage()
51
51
{
119
119
                    exit(0);
120
120
                case '?':
121
121
                    usage();
122
 
                    return 1;
 
122
                    return 0;
123
123
                default:
124
124
                    jmp_out = 1;
125
125
                    break;
135
135
                if ((1 != num) || (buf_size <= 0)) {
136
136
                    printf("Couldn't decode number after 'b=' option\n");
137
137
                    usage();
138
 
                    return 1;
 
138
                    return SG_LIB_SYNTAX_ERROR;
139
139
                }
140
140
                buf_size *= 1024;
141
141
            }
145
145
                if (1 != num) {
146
146
                    printf("Couldn't decode number after 's=' option\n");
147
147
                    usage();
148
 
                    return 1;
 
148
                    return SG_LIB_SYNTAX_ERROR;
149
149
                }
150
150
            } else if (jmp_out) {
151
151
                fprintf(stderr, "Unrecognized option: %s\n", cp);
152
152
                usage();
153
 
                return 1;
 
153
                return SG_LIB_SYNTAX_ERROR;
154
154
            }
155
155
        } else if (0 == file_name)
156
156
            file_name = cp;
158
158
            fprintf(stderr, "too many arguments, got: %s, not expecting: "
159
159
                    "%s\n", file_name, cp);
160
160
            usage();
161
 
            return 1;
 
161
            return SG_LIB_SYNTAX_ERROR;
162
162
        }
163
163
    }
164
164
    if (0 == file_name) {
165
165
        fprintf(stderr, "No <scsi_device> argument given\n");
166
166
        usage();
167
 
        return 1;
 
167
        return SG_LIB_SYNTAX_ERROR;
168
168
    }
169
169
 
170
170
    sg_fd = open(file_name, O_RDONLY | O_NONBLOCK);
171
171
    if (sg_fd < 0) {
172
172
        perror(ME "open error");
173
 
        return 1;
 
173
        return SG_LIB_FILE_ERROR;
174
174
    }
175
175
    /* Don't worry, being very careful not to write to a none-sg file ... */
176
176
    if (do_mmap) {
179
179
    }
180
180
    if (NULL == (rawp = malloc(512))) {
181
181
        printf(ME "out of memory (query)\n");
182
 
        return 1;
 
182
        return SG_LIB_CAT_OTHER;
183
183
    }
184
184
    rbBuff = rawp;
185
185
 
208
208
    if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
209
209
        perror(ME "SG_IO READ BUFFER descriptor error");
210
210
        if (rawp) free(rawp);
211
 
        return 1;
 
211
        return SG_LIB_CAT_OTHER;
212
212
    }
213
213
 
214
214
    if (verbose > 2)
215
215
        fprintf(stderr, "      duration=%u ms\n", io_hdr.duration);
216
216
    /* now for the error processing */
217
 
    switch (sg_err_category3(&io_hdr)) {
 
217
    res = sg_err_category3(&io_hdr);
 
218
    switch (res) {
218
219
    case SG_LIB_CAT_RECOVERED:
219
220
        sg_chk_n_print3("READ BUFFER descriptor, continuing", &io_hdr,
220
221
                        verbose > 1);
224
225
    default: /* won't bother decoding other categories */
225
226
        sg_chk_n_print3("READ BUFFER descriptor error", &io_hdr, verbose > 1);
226
227
        if (rawp) free(rawp);
227
 
        return 1;
 
228
        return (res >= 0) ? res : SG_LIB_CAT_OTHER;
228
229
    }
229
230
 
230
231
    buf_capacity = ((rbBuff[1] << 16) | (rbBuff[2] << 8) | rbBuff[3]);
237
238
        printf("Requested buffer size=%d exceeds reported capacity=%d\n",
238
239
               buf_size, buf_capacity);
239
240
        if (rawp) free(rawp);
240
 
        return 1;
 
241
        return SG_LIB_CAT_MALFORMED;
241
242
    }
242
243
    if (rawp) {
243
244
        free(rawp);
258
259
        if (MAP_FAILED == rbBuff) {
259
260
            if (ENOMEM == errno)
260
261
                printf(ME "mmap() out of memory, try a smaller "
261
 
                       "buffer size than %d KiB\n", buf_size / 1024);
 
262
                       "buffer size than %d KiB\n"
 
263
                       "    [with '-b=<n>' where <n> is in KB]\n",
 
264
                       buf_size / 1024);
262
265
            else
263
266
                perror(ME "error using mmap()");
264
 
            return 1;
 
267
            return SG_LIB_CAT_OTHER;
265
268
        }
266
269
    }
267
270
    else { /* non mmap-ed IO */
268
271
        rawp = malloc(buf_size + (do_dio ? psz : 0));
269
272
        if (NULL == rawp) {
270
273
            printf(ME "out of memory (data)\n");
271
 
            return 1;
 
274
            return SG_LIB_CAT_OTHER;
272
275
        }
273
276
        if (do_dio)    /* align to page boundary */
274
277
            rbBuff= (unsigned char *)(((unsigned long)rawp + psz - 1) &
323
326
        if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
324
327
            if (ENOMEM == errno)
325
328
                printf(ME "SG_IO data; out of memory, try a smaller "
326
 
                       "buffer size than %d KiB\n", buf_size / 1024);
 
329
                       "buffer size than %d KiB\n"
 
330
                       "    [with '-b=<n>' where <n> is in KB]\n",
 
331
                       buf_size / 1024);
327
332
            else
328
333
                perror(ME "SG_IO READ BUFFER data error");
329
334
            if (rawp) free(rawp);
330
 
            return 1;
 
335
            return SG_LIB_CAT_OTHER;
331
336
        }
332
337
 
333
338
        if (verbose > 2)
334
339
            fprintf(stderr, "      duration=%u ms\n",
335
340
                    io_hdr.duration);
336
341
        /* now for the error processing */
337
 
        switch (sg_err_category3(&io_hdr)) {
 
342
        res = sg_err_category3(&io_hdr);
 
343
        switch (res) {
338
344
        case SG_LIB_CAT_CLEAN:
339
345
            break;
340
346
        case SG_LIB_CAT_RECOVERED:
344
350
        default: /* won't bother decoding other categories */
345
351
            sg_chk_n_print3("READ BUFFER data error", &io_hdr, verbose > 1);
346
352
            if (rawp) free(rawp);
347
 
            return 1;
 
353
            return (res >= 0) ? res : SG_LIB_CAT_OTHER;
348
354
        }
349
355
        if (do_dio &&  
350
356
            ((io_hdr.info & SG_INFO_DIRECT_IO_MASK) != SG_INFO_DIRECT_IO))
392
398
    res = close(sg_fd);
393
399
    if (res < 0) {
394
400
        perror(ME "close error");
395
 
        return 1;
 
401
        return SG_LIB_FILE_ERROR;
396
402
    }
397
403
#ifdef SG_DEBUG
398
404
    if (clear)
400
406
    else
401
407
        printf("read buffer non-zero\n");
402
408
#endif
403
 
    return 0;
 
409
    return (res >= 0) ? res : SG_LIB_CAT_OTHER;
404
410
}