Solved

An EXEC call (UNIX)

Posted on 1998-04-19
15
315 Views
Last Modified: 2008-02-01
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      
0
Comment
Question by:danil082697
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 7

Expert Comment

by:busuka
Comment Utility
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
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
 
LVL 7

Expert Comment

by:busuka
Comment Utility
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
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
 
LVL 1

Author Comment

by:danil082697
Comment Utility
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
 
LVL 7

Expert Comment

by:busuka
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
"ls>out" is unlikely to be a valid filename in your PATH
Did you check the value of errno after the execvp fails?
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 150 total points
Comment Utility
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
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
Is this C-shell??
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
julio, I meant C, you not?
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
I thought it was called "errno"
But if danil is expecting something to return, maybe system or fork is what's wanted?

0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
it's errno
Why doing a fork? exec* failed if it returns.
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now