1

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>
0

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah!!!! Now I understand why it's putting the degree sign when run from the command line, and not when it's piped, or called from the web-service. It seem that hddtemp is checking the LANG environment variable and determining whether to put the degree symbol or not. So, if I had changed my command call to LANG=C /user/sbin/hddtemp /dev/sd[abcd]", and removed the degree symbol, it would have worked for both!
1

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.

2 Comments

A better options seems to be for me to specify the --numeric flag on hddtemp, but that does mean I lose getting the mfg and sku/model
appreciate your dedication in solving your own problem and also posting your thoughts for others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.