[fpc-pascal] TProcess question

Michael Van Canneyt michael at freepascal.org
Sat Dec 13 14:50:30 CET 2008



On Fri, 12 Dec 2008, Bart wrote:

> Hi,
> 
> Just played with TProcess for the first time. (I come from Delphi 3)
> I observed some oddities (?, or maybe I'm just dumb ...).
> 
> If you try to execute a process that is non-executable (like for
> example a word doc), then on Windows an exception is raised and
> GetLastOSError returns 193.
> 
> If you try this on (my) linux, no exception is raised and a
> non-functional childprocess is forked (you can see it with ps ax).

This is because it is not waited for. 

> If you do this in a GTK 1 app, however, then the app gets killed.
> No errormessage, it just disappears.
> On the console you can se some errormessage:
> Xlib: unexpected async reply (sequence 0x4cf)
> 
> In GTK 2 app, there is no crash.
> 
> So 3 questions:
> 
> 1.
> Shouldn't trying to execute a non-executable file raise some kind
> of exception (like it does in Windows)?

You cannot do this reliably. Starting a new process in linux is a 2-step 
algorithm:
1 Fork a clone of the current process.
2 Execute the program that you want executed.
If step 2 fails, then there is no way to return an error message to
the parent process except a run-error code (127, I believe).


> If you try to execute a non-executable file from the console and query
> for the exitcode that bash returns, it will return an exitcode of 126,
> meaning: "Command invoked cannot execute" (Permission problem or
> command is not an executable)
> (This errorcode is not known to the syserrormessage function b.t.w.)

No, because this is bash-specific.

> 
> 2.
> Why does the GTK 1 app crash? This might be a GTK bug and not fpc related.

I don't think it is fpc related.

> 
> 3.
> As a way to work arond this (and at least preventing the crash): is
> there a reliable way of cheking that a file can be executed in the
> first place? I know I can check for the execute permission of the file
> in question, but that is not enough (on my system any file copied from
> my windows partition to linux will have the x permission set).

You can use the fpaccess() call with X_OK for this; But it is not a 
bullet-proof guarantee. You must know for sure which file exactly 
will be executed.

> 
> I tend to try to make my (silly) programs foolproof, so it would be
> nice if either TProcess.Execute raises an error for this or I can
> implement my workaraound, so I can present the user with an
> appropriate errormessage, and not the app crash on him, or in the eyes
> of the user do nothing at all, when the user expects it to execute
> some other program...

Well, you can try using fpaccess.

Michael.



More information about the fpc-pascal mailing list