xref: /titanic_50/usr/src/cmd/lofiadm/main.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * lofiadm - administer lofi(7d). Very simple, add and remove file<->device
27*7c478bd9Sstevel@tonic-gate  * associations, and display status. All the ioctls are private between
28*7c478bd9Sstevel@tonic-gate  * lofi and lofiadm, and so are very simple - device information is
29*7c478bd9Sstevel@tonic-gate  * communicated via a minor number.
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/lofi.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
38*7c478bd9Sstevel@tonic-gate #include <stdio.h>
39*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
40*7c478bd9Sstevel@tonic-gate #include <locale.h>
41*7c478bd9Sstevel@tonic-gate #include <string.h>
42*7c478bd9Sstevel@tonic-gate #include <errno.h>
43*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
44*7c478bd9Sstevel@tonic-gate #include <unistd.h>
45*7c478bd9Sstevel@tonic-gate #include <stropts.h>
46*7c478bd9Sstevel@tonic-gate #include "utils.h"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static const char USAGE[] =
49*7c478bd9Sstevel@tonic-gate 	"Usage: %s -a file [ device ]\n"
50*7c478bd9Sstevel@tonic-gate 	"       %s -d file | device \n"
51*7c478bd9Sstevel@tonic-gate 	"       %s [ device | file ]\n";
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static const char *pname;
54*7c478bd9Sstevel@tonic-gate static int	addflag = 0;
55*7c478bd9Sstevel@tonic-gate static int	deleteflag = 0;
56*7c478bd9Sstevel@tonic-gate static int	errflag = 0;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #define	FORMAT "%-20s     %s\n"
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate /*
61*7c478bd9Sstevel@tonic-gate  * Print the list of all the mappings. Including a header.
62*7c478bd9Sstevel@tonic-gate  */
63*7c478bd9Sstevel@tonic-gate static void
64*7c478bd9Sstevel@tonic-gate print_mappings(int fd)
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	struct lofi_ioctl li;
67*7c478bd9Sstevel@tonic-gate 	int	minor;
68*7c478bd9Sstevel@tonic-gate 	int	maxminor;
69*7c478bd9Sstevel@tonic-gate 	char	path[MAXPATHLEN + 1];
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	li.li_minor = 0;
72*7c478bd9Sstevel@tonic-gate 	if (ioctl(fd, LOFI_GET_MAXMINOR, &li) == -1) {
73*7c478bd9Sstevel@tonic-gate 		perror("ioctl");
74*7c478bd9Sstevel@tonic-gate 		exit(E_ERROR);
75*7c478bd9Sstevel@tonic-gate 	}
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	maxminor = li.li_minor;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	(void) printf(FORMAT, "Block Device", "File");
80*7c478bd9Sstevel@tonic-gate 	for (minor = 1; minor <= maxminor; minor++) {
81*7c478bd9Sstevel@tonic-gate 		li.li_minor = minor;
82*7c478bd9Sstevel@tonic-gate 		if (ioctl(fd, LOFI_GET_FILENAME, &li) == -1) {
83*7c478bd9Sstevel@tonic-gate 			if (errno == ENXIO)
84*7c478bd9Sstevel@tonic-gate 				continue;
85*7c478bd9Sstevel@tonic-gate 			perror("ioctl");
86*7c478bd9Sstevel@tonic-gate 			break;
87*7c478bd9Sstevel@tonic-gate 		}
88*7c478bd9Sstevel@tonic-gate 		(void) snprintf(path, sizeof (path), "/dev/%s/%d",
89*7c478bd9Sstevel@tonic-gate 		    LOFI_BLOCK_NAME, minor);
90*7c478bd9Sstevel@tonic-gate 		(void) printf(FORMAT, path, li.li_filename);
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate static void
95*7c478bd9Sstevel@tonic-gate usage(void)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(USAGE), pname, pname, pname);
98*7c478bd9Sstevel@tonic-gate 	exit(E_USAGE);
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate /*
102*7c478bd9Sstevel@tonic-gate  * Translate a lofi device name to a minor number. We might be asked
103*7c478bd9Sstevel@tonic-gate  * to do this when there is no association (such as when the user specifies
104*7c478bd9Sstevel@tonic-gate  * a particular device), so we can only look at the string.
105*7c478bd9Sstevel@tonic-gate  */
106*7c478bd9Sstevel@tonic-gate static int
107*7c478bd9Sstevel@tonic-gate name_to_minor(const char *devicename)
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate 	int	minor;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	if (sscanf(devicename, "/dev/" LOFI_BLOCK_NAME "/%d", &minor) == 1) {
112*7c478bd9Sstevel@tonic-gate 		return (minor);
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate 	if (sscanf(devicename, "/dev/" LOFI_CHAR_NAME "/%d", &minor) == 1) {
115*7c478bd9Sstevel@tonic-gate 		return (minor);
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 	return (0);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /*
121*7c478bd9Sstevel@tonic-gate  * This might be the first time we've used this minor number. If so,
122*7c478bd9Sstevel@tonic-gate  * it might also be that the /dev links are in the process of being created
123*7c478bd9Sstevel@tonic-gate  * by devfsadmd (or that they'll be created "soon"). We cannot return
124*7c478bd9Sstevel@tonic-gate  * until they're there or the invoker of lofiadm might try to use them
125*7c478bd9Sstevel@tonic-gate  * and not find them. This can happen if a shell script is running on
126*7c478bd9Sstevel@tonic-gate  * an MP.
127*7c478bd9Sstevel@tonic-gate  */
128*7c478bd9Sstevel@tonic-gate static int sleeptime = 2;	/* number of seconds to sleep between stat's */
129*7c478bd9Sstevel@tonic-gate static int maxsleep = 120;	/* maximum number of seconds to sleep */
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate static void
132*7c478bd9Sstevel@tonic-gate wait_until_dev_complete(int minor)
133*7c478bd9Sstevel@tonic-gate {
134*7c478bd9Sstevel@tonic-gate 	struct stat64 buf;
135*7c478bd9Sstevel@tonic-gate 	int	cursleep;
136*7c478bd9Sstevel@tonic-gate 	char	blkpath[MAXPATHLEN + 1];
137*7c478bd9Sstevel@tonic-gate 	char	charpath[MAXPATHLEN + 1];
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	(void) snprintf(blkpath, sizeof (blkpath), "/dev/%s/%d",
140*7c478bd9Sstevel@tonic-gate 	    LOFI_BLOCK_NAME, minor);
141*7c478bd9Sstevel@tonic-gate 	(void) snprintf(charpath, sizeof (charpath), "/dev/%s/%d",
142*7c478bd9Sstevel@tonic-gate 	    LOFI_CHAR_NAME, minor);
143*7c478bd9Sstevel@tonic-gate 	cursleep = 0;
144*7c478bd9Sstevel@tonic-gate 	while (cursleep < maxsleep) {
145*7c478bd9Sstevel@tonic-gate 		if ((stat64(blkpath, &buf) == -1) ||
146*7c478bd9Sstevel@tonic-gate 		    (stat64(charpath, &buf) == -1)) {
147*7c478bd9Sstevel@tonic-gate 			(void) sleep(sleeptime);
148*7c478bd9Sstevel@tonic-gate 			cursleep += sleeptime;
149*7c478bd9Sstevel@tonic-gate 			continue;
150*7c478bd9Sstevel@tonic-gate 		}
151*7c478bd9Sstevel@tonic-gate 		return;
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 	/* one last try */
154*7c478bd9Sstevel@tonic-gate 	if (stat64(blkpath, &buf) == -1) {
155*7c478bd9Sstevel@tonic-gate 		die(gettext("%s was not created"), blkpath);
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 	if (stat64(charpath, &buf) == -1) {
158*7c478bd9Sstevel@tonic-gate 		die(gettext("%s was not created"), charpath);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate /*
163*7c478bd9Sstevel@tonic-gate  * Add a device association. If devicename is NULL, let the driver
164*7c478bd9Sstevel@tonic-gate  * pick a device.
165*7c478bd9Sstevel@tonic-gate  */
166*7c478bd9Sstevel@tonic-gate static void
167*7c478bd9Sstevel@tonic-gate add_mapping(int lfd, const char *devicename, const char *filename)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate 	struct lofi_ioctl li;
170*7c478bd9Sstevel@tonic-gate 	int	minor;
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	if (devicename == NULL) {
173*7c478bd9Sstevel@tonic-gate 		/* pick one */
174*7c478bd9Sstevel@tonic-gate 		li.li_minor = 0;
175*7c478bd9Sstevel@tonic-gate 		(void) strcpy(li.li_filename, filename);
176*7c478bd9Sstevel@tonic-gate 		minor = ioctl(lfd, LOFI_MAP_FILE, &li);
177*7c478bd9Sstevel@tonic-gate 		if (minor == -1) {
178*7c478bd9Sstevel@tonic-gate 			die(gettext("could not map file %s"), filename);
179*7c478bd9Sstevel@tonic-gate 		}
180*7c478bd9Sstevel@tonic-gate 		wait_until_dev_complete(minor);
181*7c478bd9Sstevel@tonic-gate 		/* print one picked */
182*7c478bd9Sstevel@tonic-gate 		(void) printf("/dev/%s/%d\n", LOFI_BLOCK_NAME, minor);
183*7c478bd9Sstevel@tonic-gate 		return;
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 	/* use device we were given */
186*7c478bd9Sstevel@tonic-gate 	minor = name_to_minor(devicename);
187*7c478bd9Sstevel@tonic-gate 	if (minor == 0) {
188*7c478bd9Sstevel@tonic-gate 		die(gettext("malformed device name %s\n"), devicename);
189*7c478bd9Sstevel@tonic-gate 	}
190*7c478bd9Sstevel@tonic-gate 	(void) strcpy(li.li_filename, filename);
191*7c478bd9Sstevel@tonic-gate 	li.li_minor = minor;
192*7c478bd9Sstevel@tonic-gate 	if (ioctl(lfd, LOFI_MAP_FILE_MINOR, &li) == -1) {
193*7c478bd9Sstevel@tonic-gate 		die(gettext("could not map file %s to %s"), filename,
194*7c478bd9Sstevel@tonic-gate 		    devicename);
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 	wait_until_dev_complete(minor);
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate /*
200*7c478bd9Sstevel@tonic-gate  * Remove an association. Delete by device name if non-NULL, or by
201*7c478bd9Sstevel@tonic-gate  * filename otherwise.
202*7c478bd9Sstevel@tonic-gate  */
203*7c478bd9Sstevel@tonic-gate static void
204*7c478bd9Sstevel@tonic-gate delete_mapping(int lfd, const char *devicename, const char *filename)
205*7c478bd9Sstevel@tonic-gate {
206*7c478bd9Sstevel@tonic-gate 	struct lofi_ioctl li;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	if (devicename == NULL) {
209*7c478bd9Sstevel@tonic-gate 		/* delete by filename */
210*7c478bd9Sstevel@tonic-gate 		(void) strcpy(li.li_filename, filename);
211*7c478bd9Sstevel@tonic-gate 		li.li_minor = 0;
212*7c478bd9Sstevel@tonic-gate 		if (ioctl(lfd, LOFI_UNMAP_FILE, &li) == -1) {
213*7c478bd9Sstevel@tonic-gate 			die(gettext("could not unmap file %s"), filename);
214*7c478bd9Sstevel@tonic-gate 		}
215*7c478bd9Sstevel@tonic-gate 		return;
216*7c478bd9Sstevel@tonic-gate 	}
217*7c478bd9Sstevel@tonic-gate 	/* delete by device */
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	li.li_minor = name_to_minor(devicename);
220*7c478bd9Sstevel@tonic-gate 	if (li.li_minor == 0) {
221*7c478bd9Sstevel@tonic-gate 		die(gettext("malformed device name %s\n"), devicename);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 	if (ioctl(lfd, LOFI_UNMAP_FILE_MINOR, &li) == -1) {
224*7c478bd9Sstevel@tonic-gate 		die(gettext("could not unmap device %s"), devicename);
225*7c478bd9Sstevel@tonic-gate 	}
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate static void
229*7c478bd9Sstevel@tonic-gate print_one_mapping(int lfd, const char *devicename, const char *filename)
230*7c478bd9Sstevel@tonic-gate {
231*7c478bd9Sstevel@tonic-gate 	struct lofi_ioctl li;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	if (devicename == NULL) {
234*7c478bd9Sstevel@tonic-gate 		/* given filename, print devicename */
235*7c478bd9Sstevel@tonic-gate 		li.li_minor = 0;
236*7c478bd9Sstevel@tonic-gate 		(void) strcpy(li.li_filename, filename);
237*7c478bd9Sstevel@tonic-gate 		if (ioctl(lfd, LOFI_GET_MINOR, &li) == -1) {
238*7c478bd9Sstevel@tonic-gate 			die(gettext("could not find device for %s"), filename);
239*7c478bd9Sstevel@tonic-gate 		}
240*7c478bd9Sstevel@tonic-gate 		(void) printf("/dev/%s/%d\n", LOFI_BLOCK_NAME, li.li_minor);
241*7c478bd9Sstevel@tonic-gate 		return;
242*7c478bd9Sstevel@tonic-gate 	}
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	/* given devicename, print filename */
245*7c478bd9Sstevel@tonic-gate 	li.li_minor = name_to_minor(devicename);
246*7c478bd9Sstevel@tonic-gate 	if (li.li_minor == 0) {
247*7c478bd9Sstevel@tonic-gate 		die(gettext("malformed device name %s\n"), devicename);
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate 	if (ioctl(lfd, LOFI_GET_FILENAME, &li) == -1) {
250*7c478bd9Sstevel@tonic-gate 		die(gettext("could not find filename for %s"), devicename);
251*7c478bd9Sstevel@tonic-gate 	}
252*7c478bd9Sstevel@tonic-gate 	(void) printf("%s\n", li.li_filename);
253*7c478bd9Sstevel@tonic-gate }
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate int
256*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
257*7c478bd9Sstevel@tonic-gate {
258*7c478bd9Sstevel@tonic-gate 	int	lfd;
259*7c478bd9Sstevel@tonic-gate 	int	c;
260*7c478bd9Sstevel@tonic-gate 	int	error;
261*7c478bd9Sstevel@tonic-gate 	struct stat64 buf;
262*7c478bd9Sstevel@tonic-gate 	const char *devicename = NULL;
263*7c478bd9Sstevel@tonic-gate 	const char *filename = NULL;
264*7c478bd9Sstevel@tonic-gate 	int	openflag;
265*7c478bd9Sstevel@tonic-gate 	int	minor;
266*7c478bd9Sstevel@tonic-gate 	int	fd = -1;
267*7c478bd9Sstevel@tonic-gate 	static char *lofictl = "/dev/" LOFI_CTL_NAME;
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	pname = getpname(argv[0]);
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
272*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "a:d:")) != EOF) {
275*7c478bd9Sstevel@tonic-gate 		switch (c) {
276*7c478bd9Sstevel@tonic-gate 		case 'a':
277*7c478bd9Sstevel@tonic-gate 			addflag = 1;
278*7c478bd9Sstevel@tonic-gate 			filename = optarg;
279*7c478bd9Sstevel@tonic-gate 			fd = open64(filename, O_RDONLY);
280*7c478bd9Sstevel@tonic-gate 			if (fd == -1) {
281*7c478bd9Sstevel@tonic-gate 				die(gettext("open: %s"), filename);
282*7c478bd9Sstevel@tonic-gate 			}
283*7c478bd9Sstevel@tonic-gate 			error = fstat64(fd, &buf);
284*7c478bd9Sstevel@tonic-gate 			if (error == -1) {
285*7c478bd9Sstevel@tonic-gate 				die(gettext("fstat: %s"), filename);
286*7c478bd9Sstevel@tonic-gate 			} else if (!S_ISLOFIABLE(buf.st_mode)) {
287*7c478bd9Sstevel@tonic-gate 				die(gettext("%s is not a regular file, "
288*7c478bd9Sstevel@tonic-gate 				    "block, or character device\n"),
289*7c478bd9Sstevel@tonic-gate 				    filename);
290*7c478bd9Sstevel@tonic-gate 			} else if ((buf.st_size % DEV_BSIZE) != 0) {
291*7c478bd9Sstevel@tonic-gate 				die(gettext("size of %s is not a multiple "
292*7c478bd9Sstevel@tonic-gate 				    "of %d\n"),
293*7c478bd9Sstevel@tonic-gate 				    filename, DEV_BSIZE);
294*7c478bd9Sstevel@tonic-gate 			}
295*7c478bd9Sstevel@tonic-gate 			(void) close(fd);
296*7c478bd9Sstevel@tonic-gate 			minor = name_to_minor(filename);
297*7c478bd9Sstevel@tonic-gate 			if (minor != 0) {
298*7c478bd9Sstevel@tonic-gate 				die(gettext("cannot use " LOFI_DRIVER_NAME
299*7c478bd9Sstevel@tonic-gate 				    " on itself\n"), devicename);
300*7c478bd9Sstevel@tonic-gate 			}
301*7c478bd9Sstevel@tonic-gate 			if (((argc - optind) > 0) && (*argv[optind] != '-')) {
302*7c478bd9Sstevel@tonic-gate 				/* optional device */
303*7c478bd9Sstevel@tonic-gate 				devicename = argv[optind];
304*7c478bd9Sstevel@tonic-gate 				optind++;
305*7c478bd9Sstevel@tonic-gate 			}
306*7c478bd9Sstevel@tonic-gate 			break;
307*7c478bd9Sstevel@tonic-gate 		case 'd':
308*7c478bd9Sstevel@tonic-gate 			deleteflag = 1;
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 			minor = name_to_minor(optarg);
311*7c478bd9Sstevel@tonic-gate 			if (minor != 0)
312*7c478bd9Sstevel@tonic-gate 				devicename = optarg;
313*7c478bd9Sstevel@tonic-gate 			else
314*7c478bd9Sstevel@tonic-gate 				filename = optarg;
315*7c478bd9Sstevel@tonic-gate 			break;
316*7c478bd9Sstevel@tonic-gate 		case '?':
317*7c478bd9Sstevel@tonic-gate 		default:
318*7c478bd9Sstevel@tonic-gate 			errflag = 1;
319*7c478bd9Sstevel@tonic-gate 			break;
320*7c478bd9Sstevel@tonic-gate 		}
321*7c478bd9Sstevel@tonic-gate 	}
322*7c478bd9Sstevel@tonic-gate 	if (errflag || (addflag && deleteflag))
323*7c478bd9Sstevel@tonic-gate 		usage();
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	switch (argc - optind) {
326*7c478bd9Sstevel@tonic-gate 	case 0: /* no more args */
327*7c478bd9Sstevel@tonic-gate 		break;
328*7c478bd9Sstevel@tonic-gate 	case 1: /* one arg without options means print the association */
329*7c478bd9Sstevel@tonic-gate 		if (addflag || deleteflag)
330*7c478bd9Sstevel@tonic-gate 			usage();
331*7c478bd9Sstevel@tonic-gate 		minor = name_to_minor(argv[optind]);
332*7c478bd9Sstevel@tonic-gate 		if (minor != 0)
333*7c478bd9Sstevel@tonic-gate 			devicename = argv[optind];
334*7c478bd9Sstevel@tonic-gate 		else
335*7c478bd9Sstevel@tonic-gate 			filename = argv[optind];
336*7c478bd9Sstevel@tonic-gate 		break;
337*7c478bd9Sstevel@tonic-gate 	default:
338*7c478bd9Sstevel@tonic-gate 		usage();
339*7c478bd9Sstevel@tonic-gate 		break;
340*7c478bd9Sstevel@tonic-gate 	}
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	if (filename && !valid_abspath(filename))
343*7c478bd9Sstevel@tonic-gate 		exit(E_ERROR);
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate 	/*
346*7c478bd9Sstevel@tonic-gate 	 * Here, we know the arguments are correct, the filename is an
347*7c478bd9Sstevel@tonic-gate 	 * absolute path, it exists and is a regular file. We don't yet
348*7c478bd9Sstevel@tonic-gate 	 * know that the device name is ok or not.
349*7c478bd9Sstevel@tonic-gate 	 */
350*7c478bd9Sstevel@tonic-gate 	/*
351*7c478bd9Sstevel@tonic-gate 	 * Now to the real work.
352*7c478bd9Sstevel@tonic-gate 	 */
353*7c478bd9Sstevel@tonic-gate 	openflag = O_EXCL;
354*7c478bd9Sstevel@tonic-gate 	if (addflag || deleteflag)
355*7c478bd9Sstevel@tonic-gate 		openflag |= O_RDWR;
356*7c478bd9Sstevel@tonic-gate 	else
357*7c478bd9Sstevel@tonic-gate 		openflag |= O_RDONLY;
358*7c478bd9Sstevel@tonic-gate 	lfd = open(lofictl, openflag);
359*7c478bd9Sstevel@tonic-gate 	if (lfd == -1) {
360*7c478bd9Sstevel@tonic-gate 		if ((errno == EPERM) || (errno == EACCES)) {
361*7c478bd9Sstevel@tonic-gate 			die("you do not have permission to perform "
362*7c478bd9Sstevel@tonic-gate 			    "that operation.\n");
363*7c478bd9Sstevel@tonic-gate 		} else {
364*7c478bd9Sstevel@tonic-gate 			die("%s", lofictl);
365*7c478bd9Sstevel@tonic-gate 		}
366*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
367*7c478bd9Sstevel@tonic-gate 	}
368*7c478bd9Sstevel@tonic-gate 	if (addflag)
369*7c478bd9Sstevel@tonic-gate 		add_mapping(lfd, devicename, filename);
370*7c478bd9Sstevel@tonic-gate 	else if (deleteflag)
371*7c478bd9Sstevel@tonic-gate 		delete_mapping(lfd, devicename, filename);
372*7c478bd9Sstevel@tonic-gate 	else if (filename || devicename)
373*7c478bd9Sstevel@tonic-gate 		print_one_mapping(lfd, devicename, filename);
374*7c478bd9Sstevel@tonic-gate 	else
375*7c478bd9Sstevel@tonic-gate 		print_mappings(lfd);
376*7c478bd9Sstevel@tonic-gate 	(void) close(lfd);
377*7c478bd9Sstevel@tonic-gate 	return (E_SUCCESS);
378*7c478bd9Sstevel@tonic-gate }
379