1
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) NGINX, Inc.
5 */
6
7 #include <nxt_main.h>
8
9
10 nxt_uint_t nxt_ncpu = 1;
11 nxt_uint_t nxt_pagesize;
12 nxt_task_t nxt_main_task;
13 nxt_atomic_t nxt_task_ident;
14
15 nxt_thread_declare_data(nxt_thread_t, nxt_thread_context);
16
17
18 #if (NXT_DEBUG && NXT_FREEBSD)
19 /*
20 * Fill memory with 0xA5 after malloc() and with 0x5A before free().
21 * malloc() options variable has to override the libc symbol, otherwise
22 * it has no effect.
23 */
24 #if __FreeBSD_version < 1000011
25 const char *_malloc_options = "J";
26 #else
27 const char *malloc_conf = "junk:true";
28 #endif
29 #endif
30
31
32 nxt_int_t
nxt_lib_start(const char * app,char ** argv,char *** envp)33 nxt_lib_start(const char *app, char **argv, char ***envp)
34 {
35 int n;
36 nxt_int_t flags;
37 nxt_bool_t update;
38 nxt_thread_t *thread;
39
40 flags = nxt_stderr_start();
41
42 nxt_log_start(app);
43
44 nxt_pid = getpid();
45 nxt_ppid = getppid();
46 nxt_euid = geteuid();
47 nxt_egid = getegid();
48
49 #if (NXT_DEBUG)
50
51 nxt_main_log.level = NXT_LOG_DEBUG;
52
53 #if (NXT_HAVE_MALLOPT)
54 /* Fill memory with 0xAA after malloc() and with 0x55 before free(). */
55 mallopt(M_PERTURB, 0x55);
56 #endif
57
58 #if (NXT_MACOSX)
59 /* Fill memory with 0xAA after malloc() and with 0x55 before free(). */
60 setenv("MallocScribble", "1", 0);
61 #endif
62
63 #endif /* NXT_DEBUG */
64
65 /* Thread log is required for nxt_malloc() in nxt_strerror_start(). */
66
67 nxt_thread_init_data(nxt_thread_context);
68 thread = nxt_thread();
69 thread->log = &nxt_main_log;
70
71 thread->handle = nxt_thread_handle();
72 thread->time.signal = -1;
73 nxt_thread_time_update(thread);
74
75 nxt_main_task.thread = thread;
76 nxt_main_task.log = thread->log;
77 nxt_main_task.ident = nxt_task_next_ident();
78
79 if (nxt_strerror_start() != NXT_OK) {
80 return NXT_ERROR;
81 }
82
83 if (flags != -1) {
84 nxt_debug(&nxt_main_task, "stderr flags: 0x%04Xd", flags);
85 }
86
87 #ifdef _SC_NPROCESSORS_ONLN
88 /* Linux, FreeBSD, Solaris, MacOSX. */
89 n = sysconf(_SC_NPROCESSORS_ONLN);
90
91 #elif (NXT_HPUX)
92 n = mpctl(MPC_GETNUMSPUS, NULL, NULL);
93
94 #else
95 n = 0;
96
97 #endif
98
99 nxt_debug(&nxt_main_task, "ncpu: %d", n);
100
101 if (n > 1) {
102 nxt_ncpu = n;
103 }
104
105 nxt_thread_spin_init(nxt_ncpu, 0);
106
107 nxt_random_init(&thread->random);
108
109 nxt_pagesize = getpagesize();
110
111 nxt_debug(&nxt_main_task, "pagesize: %ui", nxt_pagesize);
112
113 if (argv != NULL) {
114 update = (argv[0] == app);
115
116 nxt_process_arguments(&nxt_main_task, argv, envp);
117
118 if (update) {
119 nxt_log_start(nxt_process_argv[0]);
120 }
121 }
122
123 return NXT_OK;
124 }
125
126
127 void
nxt_lib_stop(void)128 nxt_lib_stop(void)
129 {
130 /* TODO: stop engines */
131
132 #if 0
133
134 for ( ;; ) {
135 nxt_thread_pool_t *tp;
136
137 nxt_thread_spin_lock(&rt->lock);
138
139 tp = rt->thread_pools;
140 rt->thread_pools = (tp != NULL) ? tp->next : NULL;
141
142 nxt_thread_spin_unlock(&rt->lock);
143
144 if (tp == NULL) {
145 break;
146 }
147
148 nxt_thread_pool_destroy(tp);
149 }
150
151 #else
152
153 exit(0);
154 nxt_unreachable();
155
156 #endif
157 }
158