Skip to content

Instantly share code, notes, and snippets.

@acg
Created December 18, 2013 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acg/8016689 to your computer and use it in GitHub Desktop.
Save acg/8016689 to your computer and use it in GitHub Desktop.
Factoring out the listen(2) half of an ucspi unixserver.
#!/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