File: run.h

package info (click to toggle)
tinyssh 20230101-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,244 kB
  • sloc: ansic: 12,106; sh: 1,168; python: 479; makefile: 42
file content (51 lines) | stat: -rw-r--r-- 3,344 bytes parent folder | download | duplicates (4)
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
#ifndef _RUN_H____
#define _RUN_H____

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "fail.h"

static void _run_mustfail(const char *file, unsigned long long line, void (*op)(void)) {\
                                                                                        \
    pid_t pid;                                                                          \
    int status;                                                                         \
                                                                                        \
    pid = fork();                                                                       \
    if (pid == -1) fail_(file, line, "unable to fork");                                 \
                                                                                        \
    if (pid == 0) {                                                                     \
        close(2);                                                                       \
        op();                                                                           \
        _exit(0);                                                                       \
    }                                                                                   \
                                                                                        \
    while (waitpid(pid, &status, 0) != pid) {};                                         \
    if (!WIFEXITED(status)) fail_(file, line, "process killed");                        \
    if (!WEXITSTATUS(status)) fail_(file, line, "process exited with status = 0");      \
}

static void _run_mustpass(const char *file, unsigned long long line, void (*op)(void)) {\
                                                                                        \
    pid_t pid;                                                                          \
    int status;                                                                         \
                                                                                        \
    pid = fork();                                                                       \
    if (pid == -1) fail_(file, line, "unable to fork");                                 \
                                                                                        \
    if (pid == 0) {                                                                     \
        close(2);                                                                       \
        op();                                                                           \
        _exit(111);                                                                     \
    }                                                                                   \
                                                                                        \
    while (waitpid(pid, &status, 0) != pid) {};                                         \
    if (!WIFEXITED(status)) fail_(file, line, "process killed");                        \
    if (WEXITSTATUS(status)) fail_(file, line, "process exited with status != 0");      \
}

#define run_mustfail(x) _run_mustfail(__FILE__, __LINE__, (x));
#define run_mustpass(x) _run_mustpass(__FILE__, __LINE__, (x));

#endif