Created
December 18, 2013 03:09
-
-
Save acg/8016689 to your computer and use it in GitHub Desktop.
Factoring out the listen(2) half of an ucspi unixserver.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
Like unixserver(1), but just listen and exec the subordinate program | |
with the listening socket as stdin. The subordinate program still needs | |
to call accept(2) on stdin. | |
''' | |
import sys | |
import os | |
import socket | |
import errno | |
prog = sys.argv.pop(0) | |
path = sys.argv.pop(0) | |
# Remove socket from previous run, if it exists. | |
try: | |
os.unlink(path) | |
except OSError, e: | |
if e.errno != errno.ENOENT: | |
raise | |
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
s.bind(path) | |
s.listen(32) | |
os.dup2(s.fileno(),0) | |
os.close(s.fileno()) | |
os.execvp(sys.argv[0],sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment