Do proper error handling if the socket is closed
[Samba/gebeck_regimport.git] / source3 / profile / profile.c
blobbdbd8057183b835abb14c2680bc12f83672f0e12
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"
24 #ifdef WITH_PROFILE
25 #define IPC_PERMS ((S_IRUSR | S_IWUSR) | S_IRGRP | S_IROTH)
26 #endif /* WITH_PROFILE */
28 #ifdef WITH_PROFILE
29 static int shm_id;
30 static bool read_only;
31 #if defined(HAVE_CLOCK_GETTIME)
32 clockid_t __profile_clock;
33 bool have_profiling_clock = False;
34 #endif
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 */
64 #if defined(HAVE_CLOCK_GETTIME)
65 if (!have_profiling_clock) {
66 do_profile_flag = True;
67 do_profile_times = False;
68 DEBUG(1,("INFO: Profiling counts turned ON from "
69 "pid %d\n", (int)procid_to_pid(&src)));
70 DEBUGADD(1,("INFO: Profiling times disabled "
71 "due to lack of a suitable clock\n"));
72 break;
74 #endif
76 do_profile_flag = True;
77 do_profile_times = True;
78 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n",
79 (int)procid_to_pid(&src)));
80 break;
81 case 3: /* reset profile values */
82 memset((char *)profile_p, 0, sizeof(*profile_p));
83 DEBUG(1,("INFO: Profiling values cleared from pid %d\n",
84 (int)procid_to_pid(&src)));
85 break;
87 #else /* WITH_PROFILE */
88 DEBUG(1,("INFO: Profiling support unavailable in this build.\n"));
89 #endif /* WITH_PROFILE */
92 #ifdef WITH_PROFILE
94 /****************************************************************************
95 receive a set profile level message
96 ****************************************************************************/
97 static void profile_message(struct messaging_context *msg_ctx,
98 void *private_data,
99 uint32_t msg_type,
100 struct server_id src,
101 DATA_BLOB *data)
103 int level;
105 if (data->length != sizeof(level)) {
106 DEBUG(0, ("got invalid profile message\n"));
107 return;
110 memcpy(&level, data->data, sizeof(level));
111 set_profile_level(level, src);
114 /****************************************************************************
115 receive a request profile level message
116 ****************************************************************************/
117 static void reqprofile_message(struct messaging_context *msg_ctx,
118 void *private_data,
119 uint32_t msg_type,
120 struct server_id src,
121 DATA_BLOB *data)
123 int level;
125 #ifdef WITH_PROFILE
126 level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
127 #else
128 level = 0;
129 #endif
130 DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",
131 (unsigned int)procid_to_pid(&src)));
132 messaging_send_buf(msg_ctx, src, MSG_PROFILELEVEL,
133 (uint8 *)&level, sizeof(level));
136 /*******************************************************************
137 open the profiling shared memory area
138 ******************************************************************/
140 #ifdef HAVE_CLOCK_GETTIME
142 /* Find a clock. Just because the definition for a particular clock ID is
143 * present doesn't mean the system actually supports it.
145 static void init_clock_gettime(void)
147 struct timespec ts;
149 have_profiling_clock = False;
151 #ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID
152 /* CLOCK_PROCESS_CPUTIME_ID is sufficiently fast that the
153 * always profiling times is plausible. Unfortunately on Linux
154 * it is only accurate if we can guarantee we will not be scheduled
155 * scheduled onto a different CPU between samples. Until there is
156 * some way to set processor affinity, we can only use this on
157 * uniprocessors.
159 if (!this_is_smp()) {
160 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
161 DEBUG(10, ("Using CLOCK_PROCESS_CPUTIME_ID "
162 "for profile_clock\n"));
163 __profile_clock = CLOCK_PROCESS_CPUTIME_ID;
164 have_profiling_clock = True;
167 #endif
169 #ifdef HAVE_CLOCK_MONOTONIC
170 if (!have_profiling_clock &&
171 clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
172 DEBUG(10, ("Using CLOCK_MONOTONIC for profile_clock\n"));
173 __profile_clock = CLOCK_MONOTONIC;
174 have_profiling_clock = True;
176 #endif
178 #ifdef HAVE_CLOCK_REALTIME
179 /* POSIX says that CLOCK_REALTIME should be defined everywhere
180 * where we have clock_gettime...
182 if (!have_profiling_clock &&
183 clock_gettime(CLOCK_REALTIME, &ts) == 0) {
184 __profile_clock = CLOCK_REALTIME;
185 have_profiling_clock = True;
187 SMB_WARN(__profile_clock != CLOCK_REALTIME,
188 ("forced to use a slow profiling clock"));
191 #endif
193 SMB_WARN(have_profiling_clock == True,
194 ("could not find a working clock for profiling"));
195 return;
197 #endif
199 bool profile_setup(struct messaging_context *msg_ctx, bool rdonly)
201 struct shmid_ds shm_ds;
203 read_only = rdonly;
205 #ifdef HAVE_CLOCK_GETTIME
206 init_clock_gettime();
207 #endif
209 again:
210 /* try to use an existing key */
211 shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
213 /* if that failed then create one. There is a race condition here
214 if we are running from inetd. Bad luck. */
215 if (shm_id == -1) {
216 if (read_only) return False;
217 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h),
218 IPC_CREAT | IPC_EXCL | IPC_PERMS);
221 if (shm_id == -1) {
222 DEBUG(0,("Can't create or use IPC area. Error was %s\n",
223 strerror(errno)));
224 return False;
228 profile_h = (struct profile_header *)shmat(shm_id, 0,
229 read_only?SHM_RDONLY:0);
230 if ((long)profile_p == -1) {
231 DEBUG(0,("Can't attach to IPC area. Error was %s\n",
232 strerror(errno)));
233 return False;
236 /* find out who created this memory area */
237 if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
238 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n",
239 strerror(errno)));
240 return False;
243 if (shm_ds.shm_perm.cuid != sec_initial_uid() ||
244 shm_ds.shm_perm.cgid != sec_initial_gid()) {
245 DEBUG(0,("ERROR: we did not create the shmem "
246 "(owned by another user, uid %u, gid %u)\n",
247 shm_ds.shm_perm.cuid,
248 shm_ds.shm_perm.cgid));
249 return False;
252 if (shm_ds.shm_segsz != sizeof(*profile_h)) {
253 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
254 (int)shm_ds.shm_segsz, (int)sizeof(*profile_h)));
255 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
256 goto again;
257 } else {
258 return False;
262 if (!read_only && (shm_ds.shm_nattch == 1)) {
263 memset((char *)profile_h, 0, sizeof(*profile_h));
264 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
265 profile_h->prof_shm_version = PROF_SHM_VERSION;
266 DEBUG(3,("Initialised profile area\n"));
269 profile_p = &profile_h->stats;
270 if (msg_ctx != NULL) {
271 messaging_register(msg_ctx, NULL, MSG_PROFILE,
272 profile_message);
273 messaging_register(msg_ctx, NULL, MSG_REQ_PROFILELEVEL,
274 reqprofile_message);
276 return True;
279 const char * profile_value_name(enum profile_stats_values val)
281 static const char * valnames[PR_VALUE_MAX + 1] =
283 "smbd_idle", /* PR_VALUE_SMBD_IDLE */
284 "syscall_opendir", /* PR_VALUE_SYSCALL_OPENDIR */
285 "syscall_readdir", /* PR_VALUE_SYSCALL_READDIR */
286 "syscall_seekdir", /* PR_VALUE_SYSCALL_SEEKDIR */
287 "syscall_telldir", /* PR_VALUE_SYSCALL_TELLDIR */
288 "syscall_rewinddir", /* PR_VALUE_SYSCALL_REWINDDIR */
289 "syscall_mkdir", /* PR_VALUE_SYSCALL_MKDIR */
290 "syscall_rmdir", /* PR_VALUE_SYSCALL_RMDIR */
291 "syscall_closedir", /* PR_VALUE_SYSCALL_CLOSEDIR */
292 "syscall_open", /* PR_VALUE_SYSCALL_OPEN */
293 "syscall_close", /* PR_VALUE_SYSCALL_CLOSE */
294 "syscall_read", /* PR_VALUE_SYSCALL_READ */
295 "syscall_pread", /* PR_VALUE_SYSCALL_PREAD */
296 "syscall_write", /* PR_VALUE_SYSCALL_WRITE */
297 "syscall_pwrite", /* PR_VALUE_SYSCALL_PWRITE */
298 "syscall_lseek", /* PR_VALUE_SYSCALL_LSEEK */
299 "syscall_sendfile", /* PR_VALUE_SYSCALL_SENDFILE */
300 "syscall_recvfile", /* PR_VALUE_SYSCALL_RECVFILE */
301 "syscall_rename", /* PR_VALUE_SYSCALL_RENAME */
302 "syscall_fsync", /* PR_VALUE_SYSCALL_FSYNC */
303 "syscall_stat", /* PR_VALUE_SYSCALL_STAT */
304 "syscall_fstat", /* PR_VALUE_SYSCALL_FSTAT */
305 "syscall_lstat", /* PR_VALUE_SYSCALL_LSTAT */
306 "syscall_unlink", /* PR_VALUE_SYSCALL_UNLINK */
307 "syscall_chmod", /* PR_VALUE_SYSCALL_CHMOD */
308 "syscall_fchmod", /* PR_VALUE_SYSCALL_FCHMOD */
309 "syscall_chown", /* PR_VALUE_SYSCALL_CHOWN */
310 "syscall_fchown", /* PR_VALUE_SYSCALL_FCHOWN */
311 "syscall_chdir", /* PR_VALUE_SYSCALL_CHDIR */
312 "syscall_getwd", /* PR_VALUE_SYSCALL_GETWD */
313 "syscall_ntimes", /* PR_VALUE_SYSCALL_NTIMES */
314 "syscall_ftruncate", /* PR_VALUE_SYSCALL_FTRUNCATE */
315 "syscall_fcntl_lock", /* PR_VALUE_SYSCALL_FCNTL_LOCK */
316 "syscall_kernel_flock", /* PR_VALUE_SYSCALL_KERNEL_FLOCK */
317 "syscall_linux_setlease", /* PR_VALUE_SYSCALL_LINUX_SETLEASE */
318 "syscall_fcntl_getlock", /* PR_VALUE_SYSCALL_FCNTL_GETLOCK */
319 "syscall_readlink", /* PR_VALUE_SYSCALL_READLINK */
320 "syscall_symlink", /* PR_VALUE_SYSCALL_SYMLINK */
321 "syscall_link", /* PR_VALUE_SYSCALL_LINK */
322 "syscall_mknod", /* PR_VALUE_SYSCALL_MKNOD */
323 "syscall_realpath", /* PR_VALUE_SYSCALL_REALPATH */
324 "syscall_get_quota", /* PR_VALUE_SYSCALL_GET_QUOTA */
325 "syscall_set_quota", /* PR_VALUE_SYSCALL_SET_QUOTA */
326 "SMBmkdir", /* PR_VALUE_SMBMKDIR */
327 "SMBrmdir", /* PR_VALUE_SMBRMDIR */
328 "SMBopen", /* PR_VALUE_SMBOPEN */
329 "SMBcreate", /* PR_VALUE_SMBCREATE */
330 "SMBclose", /* PR_VALUE_SMBCLOSE */
331 "SMBflush", /* PR_VALUE_SMBFLUSH */
332 "SMBunlink", /* PR_VALUE_SMBUNLINK */
333 "SMBmv", /* PR_VALUE_SMBMV */
334 "SMBgetatr", /* PR_VALUE_SMBGETATR */
335 "SMBsetatr", /* PR_VALUE_SMBSETATR */
336 "SMBread", /* PR_VALUE_SMBREAD */
337 "SMBwrite", /* PR_VALUE_SMBWRITE */
338 "SMBlock", /* PR_VALUE_SMBLOCK */
339 "SMBunlock", /* PR_VALUE_SMBUNLOCK */
340 "SMBctemp", /* PR_VALUE_SMBCTEMP */
341 "SMBmknew", /* PR_VALUE_SMBMKNEW */
342 "SMBcheckpath", /* PR_VALUE_SMBCHECKPATH */
343 "SMBexit", /* PR_VALUE_SMBEXIT */
344 "SMBlseek", /* PR_VALUE_SMBLSEEK */
345 "SMBlockread", /* PR_VALUE_SMBLOCKREAD */
346 "SMBwriteunlock", /* PR_VALUE_SMBWRITEUNLOCK */
347 "SMBreadbraw", /* PR_VALUE_SMBREADBRAW */
348 "SMBreadBmpx", /* PR_VALUE_SMBREADBMPX */
349 "SMBreadBs", /* PR_VALUE_SMBREADBS */
350 "SMBwritebraw", /* PR_VALUE_SMBWRITEBRAW */
351 "SMBwriteBmpx", /* PR_VALUE_SMBWRITEBMPX */
352 "SMBwriteBs", /* PR_VALUE_SMBWRITEBS */
353 "SMBwritec", /* PR_VALUE_SMBWRITEC */
354 "SMBsetattrE", /* PR_VALUE_SMBSETATTRE */
355 "SMBgetattrE", /* PR_VALUE_SMBGETATTRE */
356 "SMBlockingX", /* PR_VALUE_SMBLOCKINGX */
357 "SMBtrans", /* PR_VALUE_SMBTRANS */
358 "SMBtranss", /* PR_VALUE_SMBTRANSS */
359 "SMBioctl", /* PR_VALUE_SMBIOCTL */
360 "SMBioctls", /* PR_VALUE_SMBIOCTLS */
361 "SMBcopy", /* PR_VALUE_SMBCOPY */
362 "SMBmove", /* PR_VALUE_SMBMOVE */
363 "SMBecho", /* PR_VALUE_SMBECHO */
364 "SMBwriteclose", /* PR_VALUE_SMBWRITECLOSE */
365 "SMBopenX", /* PR_VALUE_SMBOPENX */
366 "SMBreadX", /* PR_VALUE_SMBREADX */
367 "SMBwriteX", /* PR_VALUE_SMBWRITEX */
368 "SMBtrans2", /* PR_VALUE_SMBTRANS2 */
369 "SMBtranss2", /* PR_VALUE_SMBTRANSS2 */
370 "SMBfindclose", /* PR_VALUE_SMBFINDCLOSE */
371 "SMBfindnclose", /* PR_VALUE_SMBFINDNCLOSE */
372 "SMBtcon", /* PR_VALUE_SMBTCON */
373 "SMBtdis", /* PR_VALUE_SMBTDIS */
374 "SMBnegprot", /* PR_VALUE_SMBNEGPROT */
375 "SMBsesssetupX", /* PR_VALUE_SMBSESSSETUPX */
376 "SMBulogoffX", /* PR_VALUE_SMBULOGOFFX */
377 "SMBtconX", /* PR_VALUE_SMBTCONX */
378 "SMBdskattr", /* PR_VALUE_SMBDSKATTR */
379 "SMBsearch", /* PR_VALUE_SMBSEARCH */
380 "SMBffirst", /* PR_VALUE_SMBFFIRST */
381 "SMBfunique", /* PR_VALUE_SMBFUNIQUE */
382 "SMBfclose", /* PR_VALUE_SMBFCLOSE */
383 "SMBnttrans", /* PR_VALUE_SMBNTTRANS */
384 "SMBnttranss", /* PR_VALUE_SMBNTTRANSS */
385 "SMBntcreateX", /* PR_VALUE_SMBNTCREATEX */
386 "SMBntcancel", /* PR_VALUE_SMBNTCANCEL */
387 "SMBntrename", /* PR_VALUE_SMBNTRENAME */
388 "SMBsplopen", /* PR_VALUE_SMBSPLOPEN */
389 "SMBsplwr", /* PR_VALUE_SMBSPLWR */
390 "SMBsplclose", /* PR_VALUE_SMBSPLCLOSE */
391 "SMBsplretq", /* PR_VALUE_SMBSPLRETQ */
392 "SMBsends", /* PR_VALUE_SMBSENDS */
393 "SMBsendb", /* PR_VALUE_SMBSENDB */
394 "SMBfwdname", /* PR_VALUE_SMBFWDNAME */
395 "SMBcancelf", /* PR_VALUE_SMBCANCELF */
396 "SMBgetmac", /* PR_VALUE_SMBGETMAC */
397 "SMBsendstrt", /* PR_VALUE_SMBSENDSTRT */
398 "SMBsendend", /* PR_VALUE_SMBSENDEND */
399 "SMBsendtxt", /* PR_VALUE_SMBSENDTXT */
400 "SMBinvalid", /* PR_VALUE_SMBINVALID */
401 "pathworks_setdir", /* PR_VALUE_PATHWORKS_SETDIR */
402 "Trans2_open", /* PR_VALUE_TRANS2_OPEN */
403 "Trans2_findfirst", /* PR_VALUE_TRANS2_FINDFIRST */
404 "Trans2_findnext", /* PR_VALUE_TRANS2_FINDNEXT */
405 "Trans2_qfsinfo", /* PR_VALUE_TRANS2_QFSINFO */
406 "Trans2_setfsinfo", /* PR_VALUE_TRANS2_SETFSINFO */
407 "Trans2_qpathinfo", /* PR_VALUE_TRANS2_QPATHINFO */
408 "Trans2_setpathinfo", /* PR_VALUE_TRANS2_SETPATHINFO */
409 "Trans2_qfileinfo", /* PR_VALUE_TRANS2_QFILEINFO */
410 "Trans2_setfileinfo", /* PR_VALUE_TRANS2_SETFILEINFO */
411 "Trans2_fsctl", /* PR_VALUE_TRANS2_FSCTL */
412 "Trans2_ioctl", /* PR_VALUE_TRANS2_IOCTL */
413 "Trans2_findnotifyfirst", /* PR_VALUE_TRANS2_FINDNOTIFYFIRST */
414 "Trans2_findnotifynext", /* PR_VALUE_TRANS2_FINDNOTIFYNEXT */
415 "Trans2_mkdir", /* PR_VALUE_TRANS2_MKDIR */
416 "Trans2_session_setup", /* PR_VALUE_TRANS2_SESSION_SETUP */
417 "Trans2_get_dfs_referral", /* PR_VALUE_TRANS2_GET_DFS_REFERRAL */
418 "Trans2_report_dfs_inconsistancy", /* PR_VALUE_TRANS2_REPORT_DFS_INCONSISTANCY */
419 "NT_transact_create", /* PR_VALUE_NT_TRANSACT_CREATE */
420 "NT_transact_ioctl", /* PR_VALUE_NT_TRANSACT_IOCTL */
421 "NT_transact_set_security_desc", /* PR_VALUE_NT_TRANSACT_SET_SECURITY_DESC */
422 "NT_transact_notify_change",/* PR_VALUE_NT_TRANSACT_NOTIFY_CHANGE */
423 "NT_transact_rename", /* PR_VALUE_NT_TRANSACT_RENAME */
424 "NT_transact_query_security_desc", /* PR_VALUE_NT_TRANSACT_QUERY_SECURITY_DESC */
425 "NT_transact_get_user_quota",/* PR_VALUE_NT_TRANSACT_GET_USER_QUOTA */
426 "NT_transact_set_user_quota",/* PR_VALUE_NT_TRANSACT_SET_USER_QUOTA */
427 "get_nt_acl", /* PR_VALUE_GET_NT_ACL */
428 "fget_nt_acl", /* PR_VALUE_FGET_NT_ACL */
429 "fset_nt_acl", /* PR_VALUE_FSET_NT_ACL */
430 "chmod_acl", /* PR_VALUE_CHMOD_ACL */
431 "fchmod_acl", /* PR_VALUE_FCHMOD_ACL */
432 "name_release", /* PR_VALUE_NAME_RELEASE */
433 "name_refresh", /* PR_VALUE_NAME_REFRESH */
434 "name_registration", /* PR_VALUE_NAME_REGISTRATION */
435 "node_status", /* PR_VALUE_NODE_STATUS */
436 "name_query", /* PR_VALUE_NAME_QUERY */
437 "host_announce", /* PR_VALUE_HOST_ANNOUNCE */
438 "workgroup_announce", /* PR_VALUE_WORKGROUP_ANNOUNCE */
439 "local_master_announce", /* PR_VALUE_LOCAL_MASTER_ANNOUNCE */
440 "master_browser_announce", /* PR_VALUE_MASTER_BROWSER_ANNOUNCE */
441 "lm_host_announce", /* PR_VALUE_LM_HOST_ANNOUNCE */
442 "get_backup_list", /* PR_VALUE_GET_BACKUP_LIST */
443 "reset_browser", /* PR_VALUE_RESET_BROWSER */
444 "announce_request", /* PR_VALUE_ANNOUNCE_REQUEST */
445 "lm_announce_request", /* PR_VALUE_LM_ANNOUNCE_REQUEST */
446 "domain_logon", /* PR_VALUE_DOMAIN_LOGON */
447 "sync_browse_lists", /* PR_VALUE_SYNC_BROWSE_LISTS */
448 "run_elections", /* PR_VALUE_RUN_ELECTIONS */
449 "election", /* PR_VALUE_ELECTION */
450 "" /* PR_VALUE_MAX */
453 SMB_ASSERT(val >= 0);
454 SMB_ASSERT(val < PR_VALUE_MAX);
455 return valnames[val];
458 #endif /* WITH_PROFILE */