xref: /openbsd/sbin/mount_cd9660/mount_cd9660.c (revision d7259957)
1 /*	$OpenBSD: mount_cd9660.c,v 1.23 2022/12/04 23:50:46 cheloha Exp $	*/
2 /*	$NetBSD: mount_cd9660.c,v 1.3 1996/04/13 01:31:08 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1992, 1993, 1994
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley
9  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
10  * Support code is derived from software contributed to Berkeley
11  * by Atsushi Murai (amurai@spec.co.jp).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/types.h>
39 #define CD9660
40 #include <sys/mount.h>
41 
42 #include <err.h>
43 #include <errno.h>
44 #include <stdint.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <limits.h>
50 
51 #include "mntopts.h"
52 
53 const struct mntopt mopts[] = {
54 	MOPT_STDOPTS,
55 	MOPT_UPDATE,
56 	{ NULL }
57 };
58 
59 void	usage(void);
60 
61 int
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64 	struct iso_args args;
65 	int ch, mntflags, opts, sess = 0;
66 	char *dev, dir[PATH_MAX];
67 	const char *errstr;
68 
69 	mntflags = opts = 0;
70 	while ((ch = getopt(argc, argv, "egjo:Rs:")) != -1)
71 		switch (ch) {
72 		case 'e':
73 			opts |= ISOFSMNT_EXTATT;
74 			break;
75 		case 'g':
76 			opts |= ISOFSMNT_GENS;
77 			break;
78 		case 'j':
79 			opts |= ISOFSMNT_NOJOLIET;
80 			break;
81 		case 'o':
82 			getmntopts(optarg, mopts, &mntflags);
83 			break;
84 		case 'R':
85 			opts |= ISOFSMNT_NORRIP;
86 			break;
87 		case 's':
88 			opts |= ISOFSMNT_SESS;
89 			sess = strtonum(optarg, 0, INT32_MAX, &errstr);
90 			if (errstr)
91 				errx(1, "session number is %s: %s", errstr,
92 				    optarg);
93 			break;
94 		default:
95 			usage();
96 		}
97 	argc -= optind;
98 	argv += optind;
99 
100 	if (argc != 2)
101 		usage();
102 
103 	dev = argv[0];
104 	if (realpath(argv[1], dir) == NULL)
105 		err(1, "realpath %s", argv[1]);
106 
107 #define DEFAULT_ROOTUID	-2
108 	args.fspec = dev;
109 	args.export_info.ex_root = DEFAULT_ROOTUID;
110 
111 	mntflags |= MNT_RDONLY;
112 	if (mntflags & MNT_RDONLY)
113 		args.export_info.ex_flags = MNT_EXRDONLY;
114 	else
115 		args.export_info.ex_flags = 0;
116 	args.flags = opts;
117 	args.sess = sess;
118 
119 	if (mount(MOUNT_CD9660, dir, mntflags, &args) == -1) {
120 		if (errno == EOPNOTSUPP)
121 			errx(1, "%s: Filesystem not supported by kernel", dir);
122 		else
123 			err(1, "%s on %s", args.fspec, dir);
124 	}
125 	exit(0);
126 }
127 
128 void
usage(void)129 usage(void)
130 {
131 	(void)fprintf(stderr,
132 		"usage: mount_cd9660 [-egjR] [-o options] [-s offset] "
133 		"special node\n");
134 	exit(1);
135 }
136