geom.c revision 1.2 1 1.2 martin /* $NetBSD: geom.c,v 1.2 2019/06/12 06:20:17 martin Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright (c) 1995, 1997 Jason R. Thorpe.
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * Redistribution and use in source and binary forms, with or without
8 1.1 dholland * modification, are permitted provided that the following conditions
9 1.1 dholland * are met:
10 1.1 dholland * 1. Redistributions of source code must retain the above copyright
11 1.1 dholland * notice, this list of conditions and the following disclaimer.
12 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 dholland * notice, this list of conditions and the following disclaimer in the
14 1.1 dholland * documentation and/or other materials provided with the distribution.
15 1.1 dholland * 3. All advertising materials mentioning features or use of this software
16 1.1 dholland * must display the following acknowledgement:
17 1.1 dholland * This product includes software developed for the NetBSD Project
18 1.1 dholland * by Jason R. Thorpe.
19 1.1 dholland * 4. The name of the author may not be used to endorse or promote products
20 1.1 dholland * derived from this software without specific prior written permission.
21 1.1 dholland *
22 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 dholland * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 dholland * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 dholland * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 dholland * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 1.1 dholland * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 1.1 dholland * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 1.1 dholland * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 1.1 dholland * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 dholland * SUCH DAMAGE.
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland /* Modified by Philip A. Nelson for use in sysinst. */
36 1.1 dholland
37 1.1 dholland #include <sys/param.h>
38 1.1 dholland #include <sys/ioctl.h>
39 1.1 dholland #include <fcntl.h>
40 1.1 dholland #include <unistd.h>
41 1.1 dholland #include <util.h>
42 1.1 dholland #include <errno.h>
43 1.2 martin #include "partutil.h"
44 1.1 dholland
45 1.1 dholland #include "defs.h"
46 1.1 dholland
47 1.1 dholland static int
48 1.1 dholland get_label(const char *disk, struct disklabel *l, unsigned long cmd)
49 1.1 dholland {
50 1.1 dholland char diskpath[MAXPATHLEN];
51 1.1 dholland int fd;
52 1.1 dholland int sv_errno;
53 1.1 dholland
54 1.1 dholland /* Open the disk. */
55 1.1 dholland fd = opendisk(disk, O_RDONLY, diskpath, sizeof(diskpath), 0);
56 1.1 dholland if (fd < 0)
57 1.1 dholland return 0;
58 1.1 dholland
59 1.1 dholland if (ioctl(fd, cmd, l) < 0) {
60 1.1 dholland sv_errno = errno;
61 1.1 dholland (void)close(fd);
62 1.1 dholland errno = sv_errno;
63 1.1 dholland return 0;
64 1.1 dholland }
65 1.1 dholland (void)close(fd);
66 1.1 dholland return 1;
67 1.1 dholland }
68 1.1 dholland
69 1.1 dholland int
70 1.2 martin get_disk_geom(const char *disk, struct disk_geom *d)
71 1.1 dholland {
72 1.2 martin char buf[MAXPATHLEN];
73 1.2 martin int fd, error;
74 1.2 martin
75 1.2 martin if ((fd = opendisk(disk, O_RDONLY, buf, sizeof(buf), 0)) == -1)
76 1.2 martin return 0;
77 1.2 martin
78 1.2 martin error = getdiskinfo(disk, fd, NULL, d, NULL);
79 1.2 martin close(fd);
80 1.2 martin if (error < 0) {
81 1.2 martin errno = error;
82 1.2 martin return 0;
83 1.2 martin }
84 1.2 martin return 1;
85 1.1 dholland }
86 1.1 dholland
87 1.1 dholland int
88 1.2 martin get_label_geom(const char *disk, struct disklabel *l)
89 1.1 dholland {
90 1.1 dholland
91 1.1 dholland return get_label(disk, l, DIOCGDINFO);
92 1.1 dholland }
93