Obscure import of _SC_CFMachPortCreateWithPort

Mac OSX specific forum
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Obscure import of _SC_CFMachPortCreateWithPort

Post by Rinzwind »

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: Select all

/*
 * 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.
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Obscure import of _SC_CFMachPortCreateWithPort

Post by Justin »

Although I have no idea of the CFMach api It seems a wrapper for CFMachPortCreateWithPort
https://ssd-disclosure.com/archives/394 ... escalation

As for CFMachPortCreateWithPort() this seems to work:

Code: Select all

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)

wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Obscure import of _SC_CFMachPortCreateWithPort

Post by wilbert »

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
Windows (x64)
Raspberry Pi OS (Arm64)
Rinzwind
Enthusiast
Enthusiast
Posts: 636
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: Obscure import of _SC_CFMachPortCreateWithPort

Post by Rinzwind »

Well, Im too interested in cancelled logoff and the notification events dont supply these apparently.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Obscure import of _SC_CFMachPortCreateWithPort

Post by wilbert »

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.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply