BCHS LogoBCHS stack

BSD, C, httpd, SQLite: start, tools, example.
    1 #include <err.h>
    2 #include <stdlib.h>
    3 #include <stdio.h>
    4 #include <unistd.h>
    5 
    6 int
    7 main(void)
    8 {
    9     if (-1 == pledge("stdio", NULL)) 
   10         err(EXIT_FAILURE, "pledge");
   11     puts("Status: 200 OK\r");
   12     puts("Content-Type: text/html\r");
   13     puts("\r");
   14     puts("Hello, haters!");
   15     return(EXIT_SUCCESS);
   16 }

This is your code. Read it: it’s exactly what’s going to happen. No mysticism.

What’s going on? pledge(2) makes sure you’re only doing what you want to do: work with existing descriptors. The rest is just HTTP. Don’t want to work with HTTP? Use kcgi(3) or equivalent.

    1 server "localhost" {
    2     listen on $ext_addr port 80
    3     location "/cgi-bin/*" {
    4         fastcgi
    5         root "/"
    6     }
    7 }

httpd.conf(5) is so… simple. Server tells us the name. Listen tells us to listen. Location tells us a location. Where we have FastCGI. Relative to the root.

Don’t like FastCGI? Neither do we. But we’re not using it for this example: in the next snippet, we’ll use slowcgi(8) to invoke our CGI. So relax.

    1 #! /bin/sh
    2 cc -static -g -W -Wall -o cgi cgi.c
    3 doas install -o www -g www -m 0500 cgi /var/www/cgi-bin
    4 doas rcctl enable slowcgi
    5 doas rcctl start slowcgi
    6 doas rcctl check slowcgi
    7 doas rcctl restart httpd

Wow. Very configure. Compiles the script. Installs it. Makes sure slowcgi(8) and of course httpd(8) are running. Done.

Ready for more examples? See Secure CGI Applications in C on BSD.