aboutsummaryrefslogtreecommitdiffstats
path: root/unload.c
blob: a1005c2862b9b16d307100fb5c8d8487c8fe7d8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

#include "scsi/scsi.h"
#include "scsi/scsi_ioctl.h"

#define DEFAULT_DEVICE	"/dev/sda"

typedef struct scsi_ioctl_command {
        unsigned int inlen;
        unsigned int outlen;
        unsigned char data[0];
} Scsi_Ioctl_Command;


int
main(int argc, char *argv[])
{
    int  fd;
    char *prog, device[256], request[256], *h;
    Scsi_Ioctl_Command *sic = (Scsi_Ioctl_Command*)request;

    h = strrchr(argv[0],'/');
    prog = h ? h+1 : argv[0];

    if (argc > 1) {
	strcpy(device,argv[1]);
    } else {
	strcpy(device,DEFAULT_DEVICE);
    }

    if (-1 == (fd = open(device, O_RDONLY))) {
	fprintf(stderr,"%s: can't open %s: %s\n",prog,device,
		sys_errlist[errno]);
	exit(1);
    }

    if (-1 == ioctl(fd,SCSI_IOCTL_DOORUNLOCK,sic)) {
	perror("ioctl (unlock)");
	exit(1);
    }

    memset(request,0,256);
    sic->inlen  = 0;
    sic->outlen = 0;
    sic->data[0] = START_STOP;
    sic->data[1] = 0;
    sic->data[2] = 0;
    sic->data[3] = 0;
    sic->data[4] = (0 == strcmp(prog,"unload")) ? 2 : 3;
    sic->data[5] = 0;

    if (-1 == ioctl(fd,SCSI_IOCTL_SEND_COMMAND,sic)) {
	perror("ioctl (eject)");
	exit(1);
    }

    close(fd);
    exit(0);
}