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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at>                 |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/
#include "gate/processes.hpp"
#include "gate/utilities.hpp"
#include "gate/times.hpp"

namespace gate
{
    Process::Instance::Instance(id_t processId)
        : procId(processId)
    {
    }
    Process::Instance::Instance(Instance const& src)
        : procId(src.procId)
    {
    }
    Process::Instance& Process::Instance::operator=(Instance const& src)
    {
        this->procId = src.procId;
        return *this;
    }
    Process::Instance::~Instance()
    {
    }

    Process::id_t Process::Instance::getId() const noexcept
    {
        return this->procId;
    }
    bool_t Process::Instance::wait(uint32_t timeoutMS)
    {
        return Process::wait(this->procId, timeoutMS);
    }
    void Process::Instance::wait()
    {
        Process::wait(this->procId, GATE_PROCESS_ID_INVALID);
    }
    void Process::Instance::terminate(bool_t systemRequest)
    {
        Process::terminate(this->procId, systemRequest);
    }
    void Process::Instance::kill()
    {
        Process::kill(this->procId);
    }
    void Process::Instance::suspend()
    {
        Process::suspend(this->procId);
    }
    void Process::Instance::resume()
    {
        Process::resume(this->procId);
    }


    Process::Child::Child() noexcept
        : Instance(GATE_PROCESS_ID_INVALID),
        childHandle(GATE_PROCESS_HANDLE_INVALID),
        childStream(NullStream())
    {
    }
    Process::Child::~Child() noexcept
    {
        if (this->childHandle != GATE_PROCESS_HANDLE_INVALID)
        {
            Process::close(this->childHandle);
        }
    }

    Process::handle_t Process::Child::getHandle() const noexcept
    {
        return this->childHandle;
    }

    Stream& Process::Child::getStream() noexcept
    {
        return this->childStream;
    }

    bool_t Process::Child::wait(uint32_t timeoutMS)
    {
        return Process::wait(this->childHandle, timeoutMS);
    }
    void Process::Child::wait()
    {
        this->wait(GATE_PROCESS_WAIT_INFINITE);
    }
    void Process::Child::terminate(bool_t systemRequest)
    {
        Process::terminate(this->childHandle, systemRequest);
    }
    void Process::Child::kill()
    {
        Process::kill(this->childHandle);
    }
    void Process::Child::suspend()
    {
        Process::suspend(this->childHandle);
    }
    void Process::Child::resume()
    {
        Process::resume(this->childHandle);
    }
    int Process::Child::getExitCode()
    {
        return Process::getExitCode(this->childHandle);
    }


    void Process::Info::init(gate_process_t const& process)
    {
        this->name = String(process.name);
        this->path = String(process.path);
        this->owner = String(process.owner);

        Mem::copy(&this->impl, &process, sizeof(this->impl));
        this->impl.name = this->name.c_str();
        this->impl.path = this->path.c_str();
        this->impl.owner = this->owner.c_str();
    }

    Process::Info::Info()
    {
        gate_process_t p;
        Mem::clear(p);
        this->init(p);
    }
    Process::Info::Info(gate_process_t const& proc)
        : impl(proc)
    {
        this->init(proc);
    }
    Process::Info::Info(Info const& src)
        : impl(src.impl)
    {
        this->init(src.impl);
    }
    Process::Info& Process::Info::operator=(Info const& src)
    {
        if (this != &src)
        {
            this->init(src.impl);
        }
        return *this;
    }

    Process::id_t Process::Info::Id() const
    {
        return this->impl.pid;
    }
    Process::id_t Process::Info::Parent() const
    {
        return this->impl.parent_pid;
    }
    uint64_t Process::Info::MemoryUsed() const
    {
        return this->impl.memory_used;
    }
    time::Milliseconds Process::Info::UpTime() const
    {
        return time::Milliseconds(static_cast<time::duration_value_t>(this->impl.uptime));
    }
    time::Milliseconds Process::Info::CpuTime() const
    {
        return time::Milliseconds(static_cast<time::duration_value_t>(this->impl.cputime));
    }
    uint64_t Process::Info::Resources() const
    {
        return this->impl.resources;
    }
    String Process::Info::Name() const
    {
        return this->name;
    }
    String Process::Info::Path() const
    {
        return this->path;
    }
    String Process::Info::Owner() const
    {
        return this->owner;
    }

    static gate_bool_t Process_list_callback(gate_process_t* process, void* userparam)
    {
        Process::ListCallback* cb = static_cast<Process::ListCallback*>(userparam);
        Process::Info info(*process);
        gate_bool_t ret = true;
        cb->invoke(info, ret);
        return ret;
    }

    Process::id_t Process::getId()
    {
        gate_process_id_t id = 0;
        result_t result = gate_process_get_id(&id);
        GATEXX_CHECK_ERROR(result);
        return id;
    }


    void Process::list(ListCallback callback, enumint_t flags)
    {
        result_t result = gate_process_enum(&Process_list_callback, &callback, flags);
        GATEXX_CHECK_EXCEPTION(result);
    }

    static gate_bool_t Process_list_array_callback(gate_process_t* process, void* userparam)
    {
        ArrayList<Process::Info>* ptrList = static_cast<ArrayList< Process::Info>*>(userparam);
        Process::Info info(*process);
        try
        {
            ptrList->add(info);
        }
        catch (...) {}
        return true;
    }

    Array<Process::Info> Process::list(enumint_t flags)
    {
        ArrayList<Process::Info> list;
        result_t result = gate_process_enum(&Process_list_array_callback, &list, flags);
        GATEXX_CHECK_EXCEPTION(result);
        return list.toArray();
    }

    Process::Info Process::getInfo(id_t pid, enumint_t flags)
    {
        gate_process_infobuffer_t buffer;
        result_t result = gate_process_getinfo(&buffer, pid, flags);
        GATEXX_CHECK_EXCEPTION(result);
        return Info(buffer.process);
    }

    void Process::start(String const& executablepath, Array<String> const& args,
        String const& workdir, Array<String> const& envvars, enumint_t flags,
        handle_t* result_handle,
        id_t* result_pid,
        Stream* result_stream)
    {
        gate_string_t arglist[128];
        gate_string_t envlist[128];
        gate_process_stream_t* streamptr = NULL;

        size_t argcount = util::convertStringArray(args, arglist, 128);
        size_t envcount = util::convertStringArray(envvars, envlist, 128);

        result_t result = gate_process_start(executablepath.c_impl(), arglist, argcount, workdir.c_impl(),
            envlist, envcount, flags, result_handle, result_pid,
            result_stream ? &streamptr : NULL);

        util::releaseStringArray(envlist, envcount);
        util::releaseStringArray(arglist, argcount);

        GATEXX_CHECK_EXCEPTION(result);

        if (streamptr)
        {
            if (result_stream)
            {
                *result_stream = Stream((gate_stream_t*)streamptr);
            }
            else
            {
                gate_object_release(streamptr);
            }
        }
    }

    void Process::start(String const& executablepath, Array<String> const& args,
        String const& workdir, Array<String> const& envvars, enumint_t flags,
        Child& child)
    {
        handle_t handle = GATE_PROCESS_HANDLE_INVALID;
        id_t id = GATE_PROCESS_ID_INVALID;
        Stream strm = NullStream();
        Process::start(executablepath, args, workdir, envvars, flags, &handle, &id, &strm);
        child.childHandle = handle;
        child.procId = id;
        child.childStream = strm;
    }


    void Process::start(String const& executablepath, Array<String> const& args,
        String const& workdir, Array<String> const& envvars, enumint_t flags,
        String const& sessionlocation,
        String const& username, String const& password,
        handle_t* result_handle,
        id_t* result_pid,
        Stream* result_stream)
    {
        gate_string_t arglist[128];
        gate_string_t envlist[128];
        char session[256];
        char user[256];
        char pass[512];
        gate_process_stream_t* streamptr = NULL;

        sessionlocation.copyTo(session, sizeof(session));
        username.copyTo(user, sizeof(user));
        password.copyTo(pass, sizeof(pass));

        size_t argcount = util::convertStringArray(args, arglist, 128);
        size_t envcount = util::convertStringArray(envvars, envlist, 128);

        result_t result = gate_process_start_ex(executablepath.c_impl(), arglist, argcount, workdir.c_impl(),
            envlist, envcount, flags,
            session, user, pass,
            result_handle, result_pid,
            result_stream ? &streamptr : NULL);

        util::releaseStringArray(envlist, envcount);
        util::releaseStringArray(arglist, argcount);

        GATEXX_CHECK_EXCEPTION(result);

        if (streamptr)
        {
            if (result_stream)
            {
                *result_stream = Stream((gate_stream_t*)streamptr);
            }
            else
            {
                gate_object_release(streamptr);
            }
        }
    }

    VoidResult Process::close(handle_t& handle)
    {
        result_t result = gate_process_close(&handle);
        return makeResult(result);
    }

    int Process::getExitCode(handle_t& handle)
    {
        int exitcode = 0;
        result_t result = gate_process_get_exitcode(&handle, &exitcode);
        GATEXX_CHECK_EXCEPTION(result);
        return exitcode;
    }

    void Process::run(String const& executablepath, Array<String> const& args,
        String const& workdir, Array<String> const& envVars,
        enumint_t flags, Stream* output, int* exit_code)
    {
        gate_string_t argslist[128];
        gate_string_t envlist[128];
        gate_stream_t* ptr_stream = NULL;<--- Assignment 'ptr_stream=NULL', assigned value is 0
        size_t argscount = util::convertStringArray(args, argslist, 128);
        size_t envcount = util::convertStringArray(envVars, envlist, 128);
        result_t result = gate_process_run(executablepath.c_impl(), argslist, argscount, workdir.c_impl(),
            envlist, envcount, flags,
            output ? ptr_stream : NULL, exit_code);
        util::releaseStringArray(envlist, envcount);
        util::releaseStringArray(argslist, argscount);
        GATEXX_CHECK_EXCEPTION(result);
        if (ptr_stream)<--- Condition 'ptr_stream' is always false
        {
            if (output)
            {
                *output = Stream(ptr_stream);
            }
            else
            {
                gate_object_release(ptr_stream);
            }
        }
    }

    bool_t Process::wait(handle_t& handle, uint32_t timeoutms)
    {
        result_t result = gate_process_wait(&handle, timeoutms);
        if (results::Timeout == result)
        {
            return false;
        }
        GATEXX_CHECK_EXCEPTION(result);
        return true;
    }

    bool_t Process::wait(id_t pid, uint32_t timeoutms)
    {
        result_t result = gate_process_wait_pid(pid, timeoutms);
        if (results::Timeout == result)
        {
            return false;
        }
        GATEXX_CHECK_EXCEPTION(result);
        return true;
    }

    void Process::terminate(handle_t& handle, bool_t system_request)
    {
        result_t result = gate_process_terminate(&handle, system_request);
        GATEXX_CHECK_EXCEPTION(result);
    }

    void Process::terminate(id_t pid, bool_t system_request)
    {
        result_t result = gate_process_terminate_pid(pid, system_request);
        GATEXX_CHECK_EXCEPTION(result);
    }

    void Process::kill(handle_t& handle)
    {
        result_t result = gate_process_kill(&handle);
        GATEXX_CHECK_EXCEPTION(result);
    }

    void Process::kill(id_t pid)
    {
        result_t result = gate_process_kill_pid(pid);
        GATEXX_CHECK_EXCEPTION(result);
    }

    void Process::suspend(handle_t& handle)
    {
        result_t result = gate_process_suspend(&handle);
        GATEXX_CHECK_EXCEPTION(result);
    }

    void Process::suspend(id_t pid)
    {
        result_t result = gate_process_suspend_pid(pid);
        GATEXX_CHECK_EXCEPTION(result);
    }

    void Process::resume(handle_t& handle)
    {
        result_t result = gate_process_resume(&handle);
        GATEXX_CHECK_EXCEPTION(result);
    }

    void Process::resume(id_t pid)
    {
        result_t result = gate_process_resume_pid(pid);
        GATEXX_CHECK_EXCEPTION(result);
    }

    String Process::printPid(gate_process_id_t pid)
    {
        char buffer[64];
        size_t used = gate_process_print_pid(pid, buffer, 64);
        return String(buffer, used);
    }

    Process::id_t Process::parsePid(String const& text)
    {
        id_t ret = 0;
        size_t len = gate_process_parse_pid(text.c_str(), text.length(), &ret);
        if (len == 0)
        {
            GATEXX_RAISE_EXCEPTION(results::InvalidData, "Cannot parse PID", 0);
        }
        return ret;
    }


}	//	end of namespace gate