def spawn_sudo(proxy):
    """Spawn the sudo slave process, returning proc and a pipe to message it."""

    pipe = SecureStringPipe()
    c_pipe = pipe.connect()

    if not getattr(sys,"frozen",False):
        exe = [sys.executable,"-c","import esky; esky.run_startup_hooks()"]
    elif os.path.basename(sys.executable).lower() in ("python","pythonw"):
        exe = [sys.executable,"-c","import esky; esky.run_startup_hooks()"]
    else:
        if not esky._startup_hooks_were_run:
            raise OSError(None,"unable to sudo: startup hooks not run")
        exe = [sys.executable]
    args = ["--esky-spawn-sudo"]
    args.append(base.b64pickle(proxy))
    args.append(base.b64pickle(c_pipe))

    # Make it a slave process so it dies if we die
    exe = exe + esky.slaveproc.get_slave_process_args() + args

    auth = ctypes.c_void_p()

    right = AuthorizationRight()
    right.name = "py.esky.sudo." + proxy.name
    right.valueLength = 0
    right.value = None
    right.flags = 0

    rights = AuthorizationRights()
    rights.count = 1
    rights.items[0] = right

    r_auth = byref(auth)
    err = sec.AuthorizationCreate(None,None,kAuthorizationFlagDefaults,r_auth)
    if err:
        raise OSError(errno.EACCES,"could not sudo: %d" % (err,))

    try:

        kAuthFlags = kAuthorizationFlagDefaults \
                     | kAuthorizationFlagPreAuthorize \
                     | kAuthorizationFlagInteractionAllowed \
                     | kAuthorizationFlagExtendRights
        
        err = sec.AuthorizationCopyRights(auth,None,None,kAuthFlags,None)
        if err:
            raise OSError(errno.EACCES,"could not sudo: %d" % (err,))

        args = (ctypes.c_char_p * len(exe))()
        for i,arg in enumerate(exe[1:]):
            args[i] = arg
        args[len(exe)-1] = None
        io = ctypes.c_void_p()
        err = sec.AuthorizationExecuteWithPrivileges(auth,exe[0],0,args,byref(io))
        if err:
            raise OSError(errno.EACCES,"could not sudo: %d" %(err,))
        
        buf = ctypes.create_string_buffer(8)
        read = libc.fread(byref(buf),1,4,io)
        if read != 4:
            libc.fclose(io)
            raise OSError(errno.EACCES,"could not sudo: child failed")
        pid = struct.unpack("I",buf.raw[:4])[0]
        pipe.fp = io
        return (FakePopen(pid),pipe)
    finally:
        sec.AuthorizationFree(auth,kAuthorizationFlagDestroyRights)
dataset/ETHPy150Open cloudmatrix/esky/esky/sudo/sudo_osx.py/spawn_sudo/original