3 * _chkufb.c - return struct ufb * from a file handle (SAS/C)
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
13 #include <bsdsocket.h>
14 #include <sys/cdefs.h>
15 #include <amitcp/socketbasetags.h>
18 extern unsigned long __fmask
;
19 extern int (*__closefunc
)(int);
21 long ASM
fdCallback(REG(d0
) int fd
, REG(d1
) int action
);
24 * The initializator priority is just above the standard I/O, so that this
25 * will be called after the standard I/O is initialized
28 _STI_510_install_AmiTCP_callback(void)
30 if (SocketBaseTags(SBTM_SETVAL(SBTC_FDCALLBACK
), &fdCallback
, TAG_END
)) {
31 syslog(LOG_ERR
, "Cannot install fdCallback!");
32 #if __VERSION__ > 6 || __REVISION__ > 3
40 * Set up __closefunc (which is used at stdio cleanup)
42 __closefunc
= __close
;
45 * Set default file mask to UNIX style
53 fdCallback(REG(d0
) int fd
, REG(d1
) int action
)
59 syslog(LOG_INFO
, "fdCallback(fd: %d, action: %d)", fd
, action
);
68 if (!(ufb
->ufbflg
& UFB_SOCK
) && ufb
->ufbflg
!= 0) {
70 syslog(LOG_ERR
, "fdCallback: fd (%d) is not a socket!", fd
);
80 ufb
= __allocufb(&fd2
);
85 syslog(LOG_ERR
, "fdCallback: fd2(%d) > fd(%d)!", fd2
, fd
);
89 ufb
->ufbflg
= UFB_SOCK
| UFB_WA
| UFB_RA
; /* read/write socket */
90 ufb
->ufbfh
= NULL
; /* no file handle */
91 ufb
->ufbfn
= NULL
; /* no name */
97 if (ufb
!= NULL
&& ufb
->ufbflg
!= 0)
104 syslog(LOG_ERR
, "fdCallback: invalid action.");
116 /* a single element cache */
117 static struct UFB
*last_ufb
= NULL
;
118 static int last_fd
= -1;
122 if ((unsigned int)fd
>= __nufbs
) { /* unsigned cast checks for (fd < 0) */
128 * Check the cache first
133 last_fd
= fd
; /* update cache */
135 while (fd
> 0 && ufb
!= NULL
) {
139 last_ufb
= ufb
; /* update cache */
142 last_fd
= -1; /* invalidate cache */