1*433d6423SLionel Sambuc /* asyn_write() Author: Kees J. Bot
2*433d6423SLionel Sambuc * 7 Jul 1997
3*433d6423SLionel Sambuc */
4*433d6423SLionel Sambuc #include "asyn.h"
5*433d6423SLionel Sambuc #include <signal.h>
6*433d6423SLionel Sambuc
asyn_write(asynchio_t * asyn,int fd,const void * buf,size_t len)7*433d6423SLionel Sambuc ssize_t asyn_write(asynchio_t *asyn, int fd, const void *buf, size_t len)
8*433d6423SLionel Sambuc /* Nonblocking write(). (See asyn_read()). */
9*433d6423SLionel Sambuc {
10*433d6423SLionel Sambuc asynfd_t *afd;
11*433d6423SLionel Sambuc
12*433d6423SLionel Sambuc asyn->asyn_more++;
13*433d6423SLionel Sambuc
14*433d6423SLionel Sambuc if ((unsigned) fd >= FD_SETSIZE) { errno= EBADF; return -1; }
15*433d6423SLionel Sambuc afd= &asyn->asyn_afd[fd];
16*433d6423SLionel Sambuc
17*433d6423SLionel Sambuc if (!afd->afd_seen) {
18*433d6423SLionel Sambuc if ((afd->afd_flags= fcntl(fd, F_GETFL)) < 0) return -1;
19*433d6423SLionel Sambuc afd->afd_seen= 1;
20*433d6423SLionel Sambuc }
21*433d6423SLionel Sambuc
22*433d6423SLionel Sambuc if (afd->afd_state[SEL_WRITE] == PENDING) {
23*433d6423SLionel Sambuc sigset_t mask;
24*433d6423SLionel Sambuc ssize_t result;
25*433d6423SLionel Sambuc int err;
26*433d6423SLionel Sambuc
27*433d6423SLionel Sambuc sigemptyset(&mask);
28*433d6423SLionel Sambuc if (sigprocmask(SIG_SETMASK, &mask, &mask) < 0) return -1;
29*433d6423SLionel Sambuc (void) fcntl(fd, F_SETFL, afd->afd_flags | O_NONBLOCK);
30*433d6423SLionel Sambuc
31*433d6423SLionel Sambuc result= write(fd, buf, len);
32*433d6423SLionel Sambuc err= errno;
33*433d6423SLionel Sambuc
34*433d6423SLionel Sambuc (void) fcntl(fd, F_SETFL, afd->afd_flags);
35*433d6423SLionel Sambuc (void) sigprocmask(SIG_SETMASK, &mask, nil);
36*433d6423SLionel Sambuc
37*433d6423SLionel Sambuc errno= err;
38*433d6423SLionel Sambuc if (result != -1 || errno != EAGAIN) {
39*433d6423SLionel Sambuc afd->afd_state[SEL_WRITE]= IDLE;
40*433d6423SLionel Sambuc return result;
41*433d6423SLionel Sambuc }
42*433d6423SLionel Sambuc }
43*433d6423SLionel Sambuc
44*433d6423SLionel Sambuc afd->afd_state[SEL_WRITE]= WAITING;
45*433d6423SLionel Sambuc FD_SET(fd, &asyn->asyn_fdset[SEL_WRITE]);
46*433d6423SLionel Sambuc errno= EAGAIN;
47*433d6423SLionel Sambuc asyn->asyn_more--;
48*433d6423SLionel Sambuc return -1;
49*433d6423SLionel Sambuc }
50