1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
#import "MAServiceControl.h"
#include <sys/wait.h>
#import "MAdministrator.h"
#import <MySQLToolsCommon/MConnectionInfo.h>
#include "mahelper.h"
@interface MAServiceControl(Private)
- (void)updateStatus;
- (BOOL)performCommand:(MAHelperCommand)command
withRight:(const char*)rightName;
- (void)startStopServer:(id)flag;
- (void)enableServerAutoStart:(BOOL)flag;
@end
@implementation MAServiceControl(Private)
- (void)updateStatus
{
NSImage *image;
NSString *status;
NSString *blabel;
BOOL changeable= YES;
BOOL autoStart= NO;
if (![_owner isLocal])
{
image= [NSImage imageNamed: @"service_status_running.png"];
status= @"Remote Server is Running";
blabel= @"Stop Server";
changeable= NO;
}
else
{
// check if autostart is enabled
autoStart= mautoStartState();
[startOnBootCheck setState:autoStart?NSOnState:NSOffState];
switch ([_owner serverStatus])
{
case 0:
image= [NSImage imageNamed: @"service_status_stopped.png"];
status= @"Server is Stopped";
blabel= @"Start Server";
break;
case 1:
image= [NSImage imageNamed: @"service_status_running.png"];
status= @"Server is Running";
blabel= @"Stop Server";
break;
default:
image= [NSImage imageNamed: @"service_status_unknown.png"];
status= @"Cannot determine server status";
blabel= @"Stop Server";
changeable= NO;
break;
}
}
[statusImage setImage: image];
[statusLabel setStringValue: status];
[toggleButton setTitle:blabel];
[toggleButton setEnabled:changeable];
[startOnBootCheck setEnabled:changeable];
}
static void helperout_callback(const char *text, void *data)
{
[[[((MAServiceControl*)data)->messageText textStorage] mutableString] appendFormat:@"%s", text];
}
- (BOOL)performCommand:(MAHelperCommand)command
withRight:(const char*)rightName
{
NSString *helperPath;
AuthorizationItem right = { rightName, 0, NULL, 0 };
AuthorizationRights rightSet = { 1, &right };
OSStatus status;
helperPath= [[NSBundle bundleForClass:[self class]] pathForResource:@"mahelper"
ofType:@""];
if (!_authRef)
{
if (AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults,
&_authRef)!=errAuthorizationSuccess)
{
[[[messageText textStorage] mutableString] appendString:@"Error setting-up authorization for the helper.\n"];
NSLog(@"Could not create authorization reference object.");
return NO;
}
}
status = AuthorizationCopyRights(_authRef, &rightSet, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults | kAuthorizationFlagPreAuthorize
| kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights,
NULL);
if (status == errAuthorizationSuccess)
{
int hstatus;
hstatus = mhelperPerformCommand2(_authRef, [helperPath fileSystemRepresentation], command,
helperout_callback, self);
}
else
{
AuthorizationFree(_authRef, kAuthorizationFlagDestroyRights);
_authRef= 0;
if (status != errAuthorizationCanceled)
{
// pre-auth failed
[[[messageText textStorage] mutableString] appendString:@"Error during authorization step for the helper.\n"];
}
else
return YES; // YES means it was cancelled
}
return NO;
}
- (void)startStopServer:(id)startNum
{
MAHelperCommand command;
BOOL start= [startNum boolValue];
BOOL cancelled;
command.authorizedCommandId= start ? MAHelperStartMySQL : MAHelperStopMySQL;
cancelled= [self performCommand:command
withRight:"com.mysql.administrator.server"];
if (cancelled)
{
[[[messageText textStorage] mutableString] appendString:@"Cancelled.\n"];
[_owner serverStatus];
return;
}
if (start)
{
if ([_owner serverStatus]==0)
[[[messageText textStorage] mutableString] appendString:@"Failed to start server.\n"];
else
{
[[[messageText textStorage] mutableString] appendString:@"Started server.\n"];
[[[messageText textStorage] mutableString] appendString:@"Reconnecting...\n"];
if ([[_owner serverInfo] reconnect:[_owner mysql]])
[[[messageText textStorage] mutableString] appendString:@"Reconnected to server.\n"];
else
[[[messageText textStorage] mutableString] appendString:@"Reconnection failed!\n"];
}
}
else
{
if ([_owner serverStatus]!=0)
{
[[[messageText textStorage] mutableString] appendString:@"Failed to stop server, trying shutdown...\n"];
#if ((MYSQL_VERSION_ID >= 40103) && (MYSQL_VERSION_ID < 50000)) || (MYSQL_VERSION_ID >= 50001)
mysql_shutdown([_owner mysql], SHUTDOWN_DEFAULT);
#else
mysql_shutdown([_owner mysql]);
#endif
// sleep a bit waiting the server to finish shutdown
sleep(2);
if ([_owner serverStatus]!=0)
[[[messageText textStorage] mutableString] appendString:@"Failed to stop server.\n"];
else
[[[messageText textStorage] mutableString] appendString:@"Stopped server.\n"];
}
else
[[[messageText textStorage] mutableString] appendString:@"Stopped server.\n"];
}
[self updateStatus];
}
- (void)enableServerAutoStart:(BOOL)flag
{
MAHelperCommand command;
command.authorizedCommandId= MAHelperToggleAutoStart;
command.enable= flag;
if (![self performCommand:command
withRight:"com.mysql.administrator.chconfig"])
{
NSRunAlertPanel(@"Error", @"Could not change automatic startup configuration.",
nil, nil, nil);
}
}
@end
@implementation MAServiceControl
- (IBAction)toggleServer:(id)sender
{
BOOL start= [_owner serverStatus]==0;
if (start)
[[[messageText textStorage] mutableString] appendString:@"Starting server...\n"];
else
[[[messageText textStorage] mutableString] appendString:@"Stopping server...\n"];
[toggleButton setEnabled:NO];
[NSApplication detachDrawingThread:@selector(startStopServer:)
toTarget:self
withObject:[NSNumber numberWithBool: start]];
}
- (IBAction)toggleServerStartup:(id)sender
{
[self enableServerAutoStart:[sender state]==NSOnState];
[self updateStatus];
}
- (void)didShow
{
[self updateStatus];
}
- (void)awakeFromNib
{
[self updateStatus];
/*
if ([self getStartupItem]==NULL)
{
[messageText setString:@"The MySQL Startup Item was not found in /Librart/StartupItems\n"
"You must select the \"Install Startup Item\" option during installation to enable "
"server control from this tool."];
[toggleButton setEnabled:NO];
[startOnBootCheck setEnabled:NO];
}
*/
}
+ (NSImage*)icon
{
return [NSImage imageNamed: @"OSX-Icons_35.png"];
}
+ (NSString*)label
{
return @"Service";
}
+ (NSString*)toolTip
{
return @"Control MySQL Server Startup and Service.";
}
+ (BOOL)needsConnection
{
return NO;
}
- (id)initWithOwner: (id<MAdministratorProtocol>)owner
{
self= [super initWithNibFile: @"ServiceControl" panelOwner: owner];
_defaultFrame= [[self topView] frame];
return self;
}
- (void)dealloc
{
if (_authRef)
AuthorizationFree(_authRef, kAuthorizationFlagDestroyRights);
[super dealloc];
}
@end
|