xref: /unit/src/nxt_runtime.h (revision 216:07257705cd64)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) Valentin V. Bartenev
5  * Copyright (C) NGINX, Inc.
6  */
7 
8 #ifndef _NXT_RUNTIME_H_INCLUDED_
9 #define _NXT_RUNTIME_H_INCLUDED_
10 
11 
12 typedef void (*nxt_runtime_cont_t)(nxt_task_t *task);
13 
14 
15 struct nxt_runtime_s {
16     nxt_mp_t               *mem_pool;
17 
18     nxt_array_t            *inherited_sockets;  /* of nxt_listen_socket_t */
19     nxt_array_t            *listen_sockets;     /* of nxt_listen_socket_t */
20 
21     nxt_array_t            *services;           /* of nxt_service_t */
22     nxt_array_t            *languages;          /* of nxt_app_lang_module_t */
23     void                   *data;
24 
25     nxt_runtime_cont_t     start;
26 
27     nxt_str_t              *conf_prefix;
28     nxt_str_t              *prefix;
29 
30     nxt_str_t              hostname;
31 
32     nxt_file_name_t        *pid_file;
33 
34 #if (NXT_THREADS)
35     nxt_array_t            *thread_pools;       /* of nxt_thread_pool_t */
36     nxt_runtime_cont_t     continuation;
37 #endif
38 
39     nxt_process_t          *mprocess;
40     size_t                 nprocesses;
41     nxt_thread_mutex_t     processes_mutex;
42     nxt_lvlhsh_t           processes;           /* of nxt_process_t */
43 
44     nxt_port_t             *port_by_type[NXT_PROCESS_MAX];
45     nxt_lvlhsh_t           ports;               /* of nxt_port_t */
46 
47     nxt_list_t             *log_files;          /* of nxt_file_t */
48 
49     uint32_t               last_engine_id;
50 
51     uint32_t               types;         /* bitset of nxt_process_type_t */
52 
53     nxt_timer_t            timer;
54 
55     uint8_t                daemon;
56     uint8_t                batch;
57     uint8_t                master_process;
58     const char             *engine;
59     uint32_t               engine_connections;
60     uint32_t               worker_processes;
61     uint32_t               auxiliary_threads;
62     nxt_user_cred_t        user_cred;
63     const char             *group;
64     const char             *pid;
65     const char             *error_log;
66 
67     nxt_queue_t            engines;            /* of nxt_event_engine_t */
68 
69     nxt_sockaddr_t         *controller_listen;
70     nxt_listen_socket_t    *controller_socket;
71     nxt_str_t              upstream;
72 };
73 
74 
75 
76 typedef nxt_int_t (*nxt_module_init_t)(nxt_thread_t *thr, nxt_runtime_t *rt);
77 
78 
79 nxt_int_t nxt_runtime_create(nxt_task_t *task);
80 void nxt_runtime_quit(nxt_task_t *task);
81 
82 void nxt_runtime_event_engine_free(nxt_runtime_t *rt);
83 
84 #if (NXT_THREADS)
85 nxt_int_t nxt_runtime_thread_pool_create(nxt_thread_t *thr, nxt_runtime_t *rt,
86     nxt_uint_t max_threads, nxt_nsec_t timeout);
87 #endif
88 
89 
90 nxt_inline nxt_bool_t
91 nxt_runtime_is_type(nxt_runtime_t *rt, nxt_process_type_t type)
92 {
93     return (rt->types & (1U << type)) != 0;
94 }
95 
96 
97 nxt_inline nxt_bool_t
98 nxt_runtime_is_master(nxt_runtime_t *rt)
99 {
100     return nxt_runtime_is_type(rt, NXT_PROCESS_MASTER);
101 }
102 
103 
104 nxt_process_t *nxt_runtime_process_new(nxt_runtime_t *rt);
105 
106 nxt_process_t *nxt_runtime_process_get(nxt_runtime_t *rt, nxt_pid_t pid);
107 
108 void nxt_runtime_process_add(nxt_runtime_t *rt, nxt_process_t *process);
109 
110 nxt_process_t *nxt_runtime_process_find(nxt_runtime_t *rt, nxt_pid_t pid);
111 
112 void nxt_runtime_process_remove(nxt_runtime_t *rt, nxt_process_t *process);
113 
114 nxt_process_t *nxt_runtime_process_first(nxt_runtime_t *rt,
115     nxt_lvlhsh_each_t *lhe);
116 
117 #define nxt_runtime_process_next(rt, lhe)                                     \
118     nxt_lvlhsh_each(&rt->processes, lhe)
119 
120 
121 void nxt_runtime_port_add(nxt_runtime_t *rt, nxt_port_t *port);
122 
123 void nxt_runtime_port_remove(nxt_runtime_t *rt, nxt_port_t *port);
124 
125 nxt_port_t *nxt_runtime_port_find(nxt_runtime_t *rt, nxt_pid_t pid,
126     nxt_port_id_t port_id);
127 
128 nxt_port_t *nxt_runtime_port_first(nxt_runtime_t *rt,
129     nxt_lvlhsh_each_t *lhe);
130 
131 #define nxt_runtime_port_next(rt, lhe)                                        \
132     nxt_port_hash_next(&rt->ports, lhe)
133 
134 
135 /* STUB */
136 nxt_int_t nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt);
137 
138 nxt_str_t *nxt_current_directory(nxt_mp_t *mp);
139 
140 nxt_listen_socket_t *nxt_runtime_listen_socket_add(nxt_runtime_t *rt,
141     nxt_sockaddr_t *sa);
142 nxt_int_t nxt_runtime_listen_sockets_create(nxt_task_t *task,
143     nxt_runtime_t *rt);
144 nxt_int_t nxt_runtime_listen_sockets_enable(nxt_task_t *task,
145     nxt_runtime_t *rt);
146 nxt_file_t *nxt_runtime_log_file_add(nxt_runtime_t *rt, nxt_str_t *name);
147 
148 /* STUB */
149 void nxt_cdecl nxt_log_time_handler(nxt_uint_t level, nxt_log_t *log,
150     const char *fmt, ...);
151 
152 void nxt_stream_connection_init(nxt_task_t *task, void *obj, void *data);
153 
154 void nxt_port_app_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
155 
156 
157 #define nxt_runtime_process_each(rt, process)                                 \
158     do {                                                                      \
159         nxt_lvlhsh_each_t  _lhe;                                              \
160         nxt_process_t      *_nxt;                                             \
161                                                                               \
162         for (process = nxt_runtime_process_first(rt, &_lhe);                  \
163              process != NULL;                                                 \
164              process = _nxt) {                                                \
165                                                                               \
166             _nxt = nxt_runtime_process_next(rt, &_lhe);                       \
167 
168 #define nxt_runtime_process_loop                                              \
169         }                                                                     \
170     } while(0)
171 
172 
173 #define nxt_runtime_port_each(rt, port)                                       \
174     do {                                                                      \
175         nxt_lvlhsh_each_t  _lhe;                                              \
176                                                                               \
177         for (port = nxt_runtime_port_first(rt, &_lhe);                        \
178              port != NULL;                                                    \
179              port = nxt_runtime_port_next(rt, &_lhe)) {                       \
180 
181 #define nxt_runtime_port_loop                                                 \
182         }                                                                     \
183     } while(0)
184 
185 
186 extern nxt_module_init_t  nxt_init_modules[];
187 extern nxt_uint_t         nxt_init_modules_n;
188 
189 
190 #endif /* _NXT_RUNTIME_H_INCLIDED_ */
191