xref: /netbsd-src/lib/libutil/opendisk.3 (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1.\"	$NetBSD: opendisk.3,v 1.13 2016/06/07 11:20:45 wiz Exp $
2.\"
3.\" Copyright (c) 1997, 2001 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Luke Mewburn.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd June 6, 2016
31.Dt OPENDISK 3
32.Os
33.Sh NAME
34.Nm opendisk
35.Nd open a disk partition
36.Sh LIBRARY
37.Lb libutil
38.Sh SYNOPSIS
39.In util.h
40.Ft int
41.Fo opendisk
42.Fa "const char *path"
43.Fa "int flags"
44.Fa "char *buf"
45.Fa "size_t buflen"
46.Fa "int iscooked"
47.Fc
48.Sh DESCRIPTION
49.Fn opendisk
50opens
51.Fa path ,
52for reading and/or writing as specified by the argument
53.Fa flags
54using
55.Xr open 2 ,
56and the file descriptor is returned to the caller.
57.Fa buf
58is used to store the resultant filename.
59.Fa buflen
60is the size, in bytes, of the array referenced by
61.Fa buf
62(usually
63.Dv MAXPATHLEN
64bytes).
65.Fa iscooked
66controls which paths in
67.Pa /dev
68are tried.
69.Pp
70.Fn opendisk
71attempts to open the following variations of
72.Fa path ,
73in order:
74.Pp
75If
76.Fa path
77does not contain a
78slash
79.Pq Dq / ,
80the following variations are attempted:
81.Pp
82.Bl -dash -compact
83.It
84If
85.Fa iscooked
86is zero:
87.Bl -tag -compact -width "/dev/rpathX"
88.It Pa /dev/rpath
89.Fa path
90with a prefix of
91.Dq Pa /dev/r .
92.It Pa /dev/rpath Ns Em X
93.Fa path
94with a prefix of
95.Dq Pa /dev/r
96and a suffix of
97.Sq Em X
98(q.v.).
99.El
100.It
101If
102.Fa iscooked
103is non-zero:
104.Bl -tag -compact -width "/dev/rpathX"
105.It Pa /dev/path
106.Fa path
107with a prefix of
108.Dq Pa /dev/ .
109.It Pa /dev/path Ns Em X
110.Fa path
111with a prefix of
112.Dq Pa /dev/
113and a suffix of
114.Sq Em X
115(q.v.).
116.El
117.El
118.Pp
119If the above fails, then the original
120.Fa path
121is tried using the following two variations:
122.Pp
123.Bl -dash -compact
124.It
125The
126.Fa iscooked
127value is ignored:
128.Bl -tag -compact -width "/dev/rpathX"
129.It Pa path
130The pathname as given.
131.It Pa path Ns Em X
132.Fa path
133with a suffix of
134.Sq Em X ,
135where
136.Sq Em X
137represents the raw partition of the device, as determined by
138.Xr getrawpartition 3 ,
139usually
140.Dq c .
141.El
142.El
143.Sh RETURN VALUES
144An open file descriptor, or -1 if the
145.Xr open 2
146failed.
147.Sh ERRORS
148.Fn opendisk
149may set
150.Va errno
151to one of the following values:
152.Bl -tag -width Er
153.It Bq Er EFAULT
154.Fa buf
155was the
156.Dv NULL
157pointer.
158.It Bq Er EINVAL
159.Dv O_CREAT
160was set in
161.Fa flags ,
162or
163.Xr getrawpartition 3
164didn't return a valid partition.
165.El
166.Pp
167The
168.Fn opendisk
169function
170may also set
171.Va errno
172to any value specified by the
173.Xr open 2
174function.
175.Sh SEE ALSO
176.Xr open 2 ,
177.Xr getrawpartition 3
178.Sh HISTORY
179The
180.Fn opendisk
181function first appeared in
182.Nx 1.3 .
183.Pp
184The lookup order of
185.Fn opendisk
186was changed in
187.Nx 8
188to first look in
189.Pa /dev
190in order to avoid opening random files in the current working directory.
191