Make the userspace work.
[dragonfly.git] / sbin / mount_cd9660 / mount_cd9660.c
CommitLineData
984263bc
MD
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
1de703da
MD
39 *
40 * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved.
41 * @(#)mount_cd9660.c 8.7 (Berkeley) 5/1/95
42 * $FreeBSD: src/sbin/mount_cd9660/mount_cd9660.c,v 1.15.2.3 2001/03/14 12:05:01 bp Exp $
e7adb9bc 43 * $DragonFly: src/sbin/mount_cd9660/mount_cd9660.c,v 1.5 2005/04/02 21:43:15 dillon Exp $
984263bc
MD
44 */
45
984263bc
MD
46#include <sys/cdio.h>
47#include <sys/file.h>
48#include <sys/param.h>
49#include <sys/mount.h>
f50c51ce
AP
50#include <sys/iconv.h>
51#include <sys/linker.h>
52#include <sys/module.h>
53#include <vfs/isofs/cd9660/cd9660_mount.h>
984263bc
MD
54
55#include <err.h>
56#include <errno.h>
57#include <stdlib.h>
58#include <stdio.h>
59#include <string.h>
f50c51ce 60#include <locale.h>
984263bc
MD
61#include <sysexits.h>
62#include <unistd.h>
63
64#include "mntopts.h"
65
66struct mntopt mopts[] = {
67 MOPT_STDOPTS,
68 MOPT_UPDATE,
69 { "extatt", 0, ISOFSMNT_EXTATT, 1 },
70 { "gens", 0, ISOFSMNT_GENS, 1 },
71 { "rrip", 1, ISOFSMNT_NORRIP, 1 },
72 { "joliet", 1, ISOFSMNT_NOJOLIET, 1 },
73 { "strictjoliet", 1, ISOFSMNT_BROKENJOLIET, 1 },
e7adb9bc 74 MOPT_NULL
984263bc
MD
75};
76
e7adb9bc
MD
77static int get_ssector(const char *dev);
78static void usage(void);
f50c51ce 79int set_charset(struct iso_args *args, const char *cs_local, const char *cs_disk);
984263bc
MD
80
81int
82main(int argc, char **argv)
83{
84 struct iso_args args;
85 int ch, mntflags, opts;
86 char *dev, *dir, mntpath[MAXPATHLEN];
87 struct vfsconf vfc;
88 int error, verbose;
f50c51ce
AP
89 char *csp;
90 const char *quirk;
91 char *cs_local = NULL;
92 char *cs_disk = NULL;
984263bc
MD
93
94 mntflags = opts = verbose = 0;
95 memset(&args, 0, sizeof args);
96 args.ssector = -1;
f50c51ce 97 while ((ch = getopt(argc, argv, "begjo:rs:L:v")) != -1)
984263bc
MD
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;
f50c51ce
AP
111 case 'L':
112 if (setlocale(LC_CTYPE, optarg) == NULL)
113 err(EX_CONFIG, "%s", optarg);
114 csp = strchr(optarg,'.');
115 if (!csp)
116 err(EX_CONFIG, "%s", optarg);
117 quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT);
118 cs_local = strdup(quirk);
119 opts |= ISOFSMNT_KICONV;
120 break;
984263bc
MD
121 case 'o':
122 getmntopts(optarg, mopts, &mntflags, &opts);
123 break;
124 case 'r':
125 opts |= ISOFSMNT_NORRIP;
126 break;
127 case 's':
128 args.ssector = atoi(optarg);
129 break;
130 case 'v':
131 verbose++;
132 break;
133 case '?':
134 default:
135 usage();
136 }
137 argc -= optind;
138 argv += optind;
139
140 if (argc != 2)
141 usage();
142
143 dev = argv[0];
144 dir = argv[1];
145
146 /*
147 * Resolve the mountpoint with realpath(3) and remove unnecessary
148 * slashes from the devicename if there are any.
149 */
7cee7052
SW
150 checkpath(dir, mntpath);
151 rmslashes(dev, dev);
984263bc
MD
152
153#define DEFAULT_ROOTUID -2
154 /*
155 * ISO 9660 filesystems are not writeable.
156 */
157 mntflags |= MNT_RDONLY;
158 args.export.ex_flags = MNT_EXRDONLY;
159 args.fspec = dev;
160 args.export.ex_root = DEFAULT_ROOTUID;
161 args.flags = opts;
162
f50c51ce
AP
163 if (set_charset(&args, cs_local, cs_disk) == -1)
164 err(EX_OSERR, "msdos_iconv");
165
984263bc
MD
166 if (args.ssector == -1) {
167 /*
168 * The start of the session has not been specified on
169 * the command line. If we can successfully read the
170 * TOC of a CD-ROM, use the last data track we find.
171 * Otherwise, just use 0, in order to mount the very
172 * first session. This is compatible with the
173 * historic behaviour of mount_cd9660(8). If the user
174 * has specified -s <ssector> above, we don't get here
175 * and leave the user's will.
176 */
177 if ((args.ssector = get_ssector(dev)) == -1) {
178 if (verbose)
179 printf("could not determine starting sector, "
180 "using very first session\n");
181 args.ssector = 0;
182 } else if (verbose)
183 printf("using starting sector %d\n", args.ssector);
184 }
185
186 error = getvfsbyname("cd9660", &vfc);
187 if (error && vfsisloadable("cd9660")) {
188 if (vfsload("cd9660"))
189 err(EX_OSERR, "vfsload(cd9660)");
190 endvfsent(); /* flush cache */
191 error = getvfsbyname("cd9660", &vfc);
192 }
193 if (error)
194 errx(1, "cd9660 filesystem is not available");
195
196 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
197 err(1, "%s", args.fspec);
198 exit(0);
199}
200
f50c51ce
AP
201int
202set_charset(struct iso_args *args, const char *cs_local, const char *cs_disk)
203{
204 int error;
205 if (modfind("cd9660_iconv") < 0) {
206 if (kldload("cd9660_iconv") < 0 || modfind("cd9660_iconv") < 0)
207 {
208 warnx("cannot find or load \"cd9660_iconv\" kernel module");
209 return (-1);
210 }
211 }
212 snprintf(args->cs_local, ICONV_CSNMAXLEN, "%s", cs_local);
213 error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local);
214 if (error)
215 return (-1);
216 if (!cs_disk)
217 cs_disk = strdup(ENCODING_UNICODE);
218 snprintf(args->cs_disk, ICONV_CSNMAXLEN, "%s", cs_disk);
219 error = kiconv_add_xlat16_cspairs(cs_disk, cs_local);
220 if (error)
221 return (-1);
222 return (0);
223}
224
225
e7adb9bc 226static void
984263bc
MD
227usage(void)
228{
7cee7052
SW
229 fprintf(stderr,
230 "usage: mount_cd9660 [-egrv] [-o options] [-s startsector] special node\n");
984263bc
MD
231 exit(EX_USAGE);
232}
233
e7adb9bc 234static int
984263bc
MD
235get_ssector(const char *dev)
236{
237 struct ioc_toc_header h;
238 struct ioc_read_toc_entry t;
239 struct cd_toc_entry toc_buffer[100];
240 int fd, ntocentries, i;
241
242 if ((fd = open(dev, O_RDONLY)) == -1)
243 return -1;
244 if (ioctl(fd, CDIOREADTOCHEADER, &h) == -1) {
245 close(fd);
246 return -1;
247 }
248
249 ntocentries = h.ending_track - h.starting_track + 1;
250 if (ntocentries > 100) {
251 /* unreasonable, only 100 allowed */
252 close(fd);
253 return -1;
254 }
255 t.address_format = CD_LBA_FORMAT;
256 t.starting_track = 0;
257 t.data_len = ntocentries * sizeof(struct cd_toc_entry);
258 t.data = toc_buffer;
259
260 if (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t) == -1) {
261 close(fd);
262 return -1;
263 }
264 close(fd);
265
266 for (i = ntocentries - 1; i >= 0; i--)
267 if ((toc_buffer[i].control & 4) != 0)
268 /* found a data track */
269 break;
270 if (i < 0)
271 return -1;
272
273 return ntohl(toc_buffer[i].addr.lba);
274}