summaryrefslogtreecommitdiff
path: root/static/unix-v10/man4/pex.4
blob: 5b87e11fa60dc4a0fe376c4cff60d437ebe84460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
.TH PEX 4
.SH NAME
pex \- ioctl requests for process-exclusive access
.SH SYNOPSIS
.nf
.B #include <sys/pex.h>
.PP
.B ioctl(fildes, FIOPX, p)
.B struct pexclude *p;
.PP
.B ioctl(fildes, FIONPX, p)
.B struct pexclude *p;
.PP
.B ioctl(fildes, FIOQX, p)
.B struct pexclude *p;
.PP
.B ioctl(fildes, FIOAPX, p)
.B struct pexclude *p;
.PP
.B ioctl(fildes, FIOANPX, p)
.B struct pexclude *p;
.ig
.PP
.B ioctl(fildes, FIONBUF, np)
.B int *np;
..
.fi
.SH DESCRIPTION
These 
.IR ioctl (2)
requests provide and check temporary exclusive access to
an input/output source.
.B FIOPX
marks as `pexed' the file or
pipe end referred to by
.I fildes.
On a pexed file 
.I read,
.IR write (2),
and most forms of
.IR ioctl 
work only in the pexing process.
Moreover, these operations do not work in any process
on a half-pexed pipe (a pipe with exactly one pexed end).
The mark remains until the pexing process requests
.B FIONPX
or closes all file descriptors that
refer to the file.
.PP
When
.I fildes
refers to a stream,
.B FIOPX
and
.B FIONPX
require the stream's input and output queues to be empty;
.IR pex (3)
gives a method for emptying them.
When
.I fildes
refers to a pipe, the far end of which is unpexed,
.B FIOPX
waits, with timeout, for an answering
.B FIOPX
or
.B FIONPX
at the far end.
.B FIONPX
waits similarly when the far end is pexed.
Either request returns 1 when it leaves a pipe with exactly one end 
pexed.
A pipe must cycle through the fully unpexed state
between fully pexed states;
from the time one end becomes unpexed until the far end does too, 
.B FIOPX
on the unpexed end will return error
.BR ECONC  .
.PP
If argument
.I p
is nonzero, the structure it points to is filled in 
with information about the pexedness of the file and
about the process at the far end of a pexed pipe.
The format, defined in
.BR <sys/filio.h> 
is:
.EX
struct pexclude {
	int oldnear;	/* FIOPX or FIONPX: state at beginning of call */
	int newnear;	/* FIOPX or FIONPX: state at end of call */
	int farpid;	/* -1 if not pipe, 0 if not pexed, else process id */
	int farcap;	/* if farpid>0, capabilities */
	int faruid;	/* if farpid>0, user id */
};
.EE
Capabilities are represented as in the 
.B lb_t
field of a label; see
.IR getflab (2).
.PP
.B FIOQX
obtains the information without affecting state.
.PP
.I Read, write,
or
.I ioctl 
calls that fail due to pexedness return error
.BR ECONC .
The only
.I ioctl
requests that may succeed on a half-pexed pipe are
.BR FIOCLEX  , 
.BR FIONCLEX  ,
.BR FIOPX , 
.BR FIONPX  ,
and 
.BR FIOQX   .
A half-pexed pipe is deemed ready by
.IR select (2).
.PP
.B FIOANPX
and
.BR FIOAPX 
modify the response of open stream device files to
.B FIOPX
requests.
They require
.B T_EXTERN
capability; see
.IR getplab (2).
After 
.B FIOANPX
all
.B FIOPX 
requests on the special file return 1 and leave the device in an
unusable state
(as if the device driver were a process at the far end of a pipe,
always responding
.BR FIONPX ).
The treatment is reversed with
.BR FIOAPX .
This mechanism
allows a terminal to be denounced to the kernel
as being
attached to an untrusted remote computer
that cannot guarantee the exclusivity asked by
.BR FIOPX .
.ig
.PP
The request
.B FIONBUF
stores, in the integer pointed to by
.I np,
the number of bytes of data buffers currently in a stream.
This number may exceed the number of bytes of data in
the stream, but a stream will not contain any empty buffers.
The request may be used to tell whether a stream is empty before
executing one of the process-exclusive controls, which
destroy stream contents.
..
.SH EXAMPLES
A program collecting a password wishes to exclude other
programs from the dialogue.
The following code does the trick.
(When the dialogue passes through
.IR mux (9.1)
or
.IR con (1),
downstream stages of the path to the terminal
can be assumed to be similarly pexed, provided
.B FIOPX
succeeds.)
.IP
.EX
#define ok(p) (p->farpid==-1 || p->farpid>0 && p->farcap!=0)
struct pexclude x;
if(ioctl(fd, FIOPX, &x) == 0 && ok(&x)) {
	static char buf[9];
	write(fd, promptstr, strlen(promptstr));
	read(fd, buf, 8);
	s = buf;
} else 
	s = 0;
ioctl(fd, x.oldnear, 0);	/* restore state */
.EE
.LP
An intervening trusted program, with a policy of
recognizing exclusive access only for trusted processes,
may cooperate with
.IP
.EX
n = read(fd, buf, BUFSIZE);
if(n == -1 && errno == ECONC) {
	if(ioctl(fd, FIOPX, &pexcode)!=0 || pexcode.farcap==0)
		ioctl(fd, FIONPX, 0);
	} else  /* improper pexing */
.EE
.SH SEE ALSO
.IR ioctl (2),
.IR pipe (2),
.IR stream (4),
.IR pex (3)
.SH DIAGNOSTICS
.BR EBADF ,
.BR ECONC ,
.BR EFAULT ,
.BR EIO ,
.BR ENOTTY
.RB ( FIOAPX
and
.BR FIOANPX )
.br
.B ECONC
for forbidden IO calls in other processes.
.br
.B EBUSY
for an undrained queue.