source: launchers/macosx/RouterTask.h @ a9bf9e06

Last change on this file since a9bf9e06 was 2233f7f, checked in by meeh <meeh@…>, 23 months ago

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
  • Property mode set to 100644
File size: 5.2 KB
Line 
1#pragma once
2
3#include <dispatch/dispatch.h>
4#include <memory.h>
5#include <sys/sysctl.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10#include <Cocoa/Cocoa.h>
11#import <AppKit/AppKit.h>
12
13#ifdef __cplusplus
14#include <vector>
15#include <string>
16
17
18
19const std::vector<NSString*> defaultStartupFlags {
20  @"-Xmx512M",
21  @"-Xms128m",
22  @"-Djava.awt.headless=true",
23  @"-Dwrapper.logfile=/tmp/router.log",
24  @"-Dwrapper.logfile.loglevel=DEBUG",
25  @"-Dwrapper.java.pidfile=/tmp/routerjvm.pid",
26  @"-Dwrapper.console.loglevel=DEBUG"
27};
28
29const std::vector<std::string> defaultFlagsForExtractorJob {
30  "-Xmx512M",
31  "-Xms128m",
32  "-Djava.awt.headless=true"
33};
34
35#endif
36
37
38@class RTaskOptions;
39@interface RTaskOptions : NSObject
40@property (strong) NSString* binPath;
41@property (strong) NSArray<NSString *>* arguments;
42@property (strong) NSString* i2pBaseDir;
43@end
44
45@class I2PRouterTask;
46@interface I2PRouterTask : NSObject
47@property (strong) NSTask* routerTask;
48@property (strong) NSPipe *processPipe;
49@property (atomic) BOOL isRouterRunning;
50@property (atomic) BOOL userRequestedRestart;
51- (instancetype) initWithOptions : (RTaskOptions*) options;
52- (int) execute;
53- (void) requestShutdown;
54- (void) requestRestart;
55- (BOOL) isRunning;
56- (int) getPID;
57- (void)routerStdoutData:(NSNotification *)notification;
58@end
59
60
61@interface IIProcessInfo : NSObject {
62 
63@private
64  int numberOfProcesses;
65  NSMutableArray *processList;
66}
67- (id) init;
68- (int)numberOfProcesses;
69- (void)obtainFreshProcessList;
70- (BOOL)findProcessWithStringInNameOrArguments:(NSString *)procNameToSearch;
71@end
72
73#ifdef __cplusplus
74
75// Inspired by the "ps" util.
76
77inline std::string getArgvOfPid(int pid) {
78  int    mib[3], argmax, nargs, c = 0;
79  size_t    size;
80  char    *procargs, *sp, *np, *cp;
81  int show_args = 1;
82  mib[0] = CTL_KERN;
83  mib[1] = KERN_ARGMAX;
84 
85  size = sizeof(argmax);
86  if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) {
87    return std::string("sorry");
88  }
89 
90  /* Allocate space for the arguments. */
91  procargs = (char *)malloc(argmax);
92  if (procargs == NULL) {
93    return std::string("sorry");
94  }
95  /*
96   * Make a sysctl() call to get the raw argument space of the process.
97   * The layout is documented in start.s, which is part of the Csu
98   * project.  In summary, it looks like:
99   *
100   * /---------------\ 0x00000000
101   * :               :
102   * :               :
103   * |---------------|
104   * | argc          |
105   * |---------------|
106   * | arg[0]        |
107   * |---------------|
108   * :               :
109   * :               :
110   * |---------------|
111   * | arg[argc - 1] |
112   * |---------------|
113   * | 0             |
114   * |---------------|
115   * | env[0]        |
116   * |---------------|
117   * :               :
118   * :               :
119   * |---------------|
120   * | env[n]        |
121   * |---------------|
122   * | 0             |
123   * |---------------| <-- Beginning of data returned by sysctl() is here.
124   * | argc          |
125   * |---------------|
126   * | exec_path     |
127   * |:::::::::::::::|
128   * |               |
129   * | String area.  |
130   * |               |
131   * |---------------| <-- Top of stack.
132   * :               :
133   * :               :
134   * \---------------/ 0xffffffff
135   */
136  mib[0] = CTL_KERN;
137  mib[1] = KERN_PROCARGS2;
138  mib[2] = pid;
139 
140 
141  size = (size_t)argmax;
142  if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) {
143    free(procargs);
144    return std::string("sorry");
145  }
146 
147  memcpy(&nargs, procargs, sizeof(nargs));
148  cp = procargs + sizeof(nargs);
149 
150  /* Skip the saved exec_path. */
151  for (; cp < &procargs[size]; cp++) {
152    if (*cp == '\0') {
153      /* End of exec_path reached. */
154      break;
155    }
156  }
157  if (cp == &procargs[size]) {
158    free(procargs);
159    return std::string("sorry");
160  }
161 
162  /* Skip trailing '\0' characters. */
163  for (; cp < &procargs[size]; cp++) {
164    if (*cp != '\0') {
165      /* Beginning of first argument reached. */
166      break;
167    }
168  }
169  if (cp == &procargs[size]) {
170    free(procargs);
171    return std::string("sorry");
172  }
173  /* Save where the argv[0] string starts. */
174  sp = cp;
175 
176  /*
177   * Iterate through the '\0'-terminated strings and convert '\0' to ' '
178   * until a string is found that has a '=' character in it (or there are
179   * no more strings in procargs).  There is no way to deterministically
180   * know where the command arguments end and the environment strings
181   * start, which is why the '=' character is searched for as a heuristic.
182   */
183  for (np = NULL; c < nargs && cp < &procargs[size]; cp++) {
184    if (*cp == '\0') {
185      c++;
186      if (np != NULL) {
187        /* Convert previous '\0'. */
188        *np = ' ';
189      } else {
190        /* *argv0len = cp - sp; */
191      }
192      /* Note location of current '\0'. */
193      np = cp;
194     
195      if (!show_args) {
196        /*
197         * Don't convert '\0' characters to ' '.
198         * However, we needed to know that the
199         * command name was terminated, which we
200         * now know.
201         */
202        break;
203      }
204    }
205  }
206 
207  /*
208   * sp points to the beginning of the arguments/environment string, and
209   * np should point to the '\0' terminator for the string.
210   */
211  if (np == NULL || np == sp) {
212    /* Empty or unterminated string. */
213    free(procargs);
214    return std::string("sorry");
215  }
216  return std::string(sp);
217}
218
219#endif
Note: See TracBrowser for help on using the repository browser.