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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include "gate/gatemain.h"
#include "gate/results.h"
#include "gate/applications.h"
#include "gate/console.h"
#include "gate/processes.h"
#include "gate/system/services.h"

#if defined(GATE_COMPILER_MSVC) && defined(GATE_LEAK_DETECTION) && defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINCE) && (GATE_ARCH != GATE_ARCH_ARM32) && (GATE_ARCH != GATE_ARCH_ARM64)
#	include <vld.h>
#endif

typedef struct procinfo_args_class
{
    gate_bool_t		help;
    gate_bool_t		list;
    gate_string_t	start;
    gate_string_t	run;
    gate_string_t	view;
    gate_string_t	term;
    gate_string_t	kill;
    gate_bool_t		list_services;
    gate_string_t	view_service;
    gate_string_t	start_service;
    gate_string_t	stop_service;
    gate_string_t	kill_service;
} procinfo_args_t;

static procinfo_args_t procinfo_args;
static gate_app_option_t procinfo_options[] =
{
    GATE_APP_OPTION_INIT_STATIC("list", GATE_APP_OPTION_TYPE_SWITCH, &procinfo_args.list, "L", "list_processes", "List running processes"),
    GATE_APP_OPTION_INIT_STATIC("start", GATE_APP_OPTION_TYPE_STRING, &procinfo_args.start, "S", "executable_path", "Starts a new process"),
    GATE_APP_OPTION_INIT_STATIC("run", GATE_APP_OPTION_TYPE_STRING, &procinfo_args.run, "R", "executable_path", "Runs a new process in background"),
    GATE_APP_OPTION_INIT_STATIC("view", GATE_APP_OPTION_TYPE_STRING, &procinfo_args.view, "V", "process_id", "View details about a given process"),
    GATE_APP_OPTION_INIT_STATIC("term", GATE_APP_OPTION_TYPE_STRING, &procinfo_args.term, "T", "process_id", "Sends a termination request to the specified process"),
    GATE_APP_OPTION_INIT_STATIC("kill", GATE_APP_OPTION_TYPE_STRING, &procinfo_args.kill, "K", "process_id", "Kills the specified process"),
    GATE_APP_OPTION_INIT_STATIC("list-services", GATE_APP_OPTION_TYPE_SWITCH, &procinfo_args.list_services, "LS", "list_services", "Lists registered service entries"),
    GATE_APP_OPTION_INIT_STATIC("view-service", GATE_APP_OPTION_TYPE_STRING, &procinfo_args.view_service, "VS", "service_name", "View service status informations"),
    GATE_APP_OPTION_INIT_STATIC("start-service", GATE_APP_OPTION_TYPE_STRING, &procinfo_args.start_service, "STS", "service_name", "Starts a service"),
    GATE_APP_OPTION_INIT_STATIC("stop-service", GATE_APP_OPTION_TYPE_STRING, &procinfo_args.stop_service, "SPS", "service_name", "Stops a running service"),
    GATE_APP_OPTION_INIT_STATIC("kill-service", GATE_APP_OPTION_TYPE_STRING, &procinfo_args.kill_service, "KS", "service_name", "Kills a running service"),

    GATE_APP_OPTION_INIT_STATIC("help", GATE_APP_OPTION_TYPE_SWITCH, &procinfo_args.help, "?", "show_help", "Shows help")
};
static gate_size_t procinfo_options_count = sizeof(procinfo_options) / sizeof(procinfo_options[0]);

static void print_help(gate_app_option_t const* options, gate_size_t options_count, gate_stream_t* strm)
{
    gate_app_options_print(options, options_count, strm);
}

static bool list_process_cb(gate_process_t* process, void* userparam)
{
    char pidbuffer[256];
    gate_stream_t* strm = (gate_stream_t*)userparam;
    gate_process_print_pid(process->pid, pidbuffer, sizeof(pidbuffer));
    gate_stream_print(strm,
        GATE_PRINT_CSTR, pidbuffer,
        GATE_PRINT_CSTR, "\t", GATE_PRINT_CSTR, process->name,
        GATE_PRINT_CSTR, "\t", GATE_PRINT_CSTR, process->path,
        GATE_PRINT_NEWLINE, GATE_PRINT_END);
    return true;
}

static int list_processes(gate_stream_t* strm)
{
    gate_result_t result = gate_process_enum(&list_process_cb, strm, GATE_PROCESS_ENUM_PATH | GATE_PROCESS_ENUM_NAME);
    return GATE_RESULT_TO_EXITCODE(result);
}

static int terminate_process(gate_stream_t* strm, gate_string_t const* str_pid)
{
    gate_process_id_t pid = 0;
    gate_size_t len_parsed;
    gate_result_t result;
    len_parsed = gate_process_parse_pid(str_pid->str, str_pid->length, &pid);
    if (len_parsed == 0)
    {
        gate_stream_print(strm, GATE_PRINT_CSTR, "Failed to parse PID", GATE_PRINT_NEWLINE, GATE_PRINT_END);
        return 1;
    }
    gate_stream_print(strm,
        GATE_PRINT_CSTR, "Terminating process with PID ",
        GATE_PRINT_STRING, str_pid,
        GATE_PRINT_CSTR, " ... ",
        GATE_PRINT_END);
    result = gate_process_terminate_pid(pid, true);
    if (GATE_SUCCEEDED(result))
    {
        gate_stream_print(strm, GATE_PRINT_CSTR, "succeeded", GATE_PRINT_NEWLINE, GATE_PRINT_END);
    }
    else
    {
        gate_stream_print(strm, GATE_PRINT_CSTR, "failed", GATE_PRINT_NEWLINE, GATE_PRINT_END);
        gate_stream_println_cstr(strm, gate_result_text(result));
    }

    return GATE_RESULT_TO_EXITCODE(result);
}

static int kill_process(gate_stream_t* strm, gate_string_t const* str_pid)
{
    gate_process_id_t pid = 0;
    gate_size_t len_parsed;
    gate_result_t result;
    len_parsed = gate_process_parse_pid(str_pid->str, str_pid->length, &pid);
    if (len_parsed == 0)
    {
        gate_stream_print(strm, GATE_PRINT_CSTR, "Failed to parse PID", GATE_PRINT_NEWLINE, GATE_PRINT_END);
        return 1;
    }
    gate_stream_print(strm,
        GATE_PRINT_CSTR, "Killing process with PID ",
        GATE_PRINT_STRING, str_pid,
        GATE_PRINT_CSTR, " ... ",
        GATE_PRINT_END);
    result = gate_process_kill_pid(pid);
    if (GATE_SUCCEEDED(result))
    {
        gate_stream_print(strm, GATE_PRINT_CSTR, "succeeded", GATE_PRINT_NEWLINE, GATE_PRINT_END);
    }
    else
    {
        gate_stream_print(strm, GATE_PRINT_CSTR, "failed", GATE_PRINT_NEWLINE, GATE_PRINT_END);
        gate_stream_println_cstr(strm, gate_result_text(result));
    }

    return GATE_RESULT_TO_EXITCODE(result);
}

static int view_process(gate_stream_t* strm, gate_string_t const* str_pid)
{
    gate_process_id_t pid = 0;
    gate_size_t len_parsed;
    gate_result_t result;
    gate_process_infobuffer_t infobuffer;
    char pidbuffer[256];
    char ppidbuffer[256];

    len_parsed = gate_process_parse_pid(str_pid->str, str_pid->length, &pid);
    if (len_parsed == 0)
    {
        gate_stream_print(strm, GATE_PRINT_CSTR, "Failed to parse PID", GATE_PRINT_NEWLINE, GATE_PRINT_END);
        return GATE_RESULT_TO_EXITCODE(GATE_RESULT_NOMATCH);
    }
    gate_stream_print(strm,
        GATE_PRINT_CSTR, "Reading process infos from PID ",
        GATE_PRINT_STRING, str_pid,
        GATE_PRINT_CSTR, " ... ",
        GATE_PRINT_END);
    result = gate_process_getinfo(&infobuffer, pid,
        GATE_PROCESS_ENUM_NAME | GATE_PROCESS_ENUM_PATH | GATE_PROCESS_ENUM_OWNER |
        GATE_PROCESS_ENUM_MEMORY | GATE_PROCESS_ENUM_TIMES | GATE_PROCESS_ENUM_RESOURCES);
    if (GATE_SUCCEEDED(result))
    {
        gate_process_print_pid(infobuffer.process.pid, pidbuffer, sizeof(pidbuffer));
        gate_process_print_pid(infobuffer.process.parent_pid, ppidbuffer, sizeof(ppidbuffer));
        gate_stream_print(
            strm,
            GATE_PRINT_CSTR, "succeeded", GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Name:       ", GATE_PRINT_CSTR, infobuffer.process.name, GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Path:       ", GATE_PRINT_CSTR, infobuffer.process.path, GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "ID:         ", GATE_PRINT_CSTR, pidbuffer, GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Parent-ID:  ", GATE_PRINT_CSTR, ppidbuffer, GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Owner:      ", GATE_PRINT_CSTR, infobuffer.process.owner, GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Memory:     ", GATE_PRINT_I64, infobuffer.process.memory_used, GATE_PRINT_CSTR, " bytes", GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Resources:  ", GATE_PRINT_I64, infobuffer.process.resources, GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "CPU-time:   ", GATE_PRINT_I64, infobuffer.process.cputime, GATE_PRINT_CSTR, " ms", GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Uptime:     ", GATE_PRINT_I64, infobuffer.process.uptime, GATE_PRINT_CSTR, " ms", GATE_PRINT_NEWLINE,
            GATE_PRINT_END);
    }
    else
    {
        gate_stream_print(strm, GATE_PRINT_CSTR, "failed", GATE_PRINT_NEWLINE, GATE_PRINT_END);
    }

    return GATE_RESULT_TO_EXITCODE(result);
}


static int start_process(gate_stream_t* strm, gate_string_t const* path)
{
    gate_process_handle_t phandle;
    gate_process_id_t pid;
    char pidbuffer[256];
    gate_result_t result;
    gate_enumint_t flags = 0;

    gate_stream_print(strm,
        GATE_PRINT_CSTR, "Starting process: ",
        GATE_PRINT_STRING, path,
        GATE_PRINT_CSTR, " ... ", GATE_PRINT_END);

    result = gate_process_start(path, NULL, 0, NULL, NULL, 0,
        flags, &phandle, &pid, NULL);

    if (GATE_SUCCEEDED(result))
    {
        gate_process_print_pid(pid, pidbuffer, sizeof(pidbuffer));
        gate_stream_print(strm,
            GATE_PRINT_CSTR, "succeeded: ",
            GATE_PRINT_CSTR, pidbuffer,
            GATE_PRINT_NEWLINE, GATE_PRINT_END);
    }
    else
    {
        gate_stream_print(strm,
            GATE_PRINT_CSTR, "failed",
            GATE_PRINT_NEWLINE, GATE_PRINT_END);
        gate_stream_println_cstr(strm, gate_result_text(result));
    }

    return GATE_RESULT_TO_EXITCODE(result);
}


static int run_process(gate_stream_t* strm, gate_string_t const* path)
{
    gate_process_handle_t phandle = GATE_PROCESS_HANDLE_INVALID;
    gate_process_id_t pid;
    char pidbuffer[256];
    gate_result_t result;
    gate_enumint_t flags = GATE_PROCESS_START_NEWTERMINAL;
    gate_process_stream_t* ptr_procstream = NULL;
    gate_memstream_t* ptr_mem = NULL;
    gate_string_t proc_output = GATE_STRING_INIT_EMPTY;

    do
    {
        gate_stream_print(strm,
            GATE_PRINT_CSTR, "Running process: ",
            GATE_PRINT_STRING, path,
            GATE_PRINT_NEWLINE, GATE_PRINT_END);

        result = gate_process_start(path, NULL, 0, NULL, NULL, 0,
            flags, &phandle, &pid, &ptr_procstream);

        if (GATE_FAILED(result))
        {
            gate_stream_print(strm,
                GATE_PRINT_CSTR, "failed",
                GATE_PRINT_NEWLINE, GATE_PRINT_END);
            gate_stream_println_cstr(strm, gate_result_text(result));
            break;
        }

        ptr_mem = gate_memstream_create(1024);
        if (!ptr_mem)
        {
            result = GATE_RESULT_OUTOFMEMORY;
            break;
        }

        result = gate_stream_transfer((gate_stream_t*)ptr_procstream, (gate_stream_t*)ptr_mem);

        gate_string_create_static_len(&proc_output, gate_memstream_get_data(ptr_mem), gate_memstream_size(ptr_mem));

        gate_stream_print(strm,
            GATE_PRINT_CSTR, "Captured process output:", GATE_PRINT_NEWLINE,
            GATE_PRINT_STRING, &proc_output, GATE_PRINT_NEWLINE,
            GATE_PRINT_END);

        gate_string_release(&proc_output);
    } while (0);

    if (ptr_procstream)
    {
        gate_object_release(ptr_procstream);
    }
    if (ptr_mem)
    {
        gate_object_release(ptr_mem);
    }
    if (phandle != GATE_PROCESS_HANDLE_INVALID)
    {
        gate_process_close(&phandle);
    }

    return GATE_RESULT_TO_EXITCODE(result);
}

static gate_bool_t list_services_callback(gate_service_t const* service, void* user_param)
{
    gate_stream_t* const strm = (gate_stream_t*)user_param;
    gate_stream_print(strm,
        GATE_PRINT_CSTR, service->name,
        GATE_PRINT_CSTR, "\t",
        GATE_PRINT_CSTR, service->description,
        GATE_PRINT_NEWLINE,
        GATE_PRINT_END);
    return true;
}

static int list_services(gate_stream_t* strm)
{
    gate_result_t result = gate_services_enum(&list_services_callback, (void*)strm);
    return GATE_RESULT_TO_EXITCODE(result);
}

static int view_service(gate_stream_t* strm, gate_string_t const* service)
{
    gate_result_t result;
    gate_service_config_t config;
    gate_enumint_t status = 0;
    gate_string_t strpid = GATE_STRING_INIT_EMPTY;

    gate_stream_println_cstr(strm, "Reading service status information...");

    result = gate_service_get_config(service, &config);
    if (GATE_SUCCEEDED(result))
    {
        gate_stream_print(strm,
            GATE_PRINT_CSTR, "Name:\t", GATE_PRINT_CSTR, config.name, GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Description:\t", GATE_PRINT_CSTR, config.description, GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Command:\t", GATE_PRINT_CSTR, config.command, GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Dependencies:\t", GATE_PRINT_CSTR, config.dependencies, GATE_PRINT_NEWLINE,
            GATE_PRINT_END);
    }

    result = gate_service_get_status(service, &status, &strpid);
    if (GATE_SUCCEEDED(result))
    {
        gate_stream_print(strm,
            GATE_PRINT_CSTR, "Status:\t", GATE_PRINT_CSTR, gate_service_print_state(status), GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "PID:\t", GATE_PRINT_STRING, &strpid, GATE_PRINT_NEWLINE,
            GATE_PRINT_END);
        gate_string_release(&strpid);
    }

    return GATE_RESULT_TO_EXITCODE(result);
}


static void service_message_callback(gate_string_t const* message, void* user_param)
{
    gate_stream_t* const strm = (gate_stream_t*)user_param;
    gate_stream_print(strm, GATE_PRINT_STRING, message,
        GATE_PRINT_NEWLINE, GATE_PRINT_END);
}

static int start_service(gate_stream_t* strm, gate_string_t const* service)
{
    gate_result_t result;

    gate_stream_println_cstr(strm, "Starting service...");
    result = gate_service_start(service, &service_message_callback, (void*)strm);
    gate_stream_println_cstr(strm, NULL);
    if (GATE_SUCCEEDED(result))
    {
        gate_stream_println_cstr(strm, "Service started");
    }
    else
    {
        gate_stream_print(strm,
            GATE_PRINT_CSTR, "Failed to start service: ",
            GATE_PRINT_CSTR, gate_result_text(result),
            GATE_PRINT_NEWLINE, GATE_PRINT_END);
    }
    return GATE_RESULT_TO_EXITCODE(result);
}

static int stop_service(gate_stream_t* strm, gate_string_t const* service, gate_bool_t force)
{
    gate_result_t result;

    gate_stream_println_cstr(strm, "Stopping service...");
    result = gate_service_stop(service, 0, force, &service_message_callback, (void*)strm);
    gate_stream_println_cstr(strm, NULL);
    if (GATE_SUCCEEDED(result))
    {
        gate_stream_println_cstr(strm, "Service stopped");
    }
    else
    {
        gate_stream_print(strm,
            GATE_PRINT_CSTR, "Failed to stop service: ",
            GATE_PRINT_CSTR, gate_result_text(result),
            GATE_PRINT_NEWLINE, GATE_PRINT_END);
    }
    return GATE_RESULT_TO_EXITCODE(result);
}


static void release_procinfo_args(procinfo_args_t* args)
{
    args->list = false;
    args->list_services = false;
    args->help = false;
    gate_string_release(&args->run);
    gate_string_release(&args->start);
    gate_string_release(&args->term);
    gate_string_release(&args->kill);
    gate_string_release(&args->view);
    gate_string_release(&args->start_service);
    gate_string_release(&args->stop_service);
    gate_string_release(&args->kill_service);
}

int gate_main(char const* program, char const* const* arguments, gate_size_t argcount, gate_uintptr_t apphandle)
{
    int ret = 0;
    gate_console_t* con = gate_console();
    gate_stream_t* strm = (gate_stream_t*)con;

    GATE_UNUSED_ARG(program);
    GATE_UNUSED_ARG(apphandle);

    gate_app_options_parse_strs(arguments, argcount, procinfo_options, procinfo_options_count);

    if (procinfo_args.help)
    {
        print_help(procinfo_options, procinfo_options_count, strm);
    }
    else if (procinfo_args.list)
    {
        ret = list_processes(strm);
    }
    else if (!gate_string_is_empty(&procinfo_args.term))
    {
        ret = terminate_process(strm, &procinfo_args.term);
    }
    else if (!gate_string_is_empty(&procinfo_args.kill))
    {
        ret = kill_process(strm, &procinfo_args.kill);
    }
    else if (!gate_string_is_empty(&procinfo_args.view))
    {
        ret = view_process(strm, &procinfo_args.view);
    }
    else if (!gate_string_is_empty(&procinfo_args.start))
    {
        ret = start_process(strm, &procinfo_args.start);
    }
    else if (!gate_string_is_empty(&procinfo_args.run))
    {
        ret = run_process(strm, &procinfo_args.run);
    }
    else if (procinfo_args.list_services)
    {
        ret = list_services(strm);
    }
    else if (!gate_string_is_empty(&procinfo_args.view_service))
    {
        ret = view_service(strm, &procinfo_args.view_service);
    }
    else if (!gate_string_is_empty(&procinfo_args.start_service))
    {
        ret = start_service(strm, &procinfo_args.start_service);
    }
    else if (!gate_string_is_empty(&procinfo_args.stop_service))
    {
        ret = stop_service(strm, &procinfo_args.stop_service, false);
    }
    else if (!gate_string_is_empty(&procinfo_args.kill_service))
    {
        ret = stop_service(strm, &procinfo_args.kill_service, true);
    }
    else
    {
        gate_stream_print(strm,
            GATE_PRINT_CSTR, "Missing argument", GATE_PRINT_NEWLINE,
            GATE_PRINT_CSTR, "Use '--help' or '-?' to show supported arguments", GATE_PRINT_NEWLINE,
            GATE_PRINT_END);
        ret = 1;
    }

    release_procinfo_args(&procinfo_args);
    return ret;
}