xref: /unit/src/nxt_process.h (revision 1489:4a3ec07f4b19)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_PROCESS_H_INCLUDED_
8 #define _NXT_PROCESS_H_INCLUDED_
9 
10 #if (NXT_HAVE_CLONE)
11 #include <unistd.h>
12 #include <nxt_clone.h>
13 #endif
14 
15 
16 #if (NXT_HAVE_CLONE)
17 /*
18  * Old glibc wrapper for getpid(2) returns a cached pid invalidated only by
19  * fork(2) calls.  As we use clone(2) for container, it returns the wrong pid.
20  */
21 #define nxt_getpid()                                                          \
22     syscall(__NR_getpid)
23 #else
24 #define nxt_getpid()                                                          \
25     getpid()
26 #endif
27 
28 typedef pid_t            nxt_pid_t;
29 
30 
31 typedef struct nxt_common_app_conf_s nxt_common_app_conf_t;
32 
33 
34 typedef struct {
35     nxt_runtime_t              *rt;
36 } nxt_discovery_init_t;
37 
38 
39 typedef struct {
40     nxt_str_t                  conf;
41 #if (NXT_TLS)
42     nxt_array_t                *certs;
43 #endif
44 } nxt_controller_init_t;
45 
46 
47 typedef union {
48     void                       *discovery;
49     nxt_controller_init_t      controller;
50     void                       *router;
51     nxt_common_app_conf_t      *app;
52 } nxt_process_data_t;
53 
54 
55 typedef enum {
56     NXT_PROCESS_STATE_CREATING = 0,
57     NXT_PROCESS_STATE_CREATED,
58     NXT_PROCESS_STATE_READY,
59 } nxt_process_state_t;
60 
61 
62 typedef struct nxt_port_mmap_s  nxt_port_mmap_t;
63 
64 
65 typedef struct {
66     nxt_thread_mutex_t  mutex;
67     uint32_t            size;
68     uint32_t            cap;
69     nxt_port_mmap_t     *elts;
70 } nxt_port_mmaps_t;
71 
72 typedef struct {
73     u_char                     *rootfs;
74     nxt_array_t                *mounts;     /* of nxt_mount_t */
75 
76 #if (NXT_HAVE_CLONE)
77     nxt_clone_t                clone;
78 #endif
79 
80 #if (NXT_HAVE_PR_SET_NO_NEW_PRIVS)
81     uint8_t                    new_privs;   /* 1 bit */
82 #endif
83 } nxt_process_isolation_t;
84 
85 
86 typedef struct {
87     nxt_pid_t                pid;
88     const char               *name;
89     nxt_queue_t              ports;      /* of nxt_port_t */
90     nxt_process_state_t      state;
91     nxt_bool_t               registered;
92     nxt_int_t                use_count;
93 
94     nxt_port_mmaps_t         incoming;
95     nxt_port_mmaps_t         outgoing;
96 
97     nxt_thread_mutex_t       cp_mutex;
98     nxt_lvlhsh_t             connected_ports; /* of nxt_port_t */
99 
100     uint32_t                 stream;
101 
102     nxt_mp_t                 *mem_pool;
103     nxt_credential_t         *user_cred;
104 
105     nxt_process_data_t       data;
106 
107     nxt_process_isolation_t  isolation;
108 } nxt_process_t;
109 
110 
111 typedef nxt_int_t (*nxt_process_prefork_t)(nxt_task_t *task,
112     nxt_process_t *process, nxt_mp_t *mp);
113 typedef nxt_int_t (*nxt_process_postfork_t)(nxt_task_t *task,
114     nxt_process_t *process, nxt_mp_t *mp);
115 typedef nxt_int_t (*nxt_process_setup_t)(nxt_task_t *task,
116     nxt_process_t *process);
117 typedef nxt_int_t (*nxt_process_start_t)(nxt_task_t *task,
118     nxt_process_data_t *data);
119 
120 
121 typedef struct {
122     const char                 *name;
123     nxt_process_type_t         type;
124 
125     nxt_process_prefork_t      prefork;
126 
127     nxt_process_setup_t        setup;
128     nxt_process_start_t        start;
129 
130     uint8_t                    restart; /* 1-bit */
131 
132     const nxt_port_handlers_t  *port_handlers;
133     const nxt_sig_event_t      *signals;
134 } nxt_process_init_t;
135 
136 
137 extern nxt_bool_t  nxt_proc_conn_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
138 extern nxt_bool_t
139           nxt_proc_remove_notify_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
140 
141 NXT_EXPORT nxt_pid_t nxt_process_create(nxt_task_t *task,
142     nxt_process_t *process);
143 NXT_EXPORT nxt_pid_t nxt_process_execute(nxt_task_t *task, char *name,
144     char **argv, char **envp);
145 NXT_EXPORT nxt_int_t nxt_process_daemon(nxt_task_t *task);
146 NXT_EXPORT void nxt_nanosleep(nxt_nsec_t ns);
147 
148 NXT_EXPORT void nxt_process_arguments(nxt_task_t *task, char **orig_argv,
149     char ***orig_envp);
150 
151 #define nxt_process_init(process)                                             \
152     (nxt_pointer_to(process, sizeof(nxt_process_t)))
153 
154 #define nxt_process_port_remove(port)                                         \
155     nxt_queue_remove(&port->link)
156 
157 #define nxt_process_port_first(process)                                       \
158     nxt_queue_link_data(nxt_queue_first(&process->ports), nxt_port_t, link)
159 
160 NXT_EXPORT void nxt_process_port_add(nxt_task_t *task, nxt_process_t *process,
161     nxt_port_t *port);
162 
163 #define nxt_process_port_each(process, port)                                   \
164     nxt_queue_each(port, &process->ports, nxt_port_t, link)
165 
166 #define nxt_process_port_loop                                                 \
167     nxt_queue_loop
168 
169 nxt_process_type_t nxt_process_type(nxt_process_t *process);
170 
171 void nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i);
172 
173 void nxt_process_close_ports(nxt_task_t *task, nxt_process_t *process);
174 
175 void nxt_process_connected_port_add(nxt_process_t *process, nxt_port_t *port);
176 
177 void nxt_process_connected_port_remove(nxt_process_t *process,
178     nxt_port_t *port);
179 
180 nxt_port_t *nxt_process_connected_port_find(nxt_process_t *process,
181     nxt_port_t *port);
182 
183 void nxt_process_quit(nxt_task_t *task, nxt_uint_t exit_status);
184 void nxt_signal_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
185 
186 nxt_int_t nxt_process_core_setup(nxt_task_t *task, nxt_process_t *process);
187 nxt_int_t nxt_process_creds_set(nxt_task_t *task, nxt_process_t *process,
188     nxt_str_t *user, nxt_str_t *group);
189 nxt_int_t nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process);
190 
191 #if (NXT_HAVE_CLONE_NEWUSER)
192 nxt_int_t nxt_process_vldt_isolation_creds(nxt_task_t *task,
193     nxt_process_t *process);
194 #endif
195 
196 nxt_int_t nxt_process_change_root(nxt_task_t *task, nxt_process_t *process);
197 
198 #if (NXT_HAVE_ISOLATION_ROOTFS)
199 void nxt_process_unmount_all(nxt_task_t *task, nxt_process_t *process);
200 #endif
201 
202 #if (NXT_HAVE_SETPROCTITLE)
203 
204 #define nxt_process_title(task, fmt, ...)                                     \
205     setproctitle(fmt, __VA_ARGS__)
206 
207 #elif (NXT_LINUX || NXT_SOLARIS || NXT_MACOSX)
208 
209 #define NXT_SETPROCTITLE_ARGV  1
210 NXT_EXPORT void nxt_process_title(nxt_task_t *task, const char *fmt, ...);
211 
212 #endif
213 
214 
215 #define nxt_sched_yield()                                                     \
216     sched_yield()
217 
218 /*
219  * Solaris declares abort() as __NORETURN,
220  * raise(SIGABRT) is mostly the same.
221  */
222 
223 #define nxt_abort()                                                           \
224     (void) raise(SIGABRT)
225 
226 
227 NXT_EXPORT extern nxt_pid_t  nxt_pid;
228 NXT_EXPORT extern nxt_pid_t  nxt_ppid;
229 NXT_EXPORT extern nxt_uid_t  nxt_euid;
230 NXT_EXPORT extern nxt_gid_t  nxt_egid;
231 NXT_EXPORT extern char       **nxt_process_argv;
232 NXT_EXPORT extern char       ***nxt_process_environ;
233 
234 
235 #endif /* _NXT_PROCESS_H_INCLUDED_ */
236