Re: How does Sendmail know how it was invoked?

2007-08-04 Thread Don Hinton
On Saturday 04 August 2007 13:06:34 RW wrote:
 mailwrapper checks to see how it was invoked and then looks up the
 appropriate command in mailer.conf.   All of the entries in mailer.conf
 point to /usr/libexec/sendmail/sendmail, so how does that binary know what
 it's supposed to do.

It checks argv[0], i.e., the name used to invoke it.  Here's a simple program 
demostrating it:

#include iostream
int main (int argc, char* argv[])
{
  std::cout  my name is:   argv[0]  std::endl;
  return 0;
}

Save it to a file and do the following:
$ c++ -o foo file.cxx
$ ./foo
my name is: ./foo
$ mv foo bar
$ ./bar
my name is: ./bar

hth...
don

 I'm just curious.
 ___
 freebsd-questions@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-questions
 To unsubscribe, send any mail to
 [EMAIL PROTECTED]



-- 
Don Hinton don.hinton at vanderbilt.edu or hintonda at gmail.com
Institute for Software Integrated Systems (ISIS), Vanderbilt University
tel: 615.480.5667 or 615.870.9728
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: How does Sendmail know how it was invoked?

2007-08-04 Thread Dan Nelson
In the last episode (Aug 04), RW said:
 mailwrapper checks to see how it was invoked and then looks up the
 appropriate command in mailer.conf.  All of the entries in
 mailer.conf point to /usr/libexec/sendmail/sendmail, so how does that
 binary know what it's supposed to do.

The kernel passes the executable name to the running process along with
the rest of the commandline arguments.  If you run ls -l /tmp, for
example, the ls binary gets ls, -l, and /tmp as its arguments. 
See around line 360 of src/contrib/sendmail/src/main.c.

http://www.freebsd.org/cgi/cvsweb.cgi/src/contrib/sendmail/src/main.c?annotate=HEAD

-- 
Dan Nelson
[EMAIL PROTECTED]
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: How does Sendmail know how it was invoked?

2007-08-04 Thread RW
On Sat, 4 Aug 2007 13:23:07 -0500
Dan Nelson [EMAIL PROTECTED] wrote:

 In the last episode (Aug 04), RW said:
  mailwrapper checks to see how it was invoked and then looks up the
  appropriate command in mailer.conf.  All of the entries in
  mailer.conf point to /usr/libexec/sendmail/sendmail, so how does
  that binary know what it's supposed to do.
 
 The kernel passes the executable name to the running process along
 with the rest of the commandline arguments.  If you run ls -l /tmp,
 for example, the ls binary gets ls, -l, and /tmp as its
 arguments. See around line 360 of src/contrib/sendmail/src/main.c.


Yes, I understand that. When you type mailq, mailwrapper's argv[0] will
contain mailq. but then mailwrapper looks-up mailq in mailer.conf
and runs /usr/libexec/sendmail/sendmail. So when sendmail checks it's
argv[0] I was assuming that it would see sendmail.

What I didn't get was that when a binary is executed from execve(), it's
the parent program that sets the argv[0] seen by the child, and not
the kernel.


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: How does Sendmail know how it was invoked?

2007-08-04 Thread Don Hinton
On Saturday 04 August 2007 15:13:34 RW wrote:
 On Sat, 4 Aug 2007 13:23:07 -0500

 Dan Nelson [EMAIL PROTECTED] wrote:
  In the last episode (Aug 04), RW said:
   mailwrapper checks to see how it was invoked and then looks up the
   appropriate command in mailer.conf.  All of the entries in
   mailer.conf point to /usr/libexec/sendmail/sendmail, so how does
   that binary know what it's supposed to do.
 
  The kernel passes the executable name to the running process along
  with the rest of the commandline arguments.  If you run ls -l /tmp,
  for example, the ls binary gets ls, -l, and /tmp as its
  arguments. See around line 360 of src/contrib/sendmail/src/main.c.

 Yes, I understand that. When you type mailq, mailwrapper's argv[0] will
 contain mailq. but then mailwrapper looks-up mailq in mailer.conf
 and runs /usr/libexec/sendmail/sendmail. So when sendmail checks it's
 argv[0] I was assuming that it would see sendmail.

 What I didn't get was that when a binary is executed from execve(), it's
 the parent program that sets the argv[0] seen by the child, and not
 the kernel.

Sorry, I should have paid closer attention to your question and actually 
looked at the code to see what they were doing in this specific case.

They original args, including argv[0], are passed as args parameter to execve.  
So from the perspective of the called application, the original argv[0] is 
now argv[1].  

Take a look at how mailwrapper.c uses the arglist structure.

http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.sbin/mailwrapper/mailwrapper.c?rev=1.11;content-type=text%2Fplain

hth...
don



 ___
 freebsd-questions@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-questions
 To unsubscribe, send any mail to
 [EMAIL PROTECTED]



-- 
Don Hinton don.hinton at vanderbilt.edu or hintonda at gmail.com
Institute for Software Integrated Systems (ISIS), Vanderbilt University
tel: 615.480.5667 or 615.870.9728
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: How does Sendmail know how it was invoked?

2007-08-04 Thread RW
On Sat, 4 Aug 2007 15:48:11 -0500
Don Hinton [EMAIL PROTECTED] wrote:

 On Saturday 04 August 2007 15:13:34 RW wrote:
  On Sat, 4 Aug 2007 13:23:07 -0500
 

  What I didn't get was that when a binary is executed from execve(),
  it's the parent program that sets the argv[0] seen by the child,
  and not the kernel.
 
 Sorry, I should have paid closer attention to your question and
 actually looked at the code to see what they were doing in this
 specific case.
 
 They original args, including argv[0], are passed as args parameter
 to execve. So from the perspective of the called application, the
 original argv[0] is now argv[1].  

I don't think that's right. As I understand it, the argv argument to
execve() is passed-on directly as the child processes arguments, and
the parent can write whatever it likes into argv[0] - it's only
convention that it's a filename. So mailwrapper passes its own
argv[0] as sendmail's argv[0]. And so sendmail behaves as if it had been
invoked as mailq or whatever.

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: How does Sendmail know how it was invoked?

2007-08-04 Thread Don Hinton

 I don't think that's right. As I understand it, the argv argument to
 execve() is passed-on directly as the child processes arguments, and
 the parent can write whatever it likes into argv[0] - it's only
 convention that it's a filename. So mailwrapper passes its own
 argv[0] as sendmail's argv[0]. And so sendmail behaves as if it had been
 invoked as mailq or whatever.

You're exactly right.  I misread the man file and did a little test to confirm 
it. 

thanks...
don
-- 
Don Hinton don.hinton at vanderbilt.edu or hintonda at gmail.com
Institute for Software Integrated Systems (ISIS), Vanderbilt University
tel: 615.480.5667 or 615.870.9728
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]