kwaping has asked for the wisdom of the Perl Monks concerning the following question:

Well Monks, it's time for my first topic... I searched and did not find the answer - if you can direct me to an existing topic with the answer, I will be eternally grateful. With that said, here it goes.

I have a compiled C++ binary which initializes using files from the current working directory ".". I cannot change this, as it is a commercial product that I don't have the source for. What I'm trying to do is call this program from within Perl using a combination of chdir and backticks.

This works great from the command line. However, as a CGI, I'm getting an error that the initialization files can't be found. I've already checked permissions on all directories in the path, as well as on the files themselves - everything is in order there. I know the Perl script is in the proper working directory and that the program does indeed execute because I'm calling the binary application with a relative command, i.e. `./binary arg1 arg2`.

Any suggestions?? I'm in contact with their tech support staff but so far, no good.

Replies are listed 'Best First'.
Re: Calling a C++ binary from Perl
by sfink (Deacon) on May 13, 2005 at 22:29 UTC
    Assuming this is running under Linux or some other OS with strace, I'd replace the call to the binary with strace -o /tmp/curseword.log -s800 ./binary arg1 arg2 (the curse word should be chosen appropriately for the length of time you have been struggling with this problem.) Then grep through the resulting log for the files that it is unable to find, and look to see how it is trying to access them and what error code it's getting back.

    Assuming the worst case (namely, it accesses them with the expected relative paths and gets back ENOTFOUND), replace "binary" with a shell script that records its working directory to a log file before exec'ing the real, renamed binary with the given args.

    If the problem remains a mystery, you might try checking the sign of the Zodiac under which the computer was first booted. Watch out, though -- it's easy to be fooled by OS reinstalls and calendar drifts.

Re: Calling a C++ binary from Perl
by mrborisguy (Hermit) on May 13, 2005 at 20:36 UTC
    does your binary have relative paths to files? if you are calling it from a different directory, then maybe your effective directory is still in your cgi folder, and so the binary program is looking for files there, instead of somewhere relative to itself. just a thought.

    also, it looks like you've probably got this covered, but is it have something to do with the user? because most webservers use a different user (like 'nobody' or 'apache'), so you may want to make sure your webserver's user as the correct permissions.
      Yes, the binary does use relative paths, and I cannot change this fact unfortunately. I know the chdir command worked, because I invoke the c++ program by using a relative command (./programname).

      The permissions have been validated from / on down, including directories and files. Everything is either 755 or 644. Thanks for your suggestions though!
Re: Calling a C++ binary from Perl
by kwaping (Priest) on May 14, 2005 at 00:54 UTC
    Thanks for the suggestions, everyone! I ended up viewing the contents of all the files in the directory that appeared to be config files and noticed there was a reference to /tmp in there. I checked /tmp and found a file belonging to the c++ application that needed to be a+w but wasn't... So it was permissions after all!
      Don't do that. Chances are that the file was created while you were testing your app with your account, but if the app is supposed to be executed only by the apache/nobody/whatever CGI user just change the file owner leaving the permissions as they are were before (you probably can afford pretty the same result simply removing the file and letting the application re-create it from scratch).

      Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

      Don't fool yourself.
Re: Calling a C++ binary from Perl
by rir (Vicar) on May 13, 2005 at 21:15 UTC
    This sounds like a shell, environment issue. Rather than running your executable, run a shell and cd to where you need to be. This may have security implications. Something like system "(cd /your_bin; handle_cd_error; ./your_cmd; )";. Watch the security issues with system and with shelling-out.

    Be well,
    rir

    Update: My point invalidated by further info from kwaping.

Re: Calling a C++ binary from Perl
by kwaping (Priest) on May 14, 2005 at 17:40 UTC
    Thanks Steve and Flavio, I'll try those suggestions when I get back into work on Monday.