def drop_privileges():
  we_are_root = os.getuid() == 0
  if not we_are_root:
    print >>sys.stdout, "[INFO] Not running as root, skipping privilege drop"
    return

  try:
    pw = pwd.getpwnam(SETUID_USER)
  except KeyError:
    print >>sys.stderr, "[ERROR] Couldn't get user information for user " + SETUID_USER
    raise

  try:
    gr = grp.getgrnam(SETGID_GROUP)
  except KeyError:
    print >>sys.stderr, "[ERROR] Couldn't get group information for group " + SETGID_GROUP
    raise

  # gid has to be set first
  os.setgid(gr.gr_gid)
  os.setuid(pw.pw_uid)
Drop root privileges down to the specified SETUID_USER.
dataset/ETHPy150Open cloudera/hue/desktop/core/src/desktop/supervisor.py/drop_privileges