summaryrefslogtreecommitdiff
path: root/pflocal/io.c
blob: 278f6e307677f5ceb5ed35e55fe5431effc241d5 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* Socket I/O operations

   Copyright (C) 1995 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include "pflocal.h"

/* Read data from an IO object.  If offset if -1, read from the object
   maintained file pointer.  If the object is not seekable, offset is
   ignored.  The amount desired to be read is in amount.  */
error_t
S_io_read (struct sock_user *user,
	   char **data, mach_msg_type_number_t *data_len,
	   off_t offset, mach_msg_type_number_t amount)
{
  error_t err = 0;
  unsigned readable;
  struct pipe *pipe;

  if (!user)
    return EOPNOTSUPP;

  pipe = sock_aquire_read_pipe (user->sock);
  if (pipe == NULL)
    return EBADF;
  
  while ((readable = pipe_readable (pipe)) == 0 && pipe->writer)
    {
      unsigned seq_num = pipe->interrupt_seq_num;
      condition_wait (&pipe->pending_reads, &pipe->lock);
      if (seq_num != pipe->interrupt_seq_num)
	{
	  pipe_release (pipe);
	  return EINTR;
	}
    }

  if (readable)
    err = pipe_read (pipe, data, data_len, amount);
  if (readable && !err)
    timestamp (&pipe->read_time);

  pipe_release (pipe);
  return err;
}

/* Cause a pending request on this object to immediately return.  The
   exact semantics are dependent on the specific object.  */
error_t
S_interrupt_operation (struct sock_user *user)
{
  struct pipe *pipe;

  if (!user)
    return EOPNOTSUPP;

  /* Interrupt pending reads on this socket.  We don't bother with writes
     since they never block.  */
  pipe = sock_aquire_read_pipe (user->sock);
  if (pipe != NULL)
    {
      /* Indicate to currently waiting threads they've been interrupted.  */
      pipe->interrupt_seq_num++;

      /* Now wake them all up for the bad news... */
      condition_broadcast (&pipe->pending_reads, &pipe->lock);
      mutex_lock (&pipe->lock);	/* Get back the lock on PIPE.  */
      condition_broadcast (&pipe->pending_selects, &pipe->lock);
      mutex_lock (&pipe->lock);	/* Get back the lock on PIPE.  */

      pipe_release (pipe);
    }

  return 0;
}

S_io_get_openmodes (struct sock_user *user, int *bits)
{
  if (!user)
    return EOPNOTSUPP;
  *bits =
    (user->sock->read_pipe ? O_READ : 0)
      | (user->sock->write_pipe ? O_WRITE : 0);
  return 0;
}

/* Tell how much data can be read from the object without blocking for
   a "long time" (this should be the same meaning of "long time" used
   by the nonblocking flag.  */
error_t 
S_io_readable (struct sock_user *user, mach_msg_type_number_t *amount)
{
  error_t err = 0;

  if (!user)
    return EOPNOTSUPP;

  mutex_lock (&user->sock->lock);
  if (user->sock->read_pipe)
    *amount = pipe_readable (user->sock->read_pipe);
  else
    err = EBADF;
  mutex_unlock (&user->sock->lock);

  return err;
}

/* Change current read/write offset */
error_t
S_io_seek (struct sock_user *user,
	   off_t offset, int whence, off_t *new_offset)
{
  return user ? ESPIPE : EOPNOTSUPP;
}

/* Return a new port with the same semantics as the existing port. */
error_t
S_io_duplicate (struct sock_user *user,
		mach_port_t *new_port, mach_msg_type_name_t *new_port_type)
{
  struct sock *sock;
  struct sock_user *new_user;

  if (!user)
    return EOPNOTSUPP;

  sock = user->sock;
  mutex_lock (&sock->lock);
  sock->refs++;
  mutex_unlock (&sock->lock);

  new_user =
    port_allocate_port (sock_user_bucket,
			sizeof (struct sock_user),
			sock_user_class);
  new_user->sock = sock;

  *new_port = ports_get_right (new_user);
  *new_port_type = MACH_MSG_TYPE_MAKE_SEND;
  return 0;
}

/* Write data to an IO object.  If offset is -1, write at the object
   maintained file pointer.  If the object is not seekable, offset is
   ignored.  The amount successfully written is returned in amount.  A
   given user should not have more than one outstanding io_write on an
   object at a time; servers implement congestion control by delaying
   responses to io_write.  Servers may drop data (returning ENOBUFS)
   if they recevie more than one write when not prepared for it.  */
error_t
S_io_write (struct sock_user *user,
	    char *data, mach_msg_type_number_t data_len,
	    off_t offset, mach_msg_type_number_t *amount)
{
  error_t err = 0;
  struct pipe *pipe;

  if (!user)
    return EOPNOTSUPP;

  pipe = sock_aquire_write_pipe (user->sock);
  if (pipe == NULL)
    return EBADF;
  
  if (pipe->reader == NULL)
    err = EPIPE;
  if (!err)
    err = pipe_write(pipe, data, data_len, amount);
  if (!err)
    {
      timestamp (&pipe->write_time);
      
      /* And wakeup anyone that might be interested in it.  */
      condition_signal (&pipe->pending_reads, &pipe->lock);
      mutex_lock (&pipe->lock);	/* Get back the lock on PIPE.  */
      
      /* Only wakeup selects if there's still data available.  */
      if (pipe_readable (pipe))
	{
	  condition_signal (&pipe->pending_selects, &pipe->lock);
	  mutex_lock (&pipe->lock); /* Get back the lock on PIPE.  */
	}
    }

  pipe_release (pipe);
  return 0;
}

/* SELECT_TYPE is the bitwise OR of SELECT_READ, SELECT_WRITE, and SELECT_URG.
   Block until one of the indicated types of i/o can be done "quickly", and
   return the types that are then available.  ID_TAG is returned as passed; it
   is just for the convenience of the user in matching up reply messages with
   specific requests sent.  */
error_t
S_io_select (struct sock_user *user, int *select_type, int *id_tag)
{
  struct sock *sock;

  if (!user)
    return EOPNOTSUPP;

  sock = user->sock;
  mutex_lock (&sock->lock);

  *select_type |= ~SELECT_URG;

  if ((*select_type & SELECT_WRITE) && !sock->write_pipe)
    {
      mutex_unlock (&sock->lock);
      return EBADF;
    }
  /* Otherwise, pipes are always writable... */

  if (*select_type & SELECT_READ)
    {
      struct pipe *pipe = sock->read_pipe;
      if (pipe)
	pipe_aquire (pipe);

      /* We unlock SOCK here, as it's not subsequently used, and we might
	 go to sleep waiting for readable data.  */
      mutex_unlock (&sock->lock);

      if (!pipe)
	return EBADF;

      if (! pipe_readable (pipe))
	/* Nothing to read on PIPE yet...  */
	if (*select_type & ~SELECT_READ)
	  /* But there's other stuff to report, so return that.  */
	  *select_type &= ~SELECT_READ;
	else
	  /* The user only cares about reading, so wait until something is
	     readable.  */
	  while (! pipe_readable (pipe) && pipe->writer)
	    {
	      unsigned seq_num = pipe->interrupt_seq_num;
	      condition_wait (&pipe->pending_reads, &pipe->lock);
	      if (seq_num != pipe->interrupt_seq_num)
		{
		  pipe_release (pipe);
		  return EINTR;
		}
	    }

      pipe_release (pipe);
    }
  else
    mutex_unlock (&sock->lock);

  return 0;
}

/* Return the current status of the object.  Not all the fields of the
   io_statuf_t are meaningful for all objects; however, the access and
   modify times, the optimal IO size, and the fs type are meaningful
   for all objects.  */
error_t
S_io_stat (struct sock_user *user, struct stat *st)
{
  struct sock *sock;
  void copy_time (time_value_t from, time_t *to_sec, unsigned long *to_usec)
    {
      *to_sec = from.seconds;
      *to_usec = from.microseconds;
    }

  if (!user)
    return EOPNOTSUPP;

  sock = user->sock;

  bzero (st, sizeof (struct stat));

  st->st_fstype = FSTYPE_SOCKET;
  st->st_fsid = getpid ();
  st->st_ino = sock->id;

  st->st_blksize = vm_page_size * 8;

  mutex_lock (&sock->lock);	/* Make sure the pipes don't go away...  */

  if (sock->read_pipe)
    copy_time (&sock->read_pipe->read_time, &st->st_atime, &st->atime_usec);
  if (sock->write_pipe)
    copy_time (&sock->read_pipe->write_time, &st->st_mtime, &st->mtime_usec);
  copy_time (&sock->change_time, &st->st_ctime, &st->ctime_usec);

  return 0;
}