Search code examples
c++cgiopenwrtluci

Luci unable to launch the requested CGI program: /www/cgi-bin/sample.cgi: Exec error


I tried to execute a Cgi-Cpp program on a uhttpd server running on an OpenWRT install. The Cgi file is /www/cgi-bin/sample.cgi and has execute permissions too. I am trying to access this Cgi with HTML file. This is my sample.cgi file:

#include <iostream>
#include <vector>  
#include <string>  
#include <stdio.h>  
#include <stdlib.h> 

#include <cgicc/CgiDefs.h> 
#include <cgicc/Cgicc.h> 
#include <cgicc/HTTPHTMLHeader.h> 
#include <cgicc/HTMLClasses.h>  

using namespace std;
using namespace cgicc;

int main () {
   Cgicc formData;

   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>Using GET and POST Methods</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";

   form_iterator fi = formData.getElement("first_name");  

   if( !fi->isEmpty() && fi != (*formData).end()) {  
      cout << "First name: " << **fi << endl;  
   }else{
      cout << "No text entered for first name" << endl;  
   }
   cout << "</body>\n";
   cout << "</html>\n";

   return 0;
} 

I get an error when I click the submit button in HTML:

unable to launch the requested CGI program: /www/cgi-bin/sample.cgi: Exec error

What am I missing? All permission are set there. Any suggestions?


Solution

  • Make sure you are building the program correctly. First open a terminal window and cd to /www/cgi-bin. Now use

    ls -l 
    

    to list the programs in the directory. You should have see your sample.cpp file there. If it is not there, move it there. If you no longer have a sample.cpp file, create a text file using the code you originally posted. You can do this with a text editor like nano.

    nano sample.cpp
    

    Type or paste in your source file. Then press ctrl-x to save and exit the program. Make sure you tell nano to save the program and not just exit.

    Next delete your existing sample.cgi. Do not delete sample.cpp.

    Use this command to build the source code (sample.cpp) into a binary executable file:

    g++ sample.cpp -o sample.cgi
    

    The "-o" is the letter "o", not the number "0". If building throws compilation errors, fix the errors and repeat until you successfully build the program. You will now have a binary file "sample.cgi".

    Make sure "sample.cgi" is executable. If not, then make it executable with

    sudo chmod a+x sample.cgi
    

    Make sure you can first execute sample.cgi from the command line. If you cannot do this, then the server won't be able to either. Then type

    "./sample.cgi". 
    

    You should see your html you sent from your app in your terminal window after you executed the app.

    Let us know if you need further help.