Sync usage with man page.
[netbsd-mini2440.git] / usr.bin / eject / eject.c
blobbdc3f94eb21cd652c8b0dd620e3e0277d37d5602
1 /* $NetBSD: eject.c,v 1.24 2009/01/15 03:18:30 lukem Exp $ */
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Chris Jones.
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.
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.
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 */
38 #ifndef lint
39 __RCSID("$NetBSD: eject.c,v 1.24 2009/01/15 03:18:30 lukem Exp $");
40 #endif /* not lint */
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>
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>
60 #ifdef AMD_SUPPORT
61 # include "am_glue.h"
62 #endif
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 { "cdr", "cd", DISK | LOADABLE },
84 { "cdrw", "cd", DISK | LOADABLE },
85 { "dvdrom", "cd", DISK | LOADABLE },
86 { "dvd", "cd", DISK | LOADABLE },
87 { "dvdr", "cd", DISK | LOADABLE },
88 { "dvdrw", "cd", DISK | LOADABLE },
89 { "mcd", "mcd", DISK | LOADABLE }, /* XXX Is this true? */
90 { "tape", "st", TAPE | NOTLOADABLE },
91 { "st", "st", TAPE | NOTLOADABLE },
92 { "dat", "st", TAPE | NOTLOADABLE },
93 { "exabyte", "st", TAPE | NOTLOADABLE }
95 #define MAXNICKLEN 12 /* at least enough room for the longest
96 * nickname */
97 #define MAXDEVLEN (MAXNICKLEN + 7) /* "/dev/r" ... "a" */
99 struct devtypes_s {
100 const char *name;
101 int type;
102 } devtypes[] = {
103 { "diskette", DISK | NOTLOADABLE },
104 { "floppy", DISK | NOTLOADABLE },
105 { "cdrom", DISK | LOADABLE },
106 { "disk", DISK | NOTLOADABLE },
107 { "tape", TAPE | NOTLOADABLE }
110 enum eject_op {
111 OP_EJECT, OP_LOAD, OP_LOCK, OP_UNLOCK
114 int verbose_f = 0;
115 int umount_f = 1;
117 int main(int, char *[]);
118 __dead static void usage(void);
119 static char *nick2dev(const char *);
120 static char *nick2rdev(const char *);
121 static int guess_devtype(const char *);
122 static void eject_disk(const char *, enum eject_op);
123 static void eject_tape(const char *, enum eject_op);
124 static void unmount_dev(const char *);
127 main(int argc, char *argv[])
129 int ch;
130 int devtype;
131 size_t n;
132 char *dev_name; /* XXX - devname is declared in stdlib.h */
133 enum eject_op op;
135 devtype = -1;
136 dev_name = NULL;
137 op = OP_EJECT;
139 while ((ch = getopt(argc, argv, "d:flLnt:Uv")) != -1) {
140 switch (ch) {
141 case 'd':
142 dev_name = optarg;
143 break;
144 case 'f':
145 umount_f = 0;
146 break;
147 case 'l':
148 if (op != OP_EJECT)
149 usage();
150 op = OP_LOAD;
151 break;
152 case 'L':
153 if (op != OP_EJECT)
154 usage();
155 op = OP_LOCK;
156 break;
157 case 'n':
158 for (n = 0; n < __arraycount(nicknames); n++) {
159 struct nicknames_s *np = &nicknames[n];
161 (void)printf("%s -> %s\n",
162 np->name, nick2dev(np->name));
164 return 0;
165 case 't':
166 for (n = 0; n < __arraycount(devtypes); n++) {
167 if (strcasecmp(devtypes[n].name, optarg)
168 == 0) {
169 devtype = devtypes[n].type;
170 break;
173 if (devtype == -1)
174 errx(1, "%s: unknown device type", optarg);
175 break;
176 case 'U':
177 if (op != OP_EJECT)
178 usage();
179 op = OP_UNLOCK;
180 break;
181 case 'v':
182 verbose_f = 1;
183 break;
184 default:
185 usage();
186 /* NOTREACHED */
189 argc -= optind;
190 argv += optind;
192 if (dev_name == NULL) {
193 if (argc == 0)
194 usage();
195 else
196 dev_name = argv[0];
198 if (devtype == -1)
199 devtype = guess_devtype(dev_name);
200 if (devtype == -1)
201 errx(1, "%s: unable to determine type of device", dev_name);
202 if (verbose_f) {
203 (void)printf("device type == ");
204 if ((devtype & TYPEMASK) == TAPE)
205 (void)printf("tape\n");
206 else
207 (void)printf("disk, floppy, or cdrom\n");
209 if (umount_f)
210 unmount_dev(dev_name);
212 /* XXX Tapes and disks have different ioctl's: */
213 if ((devtype & TYPEMASK) == TAPE)
214 eject_tape(dev_name, op);
215 else
216 eject_disk(dev_name, op);
218 if (verbose_f)
219 (void)printf("done.\n");
221 return 0;
224 __dead
225 static void
226 usage(void)
229 (void)fprintf(stderr, "usage: eject [-fv] [-l | -L | -U] "
230 "[-t device-type] [-d] device\n");
231 (void)fprintf(stderr, " eject -n\n");
232 exit(1);
235 static int
236 guess_devtype(const char *dev_name)
238 size_t n;
240 /* Nickname match: */
241 for (n = 0; n < __arraycount(nicknames); n++) {
242 if (strncasecmp(nicknames[n].name, dev_name,
243 strlen(nicknames[n].name)) == 0)
244 return nicknames[n].type;
248 * If we still don't know it, then try to compare vs. dev and
249 * rdev names that we know.
251 /* dev first: */
252 for (n = 0; n < __arraycount(nicknames); n++) {
253 char *name;
254 name = nick2dev(nicknames[n].name);
256 * Assume that the part of the name that distinguishes
257 * the instance of this device begins with a 0.
259 *(strchr(name, '0')) = '\0';
260 if (strncmp(name, dev_name, strlen(name)) == 0)
261 return nicknames[n].type;
264 /* Now rdev: */
265 for (n = 0; n < __arraycount(nicknames); n++) {
266 char *name = nick2rdev(nicknames[n].name);
267 *(strchr(name, '0')) = '\0';
268 if (strncmp(name, dev_name, strlen(name)) == 0)
269 return nicknames[n].type;
272 /* Not found. */
273 return -1;
276 /* "floppy5" -> "/dev/fd5a". Yep, this uses a static buffer. */
277 static char *
278 nick2dev(const char *nn)
280 static char dev_name[MAXDEVLEN];
281 size_t n;
282 int devnum;
284 devnum = 0;
285 for (n = 0; n < __arraycount(nicknames); n++) {
286 if (strncasecmp(nicknames[n].name, nn,
287 strlen(nicknames[n].name)) == 0) {
288 (void)sscanf(nn, "%*[^0-9]%d", &devnum);
289 (void)sprintf(dev_name, "/dev/%s%d",
290 nicknames[n].devname, devnum);
291 if ((nicknames[n].type & TYPEMASK) != TAPE)
292 (void)strcat(dev_name, "a");
293 return dev_name;
296 return NULL;
299 /* "floppy5" -> "/dev/rfd5c". Static buffer. */
300 static char *
301 nick2rdev(const char *nn)
303 static char dev_name[MAXDEVLEN];
304 size_t n;
305 int devnum;
307 devnum = 0;
308 for (n = 0; n < __arraycount(nicknames); n++) {
309 if (strncasecmp(nicknames[n].name, nn,
310 strlen(nicknames[n].name)) == 0) {
311 (void)sscanf(nn, "%*[^0-9]%d", &devnum);
312 (void)sprintf(dev_name, "/dev/r%s%d",
313 nicknames[n].devname, devnum);
314 if ((nicknames[n].type & TYPEMASK) != TAPE) {
315 (void)strcat(dev_name, "a");
316 if ((nicknames[n].type & FLOPPY) != FLOPPY)
317 dev_name[strlen(dev_name) - 1]
318 += getrawpartition();
320 return dev_name;
323 return NULL;
326 /* Unmount all filesystems attached to dev. */
327 static void
328 unmount_dev(const char *name)
330 struct statvfs *mounts;
331 size_t len;
332 int i, nmnts;
333 const char *dn;
335 #ifdef AMD_SUPPORT
336 am_init();
337 #endif
338 nmnts = getmntinfo(&mounts, MNT_NOWAIT);
339 if (nmnts == 0)
340 err(1, "getmntinfo");
342 /* Make sure we have a device name: */
343 dn = nick2dev(name);
344 if (dn == NULL)
345 dn = name;
347 /* Set len to strip off the partition name: */
348 len = strlen(dn);
349 if (!isdigit((unsigned char)dn[len - 1]))
350 len--;
351 if (!isdigit((unsigned char)dn[len - 1]))
352 errx(1, "Can't figure out base name for dev name %s", dn);
354 for (i = 0; i < nmnts; i++) {
355 if (strncmp(mounts[i].f_mntfromname, dn, len) == 0) {
356 if (verbose_f)
357 (void)printf("Unmounting %s from %s...\n",
358 mounts[i].f_mntfromname,
359 mounts[i].f_mntonname);
361 if (
362 #ifdef AMD_SUPPORT
363 am_unmount(mounts[i].f_mntonname) != 0 &&
364 #endif
365 unmount(mounts[i].f_mntonname, 0) == -1)
366 err(1, "unmount: %s", mounts[i].f_mntonname);
369 return;
372 static void
373 eject_tape(const char *name, enum eject_op op)
375 struct mtop m;
376 int fd;
377 const char *dn;
379 dn = nick2rdev(name);
380 if (dn == NULL)
381 dn = name; /* Hope for the best. */
382 fd = open(dn, O_RDONLY);
383 if (fd == -1)
384 err(1, "open: %s", dn);
385 switch (op) {
386 case OP_EJECT:
387 if (verbose_f)
388 (void)printf("Ejecting %s...\n", dn);
390 m.mt_op = MTOFFL;
391 m.mt_count = 0;
392 if (ioctl(fd, MTIOCTOP, &m) == -1)
393 err(1, "ioctl: MTIOCTOP: %s", dn);
394 break;
395 case OP_LOAD:
396 errx(1, "cannot load tapes");
397 /* NOTREACHED */
398 case OP_LOCK:
399 errx(1, "cannot lock tapes");
400 /* NOTREACHED */
401 case OP_UNLOCK:
402 errx(1, "cannot unlock tapes");
403 /* NOTREACHED */
405 (void)close(fd);
406 return;
409 static void
410 eject_disk(const char *name, enum eject_op op)
412 int fd;
413 const char *dn;
414 int arg;
416 dn = nick2rdev(name);
417 if (dn == NULL)
418 dn = name; /* Hope for the best. */
419 fd = open(dn, O_RDONLY);
420 if (fd == -1)
421 err(1, "open: %s", dn);
422 switch (op) {
423 case OP_LOAD:
424 if (verbose_f)
425 (void)printf("Closing %s...\n", dn);
427 if (ioctl(fd, CDIOCCLOSE, NULL) == -1)
428 err(1, "ioctl: CDIOCCLOSE: %s", dn);
429 break;
430 case OP_EJECT:
431 if (verbose_f)
432 (void)printf("Ejecting %s...\n", dn);
434 arg = 0;
435 if (umount_f == 0) {
436 /* force eject, unlock the device first */
437 if (ioctl(fd, DIOCLOCK, &arg) == -1)
438 err(1, "ioctl: DIOCLOCK: %s", dn);
439 arg = 1;
441 if (ioctl(fd, DIOCEJECT, &arg) == -1)
442 err(1, "ioctl: DIOCEJECT: %s", dn);
443 break;
444 case OP_LOCK:
445 if (verbose_f)
446 (void)printf("Locking %s...\n", dn);
448 arg = 1;
449 if (ioctl(fd, DIOCLOCK, &arg) == -1)
450 err(1, "ioctl: DIOCLOCK: %s", dn);
451 break;
452 case OP_UNLOCK:
453 if (verbose_f)
454 (void)printf("Unlocking %s...\n", dn);
456 arg = 0;
457 if (ioctl(fd, DIOCLOCK, &arg) == -1)
458 err(1, "ioctl: DIOCLOCK: %s", dn);
459 break;
462 (void)close(fd);
463 return;