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