RE: Problems with a C application that changes users and run 'screen-x'

2002-12-27 Thread Andrew Prewett
On Dec 20 Paul Everlund wrote:

 On Fri, 20 Dec 2002, Paul Everlund wrote:

 Found an error in my reply...

  On Fri, 20 Dec 2002, Aaron Burke wrote:
 
 [big snip]

  I think execlp is writing over your current process. So first your
  process is exchanged with ppp, then ppp is exchanged with screen. You
  have to make a copy of your current process, a.out, by using fork, and
  then exchange the process image in this copy using execlp.

 Correction... Your a.out process is replaced with ppp, then nothing
 else happens, as screen never is called du to the replacement.

 the process image replaced with su and the second execlp() newer called
 if the first execlp() call succeeds... (which won't) else replaced with
 screen if the second execlp() call succeeds (which won't)...
 (if exec??() returns, then an error has occured)

 Here is some code for the OP to start with:

#include sys/types.h
#include sys/wait.h
#include unistd.h
#include stdio.h
#include sysexits.h

int main(void)
{
pid_t pid;
int s;

switch (pid = fork()) {
case -1:
perror(fork);
exit(EX_OSERR);
case 0: /* I'm the child. */
execlp(/usr/bin/su, /usr/bin/su,
arg1, arg2, argn, NULL);
/* kaboom */
/* perror(execlp); */
exit(EX_SOFTWARE);
default: /* I'm the parent */
waitpid(pid, s, 0);
break;
}
return WEXITSTATUS(s);
}

-andrew


To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message



RE: Problems with a C application that changes users and run 'screen -x'

2002-12-20 Thread Aaron Burke
 You're not running the executable as `root'.  Since you are not the
 superuser, you do not have permissions to operate on the pseudo-tty
 that login attempts to work with, and this is why you get the
 following error message:

This is as I expected. And I dont know of a way to get around
it.

 
  Cannot open your terminal '/dev/ttyp0' - please check.
 
 Three possibilities that you might wish to investigate further are:
 
 1. Write a shell script that does the equivalent of the system() call
you are using now.  This should be fairly easy and will work fine
if you execute the script from a root shell.

I dont think that this will work. The super-user has nothing to do
with the process that needs to be run. The user that logs in is not
privliged, and the account that he is becomming is not privlidged
either.

 
 2. Fix your program by removing the bad use of `'.

Done. Thanks for the comment on this. I noticed a warning from g++ about
this today.

 
 3. Avoid using system() which I vaguely recall being described with a
lot of bad words in various places and use fork(), exec(), _exit(),
waitpid() and exit() instead.

How would I do this with exec. According to the man page for exec
I have only a few options.
 int execl(const char *path, const char *arg, ...);
 int execlp(const char *file, const char *arg, ...);
 int execle(const char *path, const char *arg, ...);
 int exect(const char *path, char *const argv[], 
   char *const envp[]);
 int execv(const char *path, char *const argv[]);
 int execvp(const char *file, char *const argv[]);

Can you point me to the right documentation to learn about
the exec functions provided by unistd.h?

Allthough I am not familiar with unistd.h at all, I did do
a little bit of expermentation.

Here is my new code:

#include stdio.h
#include stdlib.h
#include unistd.h

int main(int argc, char* pszArgs[])
{
int result, result2;
result= execlp(/usr/bin/su, ppp, -m);
result2=execlp(/usr/local/bin/screen, -x);
return result + result2;
}
bash-2.05$ g++ run-ppp.c
bash-2.05$ ./a.out
bash-2.05$

I am a little supprised that nothing appeared to have happened.
Perhaps I am running these improperly. Am I using the correct
exec command? Can you demonstrate how this should work? 
What else could execlp(args) needs to say?

 
 - Giorgos
 

Thanks for your time.

To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message



RE: Problems with a C application that changes users and run 'screen-x'

2002-12-20 Thread Paul Everlund
On Fri, 20 Dec 2002, Aaron Burke wrote:

  3. Avoid using system() which I vaguely recall being described with a
 lot of bad words in various places and use fork(), exec(), _exit(),
 waitpid() and exit() instead.

 How would I do this with exec. According to the man page for exec
 I have only a few options.
  int execl(const char *path, const char *arg, ...);
  int execlp(const char *file, const char *arg, ...);
  int execle(const char *path, const char *arg, ...);
  int exect(const char *path, char *const argv[],
  char *const envp[]);
  int execv(const char *path, char *const argv[]);
  int execvp(const char *file, char *const argv[]);

 Can you point me to the right documentation to learn about
 the exec functions provided by unistd.h?

 Allthough I am not familiar with unistd.h at all, I did do
 a little bit of expermentation.

 Here is my new code:

 #include stdio.h
 #include stdlib.h
 #include unistd.h

 int main(int argc, char* pszArgs[])
 {
 int result, result2;
 result= execlp(/usr/bin/su, ppp, -m);
 result2=execlp(/usr/local/bin/screen, -x);
 return result + result2;
 }
 bash-2.05$ g++ run-ppp.c
 bash-2.05$ ./a.out
 bash-2.05$

 I am a little supprised that nothing appeared to have happened.
 Perhaps I am running these improperly. Am I using the correct
 exec command? Can you demonstrate how this should work?
 What else could execlp(args) needs to say?

 
  - Giorgos
 

 Thanks for your time.

I think execlp is writing over your current process. So first your
process is exchanged with ppp, then ppp is exchanged with screen. You
have to make a copy of your current process, a.out, by using fork, and
then exchange the process image in this copy using execlp.

I suggest you read more about those functions Giorgos mentioned: fork,
execlp, waitpid, and exit.

Best regards,
Paul


To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message



RE: Problems with a C application that changes users and run 'screen-x'

2002-12-20 Thread Paul Everlund
On Fri, 20 Dec 2002, Paul Everlund wrote:

Found an error in my reply...

 On Fri, 20 Dec 2002, Aaron Burke wrote:

[big snip]

 I think execlp is writing over your current process. So first your
 process is exchanged with ppp, then ppp is exchanged with screen. You
 have to make a copy of your current process, a.out, by using fork, and
 then exchange the process image in this copy using execlp.

Correction... Your a.out process is replaced with ppp, then nothing
else happens, as screen never is called du to the replacement.

 Best regards,
 Paul

Best regards,
Paul


To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message



Re: Problems with a C application that changes users and run 'screen -x'

2002-12-19 Thread Giorgos Keramidas
On 2002-12-19 02:50, Aaron Burke [EMAIL PROTECTED] wrote:
 I have an application that simply logs in as another user and runs
 screen -x. The problem I am having with the followin code is that
 the results of execution is a message from (I am guessing the shell)
 saying that I dont have access to the /dev/ttyp? where ? is the
 current virtual terminal that I am running on.

 Here is my application:

 #include stdlib.h
 int main(int argc, char* pszArgs[])
 {
 int result;
 result= system(/usr/bin/su ppp -m --login -c  
  /usr/local/bin/screen -x);
 return result;
 }

You are using the  operator in a very strange manner here.  It most
certainly doesn't work the way you might think it does.

You should also avoid using system, if possible.

 And here is the output:
 bash-2.05$ ./a.out

You're not running the executable as `root'.  Since you are not the
superuser, you do not have permissions to operate on the pseudo-tty
that login attempts to work with, and this is why you get the
following error message:

 Cannot open your terminal '/dev/ttyp0' - please check.

Three possibilities that you might wish to investigate further are:

1. Write a shell script that does the equivalent of the system() call
   you are using now.  This should be fairly easy and will work fine
   if you execute the script from a root shell.

2. Fix your program by removing the bad use of `'.

3. Avoid using system() which I vaguely recall being described with a
   lot of bad words in various places and use fork(), exec(), _exit(),
   waitpid() and exit() instead.

- Giorgos


To Unsubscribe: send mail to [EMAIL PROTECTED]
with unsubscribe freebsd-questions in the body of the message