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