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/>.
23 #include "librpc/gen_ndr/messaging.h"
26 #define IPC_PERMS ((S_IRUSR | S_IWUSR) | S_IRGRP | S_IROTH)
27 #endif /* WITH_PROFILE */
31 static bool read_only;
32 clockid_t __profile_clock;
33 bool have_profiling_clock = False;
36 struct profile_header *profile_h;
37 struct profile_stats *profile_p;
39 bool do_profile_flag = False;
40 bool do_profile_times = False;
42 /****************************************************************************
43 Set a profiling level.
44 ****************************************************************************/
45 void set_profile_level(int level, struct server_id src)
49 case 0: /* turn off profiling */
50 do_profile_flag = False;
51 do_profile_times = False;
52 DEBUG(1,("INFO: Profiling turned OFF from pid %d\n",
53 (int)procid_to_pid(&src)));
55 case 1: /* turn on counter profiling only */
56 do_profile_flag = True;
57 do_profile_times = False;
58 DEBUG(1,("INFO: Profiling counts turned ON from pid %d\n",
59 (int)procid_to_pid(&src)));
61 case 2: /* turn on complete profiling */
63 #if defined(HAVE_CLOCK_GETTIME)
64 if (!have_profiling_clock) {
65 do_profile_flag = True;
66 do_profile_times = False;
67 DEBUG(1,("INFO: Profiling counts turned ON from "
68 "pid %d\n", (int)procid_to_pid(&src)));
69 DEBUGADD(1,("INFO: Profiling times disabled "
70 "due to lack of a suitable clock\n"));
75 do_profile_flag = True;
76 do_profile_times = True;
77 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n",
78 (int)procid_to_pid(&src)));
80 case 3: /* reset profile values */
81 memset((char *)profile_p, 0, sizeof(*profile_p));
82 DEBUG(1,("INFO: Profiling values cleared from pid %d\n",
83 (int)procid_to_pid(&src)));
86 #else /* WITH_PROFILE */
87 DEBUG(1,("INFO: Profiling support unavailable in this build.\n"));
88 #endif /* WITH_PROFILE */
93 /****************************************************************************
94 receive a set profile level message
95 ****************************************************************************/
96 static void profile_message(struct messaging_context *msg_ctx,
104 if (data->length != sizeof(level)) {
105 DEBUG(0, ("got invalid profile message\n"));
109 memcpy(&level, data->data, sizeof(level));
110 set_profile_level(level, src);
113 /****************************************************************************
114 receive a request profile level message
115 ****************************************************************************/
116 static void reqprofile_message(struct messaging_context *msg_ctx,
119 struct server_id src,
125 level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
129 DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",
130 (unsigned int)procid_to_pid(&src)));
131 messaging_send_buf(msg_ctx, src, MSG_PROFILELEVEL,
132 (uint8 *)&level, sizeof(level));
135 /*******************************************************************
136 open the profiling shared memory area
137 ******************************************************************/
139 /* Find a clock. Just because the definition for a particular clock ID is
140 * present doesn't mean the system actually supports it.
142 static void init_clock_gettime(void)
146 have_profiling_clock = False;
148 #ifdef HAVE_CLOCK_MONOTONIC
149 if (!have_profiling_clock &&
150 clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
151 DEBUG(10, ("Using CLOCK_MONOTONIC for profile_clock\n"));
152 __profile_clock = CLOCK_MONOTONIC;
153 have_profiling_clock = True;
157 #ifdef HAVE_CLOCK_REALTIME
158 /* POSIX says that CLOCK_REALTIME should be defined everywhere
159 * where we have clock_gettime...
161 if (!have_profiling_clock &&
162 clock_gettime(CLOCK_REALTIME, &ts) == 0) {
163 __profile_clock = CLOCK_REALTIME;
164 have_profiling_clock = True;
166 SMB_WARN(__profile_clock != CLOCK_REALTIME,
167 ("forced to use a slow profiling clock"));
172 SMB_WARN(have_profiling_clock == True,
173 ("could not find a working clock for profiling"));
177 bool profile_setup(struct messaging_context *msg_ctx, bool rdonly)
179 struct shmid_ds shm_ds;
183 init_clock_gettime();
186 /* try to use an existing key */
187 shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
189 /* if that failed then create one. There is a race condition here
190 if we are running from inetd. Bad luck. */
192 if (read_only) return False;
193 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h),
194 IPC_CREAT | IPC_EXCL | IPC_PERMS);
198 DEBUG(0,("Can't create or use IPC area. Error was %s\n",
204 profile_h = (struct profile_header *)shmat(shm_id, 0,
205 read_only?SHM_RDONLY:0);
206 if ((long)profile_h == -1) {
207 DEBUG(0,("Can't attach to IPC area. Error was %s\n",
212 /* find out who created this memory area */
213 if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
214 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n",
219 if (shm_ds.shm_perm.cuid != sec_initial_uid() ||
220 shm_ds.shm_perm.cgid != sec_initial_gid()) {
221 DEBUG(0,("ERROR: we did not create the shmem "
222 "(owned by another user, uid %u, gid %u)\n",
223 shm_ds.shm_perm.cuid,
224 shm_ds.shm_perm.cgid));
228 if (shm_ds.shm_segsz != sizeof(*profile_h)) {
229 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
230 (int)shm_ds.shm_segsz, (int)sizeof(*profile_h)));
231 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
238 if (!read_only && (shm_ds.shm_nattch == 1)) {
239 memset((char *)profile_h, 0, sizeof(*profile_h));
240 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
241 profile_h->prof_shm_version = PROF_SHM_VERSION;
242 DEBUG(3,("Initialised profile area\n"));
245 profile_p = &profile_h->stats;
246 if (msg_ctx != NULL) {
247 messaging_register(msg_ctx, NULL, MSG_PROFILE,
249 messaging_register(msg_ctx, NULL, MSG_REQ_PROFILELEVEL,
255 const char * profile_value_name(enum profile_stats_values val)
257 static const char * valnames[PR_VALUE_MAX + 1] =
259 "smbd_idle", /* PR_VALUE_SMBD_IDLE */
260 "syscall_opendir", /* PR_VALUE_SYSCALL_OPENDIR */
261 "syscall_readdir", /* PR_VALUE_SYSCALL_READDIR */
262 "syscall_seekdir", /* PR_VALUE_SYSCALL_SEEKDIR */
263 "syscall_telldir", /* PR_VALUE_SYSCALL_TELLDIR */
264 "syscall_rewinddir", /* PR_VALUE_SYSCALL_REWINDDIR */
265 "syscall_mkdir", /* PR_VALUE_SYSCALL_MKDIR */
266 "syscall_rmdir", /* PR_VALUE_SYSCALL_RMDIR */
267 "syscall_closedir", /* PR_VALUE_SYSCALL_CLOSEDIR */
268 "syscall_open", /* PR_VALUE_SYSCALL_OPEN */
269 "syscall_createfile", /* PR_VALUE_SYSCALL_CREATEFILE */
270 "syscall_close", /* PR_VALUE_SYSCALL_CLOSE */
271 "syscall_read", /* PR_VALUE_SYSCALL_READ */
272 "syscall_pread", /* PR_VALUE_SYSCALL_PREAD */
273 "syscall_write", /* PR_VALUE_SYSCALL_WRITE */
274 "syscall_pwrite", /* PR_VALUE_SYSCALL_PWRITE */
275 "syscall_lseek", /* PR_VALUE_SYSCALL_LSEEK */
276 "syscall_sendfile", /* PR_VALUE_SYSCALL_SENDFILE */
277 "syscall_recvfile", /* PR_VALUE_SYSCALL_RECVFILE */
278 "syscall_rename", /* PR_VALUE_SYSCALL_RENAME */
279 "syscall_rename_at", /* PR_VALUE_SYSCALL_RENAME_AT */
280 "syscall_fsync", /* PR_VALUE_SYSCALL_FSYNC */
281 "syscall_stat", /* PR_VALUE_SYSCALL_STAT */
282 "syscall_fstat", /* PR_VALUE_SYSCALL_FSTAT */
283 "syscall_lstat", /* PR_VALUE_SYSCALL_LSTAT */
284 "syscall_unlink", /* PR_VALUE_SYSCALL_UNLINK */
285 "syscall_chmod", /* PR_VALUE_SYSCALL_CHMOD */
286 "syscall_fchmod", /* PR_VALUE_SYSCALL_FCHMOD */
287 "syscall_chown", /* PR_VALUE_SYSCALL_CHOWN */
288 "syscall_fchown", /* PR_VALUE_SYSCALL_FCHOWN */
289 "syscall_chdir", /* PR_VALUE_SYSCALL_CHDIR */
290 "syscall_getwd", /* PR_VALUE_SYSCALL_GETWD */
291 "syscall_ntimes", /* PR_VALUE_SYSCALL_NTIMES */
292 "syscall_ftruncate", /* PR_VALUE_SYSCALL_FTRUNCATE */
293 "syscall_fcntl_lock", /* PR_VALUE_SYSCALL_FCNTL_LOCK */
294 "syscall_kernel_flock", /* PR_VALUE_SYSCALL_KERNEL_FLOCK */
295 "syscall_linux_setlease", /* PR_VALUE_SYSCALL_LINUX_SETLEASE */
296 "syscall_fcntl_getlock", /* PR_VALUE_SYSCALL_FCNTL_GETLOCK */
297 "syscall_readlink", /* PR_VALUE_SYSCALL_READLINK */
298 "syscall_symlink", /* PR_VALUE_SYSCALL_SYMLINK */
299 "syscall_link", /* PR_VALUE_SYSCALL_LINK */
300 "syscall_mknod", /* PR_VALUE_SYSCALL_MKNOD */
301 "syscall_realpath", /* PR_VALUE_SYSCALL_REALPATH */
302 "syscall_get_quota", /* PR_VALUE_SYSCALL_GET_QUOTA */
303 "syscall_set_quota", /* PR_VALUE_SYSCALL_SET_QUOTA */
304 "syscall_get_sd", /* PR_VALUE_SYSCALL_GET_SD */
305 "syscall_set_sd", /* PR_VALUE_SYSCALL_SET_SD */
306 "syscall_brl_lock", /* PR_VALUE_SYSCALL_BRL_LOCK */
307 "syscall_brl_unlock", /* PR_VALUE_SYSCALL_BRL_UNLOCK */
308 "syscall_brl_cancel", /* PR_VALUE_SYSCALL_BRL_CANCEL */
309 "SMBmkdir", /* PR_VALUE_SMBMKDIR */
310 "SMBrmdir", /* PR_VALUE_SMBRMDIR */
311 "SMBopen", /* PR_VALUE_SMBOPEN */
312 "SMBcreate", /* PR_VALUE_SMBCREATE */
313 "SMBclose", /* PR_VALUE_SMBCLOSE */
314 "SMBflush", /* PR_VALUE_SMBFLUSH */
315 "SMBunlink", /* PR_VALUE_SMBUNLINK */
316 "SMBmv", /* PR_VALUE_SMBMV */
317 "SMBgetatr", /* PR_VALUE_SMBGETATR */
318 "SMBsetatr", /* PR_VALUE_SMBSETATR */
319 "SMBread", /* PR_VALUE_SMBREAD */
320 "SMBwrite", /* PR_VALUE_SMBWRITE */
321 "SMBlock", /* PR_VALUE_SMBLOCK */
322 "SMBunlock", /* PR_VALUE_SMBUNLOCK */
323 "SMBctemp", /* PR_VALUE_SMBCTEMP */
324 "SMBmknew", /* PR_VALUE_SMBMKNEW */
325 "SMBcheckpath", /* PR_VALUE_SMBCHECKPATH */
326 "SMBexit", /* PR_VALUE_SMBEXIT */
327 "SMBlseek", /* PR_VALUE_SMBLSEEK */
328 "SMBlockread", /* PR_VALUE_SMBLOCKREAD */
329 "SMBwriteunlock", /* PR_VALUE_SMBWRITEUNLOCK */
330 "SMBreadbraw", /* PR_VALUE_SMBREADBRAW */
331 "SMBreadBmpx", /* PR_VALUE_SMBREADBMPX */
332 "SMBreadBs", /* PR_VALUE_SMBREADBS */
333 "SMBwritebraw", /* PR_VALUE_SMBWRITEBRAW */
334 "SMBwriteBmpx", /* PR_VALUE_SMBWRITEBMPX */
335 "SMBwriteBs", /* PR_VALUE_SMBWRITEBS */
336 "SMBwritec", /* PR_VALUE_SMBWRITEC */
337 "SMBsetattrE", /* PR_VALUE_SMBSETATTRE */
338 "SMBgetattrE", /* PR_VALUE_SMBGETATTRE */
339 "SMBlockingX", /* PR_VALUE_SMBLOCKINGX */
340 "SMBtrans", /* PR_VALUE_SMBTRANS */
341 "SMBtranss", /* PR_VALUE_SMBTRANSS */
342 "SMBioctl", /* PR_VALUE_SMBIOCTL */
343 "SMBioctls", /* PR_VALUE_SMBIOCTLS */
344 "SMBcopy", /* PR_VALUE_SMBCOPY */
345 "SMBmove", /* PR_VALUE_SMBMOVE */
346 "SMBecho", /* PR_VALUE_SMBECHO */
347 "SMBwriteclose", /* PR_VALUE_SMBWRITECLOSE */
348 "SMBopenX", /* PR_VALUE_SMBOPENX */
349 "SMBreadX", /* PR_VALUE_SMBREADX */
350 "SMBwriteX", /* PR_VALUE_SMBWRITEX */
351 "SMBtrans2", /* PR_VALUE_SMBTRANS2 */
352 "SMBtranss2", /* PR_VALUE_SMBTRANSS2 */
353 "SMBfindclose", /* PR_VALUE_SMBFINDCLOSE */
354 "SMBfindnclose", /* PR_VALUE_SMBFINDNCLOSE */
355 "SMBtcon", /* PR_VALUE_SMBTCON */
356 "SMBtdis", /* PR_VALUE_SMBTDIS */
357 "SMBnegprot", /* PR_VALUE_SMBNEGPROT */
358 "SMBsesssetupX", /* PR_VALUE_SMBSESSSETUPX */
359 "SMBulogoffX", /* PR_VALUE_SMBULOGOFFX */
360 "SMBtconX", /* PR_VALUE_SMBTCONX */
361 "SMBdskattr", /* PR_VALUE_SMBDSKATTR */
362 "SMBsearch", /* PR_VALUE_SMBSEARCH */
363 "SMBffirst", /* PR_VALUE_SMBFFIRST */
364 "SMBfunique", /* PR_VALUE_SMBFUNIQUE */
365 "SMBfclose", /* PR_VALUE_SMBFCLOSE */
366 "SMBnttrans", /* PR_VALUE_SMBNTTRANS */
367 "SMBnttranss", /* PR_VALUE_SMBNTTRANSS */
368 "SMBntcreateX", /* PR_VALUE_SMBNTCREATEX */
369 "SMBntcancel", /* PR_VALUE_SMBNTCANCEL */
370 "SMBntrename", /* PR_VALUE_SMBNTRENAME */
371 "SMBsplopen", /* PR_VALUE_SMBSPLOPEN */
372 "SMBsplwr", /* PR_VALUE_SMBSPLWR */
373 "SMBsplclose", /* PR_VALUE_SMBSPLCLOSE */
374 "SMBsplretq", /* PR_VALUE_SMBSPLRETQ */
375 "SMBsends", /* PR_VALUE_SMBSENDS */
376 "SMBsendb", /* PR_VALUE_SMBSENDB */
377 "SMBfwdname", /* PR_VALUE_SMBFWDNAME */
378 "SMBcancelf", /* PR_VALUE_SMBCANCELF */
379 "SMBgetmac", /* PR_VALUE_SMBGETMAC */
380 "SMBsendstrt", /* PR_VALUE_SMBSENDSTRT */
381 "SMBsendend", /* PR_VALUE_SMBSENDEND */
382 "SMBsendtxt", /* PR_VALUE_SMBSENDTXT */
383 "SMBinvalid", /* PR_VALUE_SMBINVALID */
384 "pathworks_setdir", /* PR_VALUE_PATHWORKS_SETDIR */
385 "Trans2_open", /* PR_VALUE_TRANS2_OPEN */
386 "Trans2_findfirst", /* PR_VALUE_TRANS2_FINDFIRST */
387 "Trans2_findnext", /* PR_VALUE_TRANS2_FINDNEXT */
388 "Trans2_qfsinfo", /* PR_VALUE_TRANS2_QFSINFO */
389 "Trans2_setfsinfo", /* PR_VALUE_TRANS2_SETFSINFO */
390 "Trans2_qpathinfo", /* PR_VALUE_TRANS2_QPATHINFO */
391 "Trans2_setpathinfo", /* PR_VALUE_TRANS2_SETPATHINFO */
392 "Trans2_qfileinfo", /* PR_VALUE_TRANS2_QFILEINFO */
393 "Trans2_setfileinfo", /* PR_VALUE_TRANS2_SETFILEINFO */
394 "Trans2_fsctl", /* PR_VALUE_TRANS2_FSCTL */
395 "Trans2_ioctl", /* PR_VALUE_TRANS2_IOCTL */
396 "Trans2_findnotifyfirst", /* PR_VALUE_TRANS2_FINDNOTIFYFIRST */
397 "Trans2_findnotifynext", /* PR_VALUE_TRANS2_FINDNOTIFYNEXT */
398 "Trans2_mkdir", /* PR_VALUE_TRANS2_MKDIR */
399 "Trans2_session_setup", /* PR_VALUE_TRANS2_SESSION_SETUP */
400 "Trans2_get_dfs_referral", /* PR_VALUE_TRANS2_GET_DFS_REFERRAL */
401 "Trans2_report_dfs_inconsistancy", /* PR_VALUE_TRANS2_REPORT_DFS_INCONSISTANCY */
402 "NT_transact_create", /* PR_VALUE_NT_TRANSACT_CREATE */
403 "NT_transact_ioctl", /* PR_VALUE_NT_TRANSACT_IOCTL */
404 "NT_transact_set_security_desc", /* PR_VALUE_NT_TRANSACT_SET_SECURITY_DESC */
405 "NT_transact_notify_change",/* PR_VALUE_NT_TRANSACT_NOTIFY_CHANGE */
406 "NT_transact_rename", /* PR_VALUE_NT_TRANSACT_RENAME */
407 "NT_transact_query_security_desc", /* PR_VALUE_NT_TRANSACT_QUERY_SECURITY_DESC */
408 "NT_transact_get_user_quota",/* PR_VALUE_NT_TRANSACT_GET_USER_QUOTA */
409 "NT_transact_set_user_quota",/* PR_VALUE_NT_TRANSACT_SET_USER_QUOTA */
410 "get_nt_acl", /* PR_VALUE_GET_NT_ACL */
411 "fget_nt_acl", /* PR_VALUE_FGET_NT_ACL */
412 "fset_nt_acl", /* PR_VALUE_FSET_NT_ACL */
413 "chmod_acl", /* PR_VALUE_CHMOD_ACL */
414 "fchmod_acl", /* PR_VALUE_FCHMOD_ACL */
415 "name_release", /* PR_VALUE_NAME_RELEASE */
416 "name_refresh", /* PR_VALUE_NAME_REFRESH */
417 "name_registration", /* PR_VALUE_NAME_REGISTRATION */
418 "node_status", /* PR_VALUE_NODE_STATUS */
419 "name_query", /* PR_VALUE_NAME_QUERY */
420 "host_announce", /* PR_VALUE_HOST_ANNOUNCE */
421 "workgroup_announce", /* PR_VALUE_WORKGROUP_ANNOUNCE */
422 "local_master_announce", /* PR_VALUE_LOCAL_MASTER_ANNOUNCE */
423 "master_browser_announce", /* PR_VALUE_MASTER_BROWSER_ANNOUNCE */
424 "lm_host_announce", /* PR_VALUE_LM_HOST_ANNOUNCE */
425 "get_backup_list", /* PR_VALUE_GET_BACKUP_LIST */
426 "reset_browser", /* PR_VALUE_RESET_BROWSER */
427 "announce_request", /* PR_VALUE_ANNOUNCE_REQUEST */
428 "lm_announce_request", /* PR_VALUE_LM_ANNOUNCE_REQUEST */
429 "domain_logon", /* PR_VALUE_DOMAIN_LOGON */
430 "sync_browse_lists", /* PR_VALUE_SYNC_BROWSE_LISTS */
431 "run_elections", /* PR_VALUE_RUN_ELECTIONS */
432 "election", /* PR_VALUE_ELECTION */
433 "smb2_negprot", /* PR_VALUE_SMB2_NEGPROT */
434 "smb2_sesssetup", /* PR_VALUE_SMB2_SESSETUP */
435 "smb2_logoff", /* PR_VALUE_SMB2_LOGOFF */
436 "smb2_tcon", /* PR_VALUE_SMB2_TCON */
437 "smb2_tdis", /* PR_VALUE_SMB2_TDIS */
438 "smb2_create", /* PR_VALUE_SMB2_CREATE */
439 "smb2_close", /* PR_VALUE_SMB2_CLOSE */
440 "smb2_flush", /* PR_VALUE_SMB2_FLUSH */
441 "smb2_read", /* PR_VALUE_SMB2_READ */
442 "smb2_write", /* PR_VALUE_SMB2_WRITE */
443 "smb2_lock", /* PR_VALUE_SMB2_LOCK */
444 "smb2_ioctl", /* PR_VALUE_SMB2_IOCTL */
445 "smb2_cancel", /* PR_VALUE_SMB2_CANCEL */
446 "smb2_keepalive", /* PR_VALUE_SMB2_KEEPALIVE */
447 "smb2_find", /* PR_VALUE_SMB2_FIND */
448 "smb2_notify", /* PR_VALUE_SMB2_NOTIFY */
449 "smb2_getinfo", /* PR_VALUE_SMB2_GETINFO */
450 "smb2_setinfo" /* PR_VALUE_SMB2_SETINFO */
451 "smb2_break", /* PR_VALUE_SMB2_BREAK */
452 "" /* PR_VALUE_MAX */
455 SMB_ASSERT(val >= 0);
456 SMB_ASSERT(val < PR_VALUE_MAX);
457 return valnames[val];
460 #endif /* WITH_PROFILE */