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