io.c 14.8 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
/* io.c
 *
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 *
 * $Id$ */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * 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 of the
 * License, 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.
Niels Möller's avatar
Niels Möller committed
24
25
 */

Niels Möller's avatar
Niels Möller committed
26
27
28
#include <assert.h>
#include <string.h>

Niels Möller's avatar
Niels Möller committed
29
#include <unistd.h>
30
31

#ifdef HAVE_POLL
Niels Möller's avatar
Niels Möller committed
32
#include <poll.h>
33
34
35
36
#else
#include "poll.h"
#endif

Niels Möller's avatar
Niels Möller committed
37
38
39
40
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
Niels Möller's avatar
Niels Möller committed
41
#include <arpa/inet.h>
42
#include <signal.h>
Niels Möller's avatar
Niels Möller committed
43

Niels Möller's avatar
Niels Möller committed
44
45
46
47
48
#include "io.h"
#include "werror.h"
#include "write_buffer.h"
#include "xalloc.h"

Niels Möller's avatar
Niels Möller committed
49
50
51
52
53
/* If there's nothing to do for this amount of time (ms), do
 * spontaneous gc. */

#define IDLE_TIME 100

Niels Möller's avatar
Niels Möller committed
54
55
56
57
58
int io_iter(struct io_backend *b)
{
  unsigned long nfds; /* FIXME: Should be nfds_t if that type is defined */
  struct pollfd *fds;

Niels Möller's avatar
Niels Möller committed
59
60
  /* FIXME: Callouts not implemented */
  /* int timeout; */
Niels Möller's avatar
Niels Möller committed
61
62
63
64
65
66
67
68
69
  int res;

  nfds = 0;
  
  {
    /* Prepare fd:s. This fase calls the prepare-methods, also closes
     * and unlinks any fd:s that should be closed, and also counts how
     * many fd:s there are. */
    
Niels Möller's avatar
Niels Möller committed
70
    struct lsh_fd **fd_p;
Niels Möller's avatar
Niels Möller committed
71
72
    struct lsh_fd *fd;
    
Niels Möller's avatar
Niels Möller committed
73
    for(fd_p = &b->files; (fd = *fd_p); )
Niels Möller's avatar
Niels Möller committed
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
      {
	if (!fd->close_now && fd->prepare)
	  PREPARE_FD(fd);
	
	if (fd->close_now)
	  {
	    if (fd->fd < 0)
	      /* Unlink the file object, but don't close any underlying file. */
	      ;
	    else
	      {
		/* Used by write fd:s to make sure that writing to its
		 * buffer fails. */
		if (fd->really_close)
		  REALLY_CLOSE_FD(fd);
		
		/* FIXME: The value returned from the close callback could be used
		 * to choose an exit code. */
		if (fd->close_callback && fd->close_reason)
		  (void) CLOSE_CALLBACK(fd->close_callback, fd->close_reason);
		
		debug("Closing fd %d.\n", fd->fd);
		
		close(fd->fd);
	      }
	    /* Unlink this fd */
Niels Möller's avatar
Niels Möller committed
100
	    *fd_p = fd->next;
Niels Möller's avatar
Niels Möller committed
101
102
103
	    continue;
	  }
	nfds++;
Niels Möller's avatar
Niels Möller committed
104
	fd_p = &fd->next;
Niels Möller's avatar
Niels Möller committed
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
      }
	
  }

  if (!nfds)
    /* Nothing more to do.
     *
     * NOTE: There might be some callouts left, but we won't wait for them. */
    return 0;
  
  fds = alloca(sizeof(struct pollfd) * nfds);

  /* Fill out fds-array */
  {
    struct lsh_fd *fd;
    int i;
    int all_events = 0;
    
    for (fd = b->files, i = 0; fd; fd = fd->next, i++)
      {
	assert(i < nfds);

	fds[i].fd = fd->fd;
	fds[i].events = 0;

	if (fd->want_read)
	  fds[i].events |= POLLIN;

	if (fd->want_write)
	  fds[i].events |= POLLOUT;

	all_events |= fds[i].events;
      }
    assert(i == nfds);

    if (!all_events)
      {
	/* Nothing happens */
	/* NOTE: There might be some callouts left, but we don't wait */
	return 0;
      }
  }

Niels Möller's avatar
Niels Möller committed
148
  res = poll(fds, nfds, IDLE_TIME);
Niels Möller's avatar
Niels Möller committed
149

Niels Möller's avatar
Niels Möller committed
150
151
152
153
154
155
  if (!res)
    {
      gc_maybe(&b->super, 0);
      res = poll(fds, nfds, -1);
    }
  
Niels Möller's avatar
Niels Möller committed
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
  if (!res)
    {
      /* Callouts are not implemented */
      fatal("Unexpected timeout\n");
    }
  if (res < 0)
    switch(errno)
      {
      case EAGAIN:
      case EINTR:
	return 1;
      default:
	fatal("io_iter: poll failed: %s", strerror(errno));
      }
  
  {
    /* Do io. Note that the callback functions may add new fds to the
     * head of the list, or set the close_now flag on any fd. */

    struct lsh_fd *fd;
    int i;
    
    for(fd = b->files, i=0; fd; fd = fd->next, i++)
      {
	assert(i<nfds);
	
	if (fd->close_now)
	  continue;

	if (fds[i].revents & POLLOUT)
	  WRITE_FD(fd);

	if (fd->close_now)
	  continue;

	if (fds[i].revents & POLLIN)
	  READ_FD(fd);
      }
    assert(i == nfds);
  }

  return 1;
}
Niels Möller's avatar
Niels Möller committed
199
200
201

struct fd_read
{
202
  struct abstract_read super;
Niels Möller's avatar
Niels Möller committed
203
204
205
  int fd;
};

Niels Möller's avatar
Niels Möller committed
206
static int do_read(struct abstract_read **r, UINT32 length, UINT8 *buffer)
Niels Möller's avatar
Niels Möller committed
207
{
208
209
210
  struct fd_read *closure
    = (struct fd_read *) *r;

211
212
  MDEBUG(closure);
  
213
214
215
216
217
218
219
  while(1)
    {
      int res = read(closure->fd, buffer, length);
      if (!res)
	return A_EOF;
      if (res > 0)
	return res;
220

221
222
223
      switch(errno)
	{
	case EINTR:
224
225
	  continue;  /* FIXME: Is it really worth looping here,
		      * instead of in the select loop? */
226
227
	case EWOULDBLOCK:  /* aka EAGAIN */
	  return 0;
Niels Möller's avatar
Niels Möller committed
228
229
230
	case EPIPE:
	  werror("io.c: read() returned EPIPE! Treating it as EOF.\n");
	  return A_EOF;
231
	default:
Niels Möller's avatar
Niels Möller committed
232
233
234
235
	  werror("io.c: do_read: read() failed (errno %d), %s\n",
		 errno, strerror(errno));
	  debug("  fd = %d, buffer = %p, length = %ud\n",
		closure->fd, buffer, length);
236
237
238
239
	  return A_FAIL;
	}
    }
}
Niels Möller's avatar
Niels Möller committed
240

Niels Möller's avatar
Niels Möller committed
241
static void read_callback(struct lsh_fd *fd)
242
{
Niels Möller's avatar
Niels Möller committed
243
244
  struct io_fd *self = (struct io_fd *) fd;
  int res;
245

Niels Möller's avatar
Niels Möller committed
246
247
  struct fd_read r =
  { { STACK_HEADER, do_read }, fd->fd };
248

Niels Möller's avatar
Niels Möller committed
249
250
251
252
253
  MDEBUG(self);
  
  /* The handler function may install a new handler */
  res = READ_HANDLER(self->handler,
		     &r.super);
254

Niels Möller's avatar
Niels Möller committed
255
256
  /* NOTE: These flags are not mutually exclusive. All combination
   * must be handled correctly. */
257
  
Niels Möller's avatar
Niels Möller committed
258
259
  /* NOTE: (i) If LSH_DIE is set, LSH_CLOSE is ignored. (ii) If the fd
   * is read_only, LSH_CLOSE is the same as LSH_DIE. */
260

Niels Möller's avatar
Niels Möller committed
261
262
  /* This condition must be taken care of earlier. */
  assert(!(res & LSH_CHANNEL_FINISHED));
263

Niels Möller's avatar
Niels Möller committed
264
265
  /* Not implemented */
  assert(!(res & LSH_KILL_OTHERS));
266

Niels Möller's avatar
Niels Möller committed
267
  if (res & LSH_HOLD)
268
    {
Niels Möller's avatar
Niels Möller committed
269
270
271
      /* This flag should not be combined with anything else */
      assert(res == LSH_HOLD);
      fd->want_read = 0;
272
    }
Niels Möller's avatar
Niels Möller committed
273
  if (res & LSH_DIE)
Niels Möller's avatar
Niels Möller committed
274
    {
Niels Möller's avatar
Niels Möller committed
275
276
277
278
279
280
      if (self->buffer)
	write_buffer_close(self->buffer);
		  
      fd->close_reason = LSH_FAILUREP(res)
	? CLOSE_PROTOCOL_FAILURE : 0;
      fd->close_now = 1;
281
    }
Niels Möller's avatar
Niels Möller committed
282
  else if (res & LSH_CLOSE)
283
    {
Niels Möller's avatar
Niels Möller committed
284
285
286
287
288
289
290
291
292
293
294
295
296
      if (self->buffer)
	{
	  write_buffer_close(self->buffer);
	  /* Don't attempt to read any further. */
	  /* FIXME: Is it safe to free the handler here? */
	  self->super.want_read = 0;
	  self->handler = NULL;
	}
      else
	fd->close_now = 1;
		  
      fd->close_reason
	= LSH_FAILUREP(res) ? CLOSE_PROTOCOL_FAILURE : CLOSE_EOF;
297
    }
Niels Möller's avatar
Niels Möller committed
298
}
299

Niels Möller's avatar
Niels Möller committed
300
301
302
303
304
305
306
static void write_callback(struct lsh_fd *fd)
{
  struct io_fd *self = (struct io_fd *) fd;
  UINT32 size;
  int res;
  
  MDEBUG(self);
Niels Möller's avatar
Niels Möller committed
307

Niels Möller's avatar
Niels Möller committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  size = MIN(self->buffer->end - self->buffer->start,
	     self->buffer->block_size);
  assert(size);
  
  res = write(fd->fd,
	      self->buffer->buffer + self->buffer->start,
	      size);
  if (!res)
    fatal("Closed?");
  if (res < 0)
    switch(errno)
      {
      case EINTR:
      case EAGAIN:
	break;
      case EPIPE:
	werror("Broken pipe\n");
	fd->close_reason = CLOSE_WRITE_FAILED;
	fd->close_now = 1;
	break;
      default:
	werror("io.c: write failed, %s\n", strerror(errno));

	fd->close_reason = CLOSE_WRITE_FAILED;
	fd->close_now = 1;
	
	break;
      }
  else
    self->buffer->start += res;
}  
339

Niels Möller's avatar
Niels Möller committed
340
341
342
343
344
345
346
static void listen_callback(struct lsh_fd *fd)
{
  struct listen_fd *self = (struct listen_fd *) fd;
  struct sockaddr_in peer;
  size_t addr_len = sizeof(peer);
  int res;
  int conn;
347

Niels Möller's avatar
Niels Möller committed
348
349
350
  MDEBUG(self);
  
  /* FIXME: Do something with the peer address? */
351

Niels Möller's avatar
Niels Möller committed
352
353
354
  conn = accept(fd->fd,
		(struct sockaddr *) &peer, &addr_len);
  if (conn < 0)
355
    {
Niels Möller's avatar
Niels Möller committed
356
357
      werror("io.c: accept() failed, %s", strerror(errno));
      return;
358
    }
Niels Möller's avatar
Niels Möller committed
359
360
  res = FD_CALLBACK(self->callback, conn);
  if (LSH_ACTIONP(res))
361
    {
Niels Möller's avatar
Niels Möller committed
362
363
364
365
366
      werror("Strange: Accepted a connection, "
	     "but failed before writing anything.\n");
      fd->close_now = 1;
      fd->close_reason = LSH_FAILUREP(res) ? CLOSE_PROTOCOL_FAILURE
	: CLOSE_EOF;
367
    }
Niels Möller's avatar
Niels Möller committed
368
}
369

Niels Möller's avatar
Niels Möller committed
370
371
372
373
374
375
static void connect_callback(struct lsh_fd *fd)
{
  struct connect_fd *self = (struct connect_fd *) fd;
  int res;
  
  MDEBUG(self);
376

Niels Möller's avatar
Niels Möller committed
377
  res = FD_CALLBACK(self->callback, fd->fd);
378

Niels Möller's avatar
Niels Möller committed
379
  if (LSH_ACTIONP(res))
380
    {
Niels Möller's avatar
Niels Möller committed
381
382
      werror("Strange: Connected, "
	     "but failed before writing anything.\n");
383
384
    }
  else
Niels Möller's avatar
Niels Möller committed
385
386
387
    {
      /* To avoid actually closing the fd */
      fd->fd = -1;
388
    }
Niels Möller's avatar
Niels Möller committed
389
  fd->close_now = 1;
390
391
}

392
/* FIXME: Prehaps this function should return a suitable exit code? */
393
394
void io_run(struct io_backend *b)
{
Niels Möller's avatar
Niels Möller committed
395
396
397
398
399
400
401
402
403
  struct sigaction pipe;

  pipe.sa_handler = SIG_IGN;
  sigemptyset(&pipe.sa_mask);
  pipe.sa_flags = 0;
  pipe.sa_restorer = NULL;
  
  if (sigaction(SIGPIPE, &pipe, NULL) < 0)
    fatal("Failed to ignore SIGPIPE.\n");
404
  
405
406
  while(io_iter(b))
    ;
407
408
}

409
410
void init_backend(struct io_backend *b)
{
Niels Möller's avatar
Niels Möller committed
411
412
413
414
  b->files = NULL;
#if 0
  b->callouts = 0;
#endif
415
416
}

Niels Möller's avatar
Niels Möller committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* Initializes a file structure, and adds it to the backend's list. */
static void init_file(struct io_backend *b, struct lsh_fd *f, int fd)
{
  /* assert(fd); */
  f->fd = fd;
  f->close_reason = -1; /* Invalid reason */
  f->close_callback = NULL;

  f->prepare = NULL;

  f->want_read = 0;
  f->read = NULL;

  f->want_write = 0;
  f->write = NULL;

  f->close_now = 0;
  f->really_close = NULL;

  f->next = b->files;
  b->files = f;
}
    
440
441
442
/*
 * Fill in ADDR from HOST, SERVICE and PROTOCOL.
 * Supplying a null pointer for HOST means use INADDR_ANY.
443
 * Otherwise HOST is an numbers-and-dots ip-number or a dns name.
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
 *
 * PROTOCOL can be tcp or udp.
 *
 * Supplying a null pointer for SERVICE, means use port 0, i.e. no port.
 * 
 * Returns zero on errors, 1 if everything is ok.
 */
int
get_inaddr(struct sockaddr_in	* addr,
	   const char		* host,
	   const char		* service,
	   const char		* protocol)
{
  memset(addr, 0, sizeof *addr);
  addr->sin_family = AF_INET;

  /*
   *  Set host part of ADDR
   */
  if (host == NULL)
    addr->sin_addr.s_addr = INADDR_ANY;
  else
    {
      /* First check for numerical ip-number */
      addr->sin_addr.s_addr = inet_addr(host);
      if (addr->sin_addr.s_addr == (unsigned long)-1)
	{
	  struct hostent * hp;
	  
	  hp = gethostbyname(host);
	  if (hp == NULL)
	    return 0;
	  memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
	  addr->sin_family = hp->h_addrtype;
	}
    }

  /*
   *  Set port part of ADDR
   */
  if (service == NULL)
    addr->sin_port = htons(0);
  else
    {
      char		* end;
      long		  portno;

      portno = strtol(service, &end, 10);
      if (portno > 0  &&  portno <= 65535
	  &&  end != service  &&  *end == '\0')
	{
	  addr->sin_port = htons(portno);
	}
      else
	{
	  struct servent	* serv;

	  serv = getservbyname(service, "tcp");
	  if (serv == NULL)
	    return 0;
	  addr->sin_port = serv->s_port;
Niels Möller's avatar
Niels Möller committed
505
506
	}
    }
507
508

  return 1;
Niels Möller's avatar
Niels Möller committed
509
510
511
512
513
514
515
516
}

void io_set_nonblocking(int fd)
{
  if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
    fatal("io_set_nonblocking: fcntl() failed, %s", strerror(errno));
}

517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
void io_set_close_on_exec(int fd)
{
  if (fcntl(fd, F_SETFD, 1) < 0)
    fatal("Can't set close-on-exec flag for fd %d: %s\n",
	  fd, strerror(errno));
}

/* ALL file descripters handled by the backend should use non-blocking mode,
 * and have the close-on-exec flag set. */

void io_init_fd(int fd)
{
  io_set_nonblocking(fd);
  io_set_close_on_exec(fd);
}

533
534
535
536
537
/* Some code is taken from bellman's tcputils. */
struct connect_fd *io_connect(struct io_backend *b,
			      struct sockaddr_in *remote,
			      struct sockaddr_in *local,
			      struct fd_callback *f)
Niels Möller's avatar
Niels Möller committed
538
{
539
540
  struct connect_fd *fd;
  int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Niels Möller's avatar
Niels Möller committed
541
  
542
543
  if (s<0)
    return NULL;
Niels Möller's avatar
Niels Möller committed
544

545
546
  debug("io.c: connecting using fd %d\n", s);
  
547
  io_init_fd(s);
Niels Möller's avatar
Niels Möller committed
548

549
550
551
552
553
554
555
  if (local  &&  bind(s, (struct sockaddr *)local, sizeof *local) < 0)
    {
      int saved_errno = errno;
      close(s);
      errno = saved_errno;
      return NULL;
    }
Niels Möller's avatar
Niels Möller committed
556

557
558
559
560
561
562
563
564
565
  if ( (connect(s, (struct sockaddr *)remote, sizeof *remote) < 0)
       && (errno != EINPROGRESS) )       
    {
      int saved_errno = errno;
      close(s);
      errno = saved_errno;
      return NULL;
    }
  
Niels Möller's avatar
Niels Möller committed
566
  NEW(fd);
Niels Möller's avatar
Niels Möller committed
567
  init_file(b, &fd->super, s);
568

Niels Möller's avatar
Niels Möller committed
569
570
571
  fd->super.want_write = 1;
  fd->super.write = connect_callback;
  fd->callback = f;
Niels Möller's avatar
Niels Möller committed
572

573
  return fd;
Niels Möller's avatar
Niels Möller committed
574
575
}

576
577
578
579
580
581
struct listen_fd *io_listen(struct io_backend *b,
			    struct sockaddr_in *local,
			    struct fd_callback *callback)
{
  struct listen_fd *fd;
  int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Niels Möller's avatar
Niels Möller committed
582
  
583
584
585
  if (s<0)
    return NULL;

586
587
  debug("io.c: listening on fd %d\n", s);
  
588
  io_init_fd(s);
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606

  {
    int yes = 1;
    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof yes);
  }

  if (bind(s, (struct sockaddr *)local, sizeof *local) < 0)
    {
      close(s);
      return NULL;
    }

  if (listen(s, 256) < 0) 
    {
      close(s);
      return NULL;
    }

Niels Möller's avatar
Niels Möller committed
607
  NEW(fd);
608

Niels Möller's avatar
Niels Möller committed
609
610
611
612
  init_file(b, &fd->super, s);

  fd->super.want_read = 1;
  fd->super.read = listen_callback;
613
614
615
616
617
  fd->callback = callback;

  return fd;
}

Niels Möller's avatar
Niels Möller committed
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
static void really_close(struct lsh_fd *fd)
{
  struct io_fd *self = (struct io_fd *) fd;

  MDEBUG(self);

  assert(self->buffer);

  write_buffer_close(self->buffer);
}

static void prepare_write(struct lsh_fd *fd)
{
  struct io_fd *self = (struct io_fd *) fd;

  MDEBUG(self);

  assert(self->buffer);

  if (! (fd->want_write = write_buffer_pre_write(self->buffer))
      && self->buffer->closed)
    fd->close_now = 1;
}
  
Niels Möller's avatar
Niels Möller committed
642
643
struct abstract_write *io_read_write(struct io_backend *b,
				     int fd,
Niels Möller's avatar
Niels Möller committed
644
				     struct read_handler *handler,
Niels Möller's avatar
Niels Möller committed
645
				     UINT32 block_size,
646
				     struct close_callback *close_callback)
647
{
Niels Möller's avatar
Niels Möller committed
648
  struct io_fd *f;
649
  struct write_buffer *buffer = write_buffer_alloc(block_size);
Niels Möller's avatar
Niels Möller committed
650

651
652
  debug("io.c: Preparing fd %d for reading and writing\n", fd);
  
653
654
  io_init_fd(fd);
  
Niels Möller's avatar
Niels Möller committed
655
  NEW(f);
Niels Möller's avatar
Niels Möller committed
656
  init_file(b, &f->super, fd);
657
  
658
  /* Reading */
Niels Möller's avatar
Niels Möller committed
659
660
661
  f->super.read = read_callback;
  f->super.want_read = !!handler;
  f->handler = handler;
662
663

  /* Writing */
Niels Möller's avatar
Niels Möller committed
664
665
  f->super.prepare = prepare_write;
  f->super.write = write_callback;
Niels Möller's avatar
Niels Möller committed
666
  f->buffer = buffer;
667

Niels Möller's avatar
Niels Möller committed
668
669
670
  /* Closing */
  f->super.really_close = really_close;
  f->super.close_callback = close_callback;
671

672
  return &buffer->super;
673
}
674
675
676

struct io_fd *io_read(struct io_backend *b,
		      int fd,
Niels Möller's avatar
Niels Möller committed
677
		      struct read_handler *handler,
678
679
680
681
		      struct close_callback *close_callback)
{
  struct io_fd *f;

682
683
  debug("io.c: Preparing fd %d for reading\n", fd);
  
684
685
  io_init_fd(fd);

686
687
  NEW(f);

Niels Möller's avatar
Niels Möller committed
688
  init_file(b, &f->super, fd);
689

Niels Möller's avatar
Niels Möller committed
690
691
692
693
  /* Reading */
  f->super.want_read = !!handler;
  f->super.read = read_callback;
  f->handler = handler;
694

Niels Möller's avatar
Niels Möller committed
695
  f->super.close_callback = close_callback;
696
697
698
699

  return f;
}

700
701
702
703
struct io_fd *io_write(struct io_backend *b,
		       int fd,
		       UINT32 block_size,
		       struct close_callback *close_callback)
704
705
706
707
{
  struct io_fd *f;
  struct write_buffer *buffer = write_buffer_alloc(block_size);

708
709
  debug("io.c: Preparing fd %d for writing\n", fd);
  
710
711
  io_init_fd(fd);
  
712
  NEW(f);
Niels Möller's avatar
Niels Möller committed
713
  init_file(b, &f->super, fd);
714
715

  /* Writing */
Niels Möller's avatar
Niels Möller committed
716
717
  f->super.prepare = prepare_write;
  f->super.write = write_callback;
718
  f->buffer = buffer;
Niels Möller's avatar
Niels Möller committed
719
720
  
  f->super.close_callback = close_callback;
721

722
  return f;
723
}
Niels Möller's avatar
Niels Möller committed
724
725
726
727

/* Marks a file for closing, at the end of the current iteration.
 * FIXME: Could be generalized for other fd:s than read-write fds. */

728
void close_fd(struct lsh_fd *fd, int reason)
Niels Möller's avatar
Niels Möller committed
729
{
730
  debug("Marking fd %d for closing.\n", fd->fd);
731
  fd->close_reason = reason;
Niels Möller's avatar
Niels Möller committed
732
733
  fd->close_now = 1;
}