xref: /unit/src/nxt_process.h (revision 2015:4570130dd183)
10Sigor@sysoev.ru 
20Sigor@sysoev.ru /*
30Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
40Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
50Sigor@sysoev.ru  */
60Sigor@sysoev.ru 
720Sigor@sysoev.ru #ifndef _NXT_PROCESS_H_INCLUDED_
820Sigor@sysoev.ru #define _NXT_PROCESS_H_INCLUDED_
920Sigor@sysoev.ru 
101306St.nateldemoura@f5.com #if (NXT_HAVE_CLONE)
111488St.nateldemoura@f5.com #include <unistd.h>
121306St.nateldemoura@f5.com #include <nxt_clone.h>
131306St.nateldemoura@f5.com #endif
141182St.nateldemoura@f5.com 
1520Sigor@sysoev.ru 
161488St.nateldemoura@f5.com #if (NXT_HAVE_CLONE)
171488St.nateldemoura@f5.com /*
181488St.nateldemoura@f5.com  * Old glibc wrapper for getpid(2) returns a cached pid invalidated only by
191488St.nateldemoura@f5.com  * fork(2) calls.  As we use clone(2) for container, it returns the wrong pid.
201488St.nateldemoura@f5.com  */
211488St.nateldemoura@f5.com #define nxt_getpid()                                                          \
221488St.nateldemoura@f5.com     syscall(__NR_getpid)
231488St.nateldemoura@f5.com #else
241488St.nateldemoura@f5.com #define nxt_getpid()                                                          \
251488St.nateldemoura@f5.com     getpid()
261488St.nateldemoura@f5.com #endif
271488St.nateldemoura@f5.com 
2820Sigor@sysoev.ru typedef pid_t            nxt_pid_t;
2920Sigor@sysoev.ru 
3020Sigor@sysoev.ru 
311488St.nateldemoura@f5.com typedef struct nxt_common_app_conf_s nxt_common_app_conf_t;
321488St.nateldemoura@f5.com 
331488St.nateldemoura@f5.com 
341488St.nateldemoura@f5.com typedef struct {
351488St.nateldemoura@f5.com     nxt_runtime_t              *rt;
361488St.nateldemoura@f5.com } nxt_discovery_init_t;
370Sigor@sysoev.ru 
381488St.nateldemoura@f5.com 
391488St.nateldemoura@f5.com typedef struct {
401488St.nateldemoura@f5.com     nxt_str_t                  conf;
411488St.nateldemoura@f5.com #if (NXT_TLS)
421488St.nateldemoura@f5.com     nxt_array_t                *certs;
431488St.nateldemoura@f5.com #endif
441488St.nateldemoura@f5.com } nxt_controller_init_t;
4520Sigor@sysoev.ru 
46141Smax.romanov@nginx.com 
471488St.nateldemoura@f5.com typedef union {
481488St.nateldemoura@f5.com     void                       *discovery;
491488St.nateldemoura@f5.com     nxt_controller_init_t      controller;
501488St.nateldemoura@f5.com     void                       *router;
511488St.nateldemoura@f5.com     nxt_common_app_conf_t      *app;
521488St.nateldemoura@f5.com } nxt_process_data_t;
53141Smax.romanov@nginx.com 
541488St.nateldemoura@f5.com 
551488St.nateldemoura@f5.com typedef enum {
561488St.nateldemoura@f5.com     NXT_PROCESS_STATE_CREATING = 0,
571488St.nateldemoura@f5.com     NXT_PROCESS_STATE_CREATED,
581488St.nateldemoura@f5.com     NXT_PROCESS_STATE_READY,
591488St.nateldemoura@f5.com } nxt_process_state_t;
6020Sigor@sysoev.ru 
6120Sigor@sysoev.ru 
62364Smax.romanov@nginx.com typedef struct nxt_port_mmap_s  nxt_port_mmap_t;
631579St.nateldemoura@f5.com typedef struct nxt_process_s    nxt_process_t;
641579St.nateldemoura@f5.com typedef void (*nxt_isolation_cleanup_t)(nxt_task_t *task,
651579St.nateldemoura@f5.com     nxt_process_t *process);
661488St.nateldemoura@f5.com 
67364Smax.romanov@nginx.com 
681488St.nateldemoura@f5.com typedef struct {
69364Smax.romanov@nginx.com     nxt_thread_mutex_t  mutex;
70364Smax.romanov@nginx.com     uint32_t            size;
71364Smax.romanov@nginx.com     uint32_t            cap;
72364Smax.romanov@nginx.com     nxt_port_mmap_t     *elts;
731488St.nateldemoura@f5.com } nxt_port_mmaps_t;
74364Smax.romanov@nginx.com 
751585St.nateldemoura@f5.com 
761489St.nateldemoura@f5.com typedef struct {
771673St.nateldemoura@f5.com     uint8_t             language_deps;      /* 1-bit */
781704St.nateldemoura@f5.com     uint8_t             tmpfs;              /* 1-bit */
791708St.nateldemoura@f5.com     uint8_t             procfs;             /* 1-bit */
801585St.nateldemoura@f5.com } nxt_process_automount_t;
811585St.nateldemoura@f5.com 
821489St.nateldemoura@f5.com 
831585St.nateldemoura@f5.com typedef struct {
841585St.nateldemoura@f5.com     u_char                   *rootfs;
851585St.nateldemoura@f5.com     nxt_process_automount_t  automount;
861585St.nateldemoura@f5.com     nxt_array_t              *mounts;     /* of nxt_mount_t */
871585St.nateldemoura@f5.com 
881585St.nateldemoura@f5.com     nxt_isolation_cleanup_t  cleanup;
891579St.nateldemoura@f5.com 
901489St.nateldemoura@f5.com #if (NXT_HAVE_CLONE)
911585St.nateldemoura@f5.com     nxt_clone_t              clone;
921489St.nateldemoura@f5.com #endif
931489St.nateldemoura@f5.com 
941489St.nateldemoura@f5.com #if (NXT_HAVE_PR_SET_NO_NEW_PRIVS)
951585St.nateldemoura@f5.com     uint8_t                  new_privs;   /* 1 bit */
961489St.nateldemoura@f5.com #endif
971489St.nateldemoura@f5.com } nxt_process_isolation_t;
981489St.nateldemoura@f5.com 
99364Smax.romanov@nginx.com 
1001579St.nateldemoura@f5.com struct nxt_process_s {
1011489St.nateldemoura@f5.com     nxt_pid_t                pid;
1021998St.nateldemoura@f5.com     nxt_queue_t              ports;      /* of nxt_port_t.link */
1031489St.nateldemoura@f5.com     nxt_process_state_t      state;
1041489St.nateldemoura@f5.com     nxt_bool_t               registered;
1051489St.nateldemoura@f5.com     nxt_int_t                use_count;
1061488St.nateldemoura@f5.com 
1071489St.nateldemoura@f5.com     nxt_port_mmaps_t         incoming;
1081488St.nateldemoura@f5.com 
1091998St.nateldemoura@f5.com 
1101998St.nateldemoura@f5.com     nxt_pid_t                isolated_pid;
1111998St.nateldemoura@f5.com     const char               *name;
1121998St.nateldemoura@f5.com     nxt_port_t               *parent_port;
1131998St.nateldemoura@f5.com 
1141489St.nateldemoura@f5.com     uint32_t                 stream;
1151488St.nateldemoura@f5.com 
1161489St.nateldemoura@f5.com     nxt_mp_t                 *mem_pool;
1171489St.nateldemoura@f5.com     nxt_credential_t         *user_cred;
11842Smax.romanov@nginx.com 
1191998St.nateldemoura@f5.com     nxt_queue_t              children;   /* of nxt_process_t.link */
1201998St.nateldemoura@f5.com     nxt_queue_link_t         link;       /* for nxt_process_t.children */
1211998St.nateldemoura@f5.com 
1221489St.nateldemoura@f5.com     nxt_process_data_t       data;
1231489St.nateldemoura@f5.com 
1241489St.nateldemoura@f5.com     nxt_process_isolation_t  isolation;
1251579St.nateldemoura@f5.com };
1261488St.nateldemoura@f5.com 
127141Smax.romanov@nginx.com 
1281488St.nateldemoura@f5.com typedef nxt_int_t (*nxt_process_prefork_t)(nxt_task_t *task,
1291488St.nateldemoura@f5.com     nxt_process_t *process, nxt_mp_t *mp);
1301488St.nateldemoura@f5.com typedef nxt_int_t (*nxt_process_postfork_t)(nxt_task_t *task,
1311488St.nateldemoura@f5.com     nxt_process_t *process, nxt_mp_t *mp);
1321488St.nateldemoura@f5.com typedef nxt_int_t (*nxt_process_setup_t)(nxt_task_t *task,
1331488St.nateldemoura@f5.com     nxt_process_t *process);
1341488St.nateldemoura@f5.com typedef nxt_int_t (*nxt_process_start_t)(nxt_task_t *task,
1351488St.nateldemoura@f5.com     nxt_process_data_t *data);
1361488St.nateldemoura@f5.com 
13788Smax.romanov@nginx.com 
1381488St.nateldemoura@f5.com typedef struct {
1391488St.nateldemoura@f5.com     const char                 *name;
1401488St.nateldemoura@f5.com     nxt_process_type_t         type;
1411488St.nateldemoura@f5.com 
1421488St.nateldemoura@f5.com     nxt_process_prefork_t      prefork;
1431488St.nateldemoura@f5.com 
1441488St.nateldemoura@f5.com     nxt_process_setup_t        setup;
1451488St.nateldemoura@f5.com     nxt_process_start_t        start;
1461488St.nateldemoura@f5.com 
1471488St.nateldemoura@f5.com     uint8_t                    restart; /* 1-bit */
1481488St.nateldemoura@f5.com 
1491488St.nateldemoura@f5.com     const nxt_port_handlers_t  *port_handlers;
1501488St.nateldemoura@f5.com     const nxt_sig_event_t      *signals;
1511488St.nateldemoura@f5.com } nxt_process_init_t;
1520Sigor@sysoev.ru 
1530Sigor@sysoev.ru 
154*2015Smax.romanov@nginx.com extern uint8_t  nxt_proc_keep_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
155*2015Smax.romanov@nginx.com extern uint8_t  nxt_proc_send_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
156*2015Smax.romanov@nginx.com extern uint8_t  nxt_proc_remove_notify_matrix[NXT_PROCESS_MAX][NXT_PROCESS_MAX];
157366Smax.romanov@nginx.com 
15820Sigor@sysoev.ru NXT_EXPORT nxt_pid_t nxt_process_execute(nxt_task_t *task, char *name,
15920Sigor@sysoev.ru     char **argv, char **envp);
16020Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_process_daemon(nxt_task_t *task);
16120Sigor@sysoev.ru NXT_EXPORT void nxt_nanosleep(nxt_nsec_t ns);
16220Sigor@sysoev.ru 
16320Sigor@sysoev.ru NXT_EXPORT void nxt_process_arguments(nxt_task_t *task, char **orig_argv,
16420Sigor@sysoev.ru     char ***orig_envp);
16520Sigor@sysoev.ru 
1661488St.nateldemoura@f5.com #define nxt_process_init(process)                                             \
1671488St.nateldemoura@f5.com     (nxt_pointer_to(process, sizeof(nxt_process_t)))
1681488St.nateldemoura@f5.com 
16942Smax.romanov@nginx.com #define nxt_process_port_remove(port)                                         \
17042Smax.romanov@nginx.com     nxt_queue_remove(&port->link)
17142Smax.romanov@nginx.com 
17242Smax.romanov@nginx.com #define nxt_process_port_first(process)                                       \
17342Smax.romanov@nginx.com     nxt_queue_link_data(nxt_queue_first(&process->ports), nxt_port_t, link)
17442Smax.romanov@nginx.com 
175164Smax.romanov@nginx.com NXT_EXPORT void nxt_process_port_add(nxt_task_t *task, nxt_process_t *process,
176164Smax.romanov@nginx.com     nxt_port_t *port);
17742Smax.romanov@nginx.com 
17842Smax.romanov@nginx.com #define nxt_process_port_each(process, port)                                   \
17942Smax.romanov@nginx.com     nxt_queue_each(port, &process->ports, nxt_port_t, link)
18042Smax.romanov@nginx.com 
18142Smax.romanov@nginx.com #define nxt_process_port_loop                                                 \
18242Smax.romanov@nginx.com     nxt_queue_loop
18320Sigor@sysoev.ru 
1841997St.nateldemoura@f5.com nxt_process_t *nxt_process_new(nxt_runtime_t *rt);
1851997St.nateldemoura@f5.com void nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i);
1861997St.nateldemoura@f5.com nxt_int_t nxt_process_init_start(nxt_task_t *task, nxt_process_init_t init);
1871997St.nateldemoura@f5.com nxt_int_t nxt_process_start(nxt_task_t *task, nxt_process_t *process);
188366Smax.romanov@nginx.com nxt_process_type_t nxt_process_type(nxt_process_t *process);
189366Smax.romanov@nginx.com 
1901254Shongzhidao@gmail.com void nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i);
1911254Shongzhidao@gmail.com 
192349Smax.romanov@nginx.com void nxt_process_close_ports(nxt_task_t *task, nxt_process_t *process);
19388Smax.romanov@nginx.com 
1941488St.nateldemoura@f5.com void nxt_process_quit(nxt_task_t *task, nxt_uint_t exit_status);
1951488St.nateldemoura@f5.com void nxt_signal_quit_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
196248Svbart@nginx.com 
1971488St.nateldemoura@f5.com nxt_int_t nxt_process_core_setup(nxt_task_t *task, nxt_process_t *process);
1981488St.nateldemoura@f5.com nxt_int_t nxt_process_creds_set(nxt_task_t *task, nxt_process_t *process,
1991488St.nateldemoura@f5.com     nxt_str_t *user, nxt_str_t *group);
2001488St.nateldemoura@f5.com nxt_int_t nxt_process_apply_creds(nxt_task_t *task, nxt_process_t *process);
2011306St.nateldemoura@f5.com 
20220Sigor@sysoev.ru #if (NXT_HAVE_SETPROCTITLE)
2030Sigor@sysoev.ru 
20420Sigor@sysoev.ru #define nxt_process_title(task, fmt, ...)                                     \
20520Sigor@sysoev.ru     setproctitle(fmt, __VA_ARGS__)
20620Sigor@sysoev.ru 
20720Sigor@sysoev.ru #elif (NXT_LINUX || NXT_SOLARIS || NXT_MACOSX)
20820Sigor@sysoev.ru 
20920Sigor@sysoev.ru #define NXT_SETPROCTITLE_ARGV  1
21020Sigor@sysoev.ru NXT_EXPORT void nxt_process_title(nxt_task_t *task, const char *fmt, ...);
21120Sigor@sysoev.ru 
21220Sigor@sysoev.ru #endif
21320Sigor@sysoev.ru 
21420Sigor@sysoev.ru 
21520Sigor@sysoev.ru #define nxt_sched_yield()                                                     \
21620Sigor@sysoev.ru     sched_yield()
2170Sigor@sysoev.ru 
2180Sigor@sysoev.ru /*
2190Sigor@sysoev.ru  * Solaris declares abort() as __NORETURN,
2200Sigor@sysoev.ru  * raise(SIGABRT) is mostly the same.
2210Sigor@sysoev.ru  */
2220Sigor@sysoev.ru 
22320Sigor@sysoev.ru #define nxt_abort()                                                           \
2240Sigor@sysoev.ru     (void) raise(SIGABRT)
2250Sigor@sysoev.ru 
2260Sigor@sysoev.ru 
2270Sigor@sysoev.ru NXT_EXPORT extern nxt_pid_t  nxt_pid;
2280Sigor@sysoev.ru NXT_EXPORT extern nxt_pid_t  nxt_ppid;
2291306St.nateldemoura@f5.com NXT_EXPORT extern nxt_uid_t  nxt_euid;
2301306St.nateldemoura@f5.com NXT_EXPORT extern nxt_gid_t  nxt_egid;
2310Sigor@sysoev.ru NXT_EXPORT extern char       **nxt_process_argv;
2320Sigor@sysoev.ru NXT_EXPORT extern char       ***nxt_process_environ;
2330Sigor@sysoev.ru 
2340Sigor@sysoev.ru 
23520Sigor@sysoev.ru #endif /* _NXT_PROCESS_H_INCLUDED_ */
236