C++/Solaris: retrieving args from an execv() call - and I am an idiot with pointers

C++/Solaris: retrieving args from an execv() call - and I am an idiot with pointers

Post by M Herriot » Fri, 27 Feb 2004 10:56:31



Hello to all -

#1: I would have rather used an AND with the choice of newsgroups. I
would like to talk to those who subscribe to comp.lang.c++ AND
comp.unix.solaris - but I'm stuck with OR. Please accept my sincerest
apologies for what I am about to do, I know it is, in the purest sense,
incorrect.

Having said that much, let me describe my problem. I am running two
processes on a Solaris server. Process 1 forks and calls process 2 using
execv(). All well and good. The problem comes in when I try and pull the
  passed args from the pointer array - I get nonsense where once there
was sense.

Now, I am full willing to admit I'm not sure what exactly I am doing
here. I believe my pointers have gotten fouled up, and I've been staring
at them for so long that they have ceased to mean anything. Please advise:

// Process 1

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <iostream.h>

void main()
{
     int forkA, proc_id00, sleep_err, kill_err;
     int time = 3;
     int kill_sig = 9;

     char *argv[3];

     argv[0] = "one";
     argv[1] = "two";
     argv[2] = NULL;

     forkA = fork ();

     if (forkA == 0)
     {
        int error = 0;
        int halftime = 5;
        error = execv ("testit2", argv);
        sleep_err = sleep(halftime);
        exit (101);
     }

     else
     {
        sleep_err = sleep(time);
        kill_err = kill (forkA, kill_sig);
        exit (100);
     }

Quote:}

// Process 2

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <iostream.h>

void main(int arg_count, char *arg_val)
{
     char **argv = &arg_val;
     int i = 0;

     cout << "Program 2: ";

     while (i < arg_count)
     {
        cout << argv[i++];
     }

     cout << endl;

     exit(103);

Quote:}

My output looks like this: Program 2: ??????(null)
 
 
 

C++/Solaris: retrieving args from an execv() call - and I am an idiot with pointers

Post by Rich Tee » Fri, 27 Feb 2004 11:48:23



> // Process 1

> #include <sys/types.h>
> #include <sys/wait.h>
> #include <signal.h>
> #include <unistd.h>
> #include <iostream.h>

> void main()

This is your first mistake: main() is NEVER void.  It should
ALWAYS be defined as returning an int.

Quote:> {
>      int forkA, proc_id00, sleep_err, kill_err;

The value returned by fork() has a type of pid_t, not int.

Quote:>      int time = 3;
>      int kill_sig = 9;

>      char *argv[3];

>      argv[0] = "one";
>      argv[1] = "two";
>      argv[2] = NULL;

>      forkA = fork ();

>      if (forkA == 0)
>      {
>    int error = 0;
>    int halftime = 5;
>    error = execv ("testit2", argv);

Is testit2 in your PATH?  If so, that means that "." is in it,
which is a security no-no.

Quote:>    sleep_err = sleep(halftime);
>    exit (101);

If execv() is successful, those last two lines will never get
executed.  Also, _exit () should be used here, and why are you
sleepiing?

Quote:>      else

WHat about if fork() fails, returning -1?

Quote:>      {
>    sleep_err = sleep(time);
>    kill_err = kill (forkA, kill_sig);
>    exit (100);

Why are you sleeping and sending a kill signal to the child?
Calling wait or waitpid is sufficient.

Quote:> #include <sys/types.h>
> #include <sys/wait.h>
> #include <signal.h>
> #include <unistd.h>
> #include <iostream.h>

> void main(int arg_count, char *arg_val)

There's that "void main" again - and is there any reason why
you're not using the conventoinal variable names of argc and
argv here?  Whatever, "arg_val" should be a "char **", not a
"char *".

Quote:> {
>      char **argv = &arg_val;
>      int i = 0;

>      cout << "Program 2: ";

>      while (i < arg_count)
>      {
>    cout << argv[i++];
>      }

Might I suggest using a for loop instead of a while?  E.g.,

        for (i = 0; i < arg_count; i++)
                cout << argv[i];

In fact, I'd writ the whole thing lijke this:

int main (int argc, char **argv)
{
        int i;

        cout << "Program 2: ";

        for (i = 0; i < argc; i++)
                cout << argv[i];

        cout << endl;

        return (103);

Quote:}

(Using return instead of exit here keeps the compiler happy.)

HTH,

--
Rich Teer, SCNA, SCSA

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-online.net

 
 
 

C++/Solaris: retrieving args from an execv() call - and I am an idiot with pointers

Post by Artie Gol » Fri, 27 Feb 2004 11:48:41



> Hello to all -

> #1: I would have rather used an AND with the choice of newsgroups. I
> would like to talk to those who subscribe to comp.lang.c++ AND
> comp.unix.solaris - but I'm stuck with OR. Please accept my sincerest
> apologies for what I am about to do, I know it is, in the purest sense,
> incorrect.

There are many problems with your code, but I'll only point out the
specific one that's causing you this particular problem.

[snip]

Quote:

> void main(int arg_count, char *arg_val)

int main(int arg_count, char **arg_val)

Quote:> {
>     // char **argv = &arg_val;
>     int i = 0;

>     cout << "Program 2: ";

>     while (i < arg_count)
>     {
>     cout << arg_val[i++];
>     }

>     cout << endl;

>     exit(103);
> }

> My output looks like this: Program 2: ??????(null)

HTH,
--ag

--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"

 
 
 

C++/Solaris: retrieving args from an execv() call - and I am an idiot with pointers

Post by dsgfd » Fri, 27 Feb 2004 19:48:44


| Hello to all -
|
| #1: I would have rather used an AND with the choice of newsgroups. I
| would like to talk to those who subscribe to comp.lang.c++ AND
| comp.unix.solaris - but I'm stuck with OR. Please accept my sincerest
| apologies for what I am about to do, I know it is, in the purest sense,
| incorrect.
|
| Having said that much, let me describe my problem. I am running two
| processes on a Solaris server. Process 1 forks and calls process 2 using
| execv(). All well and good. The problem comes in when I try and pull the
|   passed args from the pointer array - I get nonsense where once there
| was sense.
|
| Now, I am full willing to admit I'm not sure what exactly I am doing
| here. I believe my pointers have gotten fouled up, and I've been staring
| at them for so long that they have ceased to mean anything. Please advise:
|
| // Process 1
|
| #include <sys/types.h>
| #include <sys/wait.h>
| #include <signal.h>
| #include <unistd.h>
| #include <iostream.h>
|
| void main()
| {
|      int forkA, proc_id00, sleep_err, kill_err;
|      int time = 3;
|      int kill_sig = 9;
|
|      char *argv[3];
|
|      argv[0] = "one";
|      argv[1] = "two";
|      argv[2] = NULL;
|
|      forkA = fork ();
|
|      if (forkA == 0)
|      {
|       int error = 0;
|       int halftime = 5;
|       error = execv ("testit2", argv);
|       sleep_err = sleep(halftime);
|       exit (101);
|      }
|
|      else
|      {
|       sleep_err = sleep(time);
|       kill_err = kill (forkA, kill_sig);
|       exit (100);
|      }
| }
|
| // Process 2
|
| #include <sys/types.h>
| #include <sys/wait.h>
| #include <signal.h>
| #include <unistd.h>
| #include <iostream.h>
|
| void main(int arg_count, char *arg_val)
| {
|      char **argv = &arg_val;
|      int i = 0;
|
|      cout << "Program 2: ";
|
|      while (i < arg_count)
|      {
|       cout << argv[i++];
|      }
|
|      cout << endl;
|
|      exit(103);
| }
|
|
| My output looks like this: Program 2: ??????(null)
|
|

1234

 
 
 

C++/Solaris: retrieving args from an execv() call - and I am an idiot with pointers

Post by M Herriot » Fri, 27 Feb 2004 12:16:53


Rich, Artie -

Your help has been exactly what I have needed. Thank you so much for
your efforts. The code works now - or at least what parts of the code
you corrected. I will try and incorporate the rest of your suggestions
throughout this program.

Thank you again for your comments,

- Mark Herriott

 
 
 

1. System call on Solaris 2.8 to retrieve current user's password

Unix Gurus,

I have to implement a "C" program which retrieves current user
password and compares it with current input user password and do some
kind of checking on it like it's same as the current stored user
password etc. This program will be a wrapper on unix passwd program
and wrapper program will run as setuid-bit-set as root. So please let
me know if there is someway I can retrieve current stored password of
a user on Solaris 2.8 through some system call.

Any help/pointers will be greatly appreciated.

Thank you
Jitendra

2. Connecting a Linux machine to an NT Proxy server

3. setting environment before calling execv()

4. BackUp/Tape Devices

5. C++ - Alternatives for execv() & friends

6. Understanding bad blocks (ESDI/others)

7. I am an idiot.

8. Please help: no X

9. Am I just an idiot or the index page cant be in php

10. Pointer on char truncation while calling Pro*C 9.2 function on Solaris 8 !?

11. HELP - Im am a complete idiot!!

12. IPNAT Not working I am an idiot

13. I am an admitted unix idiot-please help!