Home > Archive > Unix Programming > November 2005 > how to know is the command exist at all?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to know is the command exist at all?
|
|
|
| Hello everybody,
I looked in google but could not find the answer to following question.
before executing an separate application via exec() function I want to
know is such application exist?
something like
const char* name;
......
if (is_application_exist(name))
exec(name);
The problem that I don know how to write this is_application_exist()
function?
Thanks
| |
| Thomas Maier-Komor 2005-11-28, 9:58 pm |
| ul wrote:
> Hello everybody,
>
> I looked in google but could not find the answer to following question.
>
> before executing an separate application via exec() function I want to
> know is such application exist?
> something like
>
> const char* name;
> .....
> if (is_application_exist(name))
> exec(name);
>
> The problem that I don know how to write this is_application_exist()
> function?
>
> Thanks
>
You can check the return value of exec, which you should do
anyway. Additionally, you might consider using access(2) to check
if the file exists and is executable.
Tom
| |
| Pascal Bourguignon 2005-11-28, 9:58 pm |
| "ul" <yuliy@gmx.de> writes:
> Hello everybody,
>
> I looked in google but could not find the answer to following question.
>
> before executing an separate application via exec() function I want to
> know is such application exist?
> something like
>
> const char* name;
> .....
> if (is_application_exist(name))
> exec(name);
>
> The problem that I don know how to write this is_application_exist()
> function?
The best way to implement is_application_exist() is to call exec.
You're forgetting that you are on a multi-user, multi-process system.
Between the time you test for the existance of a program and the time
you execute it, another process may have deleted it!
So you must just write it as:
if(execl(app_path,0)){
perror("Cannot exec the program.");
}/* else never occurs */
--
__Pascal Bourguignon__ http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
| |
| Lew Pitcher 2005-11-28, 9:58 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Pascal Bourguignon wrote:
> "ul" <yuliy@gmx.de> writes:
>
>
>
>
> The best way to implement is_application_exist() is to call exec.
[snip]
>
> if(execl(app_path,0)){
> perror("Cannot exec the program.");
> }/* else never occurs */
Given that, if the application /does/ exist, the execl(3) call will never
return, then this logic can be simplified a bit into
execl(app_path,0); /* never returns unless app_path not available */
perror("Cannot exec the program);
- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFDixigagVFX4UWr64RAiZ4AKCFra5+2HYt
/WkQfCUqzs/3YGz+TQCeOkrd
LzqKPdz8akUu+xufYPcwmKU=
=pziE
-----END PGP SIGNATURE-----
| |
| Gordon Burditt 2005-11-28, 9:58 pm |
| >I looked in google but could not find the answer to following question.
>
>before executing an separate application via exec() function I want to
>know is such application exist?
exec() doesn't run applications, it runs programs. An application
may require a whole bunch of programs and data files, and chances
are the calling code doesn't know what those are.
>something like
>
>const char* name;
>.....
>if (is_application_exist(name))
> exec(name);
>
>The problem that I don know how to write this is_application_exist()
>function?
There are several approaches:
- stat() the file and check the permissions and file type.
You can't exec() sockets or directories, for example.
Something with all the x bits reset cannot be exec()d (even by root).
- If running the program setuid is not an issue, use access() or eaccess()
with X_OK.
- Run it and if exec() returns, it failed.
None of these ensures that the file is actually an executable program
that can be executed on *this* platform, nor that all the prerequesite
shared libraries are present. In the case of shell scripts,
there's no assurance that the program after the #! exists.
A combined approach is best if you want to be thorough. Testing
with stat() eliminates the non-ordinary-file case. access()
eliminates the path-not-accessible case.
Gordon L. Burditt
| |
| SM Ryan 2005-11-28, 9:58 pm |
| Thomas Maier-Komor <maierkom@lpr.e-technik.no-spam.tu-muenchen.de> wrote:
# ul wrote:
# > Hello everybody,
# >
# > I looked in google but could not find the answer to following question.
# >
# > before executing an separate application via exec() function I want to
# > know is such application exist?
# > something like
# >
# > const char* name;
# > .....
# > if (is_application_exist(name))
# > exec(name);
# >
# > The problem that I don know how to write this is_application_exist()
# > function?
# >
# > Thanks
# >
#
# You can check the return value of exec, which you should do
Of course any return value of exec is an error return.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Elvis was an artist. But that didn't stop him from joining the service
in time of war. That's why he is the king, and you're a shmuck.
| |
| Mark Rafn 2005-11-28, 9:58 pm |
| ul <yuliy@gmx.de> wrote:
>before executing an separate application via exec() function I want to
>know is such application exist?
>something like
>
>const char* name;
>.....
>if (is_application_exist(name))
> exec(name);
There is no perfect way to do this. You can get close by searching the PATH
yourself (for an exec*p() function) and looking at stat() to to ensure that
it's a regular file and executable. This won't catch things marked
executable that don't actually run (due to corruption or whatnot).
It's also going to have a race condition: what happens if the executable is
deleted or made non-executable between your check and exec()?
>The problem that I don know how to write this is_application_exist()
>function?
In most cases, you don't actually need it. exec() has a return value that
tells you if it fails. Instead of
if (executable_exists(name)
execlp(name, ...); /* WRONG! fail to check return value */
else
handle_failure();
just do
if (execlp(name, ...) == -1)
handle_failure(errno);
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
| |
|
| ok
Thanks for everybody. yes it is look it is not trivial task, and in
case of multiuser system it is simply not predictable.
| |
| Thomas Maier-Komor 2005-11-29, 7:57 am |
| SM Ryan wrote:
> Thomas Maier-Komor <maierkom@lpr.e-technik.no-spam.tu-muenchen.de> wrote:
> # ul wrote:
> # > Hello everybody,
> # >
> # > I looked in google but could not find the answer to following question.
> # >
> # > before executing an separate application via exec() function I want to
> # > know is such application exist?
> # > something like
> # >
> # > const char* name;
> # > .....
> # > if (is_application_exist(name))
> # > exec(name);
> # >
> # > The problem that I don know how to write this is_application_exist()
> # > function?
> # >
> # > Thanks
> # >
> #
> # You can check the return value of exec, which you should do
>
> Of course any return value of exec is an error return.
>
That is obvious and correct. But some people assume that exec never
returns and get funny program behavior in the case of an exec error.
So what I meant is: don't assume that exec succeeds, and verify if
it returns, which of course is an error condition that must be handled
correctly, like any other error.
Tom
|
|
|
|
|