xref: /unit/src/nxt_process.h (revision 2153:37bccff06c9f)
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 } nxt_process_init_t;
152 
153 
154 extern uint8_t  nxt_proc_keep_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
155 extern uint8_t  nxt_proc_send_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
156 extern uint8_t  nxt_proc_remove_notify_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
157 
158 NXT_EXPORT nxt_pid_t nxt_process_execute(nxt_task_t *task, char *name,
159     char **argv, char **envp);
160 NXT_EXPORT nxt_int_t nxt_process_daemon(nxt_task_t *task);
161 NXT_EXPORT void nxt_nanosleep(nxt_nsec_t ns);
162 
163 NXT_EXPORT void nxt_process_arguments(nxt_task_t *task, char **orig_argv,
164     char ***orig_envp);
165 
166 #define nxt_process_init(process)                                             \
167     (nxt_pointer_to(process, sizeof(nxt_process_t)))
168 
169 #define nxt_process_port_remove(port)                                         \
170     nxt_queue_remove(&port->link)
171 
172 #define nxt_process_port_first(process)                                       \
173     nxt_queue_link_data(nxt_queue_first(&process->ports), nxt_port_t, link)
174 
175 NXT_EXPORT void nxt_process_port_add(nxt_task_t *task, nxt_process_t *process,
176     nxt_port_t *port);
177 
178 #define nxt_process_port_each(process, port)                                   \
179     nxt_queue_each(port, &process->ports, nxt_port_t, link)
180 
181 #define nxt_process_port_loop                                                 \
182     nxt_queue_loop
183 
184 nxt_process_t *nxt_process_new(nxt_runtime_t *rt);
185 void nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i);
186 nxt_int_t nxt_process_init_start(nxt_task_t *task, nxt_process_init_t init);
187 nxt_int_t nxt_process_start(nxt_task_t *task, nxt_process_t *process);
188 nxt_process_type_t nxt_process_type(nxt_process_t *process);
189 
190 void nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i);
191 
192 void nxt_process_close_ports(nxt_task_t *task, nxt_process_t *process);
193 
194 void nxt_process_quit(nxt_task_t *task, nxt_uint_t exit_status);
195 void nxt_signal_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
196 
197 nxt_int_t nxt_process_core_setup(nxt_task_t *task, nxt_process_t *process);
198 nxt_int_t nxt_process_creds_set(nxt_task_t *task, nxt_process_t *process,
199     nxt_str_t *user, nxt_str_t *group);
200 nxt_int_t nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process);
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