summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_process.h
blob: 343fffb854c83634ccb9c1e409b28ca5cbfe65d4 (plain) (blame)
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

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#ifndef _NXT_PROCESS_H_INCLUDED_
#define _NXT_PROCESS_H_INCLUDED_

#if (NXT_HAVE_CLONE)
#include <nxt_clone.h>
#endif


typedef pid_t            nxt_pid_t;


typedef struct nxt_process_init_s  nxt_process_init_t;
typedef nxt_int_t (*nxt_process_start_t)(nxt_task_t *task, void *data);
typedef nxt_int_t (*nxt_process_restart_t)(nxt_task_t *task, nxt_runtime_t *rt,
    nxt_process_init_t *init);

struct nxt_process_init_s {
    nxt_mp_t                   *mem_pool;
    nxt_process_start_t        start;
    const char                 *name;
    nxt_credential_t           *user_cred;

    const nxt_port_handlers_t  *port_handlers;
    const nxt_sig_event_t      *signals;

    nxt_process_type_t         type;

    void                       *data;
    uint32_t                   stream;

    union {
#if (NXT_HAVE_CLONE)
        nxt_clone_t            clone;
#endif
    } isolation;
};


typedef struct nxt_port_mmap_s  nxt_port_mmap_t;
typedef struct nxt_port_mmaps_s nxt_port_mmaps_t;

struct nxt_port_mmaps_s {
    nxt_thread_mutex_t  mutex;
    uint32_t            size;
    uint32_t            cap;
    nxt_port_mmap_t     *elts;
};


typedef struct {
    nxt_pid_t           pid;
    nxt_queue_t         ports;      /* of nxt_port_t */
    nxt_bool_t          ready;
    nxt_bool_t          registered;
    nxt_int_t           use_count;

    nxt_process_init_t  *init;

    nxt_port_mmaps_t    incoming;
    nxt_port_mmaps_t    outgoing;

    nxt_thread_mutex_t  cp_mutex;
    nxt_lvlhsh_t        connected_ports; /* of nxt_port_t */
} nxt_process_t;


extern nxt_bool_t  nxt_proc_conn_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
extern nxt_bool_t
          nxt_proc_remove_notify_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];

NXT_EXPORT nxt_pid_t nxt_process_create(nxt_task_t *task,
    nxt_process_t *process);
NXT_EXPORT nxt_pid_t nxt_process_execute(nxt_task_t *task, char *name,
    char **argv, char **envp);
NXT_EXPORT nxt_int_t nxt_process_daemon(nxt_task_t *task);
NXT_EXPORT void nxt_nanosleep(nxt_nsec_t ns);

NXT_EXPORT void nxt_process_arguments(nxt_task_t *task, char **orig_argv,
    char ***orig_envp);

#define nxt_process_port_remove(port)                                         \
    nxt_queue_remove(&port->link)

#define nxt_process_port_first(process)                                       \
    nxt_queue_link_data(nxt_queue_first(&process->ports), nxt_port_t, link)

NXT_EXPORT void nxt_process_port_add(nxt_task_t *task, nxt_process_t *process,
    nxt_port_t *port);

#define nxt_process_port_each(process, port)                                   \
    nxt_queue_each(port, &process->ports, nxt_port_t, link)

#define nxt_process_port_loop                                                 \
    nxt_queue_loop

nxt_process_type_t nxt_process_type(nxt_process_t *process);

void nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i);

void nxt_process_close_ports(nxt_task_t *task, nxt_process_t *process);

void nxt_process_connected_port_add(nxt_process_t *process, nxt_port_t *port);

void nxt_process_connected_port_remove(nxt_process_t *process,
    nxt_port_t *port);

nxt_port_t *nxt_process_connected_port_find(nxt_process_t *process,
    nxt_pid_t pid, nxt_port_id_t port_id);

void nxt_worker_process_quit_handler(nxt_task_t *task,
    nxt_port_recv_msg_t *msg);

void nxt_init_destroy(nxt_runtime_t *rt, nxt_process_init_t *init);


#if (NXT_HAVE_SETPROCTITLE)

#define nxt_process_title(task, fmt, ...)                                     \
    setproctitle(fmt, __VA_ARGS__)

#elif (NXT_LINUX || NXT_SOLARIS || NXT_MACOSX)

#define NXT_SETPROCTITLE_ARGV  1
NXT_EXPORT void nxt_process_title(nxt_task_t *task, const char *fmt, ...);

#endif


#define nxt_sched_yield()                                                     \
    sched_yield()

/*
 * Solaris declares abort() as __NORETURN,
 * raise(SIGABRT) is mostly the same.
 */

#define nxt_abort()                                                           \
    (void) raise(SIGABRT)


NXT_EXPORT extern nxt_pid_t  nxt_pid;
NXT_EXPORT extern nxt_pid_t  nxt_ppid;
NXT_EXPORT extern nxt_uid_t  nxt_euid;
NXT_EXPORT extern nxt_gid_t  nxt_egid;
NXT_EXPORT extern char       **nxt_process_argv;
NXT_EXPORT extern char       ***nxt_process_environ;


#endif /* _NXT_PROCESS_H_INCLUDED_ */