Atish Kathpal
Atish Kathpal

Reputation: 691

Segmentation fault while initiating a C program through PHP

OS: ubuntu 11.10 Webserver: Apache Code: PHP

Hello I am trying to "exec" a C code through PHP web page. When I run the same C code directly on terminal, it works fine, but when I "exec" it through PHP, I get a segmentation fault.

Any idea why such behavior? My C code is doing small "malloc"s at a few places. The code never the less works fine on directly executing through terminal using ./a.out

Is there a way for me to gdb the C code, when PHP tries to execute it?

Thanks

Upvotes: 0

Views: 1127

Answers (3)

Mario
Mario

Reputation: 36517

Hard to tell without actually seeing the code. Are you sure your program doesn't leak? Are you able to add some debug console output to see when/where it crashes? Does your program try to access any ressources (like files, ports, etc.)? Are there sufficient rights for the web server's user (or whoever runs the php script) to actually execute it properly?

Upvotes: 0

boto
boto

Reputation: 455

What you could do is enabling core dump file creation and read the core dump into gdb after the exe crash. To enable core dump creation see what 'ulimit' does.

BTW: One possible reason for your program crash can be uninitialized variables, in particular pointer variables.

Upvotes: 0

asc99c
asc99c

Reputation: 3905

Most likely it is a user permissions error. Your web server will run as a different user (nobody, wwwrun or similar). Try doing an su to the web server user, and running the C program as that user.

Upvotes: 1

Related Questions