Transferring data between PHP and C executables on Linux

On Linux, if I want to pass a clean string from PHP to C, how do I do this? what i tried to do far:

exec("./myexec.bin -a mystring"); 

in PHP and

 getopt(argc,argv, "a:"); 

in C

everything works, but when I pass strings longer than MAX_ARG_STRLEN (131072), it will no longer return 0, but returns 127, which was not found by the command.

Are there other ways to pass string data to linux executable? or is there a way to overcome MAX_ARG_STRLEN ?

+6
source share
4 answers

You can use popen() to open the channel for the executable:

 $fp = popen('./myexec.bin', 'w'); fwrite($fp, $data); pclose($fp); 

Then, as suggested earlier, read from stdin in your C program:

 fopen(stdin, "r"); // ... 

It is popen() use popen() rather than exec('/bin/echo') , because you can write characters that are otherwise interpreted by the shell (&, |, ...). Note that the handle returned from PHP popen() must be closed with pclose() .

+6
source

Several spring options to recall right away:

  • Store the data in a file and pass the file name on the command line. It's easy and simple, but requires permission to create and store files somewhere in the file system.

  • Open the handset between your program and program C; leave both processes running, at least until C has absorbed the entire contents of your line. popen() is a convenient wrapper around this approach, but assumes that standard input is the correct destination, and it is unidirectional. Managing the pipes themselves allows you to use a different file descriptor - and you can tell the child which file descriptor to read through the command line argument. (See gpg(1) command line --passphrase-fd to understand what I mean.)

  • Use the SysV or POSIX shared memory section to store data in PHP and then join the shared memory segment from your C to read the contents. Please note that the shared memory segments are saved, so you must clear them after completion, otherwise you will be a memory leak. This does not require permissions to create files in the file system and can be a more convenient mechanism than working with pipes, and support both processes long enough to be able to completely write data, and the other is to read the data completely.

    / li>
+3
source

If this looks more like a data structure than a string, what about using the embedded web server? At first glance this may seem redundant for your purpose, but mongoose, for example, is a very lightweight embedded web server:

http://code.google.com/p/mongoose/

There is also a good tutorial on the same issue as yours, data transfer between a PHP application and a C / C ++ application. It's in German, though ... but maybe a Google translator can help:

http://blog.aditu.de/2010/05/15/serverbridge-zwischen-php-und-cc/

+1
source

try using echo and output the output to your C executable instead of using args:

exec("/bin/echo | ./myexec.bin");

like @sarnold mentioned in the comments incorrectly. See @Linus Kleen answer .

in your C program:

 fopen(stdin, "r"); // ... 
0
source

Source: https://habr.com/ru/post/901877/


All Articles