mount_cd9660.c revision 74097
1/*
2 * Copyright (c) 1992, 1993, 1994
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley
6 * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7 * Support code is derived from software contributed to Berkeley
8 * by Atsushi Murai (amurai@spec.co.jp).
9 *
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.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *      @(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95
39 */
40
41#ifndef lint
42static char copyright[] =
43"@(#) Copyright (c) 1992, 1993, 1994\n\
44        The Regents of the University of California.  All rights reserved.\n";
45#endif /* not lint */
46
47#ifndef lint
48/*
49static char sccsid[] = "@(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95";
50*/
51static const char rcsid[] =
52  "$FreeBSD: head/sbin/mount_cd9660/mount_cd9660.c 74097 2001-03-11 10:06:28Z bp $";
53#endif /* not lint */
54
55#include <sys/cdio.h>
56#include <sys/file.h>
57#include <sys/param.h>
58#include <sys/mount.h>
59#include <sys/../isofs/cd9660/cd9660_mount.h>
60
61#include <err.h>
62#include <errno.h>
63#include <stdlib.h>
64#include <stdio.h>
65#include <string.h>
66#include <sysexits.h>
67#include <unistd.h>
68
69#include "mntopts.h"
70
71struct mntopt mopts[] = {
72	MOPT_STDOPTS,
73	MOPT_UPDATE,
74	{ "extatt", 0, ISOFSMNT_EXTATT, 1 },
75	{ "gens", 0, ISOFSMNT_GENS, 1 },
76	{ "rrip", 1, ISOFSMNT_NORRIP, 1 },
77	{ "joliet", 1, ISOFSMNT_NOJOLIET, 1 },
78	{ "strictjoliet", 1, ISOFSMNT_BROKENJOLIET, 1 },
79	{ NULL }
80};
81
82int	get_ssector(const char *dev);
83void	usage(void);
84
85int
86main(int argc, char **argv)
87{
88	struct iso_args args;
89	int ch, mntflags, opts;
90	char *dev, *dir, mntpath[MAXPATHLEN];
91	struct vfsconf vfc;
92	int error, verbose;
93
94	mntflags = opts = verbose = 0;
95	memset(&args, 0, sizeof args);
96	args.ssector = -1;
97	while ((ch = getopt(argc, argv, "begjo:rs:v")) != -1)
98		switch (ch) {
99		case 'b':
100			opts |= ISOFSMNT_BROKENJOLIET;
101			break;
102		case 'e':
103			opts |= ISOFSMNT_EXTATT;
104			break;
105		case 'g':
106			opts |= ISOFSMNT_GENS;
107			break;
108		case 'j':
109			opts |= ISOFSMNT_NOJOLIET;
110			break;
111		case 'o':
112			getmntopts(optarg, mopts, &mntflags, &opts);
113			break;
114		case 'r':
115			opts |= ISOFSMNT_NORRIP;
116			break;
117		case 's':
118			args.ssector = atoi(optarg);
119			break;
120		case 'v':
121			verbose++;
122			break;
123		case '?':
124		default:
125			usage();
126		}
127	argc -= optind;
128	argv += optind;
129
130	if (argc != 2)
131		usage();
132
133	dev = argv[0];
134	dir = argv[1];
135
136	/*
137	 * Resolve the mountpoint with realpath(3) and remove unnecessary
138	 * slashes from the devicename if there are any.
139	 */
140	(void)checkpath(dir, mntpath);
141	(void)rmslashes(dev, dev);
142
143#define DEFAULT_ROOTUID	-2
144	/*
145	 * ISO 9660 filesystems are not writeable.
146	 */
147	mntflags |= MNT_RDONLY;
148	args.export.ex_flags = MNT_EXRDONLY;
149	args.fspec = dev;
150	args.export.ex_root = DEFAULT_ROOTUID;
151	args.flags = opts;
152
153	if (args.ssector == -1) {
154		/*
155		 * The start of the session has not been specified on
156		 * the command line.  If we can successfully read the
157		 * TOC of a CD-ROM, use the last data track we find.
158		 * Otherwise, just use 0, in order to mount the very
159		 * first session.  This is compatible with the
160		 * historic behaviour of mount_cd9660(8).  If the user
161		 * has specified -s <ssector> above, we don't get here
162		 * and leave the user's will.
163		 */
164		if ((args.ssector = get_ssector(dev)) == -1) {
165			if (verbose)
166				printf("could not determine starting sector, "
167				       "using very first session\n");
168			args.ssector = 0;
169		} else if (verbose)
170			printf("using starting sector %d\n", args.ssector);
171	}
172
173	error = getvfsbyname("cd9660", &vfc);
174	if (error && vfsisloadable("cd9660")) {
175		if (vfsload("cd9660"))
176			err(EX_OSERR, "vfsload(cd9660)");
177		endvfsent();	/* flush cache */
178		error = getvfsbyname("cd9660", &vfc);
179	}
180	if (error)
181		errx(1, "cd9660 filesystem is not available");
182
183	if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
184		err(1, "%s", args.fspec);
185	exit(0);
186}
187
188void
189usage(void)
190{
191	(void)fprintf(stderr,
192		"usage: mount_cd9660 [-egrv] [-o options] [-s startsector] special node\n");
193	exit(EX_USAGE);
194}
195
196int
197get_ssector(const char *dev)
198{
199	struct ioc_toc_header h;
200	struct ioc_read_toc_entry t;
201	struct cd_toc_entry toc_buffer[100];
202	int fd, ntocentries, i;
203
204	if ((fd = open(dev, O_RDONLY)) == -1)
205		return -1;
206	if (ioctl(fd, CDIOREADTOCHEADER, &h) == -1) {
207		close(fd);
208		return -1;
209	}
210
211	ntocentries = h.ending_track - h.starting_track + 1;
212	if (ntocentries > 100) {
213		/* unreasonable, only 100 allowed */
214		close(fd);
215		return -1;
216	}
217	t.address_format = CD_LBA_FORMAT;
218	t.starting_track = 0;
219	t.data_len = ntocentries * sizeof(struct cd_toc_entry);
220	t.data = toc_buffer;
221
222	if (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t) == -1) {
223		close(fd);
224		return -1;
225	}
226	close(fd);
227
228	for (i = ntocentries - 1; i >= 0; i--)
229		if ((toc_buffer[i].control & 4) != 0)
230			/* found a data track */
231			break;
232	if (i < 0)
233		return -1;
234
235	return ntohl(toc_buffer[i].addr.lba);
236}
237