Drew Chapin
Drew Chapin

Reputation: 8009

CGI C output correct when executing from the command line, but not from web-request

Code:

#include <unistd.h>
#include <stdio.h>

typedef struct hddinfo
{
    char szDevice[4];
    char szMfg[64];
    char szModel[64];
    int nTemp;
} hddinfo;

int main( int argc, char *argv[] )
{

    setuid( 0 );

    hddinfo hdi[4] = {0};

    FILE *fp = popen("/usr/sbin/hddtemp /dev/sd[abcd]","r");
    if( NULL != fp )
    {

        for( int i = 0; i < 4; i++ )
            fscanf( fp,"/dev/%3[^:]: %64s %64[^:]: %d°C\n",&hdi[i].szDevice, &hdi[i].szMfg, &hdi[i].szModel, &hdi[i].nTemp );

        pclose( fp );

        printf( "Content-type: text/html\n\n" );
        printf( "<table id=\"hddtemp\">\n\t<tr>\n\t\t<th>Device</th>\n\t\t<th>Mfg</th>\n\t\t<th>Model</th>\n\t\t<th>Temp</th>\n\t</tr>\n" );
        for( int i = 0; i < 4; i++ )
            printf( "\t<tr>\n\t\t<td>%s</td>\n\t\t<td>%s</td>\n\t\t<td>%s</td>\n\t\t<td>%d&deg;C</td>\n\t</tr>\n", hdi[i].szDevice, hdi[i].szMfg, hdi[i].szModel, hdi[i].nTemp );
        printf( "</table>" );

    }
    else
        printf( "Status: 400 Bad Request\nContent-type: text/html\n\n" );

    return 0;
}

Output when run from command line as www-data user:

<table id="hddtemp">
    <tr>
        <th>Device</th>
        <th>Mfg</th>
        <th>Model</th>
        <th>Temp</th>
    </tr>
    <tr>
        <td>sda</td>
        <td>WDC</td>
        <td>WD2500AAKX-001CA0</td>
        <td>29&deg;C</td>
    </tr>
    <tr>
        <td>sdb</td>
        <td>WDC</td>
        <td>WD20EURS-63S48Y0</td>
        <td>30&deg;C</td>
    </tr>
    <tr>
        <td>sdc</td>
        <td>WDC</td>
        <td>WD20EURS-63S48Y0</td>
        <td>30&deg;C</td>
    </tr>
    <tr>
        <td>sdd</td>
        <td>WDC</td>
        <td>WD20EURS-63S48Y0</td>
        <td>30&deg;C</td>
    </tr>
</table>

Output when accessed through the web-browser:

<table id="hddtemp">
    <tr>
        <th>Device</th>
        <th>Mfg</th>
        <th>Model</th>
        <th>Temp</th>
    </tr>

    <tr>
        <td>sda</td>
        <td>WDC</td>
        <td>WD2500AAKX-001CA0</td>
        <td>29&deg;C</td>
    </tr>

    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>0&deg;C</td>
    </tr>
    <tr>
        <td></td>

        <td></td>
        <td></td>
        <td>0&deg;C</td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>

        <td>0&deg;C</td>
    </tr>
</table>

Upvotes: 1

Views: 216

Answers (2)

Employed Russian
Employed Russian

Reputation: 213935

It looks like you have non-ASCII format in your fscanf call (for the degree sign).

It is likely that that's where your parsing stops, due to LANG=C while executing from within CGI, while it is set to something else when executing on command line.

Upvotes: 2

Drew Chapin
Drew Chapin

Reputation: 8009

Okay, so I found the problem... Apparently whenever apache runs the program for a web-request, the degree character is not included, and a space character is include instead. I can also replicate that issue from the command line by piping the output to another application like so:

./hddtemp.cgi | tee

Removing the degree character fixes the output for web-requests, but then the problem occurs on command line calls. Looks like I'm stuck with one or the other working.

Upvotes: 1

Related Questions