Don't use the autofree context for the globals. This causes child smbd's forked
[ira/wip.git] / source3 / smbd / globals.c
1 /*
2    Unix SMB/Netbios implementation.
3    smbd globals
4    Copyright (C) Stefan Metzmacher 2009
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "smbd/globals.h"
22
23 #if defined(WITH_AIO)
24 struct aio_extra *aio_list_head = NULL;
25 struct tevent_signal *aio_signal_event = NULL;
26 int aio_pending_size = 0;
27 int outstanding_aio_calls = 0;
28 #endif
29
30 #ifdef USE_DMAPI
31 struct smbd_dmapi_context *dmapi_ctx = NULL;
32 #endif
33
34
35 bool dfree_broken = false;
36
37 /* how many write cache buffers have been allocated */
38 unsigned int allocated_write_caches = 0;
39
40 int real_max_open_files = 0;
41 struct bitmap *file_bmap = NULL;
42 files_struct *Files = NULL;
43 int files_used = 0;
44 struct fsp_singleton_cache fsp_fi_cache = {
45         .fsp = NULL,
46         .id = {
47                 .devid = 0,
48                 .inode = 0,
49                 .extid = 0
50         }
51 };
52 unsigned long file_gen_counter = 0;
53 int first_file = 0;
54
55 const struct mangle_fns *mangle_fns = NULL;
56
57 unsigned char *chartest = NULL;
58 TDB_CONTEXT *tdb_mangled_cache = NULL;
59
60 /* these tables are used to provide fast tests for characters */
61 unsigned char char_flags[256];
62 /*
63   this determines how many characters are used from the original filename
64   in the 8.3 mangled name. A larger value leads to a weaker hash and more collisions.
65   The largest possible value is 6.
66 */
67 unsigned mangle_prefix = 0;
68 unsigned char base_reverse[256];
69
70 struct msg_state *smbd_msg_state = NULL;
71
72 bool logged_ioctl_message = false;
73
74 int trans_num = 0;
75 pid_t mypid = 0;
76 time_t last_smb_conf_reload_time = 0;
77 time_t last_printer_reload_time = 0;
78 /****************************************************************************
79  structure to hold a linked list of queued messages.
80  for processing.
81 ****************************************************************************/
82 struct pending_message_list *deferred_open_queue = NULL;
83 uint32_t common_flags2 = FLAGS2_LONG_PATH_COMPONENTS|FLAGS2_32_BIT_ERROR_CODES|FLAGS2_EXTENDED_ATTRIBUTES;
84
85 struct smb_srv_trans_enc_ctx *partial_srv_trans_enc_ctx = NULL;
86 struct smb_srv_trans_enc_ctx *srv_trans_enc_ctx = NULL;
87
88 /* A stack of security contexts.  We include the current context as being
89    the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
90 struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
91 int sec_ctx_stack_ndx = 0;
92 bool become_uid_done = false;
93 bool become_gid_done = false;
94
95 connection_struct *last_conn = NULL;
96 uint16_t last_flags = 0;
97
98 uint32_t global_client_caps = 0;
99
100 uint16_t fnf_handle = 257;
101
102 /* A stack of current_user connection contexts. */
103 struct conn_ctx conn_ctx_stack[MAX_SEC_CTX_DEPTH];
104 int conn_ctx_stack_ndx = 0;
105
106 struct vfs_init_function_entry *backends = NULL;
107 char *sparse_buf = NULL;
108
109 /* Current number of oplocks we have outstanding. */
110 int32_t exclusive_oplocks_open = 0;
111 int32_t level_II_oplocks_open = 0;
112 struct kernel_oplocks *koplocks = NULL;
113
114 int am_parent = 1;
115 int server_fd = -1;
116 struct event_context *smbd_event_ctx = NULL;
117 struct messaging_context *smbd_msg_ctx = NULL;
118 struct memcache *smbd_memcache_ctx = NULL;
119 bool exit_firsttime = true;
120 struct child_pid *children = 0;
121 int num_children = 0;
122
123 struct smbd_server_connection *smbd_server_conn = NULL;
124
125 struct messaging_context *smbd_messaging_context(void)
126 {
127         if (smbd_msg_ctx == NULL) {
128                 /*
129                  * Note we MUST use the NULL context here, not the
130                  * autofree context, to avoid side effects in forked
131                  * children exiting.
132                  */
133                 smbd_msg_ctx = messaging_init(NULL,
134                                               procid_self(),
135                                               smbd_event_context());
136         }
137         if (smbd_msg_ctx == NULL) {
138                 DEBUG(0, ("Could not init smbd messaging context.\n"));
139         }
140         return smbd_msg_ctx;
141 }
142
143 struct memcache *smbd_memcache(void)
144 {
145         if (!smbd_memcache_ctx) {
146                 /*
147                  * Note we MUST use the NULL context here, not the
148                  * autofree context, to avoid side effects in forked
149                  * children exiting.
150                  */
151                 smbd_memcache_ctx = memcache_init(NULL,
152                                                   lp_max_stat_cache_size()*1024);
153         }
154         if (!smbd_memcache_ctx) {
155                 smb_panic("Could not init smbd memcache");
156         }
157
158         return smbd_memcache_ctx;
159 }
160
161
162 void smbd_init_globals(void)
163 {
164         ZERO_STRUCT(char_flags);
165         ZERO_STRUCT(base_reverse);
166
167         ZERO_STRUCT(conn_ctx_stack);
168
169         ZERO_STRUCT(sec_ctx_stack);
170
171         smbd_server_conn = talloc_zero(smbd_event_context(), struct smbd_server_connection);
172         if (!smbd_server_conn) {
173                 exit_server("failed to create smbd_server_connection");
174         }
175
176         smbd_server_conn->smb1.echo_handler.trusted_fd = -1;
177         smbd_server_conn->smb1.echo_handler.socket_lock_fd = -1;
178 }