Search code examples
iosiphonejailbreakiphone-privateapi

Theos/Logos: Accessing Static/Global Variable Across Multiple Hooked Processes


So granted, I am very new to tweak development, but I'm running into an issue. I'm working on a tweak for personal use that hooks into multiple processes, but I'm having difficulty sharing a variable between those process-specific hooks. For instance, my SpringBoard method hooks are able to set the value of a static variable and then access the value set. But the app-specific method hooks are unable to retrieve the value set within the context of a SpringBoard method. The variable is coming back uninitialized.

Since the tweak library is common, how can I initialize a "global" library-level variable within the context of one process' hook and access that value within the context of another process.

My first attempts look something like this:

static BOOL isEnabled;

%hook FirstProcessFirstClass

- (void) methodInFirstProcessFirstClass {
    isEnabled = YES;
    %orig;
}

%end

%hook FirstProcessSecondClass

- (void) methodInFirstProcessSecondClass {
    // This will be 1 if it occurs after methodInFirstProcessFirstClass
    NSLog("isEnabled equals %d", isEnabled);
    %orig;
}

%end

%hook SecondProcessClass

- (void) methodInSecondProcessClass {
    // This is always going to be uninitialized (i.e., 0)
    NSLog("isEnabled equals %d", isEnabled);
    %orig;
}

%end

You get the picture, I'd like to share a "global variable" between hooked processes. Thanks for humoring me. :/


Solution

  • Sharing a variable across processes is a bit more complex than just having a global variable. You'll need to use some form of IPC (Inter Process Communication) to synchronize the variable across processes. Since you hook SpringBoard and other apps, you can set up SpringBoard as the "server" so that it sends the new state of the variable on other processes.

    You can also make use of the %group directive to make the hooks be applied depending on which process your tweak is actually hooked, so that the hooks for SpringBoard are only active in the SpringBoard process and not on the apps. This will not change much, but there will not be unnecessary hooks in place.