s3:smbd/globals: change 'int am_parent' into 'struct smbd_parent_context *am_parent'
[kai/samba.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/smbd.h"
22 #include "smbd/globals.h"
23 #include "lib/smbd_shim.h"
24 #include "memcache.h"
25 #include "messages.h"
26 #include "tdb_compat.h"
27
28 #if defined(WITH_AIO)
29 struct aio_extra *aio_list_head = NULL;
30 struct tevent_signal *aio_signal_event = NULL;
31 int aio_pending_size = 0;
32 int outstanding_aio_calls = 0;
33 #endif
34
35 #ifdef USE_DMAPI
36 struct smbd_dmapi_context *dmapi_ctx = NULL;
37 #endif
38
39
40 bool dfree_broken = false;
41
42 /* how many write cache buffers have been allocated */
43 unsigned int allocated_write_caches = 0;
44
45 const struct mangle_fns *mangle_fns = NULL;
46
47 unsigned char *chartest = NULL;
48 TDB_CONTEXT *tdb_mangled_cache = NULL;
49
50 /*
51   this determines how many characters are used from the original filename
52   in the 8.3 mangled name. A larger value leads to a weaker hash and more collisions.
53   The largest possible value is 6.
54 */
55 unsigned mangle_prefix = 0;
56
57 struct msg_state *smbd_msg_state = NULL;
58
59 bool logged_ioctl_message = false;
60
61 time_t last_smb_conf_reload_time = 0;
62 time_t last_printer_reload_time = 0;
63 pid_t background_lpq_updater_pid = -1;
64
65 /****************************************************************************
66  structure to hold a linked list of queued messages.
67  for processing.
68 ****************************************************************************/
69 uint32_t common_flags2 = FLAGS2_LONG_PATH_COMPONENTS|FLAGS2_32_BIT_ERROR_CODES|FLAGS2_EXTENDED_ATTRIBUTES;
70
71 struct smb_trans_enc_state *partial_srv_trans_enc_ctx = NULL;
72 struct smb_trans_enc_state *srv_trans_enc_ctx = NULL;
73
74 /* A stack of security contexts.  We include the current context as being
75    the first one, so there is room for another MAX_SEC_CTX_DEPTH more. */
76 struct sec_ctx sec_ctx_stack[MAX_SEC_CTX_DEPTH + 1];
77 int sec_ctx_stack_ndx = 0;
78 bool become_uid_done = false;
79 bool become_gid_done = false;
80
81 connection_struct *last_conn = NULL;
82 uint16_t last_flags = 0;
83
84 uint32_t global_client_caps = 0;
85
86 uint16_t fnf_handle = 257;
87
88 /* A stack of current_user connection contexts. */
89 struct conn_ctx conn_ctx_stack[MAX_SEC_CTX_DEPTH];
90 int conn_ctx_stack_ndx = 0;
91
92 struct vfs_init_function_entry *backends = NULL;
93 char *sparse_buf = NULL;
94 char *LastDir = NULL;
95
96 /* Current number of oplocks we have outstanding. */
97 int32_t exclusive_oplocks_open = 0;
98 int32_t level_II_oplocks_open = 0;
99 struct kernel_oplocks *koplocks = NULL;
100
101 struct smbd_parent_context *am_parent = NULL;
102 struct memcache *smbd_memcache_ctx = NULL;
103 bool exit_firsttime = true;
104 struct child_pid *children = 0;
105 int num_children = 0;
106
107 struct smbd_server_connection *smbd_server_conn = NULL;
108
109 struct smbd_server_connection *msg_ctx_to_sconn(struct messaging_context *msg_ctx)
110 {
111         struct server_id my_id, msg_id;
112
113         my_id = messaging_server_id(smbd_server_conn->msg_ctx);
114         msg_id = messaging_server_id(msg_ctx);
115
116         if (!procid_equal(&my_id, &msg_id)) {
117                 return NULL;
118         }
119         return smbd_server_conn;
120 }
121
122 struct messaging_context *smbd_messaging_context(void)
123 {
124         struct messaging_context *msg_ctx = server_messaging_context();
125         if (likely(msg_ctx != NULL)) {
126                 return msg_ctx;
127         }
128         smb_panic("Could not init smbd's messaging context.\n");
129         return NULL;
130 }
131
132 struct memcache *smbd_memcache(void)
133 {
134         if (!smbd_memcache_ctx) {
135                 /*
136                  * Note we MUST use the NULL context here, not the
137                  * autofree context, to avoid side effects in forked
138                  * children exiting.
139                  */
140                 smbd_memcache_ctx = memcache_init(NULL,
141                                                   lp_max_stat_cache_size()*1024);
142         }
143         if (!smbd_memcache_ctx) {
144                 smb_panic("Could not init smbd memcache");
145         }
146
147         return smbd_memcache_ctx;
148 }
149
150 static const struct smbd_shim smbd_shim_fns = 
151 {
152         .cancel_pending_lock_requests_by_fid = smbd_cancel_pending_lock_requests_by_fid,
153         .send_stat_cache_delete_message = smbd_send_stat_cache_delete_message,
154         .change_to_root_user = smbd_change_to_root_user,
155
156         .contend_level2_oplocks_begin = smbd_contend_level2_oplocks_begin,
157         .contend_level2_oplocks_end = smbd_contend_level2_oplocks_end,
158
159         .become_root = smbd_become_root,
160         .unbecome_root = smbd_unbecome_root
161 };
162
163 void smbd_init_globals(void)
164 {
165         set_smbd_shim(&smbd_shim_fns);
166
167         ZERO_STRUCT(conn_ctx_stack);
168
169         ZERO_STRUCT(sec_ctx_stack);
170
171         smbd_server_conn = talloc_zero(server_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->ev_ctx = server_event_context();
177         smbd_server_conn->smb1.echo_handler.trusted_fd = -1;
178         smbd_server_conn->smb1.echo_handler.socket_lock_fd = -1;
179 }