Ignore:
Timestamp:
Sep 30, 2018 9:40:43 AM (23 months ago)
Author:
meeh <meeh@…>
Branches:
master
Children:
b5497ef
Parents:
36b758f
Message:

Mac OSX Launcher:

  • Update readme about event manager
  • RouterTask? can now detect a running router by scanning processes&arguments for i2p.jar
  • The logger will log to OSX's default: ~/Library/Logs?/I2P/[whatever].log
File:
1 edited

Legend:

Unmodified
Added
Removed
  • launchers/macosx/RouterTask.mm

    r36b758f r2233f7f  
    99#import "I2PLauncher-Swift.h"
    1010#include "AppDelegate.h"
     11
     12#include <assert.h>
     13#include <errno.h>
     14#include <stdbool.h>
     15#include <sys/sysctl.h>
    1116#endif
    1217
     
    2126@implementation I2PRouterTask
    2227
    23 
    2428- (void)routerStdoutData:(NSNotification *)notification
    2529{
     
    3236  self.userRequestedRestart = NO;
    3337  self.isRouterRunning = NO;
    34   //self.input = [NSFileHandle fileHandleWithStandardInput];
    3538  self.routerTask = [NSTask new];
    3639  self.processPipe = [NSPipe new];
     
    4851    // Cleanup
    4952    NSLog(@"termHandler triggered!");
    50     auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
    51     [swiftRouterStatus setRouterStatus: false];
    52     [swiftRouterStatus setRouterRanByUs: false];
    53     [swiftRouterStatus triggerEventWithEn:@"router_stop" details:@"normal shutdown"];
     53    [[[RouterProcessStatus alloc] init] triggerEventWithEn:@"router_stop" details:@"normal shutdown"];
    5454    [[SBridge sharedInstance] setCurrentRouterInstance:nil];
    5555    sendUserNotification(APP_IDSTR, @"I2P Router has stopped");
    5656  }];
    57     return self;
     57  return self;
    5858}
    5959
     
    8484        {
    8585                NSLog(@"Expection occurred %@", [e reason]);
    86     auto swiftRouterStatus = [[RouterProcessStatus alloc] init];
    8786    self.isRouterRunning = NO;
    88     [swiftRouterStatus triggerEventWithEn:@"router_exception" details:[e reason]];
     87    [[[RouterProcessStatus alloc] init] triggerEventWithEn:@"router_exception" details:[e reason]];
    8988    [[SBridge sharedInstance] setCurrentRouterInstance:nil];
    9089    sendUserNotification(@"An error occured, can't start the I2P Router", [e reason]);
     
    9998
    10099@end
     100
     101typedef struct kinfo_proc kinfo_proc;
     102
     103@implementation IIProcessInfo
     104- (id) init
     105{
     106  self = [super init];
     107  if (self != nil)
     108  {
     109    numberOfProcesses = -1; // means "not initialized"
     110    processList = NULL;
     111  }
     112  return self;
     113}
     114
     115- (int)numberOfProcesses
     116{
     117  return numberOfProcesses;
     118}
     119
     120- (void)setNumberOfProcesses:(int)num
     121{
     122  numberOfProcesses = num;
     123}
     124
     125- (int)getBSDProcessList:(kinfo_proc **)procList
     126   withNumberOfProcesses:(size_t *)procCount
     127{
     128#ifdef __cplusplus
     129  int             err;
     130  kinfo_proc *    result;
     131  bool            done;
     132  static const int    name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
     133  size_t          length;
     134 
     135  // a valid pointer procList holder should be passed
     136  assert( procList != NULL );
     137  // But it should not be pre-allocated
     138  assert( *procList == NULL );
     139  // a valid pointer to procCount should be passed
     140  assert( procCount != NULL );
     141 
     142  *procCount = 0;
     143  result = NULL;
     144  done = false;
     145 
     146  do
     147  {
     148    assert( result == NULL );
     149   
     150    // Call sysctl with a NULL buffer to get proper length
     151    length = 0;
     152    err = sysctl((int *)name,(sizeof(name)/sizeof(*name))-1,NULL,&length,NULL,0);
     153    if( err == -1 )
     154      err = errno;
     155   
     156    // Now, proper length is optained
     157    if( err == 0 )
     158    {
     159      result = (kinfo_proc *)malloc(length);
     160      if( result == NULL )
     161        err = ENOMEM;   // not allocated
     162    }
     163   
     164    if( err == 0 )
     165    {
     166      err = sysctl( (int *)name, (sizeof(name)/sizeof(*name))-1, result, &length, NULL, 0);
     167      if( err == -1 )
     168        err = errno;
     169     
     170      if( err == 0 )
     171        done = true;
     172      else if( err == ENOMEM )
     173      {
     174        assert( result != NULL );
     175        free( result );
     176        result = NULL;
     177        err = 0;
     178      }
     179    }
     180  }while ( err == 0 && !done );
     181 
     182  // Clean up and establish post condition
     183  if( err != 0 && result != NULL )
     184  {
     185    free(result);
     186    result = NULL;
     187  }
     188 
     189  *procList = result; // will return the result as procList
     190  if( err == 0 )
     191    *procCount = length / sizeof( kinfo_proc );
     192  assert( (err == 0) == (*procList != NULL ) );
     193  return err;
     194}
     195
     196- (void)obtainFreshProcessList
     197{
     198  int i;
     199  kinfo_proc *allProcs = 0;
     200  size_t numProcs;
     201  NSString *procName;
     202 
     203  int err =  [self getBSDProcessList:&allProcs withNumberOfProcesses:&numProcs];
     204  if( err )
     205  {
     206    numberOfProcesses = -1;
     207    processList = NULL;
     208   
     209    return;
     210  }
     211 
     212  // Construct an array for ( process name, pid, arguments concat'ed )
     213  processList = [NSMutableArray arrayWithCapacity:numProcs];
     214  for( i = 0; i < numProcs; i++ )
     215  {
     216    int pid = (int)allProcs[i].kp_proc.p_pid;
     217    procName = [NSString stringWithFormat:@"%s, pid %d, args: %s", allProcs[i].kp_proc.p_comm, pid, getArgvOfPid(pid).c_str()];
     218    [processList addObject:procName];
     219  }
     220 
     221  [self setNumberOfProcesses:(int)numProcs];
     222  free( allProcs );
     223#endif
     224}
     225
     226
     227- (BOOL)findProcessWithStringInNameOrArguments:(NSString *)procNameToSearch
     228{
     229  BOOL seenProcessThatMatch = NO;
     230  for (NSString* processInfoStr in processList) {
     231    if ([processInfoStr containsString:procNameToSearch]) {
     232      seenProcessThatMatch = YES;
     233      break;
     234    }
     235  }
     236  return seenProcessThatMatch;
     237}
     238@end
Note: See TracChangeset for help on using the changeset viewer.