smbd: Split create_conn_struct into a fn that does not change the working dir
[Samba/bb.git] / source3 / profile / profile.c
blob0287860ff2994e4f97704d5a4db00262f614344b
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/shmem.h"
24 #include "system/filesys.h"
25 #include "messages.h"
26 #include "smbprofile.h"
28 #ifdef WITH_PROFILE
29 #define IPC_PERMS ((S_IRUSR | S_IWUSR) | S_IRGRP | S_IROTH)
30 #endif /* WITH_PROFILE */
32 #ifdef WITH_PROFILE
33 static int shm_id;
34 static bool read_only;
35 #endif
37 struct profile_header *profile_h;
38 struct profile_stats *profile_p;
40 bool do_profile_flag = False;
41 bool do_profile_times = False;
43 /****************************************************************************
44 Set a profiling level.
45 ****************************************************************************/
46 void set_profile_level(int level, struct server_id src)
48 #ifdef WITH_PROFILE
49 switch (level) {
50 case 0: /* turn off profiling */
51 do_profile_flag = False;
52 do_profile_times = False;
53 DEBUG(1,("INFO: Profiling turned OFF from pid %d\n",
54 (int)procid_to_pid(&src)));
55 break;
56 case 1: /* turn on counter profiling only */
57 do_profile_flag = True;
58 do_profile_times = False;
59 DEBUG(1,("INFO: Profiling counts turned ON from pid %d\n",
60 (int)procid_to_pid(&src)));
61 break;
62 case 2: /* turn on complete profiling */
63 do_profile_flag = True;
64 do_profile_times = True;
65 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n",
66 (int)procid_to_pid(&src)));
67 break;
68 case 3: /* reset profile values */
69 memset((char *)profile_p, 0, sizeof(*profile_p));
70 DEBUG(1,("INFO: Profiling values cleared from pid %d\n",
71 (int)procid_to_pid(&src)));
72 break;
74 #else /* WITH_PROFILE */
75 DEBUG(1,("INFO: Profiling support unavailable in this build.\n"));
76 #endif /* WITH_PROFILE */
79 #ifdef WITH_PROFILE
81 /****************************************************************************
82 receive a set profile level message
83 ****************************************************************************/
84 static void profile_message(struct messaging_context *msg_ctx,
85 void *private_data,
86 uint32_t msg_type,
87 struct server_id src,
88 DATA_BLOB *data)
90 int level;
92 if (data->length != sizeof(level)) {
93 DEBUG(0, ("got invalid profile message\n"));
94 return;
97 memcpy(&level, data->data, sizeof(level));
98 set_profile_level(level, src);
101 /****************************************************************************
102 receive a request profile level message
103 ****************************************************************************/
104 static void reqprofile_message(struct messaging_context *msg_ctx,
105 void *private_data,
106 uint32_t msg_type,
107 struct server_id src,
108 DATA_BLOB *data)
110 int level;
112 #ifdef WITH_PROFILE
113 level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
114 #else
115 level = 0;
116 #endif
117 DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",
118 (unsigned int)procid_to_pid(&src)));
119 messaging_send_buf(msg_ctx, src, MSG_PROFILELEVEL,
120 (uint8 *)&level, sizeof(level));
123 /*******************************************************************
124 open the profiling shared memory area
125 ******************************************************************/
126 bool profile_setup(struct messaging_context *msg_ctx, bool rdonly)
128 struct shmid_ds shm_ds;
130 read_only = rdonly;
132 again:
133 /* try to use an existing key */
134 shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
136 /* if that failed then create one. There is a race condition here
137 if we are running from inetd. Bad luck. */
138 if (shm_id == -1) {
139 if (read_only) return False;
140 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h),
141 IPC_CREAT | IPC_EXCL | IPC_PERMS);
144 if (shm_id == -1) {
145 DEBUG(0,("Can't create or use IPC area. Error was %s\n",
146 strerror(errno)));
147 return False;
151 profile_h = (struct profile_header *)shmat(shm_id, 0,
152 read_only?SHM_RDONLY:0);
153 if ((long)profile_h == -1) {
154 DEBUG(0,("Can't attach to IPC area. Error was %s\n",
155 strerror(errno)));
156 return False;
159 /* find out who created this memory area */
160 if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
161 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n",
162 strerror(errno)));
163 return False;
166 if (shm_ds.shm_perm.cuid != sec_initial_uid() ||
167 shm_ds.shm_perm.cgid != sec_initial_gid()) {
168 DEBUG(0,("ERROR: we did not create the shmem "
169 "(owned by another user, uid %u, gid %u)\n",
170 shm_ds.shm_perm.cuid,
171 shm_ds.shm_perm.cgid));
172 return False;
175 if (shm_ds.shm_segsz != sizeof(*profile_h)) {
176 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
177 (int)shm_ds.shm_segsz, (int)sizeof(*profile_h)));
178 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
179 goto again;
180 } else {
181 return False;
185 if (!read_only && (shm_ds.shm_nattch == 1)) {
186 memset((char *)profile_h, 0, sizeof(*profile_h));
187 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
188 profile_h->prof_shm_version = PROF_SHM_VERSION;
189 DEBUG(3,("Initialised profile area\n"));
192 profile_p = &profile_h->stats;
193 if (msg_ctx != NULL) {
194 messaging_register(msg_ctx, NULL, MSG_PROFILE,
195 profile_message);
196 messaging_register(msg_ctx, NULL, MSG_REQ_PROFILELEVEL,
197 reqprofile_message);
199 return True;
202 const char * profile_value_name(enum profile_stats_values val)
204 static const char * valnames[PR_VALUE_MAX + 1] =
206 "smbd_idle", /* PR_VALUE_SMBD_IDLE */
207 "syscall_opendir", /* PR_VALUE_SYSCALL_OPENDIR */
208 "syscall_readdir", /* PR_VALUE_SYSCALL_READDIR */
209 "syscall_seekdir", /* PR_VALUE_SYSCALL_SEEKDIR */
210 "syscall_telldir", /* PR_VALUE_SYSCALL_TELLDIR */
211 "syscall_rewinddir", /* PR_VALUE_SYSCALL_REWINDDIR */
212 "syscall_mkdir", /* PR_VALUE_SYSCALL_MKDIR */
213 "syscall_rmdir", /* PR_VALUE_SYSCALL_RMDIR */
214 "syscall_closedir", /* PR_VALUE_SYSCALL_CLOSEDIR */
215 "syscall_open", /* PR_VALUE_SYSCALL_OPEN */
216 "syscall_createfile", /* PR_VALUE_SYSCALL_CREATEFILE */
217 "syscall_close", /* PR_VALUE_SYSCALL_CLOSE */
218 "syscall_read", /* PR_VALUE_SYSCALL_READ */
219 "syscall_pread", /* PR_VALUE_SYSCALL_PREAD */
220 "syscall_write", /* PR_VALUE_SYSCALL_WRITE */
221 "syscall_pwrite", /* PR_VALUE_SYSCALL_PWRITE */
222 "syscall_lseek", /* PR_VALUE_SYSCALL_LSEEK */
223 "syscall_sendfile", /* PR_VALUE_SYSCALL_SENDFILE */
224 "syscall_recvfile", /* PR_VALUE_SYSCALL_RECVFILE */
225 "syscall_rename", /* PR_VALUE_SYSCALL_RENAME */
226 "syscall_rename_at", /* PR_VALUE_SYSCALL_RENAME_AT */
227 "syscall_fsync", /* PR_VALUE_SYSCALL_FSYNC */
228 "syscall_stat", /* PR_VALUE_SYSCALL_STAT */
229 "syscall_fstat", /* PR_VALUE_SYSCALL_FSTAT */
230 "syscall_lstat", /* PR_VALUE_SYSCALL_LSTAT */
231 "syscall_unlink", /* PR_VALUE_SYSCALL_UNLINK */
232 "syscall_chmod", /* PR_VALUE_SYSCALL_CHMOD */
233 "syscall_fchmod", /* PR_VALUE_SYSCALL_FCHMOD */
234 "syscall_chown", /* PR_VALUE_SYSCALL_CHOWN */
235 "syscall_fchown", /* PR_VALUE_SYSCALL_FCHOWN */
236 "syscall_chdir", /* PR_VALUE_SYSCALL_CHDIR */
237 "syscall_getwd", /* PR_VALUE_SYSCALL_GETWD */
238 "syscall_ntimes", /* PR_VALUE_SYSCALL_NTIMES */
239 "syscall_ftruncate", /* PR_VALUE_SYSCALL_FTRUNCATE */
240 "syscall_fallocate", /* PR_VALUE_SYSCALL_FALLOCATE */
241 "syscall_fcntl_lock", /* PR_VALUE_SYSCALL_FCNTL_LOCK */
242 "syscall_kernel_flock", /* PR_VALUE_SYSCALL_KERNEL_FLOCK */
243 "syscall_linux_setlease", /* PR_VALUE_SYSCALL_LINUX_SETLEASE */
244 "syscall_fcntl_getlock", /* PR_VALUE_SYSCALL_FCNTL_GETLOCK */
245 "syscall_readlink", /* PR_VALUE_SYSCALL_READLINK */
246 "syscall_symlink", /* PR_VALUE_SYSCALL_SYMLINK */
247 "syscall_link", /* PR_VALUE_SYSCALL_LINK */
248 "syscall_mknod", /* PR_VALUE_SYSCALL_MKNOD */
249 "syscall_realpath", /* PR_VALUE_SYSCALL_REALPATH */
250 "syscall_get_quota", /* PR_VALUE_SYSCALL_GET_QUOTA */
251 "syscall_set_quota", /* PR_VALUE_SYSCALL_SET_QUOTA */
252 "syscall_get_sd", /* PR_VALUE_SYSCALL_GET_SD */
253 "syscall_set_sd", /* PR_VALUE_SYSCALL_SET_SD */
254 "syscall_brl_lock", /* PR_VALUE_SYSCALL_BRL_LOCK */
255 "syscall_brl_unlock", /* PR_VALUE_SYSCALL_BRL_UNLOCK */
256 "syscall_brl_cancel", /* PR_VALUE_SYSCALL_BRL_CANCEL */
257 "SMBmkdir", /* PR_VALUE_SMBMKDIR */
258 "SMBrmdir", /* PR_VALUE_SMBRMDIR */
259 "SMBopen", /* PR_VALUE_SMBOPEN */
260 "SMBcreate", /* PR_VALUE_SMBCREATE */
261 "SMBclose", /* PR_VALUE_SMBCLOSE */
262 "SMBflush", /* PR_VALUE_SMBFLUSH */
263 "SMBunlink", /* PR_VALUE_SMBUNLINK */
264 "SMBmv", /* PR_VALUE_SMBMV */
265 "SMBgetatr", /* PR_VALUE_SMBGETATR */
266 "SMBsetatr", /* PR_VALUE_SMBSETATR */
267 "SMBread", /* PR_VALUE_SMBREAD */
268 "SMBwrite", /* PR_VALUE_SMBWRITE */
269 "SMBlock", /* PR_VALUE_SMBLOCK */
270 "SMBunlock", /* PR_VALUE_SMBUNLOCK */
271 "SMBctemp", /* PR_VALUE_SMBCTEMP */
272 "SMBmknew", /* PR_VALUE_SMBMKNEW */
273 "SMBcheckpath", /* PR_VALUE_SMBCHECKPATH */
274 "SMBexit", /* PR_VALUE_SMBEXIT */
275 "SMBlseek", /* PR_VALUE_SMBLSEEK */
276 "SMBlockread", /* PR_VALUE_SMBLOCKREAD */
277 "SMBwriteunlock", /* PR_VALUE_SMBWRITEUNLOCK */
278 "SMBreadbraw", /* PR_VALUE_SMBREADBRAW */
279 "SMBreadBmpx", /* PR_VALUE_SMBREADBMPX */
280 "SMBreadBs", /* PR_VALUE_SMBREADBS */
281 "SMBwritebraw", /* PR_VALUE_SMBWRITEBRAW */
282 "SMBwriteBmpx", /* PR_VALUE_SMBWRITEBMPX */
283 "SMBwriteBs", /* PR_VALUE_SMBWRITEBS */
284 "SMBwritec", /* PR_VALUE_SMBWRITEC */
285 "SMBsetattrE", /* PR_VALUE_SMBSETATTRE */
286 "SMBgetattrE", /* PR_VALUE_SMBGETATTRE */
287 "SMBlockingX", /* PR_VALUE_SMBLOCKINGX */
288 "SMBtrans", /* PR_VALUE_SMBTRANS */
289 "SMBtranss", /* PR_VALUE_SMBTRANSS */
290 "SMBioctl", /* PR_VALUE_SMBIOCTL */
291 "SMBioctls", /* PR_VALUE_SMBIOCTLS */
292 "SMBcopy", /* PR_VALUE_SMBCOPY */
293 "SMBmove", /* PR_VALUE_SMBMOVE */
294 "SMBecho", /* PR_VALUE_SMBECHO */
295 "SMBwriteclose", /* PR_VALUE_SMBWRITECLOSE */
296 "SMBopenX", /* PR_VALUE_SMBOPENX */
297 "SMBreadX", /* PR_VALUE_SMBREADX */
298 "SMBwriteX", /* PR_VALUE_SMBWRITEX */
299 "SMBtrans2", /* PR_VALUE_SMBTRANS2 */
300 "SMBtranss2", /* PR_VALUE_SMBTRANSS2 */
301 "SMBfindclose", /* PR_VALUE_SMBFINDCLOSE */
302 "SMBfindnclose", /* PR_VALUE_SMBFINDNCLOSE */
303 "SMBtcon", /* PR_VALUE_SMBTCON */
304 "SMBtdis", /* PR_VALUE_SMBTDIS */
305 "SMBnegprot", /* PR_VALUE_SMBNEGPROT */
306 "SMBsesssetupX", /* PR_VALUE_SMBSESSSETUPX */
307 "SMBulogoffX", /* PR_VALUE_SMBULOGOFFX */
308 "SMBtconX", /* PR_VALUE_SMBTCONX */
309 "SMBdskattr", /* PR_VALUE_SMBDSKATTR */
310 "SMBsearch", /* PR_VALUE_SMBSEARCH */
311 "SMBffirst", /* PR_VALUE_SMBFFIRST */
312 "SMBfunique", /* PR_VALUE_SMBFUNIQUE */
313 "SMBfclose", /* PR_VALUE_SMBFCLOSE */
314 "SMBnttrans", /* PR_VALUE_SMBNTTRANS */
315 "SMBnttranss", /* PR_VALUE_SMBNTTRANSS */
316 "SMBntcreateX", /* PR_VALUE_SMBNTCREATEX */
317 "SMBntcancel", /* PR_VALUE_SMBNTCANCEL */
318 "SMBntrename", /* PR_VALUE_SMBNTRENAME */
319 "SMBsplopen", /* PR_VALUE_SMBSPLOPEN */
320 "SMBsplwr", /* PR_VALUE_SMBSPLWR */
321 "SMBsplclose", /* PR_VALUE_SMBSPLCLOSE */
322 "SMBsplretq", /* PR_VALUE_SMBSPLRETQ */
323 "SMBsends", /* PR_VALUE_SMBSENDS */
324 "SMBsendb", /* PR_VALUE_SMBSENDB */
325 "SMBfwdname", /* PR_VALUE_SMBFWDNAME */
326 "SMBcancelf", /* PR_VALUE_SMBCANCELF */
327 "SMBgetmac", /* PR_VALUE_SMBGETMAC */
328 "SMBsendstrt", /* PR_VALUE_SMBSENDSTRT */
329 "SMBsendend", /* PR_VALUE_SMBSENDEND */
330 "SMBsendtxt", /* PR_VALUE_SMBSENDTXT */
331 "SMBinvalid", /* PR_VALUE_SMBINVALID */
332 "pathworks_setdir", /* PR_VALUE_PATHWORKS_SETDIR */
333 "Trans2_open", /* PR_VALUE_TRANS2_OPEN */
334 "Trans2_findfirst", /* PR_VALUE_TRANS2_FINDFIRST */
335 "Trans2_findnext", /* PR_VALUE_TRANS2_FINDNEXT */
336 "Trans2_qfsinfo", /* PR_VALUE_TRANS2_QFSINFO */
337 "Trans2_setfsinfo", /* PR_VALUE_TRANS2_SETFSINFO */
338 "Trans2_qpathinfo", /* PR_VALUE_TRANS2_QPATHINFO */
339 "Trans2_setpathinfo", /* PR_VALUE_TRANS2_SETPATHINFO */
340 "Trans2_qfileinfo", /* PR_VALUE_TRANS2_QFILEINFO */
341 "Trans2_setfileinfo", /* PR_VALUE_TRANS2_SETFILEINFO */
342 "Trans2_fsctl", /* PR_VALUE_TRANS2_FSCTL */
343 "Trans2_ioctl", /* PR_VALUE_TRANS2_IOCTL */
344 "Trans2_findnotifyfirst", /* PR_VALUE_TRANS2_FINDNOTIFYFIRST */
345 "Trans2_findnotifynext", /* PR_VALUE_TRANS2_FINDNOTIFYNEXT */
346 "Trans2_mkdir", /* PR_VALUE_TRANS2_MKDIR */
347 "Trans2_session_setup", /* PR_VALUE_TRANS2_SESSION_SETUP */
348 "Trans2_get_dfs_referral", /* PR_VALUE_TRANS2_GET_DFS_REFERRAL */
349 "Trans2_report_dfs_inconsistancy", /* PR_VALUE_TRANS2_REPORT_DFS_INCONSISTANCY */
350 "NT_transact_create", /* PR_VALUE_NT_TRANSACT_CREATE */
351 "NT_transact_ioctl", /* PR_VALUE_NT_TRANSACT_IOCTL */
352 "NT_transact_set_security_desc", /* PR_VALUE_NT_TRANSACT_SET_SECURITY_DESC */
353 "NT_transact_notify_change",/* PR_VALUE_NT_TRANSACT_NOTIFY_CHANGE */
354 "NT_transact_rename", /* PR_VALUE_NT_TRANSACT_RENAME */
355 "NT_transact_query_security_desc", /* PR_VALUE_NT_TRANSACT_QUERY_SECURITY_DESC */
356 "NT_transact_get_user_quota",/* PR_VALUE_NT_TRANSACT_GET_USER_QUOTA */
357 "NT_transact_set_user_quota",/* PR_VALUE_NT_TRANSACT_SET_USER_QUOTA */
358 "get_nt_acl", /* PR_VALUE_GET_NT_ACL */
359 "fget_nt_acl", /* PR_VALUE_FGET_NT_ACL */
360 "fset_nt_acl", /* PR_VALUE_FSET_NT_ACL */
361 "chmod_acl", /* PR_VALUE_CHMOD_ACL */
362 "fchmod_acl", /* PR_VALUE_FCHMOD_ACL */
363 "name_release", /* PR_VALUE_NAME_RELEASE */
364 "name_refresh", /* PR_VALUE_NAME_REFRESH */
365 "name_registration", /* PR_VALUE_NAME_REGISTRATION */
366 "node_status", /* PR_VALUE_NODE_STATUS */
367 "name_query", /* PR_VALUE_NAME_QUERY */
368 "host_announce", /* PR_VALUE_HOST_ANNOUNCE */
369 "workgroup_announce", /* PR_VALUE_WORKGROUP_ANNOUNCE */
370 "local_master_announce", /* PR_VALUE_LOCAL_MASTER_ANNOUNCE */
371 "master_browser_announce", /* PR_VALUE_MASTER_BROWSER_ANNOUNCE */
372 "lm_host_announce", /* PR_VALUE_LM_HOST_ANNOUNCE */
373 "get_backup_list", /* PR_VALUE_GET_BACKUP_LIST */
374 "reset_browser", /* PR_VALUE_RESET_BROWSER */
375 "announce_request", /* PR_VALUE_ANNOUNCE_REQUEST */
376 "lm_announce_request", /* PR_VALUE_LM_ANNOUNCE_REQUEST */
377 "domain_logon", /* PR_VALUE_DOMAIN_LOGON */
378 "sync_browse_lists", /* PR_VALUE_SYNC_BROWSE_LISTS */
379 "run_elections", /* PR_VALUE_RUN_ELECTIONS */
380 "election", /* PR_VALUE_ELECTION */
381 "smb2_negprot", /* PR_VALUE_SMB2_NEGPROT */
382 "smb2_sesssetup", /* PR_VALUE_SMB2_SESSETUP */
383 "smb2_logoff", /* PR_VALUE_SMB2_LOGOFF */
384 "smb2_tcon", /* PR_VALUE_SMB2_TCON */
385 "smb2_tdis", /* PR_VALUE_SMB2_TDIS */
386 "smb2_create", /* PR_VALUE_SMB2_CREATE */
387 "smb2_close", /* PR_VALUE_SMB2_CLOSE */
388 "smb2_flush", /* PR_VALUE_SMB2_FLUSH */
389 "smb2_read", /* PR_VALUE_SMB2_READ */
390 "smb2_write", /* PR_VALUE_SMB2_WRITE */
391 "smb2_lock", /* PR_VALUE_SMB2_LOCK */
392 "smb2_ioctl", /* PR_VALUE_SMB2_IOCTL */
393 "smb2_cancel", /* PR_VALUE_SMB2_CANCEL */
394 "smb2_keepalive", /* PR_VALUE_SMB2_KEEPALIVE */
395 "smb2_find", /* PR_VALUE_SMB2_FIND */
396 "smb2_notify", /* PR_VALUE_SMB2_NOTIFY */
397 "smb2_getinfo", /* PR_VALUE_SMB2_GETINFO */
398 "smb2_setinfo" /* PR_VALUE_SMB2_SETINFO */
399 "smb2_break", /* PR_VALUE_SMB2_BREAK */
400 "" /* PR_VALUE_MAX */
403 SMB_ASSERT(val >= 0);
404 SMB_ASSERT(val < PR_VALUE_MAX);
405 return valnames[val];
408 #endif /* WITH_PROFILE */