Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

An EXEC call (UNIX)

Posted on 1998-04-19
15
Medium Priority
?
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 7

Expert Comment

by:busuka
ID: 1249852
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
ID: 1249853
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
ID: 1249854
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:julio011597
ID: 1249855
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
ID: 1249856
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
ID: 1249857
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
ID: 1249858
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
 
LVL 84

Expert Comment

by:ozo
ID: 1249859
"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
ID: 1249860
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
ID: 1249861
Is this C-shell??
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1249862
julio, I meant C, you not?
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1249863
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
ID: 1249864
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
ID: 1249865
it's errno
Why doing a fork? exec* failed if it returns.
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1249866
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

636 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