Reputation: 8009
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°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°C</td>
</tr>
<tr>
<td>sdb</td>
<td>WDC</td>
<td>WD20EURS-63S48Y0</td>
<td>30°C</td>
</tr>
<tr>
<td>sdc</td>
<td>WDC</td>
<td>WD20EURS-63S48Y0</td>
<td>30°C</td>
</tr>
<tr>
<td>sdd</td>
<td>WDC</td>
<td>WD20EURS-63S48Y0</td>
<td>30°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°C</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>0°C</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>0°C</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>0°C</td>
</tr>
</table>
Upvotes: 1
Views: 216
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
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