What do return codes mean? Well, for example, I run the executable file in php, it returns 139 to me.

<?php echo '<pre>'; system('/home/re1aps/insert', $retval); echo ' </pre>' <hr />Kod vozvrata: ' . $retval;' ?> 

The actual binary is:

 #include <mysql.h> int main(int argc, char **argv) { MYSQL *conn; int len, size; char data[1000*1024]; char chunk[2*1000*1024+1]; char query[1024*5000]; FILE *fp; conn = mysql_init(NULL); mysql_real_connect(conn, "localhost", "root", "", "bd", 0, NULL, 0); fp = fopen("test.jpg", "rb"); size = fread(data, 1, 1024*1000, fp); mysql_real_escape_string(conn, chunk, data, size); int val1= 1 ,val2= 2; char stat[] = "INSERT INTO img(id, pic, name) VALUES(\'%d\', \'%s\', \'%d\')"; len = snprintf(query, sizeof(stat)+sizeof(chunk) , stat, val1, chunk, val2); mysql_real_query(conn, query, len); fclose(fp); mysql_close(conn); } 

As a result, nothing starts and is not added. Yes, I know, all this can be done on pure php, but this is only part of the program, the main program is a complex calculation - with php this is either stupid or not realistic, rather the first. Yes, if you run everything from the console.

Ps But if I write an elementary code, then everything is ok. For example, I bring "hello world" to the console. The same is displayed in the browser, but it is worth a little more complicated and errors are added in the browser. Specifically, which were: 1, 127, 139, it seems to be the main ones, I did not find anything about them, what do they mean at all?

  • Not the fact that when you start from under the server, the system will allocate as much memory. In general, declaring arrays of this size is fundamentally wrong. Try to allocate memory dynamically and look at the error codes, maybe something will pop up here. - user6550
  • Yes, this code is nonsense, say a test file for the database. Hmm ... then it turns out that the binary in which the data on the picture is calculated will not start at all? Specifically, if you run that binary, then the code "1" is returned to the browser, nor can I understand what it means. - Re1aps
  • "Then it turns out that the binary in which the data on the picture is calculated will not start at all?" - I have no idea. If you try, then "Hello, world" will not start, but the program for calculating the route of the shuttle will work. Try to allocate memory dynamically, or simply reduce the appetites (what for is there so much? You can determine the need), it will be clear. - user6550
  • Although delete this code :) In general, you can argue about many things, a hint that if this code starts, then the other one will start too. But in general, I wouldn’t like to read about how to run the "program for calculating the shuttle route", can there be any universal option for running any applications? Yes, and here's another thing that seems to me - maybe my web server is incorrectly configured? it would not be bad if someone unsubscribed who actually ran the binary from under php. - Re1aps
  • Speaking of HW. The program #include <stdio.h> main () {printf ("Hello, World! \ N"); } contains a typical error - it does not return a return code . Correct: #include <stdio.h> main () {return printf ("Hello, World! \ N"), fflush (stdout); // fflush returns 0 on success} - avp


4 answers 4

Strange, of course, but with me at [purely for the test !!! :)]

 chmod 777 /var/www/ код на си: #include <stdlib.h> int main() { system("echo yap > test.txt "); return 0; } и код на php <?php ini_set('display_errors',1); error_reporting(E_ALL); $result = 0; $result = system("/var/www/a.out"); echo "out: ".$result."</br>"; ?> Выдает: out: 0 и в каталоге /var/www/ создается файл test.txt с текстом yap 

Look, maybe you have a problem with access rights. Moreover, the launch from the terminal is one user (re1aps) and it has its own rights and group in /home/re1aps .

    The return code is a number from 0 to 255 (obviously, it corresponds to -1), which is passed to the function (system call) exit, or returned by the return statement from main.

    In * nix, some return codes are described in /usr/include/sysexits.h

    Traditionally, a nonzero return code indicates an error. If you do not explicitly return the code (from a C / C ++ program), then it is random .

    Complete your program

      return 0; // OK или exit(0); // OK 
    • @avp, in this case, we don’t know what the system () function returned, so we don’t know how to interpret the $ retval value either :-) But I’m pretty sure that a binary with such a bunch of static data may not run under the server at all. - user6550
    • system obviously returns an exit code that returns the command it executes (your program) or -1 (it’s 255) if it cannot start the command (file not found, no rights, etc.) At the completion of the signal command, there may be some nuances (it is necessary to look specifically at PCP), but this is not the case here. - avp
    • added a retourne and in Hello world and the script described in the topic - there is no difference all the same. Code 139. What kind of code is this I xs ... now one idea came to try to implement. - Re1aps
    • @ Re1aps, in linux bash it issues such a code (echo $? Command at the end of the program), if you terminate its execution with signal 11 (kill -11 PID command from another window). This is a SIGSEGV signal, which usually occurs when a memory access error occurs (the program simply writes or reads to unavailable addresses). Why does this happen with php-> system-> helloworld ??? I do not know. - avp
    • @avp, do not forget that system () in PHP does not return the program termination code :) First, I would make sure that system ()! == FALSE, and if everything is OK, then you need to rewrite the shared code normally. That is, insert error checking when you call mysql _ * (), fopen (), when allocating memory ... And just by the results of this, think further. - user6550

    Error codes windows .
    Error code: 139 (0x008B) . Attempting to map a disk to a folder on a displayed disk.

    • what nonsense where do windows? - Re1aps
     #include <stdlib.h> #include <iostream> main() { system("/home/re1aps/insert"); std::cout << "hello angry php:(" << std::endl; return 0; } 

    I tried to run such a binary, well, the actual text prints, but does not execute systems. In the console, again, everything is ok.