xref: /freebsd/sbin/mount_cd9660/mount_cd9660.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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
42 static 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 /*
49 static char sccsid[] = "@(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95";
50 */
51 static const char rcsid[] =
52   "$FreeBSD$";
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 <stdlib.h>
63 #include <stdio.h>
64 #include <string.h>
65 #include <sysexits.h>
66 #include <unistd.h>
67 
68 #include "mntopts.h"
69 
70 struct mntopt mopts[] = {
71 	MOPT_STDOPTS,
72 	MOPT_UPDATE,
73 	{ "extatt", 0, ISOFSMNT_EXTATT, 1 },
74 	{ "gens", 0, ISOFSMNT_GENS, 1 },
75 	{ "rrip", 1, ISOFSMNT_NORRIP, 1 },
76 	{ "joliet", 1, ISOFSMNT_NOJOLIET, 1 },
77 	{ NULL }
78 };
79 
80 int	get_ssector(const char *dev);
81 void	usage(void);
82 
83 int
84 main(int argc, char **argv)
85 {
86 	struct iso_args args;
87 	int ch, mntflags, opts;
88 	char *dev, *dir, mntpath[MAXPATHLEN];
89 	struct vfsconf vfc;
90 	int error, verbose;
91 
92 	mntflags = opts = verbose = 0;
93 	memset(&args, 0, sizeof args);
94 	args.ssector = -1;
95 	while ((ch = getopt(argc, argv, "egjo:rs:v")) != -1)
96 		switch (ch) {
97 		case 'e':
98 			opts |= ISOFSMNT_EXTATT;
99 			break;
100 		case 'g':
101 			opts |= ISOFSMNT_GENS;
102 			break;
103 		case 'j':
104 			opts |= ISOFSMNT_NOJOLIET;
105 			break;
106 		case 'o':
107 			getmntopts(optarg, mopts, &mntflags, &opts);
108 			break;
109 		case 'r':
110 			opts |= ISOFSMNT_NORRIP;
111 			break;
112 		case 's':
113 			args.ssector = atoi(optarg);
114 			break;
115 		case 'v':
116 			verbose++;
117 			break;
118 		case '?':
119 		default:
120 			usage();
121 		}
122 	argc -= optind;
123 	argv += optind;
124 
125 	if (argc != 2)
126 		usage();
127 
128 	dev = argv[0];
129 	dir = argv[1];
130 
131 	/*
132 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
133 	 * slashes from the devicename if there are any.
134 	 */
135 	(void)checkpath(dir, mntpath);
136 	(void)rmslashes(dev, dev);
137 
138 #define DEFAULT_ROOTUID	-2
139 	/*
140 	 * ISO 9660 filesystems are not writeable.
141 	 */
142 	mntflags |= MNT_RDONLY;
143 	args.export.ex_flags = MNT_EXRDONLY;
144 	args.fspec = dev;
145 	args.export.ex_root = DEFAULT_ROOTUID;
146 	args.flags = opts;
147 
148 	if (args.ssector == -1) {
149 		/*
150 		 * The start of the session has not been specified on
151 		 * the command line.  If we can successfully read the
152 		 * TOC of a CD-ROM, use the last data track we find.
153 		 * Otherwise, just use 0, in order to mount the very
154 		 * first session.  This is compatible with the
155 		 * historic behaviour of mount_cd9660(8).  If the user
156 		 * has specified -s <ssector> above, we don't get here
157 		 * and leave the user's will.
158 		 */
159 		if ((args.ssector = get_ssector(dev)) == -1) {
160 			if (verbose)
161 				printf("could not determine starting sector, "
162 				       "using very first session\n");
163 			args.ssector = 0;
164 		} else if (verbose)
165 			printf("using starting sector %d\n", args.ssector);
166 	}
167 
168 	error = getvfsbyname("cd9660", &vfc);
169 	if (error && vfsisloadable("cd9660")) {
170 		if (vfsload("cd9660"))
171 			err(EX_OSERR, "vfsload(cd9660)");
172 		endvfsent();	/* flush cache */
173 		error = getvfsbyname("cd9660", &vfc);
174 	}
175 	if (error)
176 		errx(1, "cd9660 filesystem is not available");
177 
178 	if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
179 		err(1, NULL);
180 	exit(0);
181 }
182 
183 void
184 usage(void)
185 {
186 	(void)fprintf(stderr,
187 		"usage: mount_cd9660 [-egrv] [-o options] [-s startsector] special node\n");
188 	exit(EX_USAGE);
189 }
190 
191 int
192 get_ssector(const char *dev)
193 {
194 	struct ioc_toc_header h;
195 	struct ioc_read_toc_entry t;
196 	struct cd_toc_entry toc_buffer[100];
197 	int fd, ntocentries, i;
198 
199 	if ((fd = open(dev, O_RDONLY)) == -1)
200 		return -1;
201 	if (ioctl(fd, CDIOREADTOCHEADER, &h) == -1) {
202 		close(fd);
203 		return -1;
204 	}
205 
206 	ntocentries = h.ending_track - h.starting_track + 1;
207 	if (ntocentries > 100) {
208 		/* unreasonable, only 100 allowed */
209 		close(fd);
210 		return -1;
211 	}
212 	t.address_format = CD_LBA_FORMAT;
213 	t.starting_track = 0;
214 	t.data_len = ntocentries * sizeof(struct cd_toc_entry);
215 	t.data = toc_buffer;
216 
217 	if (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t) == -1) {
218 		close(fd);
219 		return -1;
220 	}
221 	close(fd);
222 
223 	for (i = ntocentries - 1; i >= 0; i--)
224 		if ((toc_buffer[i].control & 4) != 0)
225 			/* found a data track */
226 			break;
227 	if (i < 0)
228 		return -1;
229 
230 	return ntohl(toc_buffer[i].addr.lba);
231 }
232