It is currently Tue Mar 31, 2020 7:44 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Obscure import of _SC_CFMachPortCreateWithPort
PostPosted: Sun Sep 01, 2019 1:34 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Mar 11, 2009 4:06 pm
Posts: 243
Location: NL
How to use in PB? It's apparently a private convenience function for CFMachPortCreateWithPort. Don't know which library/framework to import from.

What I'm trying to partially convert?
Code:
/*
 * BSD notifications from loginwindow indicating shutdown
 */
// kLWShutdownInitiated
//   User clicked shutdown: may be aborted later
#define kLWShutdowntInitiated    "com.apple.system.loginwindow.shutdownInitiated"

// kLWRestartInitiated
//   User clicked restart: may be aborted later
#define kLWRestartInitiated     "com.apple.system.loginwindow.restartinitiated"

// kLWLogoutCancelled
//   A previously initiated shutdown, restart, or logout, has been cancelled.
#define kLWLogoutCancelled      "com.apple.system.loginwindow.logoutcancelled"

// kLWLogoutPointOfNoReturn
//   A previously initiated shutdown, restart, or logout has succeeded, and is
//   no longer abortable by anyone. Point of no return!
#define kLWLogoutPointOfNoReturn    "com.apple.system.loginwindow.logoutNoReturn"

// kLWSULogoutInitiated
//   Loginwindow is beginning a sequence of 1. logout, 2. software update, 3. then restart.
#define kLWSULogoutInitiated     "com.apple.system.loginwindow.sulogoutinitiated"


static void
initializeShutdownNotifications(void)
{
    CFMachPortRef       gNotifyMachPort = NULL;
    CFRunLoopSourceRef  gNotifyMachPortRLS = NULL;
    mach_port_t         our_port = MACH_PORT_NULL;

    // Tell the kernel that we are NOT shutting down at the moment, since
    // configd is just launching now.
    // Why: if configd crashed with "System Shutdown" == kCFbooleanTrue, reset
    // it now as the situation may no longer apply.
    _setRootDomainProperty(CFSTR("System Shutdown"), kCFBooleanFalse);

    /* * * * * * * * * * * * * */
   
    // Sneak in our registration for CPU power notifications here; to piggy-back
    // with the other mach port registrations for LW.
    notify_register_mach_port(
                        kIOPMCPUPowerNotificationKey,
                        &our_port,
                        0, /* flags */
                        &gCPUPowerNotificationToken);
   
   
    notify_register_mach_port(
                        kLWShutdowntInitiated,
                        &our_port,
                        NOTIFY_REUSE, /* flags */
                        &lwNotify.shutdown);

    notify_register_mach_port(
                        kLWRestartInitiated,
                        &our_port,
                        NOTIFY_REUSE, /* flags */
                        &lwNotify.restart);

    notify_register_mach_port(
                        kLWLogoutCancelled,
                        &our_port,
                        NOTIFY_REUSE, /* flags */
                        &lwNotify.cancel);

    notify_register_mach_port(
                        kLWLogoutPointOfNoReturn,
                        &our_port,
                        NOTIFY_REUSE, /* flags */
                        &lwNotify.pointofnoreturn);
    notify_register_mach_port(
                        kLWSULogoutInitiated,
                        &our_port,
                        NOTIFY_REUSE, /* flags */
                        &lwNotify.su);

    /* * * * * * * * * * * * * */

    gNotifyMachPort = _SC_CFMachPortCreateWithPort(
                                "PowerManagement/shutdown",
                                our_port,
                                lwShutdownCallback,
                                NULL);

    if (gNotifyMachPort) {
        gNotifyMachPortRLS = CFMachPortCreateRunLoopSource(0, gNotifyMachPort, 0);
        if (gNotifyMachPortRLS) {       
            CFRunLoopAddSource(CFRunLoopGetCurrent(), gNotifyMachPortRLS, kCFRunLoopDefaultMode);
            CFRelease(gNotifyMachPortRLS);
        }
        CFRelease(gNotifyMachPort);
    }
}

static void lwShutdownCallback(
    CFMachPortRef port,
    void *msg,
    CFIndex size,
    void *info)
{
    mach_msg_header_t   *header = (mach_msg_header_t *)msg;
    CFNumberRef n = NULL;
    static bool amidst_shutdown = false;
    static int consoleShutdownState = kIOPMStateConsoleShutdownNone;
    static int lastConsoleShutdownState = 0;

    if (header->msgh_id == gCPUPowerNotificationToken)
    {
        // System CPU power status has changed
        SystemLoadCPUPowerHasChanged(NULL);
    } else if (header->msgh_id == lwNotify.su)
    {
        // Loginwindow is logging out to begin a several-minute
        // software update. We'll suppress the immediately next shoutdown
        // and logout messages.
        consoleShutdownState = kIOPMStateConsoleSULogoutInitiated;

    } else if (header->msgh_id == lwNotify.shutdown)
    {
        // Loginwindow put a shutdown confirm panel up on screen
        // The user has not necessarily even clicked on it yet
        amidst_shutdown = true;       
        consoleShutdownState = kIOPMStateConsoleShutdownPossible;

    } else if (header->msgh_id == lwNotify.restart)
    {
        // Loginwindow put a restart confirm panel up on screen
        // The user has not necessarily even clicked on it yet
        amidst_shutdown = true;
        consoleShutdownState = kIOPMStateConsoleShutdownPossible;

    } else if (header->msgh_id == lwNotify.cancel)
    {
        amidst_shutdown = false;
        consoleShutdownState = kIOPMStateConsoleShutdownNone;

    } else if (amidst_shutdown
            && (header->msgh_id == lwNotify.pointofnoreturn))
    {
        // Whatever shutdown or restart that was in progress has succeeded.
        // All apps are quit, there's no more user input required. We will
        // hereby disable sleep for the remainder of time spent shutting down
        // this machine.

        _setRootDomainProperty(CFSTR("System Shutdown"), kCFBooleanTrue);
        consoleShutdownState = kIOPMStateConsoleShutdownCertain;
    }
   
    // Tell interested kernel drivers where we are in the GUI shutdown.
    if (lastConsoleShutdownState != consoleShutdownState) {
   
        n = CFNumberCreate(0, kCFNumberIntType, &consoleShutdownState);
        if (n) {
            _setRootDomainProperty( CFSTR(kIOPMStateConsoleShutdown), n) ;
            CFRelease(n);
        }

        lastConsoleShutdownState = consoleShutdownState;
    }
   
   
    return;
}

from https://opensource.apple.com/source/Pow ... .auto.html

Goal: another way of getting notifications for different power/logoff events.. Not sure if it's even possible. Anyway, step 1 is making _SC_CFMachPortCreateWithPort work or alternative of that function.


Top
 Profile  
Reply with quote  
 Post subject: Re: Obscure import of _SC_CFMachPortCreateWithPort
PostPosted: Sun Sep 01, 2019 4:39 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 2:49 pm
Posts: 662
Although I have no idea of the CFMach api It seems a wrapper for CFMachPortCreateWithPort
https://ssd-disclosure.com/archives/3944/ssd-advisory-ios-powerd-uninitialized-mach-message-reply-to-sandbox-escape-and-privilege-escalation

As for CFMachPortCreateWithPort() this seems to work:
Code:
ImportC ""
   CFMachPortCreate(alloc.i, cb.i, ctx.i, sf.i)
   CFMachPortCreateWithPort(alloc.i, port.l, cb.i, ctx.i, sf.i)
EndImport

Structure CFMachPortContext Align #PB_Structure_AlignC
   version.l
   info.i
   retain.i
   release.i
   copyDescription.i
EndStructure

ProcedureC lwShutdownCallback(port.i, msg.i, size.i, info.i)
   Debug "cb"
EndProcedure

Define.CFMachPortContext ctx
Define.l p
Define.b sf

For a = 0 To 10000
   If CFMachPortCreateWithPort(#Null, a, @lwShutdownCallback(), @ctx, @sf) <> 0
      Debug "port created: " + Str(a)
   EndIf
Next
; Debug CFMachPortCreate(#Null, @lwShutdownCallback(), @ctx, @sf)



Top
 Profile  
Reply with quote  
 Post subject: Re: Obscure import of _SC_CFMachPortCreateWithPort
PostPosted: Sun Sep 01, 2019 4:49 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3606
Location: Netherlands
Rinzwind wrote:
Goal: another way of getting notifications for different power/logoff events.

Will notificationCenter of NSWorkspace work for you ?
http://forums.purebasic.com/english/vie ... 19&t=70410

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: Obscure import of _SC_CFMachPortCreateWithPort
PostPosted: Sun Sep 01, 2019 5:55 pm 
Offline
Enthusiast
Enthusiast

Joined: Wed Mar 11, 2009 4:06 pm
Posts: 243
Location: NL
Well, Im too interested in cancelled logoff and the notification events dont supply these apparently.


Top
 Profile  
Reply with quote  
 Post subject: Re: Obscure import of _SC_CFMachPortCreateWithPort
PostPosted: Sun Sep 01, 2019 7:09 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3606
Location: Netherlands
I found an almost similar source that seems to use CFMachPortCreateWithPort instead of _SC_CFMachPortCreateWithPort
https://opensource.apple.com/source/Pow ... .auto.html
maybe that's easier.

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye