CVE-2023-3347: CI: add a test for server-side mandatory signing
[Samba.git] / source3 / profile / profile.c
blob6decb855282a8f861f70fc01bc2621303d62bdd5
1 /*
2 Unix SMB/CIFS implementation.
3 store smbd profiling information in shared memory
4 Copyright (C) Andrew Tridgell 1999
5 Copyright (C) James Peach 2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "system/time.h"
25 #include "messages.h"
26 #include "smbprofile.h"
27 #include "lib/tdb_wrap/tdb_wrap.h"
28 #include <tevent.h>
29 #include "../lib/crypto/crypto.h"
31 #ifdef HAVE_SYS_RESOURCE_H
32 #include <sys/resource.h>
33 #endif
35 struct profile_stats *profile_p;
36 struct smbprofile_global_state smbprofile_state;
38 /****************************************************************************
39 Set a profiling level.
40 ****************************************************************************/
41 void set_profile_level(int level, const struct server_id *src)
43 SMB_ASSERT(smbprofile_state.internal.db != NULL);
45 switch (level) {
46 case 0: /* turn off profiling */
47 smbprofile_state.config.do_count = false;
48 smbprofile_state.config.do_times = false;
49 DEBUG(1,("INFO: Profiling turned OFF from pid %d\n",
50 (int)procid_to_pid(src)));
51 break;
52 case 1: /* turn on counter profiling only */
53 smbprofile_state.config.do_count = true;
54 smbprofile_state.config.do_times = false;
55 DEBUG(1,("INFO: Profiling counts turned ON from pid %d\n",
56 (int)procid_to_pid(src)));
57 break;
58 case 2: /* turn on complete profiling */
59 smbprofile_state.config.do_count = true;
60 smbprofile_state.config.do_times = true;
61 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n",
62 (int)procid_to_pid(src)));
63 break;
64 case 3: /* reset profile values */
65 ZERO_STRUCT(profile_p->values);
66 tdb_wipe_all(smbprofile_state.internal.db->tdb);
67 DEBUG(1,("INFO: Profiling values cleared from pid %d\n",
68 (int)procid_to_pid(src)));
69 break;
73 /****************************************************************************
74 receive a set profile level message
75 ****************************************************************************/
76 static void profile_message(struct messaging_context *msg_ctx,
77 void *private_data,
78 uint32_t msg_type,
79 struct server_id src,
80 DATA_BLOB *data)
82 int level;
84 if (data->length != sizeof(level)) {
85 DEBUG(0, ("got invalid profile message\n"));
86 return;
89 memcpy(&level, data->data, sizeof(level));
90 set_profile_level(level, &src);
93 /****************************************************************************
94 receive a request profile level message
95 ****************************************************************************/
96 static void reqprofile_message(struct messaging_context *msg_ctx,
97 void *private_data,
98 uint32_t msg_type,
99 struct server_id src,
100 DATA_BLOB *data)
102 int level;
104 level = 1;
105 if (smbprofile_state.config.do_count) {
106 level += 2;
108 if (smbprofile_state.config.do_times) {
109 level += 4;
112 DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",
113 (unsigned int)procid_to_pid(&src)));
114 messaging_send_buf(msg_ctx, src, MSG_PROFILELEVEL,
115 (uint8_t *)&level, sizeof(level));
118 /*******************************************************************
119 open the profiling shared memory area
120 ******************************************************************/
121 bool profile_setup(struct messaging_context *msg_ctx, bool rdonly)
123 char *db_name;
124 bool ok = false;
125 int rc;
127 if (smbprofile_state.internal.db != NULL) {
128 return true;
131 db_name = cache_path(talloc_tos(), "smbprofile.tdb");
132 if (db_name == NULL) {
133 return false;
136 smbprofile_state.internal.db = tdb_wrap_open(
137 NULL, db_name, 0,
138 rdonly ? 0 : TDB_CLEAR_IF_FIRST|TDB_MUTEX_LOCKING,
139 O_CREAT | (rdonly ? O_RDONLY : O_RDWR), 0644);
140 if (smbprofile_state.internal.db == NULL) {
141 return false;
144 if (msg_ctx != NULL) {
145 messaging_register(msg_ctx, NULL, MSG_PROFILE,
146 profile_message);
147 messaging_register(msg_ctx, NULL, MSG_REQ_PROFILELEVEL,
148 reqprofile_message);
151 profile_p = &smbprofile_state.stats.global;
153 rc = smbprofile_magic(profile_p, &profile_p->magic);
154 if (rc != 0) {
155 goto out;
158 ok = true;
159 out:
161 return ok;
164 void smbprofile_dump_setup(struct tevent_context *ev)
166 TALLOC_FREE(smbprofile_state.internal.te);
167 smbprofile_state.internal.ev = ev;
170 static void smbprofile_dump_timer(struct tevent_context *ev,
171 struct tevent_timer *te,
172 struct timeval current_time,
173 void *private_data)
175 smbprofile_dump();
178 void smbprofile_dump_schedule_timer(void)
180 struct timeval tv;
182 GetTimeOfDay(&tv);
183 tv.tv_sec += 1;
185 smbprofile_state.internal.te = tevent_add_timer(
186 smbprofile_state.internal.ev,
187 smbprofile_state.internal.ev,
189 smbprofile_dump_timer,
190 NULL);
193 static int profile_stats_parser(TDB_DATA key, TDB_DATA value,
194 void *private_data)
196 struct profile_stats *s = private_data;
198 if (value.dsize != sizeof(struct profile_stats)) {
199 *s = (struct profile_stats) {};
200 return 0;
203 memcpy(s, value.dptr, value.dsize);
204 if (s->magic != profile_p->magic) {
205 *s = (struct profile_stats) {};
206 return 0;
209 return 0;
212 void smbprofile_dump(void)
214 pid_t pid = 0;
215 TDB_DATA key = { .dptr = (uint8_t *)&pid, .dsize = sizeof(pid) };
216 struct profile_stats s = {};
217 int ret;
218 #ifdef HAVE_GETRUSAGE
219 struct rusage rself;
220 #endif /* HAVE_GETRUSAGE */
222 TALLOC_FREE(smbprofile_state.internal.te);
224 if (! (smbprofile_state.config.do_count ||
225 smbprofile_state.config.do_times)) {
226 return;
229 if (smbprofile_state.internal.db == NULL) {
230 return;
233 pid = tevent_cached_getpid();
235 #ifdef HAVE_GETRUSAGE
236 ret = getrusage(RUSAGE_SELF, &rself);
237 if (ret != 0) {
238 ZERO_STRUCT(rself);
241 profile_p->values.cpu_user_stats.time =
242 (rself.ru_utime.tv_sec * 1000000) +
243 rself.ru_utime.tv_usec;
244 profile_p->values.cpu_system_stats.time =
245 (rself.ru_stime.tv_sec * 1000000) +
246 rself.ru_stime.tv_usec;
247 #endif /* HAVE_GETRUSAGE */
249 ret = tdb_chainlock(smbprofile_state.internal.db->tdb, key);
250 if (ret != 0) {
251 return;
254 tdb_parse_record(smbprofile_state.internal.db->tdb,
255 key, profile_stats_parser, &s);
257 smbprofile_stats_accumulate(profile_p, &s);
259 tdb_store(smbprofile_state.internal.db->tdb, key,
260 (TDB_DATA) {
261 .dptr = (uint8_t *)profile_p,
262 .dsize = sizeof(*profile_p)
266 tdb_chainunlock(smbprofile_state.internal.db->tdb, key);
267 ZERO_STRUCT(profile_p->values);
269 return;
272 void smbprofile_cleanup(pid_t pid, pid_t dst)
274 TDB_DATA key = { .dptr = (uint8_t *)&pid, .dsize = sizeof(pid) };
275 struct profile_stats s = {};
276 struct profile_stats acc = {};
277 int ret;
279 if (smbprofile_state.internal.db == NULL) {
280 return;
283 ret = tdb_chainlock(smbprofile_state.internal.db->tdb, key);
284 if (ret != 0) {
285 return;
287 ret = tdb_parse_record(smbprofile_state.internal.db->tdb,
288 key, profile_stats_parser, &s);
289 if (ret == -1) {
290 tdb_chainunlock(smbprofile_state.internal.db->tdb, key);
291 return;
293 tdb_delete(smbprofile_state.internal.db->tdb, key);
294 tdb_chainunlock(smbprofile_state.internal.db->tdb, key);
296 pid = dst;
297 ret = tdb_chainlock(smbprofile_state.internal.db->tdb, key);
298 if (ret != 0) {
299 return;
301 tdb_parse_record(smbprofile_state.internal.db->tdb,
302 key, profile_stats_parser, &acc);
305 * We may have to fix the disconnect count
306 * in case the process died
308 s.values.disconnect_stats.count = s.values.connect_stats.count;
310 smbprofile_stats_accumulate(&acc, &s);
312 acc.magic = profile_p->magic;
313 tdb_store(smbprofile_state.internal.db->tdb, key,
314 (TDB_DATA) {
315 .dptr = (uint8_t *)&acc,
316 .dsize = sizeof(acc)
320 tdb_chainunlock(smbprofile_state.internal.db->tdb, key);
323 void smbprofile_collect(struct profile_stats *stats)
325 if (smbprofile_state.internal.db == NULL) {
326 return;
328 smbprofile_collect_tdb(smbprofile_state.internal.db->tdb,
329 profile_p->magic,
330 stats);