5 * Unix Domain Sockets Implementation (PF_UNIX, PF_LOCAL)
9 * dev_uds.c, table.c, uds.c
13 #include <sys/types.h>
14 #include <sys/ucred.h>
17 #include <minix/endpoint.h>
19 /* max connection backlog for incoming connections */
20 #define UDS_SOMAXCONN 64
22 typedef void* filp_id_t
;
24 /* ancillary data to be sent */
26 filp_id_t filps
[OPEN_MAX
];
33 * Internal State Information for a socket descriptor.
40 /* This file descriptor is UDS_FREE and can be allocated. */
43 /* OR it is UDS_INUSE and can't be allocated. */
46 /* state is set to UDS_INUSE in uds_open(). state is Set to
47 * UDS_FREE in uds_init() and uds_close(). state should be
48 * checked prior to all operations.
57 /* endpoint for suspend/resume */
60 /* Pipe Housekeeping */
62 /* inode number on PFS -- each descriptor is backed by 1
63 * PIPE which is allocated in uds_open() and freed in
64 * uds_close(). Data is sent/written to a peer's PIPE.
65 * Data is recv/read from this PIPE.
70 /* position in the PIPE where the data starts */
73 /* size of data in the PIPE */
76 /* control read/write, set by uds_open() and shutdown(2).
77 * Can be set to S_IRUSR|S_IWUSR, S_IRUSR, S_IWUSR, or 0
78 * for read and write, read only, write only, or neither.
79 * default is S_IRUSR|S_IWUSR.
86 /* socket type - SOCK_STREAM, SOCK_DGRAM, or SOCK_SEQPACKET
87 * Set by uds_ioctl(NWIOSUDSTYPE). It defaults to -1 in
88 * uds_open(). Any action on a socket with type -1 besides
89 * uds_ioctl(NWIOSUDSTYPE) and uds_close() will result in
94 /* queue of pending connections for server sockets.
95 * connect(2) inserts and accept(2) removes from the queue
97 int backlog
[UDS_SOMAXCONN
];
99 /* requested connection backlog size. Set by listen(2)
100 * Bounds (0 <= backlog_size <= UDS_SOMAXCONN)
101 * Defaults to UDS_SOMAXCONN which is defined above.
103 unsigned char backlog_size
;
105 /* index of peer in uds_fd_table for connected sockets.
106 * -1 is used to mean no peer. Assumptions: peer != -1 means
111 /* index of child (client sd returned by accept(2))
112 * -1 is used to mean no child.
116 /* address -- the address the socket is bound to.
117 * Assumptions: addr.sun_family == AF_UNIX means its bound.
119 struct sockaddr_un addr
;
121 /* target -- where DGRAMs are sent to on the next uds_write(). */
122 struct sockaddr_un target
;
124 /* source -- address where DGRAMs are from. used to fill in the
125 * from address in recvfrom(2) and recvmsg(2).
127 struct sockaddr_un source
;
129 /* Flag (1 or 0) - listening for incoming connections.
130 * Default to 0. Set to 1 by do_listen()
134 /* stores file pointers and credentials being sent between
135 * processes with sendmsg(2) and recvmsg(2).
137 struct ancillary ancillary_data
;
139 /* Holds an errno. This is set when a connected socket is
140 * closed and we need to pass ECONNRESET on to a suspended
145 /* Suspend/Revive Housekeeping */
148 /* SUSPEND State Flags */
151 /* Socket isn't blocked. */
152 UDS_NOT_SUSPENDED
= 0,
154 /* Socket is blocked on read(2) waiting for data to read. */
155 UDS_SUSPENDED_READ
= 1,
157 /* Socket is blocked on write(2) for space to write data. */
158 UDS_SUSPENDED_WRITE
= 2,
160 /* Socket is blocked on connect(2) waiting for the server. */
161 UDS_SUSPENDED_CONNECT
= 4,
163 /* Socket is blocked on accept(2) waiting for clients. */
164 UDS_SUSPENDED_ACCEPT
= 8
167 /* Flag (1 or 0) - thing socket was waiting for is ready.
168 * If 1, then uds_status() will attempt the operation that
169 * the socket was blocked on.
173 /* i/o grant, saved for later use by suspended procs */
176 /* is of i/o grant, saved for later use by suspended procs */
179 /* Save the call number so that uds_cancel() can unwind the
184 /* Save the IOCTL so uds_cancel() knows what got cancelled. */
187 /* Flag (1 or 0) - the system call completed.
188 * A doc I read said DEV_CANCEL might be called even though
189 * the operation is finished. We use this variable to
190 * determine if we should rollback the changes or not.
196 /* Flag (1 or 0) - the process blocked on select(2). When
197 * selecting is 1 and I/O happens on this socket, then
198 * select_proc should be notified.
202 /* when a select is in progress, we notify() this endpoint
205 endpoint_t select_proc
;
207 /* Options (SEL_RD, SEL_WR, SEL_ERR) that are requested. */
210 /* Options that are available for this socket. */
213 /* Flag (1 or 0) to be set to one before calling notify().
214 * uds_status() will use the flag to locate this descriptor.
219 typedef struct uds_fd uds_fd_t
;
221 /* File Descriptor Table -- Defined in uds.c */
222 EXTERN uds_fd_t uds_fd_table
[NR_FDS
];
225 * Take message m and get the index in uds_fd_table.
227 #define uds_minor(m) (minor((dev_t) m->DEVICE))
228 #define uds_minor_old(m) (minor((short) m->DEVICE))
231 * Fill in a reply message.
233 #define uds_set_reply(msg,type,endpoint,io_gr,status) \
235 (msg)->m_type = type; \
236 (msg)->REP_ENDPT = endpoint; \
237 (msg)->REP_IO_GRANT = io_gr; \
238 (msg)->REP_STATUS = status; \
241 #define uds_sel_reply(msg,type,minor,ops) \
243 (msg)->m_type = type; \
244 (msg)->DEV_MINOR = minor; \
245 (msg)->DEV_SEL_OPS = ops; \