r15450: Change profiling data macros to use stack variables rather than
[kai/samba.git] / source3 / profile / profile.c
1 /* 
2    Unix SMB/CIFS implementation.
3    store smbd profiling information in shared memory
4    Copyright (C) Andrew Tridgell 1999
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include "includes.h"
23
24 #ifdef WITH_PROFILE
25 #define IPC_PERMS ((S_IRUSR | S_IWUSR) | S_IRGRP | S_IROTH)
26 #endif /* WITH_PROFILE */
27
28 #ifdef WITH_PROFILE
29 static int shm_id;
30 static BOOL read_only;
31 #endif
32
33 struct profile_header *profile_h;
34 struct profile_stats *profile_p;
35
36 BOOL do_profile_flag = False;
37 BOOL do_profile_times = False;
38
39 /****************************************************************************
40 receive a set profile level message
41 ****************************************************************************/
42 void profile_message(int msg_type, struct process_id src, void *buf, size_t len)
43 {
44         int level;
45
46         memcpy(&level, buf, sizeof(int));
47 #ifdef WITH_PROFILE
48         switch (level) {
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)));
54                 break;
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)));
60                 break;
61         case 2:         /* turn on complete profiling */
62                 do_profile_flag = True;
63                 do_profile_times = True;
64                 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n",
65                          (int)procid_to_pid(&src)));
66                 break;
67         case 3:         /* reset profile values */
68                 memset((char *)profile_p, 0, sizeof(*profile_p));
69                 DEBUG(1,("INFO: Profiling values cleared from pid %d\n",
70                          (int)procid_to_pid(&src)));
71                 break;
72         }
73 #else /* WITH_PROFILE */
74         DEBUG(1,("INFO: Profiling support unavailable in this build.\n"));
75 #endif /* WITH_PROFILE */
76 }
77
78 /****************************************************************************
79 receive a request profile level message
80 ****************************************************************************/
81 void reqprofile_message(int msg_type, struct process_id src,
82                         void *buf, size_t len)
83 {
84         int level;
85
86 #ifdef WITH_PROFILE
87         level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
88 #else
89         level = 0;
90 #endif
91         DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",
92                  (unsigned int)procid_to_pid(&src)));
93         message_send_pid(src, MSG_PROFILELEVEL, &level, sizeof(int), True);
94 }
95
96 /*******************************************************************
97   open the profiling shared memory area
98   ******************************************************************/
99 #ifdef WITH_PROFILE
100 BOOL profile_setup(BOOL rdonly)
101 {
102         struct shmid_ds shm_ds;
103
104         read_only = rdonly;
105
106  again:
107         /* try to use an existing key */
108         shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
109         
110         /* if that failed then create one. There is a race condition here
111            if we are running from inetd. Bad luck. */
112         if (shm_id == -1) {
113                 if (read_only) return False;
114                 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h), 
115                                 IPC_CREAT | IPC_EXCL | IPC_PERMS);
116         }
117         
118         if (shm_id == -1) {
119                 DEBUG(0,("Can't create or use IPC area. Error was %s\n", 
120                          strerror(errno)));
121                 return False;
122         }   
123         
124         
125         profile_h = (struct profile_header *)shmat(shm_id, 0, 
126                                                    read_only?SHM_RDONLY:0);
127         if ((long)profile_p == -1) {
128                 DEBUG(0,("Can't attach to IPC area. Error was %s\n", 
129                          strerror(errno)));
130                 return False;
131         }
132
133         /* find out who created this memory area */
134         if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
135                 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n", 
136                          strerror(errno)));
137                 return False;
138         }
139
140         if (shm_ds.shm_perm.cuid != sec_initial_uid() || shm_ds.shm_perm.cgid != sec_initial_gid()) {
141                 DEBUG(0,("ERROR: we did not create the shmem (owned by another user)\n"));
142                 return False;
143         }
144
145         if (shm_ds.shm_segsz != sizeof(*profile_h)) {
146                 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
147                          (int)shm_ds.shm_segsz, sizeof(*profile_h)));
148                 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
149                         goto again;
150                 } else {
151                         return False;
152                 }
153         }
154
155         if (!read_only && (shm_ds.shm_nattch == 1)) {
156                 memset((char *)profile_h, 0, sizeof(*profile_h));
157                 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
158                 profile_h->prof_shm_version = PROF_SHM_VERSION;
159                 DEBUG(3,("Initialised profile area\n"));
160         }
161
162         profile_p = &profile_h->stats;
163         message_register(MSG_PROFILE, profile_message);
164         message_register(MSG_REQ_PROFILELEVEL, reqprofile_message);
165         return True;
166 }
167 #endif /* WITH_PROFILE */