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