mount.cifs: check access of credential files before opening
[Samba.git] / source / profile / profile.c
blobb7a2ea23beb60c3f90fc66695f40064ef101f3af
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 #ifdef WITH_PROFILE
26 #define IPC_PERMS ((S_IRUSR | S_IWUSR) | S_IRGRP | S_IROTH)
27 #endif /* WITH_PROFILE */
29 #ifdef WITH_PROFILE
30 static int shm_id;
31 static BOOL read_only;
32 #if defined(HAVE_CLOCK_GETTIME)
33 clockid_t __profile_clock;
34 BOOL have_profiling_clock = False;
35 #endif
36 #endif
38 struct profile_header *profile_h;
39 struct profile_stats *profile_p;
41 BOOL do_profile_flag = False;
42 BOOL do_profile_times = False;
44 /****************************************************************************
45 Set a profiling level.
46 ****************************************************************************/
47 void set_profile_level(int level, struct process_id src)
49 #ifdef WITH_PROFILE
50 switch (level) {
51 case 0: /* turn off profiling */
52 do_profile_flag = False;
53 do_profile_times = False;
54 DEBUG(1,("INFO: Profiling turned OFF from pid %d\n",
55 (int)procid_to_pid(&src)));
56 break;
57 case 1: /* turn on counter profiling only */
58 do_profile_flag = True;
59 do_profile_times = False;
60 DEBUG(1,("INFO: Profiling counts turned ON from pid %d\n",
61 (int)procid_to_pid(&src)));
62 break;
63 case 2: /* turn on complete profiling */
65 #if defined(HAVE_CLOCK_GETTIME)
66 if (!have_profiling_clock) {
67 do_profile_flag = True;
68 do_profile_times = False;
69 DEBUG(1,("INFO: Profiling counts turned ON from "
70 "pid %d\n", (int)procid_to_pid(&src)));
71 DEBUGADD(1,("INFO: Profiling times disabled "
72 "due to lack of a suitable clock\n"));
73 break;
75 #endif
77 do_profile_flag = True;
78 do_profile_times = True;
79 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n",
80 (int)procid_to_pid(&src)));
81 break;
82 case 3: /* reset profile values */
83 memset((char *)profile_p, 0, sizeof(*profile_p));
84 DEBUG(1,("INFO: Profiling values cleared from pid %d\n",
85 (int)procid_to_pid(&src)));
86 break;
88 #else /* WITH_PROFILE */
89 DEBUG(1,("INFO: Profiling support unavailable in this build.\n"));
90 #endif /* WITH_PROFILE */
93 /****************************************************************************
94 receive a set profile level message
95 ****************************************************************************/
96 void profile_message(int msg_type, struct process_id src, void *buf, size_t len, void *private_data)
98 int level;
100 memcpy(&level, buf, sizeof(int));
101 set_profile_level(level, src);
104 /****************************************************************************
105 receive a request profile level message
106 ****************************************************************************/
107 void reqprofile_message(int msg_type, struct process_id src,
108 void *buf, size_t len, void *private_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 message_send_pid(src, MSG_PROFILELEVEL, &level, sizeof(int), True);
122 /*******************************************************************
123 open the profiling shared memory area
124 ******************************************************************/
125 #ifdef WITH_PROFILE
127 #ifdef HAVE_CLOCK_GETTIME
129 /* Find a clock. Just because the definition for a particular clock ID is
130 * present doesn't mean the system actually supports it.
132 static void init_clock_gettime(void)
134 struct timespec ts;
136 have_profiling_clock = False;
138 #ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID
139 /* CLOCK_PROCESS_CPUTIME_ID is sufficiently fast that the
140 * always profiling times is plausible. Unfortunately on Linux
141 * it is only accurate if we can guarantee we will not be scheduled
142 * scheduled onto a different CPU between samples. Until there is
143 * some way to set processor affinity, we can only use this on
144 * uniprocessors.
146 if (!this_is_smp()) {
147 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
148 DEBUG(10, ("Using CLOCK_PROCESS_CPUTIME_ID "
149 "for profile_clock\n"));
150 __profile_clock = CLOCK_PROCESS_CPUTIME_ID;
151 have_profiling_clock = True;
154 #endif
156 #ifdef HAVE_CLOCK_MONOTONIC
157 if (!have_profiling_clock &&
158 clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
159 DEBUG(10, ("Using CLOCK_MONOTONIC for profile_clock\n"));
160 __profile_clock = CLOCK_MONOTONIC;
161 have_profiling_clock = True;
163 #endif
165 #ifdef HAVE_CLOCK_REALTIME
166 /* POSIX says that CLOCK_REALTIME should be defined everywhere
167 * where we have clock_gettime...
169 if (!have_profiling_clock &&
170 clock_gettime(CLOCK_REALTIME, &ts) == 0) {
171 __profile_clock = CLOCK_REALTIME;
172 have_profiling_clock = True;
174 SMB_WARN(__profile_clock != CLOCK_REALTIME,
175 ("forced to use a slow profiling clock"));
178 #endif
180 SMB_WARN(have_profiling_clock == True,
181 ("could not find a working clock for profiling"));
182 return;
184 #endif
186 BOOL profile_setup(BOOL rdonly)
188 struct shmid_ds shm_ds;
190 read_only = rdonly;
192 #ifdef HAVE_CLOCK_GETTIME
193 init_clock_gettime();
194 #endif
196 again:
197 /* try to use an existing key */
198 shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
200 /* if that failed then create one. There is a race condition here
201 if we are running from inetd. Bad luck. */
202 if (shm_id == -1) {
203 if (read_only) return False;
204 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h),
205 IPC_CREAT | IPC_EXCL | IPC_PERMS);
208 if (shm_id == -1) {
209 DEBUG(0,("Can't create or use IPC area. Error was %s\n",
210 strerror(errno)));
211 return False;
215 profile_h = (struct profile_header *)shmat(shm_id, 0,
216 read_only?SHM_RDONLY:0);
217 if ((long)profile_p == -1) {
218 DEBUG(0,("Can't attach to IPC area. Error was %s\n",
219 strerror(errno)));
220 return False;
223 /* find out who created this memory area */
224 if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
225 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n",
226 strerror(errno)));
227 return False;
230 if (shm_ds.shm_perm.cuid != sec_initial_uid() ||
231 shm_ds.shm_perm.cgid != sec_initial_gid()) {
232 DEBUG(0,("ERROR: we did not create the shmem "
233 "(owned by another user, uid %u, gid %u)\n",
234 shm_ds.shm_perm.cuid,
235 shm_ds.shm_perm.cgid));
236 return False;
239 if (shm_ds.shm_segsz != sizeof(*profile_h)) {
240 DEBUG(0,("WARNING: profile size is %d (expected %lu). Deleting\n",
241 (int)shm_ds.shm_segsz, sizeof(*profile_h)));
242 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
243 goto again;
244 } else {
245 return False;
249 if (!read_only && (shm_ds.shm_nattch == 1)) {
250 memset((char *)profile_h, 0, sizeof(*profile_h));
251 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
252 profile_h->prof_shm_version = PROF_SHM_VERSION;
253 DEBUG(3,("Initialised profile area\n"));
256 profile_p = &profile_h->stats;
257 message_register(MSG_PROFILE, profile_message, NULL);
258 message_register(MSG_REQ_PROFILELEVEL, reqprofile_message, NULL);
259 return True;
262 const char * profile_value_name(enum profile_stats_values val)
264 static const char * valnames[PR_VALUE_MAX + 1] =
266 "smbd_idle", /* PR_VALUE_SMBD_IDLE */
267 "syscall_opendir", /* PR_VALUE_SYSCALL_OPENDIR */
268 "syscall_readdir", /* PR_VALUE_SYSCALL_READDIR */
269 "syscall_seekdir", /* PR_VALUE_SYSCALL_SEEKDIR */
270 "syscall_telldir", /* PR_VALUE_SYSCALL_TELLDIR */
271 "syscall_rewinddir", /* PR_VALUE_SYSCALL_REWINDDIR */
272 "syscall_mkdir", /* PR_VALUE_SYSCALL_MKDIR */
273 "syscall_rmdir", /* PR_VALUE_SYSCALL_RMDIR */
274 "syscall_closedir", /* PR_VALUE_SYSCALL_CLOSEDIR */
275 "syscall_open", /* PR_VALUE_SYSCALL_OPEN */
276 "syscall_close", /* PR_VALUE_SYSCALL_CLOSE */
277 "syscall_read", /* PR_VALUE_SYSCALL_READ */
278 "syscall_pread", /* PR_VALUE_SYSCALL_PREAD */
279 "syscall_write", /* PR_VALUE_SYSCALL_WRITE */
280 "syscall_pwrite", /* PR_VALUE_SYSCALL_PWRITE */
281 "syscall_lseek", /* PR_VALUE_SYSCALL_LSEEK */
282 "syscall_sendfile", /* PR_VALUE_SYSCALL_SENDFILE */
283 "syscall_rename", /* PR_VALUE_SYSCALL_RENAME */
284 "syscall_fsync", /* PR_VALUE_SYSCALL_FSYNC */
285 "syscall_stat", /* PR_VALUE_SYSCALL_STAT */
286 "syscall_fstat", /* PR_VALUE_SYSCALL_FSTAT */
287 "syscall_lstat", /* PR_VALUE_SYSCALL_LSTAT */
288 "syscall_unlink", /* PR_VALUE_SYSCALL_UNLINK */
289 "syscall_chmod", /* PR_VALUE_SYSCALL_CHMOD */
290 "syscall_fchmod", /* PR_VALUE_SYSCALL_FCHMOD */
291 "syscall_chown", /* PR_VALUE_SYSCALL_CHOWN */
292 "syscall_fchown", /* PR_VALUE_SYSCALL_FCHOWN */
293 "syscall_chdir", /* PR_VALUE_SYSCALL_CHDIR */
294 "syscall_getwd", /* PR_VALUE_SYSCALL_GETWD */
295 "syscall_ntimes", /* PR_VALUE_SYSCALL_NTIMES */
296 "syscall_ftruncate", /* PR_VALUE_SYSCALL_FTRUNCATE */
297 "syscall_fcntl_lock", /* PR_VALUE_SYSCALL_FCNTL_LOCK */
298 "syscall_kernel_flock", /* PR_VALUE_SYSCALL_KERNEL_FLOCK */
299 "syscall_linux_setlease", /* PR_VALUE_SYSCALL_LINUX_SETLEASE */
300 "syscall_fcntl_getlock", /* PR_VALUE_SYSCALL_FCNTL_GETLOCK */
301 "syscall_readlink", /* PR_VALUE_SYSCALL_READLINK */
302 "syscall_symlink", /* PR_VALUE_SYSCALL_SYMLINK */
303 "syscall_link", /* PR_VALUE_SYSCALL_LINK */
304 "syscall_mknod", /* PR_VALUE_SYSCALL_MKNOD */
305 "syscall_realpath", /* PR_VALUE_SYSCALL_REALPATH */
306 "syscall_get_quota", /* PR_VALUE_SYSCALL_GET_QUOTA */
307 "syscall_set_quota", /* PR_VALUE_SYSCALL_SET_QUOTA */
308 "SMBmkdir", /* PR_VALUE_SMBMKDIR */
309 "SMBrmdir", /* PR_VALUE_SMBRMDIR */
310 "SMBopen", /* PR_VALUE_SMBOPEN */
311 "SMBcreate", /* PR_VALUE_SMBCREATE */
312 "SMBclose", /* PR_VALUE_SMBCLOSE */
313 "SMBflush", /* PR_VALUE_SMBFLUSH */
314 "SMBunlink", /* PR_VALUE_SMBUNLINK */
315 "SMBmv", /* PR_VALUE_SMBMV */
316 "SMBgetatr", /* PR_VALUE_SMBGETATR */
317 "SMBsetatr", /* PR_VALUE_SMBSETATR */
318 "SMBread", /* PR_VALUE_SMBREAD */
319 "SMBwrite", /* PR_VALUE_SMBWRITE */
320 "SMBlock", /* PR_VALUE_SMBLOCK */
321 "SMBunlock", /* PR_VALUE_SMBUNLOCK */
322 "SMBctemp", /* PR_VALUE_SMBCTEMP */
323 "SMBmknew", /* PR_VALUE_SMBMKNEW */
324 "SMBcheckpath", /* PR_VALUE_SMBCHECKPATH */
325 "SMBexit", /* PR_VALUE_SMBEXIT */
326 "SMBlseek", /* PR_VALUE_SMBLSEEK */
327 "SMBlockread", /* PR_VALUE_SMBLOCKREAD */
328 "SMBwriteunlock", /* PR_VALUE_SMBWRITEUNLOCK */
329 "SMBreadbraw", /* PR_VALUE_SMBREADBRAW */
330 "SMBreadBmpx", /* PR_VALUE_SMBREADBMPX */
331 "SMBreadBs", /* PR_VALUE_SMBREADBS */
332 "SMBwritebraw", /* PR_VALUE_SMBWRITEBRAW */
333 "SMBwriteBmpx", /* PR_VALUE_SMBWRITEBMPX */
334 "SMBwriteBs", /* PR_VALUE_SMBWRITEBS */
335 "SMBwritec", /* PR_VALUE_SMBWRITEC */
336 "SMBsetattrE", /* PR_VALUE_SMBSETATTRE */
337 "SMBgetattrE", /* PR_VALUE_SMBGETATTRE */
338 "SMBlockingX", /* PR_VALUE_SMBLOCKINGX */
339 "SMBtrans", /* PR_VALUE_SMBTRANS */
340 "SMBtranss", /* PR_VALUE_SMBTRANSS */
341 "SMBioctl", /* PR_VALUE_SMBIOCTL */
342 "SMBioctls", /* PR_VALUE_SMBIOCTLS */
343 "SMBcopy", /* PR_VALUE_SMBCOPY */
344 "SMBmove", /* PR_VALUE_SMBMOVE */
345 "SMBecho", /* PR_VALUE_SMBECHO */
346 "SMBwriteclose", /* PR_VALUE_SMBWRITECLOSE */
347 "SMBopenX", /* PR_VALUE_SMBOPENX */
348 "SMBreadX", /* PR_VALUE_SMBREADX */
349 "SMBwriteX", /* PR_VALUE_SMBWRITEX */
350 "SMBtrans2", /* PR_VALUE_SMBTRANS2 */
351 "SMBtranss2", /* PR_VALUE_SMBTRANSS2 */
352 "SMBfindclose", /* PR_VALUE_SMBFINDCLOSE */
353 "SMBfindnclose", /* PR_VALUE_SMBFINDNCLOSE */
354 "SMBtcon", /* PR_VALUE_SMBTCON */
355 "SMBtdis", /* PR_VALUE_SMBTDIS */
356 "SMBnegprot", /* PR_VALUE_SMBNEGPROT */
357 "SMBsesssetupX", /* PR_VALUE_SMBSESSSETUPX */
358 "SMBulogoffX", /* PR_VALUE_SMBULOGOFFX */
359 "SMBtconX", /* PR_VALUE_SMBTCONX */
360 "SMBdskattr", /* PR_VALUE_SMBDSKATTR */
361 "SMBsearch", /* PR_VALUE_SMBSEARCH */
362 "SMBffirst", /* PR_VALUE_SMBFFIRST */
363 "SMBfunique", /* PR_VALUE_SMBFUNIQUE */
364 "SMBfclose", /* PR_VALUE_SMBFCLOSE */
365 "SMBnttrans", /* PR_VALUE_SMBNTTRANS */
366 "SMBnttranss", /* PR_VALUE_SMBNTTRANSS */
367 "SMBntcreateX", /* PR_VALUE_SMBNTCREATEX */
368 "SMBntcancel", /* PR_VALUE_SMBNTCANCEL */
369 "SMBntrename", /* PR_VALUE_SMBNTRENAME */
370 "SMBsplopen", /* PR_VALUE_SMBSPLOPEN */
371 "SMBsplwr", /* PR_VALUE_SMBSPLWR */
372 "SMBsplclose", /* PR_VALUE_SMBSPLCLOSE */
373 "SMBsplretq", /* PR_VALUE_SMBSPLRETQ */
374 "SMBsends", /* PR_VALUE_SMBSENDS */
375 "SMBsendb", /* PR_VALUE_SMBSENDB */
376 "SMBfwdname", /* PR_VALUE_SMBFWDNAME */
377 "SMBcancelf", /* PR_VALUE_SMBCANCELF */
378 "SMBgetmac", /* PR_VALUE_SMBGETMAC */
379 "SMBsendstrt", /* PR_VALUE_SMBSENDSTRT */
380 "SMBsendend", /* PR_VALUE_SMBSENDEND */
381 "SMBsendtxt", /* PR_VALUE_SMBSENDTXT */
382 "SMBinvalid", /* PR_VALUE_SMBINVALID */
383 "pathworks_setdir", /* PR_VALUE_PATHWORKS_SETDIR */
384 "Trans2_open", /* PR_VALUE_TRANS2_OPEN */
385 "Trans2_findfirst", /* PR_VALUE_TRANS2_FINDFIRST */
386 "Trans2_findnext", /* PR_VALUE_TRANS2_FINDNEXT */
387 "Trans2_qfsinfo", /* PR_VALUE_TRANS2_QFSINFO */
388 "Trans2_setfsinfo", /* PR_VALUE_TRANS2_SETFSINFO */
389 "Trans2_qpathinfo", /* PR_VALUE_TRANS2_QPATHINFO */
390 "Trans2_setpathinfo", /* PR_VALUE_TRANS2_SETPATHINFO */
391 "Trans2_qfileinfo", /* PR_VALUE_TRANS2_QFILEINFO */
392 "Trans2_setfileinfo", /* PR_VALUE_TRANS2_SETFILEINFO */
393 "Trans2_fsctl", /* PR_VALUE_TRANS2_FSCTL */
394 "Trans2_ioctl", /* PR_VALUE_TRANS2_IOCTL */
395 "Trans2_findnotifyfirst", /* PR_VALUE_TRANS2_FINDNOTIFYFIRST */
396 "Trans2_findnotifynext", /* PR_VALUE_TRANS2_FINDNOTIFYNEXT */
397 "Trans2_mkdir", /* PR_VALUE_TRANS2_MKDIR */
398 "Trans2_session_setup", /* PR_VALUE_TRANS2_SESSION_SETUP */
399 "Trans2_get_dfs_referral", /* PR_VALUE_TRANS2_GET_DFS_REFERRAL */
400 "Trans2_report_dfs_inconsistancy", /* PR_VALUE_TRANS2_REPORT_DFS_INCONSISTANCY */
401 "NT_transact_create", /* PR_VALUE_NT_TRANSACT_CREATE */
402 "NT_transact_ioctl", /* PR_VALUE_NT_TRANSACT_IOCTL */
403 "NT_transact_set_security_desc", /* PR_VALUE_NT_TRANSACT_SET_SECURITY_DESC */
404 "NT_transact_notify_change",/* PR_VALUE_NT_TRANSACT_NOTIFY_CHANGE */
405 "NT_transact_rename", /* PR_VALUE_NT_TRANSACT_RENAME */
406 "NT_transact_query_security_desc", /* PR_VALUE_NT_TRANSACT_QUERY_SECURITY_DESC */
407 "NT_transact_get_user_quota",/* PR_VALUE_NT_TRANSACT_GET_USER_QUOTA */
408 "NT_transact_set_user_quota",/* PR_VALUE_NT_TRANSACT_SET_USER_QUOTA */
409 "get_nt_acl", /* PR_VALUE_GET_NT_ACL */
410 "fget_nt_acl", /* PR_VALUE_FGET_NT_ACL */
411 "set_nt_acl", /* PR_VALUE_SET_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 "" /* PR_VALUE_MAX */
436 SMB_ASSERT(val >= 0);
437 SMB_ASSERT(val < PR_VALUE_MAX);
438 return valnames[val];
441 #endif /* WITH_PROFILE */