• C

An EXEC call (UNIX)

Is there any way to know that exec failed.

Suppose i execute
        execvl("hi","hi","-la");
How do i know that this is invalid command, since it
does not return -1.

thanks      
LVL 1
danil082697Asked:
Who is Participating?
 
ahoffmannConnect With a Mentor Commented:
as julio said, exec* spawns a new process replacing the existing
(with the exec() call) and never returning, if it returns exec
failed anyhow.
Error conditions are placed in the common error variable.
So just put a printf behind and you'll see it, like:
 printf("exec failed with error %d\n", error);

Don't forget to have anywhere
      extern int error;
0
 
busukaCommented:
danil. I found only execl, execv, execle, execve, execlp and execvp functions.
Are you sure that you posted right name ? BTW, I suggest to use
spawn family instead of exec. Because in case of exec:
"A successful call to exec() does not return because the new
program overwrites the calling program."

This is result, taken from "man exec" command made on HP-UX.
0
 
julio011597Commented:
This is not a very meaningful answer.

That is, you are not supposed to get rid of exec*() calls, rather you should understand how they work.

If the exec*() call succedes, the new process image replaces the current process. I.e. successful calls to the exec functions do not return because the system overlays the calling process with the new process.

If the exec*() functions returns to the calling process image, an error has occurred; the return value is -1 (at least on my OS), and the function sets errno to indicate the error.

Have a look, on your OS, at exec(2) (man exec, or man 2 exec) for complete reference.

danil, if this is the correct answer, please reopen this question to me, thanks.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
busukaCommented:
julio, answer really answers ;) Take a look on original question:
"How do i know that this is invalid command, since it does not
return -1"
If it doesn't returns -1, that is standard for every OS I know
(maybe except VAX, where I have had troubles to find C compiler ;),
this means that danil actually ran some program "hi". We have
only one way to know that for sure: from danil's feedback.
0
 
julio011597Commented:
Yes, right... DANIL!

Joke apart, if you didn't think "execvl"  could be a typo (but how could the program compile at all then?), that's what i would have asked first, *before* answering.

My comment was mostly generated by your "I suggest to use spawn family instead of exec. Because...", which does not seem neither correct nor anyway an answer to me.

Yes, answer really answers ;-)
0
 
julio011597Commented:
Just an additional comment about real answers.

Sometimes users ask something which is simply meanigless. In those cases, IMO, you should tell them their very approach is wrong and give the correct one, instead of giving a *working* solution which keeps the wrong design.

But this is really meant as just my opinion.
0
 
danil082697Author Commented:
Actually I am wrigting a shell, and i am executing an
      status=execvp()
call, which really does not matter.
Anyway my status is different depending on command. For Ex:
"ls" -> status = 0;
"ls>out"status = 1350;/*some meanigless #*/
"hello" status = 256; /*hello does not exist, actually it                               always returns 256 if command is                               invalid*/
0
 
busukaCommented:
How you defined status ? Integer, signed/unsigned ? You print "status" with printf, right ?
What % you use ? status should be signed int and printf('Status= %i",status);
0
 
ozoCommented:
"ls>out" is unlikely to be a valid filename in your PATH
Did you check the value of errno after the execvp fails?
0
 
julio011597Commented:
Is this C-shell??
0
 
ahoffmannCommented:
julio, I meant C, you not?
0
 
julio011597Commented:
Actually, danil does not in his Tuesday, April 21 1998 - 08:27AM PDT comment.
BTW, i've never seen a common "error" variable in C.
0
 
ozoCommented:
I thought it was called "errno"
But if danil is expecting something to return, maybe system or fork is what's wanted?

0
 
ahoffmannCommented:
it's errno
Why doing a fork? exec* failed if it returns.
0
 
julio011597Commented:
With fork() you are actually spawning a child, and, in the parent, you not only know if the fork at all succeded, also will be able to get the child's exit value.

IMO, what ozo also points out is that danil should give some feedback.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.