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