File: mtx-changer

package info (click to toggle)
mtx 1.0-10
  • links: PTS
  • area: main
  • in suites: potato
  • size: 76 kB
  • ctags: 111
  • sloc: ansic: 665; perl: 158; makefile: 47
file content (224 lines) | stat: -rw-r--r-- 4,072 bytes parent folder | download | duplicates (2)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/perl

$debug = 0;
&debug_init;

$MTX = "/usr/sbin/mtx";

if(!$ENV{TAPE}){
    &debug("setting TAPE to /dev/tape");
    $ENV{TAPE} = "/dev/tape";
}

# initialize the state

@slots = (1..6);
$current_slot = 0;
$tape_device = "/dev/tape";

&changer_init();

&debug("main::current slot: $current_slot");

if($ARGV[0] eq "-eject"){
    &debug("main::got eject request");
    # eject is a no-op right now...
}

if($ARGV[0] eq "-info"){
    &debug("main::got info request");
    &print_info();
    exit(1);
}

if($ARGV[0] eq "-reset"){
    &debug("main::got changer reset request");
    &reset_changer();
}

if($ARGV[0] =~ /-slot/){
    $slot = $ARGV[1];

    if($slot =~ /^(next|prev|current|first|last)$/){
	&debug("main::got relative position request ($slot)");
	$slot = &get_slot_number($slot);
	&debug("main::translated slot number: $slot");
    }
    
    if($slot !~ /^[0-9]$/){
	&fatal_error("bogus slot number: $slot");
    }

    &change_to_slot($slot);
}

&print_status();


##### subs ########################################

sub debug {
    my $message = shift;
    print DEBUG "$message\n" if $debug;
}

sub changer_init {
    &debug("changer_init::start");
    if(!(@response = `$MTX status`)){
	&fatal_error("couldn't run mtx: $!");
    }

    &debug("changer_init:: @response");

    if($response[0] =~ /full .storage element ([1-6]) loaded/i){
	$current_slot = $1;
	&debug("changer_init::tape loaded: $current_slot");
	return();
    }

    if($response[0] =~ /empty/i) {
	&debug("changer_init::no tape loaded, checking for other tapes...");
	foreach $slot (@response) {
	    if($slot =~ /full/i) {
		$tape_in_changer = 1;
		break;
	    }
	}
	if(!$tape_in_changer) {
	    &debug("changer_init::no tapes in the changer");
	    &fatal_error("no tapes in changer");
	} else {
	    &debug("changer_init::loading tape 1");
	    &run_command("$MTX load 1");
	    $current_slot = 1;
	}
    }
}



sub print_status {
    &debug("print_status::start");
    print "$current_slot $tape_device\n";
    exit(0);
}



sub run_command {
    my $command = shift;
    
    &debug("run_command:: $command");
    system("$command 2> /dev/null");
    if($? != 0) {
	&fatal_error("[$command] returned non zero ($?)");
    }
}



sub print_info {
    print "$current_slot 6 1\n";
    exit(0);
}



sub fatal_error {
    my $error = shift;
    
    print "$current_slot $error\n";
    exit(2);
}



sub reset_changer {
    &debug("reset_changer:: start");
    &change_to_slot(1);
}



sub change_to_slot {
    my $slot = shift;

    &debug("change_to_slot:: $slot");
    if(!&in_range($slot)) {
	&non_fatal_error("slot $slot out of range");
    }

    if($slot == $current_slot){
	return;
    } else {
	&run_command("$MTX unload");
	&run_command("$MTX load $slot");
    }
    
    $current_slot = $slot;
}



sub get_slot_number {
    my $description = shift;

    &debug("get_slot_number: $description");
    if($description eq "first"){
	return(1);
    }

    if($description eq "last"){
	return(6);
    }

    if($description eq "current"){
	return($current_slot);
    }

    # we don't want to destroy $current_slot with post/preincrement
    $slot_index = $current_slot;

    if($description eq "prev"){
	if(&in_range($current_slot)){
	    return((6,1..6,1)[--$slot_index]);
	} else {
	    &non_fatal_error("current slot number out of range: $current_slot");
	}
    }

    if($description eq "next"){
	if(&in_range($current_slot)){
	    return((6,1..6,1)[++$slot_index]);
	} else {
	    &non_fatal_error("current slot number out of range: $current_slot");
	}
    }

    return(0);
}

sub in_range {
    my $slot_num = shift;
    
    &debug("in_range:: $slot_num");
    return(($slot_num <= 6) && ($slot_num >= 1));
}

sub non_fatal_error {
    my $message = shift;

    print "$current_slot $message\n";
    exit 1;
}

sub debug_init {
    return unless $debug;
    open(DEBUG, ">/tmp/x-changer.debug.$$");
    $today = localtime();
    print DEBUG "Date: $today\n";
    print DEBUG "UID: $<\n";
    print DEBUG "EUID: $>\n";
    print DEBUG "PATH: $ENV{PATH}\n";
}