Philip Hazel wrote:
> On Tue, 17 May 2005, Jeremy Harris wrote:
>
> > alarm(300) = 0
> > write(6, "mMR5AToGQBc3mGQLHkBBLmVYZAbZJ4iP"..., 8190) = 8190
> > alarm(0) = 300
> >
> > Could a select(or poll) for writability be used instead of
> > alarm, saving one syscall/cycle?
>
> I am a simple-minded programmer who likes to do things the easy way. As
> far as I can see, select() doesn't have an interface for asking "can I
> write 8190 bytes within 5 minutes?", which is what is needed here.
You need to do the following (non blocking descriptors rule):
fcntl(s, F_SETFL, O_NONBLOCK);
pollfd.fd = s
pollfd.events = POLLWRNORM | POLLERR | POLLHUP | POLLNVAL;
do {
read = fread(buffer,1,sizeof(buffer),spool_file);
if (read > 0) {
offset = 0;
again:
result = poll(&pollfd, 1, 300);
if (result == -1 && errno == EINTR)
continue;
else if (result < 1) {
if (result == -1)
log_write(0, LOG_MAIN|LOG_PANIC,
"%s on connection", strerror(errno));
else {
log_write(0, LOG_MAIN|LOG_PANIC, "timed out writing");
}
goto bail;
}
if (pollfd.revents & (POLLERR | POLLHUP | POLLNVAL))
goto bail;
wrote = send(s,buffer + offset,read - offset,0);
if (offset + wrote != read) {
offset += wrote;
goto again;
}
}
while (!feof(spool_file) && !ferror(spool_file));
...
bail:
// clean up.
Ian
--
Ian Freislich