views:

32

answers:

2

First let me say that I know it's better to use the subprocess module, but I'm editing other people's code and I'm trying to make as few changes as possible, which includes avoiding the importing any new modules. So I'd like to stick to the currently-imported modules (os, sys, and paths) if at all possible.

The code is currently (in a file called postfix-to-mailman.py that some of you may be familiar with):

if local in ('postmaster', 'abuse', 'mailer-daemon'):
  os.execv("/usr/sbin/sendmail", ("/usr/sbin/sendmail", '[email protected]'))
  sys.exit(0)

This works fine (though I think sys.exit(0) might be never be called and thus be unnecessary).

I believe this replaces the current process with a call to /usr/sbin/sendmail passing it the arguments /usr/sbin/sendmail (for argv[0] i.e. itself) and '[email protected]', then passes the environment of the current process - including the email message in sys.stdin - to the child process.

What I'd like to do is essentially send another copy of the message before doing this. I can't use execv again because then execution will stop. So I've tried the following:

if local in ('postmaster', 'abuse', 'mailer-daemon'):
  os.spawnv(os.P_WAIT, "/usr/sbin/sendmail", ("/usr/sbin/sendmail", '[email protected]'))
  os.execv("/usr/sbin/sendmail", ("/usr/sbin/sendmail", '[email protected]'))
  sys.exit(0)

However, while it sends the message to [email protected], it never sends it to [email protected]

This surprised me because I thought using spawn would start a child process and then continue execution in the current process when it returns (or without waiting, if P_NOWAIT is used).

Incidentally, I tried os.P_NOWAIT first, but the message I got at [email protected] was empty, so at least when I used P_WAIT the message came through intact. But it still never got sent to [email protected] which is a problem.

I'd rather not use os.system if I can avoid it because I'd rather not go out to a shell environment if it can be avoided (security issues, possible performance? I admit I'm being paranoid here, but if I can avoid os.system I'd still like to).

The only thing I can think of is that the call to os.spawnv is somehow consuming/emptying the contents of sys.stdin, but that doesn't really make sense either. Ideas?

+1  A: 

While it might not make sense, that does appear to be the case

import os

os.spawnv(os.P_WAIT,"/usr/bin/wc", ("/usr/bin/wc",))
os.execv("/usr/bin/wc", ("/usr/bin/wc",))

$ cat j.py | python j.py 
       4       6     106
       0       0       0

In which case you might do something like this

import os
import sys

buf = sys.stdin.read()
wc = os.popen("usr/sbin/sendmail [email protected]","w")
wc.write(buf)
wc.close()
wc = os.popen("usr/sbin/sendmail [email protected]","w")
wc.write(buf)
wc.close()
sys.exit(0)
jcopenha
Hm that might be part of it, but unfortunately inserting sys.stdin = sys.__stdin__ between the spawnv and execv lines doesn't seem to work, even though http://docs.python.org/library/sys.html#sys.stdin says that sys.__stdin__ should contain the original value of sys.stdin at the start of the program. Blech. (but thanks for testing that out and providing a clue along the way to a solution!)
Chirael
I think os.popen calls the shell as os.system does. Is there any way to do what you're suggesting without having to go out to the shell? I tried doing it with popen2, 3, 4 but while those accept sequences for arguments and bypass the shell, they don't return a file descriptor one can write to (apparently). Thanks!
Chirael
By the way, I feel like the main problem here is that iterating over sys.stdin is a one-time process; there seems to be no way to go back to the beginning of sys.stdin for the second call :-/
Chirael
From http://docs.python.org/library/os.html "Also, for each of these variants, on Unix, cmd may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with os.spawnv()). If cmd is a string it will be passed to the shell (as with os.system())." so if you change to (wc_in, wc_out) = os.popen2(["usr/sbin/sendmail","[email protected]"],"w") that should pass it without shell intervention. But popen2 is deprecated as of python 2.6.
jcopenha
I tried this and got /usr/bin/wc: write error: Broken pipe. Specifically I tried:import os, sysbuf = sys.stdin.read()(wc_in, wc_out) = os.popen2(["/usr/bin/wc", "-l"], 'w')wc_in.write(buf)wc_in.close()
Chirael
A: 

sys.stdin is a pipe and those aren't seekable so you can never rewind that file-like object to read its contents again. To actually invoke sendmail(1) twice, you need to save the contents of stdin, preferably in a temporary file but if the data is guaranteed to have a limited size you could safe it in memory instead.

But why go through the trouble? Do you specifically need the email copy to be a separately queued email (and if so, why)? Just add the wanted recipient in your original invocation of sendmail(1). The additional recipient will not be seen in the email headers.

if local in ('postmaster', 'abuse', 'mailer-daemon'):
  os.execv("/usr/sbin/sendmail", ("/usr/sbin/sendmail", 
                                  '[email protected]',
                                  '[email protected]'))
  sys.exit(0)

Oh, and the sys.exit(0) line will be executed if os.execv() for some reason fails. This'll happen if /usr/sbin/sendmail cannot be executed, e.g. if the executable file doesn't exist or isn't actually executable. In other words, this is an error condition that you should take care of.

Magnus Bäck