if we are adding a new sambaAccount, make sure that we add a
[Samba.git] / source / profile / profile.c
blobe6ad610d89f84fc02b04d3510c80118a24dcf574
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 store smbd profiling information in shared memory
5 Copyright (C) Andrew Tridgell 1999
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 ((SHM_R | SHM_W) | (SHM_R>>3) | (SHM_R>>6))
27 #endif /* WITH_PROFILE */
29 static int shm_id;
30 static BOOL read_only;
32 struct profile_header *profile_h;
33 struct profile_stats *profile_p;
35 BOOL do_profile_flag = False;
36 BOOL do_profile_times = False;
38 struct timeval profile_starttime;
39 struct timeval profile_endtime;
40 struct timeval profile_starttime_nested;
41 struct timeval profile_endtime_nested;
43 /****************************************************************************
44 receive a set profile level message
45 ****************************************************************************/
46 void profile_message(int msg_type, pid_t src, void *buf, size_t len)
48 int level;
50 memcpy(&level, buf, sizeof(int));
51 #ifdef WITH_PROFILE
52 switch (level) {
53 case 0: /* turn off profiling */
54 do_profile_flag = False;
55 do_profile_times = False;
56 DEBUG(1,("INFO: Profiling turned OFF from pid %d\n", (int)src));
57 break;
58 case 1: /* turn on counter profiling only */
59 do_profile_flag = True;
60 do_profile_times = False;
61 DEBUG(1,("INFO: Profiling counts turned ON from pid %d\n", (int)src));
62 break;
63 case 2: /* turn on complete profiling */
64 do_profile_flag = True;
65 do_profile_times = True;
66 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n", (int)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", (int)src));
71 break;
73 #else /* ndef WITH_PROFILE */
74 DEBUG(1,("INFO: Profiling support unavailable in this build.\n"));
75 #endif /* WITH_PROFILE */
78 /****************************************************************************
79 receive a request profile level message
80 ****************************************************************************/
81 void reqprofile_message(int msg_type, pid_t src, void *buf, size_t len)
83 int level;
85 #ifdef WITH_PROFILE
86 level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
87 #else
88 level = 0;
89 #endif
90 DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",(unsigned int)src));
91 message_send_pid(src, MSG_PROFILELEVEL, &level, sizeof(int), True);
94 /*******************************************************************
95 open the profiling shared memory area
96 ******************************************************************/
98 #ifdef WITH_PROFILE
99 BOOL profile_setup(BOOL rdonly)
101 struct shmid_ds shm_ds;
103 read_only = rdonly;
105 again:
106 /* try to use an existing key */
107 shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
109 /* if that failed then create one. There is a race condition here
110 if we are running from inetd. Bad luck. */
111 if (shm_id == -1) {
112 if (read_only) return False;
113 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h),
114 IPC_CREAT | IPC_EXCL | IPC_PERMS);
117 if (shm_id == -1) {
118 DEBUG(0,("Can't create or use IPC area. Error was %s\n",
119 strerror(errno)));
120 return False;
124 profile_h = (struct profile_header *)shmat(shm_id, 0,
125 read_only?SHM_RDONLY:0);
126 if ((long)profile_p == -1) {
127 DEBUG(0,("Can't attach to IPC area. Error was %s\n",
128 strerror(errno)));
129 return False;
132 /* find out who created this memory area */
133 if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
134 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n",
135 strerror(errno)));
136 return False;
139 #if 0
140 if (shm_ds.shm_perm.cuid != 0 || shm_ds.shm_perm.cgid != 0) {
141 DEBUG(0,("ERROR: root did not create the shmem\n"));
142 return False;
144 #endif
146 if (shm_ds.shm_segsz != sizeof(*profile_h)) {
147 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
148 (int)shm_ds.shm_segsz, sizeof(*profile_h)));
149 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
150 goto again;
151 } else {
152 return False;
156 if (!read_only && (shm_ds.shm_nattch == 1)) {
157 memset((char *)profile_h, 0, sizeof(*profile_h));
158 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
159 profile_h->prof_shm_version = PROF_SHM_VERSION;
160 DEBUG(3,("Initialised profile area\n"));
163 profile_p = &profile_h->stats;
164 message_register(MSG_PROFILE, profile_message);
165 message_register(MSG_REQ_PROFILELEVEL, reqprofile_message);
166 return True;
168 #endif /* WITH_PROFILE */