~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mit-pthreads/pthreads/fd_sysv.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ==== fd_sysv.c ============================================================
 
2
 * Copyright (c) 1994 by Chris Provenzano, proven@mit.edu
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 * 1. Redistributions of source code must retain the above copyright
 
9
 *    notice, this list of conditions and the following disclaimer.
 
10
 * 2. Redistributions in binary form must reproduce the above copyright
 
11
 *    notice, this list of conditions and the following disclaimer in the
 
12
 *    documentation and/or other materials provided with the distribution.
 
13
 * 3. All advertising materials mentioning features or use of this software
 
14
 *    must display the following acknowledgement:
 
15
 *  This product includes software developed by Chris Provenzano.
 
16
 * 4. The name of Chris Provenzano may not be used to endorse or promote 
 
17
 *        products derived from this software without specific prior written
 
18
 *        permission.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO ``AS IS'' AND
 
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO BE LIABLE FOR ANY 
 
24
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
25
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 
26
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
27
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
 
30
 * SUCH DAMAGE.
 
31
 *
 
32
 * Description : Transforms BSD socket calls to SYSV streams.
 
33
 *
 
34
 *  1.00 94/11/19 proven
 
35
 *      -Started coding this file.
 
36
 */
 
37
 
 
38
#ifndef lint
 
39
static const char rcsid[] = "$Id$";
 
40
#endif
 
41
 
 
42
#include <config.h>
 
43
#include <pthread.h>
 
44
#include <fcntl.h>
 
45
#include <errno.h>
 
46
 
 
47
#if defined (HAVE_SYSCALL_PUTMSG) && defined (HAVE_SYSCALL_GETMSG) && !defined(HAVE_SYSCALL_SOCKETCALL) && !defined(HAVE_SYSCALL_SOCKET)
 
48
#define HAVE_STREAMS    1
 
49
 
 
50
#include <sys/types.h>
 
51
#include <sys/uio.h>
 
52
#include <sys/socket.h>
 
53
#include <sys/stream.h>
 
54
#include <sys/stropts.h>
 
55
#include <tiuser.h>
 
56
#include <sys/tihdr.h>
 
57
#include <netinet/in.h>
 
58
#include <sys/timod.h>
 
59
 
 
60
#define STREAM_BUF_SIZE sizeof(union T_primitives) + sizeof(struct sockaddr)
 
61
 
 
62
extern struct pthread_queue fd_wait_read, fd_wait_write;
 
63
 
 
64
/* ==========================================================================
 
65
 * putmsg_timedwait_basic()
 
66
 */
 
67
static int putmsg_timedwait_basic(int fd, struct strbuf * ctlptr, 
 
68
  struct strbuf * dataptr, int flags, struct timespec * timeout)
 
69
{
 
70
 
 
71
        int ret;
 
72
 
 
73
    pthread_run->sighandled=0;          /* Added by monty */
 
74
    while ((ret = machdep_sys_putmsg(fd_table[fd]->fd.i,
 
75
      ctlptr,  dataptr, flags)) < OK) {
 
76
        if (!(fd_table[fd]->flags & __FD_NONBLOCK) &&
 
77
          ((ret == -EWOULDBLOCK) || (ret == -EAGAIN))) {
 
78
            pthread_sched_prevent();
 
79
 
 
80
            /* queue pthread for a FDW_WAIT */
 
81
                        SET_PF_WAIT_EVENT(pthread_run);
 
82
            pthread_run->data.fd.fd = fd_table[fd]->fd.i;
 
83
                        pthread_queue_enq(&fd_wait_write, pthread_run);
 
84
 
 
85
            if (timeout) {
 
86
                /* get current time */
 
87
                struct timespec current_time;
 
88
                machdep_gettimeofday(&current_time);
 
89
                sleep_schedule(& current_time, timeout);
 
90
 
 
91
                pthread_resched_resume(PS_FDW_WAIT);
 
92
 
 
93
                /* We're awake */
 
94
                pthread_sched_prevent();
 
95
                if (sleep_cancel(pthread_run) == NOTOK) {
 
96
                  CLEAR_PF_DONE_EVENT(pthread_run);
 
97
                    pthread_sched_resume();
 
98
                    SET_ERRNO(ETIMEDOUT);
 
99
                    ret = -ETIMEDOUT;
 
100
                    break;
 
101
                }
 
102
                pthread_sched_resume();
 
103
            } else {
 
104
                pthread_resched_resume(PS_FDW_WAIT);
 
105
            }
 
106
                        CLEAR_PF_DONE_EVENT(pthread_run);
 
107
            if (pthread_run->sighandled) /* Added by monty */
 
108
            {                           /* We where aborted */
 
109
              SET_ERRNO(EINTR);
 
110
              ret= -EINTR;
 
111
              break;
 
112
            }
 
113
        } else {
 
114
            SET_ERRNO(-ret);
 
115
            break;
 
116
        }
 
117
    }
 
118
    return(ret);
 
119
}
 
120
 
 
121
/* ==========================================================================
 
122
 * putmsg_timedwait()
 
123
 */
 
124
int putmsg_timedwait(int fd, struct strbuf * ctlptr, struct strbuf * dataptr,
 
125
  int flags, struct timespec * timeout)
 
126
{
 
127
        int ret;
 
128
 
 
129
        if ((ret = fd_lock(fd, FD_WRITE, timeout)) == OK) {
 
130
                ret = putmsg_timedwait_basic(fd, ctlptr, dataptr, flags, timeout);
 
131
        fd_unlock(fd, FD_WRITE);
 
132
        }
 
133
        return(ret);
 
134
}
 
135
 
 
136
/* ==========================================================================
 
137
 * putmsg()
 
138
 */
 
139
int putmsg(int fd, struct strbuf * ctlptr, struct strbuf * dataptr,
 
140
  int flags)
 
141
{
 
142
        return(putmsg_timedwait(fd, ctlptr, dataptr, flags, NULL));
 
143
}
 
144
 
 
145
/* ==========================================================================
 
146
 * getmsg_timedwait_basic()
 
147
 */
 
148
int getmsg_timedwait_basic(int fd, struct strbuf * ctlptr, 
 
149
  struct strbuf * dataptr, int * flags, struct timespec * timeout)
 
150
{
 
151
        int ret;
 
152
 
 
153
    pthread_run->sighandled=0;          /* Added by monty */
 
154
    while ((ret = machdep_sys_getmsg(fd_table[fd]->fd.i,
 
155
      ctlptr, dataptr, flags)) < OK) {
 
156
        if (!(fd_table[fd]->flags & __FD_NONBLOCK) &&
 
157
          ((ret == -EWOULDBLOCK) || (ret == -EAGAIN))) {
 
158
            pthread_sched_prevent();
 
159
 
 
160
            /* queue pthread for a FDR_WAIT */
 
161
                        SET_PF_WAIT_EVENT(pthread_run);
 
162
            pthread_run->data.fd.fd = fd_table[fd]->fd.i;
 
163
                        pthread_queue_enq(&fd_wait_read, pthread_run);
 
164
 
 
165
            if (timeout) {
 
166
                /* get current time */
 
167
                struct timespec current_time;
 
168
                machdep_gettimeofday(&current_time);
 
169
                sleep_schedule(& current_time, timeout);
 
170
 
 
171
                pthread_resched_resume(PS_FDR_WAIT);
 
172
 
 
173
                /* We're awake */
 
174
                pthread_sched_prevent();
 
175
                if (sleep_cancel(pthread_run) == NOTOK) {
 
176
                                        CLEAR_PF_DONE_EVENT(pthread_run);
 
177
                    pthread_sched_resume();
 
178
                    SET_ERRNO(ETIMEDOUT);
 
179
                    ret = -ETIMEDOUT;
 
180
                    break;
 
181
                }
 
182
                pthread_sched_resume();
 
183
            } else {
 
184
                pthread_resched_resume(PS_FDR_WAIT);
 
185
            }
 
186
                        CLEAR_PF_DONE_EVENT(pthread_run);
 
187
            if (pthread_run->sighandled) /* Added by monty */
 
188
            {                           /* We where aborted */
 
189
              SET_ERRNO(EINTR);
 
190
              ret= -EINTR;
 
191
              break;
 
192
            }
 
193
 
 
194
        } else {
 
195
            SET_ERRNO(-ret);
 
196
            break;
 
197
        }
 
198
    }
 
199
        return(ret);
 
200
}
 
201
 
 
202
/* ==========================================================================
 
203
 * getmsg_timedwait()
 
204
 */
 
205
int getmsg_timedwait(int fd, struct strbuf * ctlptr, struct strbuf * dataptr,
 
206
  int * flags, struct timespec * timeout)
 
207
{
 
208
        int ret;
 
209
 
 
210
    if ((ret = fd_lock(fd, FD_READ, timeout)) == OK) {
 
211
                ret = getmsg_timedwait_basic(fd, ctlptr, dataptr, flags, timeout);
 
212
        fd_unlock(fd, FD_READ);
 
213
        }
 
214
        return (ret);
 
215
}
 
216
 
 
217
/* ==========================================================================
 
218
 * getmsg()
 
219
 */
 
220
int getmsg(int fd, struct strbuf * ctlptr, struct strbuf * dataptr,
 
221
  int * flags)
 
222
{
 
223
        return(getmsg_timedwait(fd, ctlptr, dataptr, flags, NULL));
 
224
}
 
225
 
 
226
#endif
 
227
 
 
228
/* ==========================================================================
 
229
 * Here are the berkeley socket functions implemented with stream calls.
 
230
 * These are not POSIX.
 
231
 * ======================================================================= */
 
232
 
 
233
#if (!defined (HAVE_SYSCALL_BIND)) && defined(HAVE_STREAMS)
 
234
 
 
235
/* ==========================================================================
 
236
 * bind()
 
237
 */
 
238
int bind(int fd, const struct sockaddr *name, int namelen)
 
239
{
 
240
  char buf[STREAM_BUF_SIZE];
 
241
  union T_primitives * res;
 
242
  struct T_bind_req * req;
 
243
  struct T_bind_ack * ack;
 
244
  struct strbuf strbuf;
 
245
  int flags, ret;
 
246
 
 
247
  if ((ret = fd_lock(fd, FD_RDWR, NULL)) == OK)
 
248
  {
 
249
    req = (struct T_bind_req *)buf;
 
250
    req->PRIM_type = T_BIND_REQ;
 
251
    req->ADDR_length = namelen;
 
252
    req->ADDR_offset = sizeof(struct T_bind_req);
 
253
    req->CONIND_number = 4;
 
254
    memcpy(buf + sizeof(struct T_bind_req), name, namelen);
 
255
 
 
256
    strbuf.len = sizeof(struct T_bind_req) + namelen;
 
257
    strbuf.maxlen = STREAM_BUF_SIZE;
 
258
    strbuf.buf = buf;
 
259
 
 
260
    if ((ret=putmsg_timedwait_basic(fd, &strbuf, NULL, 0, NULL)) == OK)
 
261
    {
 
262
      memset(buf, 0, STREAM_BUF_SIZE);
 
263
 
 
264
      strbuf.len = sizeof(struct T_bind_ack) + namelen;
 
265
      strbuf.maxlen = STREAM_BUF_SIZE;
 
266
      strbuf.buf = buf;
 
267
      flags = 0;
 
268
 
 
269
      if ((ret = getmsg_timedwait_basic(fd, &strbuf, NULL,
 
270
                                        &flags, NULL)) >= OK)
 
271
      {
 
272
        res = (union T_primitives *)buf;
 
273
 
 
274
        switch(res->type) {
 
275
        case T_BIND_ACK:
 
276
          ret = OK;
 
277
          break;
 
278
        default:
 
279
          SET_ERRNO(EPROTO);                    /* What should this be? */
 
280
          ret = NOTOK;
 
281
          break;
 
282
        }
 
283
      }
 
284
      else
 
285
      {
 
286
        SET_ERRNO(-ret);
 
287
        ret = NOTOK;
 
288
      }
 
289
    }
 
290
    else
 
291
    {
 
292
      SET_ERRNO(-ret);
 
293
      ret = NOTOK;
 
294
    }
 
295
    fd_unlock(fd, FD_RDWR);
 
296
  }
 
297
  return(ret);
 
298
}
 
299
 
 
300
#endif
 
301
 
 
302
#if (!defined (HAVE_SYSCALL_CONNECT)) && defined(HAVE_STREAMS)
 
303
 
 
304
/* ==========================================================================
 
305
 * connect()
 
306
 */
 
307
int connect(int fd, const struct sockaddr *name, int namelen)
 
308
{
 
309
  char buf[STREAM_BUF_SIZE];
 
310
  union T_primitives * res;
 
311
  struct T_conn_req * req;
 
312
  struct T_conn_con * con;
 
313
  struct T_ok_ack * ok;
 
314
  struct strbuf strbuf;
 
315
  int flags, ret;
 
316
 
 
317
  if ((ret = fd_lock(fd, FD_RDWR, NULL)) == OK)
 
318
  {
 
319
    req = (struct T_conn_req *)buf;
 
320
    req->PRIM_type = T_CONN_REQ;
 
321
    req->DEST_length = namelen;
 
322
    req->DEST_offset = sizeof(struct T_conn_req);
 
323
    req->OPT_length = 0;
 
324
    req->OPT_offset = 0;
 
325
    memcpy(buf + sizeof(struct T_conn_req), name, namelen);
 
326
 
 
327
    strbuf.len = sizeof(struct T_conn_req) + namelen;
 
328
    strbuf.maxlen = STREAM_BUF_SIZE;
 
329
    strbuf.buf = buf;
 
330
 
 
331
    if ((ret=putmsg_timedwait_basic(fd, &strbuf, NULL, 0, NULL)) != OK)
 
332
      goto err;
 
333
    
 
334
    memset(buf, 0, STREAM_BUF_SIZE);
 
335
    ok = (struct T_ok_ack *)buf;
 
336
 
 
337
    strbuf.maxlen = STREAM_BUF_SIZE;
 
338
    strbuf.len = STREAM_BUF_SIZE;
 
339
    strbuf.buf = buf;
 
340
    flags = 0;
 
341
 
 
342
    if ((ret=getmsg_timedwait_basic(fd, &strbuf, NULL, &flags, NULL)) < OK)
 
343
      goto err;                         /* Fixed by monty */
 
344
    if (ok->PRIM_type != T_OK_ACK)
 
345
    {
 
346
      ret= -EPROTO;                     /* What should this be? */
 
347
      goto err;
 
348
    }
 
349
 
 
350
    memset(buf, 0, STREAM_BUF_SIZE);
 
351
    strbuf.maxlen = STREAM_BUF_SIZE;
 
352
    strbuf.len = STREAM_BUF_SIZE;
 
353
    strbuf.buf = buf;
 
354
    flags = 0;
 
355
 
 
356
    if ((ret=getmsg_timedwait_basic(fd, &strbuf, NULL, &flags, NULL) < OK))
 
357
      goto err;
 
358
 
 
359
    res = (union T_primitives *) buf;
 
360
    switch(res->type) {
 
361
    case T_CONN_CON:
 
362
      ret = OK;
 
363
      break;
 
364
    case T_DISCON_IND:
 
365
      ret= -ECONNREFUSED;
 
366
      goto err;
 
367
    default:
 
368
      ret= -EPROTO;                     /* What should this be? */
 
369
      goto err;
 
370
    }
 
371
    fd_unlock(fd, FD_RDWR);
 
372
  }
 
373
  return(ret);
 
374
 
 
375
 err:
 
376
  fd_unlock(fd, FD_RDWR);
 
377
  SET_ERRNO(-ret);                              /* Proably not needed... */
 
378
  return NOTOK;
 
379
}
 
380
 
 
381
#endif
 
382
 
 
383
#if (!defined (HAVE_SYSCALL_LISTEN)) && defined(HAVE_STREAMS)
 
384
 
 
385
/* ==========================================================================
 
386
 * listen()
 
387
 */
 
388
int listen(int fd, int backlog)
 
389
{
 
390
        return(OK);
 
391
}
 
392
 
 
393
#endif
 
394
 
 
395
#if (!defined (HAVE_SYSCALL_SOCKET)) && defined(HAVE_STREAMS)
 
396
 
 
397
extern ssize_t                  __fd_kern_write();
 
398
static pthread_ssize_t  __fd_sysv_read();
 
399
extern int                              __fd_kern_close();
 
400
extern int                              __fd_kern_fcntl();
 
401
extern int                              __fd_kern_writev();
 
402
extern int                              __fd_kern_readv();
 
403
extern off_t                    __fd_kern_lseek();
 
404
 
 
405
/* Normal file operations */
 
406
static struct fd_ops __fd_sysv_ops = {
 
407
    __fd_kern_write, __fd_sysv_read, __fd_kern_close, __fd_kern_fcntl,
 
408
    __fd_kern_writev, __fd_kern_readv, __fd_kern_lseek, 1
 
409
};
 
410
 
 
411
/* ==========================================================================
 
412
 * read()   
 
413
 */         
 
414
static pthread_ssize_t __fd_sysv_read(union fd_data fd_data, int flags,
 
415
  void *buf, size_t nbytes, struct timespec * timeout)
 
416
{           
 
417
        struct strbuf dataptr;
 
418
    int fd = fd_data.i;
 
419
    int getmsg_flags;
 
420
    int ret;    
 
421
 
 
422
    getmsg_flags = 0;
 
423
        dataptr.len = 0;
 
424
        dataptr.buf = buf;
 
425
        dataptr.maxlen = nbytes;
 
426
 
 
427
    pthread_run->sighandled=0;          /* Added by monty */
 
428
    while ((ret = machdep_sys_getmsg(fd, NULL, &dataptr, &getmsg_flags)) < OK) {
 
429
        if (!(fd_table[fd]->flags & __FD_NONBLOCK) &&
 
430
          ((ret == -EWOULDBLOCK) || (ret == -EAGAIN))) { 
 
431
            pthread_sched_prevent();
 
432
 
 
433
            /* queue pthread for a FDR_WAIT */
 
434
            pthread_run->data.fd.fd = fd;
 
435
            SET_PF_WAIT_EVENT(pthread_run);
 
436
                        pthread_queue_enq(&fd_wait_read, pthread_run);
 
437
 
 
438
            if (timeout) {
 
439
                /* get current time */
 
440
                struct timespec current_time;
 
441
                machdep_gettimeofday(&current_time);
 
442
                sleep_schedule(& current_time, timeout);
 
443
            
 
444
                pthread_resched_resume(PS_FDR_WAIT);
 
445
 
 
446
                /* We're awake */
 
447
                pthread_sched_prevent();
 
448
                if (sleep_cancel(pthread_run) == NOTOK) {
 
449
                    CLEAR_PF_DONE_EVENT(pthread_run);
 
450
                    pthread_sched_resume();
 
451
                    SET_ERRNO(ETIMEDOUT);
 
452
                    ret = -ETIMEDOUT;
 
453
                    break;
 
454
                }
 
455
                pthread_sched_resume();
 
456
            } else {
 
457
                pthread_resched_resume(PS_FDR_WAIT);
 
458
            }
 
459
            CLEAR_PF_DONE_EVENT(pthread_run);
 
460
            if (pthread_run->sighandled) /* Added by monty */
 
461
            {                           /* We where aborted */
 
462
              SET_ERRNO(EINTR);
 
463
              return(NOTOK);
 
464
            }
 
465
        } else {
 
466
            SET_ERRNO(-ret);
 
467
            return(NOTOK);
 
468
            break;
 
469
        }
 
470
    }
 
471
    return(dataptr.len);
 
472
}   
 
473
 
 
474
/* ==========================================================================
 
475
 * socket_tcp()
 
476
 */
 
477
static int socket_tcp(int fd)
 
478
{
 
479
        int ret;
 
480
 
 
481
        if ((ret = machdep_sys_open("/dev/tcp", O_RDWR | O_NONBLOCK, 0)) >= OK) {
 
482
        /* Should fstat the file to determine what type it is */
 
483
        fd_table[fd]->ops   = & __fd_sysv_ops;
 
484
        fd_table[fd]->type  = FD_FULL_DUPLEX;
 
485
        fd_table[fd]->fd.i  = ret;
 
486
        fd_table[fd]->flags = 0;
 
487
    }
 
488
    return(ret);
 
489
}
 
490
 
 
491
/* ==========================================================================
 
492
 * socket()
 
493
 */
 
494
int socket(int af, int type, int protocol)
 
495
{
 
496
  int fd, fd_kern;
 
497
 
 
498
  if ((fd = fd_allocate()) < OK)
 
499
    return (fd);
 
500
 
 
501
  switch(af) {
 
502
  case AF_INET:
 
503
    switch(type) {
 
504
    case SOCK_STREAM:
 
505
      if ((fd_kern = socket_tcp(fd)) >= OK)
 
506
        return(fd);
 
507
      SET_ERRNO(-fd_kern);
 
508
      break;
 
509
    case SOCK_DGRAM:
 
510
      if ((fd_kern = machdep_sys_open("/dev/udp",
 
511
                                      O_RDWR | O_NONBLOCK, 0)) >= OK) {
 
512
        /* Should fstat the file to determine what type it is */
 
513
        fd_table[fd]->ops   = & __fd_sysv_ops;
 
514
        fd_table[fd]->type  = FD_FULL_DUPLEX;
 
515
        fd_table[fd]->fd.i  = fd_kern;
 
516
        fd_table[fd]->flags = 0;
 
517
        return(fd);
 
518
      }
 
519
      SET_ERRNO(-fd_kern);
 
520
      break;
 
521
    default:
 
522
      SET_ERRNO(EPROTONOSUPPORT);
 
523
      break;
 
524
    }
 
525
    break;
 
526
  case AF_UNIX:
 
527
  case AF_ISO:
 
528
  case AF_NS:
 
529
  default:
 
530
    SET_ERRNO(EPROTONOSUPPORT);
 
531
    break;
 
532
  }
 
533
  fd_table[fd]->count = 0;
 
534
  return(NOTOK);                                /* Fixed by monty */
 
535
}
 
536
 
 
537
#endif
 
538
 
 
539
#if (!defined (HAVE_SYSCALL_ACCEPT)) && defined(HAVE_STREAMS)
 
540
 
 
541
/* ==========================================================================
 
542
 * accept_fd()
 
543
 */
 
544
static int accept_fd(int fd, struct sockaddr *name, int *namelen, char * buf,
 
545
                     int SEQ_number)
 
546
{
 
547
  struct T_conn_res * res;
 
548
  struct strbuf strbuf;
 
549
  int fd_new, fd_kern;
 
550
 
 
551
  /* Get a new table entry */
 
552
  if ((fd_new = fd_allocate()) < OK)
 
553
    return(NOTOK);
 
554
 
 
555
  /* Get the new kernel entry */
 
556
  if (!((fd_kern = socket_tcp(fd_new)) < OK)) { 
 
557
    res = (struct T_conn_res *)buf;
 
558
    res->PRIM_type = T_CONN_RES;
 
559
    /* res->QUEUE_ptr = (queue_t *)&fd_kern; */
 
560
    res->OPT_length = 0;
 
561
    res->OPT_offset = 0;
 
562
    res->SEQ_number = SEQ_number;
 
563
                
 
564
    strbuf.maxlen = sizeof(union T_primitives) +sizeof(struct sockaddr);
 
565
    strbuf.len = sizeof(struct T_conn_ind) + (*namelen);
 
566
    strbuf.buf = buf;
 
567
 
 
568
    {
 
569
      struct strfdinsert insert;
 
570
 
 
571
      insert.ctlbuf.maxlen = (sizeof(union T_primitives) +
 
572
                              sizeof(struct sockaddr));
 
573
      insert.ctlbuf.len =  sizeof(struct T_conn_ind);
 
574
      insert.ctlbuf.buf = buf;
 
575
      insert.databuf.maxlen = 0;
 
576
      insert.databuf.len = 0;
 
577
      insert.databuf.buf = NULL;
 
578
      insert.flags = 0;
 
579
      insert.fildes = fd_kern;
 
580
      insert.offset = 4;
 
581
      /* Should the following be checked ? */
 
582
      machdep_sys_ioctl(fd_table[fd]->fd.i,  I_FDINSERT, &insert);
 
583
    }
 
584
 
 
585
    /*          if (putmsg_timedwait_basic(fd, &strbuf, NULL, 0, NULL) == OK) {
 
586
                /*                      return(fd_new); */
 
587
    {
 
588
      int flags = 0;
 
589
      int ret;
 
590
 
 
591
      /* Should the following be checked ? */
 
592
      ret = getmsg_timedwait_basic(fd, &strbuf, NULL, &flags, NULL); 
 
593
      return(fd_new);
 
594
 
 
595
    }
 
596
    machdep_sys_close(fd_kern);
 
597
  }
 
598
  fd_table[fd_new]->count = 0;
 
599
  return(NOTOK);
 
600
}
 
601
 
 
602
 
 
603
/* ==========================================================================
 
604
 * accept()
 
605
 */
 
606
int accept(int fd, struct sockaddr *name, int *namelen)
 
607
{
 
608
  char buf[sizeof(union T_primitives) + sizeof(struct sockaddr)];
 
609
  struct T_conn_ind * ind;
 
610
  struct strbuf strbuf;
 
611
  int flags, ret;
 
612
 
 
613
  if ((ret = fd_lock(fd, FD_RDWR, NULL)) == OK)
 
614
  {
 
615
    ind = (struct T_conn_ind *)buf;
 
616
    ind->PRIM_type = T_CONN_IND;
 
617
    ind->SRC_length = (*namelen);
 
618
    ind->SRC_offset = sizeof(struct T_conn_ind);
 
619
    ind->OPT_length = 0;
 
620
    ind->OPT_offset = 0;
 
621
    ind->SEQ_number = 0;
 
622
 
 
623
    strbuf.maxlen = sizeof(union T_primitives) + sizeof(struct sockaddr);
 
624
    strbuf.len = sizeof(struct T_conn_ind) + (*namelen);
 
625
    strbuf.buf = buf;
 
626
    flags = 0;
 
627
 
 
628
    if ((ret=getmsg_timedwait_basic(fd, &strbuf, NULL, &flags, NULL)) < OK)
 
629
    {
 
630
      SET_ERRNO(-ret);
 
631
      ret= NOTOK;
 
632
    }
 
633
    else
 
634
      ret = accept_fd(fd, name, namelen, buf, ind->SEQ_number);
 
635
    fd_unlock(fd, FD_RDWR);
 
636
  }
 
637
  return(ret);
 
638
}
 
639
 
 
640
#endif /* HAVE_SYSCALL_ACCEPT */
 
641
 
 
642
#if (!defined (HAVE_SYSCALL_SENDTO)) && defined (HAVE_STREAMS)
 
643
 
 
644
/* ==========================================================================
 
645
 * sendto_timedwait()
 
646
 */
 
647
ssize_t sendto_timedwait(int fd, const void * msg, size_t len, int flags, 
 
648
  const struct sockaddr *name, int namelen, struct timespec * timeout)
 
649
{
 
650
        char buf[STREAM_BUF_SIZE];
 
651
        struct T_unitdata_req * req;
 
652
        struct strbuf dataptr;
 
653
        struct strbuf ctlptr;
 
654
    ssize_t ret, prio;
 
655
 
 
656
        req = (struct T_unitdata_req *)buf;
 
657
        req->PRIM_type = T_UNITDATA_REQ;
 
658
        req->DEST_length = namelen;
 
659
        req->DEST_offset = sizeof(struct T_unitdata_req);
 
660
        req->OPT_length = 0;
 
661
        req->OPT_offset = 0;
 
662
        memcpy(buf + sizeof(struct T_unitdata_req), name, namelen);
 
663
 
 
664
        ctlptr.len = sizeof(struct T_unitdata_req) + namelen;
 
665
        ctlptr.maxlen = STREAM_BUF_SIZE;
 
666
        ctlptr.buf = buf;
 
667
 
 
668
        dataptr.len = len;
 
669
        dataptr.maxlen = len;
 
670
        dataptr.buf = (void *)msg;
 
671
 
 
672
        if ((ret = putmsg_timedwait(fd, &ctlptr, &dataptr, 0, timeout)) == OK) {
 
673
                ret = len;
 
674
        }
 
675
        return(ret);
 
676
}
 
677
 
 
678
/* ==========================================================================
 
679
 * sendto()
 
680
 */
 
681
ssize_t sendto(int fd, const void * msg, size_t len, int flags,
 
682
  const struct sockaddr *to, int to_len)
 
683
{
 
684
        return(sendto_timedwait(fd, msg, len, flags, to, to_len, NULL));
 
685
}
 
686
 
 
687
#endif
 
688
 
 
689
#if (!defined (HAVE_SYSCALL_SEND)) && defined (HAVE_STREAMS)
 
690
 
 
691
/* ==========================================================================
 
692
 * send_timedwait()
 
693
 */
 
694
ssize_t send_timedwait(int fd, const void * msg, size_t len, int flags, 
 
695
  struct timespec * timeout)
 
696
{
 
697
        char buf[STREAM_BUF_SIZE];
 
698
        struct T_unitdata_req * req;
 
699
        struct strbuf dataptr;
 
700
        struct strbuf ctlptr;
 
701
        ssize_t ret, prio;
 
702
 
 
703
        req = (struct T_unitdata_req *)buf;
 
704
        req->PRIM_type = T_UNITDATA_REQ;
 
705
        req->DEST_length = 0;
 
706
        req->DEST_offset = 0;
 
707
        req->OPT_length = 0;
 
708
        req->OPT_offset = 0;
 
709
 
 
710
        ctlptr.len = sizeof(struct T_unitdata_req);
 
711
        ctlptr.maxlen = STREAM_BUF_SIZE;
 
712
        ctlptr.buf = buf;
 
713
 
 
714
        dataptr.len = len;
 
715
        dataptr.maxlen = len;
 
716
        dataptr.buf = (void *)msg;
 
717
 
 
718
        if ((ret = putmsg_timedwait(fd, &ctlptr, &dataptr, 0, timeout)) == OK) {
 
719
                ret = len;
 
720
        }
 
721
        return(ret);
 
722
}
 
723
 
 
724
/* ==========================================================================
 
725
 * send()
 
726
 */
 
727
ssize_t send(int fd, const void * msg, size_t len, int flags)
 
728
{
 
729
        return(send_timedwait(fd, msg, len, flags, NULL));
 
730
}
 
731
 
 
732
#endif
 
733
 
 
734
#if (!defined (HAVE_SYSCALL_RECVFROM)) && defined(HAVE_STREAMS)
 
735
 
 
736
/* ==========================================================================
 
737
 * recvfrom_timedwait()
 
738
 */
 
739
ssize_t recvfrom_timedwait(int fd, void * msg, size_t len, int flags,
 
740
  struct sockaddr * name, int * namelen, struct timespec * timeout)
 
741
{
 
742
        char buf[STREAM_BUF_SIZE];
 
743
        struct T_unitdata_ind * ind;
 
744
        struct strbuf dataptr;
 
745
        struct strbuf ctlptr;
 
746
        int ret, prio;
 
747
 
 
748
        ctlptr.len = 0;
 
749
        ctlptr.maxlen = STREAM_BUF_SIZE;
 
750
        ctlptr.buf = buf;
 
751
 
 
752
        dataptr.maxlen = len;
 
753
        dataptr.len = 0;
 
754
        dataptr.buf = msg;
 
755
        
 
756
        prio = 0;
 
757
 
 
758
        ret = getmsg_timedwait(fd, &ctlptr, &dataptr, &prio, timeout);
 
759
        if (ret >= OK) {
 
760
                if (name != NULL) {
 
761
                        ind = (struct T_unitdata_ind *)buf;
 
762
 
 
763
                        if (*namelen > ind->SRC_length)
 
764
                                *namelen = ind->SRC_length;
 
765
                        memcpy(name, buf + ind->SRC_offset, *namelen);
 
766
                }
 
767
                ret = dataptr.len;
 
768
        }
 
769
 
 
770
        return(ret);
 
771
}
 
772
 
 
773
/* ==========================================================================
 
774
 * recvfrom()
 
775
 */
 
776
ssize_t recvfrom(int fd, void * buf, size_t len, int flags,
 
777
  struct sockaddr * from, int * from_len)
 
778
{
 
779
        return(recvfrom_timedwait(fd, buf, len, flags, from, from_len, NULL));
 
780
}
 
781
 
 
782
#endif
 
783
 
 
784
#if (!defined (HAVE_SYSCALL_RECV)) && defined(HAVE_STREAMS)
 
785
 
 
786
/* ==========================================================================
 
787
 * recv_timedwait()
 
788
 */
 
789
ssize_t recv_timedwait(int fd, void * msg, size_t len, int flags,
 
790
  struct timespec * timeout)
 
791
{
 
792
        char buf[STREAM_BUF_SIZE];
 
793
        struct T_unitdata_ind * ind;
 
794
        struct strbuf dataptr;
 
795
        struct strbuf ctlptr;
 
796
        int ret, prio;
 
797
 
 
798
        ctlptr.len = 0;
 
799
        ctlptr.maxlen = STREAM_BUF_SIZE;
 
800
        ctlptr.buf = buf;
 
801
 
 
802
        dataptr.maxlen = len;
 
803
        dataptr.len = 0;
 
804
        dataptr.buf = msg;
 
805
        
 
806
        prio = 0;
 
807
 
 
808
        ret = getmsg_timedwait(fd, &ctlptr, &dataptr, &prio, timeout);
 
809
        if (ret >= OK)
 
810
                ret = dataptr.len;
 
811
 
 
812
        return(ret);
 
813
}
 
814
 
 
815
/* ==========================================================================
 
816
 * recv()
 
817
 */
 
818
ssize_t recv(int fd, void * buf, size_t len, int flags,
 
819
  struct sockaddr * from, int * from_len)
 
820
{
 
821
        return(recv_timedwait(fd, buf, len, flags, NULL));
 
822
}
 
823
 
 
824
#endif
 
825
 
 
826
#if (!defined (HAVE_SYSCALL_SETSOCKOPT)) && defined(HAVE_STREAMS)
 
827
/* ==========================================================================
 
828
 * setsockopt()
 
829
 */
 
830
int setsockopt(int s, int level, int optname, const void *optval, int optlen)
 
831
{
 
832
        return(0);
 
833
}
 
834
#endif
 
835
 
 
836
struct foo {                    /* Used by getsockname and getpeername */
 
837
        long    a;
 
838
        int     b;
 
839
        struct sockaddr *name;
 
840
};
 
841
 
 
842
#if (!defined (HAVE_SYSCALL_GETSOCKNAME)) && defined(HAVE_STREAMS)
 
843
/* ==========================================================================
 
844
 * getsockname()
 
845
 */
 
846
 
 
847
 
 
848
int getsockname(int s, struct sockaddr *name, int *namelen)
 
849
{
 
850
        struct foo foo;
 
851
        int i;
 
852
        if (*namelen < sizeof(struct sockaddr)) {
 
853
                SET_ERRNO(ENOMEM);
 
854
                return(-1);
 
855
        }
 
856
        foo.a = 0x84;
 
857
        foo.b = 0;
 
858
        foo.name = name;
 
859
        i = ioctl(s, TI_GETMYNAME, &foo);
 
860
        *namelen = foo.b;
 
861
        return(i);
 
862
}
 
863
#endif
 
864
 
 
865
#if (!defined (HAVE_SYSCALL_GETPEERNAME)) && defined(HAVE_STREAMS)
 
866
/* ==========================================================================
 
867
 * getpeername() ; Added by Monty
 
868
 */
 
869
 
 
870
int getpeername(int s, struct sockaddr *name, int *namelen)
 
871
{
 
872
        struct foo foo;
 
873
        int i;
 
874
        if (*namelen < sizeof(struct sockaddr)) {
 
875
                SET_ERRNO(ENOMEM);
 
876
                return(-1);
 
877
        }
 
878
        foo.a = 0x84;                           /* Max length ? */
 
879
        foo.b = 0;                              /* Return length */
 
880
        foo.name = name;                        /* Return buffer */
 
881
        i = ioctl(s, TI_GETPEERNAME, &foo);
 
882
        *namelen = foo.b;
 
883
        return(i);
 
884
}
 
885
#endif
 
886
 
 
887
 
 
888
#if (!defined (HAVE_SYSCALL_SHUTDOWN)) && defined(HAVE_STREAMS)
 
889
/* ==========================================================================
 
890
 * shutdown()
 
891
 */
 
892
 
 
893
int shutdown(int s, int how)
 
894
{
 
895
        return(0);
 
896
}
 
897
#endif