xref: /unit/src/nxt_process.h (revision 2174:a7fb5d8a9590)
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(SYS_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 typedef struct nxt_process_s    nxt_process_t;
64 typedef void (*nxt_isolation_cleanup_t)(nxt_task_t *task,
65     nxt_process_t *process);
66 
67 
68 typedef struct {
69     nxt_thread_mutex_t  mutex;
70     uint32_t            size;
71     uint32_t            cap;
72     nxt_port_mmap_t     *elts;
73 } nxt_port_mmaps_t;
74 
75 
76 typedef struct {
77     uint8_t             language_deps;      /* 1-bit */
78     uint8_t             tmpfs;              /* 1-bit */
79     uint8_t             procfs;             /* 1-bit */
80 } nxt_process_automount_t;
81 
82 
83 typedef struct {
84     u_char                   *rootfs;
85     nxt_process_automount_t  automount;
86     nxt_array_t              *mounts;     /* of nxt_mount_t */
87 
88     nxt_isolation_cleanup_t  cleanup;
89 
90 #if (NXT_HAVE_CLONE)
91     nxt_clone_t              clone;
92 #endif
93 
94 #if (NXT_HAVE_PR_SET_NO_NEW_PRIVS)
95     uint8_t                  new_privs;   /* 1 bit */
96 #endif
97 } nxt_process_isolation_t;
98 
99 
100 struct nxt_process_s {
101     nxt_pid_t                pid;
102     nxt_queue_t              ports;      /* of nxt_port_t.link */
103     nxt_process_state_t      state;
104     nxt_bool_t               registered;
105     nxt_int_t                use_count;
106 
107     nxt_port_mmaps_t         incoming;
108 
109 
110     nxt_pid_t                isolated_pid;
111     const char               *name;
112     nxt_port_t               *parent_port;
113 
114     uint32_t                 stream;
115 
116     nxt_mp_t                 *mem_pool;
117     nxt_credential_t         *user_cred;
118 
119     nxt_queue_t              children;   /* of nxt_process_t.link */
120     nxt_queue_link_t         link;       /* for nxt_process_t.children */
121 
122     nxt_process_data_t       data;
123 
124     nxt_process_isolation_t  isolation;
125 };
126 
127 
128 typedef nxt_int_t (*nxt_process_prefork_t)(nxt_task_t *task,
129     nxt_process_t *process, nxt_mp_t *mp);
130 typedef nxt_int_t (*nxt_process_postfork_t)(nxt_task_t *task,
131     nxt_process_t *process, nxt_mp_t *mp);
132 typedef nxt_int_t (*nxt_process_setup_t)(nxt_task_t *task,
133     nxt_process_t *process);
134 typedef nxt_int_t (*nxt_process_start_t)(nxt_task_t *task,
135     nxt_process_data_t *data);
136 
137 
138 typedef struct {
139     const char                 *name;
140     nxt_process_type_t         type;
141 
142     nxt_process_prefork_t      prefork;
143 
144     nxt_process_setup_t        setup;
145     nxt_process_start_t        start;
146 
147     uint8_t                    restart; /* 1-bit */
148 
149     const nxt_port_handlers_t  *port_handlers;
150     const nxt_sig_event_t      *signals;
151 
152     nxt_queue_t                *siblings;
153 } nxt_process_init_t;
154 
155 
156 extern uint8_t  nxt_proc_keep_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
157 extern uint8_t  nxt_proc_send_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
158 extern uint8_t  nxt_proc_remove_notify_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
159 
160 NXT_EXPORT nxt_pid_t nxt_process_execute(nxt_task_t *task, char *name,
161     char **argv, char **envp);
162 NXT_EXPORT nxt_int_t nxt_process_daemon(nxt_task_t *task);
163 NXT_EXPORT void nxt_nanosleep(nxt_nsec_t ns);
164 
165 NXT_EXPORT void nxt_process_arguments(nxt_task_t *task, char **orig_argv,
166     char ***orig_envp);
167 
168 #define nxt_process_init(process)                                             \
169     (nxt_pointer_to(process, sizeof(nxt_process_t)))
170 
171 #define nxt_process_port_remove(port)                                         \
172     nxt_queue_remove(&port->link)
173 
174 #define nxt_process_port_first(process)                                       \
175     nxt_queue_link_data(nxt_queue_first(&process->ports), nxt_port_t, link)
176 
177 NXT_EXPORT void nxt_process_port_add(nxt_task_t *task, nxt_process_t *process,
178     nxt_port_t *port);
179 
180 #define nxt_process_port_each(process, port)                                   \
181     nxt_queue_each(port, &process->ports, nxt_port_t, link)
182 
183 #define nxt_process_port_loop                                                 \
184     nxt_queue_loop
185 
186 nxt_process_t *nxt_process_new(nxt_runtime_t *rt);
187 void nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i);
188 nxt_int_t nxt_process_init_start(nxt_task_t *task, nxt_process_init_t init);
189 nxt_int_t nxt_process_start(nxt_task_t *task, nxt_process_t *process);
190 nxt_process_type_t nxt_process_type(nxt_process_t *process);
191 
192 void nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i);
193 
194 void nxt_process_close_ports(nxt_task_t *task, nxt_process_t *process);
195 
196 void nxt_process_quit(nxt_task_t *task, nxt_uint_t exit_status);
197 void nxt_signal_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
198 
199 nxt_int_t nxt_process_core_setup(nxt_task_t *task, nxt_process_t *process);
200 nxt_int_t nxt_process_creds_set(nxt_task_t *task, nxt_process_t *process,
201     nxt_str_t *user, nxt_str_t *group);
202 nxt_int_t nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process);
203 
204 #if (NXT_HAVE_SETPROCTITLE)
205 
206 #define nxt_process_title(task, fmt, ...)                                     \
207     setproctitle(fmt, __VA_ARGS__)
208 
209 #elif (NXT_LINUX || NXT_SOLARIS || NXT_MACOSX)
210 
211 #define NXT_SETPROCTITLE_ARGV  1
212 NXT_EXPORT void nxt_process_title(nxt_task_t *task, const char *fmt, ...);
213 
214 #endif
215 
216 
217 #define nxt_sched_yield()                                                     \
218     sched_yield()
219 
220 /*
221  * Solaris declares abort() as __NORETURN,
222  * raise(SIGABRT) is mostly the same.
223  */
224 
225 #define nxt_abort()                                                           \
226     (void) raise(SIGABRT)
227 
228 
229 NXT_EXPORT extern nxt_pid_t  nxt_pid;
230 NXT_EXPORT extern nxt_pid_t  nxt_ppid;
231 NXT_EXPORT extern nxt_uid_t  nxt_euid;
232 NXT_EXPORT extern nxt_gid_t  nxt_egid;
233 NXT_EXPORT extern char       **nxt_process_argv;
234 NXT_EXPORT extern char       ***nxt_process_environ;
235 
236 
237 #endif /* _NXT_PROCESS_H_INCLUDED_ */
238