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