shafeeq
shafeeq

Reputation: 1599

System call is not working in cgi

I was starting a new project in CGI in fedora.Here is my code

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

int main(void) {

    printf("Content-type:text/html\r\n\r\n");

    printf("<!DOCTYPE html>\n");
    printf("<html>\n");
    printf("<title>EWN Lab</title>\n");
    printf("<meta charset=\"UTF-8\">\n");
    printf("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n");
    printf("<body class=\"w3-light-grey\">\n");

    system("ls");                 // Problem comes due to this line

    printf("</body>\n");
    printf("</html>\n");
    return 0;
}

When I compiled and executed the program using command line it works fine. But when I tried to execute via browser it show "Internal Server Error"

I am using apache server as web server. Any one know which user will execute this while running the program through web browser?

Upvotes: 0

Views: 96

Answers (1)

shafeeq
shafeeq

Reputation: 1599

My self found a solution for it. Thankyou @user3386109. Since the system command failing here, I wrote another function using pip. Now it's working fine.

The function with var-arg capability is shown below

void system_ext(const char *format, ...) {
    char* string;
    va_list args;

    va_start(args, format);
    if(0 > vasprintf(&string, format, args)) string = NULL;
    va_end(args);

    if(string) {
        char buff[VLONG_STR_LEN] = {0};
        FILE* file = popen(string, "r");
        while(fgets(buff, VLONG_STR_LEN, file) != NULL);
        pclose(file);
        free(string);
    } else {
        printf("Error on formating string.\n");
    }
}

Upvotes: 1

Related Questions