eject.c revision 1.9 1 1.9 cjs /* $NetBSD: eject.c,v 1.9 1999/02/18 17:18:46 cjs Exp $ */
2 1.8 tron
3 1.8 tron /*-
4 1.8 tron * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.8 tron * All rights reserved.
6 1.8 tron *
7 1.8 tron * This code is derived from software contributed to The NetBSD Foundation
8 1.8 tron * by Chris Jones.
9 1.2 pk *
10 1.2 pk * Redistribution and use in source and binary forms, with or without
11 1.2 pk * modification, are permitted provided that the following conditions
12 1.2 pk * are met:
13 1.2 pk * 1. Redistributions of source code must retain the above copyright
14 1.2 pk * notice, this list of conditions and the following disclaimer.
15 1.2 pk * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 pk * notice, this list of conditions and the following disclaimer in the
17 1.2 pk * documentation and/or other materials provided with the distribution.
18 1.2 pk * 3. All advertising materials mentioning features or use of this software
19 1.2 pk * must display the following acknowledgement:
20 1.8 tron * This product includes software developed by the NetBSD
21 1.8 tron * Foundation, Inc. and its contributors.
22 1.8 tron * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.8 tron * contributors may be used to endorse or promote products derived
24 1.8 tron * from this software without specific prior written permission.
25 1.2 pk *
26 1.8 tron * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.8 tron * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.8 tron * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.8 tron * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.8 tron * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.8 tron * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.8 tron * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.8 tron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.8 tron * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.8 tron * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.8 tron * POSSIBILITY OF SUCH DAMAGE.
37 1.2 pk */
38 1.2 pk
39 1.2 pk /*
40 1.8 tron * Eject program. Deals with floppies, cdroms, and tapes. We could
41 1.8 tron * really use a generic GDIOCEJECT ioctl, that would work with
42 1.8 tron * everything. This eject also knows how to load media into cdrom
43 1.8 tron * drives, and it will attempt to extrapolate a device name from a
44 1.8 tron * nickname and number.
45 1.7 bouyer */
46 1.8 tron
47 1.8 tron #include <sys/cdefs.h>
48 1.8 tron #ifndef lint
49 1.9 cjs __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
50 1.9 cjs All rights reserved.\n");
51 1.9 cjs #endif /* not lint */
52 1.9 cjs
53 1.9 cjs #ifndef lint
54 1.9 cjs __RCSID("$NetBSD: eject.c,v 1.9 1999/02/18 17:18:46 cjs Exp $");
55 1.9 cjs #endif /* not lint */
56 1.8 tron
57 1.8 tron #include <ctype.h>
58 1.8 tron #include <err.h>
59 1.8 tron #include <fcntl.h>
60 1.8 tron #include <stdio.h>
61 1.8 tron #include <stdlib.h>
62 1.8 tron #include <string.h>
63 1.8 tron #include <unistd.h>
64 1.8 tron #include <sys/cdio.h>
65 1.8 tron #include <sys/disklabel.h>
66 1.8 tron #include <sys/ioctl.h>
67 1.8 tron #include <sys/param.h>
68 1.8 tron #include <sys/ucred.h>
69 1.8 tron #include <sys/mount.h>
70 1.8 tron #include <sys/mtio.h>
71 1.8 tron
72 1.8 tron struct nicknames_s {
73 1.8 tron char *name; /* The name given on the command line. */
74 1.8 tron char *devname; /* The base name of the device */
75 1.8 tron int type; /* The type of device, for determining
76 1.8 tron what ioctl to use. */
77 1.8 tron #define TAPE 0x10
78 1.8 tron #define DISK 0x20
79 1.8 tron /* OR one of the above with one of the below: */
80 1.8 tron #define NOTLOADABLE 0x00
81 1.8 tron #define LOADABLE 0x01
82 1.8 tron #define TYPEMASK ((int)~0x01)
83 1.8 tron } nicknames[] = {
84 1.8 tron { "diskette", "fd", DISK | NOTLOADABLE },
85 1.8 tron { "floppy", "fd", DISK | NOTLOADABLE },
86 1.8 tron { "fd", "fd", DISK | NOTLOADABLE },
87 1.8 tron { "cdrom", "cd", DISK | LOADABLE },
88 1.8 tron { "cd", "cd", DISK | LOADABLE },
89 1.8 tron { "mcd", "mcd", DISK | LOADABLE }, /* XXX Is this true? */
90 1.8 tron { "tape", "st", TAPE | NOTLOADABLE },
91 1.8 tron { "st", "st", TAPE | NOTLOADABLE },
92 1.8 tron { "dat", "st", TAPE | NOTLOADABLE },
93 1.8 tron { "exabyte", "st", TAPE | NOTLOADABLE },
94 1.8 tron };
95 1.8 tron #define MAXNICKLEN 12 /* at least enough room for the
96 1.8 tron longest nickname */
97 1.8 tron #define MAXDEVLEN (MAXNICKLEN + 7) /* "/dev/r" ... "a" */
98 1.8 tron
99 1.8 tron struct devtypes_s {
100 1.8 tron char *name;
101 1.8 tron int type;
102 1.8 tron } devtypes[] = {
103 1.8 tron { "diskette", DISK | NOTLOADABLE },
104 1.8 tron { "floppy", DISK | NOTLOADABLE },
105 1.8 tron { "cdrom", DISK | LOADABLE },
106 1.8 tron { "disk", DISK | NOTLOADABLE },
107 1.8 tron { "tape", TAPE | NOTLOADABLE },
108 1.8 tron };
109 1.8 tron
110 1.8 tron int verbose_f = 0;
111 1.8 tron int umount_f = 1;
112 1.8 tron int load_f = 0;
113 1.8 tron
114 1.8 tron int main __P((int, char *[]));
115 1.8 tron void usage __P((void));
116 1.8 tron char *nick2dev __P((char *));
117 1.8 tron char *nick2rdev __P((char *));
118 1.8 tron int guess_devtype __P((char *));
119 1.8 tron char *guess_nickname __P((char *));
120 1.8 tron void eject_tape __P((char *));
121 1.8 tron void eject_disk __P((char *));
122 1.8 tron void unmount_dev __P((char *));
123 1.8 tron
124 1.8 tron int
125 1.8 tron main(int argc,
126 1.8 tron char *argv[])
127 1.7 bouyer {
128 1.8 tron int ch;
129 1.8 tron int devtype = -1;
130 1.8 tron int n, i;
131 1.8 tron char *devname = NULL;
132 1.8 tron
133 1.8 tron while((ch = getopt(argc, argv, "d:flnt:v")) != -1) {
134 1.8 tron switch(ch) {
135 1.8 tron case 'd':
136 1.8 tron devname = optarg;
137 1.8 tron break;
138 1.8 tron case 'f':
139 1.8 tron umount_f = 0;
140 1.8 tron break;
141 1.8 tron case 'l':
142 1.8 tron load_f = 1;
143 1.8 tron break;
144 1.8 tron case 'n':
145 1.8 tron for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
146 1.8 tron n++) {
147 1.8 tron struct nicknames_s *np = &nicknames[n];
148 1.8 tron
149 1.8 tron printf("%s -> %s\n", np->name, nick2dev(np->name));
150 1.8 tron }
151 1.8 tron return(0);
152 1.8 tron case 't':
153 1.8 tron for(i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]);
154 1.8 tron i++) {
155 1.8 tron if(strcasecmp(devtypes[i].name, optarg) == 0) {
156 1.8 tron devtype = devtypes[i].type;
157 1.8 tron break;
158 1.8 tron }
159 1.8 tron }
160 1.8 tron if(devtype == -1) {
161 1.8 tron errx(1, "%s: unknown device type\n", optarg);
162 1.8 tron }
163 1.8 tron break;
164 1.8 tron case 'v':
165 1.8 tron verbose_f = 1;
166 1.8 tron break;
167 1.8 tron default:
168 1.8 tron warnx("-%c: unknown switch", ch);
169 1.8 tron usage();
170 1.8 tron /* NOTREACHED */
171 1.8 tron }
172 1.8 tron }
173 1.8 tron argc -= optind;
174 1.8 tron argv += optind;
175 1.8 tron
176 1.8 tron if(devname == NULL) {
177 1.8 tron if(argc == 0) {
178 1.8 tron usage();
179 1.8 tron /* NOTREACHED */
180 1.8 tron } else
181 1.8 tron devname = argv[0];
182 1.8 tron }
183 1.7 bouyer
184 1.7 bouyer
185 1.8 tron if(devtype == -1) {
186 1.8 tron devtype = guess_devtype(devname);
187 1.8 tron }
188 1.8 tron if(devtype == -1) {
189 1.8 tron errx(1, "%s: unable to determine type of device",
190 1.8 tron devname);
191 1.8 tron }
192 1.8 tron if(verbose_f) {
193 1.8 tron printf("device type == ");
194 1.8 tron if((devtype & TYPEMASK) == TAPE)
195 1.8 tron printf("tape\n");
196 1.8 tron else
197 1.8 tron printf("disk, floppy, or cdrom\n");
198 1.8 tron }
199 1.8 tron
200 1.8 tron if(umount_f)
201 1.8 tron unmount_dev(devname);
202 1.8 tron
203 1.8 tron /* XXX Tapes and disks have different ioctl's: */
204 1.8 tron if((devtype & TYPEMASK) == TAPE)
205 1.8 tron eject_tape(devname);
206 1.8 tron else
207 1.8 tron eject_disk(devname);
208 1.8 tron
209 1.8 tron if(verbose_f)
210 1.8 tron printf("done.\n");
211 1.8 tron
212 1.8 tron return(0);
213 1.7 bouyer }
214 1.7 bouyer
215 1.8 tron void
216 1.8 tron usage(void)
217 1.8 tron {
218 1.8 tron errx(1, "Usage: eject [-n][-f][-v][-l][-t type][-d] device | nickname");
219 1.8 tron }
220 1.2 pk
221 1.8 tron int
222 1.8 tron guess_devtype(char *devname)
223 1.2 pk {
224 1.8 tron int n;
225 1.2 pk
226 1.8 tron /* Nickname match: */
227 1.8 tron for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]);
228 1.8 tron n++) {
229 1.8 tron if(strncasecmp(nicknames[n].name, devname,
230 1.8 tron strlen(nicknames[n].name)) == 0)
231 1.8 tron return(nicknames[n].type);
232 1.8 tron }
233 1.8 tron
234 1.8 tron /*
235 1.8 tron * If we still don't know it, then try to compare vs. dev
236 1.8 tron * and rdev names that we know.
237 1.8 tron */
238 1.8 tron /* dev first: */
239 1.8 tron for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
240 1.8 tron char *name;
241 1.8 tron name = nick2dev(nicknames[n].name);
242 1.8 tron /*
243 1.8 tron * Assume that the part of the name that distinguishes the
244 1.8 tron * instance of this device begins with a 0.
245 1.8 tron */
246 1.8 tron *(strchr(name, '0')) = '\0';
247 1.8 tron if(strncmp(name, devname, strlen(name)) == 0)
248 1.8 tron return(nicknames[n].type);
249 1.8 tron }
250 1.8 tron
251 1.8 tron /* Now rdev: */
252 1.8 tron for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
253 1.8 tron char *name = nick2rdev(nicknames[n].name);
254 1.8 tron *(strchr(name, '0')) = '\0';
255 1.8 tron if(strncmp(name, devname, strlen(name)) == 0)
256 1.8 tron return(nicknames[n].type);
257 1.8 tron }
258 1.8 tron
259 1.8 tron /* Not found. */
260 1.8 tron return(-1);
261 1.8 tron }
262 1.8 tron
263 1.8 tron /* "floppy5" -> "/dev/fd5a". Yep, this uses a static buffer. */
264 1.8 tron char *
265 1.8 tron nick2dev(char *nn)
266 1.8 tron {
267 1.8 tron int n;
268 1.8 tron static char devname[MAXDEVLEN];
269 1.8 tron int devnum = 0;
270 1.8 tron
271 1.8 tron for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
272 1.8 tron if(strncasecmp(nicknames[n].name, nn,
273 1.8 tron strlen(nicknames[n].name)) == 0) {
274 1.8 tron sscanf(nn, "%*[^0-9]%d", &devnum);
275 1.8 tron sprintf(devname, "/dev/%s%d", nicknames[n].devname,
276 1.8 tron devnum);
277 1.8 tron if((nicknames[n].type & TYPEMASK) != TAPE)
278 1.8 tron strcat(devname, "a");
279 1.8 tron return(devname);
280 1.2 pk }
281 1.8 tron }
282 1.2 pk
283 1.8 tron return(NULL);
284 1.8 tron }
285 1.2 pk
286 1.8 tron /* "floppy5" -> "/dev/rfd5c". Static buffer. */
287 1.8 tron char *
288 1.8 tron nick2rdev(char *nn)
289 1.8 tron {
290 1.8 tron int n;
291 1.8 tron static char devname[MAXDEVLEN];
292 1.8 tron int devnum = 0;
293 1.8 tron
294 1.8 tron for(n = 0; n < sizeof(nicknames) / sizeof(nicknames[0]); n++) {
295 1.8 tron if(strncasecmp(nicknames[n].name, nn,
296 1.8 tron strlen(nicknames[n].name)) == 0) {
297 1.8 tron sscanf(nn, "%*[^0-9]%d", &devnum);
298 1.8 tron sprintf(devname, "/dev/r%s%d", nicknames[n].devname,
299 1.8 tron devnum);
300 1.8 tron if((nicknames[n].type & TYPEMASK) != TAPE) {
301 1.8 tron strcat(devname, "a");
302 1.8 tron devname[strlen(devname) - 1] += RAW_PART;
303 1.8 tron }
304 1.8 tron return(devname);
305 1.2 pk }
306 1.8 tron }
307 1.2 pk
308 1.8 tron return(NULL);
309 1.2 pk }
310 1.2 pk
311 1.8 tron /* Unmount all filesystems attached to dev. */
312 1.8 tron void
313 1.8 tron unmount_dev(char *name)
314 1.2 pk {
315 1.8 tron struct statfs *mounts;
316 1.8 tron int i, nmnts, len;
317 1.8 tron char *dn;
318 1.8 tron
319 1.8 tron nmnts = getmntinfo(&mounts, MNT_NOWAIT);
320 1.8 tron if(nmnts == 0) {
321 1.8 tron err(1, "getmntinfo");
322 1.8 tron }
323 1.8 tron
324 1.8 tron /* Make sure we have a device name: */
325 1.8 tron dn = nick2dev(name);
326 1.8 tron if(dn == NULL)
327 1.8 tron dn = name;
328 1.8 tron
329 1.8 tron /* Set len to strip off the partition name: */
330 1.8 tron len = strlen(dn);
331 1.8 tron if(!isdigit(dn[len - 1]))
332 1.8 tron len--;
333 1.8 tron if(!isdigit(dn[len - 1])) {
334 1.8 tron errx(1, "Can't figure out base name for dev name %s", dn);
335 1.8 tron }
336 1.8 tron
337 1.8 tron for(i = 0; i < nmnts; i++) {
338 1.8 tron if(strncmp(mounts[i].f_mntfromname, dn, len) == 0) {
339 1.8 tron if(verbose_f)
340 1.8 tron printf("Unmounting %s from %s...\n",
341 1.8 tron mounts[i].f_mntfromname,
342 1.8 tron mounts[i].f_mntonname);
343 1.8 tron
344 1.8 tron if(unmount(mounts[i].f_mntonname, 0) == -1) {
345 1.8 tron err(1, "unmount: %s", mounts[i].f_mntonname);
346 1.8 tron }
347 1.2 pk }
348 1.8 tron }
349 1.8 tron
350 1.8 tron return;
351 1.8 tron }
352 1.2 pk
353 1.8 tron void
354 1.8 tron eject_tape(char *name)
355 1.8 tron {
356 1.8 tron struct mtop m;
357 1.8 tron int fd;
358 1.8 tron char *dn;
359 1.8 tron
360 1.8 tron dn = nick2rdev(name);
361 1.8 tron if(dn == NULL) {
362 1.8 tron dn = name; /* Hope for the best. */
363 1.8 tron }
364 1.8 tron
365 1.8 tron fd = open(dn, O_RDONLY);
366 1.8 tron if(fd == -1) {
367 1.8 tron err(1, "open: %s", dn);
368 1.8 tron }
369 1.8 tron
370 1.8 tron if(verbose_f)
371 1.8 tron printf("Ejecting %s...\n", dn);
372 1.8 tron
373 1.8 tron m.mt_op = MTOFFL;
374 1.8 tron m.mt_count = 0;
375 1.8 tron if(ioctl(fd, MTIOCTOP, &m) == -1) {
376 1.8 tron err(1, "ioctl: MTIOCTOP: %s", dn);
377 1.8 tron }
378 1.2 pk
379 1.8 tron close(fd);
380 1.8 tron return;
381 1.8 tron }
382 1.2 pk
383 1.8 tron void
384 1.8 tron eject_disk(char *name)
385 1.8 tron {
386 1.8 tron int fd;
387 1.8 tron char *dn;
388 1.8 tron int arg;
389 1.8 tron
390 1.8 tron dn = nick2rdev(name);
391 1.8 tron if(dn == NULL) {
392 1.8 tron dn = name; /* Hope for the best. */
393 1.8 tron }
394 1.8 tron
395 1.8 tron fd = open(dn, O_RDONLY);
396 1.8 tron if(fd == -1) {
397 1.8 tron err(1, "open: %s", dn);
398 1.8 tron }
399 1.8 tron
400 1.8 tron if(load_f) {
401 1.8 tron if(verbose_f)
402 1.8 tron printf("Closing %s...\n", dn);
403 1.8 tron
404 1.8 tron if(ioctl(fd, CDIOCCLOSE, NULL) == -1) {
405 1.8 tron err(1, "ioctl: CDIOCCLOSE: %s", dn);
406 1.8 tron }
407 1.8 tron } else {
408 1.8 tron if(verbose_f)
409 1.8 tron printf("Ejecting %s...\n", dn);
410 1.8 tron
411 1.8 tron arg = 0;
412 1.8 tron if(ioctl(fd, DIOCLOCK, (char *)&arg) == -1)
413 1.8 tron err(1, "ioctl: DIOCLOCK: %s", dn);
414 1.8 tron if(ioctl(fd, DIOCEJECT, &arg) == -1)
415 1.8 tron err(1, "ioctl: DIOCEJECT: %s", dn);
416 1.8 tron }
417 1.2 pk
418 1.8 tron close(fd);
419 1.8 tron return;
420 1.2 pk }
421