Re: stdin, stdout, write()

Chris Lightfoot <[email protected]>
Newsgroups gmane.comp.web.fastcgi.devel
Message-ID <tpu3eLIET84X.bdOlErnEPrcpK/[email protected]>
On Thu, Nov 02, 2006 at 05:19:11AM +0000, mal content wrote:
> Hello.
> 
> I use a stdio replacement library (similar to the one used in qmail).
> It deals in raw UNIX file descriptors, as opposed to stdio FILE
> pointers.
> 
> Is it going to be possible to use this library with fastcgi? fastcgi
> seems to require the use of FCGI_printf type functions. Currently,
> this code:
> 
> int main()
> {
>   while (FCGI_Accept() >= 0) {
>     write(1, "Content-type: text/plain\r\n\r\n", 28);
>     write(1, "hello\n", 6);
>   }
>   return 0;
> }
> 
> ..doesn't work ("incomplete headers (0 bytes) received from server").
> Do I have to do some sort of magic to get access to stdout, stderr, etc?

It won't work as stated there. The FCGI stdio functions
marshall the passed data into FastCGI wire packets between
the web server and the FastCGI app. This communication
takes place over a socket, not the normal 0/1/2 file
descriptors, and FCGI_stdio doesn't wrap it.

As a real hack, you could do something like this (note
that the semantics of my_write aren't quite correct):

FILE *fdfp[] = { stdin, stdout, stderr };
#define NFDFP   ((sizeof fdfp) / (sizeof fdfp[0]))

#define write(a, b, c) my_write((a), (b), (c))

ssize_t my_write(int fd, void *buf, size_t len) {
    size_t n;
    if (fd >= NFDFP || fd < 0) {
        errno = EBADFD;
        return -1;
    }
    if (0 == (n = fwrite(buf, 1, len, fdfp[fd])))
        return -1;
    return fflush(fdfp[fd]) == 0 ? (ssize_t)n : -1;
}

#define read(a, b, c)   my_read((a), (b), (c))

ssize_t my_read(int fd, void *buf, size_t len) {
    size_t n;
    if (fd >= NFDFP || fd < 0) {
        errno = EBADFD;
        return -1;
    }
    n = fread(buf, 1, len, fdfp[fd]);
    if (0 == n)
        return ferror(fdfp[fd]) ? -1 : 0;
    else
        return (ssize_t)n;
}

-- 
... You know you'll never walk alone 'cause you can't control your legs...
  (`Swallow Everything', The Mr. T Experience)
___________________________________
fastcgi-developers mailing list
http://fastcgi.com/fastcgi-developers/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.