summaryrefslogtreecommitdiff
path: root/libchannel/channel.c
blob: e273c65fb2e6d72f61c9be95acd7bf47941bad21 (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
/* Generic channel functions.

   Copyright (C) 1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2007
     Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.org>
   Reworked for libchannel by Carl Fredrik Hammar <hammy.lite@gmail.com>

   This file is part of the GNU Hurd.

   The GNU Hurd 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.

   The GNU Hurd 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */

#include <malloc.h>

#include "channel.h"

/* Allocate a new channel of hub HUB, with FLAGS set, then return it in
   CHANNEL.  Return ENOMEM if memory for the hub couldn't be allocated.  */
error_t
channel_alloc (struct channel_hub *hub, int flags,
               struct channel **channel)
{
  struct channel *new = malloc (sizeof (struct channel));
  if (!new)
    return ENOMEM;

  new->flags = flags;
  new->hub = hub;
  new->class_hook = 0;
  new->user_hook = 0;

  *channel = new;

  return 0;
}

/* Free CHANNEL and any generic resources allocated for it.  */
void
channel_free (struct channel *channel)
{
  free (channel);
}

/* Allocate a new channel, open it, and return it in CHANNEL.  Uses
   HUB's open method and passes FLAGS to it, unless it's null.  */
error_t
channel_open (struct channel_hub *hub, int flags,
	      struct channel **channel)
{
  error_t err;

  flags |= hub->flags & (CHANNEL_READONLY | CHANNEL_WRITEONLY);
  err = channel_alloc (hub, flags, channel);
  if (err)
    return err;

  if (hub->class->open)
    err = (*hub->class->open) (*channel, flags);

  return err;
}

/* Call CHANNEL's close method, unless it's null, then free it
   (regardless.)  */
void
channel_close (struct channel *channel)
{
  if (channel->hub->class->close)
    (*channel->hub->class->close) (channel);

  channel_free (channel);
}

/* Set the flags FLAGS in CHANNEL.  Remove any already set flags in FLAGS,
   if FLAGS contain any backend flags call set_flags method or if
   set_flags is null return EINVAL.  Lastly generic flags get set.  */
error_t
channel_set_flags (struct channel *channel, int flags)
{
  error_t err = 0;
  int orig = channel->flags, new = flags & ~orig;

  if (new & CHANNEL_BACKEND_FLAGS)
    {
      if (channel->hub->class->set_flags)
	err = (*channel->hub->class->set_flags) (channel, new);
      else
	err = EINVAL;
    }

  if (! err)
    channel->flags |= (new & ~CHANNEL_BACKEND_FLAGS);

  return err;
}

/* Clear the flags FLAGS in CHANNEL.  Remove any already cleared flags in
   FLAGS, if FLAGS contain any backend flags call clear_flags method or if
   clear_flags is null return EINVAL.  Lastly generic flags get
   cleared.  */
error_t
channel_clear_flags (struct channel *channel, int flags)
{
  error_t err = 0;
  int orig = channel->flags, kill = flags & orig;

  if (kill & CHANNEL_BACKEND_FLAGS)
    {
      if (channel->hub->class->clear_flags)
	err = (*channel->hub->class->clear_flags) (channel, kill);
      else
	err = EINVAL;
    }

  if (! err)
    channel->flags &= ~(kill & ~CHANNEL_BACKEND_FLAGS);

  return err;
}

/* Reads at most AMOUNT bytes from CHANNEL into BUF and LEN with the usual
   return buf semantics.  Block until data is available and return 0 bytes
   on EOF.  If channel is write-only return EPERM, otherwise forward call
   to CHANNEL's read method.  */
error_t
channel_read (struct channel *channel, size_t amount,
	      void **buf, size_t *len)
{
  if (channel->flags & CHANNEL_WRITEONLY)
    return EPERM;

  return (*channel->hub->class->read) (channel, amount, buf, len);
}

/* Write LEN bytes of BUF to CHANNEL, AMOUNT is set to the amount actually
   witten.  Block until data can be written.  If channel is read-only
   return EPERM, otherwise forward call to CHANNEL's write method.  */
error_t
channel_write (struct channel *channel, void *buf, size_t len,
	       size_t *amount)
{
  if (channel->flags & CHANNEL_READONLY)
    return EPERM;

  return (*channel->hub->class->write) (channel, buf, len, amount);
}

/* Write out any pending data held by CHANNEL in buffers, by forwarding
   call to flush method, unless it's null.  */
error_t
channel_flush (struct channel *channel)
{
  if (channel->hub->class->flush)
    return (*channel->hub->class->flush) (channel);
  else
    return 0;
}