nappgui-sys 0.2.0

Rust raw bindings to NAppGUI
Documentation
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
/*
 * NAppGUI Cross-platform C SDK
 * 2015-2025 Francisco Garcia Collado
 * MIT Licence
 * https://nappgui.com/en/legal/license.html
 *
 * File: bproc.c
 *
 */

/* Processes */

#include "../bproc.h"
#include "../osbs.inl"
#include <sewer/bmem.h>
#include <sewer/cassert.h>
#include <sewer/ptr.h>

#if !defined(__UNIX__)
#error This file is for Unix/Unix-like system
#endif

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>

#define STDIN_READ_CHILD 0

#define STDIN_WRITE_PARENT 1

#define STDOUT_READ_PARENT 2

#define STDOUT_WRITE_CHILD 3

#define STDERR_READ_PARENT 4

#define STDERR_WRITE_CHILD 5


#if defined __LINUX__
int kill(pid_t pid, int sig);
#endif

struct _process_t
{
    int pipes[6];
    pid_t pid;
};

/*----------------------------------------------------------------------------*/

static Proc *i_create(int *pipes, pid_t pid)
{
    Proc *proc = cast(bmem_malloc(sizeof(Proc)), Proc);
    _osbs_proc_alloc();
    bmem_copy_n(proc->pipes, pipes, 6, int);
    proc->pid = pid;
    return proc;
}

/*---------------------------------------------------------------------------*/

static void i_close_pipes(int *pipes)
{
    uint32_t i;
    cassert_no_null(pipes);
    for (i = 0; i < 6; ++i)
    {
        if (pipes[i] >= 0)
            close(pipes[i]);
    }
}

/*---------------------------------------------------------------------------*/

static bool_t i_pipes(int pipes[6])
{
    pipes[0] = -1;
    pipes[1] = -1;
    pipes[2] = -1;
    pipes[3] = -1;
    pipes[4] = -1;
    pipes[5] = -1;

    if (pipe(pipes) == -1)
    {
        i_close_pipes(pipes);
        return FALSE;
    }

    if (pipe(pipes + 2) == -1)
    {
        i_close_pipes(pipes);
        return FALSE;
    }

    if (pipe(pipes + 4) == -1)
    {
        i_close_pipes(pipes);
        return FALSE;
    }

    return TRUE;
}

/*---------------------------------------------------------------------------*/

static bool_t i_exec(const char_t *command, int *pipes, pid_t *pid)
{
    cassert_no_null(pipes);
    cassert_no_null(pid);
    *pid = fork();
    if (*pid == -1)
    {
        return FALSE;
    }
    /* Child */
    else if (*pid == 0)
    {
        dup2(pipes[STDIN_READ_CHILD], STDIN_FILENO);
        close(pipes[STDIN_READ_CHILD]);
        close(pipes[STDIN_WRITE_PARENT]);

        dup2(pipes[STDOUT_WRITE_CHILD], STDOUT_FILENO);
        close(pipes[STDOUT_WRITE_CHILD]);
        close(pipes[STDOUT_READ_PARENT]);

        dup2(pipes[STDERR_WRITE_CHILD], STDERR_FILENO);
        close(pipes[STDERR_WRITE_CHILD]);
        close(pipes[STDERR_READ_PARENT]);

        execlp("bash", "bash", "-c", command, NULL);
        cassert_msg(FALSE, "It's a zombie!");
        return FALSE;
    }
    /* Parent */
    else
    {
        close(pipes[STDIN_READ_CHILD]);
        close(pipes[STDOUT_WRITE_CHILD]);
        close(pipes[STDERR_WRITE_CHILD]);
        pipes[STDIN_READ_CHILD] = -1;
        pipes[STDOUT_WRITE_CHILD] = -1;
        pipes[STDERR_WRITE_CHILD] = -1;

        /* Avoid parent process end when is block in a bproc_write with full pipe */

        /*If all file descriptors referring to the read end of a pipe have been closed,
         * then a write(2) will cause a SIGPIPE signal to be generated for the calling
         * process. If the calling process is ignoring this signal, then write(2) fails
         * with the error EPIPE */
        signal(SIGPIPE, SIG_IGN);
        return TRUE;
    }
}

/*---------------------------------------------------------------------------*/

Proc *bproc_exec(const char_t *command, perror_t *error)
{
    int pipes[6];
    pid_t pid;

    if (i_pipes(pipes) == FALSE)
    {
        ptr_assign(error, ekPPIPE);
        return NULL;
    }

    if (i_exec(command, pipes, &pid) == FALSE)
    {
        i_close_pipes(pipes);
        ptr_assign(error, ekPEXEC);
        return NULL;
    }

    return i_create(pipes, pid);
}

/*---------------------------------------------------------------------------*/

/*
   On Unix and Unix-like computer operating systems, a zombie process
   or defunct process is a process that has completed execution but still
   has an entry in the process table. This entry is still needed to allow
   the parent process to read its child's exit status.

   Processes marked <defunct> are dead processes (so-called "zombies") that
   remain because their parent has not destroyed them properly.

   If the parent process calls waitpid and child has finished, the child will
   be deleted from ps -A and will not appers like <defunc>
 */

void bproc_close(Proc **proc)
{
    cassert_no_null(proc);
    cassert_no_null(*proc);
    i_close_pipes((*proc)->pipes);
    waitpid((*proc)->pid, NULL, WNOHANG);
    _osbs_proc_dealloc();
    bmem_free(cast(*proc, byte_t));
    *proc = NULL;
}

/*---------------------------------------------------------------------------*/

bool_t bproc_cancel(Proc *proc)
{
    cassert_no_null(proc);
    if (kill(proc->pid, SIGKILL) == 0)
        return TRUE;
    else
        return FALSE;
}

/*---------------------------------------------------------------------------*/

uint32_t bproc_wait(Proc *proc)
{
    int status = 0;
    cassert_no_null(proc);
    if (waitpid(proc->pid, &status, 0) != -1)
    {
        return WEXITSTATUS(status);
    }
    else
    {
        return UINT32_MAX;
    }
}

/*---------------------------------------------------------------------------*/

bool_t bproc_finish(Proc *proc, uint32_t *code)
{
    int ret = 0, status = 0;
    int err = 0;
    cassert_no_null(proc);
    ret = waitpid(proc->pid, &status, WNOHANG);
    if (ret == proc->pid)
    {
        ptr_assign(code, (uint32_t)WEXITSTATUS(status));
        return TRUE;
    }
    else if (ret == 0)
    {
        return FALSE;
    }
    else if (ret == -1)
    {
        if ((err = errno) == ECHILD)
        {
            ptr_assign(code, (uint32_t)WEXITSTATUS(status));
            return TRUE;
        }
    }

    cassert(FALSE);
    return TRUE;
}

/*---------------------------------------------------------------------------*/

/* If a process attempts to read from an empty pipe, then read(2) will
   block until data is available.  If a process attempts to write to a
   full pipe (see below), then write(2) blocks until sufficient data has
   been read from the pipe to allow the write to complete. Nonblocking
   I/O is possible by using the fcntl(2) F_SETFL operation to enable the
   O_NONBLOCK open file status flag.

   The communication channel provided by a pipe is a byte stream: there
   is no concept of message boundaries.

   If all file descriptors referring to the write end of a pipe have
   been closed, then an attempt to read(2) from the pipe will see end-
   of-file (read(2) will return 0). If all file descriptors referring
   to the read end of a pipe have been closed, then a write(2) will
   cause a SIGPIPE signal to be generated for the calling process. If
   the calling process is ignoring this signal, then write(2) fails with
   the error EPIPE.
*/
bool_t bproc_read(Proc *proc, byte_t *data, const uint32_t size, uint32_t *rsize, perror_t *error)
{
    ssize_t lrsize;
    cassert_no_null(proc);
    lrsize = read(proc->pipes[STDOUT_READ_PARENT], cast(data, char), (unsigned)size);
    if (lrsize > 0)
    {
        ptr_assign(rsize, (uint32_t)lrsize);
        ptr_assign(error, ekPOK);
        return TRUE;
    }
    else if (lrsize == 0)
    {
        ptr_assign(rsize, 0);
        ptr_assign(error, ekPOK);
        return FALSE;
    }
    else
    {
        ptr_assign(rsize, 0);
        ptr_assign(error, ekPPIPE);
        return FALSE;
    }
}

/*---------------------------------------------------------------------------*/

bool_t bproc_eread(Proc *proc, byte_t *data, const uint32_t size, uint32_t *rsize, perror_t *error)
{
    ssize_t lrsize;
    cassert_no_null(proc);
    lrsize = read(proc->pipes[STDERR_READ_PARENT], cast(data, char), (unsigned)size);
    if (lrsize > 0)
    {
        ptr_assign(rsize, (uint32_t)lrsize);
        ptr_assign(error, ekPOK);
        return TRUE;
    }
    else if (lrsize == 0)
    {
        ptr_assign(rsize, 0);
        ptr_assign(error, ekPOK);
        return FALSE;
    }
    else
    {
        ptr_assign(rsize, 0);
        ptr_assign(error, ekPPIPE);
        return FALSE;
    }
}

/*---------------------------------------------------------------------------*/

bool_t bproc_write(Proc *proc, const byte_t *data, const uint32_t size, uint32_t *wsize, perror_t *error)
{
    ssize_t lwsize;
    cassert_no_null(proc);
    lwsize = write(proc->pipes[STDIN_WRITE_PARENT], cast_const(data, char), (unsigned)size);
    if (lwsize >= 0)
    {
        ptr_assign(wsize, (uint32_t)lwsize);
        ptr_assign(error, ekPOK);
        return TRUE;
    }
    else
    {
        ptr_assign(wsize, 0);
        if (errno == EPIPE)
        {
            ptr_assign(error, ekPOK);
        }
        else
        {
            ptr_assign(error, ekPPIPE);
        }
        return FALSE;
    }
}

/*---------------------------------------------------------------------------*/

bool_t bproc_read_close(Proc *proc)
{
    cassert_no_null(proc);
    if (proc->pipes[STDOUT_READ_PARENT] != -1)
    {
        close(proc->pipes[STDOUT_READ_PARENT]);
        proc->pipes[STDOUT_READ_PARENT] = -1;
        return TRUE;
    }
    return FALSE;
}

/*---------------------------------------------------------------------------*/

bool_t bproc_eread_close(Proc *proc)
{
    cassert_no_null(proc);
    if (proc->pipes[STDERR_READ_PARENT] != -1)
    {
        close(proc->pipes[STDERR_READ_PARENT]);
        proc->pipes[STDERR_READ_PARENT] = -1;
        return TRUE;
    }
    return FALSE;
}

/*---------------------------------------------------------------------------*/

bool_t bproc_write_close(Proc *proc)
{
    cassert_no_null(proc);
    if (proc->pipes[STDIN_WRITE_PARENT] != -1)
    {
        close(proc->pipes[STDIN_WRITE_PARENT]);
        proc->pipes[STDIN_WRITE_PARENT] = -1;
        return TRUE;
    }
    return FALSE;
}

/*---------------------------------------------------------------------------*/

void bproc_exit(const uint32_t code)
{
    exit((int)code);
}