2525#import " FanControl.h"
2626#import < Sparkle/SUUpdater.h>
2727#import " Privilege.h"
28+ #import " tb-switcher.h"
2829
2930@interface FanControl ()
3031+ (void )copyMachinesIfNecessary ;
@@ -39,9 +40,6 @@ @implementation FanControl
3940// Number of fans reported by the hardware.
4041int g_numFans = 0 ;
4142
42- NSString *moduleDstPath =
43- @" /Library/Application Support/smcFanControl2/DisableTurboBoost.64bits.kext" ;
44-
4543NSUserDefaults *defaults;
4644
4745#pragma mark **Init-Methods**
@@ -242,23 +240,14 @@ - (void)displayDonationMessage {
242240 }
243241}
244242
245- - (void )enableTurboBoost {
246- [Privilege runTaskAsAdmin: @" /sbin/kextunload" andArgs: @[moduleDstPath]];
247- }
248-
249- - (void )disableTurboBoost {
250- NSArray *argsArrayLoadKext = @[@" -v" , moduleDstPath];
251- [Privilege runTaskAsAdmin: @" /usr/bin/kextutil" andArgs: argsArrayLoadKext];
252- }
253-
254243- (void )applyTurboBoost : (id )sender {
255244 NSControlStateValue state = [sender state ];
256245 if (state == NSOffState ) {
257246 [sender setState: NSOnState ];
258- [ self enableTurboBoost ] ;
247+ enable_tb () ;
259248 } else {
260249 [sender setState: NSOffState ];
261- [ self disableTurboBoost ] ;
250+ disable_tb () ;
262251 }
263252}
264253
@@ -278,7 +267,7 @@ - (void)init_statusitem {
278267 [theMenu insertItem: s_menus[i] atIndex: i];
279268 };
280269
281- [ self disableTurboBoost ] ;
270+ disable_tb () ;
282271 NSMenuItem *fan1Item = [theMenu itemWithTitle: @" Fan: 1" ];
283272 int fan1ItemIdx = [theMenu indexOfItem: fan1Item];
284273 NSMenuItem *turboBoostItem = [[NSMenuItem alloc ]
@@ -296,7 +285,6 @@ - (void)init_statusitem {
296285 [theMenu setDelegate: self ];
297286}
298287
299-
300288#pragma mark **Action-Methods**
301289
302290- (IBAction )loginItem : (id )sender {
@@ -617,7 +605,7 @@ - (void)terminate:(id)sender {
617605 [_readTimer invalidate ];
618606 [pw deregisterForSleepWakeNotification ];
619607 [pw deregisterForPowerChange ];
620- [ self enableTurboBoost ] ;
608+ enable_tb () ;
621609 [[NSApplication sharedApplication ] terminate: self ];
622610}
623611
@@ -750,9 +738,9 @@ - (void)systemDidWakeFromSleep:(id)sender {
750738 [self apply_settings: nil controllerindex: [[defaults objectForKey: PREF_SELECTION_DEFAULT] intValue ]];
751739
752740 if ([[theMenu itemWithTitle: @" Turbo Boost" ] state ] == NSOnState ) {
753- [ self enableTurboBoost ] ;
741+ enable_tb () ;
754742 } else {
755- [ self disableTurboBoost ] ;
743+ disable_tb () ;
756744 }
757745}
758746
@@ -866,46 +854,85 @@ - (void)setStartAtLogin:(BOOL)enabled {
866854
867855#pragma mark **SMC-Binary Owner/Right Check**
868856
857+ + (void ) ensureService {
858+ NSString *tool = @" /bin/launchctl" ;
859+ NSArray *argsArray = @[@" load" , @LAUNCH_DAEMON_PLIST_PATH];
860+ NSString *output;
861+ NSString *error;
862+
863+ if ([Privilege runProcessAsAdministrator: tool withArguments: argsArray output: &output errorDescription: &error]){
864+ NSLog (@" output: %@ " , output);
865+ }else {
866+ NSLog (@" error: %@ " , error);
867+ }
868+ }
869+
869870// TODO: It looks like this function is called inefficiently.
870871// call smc binary with sudo rights and apply
871872+ (void )setRights {
872- NSString *smcpath = [[NSBundle mainBundle ] pathForResource: @" smc" ofType: @" " ];
873- NSFileManager *fmanage = [NSFileManager defaultManager ];
874- NSDictionary *fdic = [fmanage attributesOfItemAtPath: smcpath error: nil ];
875- if ([[fdic valueForKey: @" NSFileOwnerAccountName" ] isEqualToString: @" root" ] && [[fdic valueForKey: @" NSFileGroupOwnerAccountName" ] isEqualToString: @" admin" ] && ([[fdic valueForKey: @" NSFilePosixPermissions" ] intValue ] == 3437 )) {
873+ NSString *smcPath = [[NSBundle mainBundle ] pathForResource: @" smc" ofType: @" " ];
874+ NSFileManager *fManger = [NSFileManager defaultManager ];
875+ NSDictionary *fdic = [fManger attributesOfItemAtPath: smcPath error: nil ];
876+ if ([[fdic valueForKey: @" NSFileOwnerAccountName" ] isEqualToString: @" root" ] &&
877+ [[fdic valueForKey: @" NSFileGroupOwnerAccountName" ] isEqualToString: @" admin" ] &&
878+ ([[fdic valueForKey: @" NSFilePosixPermissions" ] intValue ] == 3437 )) {
876879 // If the SMC binary has already been modified to run as root, then do nothing.
877880
878881 } else {
879882 NSString *tool = @" /usr/sbin/chown" ;
880- NSArray *argsArray = @[@" root:admin" , smcpath ];
883+ NSArray *argsArray = @[@" root:admin" , smcPath ];
881884 [Privilege runTaskAsAdmin: tool andArgs: argsArray];
882885
883886 // second call for suid-bit
884887 tool = @" /bin/chmod" ;
885- argsArray = @[@" 6555" , smcpath ];
888+ argsArray = @[@" 6555" , smcPath ];
886889 [Privilege runTaskAsAdmin: tool andArgs: argsArray];
887890 }
888891
889-
890892 NSString *modulePath = [[NSBundle mainBundle ] pathForResource: @" DisableTurboBoost.64bits" ofType: @" kext" ];
891- NSFileManager *fileManager = [NSFileManager defaultManager ];
892- if (![fileManager fileExistsAtPath: moduleDstPath]) {
893+ if (![fManger fileExistsAtPath: @MODULE_PATH]) {
893894 NSString *tool = @" /bin/sh" ;
894- NSArray *argsArray = @[@" -c" , [NSString stringWithFormat: @" mkdir -p \" %@ \" && cp -Rf \" %@ /\" \" %@ \" " , moduleDstPath, modulePath, moduleDstPath]];
895+ NSArray *argsArray = @[@" -c" , [NSString stringWithFormat: @" mkdir -p \" %@ \" && cp -Rf \" %@ /\" \" %@ \" " ,
896+ @MODULE_PATH, modulePath, @MODULE_PATH]];
895897 [Privilege runTaskAsAdmin: tool andArgs: argsArray];
898+ [self setOwnAndMod: @MODULE_PATH user: @" root" group: @" wheel" mod: @" 755" ];
896899 }
897- NSDictionary *fdicKext = [fmanage attributesOfItemAtPath: moduleDstPath error: nil ];
898- if ([[fdicKext valueForKey: @" NSFileOwnerAccountName" ] isEqualToString: @" root" ] && [[fdicKext valueForKey: @" NSFileGroupOwnerAccountName" ] isEqualToString: @" wheel" ]) {
899- // If the SMC binary has already been modified to run as root, then do nothing.
900+
901+ NSString *binPath = [[NSBundle mainBundle ] pathForResource: @" tb-switcher" ofType: @" " ];
902+ if (![fManger fileExistsAtPath: @TB_SWITCHER_BIN_PATH]) {
903+ NSString *tool = @" /bin/sh" ;
904+ NSArray *argsArray = @[@" -c" , [NSString stringWithFormat: @" cp -f \" %@ \" \" %@ \" " ,
905+ binPath, @TB_SWITCHER_BIN_PATH]];
906+ [Privilege runTaskAsAdmin: tool andArgs: argsArray];
907+ [self setOwnAndMod: @TB_SWITCHER_BIN_PATH user: @" root" group: @" wheel" mod: @" 755" ];
908+ }
909+
910+ NSString *plistPath = [[NSBundle mainBundle ] pathForResource: @" com.tinkernels.tb-switcher" ofType: @" plist" ];
911+ if (![fManger fileExistsAtPath: @LAUNCH_DAEMON_PLIST_PATH]) {
912+ NSString *tool = @" /bin/sh" ;
913+ NSArray *argsArray = @[@" -c" , [NSString stringWithFormat: @" cp -f \" %@ \" \" %@ \" " ,
914+ plistPath, @LAUNCH_DAEMON_PLIST_PATH]];
915+ [Privilege runTaskAsAdmin: tool andArgs: argsArray];
916+
917+ [self setOwnAndMod: @LAUNCH_DAEMON_PLIST_PATH user: @" root" group: @" wheel" mod: @" 644" ];
918+ [self ensureService ];
919+ }
920+ }
921+
922+ + (void )setOwnAndMod : (NSString *)path user : (NSString *)user group : (NSString *)group mod : (NSString *)mod {
923+ NSFileManager *fManger = [NSFileManager defaultManager ];
924+ NSDictionary *fdict = [fManger attributesOfItemAtPath: path error: nil ];
925+ if ([[fdict valueForKey: @" NSFileOwnerAccountName" ] isEqualToString: user] &&
926+ [[fdict valueForKey: @" NSFileGroupOwnerAccountName" ] isEqualToString: group]) {
900927
901928 } else {
902929 NSString *tool = @" /usr/sbin/chown" ;
903- NSArray *argsArray = @[@" -R" , @" root:wheel " , moduleDstPath ];
930+ NSArray *argsArray = @[@" -R" , [ NSString stringWithFormat: @" %@ : %@ " , user, group], path ];
904931
905932 [Privilege runTaskAsAdmin: tool andArgs: argsArray];
906933
907934 tool = @" /bin/chmod" ;
908- argsArray = @[@" -Rf" , @" 755 " , moduleDstPath ];
935+ argsArray = @[@" -Rf" , mod, path ];
909936 [Privilege runTaskAsAdmin: tool andArgs: argsArray];
910937 }
911938}
0 commit comments