Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header.
[samba.git] / source3 / profile / profile.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    store smbd profiling information in shared memory
5    Copyright (C) Andrew Tridgell 1999
6    
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.
11    
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.
16    
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.
20
21 */
22
23 #include "includes.h"
24
25 #define IPC_PERMS ((SHM_R | SHM_W) | (SHM_R>>3) | (SHM_R>>6))
26
27 static int shm_id;
28 static BOOL read_only;
29
30 struct profile_header *profile_h;
31 struct profile_stats *profile_p;
32
33 BOOL do_profile_flag = False;
34 BOOL do_profile_times = False;
35
36 struct timeval profile_starttime;
37 struct timeval profile_endtime;
38 struct timeval profile_starttime_nested;
39 struct timeval profile_endtime_nested;
40
41 /****************************************************************************
42 receive a set profile level message
43 ****************************************************************************/
44 void profile_message(int msg_type, pid_t src, void *buf, size_t len)
45 {
46         int level;
47
48         memcpy(&level, buf, sizeof(int));
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", (int)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", (int)src));
59                 break;
60         case 2:         /* turn on complete profiling */
61                 do_profile_flag = True;
62                 do_profile_times = True;
63                 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n", (int)src));
64                 break;
65         case 3:         /* reset profile values */
66                 memset((char *)profile_p, 0, sizeof(*profile_p));
67                 DEBUG(1,("INFO: Profiling values cleared from pid %d\n", (int)src));
68                 break;
69         }
70 }
71
72 /****************************************************************************
73 receive a request profile level message
74 ****************************************************************************/
75 void reqprofile_message(int msg_type, pid_t src, void *buf, size_t len)
76 {
77         int level;
78
79 #ifdef WITH_PROFILE
80         level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
81 #else
82         level = 0;
83 #endif
84         DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",(unsigned int)src));
85         message_send_pid(src, MSG_PROFILELEVEL, &level, sizeof(int), True);
86 }
87
88 /*******************************************************************
89   open the profiling shared memory area
90   ******************************************************************/
91 BOOL profile_setup(BOOL rdonly)
92 {
93         struct shmid_ds shm_ds;
94
95         read_only = rdonly;
96
97  again:
98         /* try to use an existing key */
99         shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
100         
101         /* if that failed then create one. There is a race condition here
102            if we are running from inetd. Bad luck. */
103         if (shm_id == -1) {
104                 if (read_only) return False;
105                 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h), 
106                                 IPC_CREAT | IPC_EXCL | IPC_PERMS);
107         }
108         
109         if (shm_id == -1) {
110                 DEBUG(0,("Can't create or use IPC area. Error was %s\n", 
111                          strerror(errno)));
112                 return False;
113         }   
114         
115         
116         profile_h = (struct profile_header *)shmat(shm_id, 0, 
117                                                    read_only?SHM_RDONLY:0);
118         if ((long)profile_p == -1) {
119                 DEBUG(0,("Can't attach to IPC area. Error was %s\n", 
120                          strerror(errno)));
121                 return False;
122         }
123
124         /* find out who created this memory area */
125         if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
126                 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n", 
127                          strerror(errno)));
128                 return False;
129         }
130
131         if (shm_ds.shm_perm.cuid != sec_initial_uid() || shm_ds.shm_perm.cgid != sec_initial_gid()) {
132                 DEBUG(0,("ERROR: we did not create the shmem (owned by another user)\n"));
133                 return False;
134         }
135
136         if (shm_ds.shm_segsz != sizeof(*profile_h)) {
137                 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
138                          (int)shm_ds.shm_segsz, sizeof(*profile_h)));
139                 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
140                         goto again;
141                 } else {
142                         return False;
143                 }
144         }
145
146         if (!read_only && (shm_ds.shm_nattch == 1)) {
147                 memset((char *)profile_h, 0, sizeof(*profile_h));
148                 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
149                 profile_h->prof_shm_version = PROF_SHM_VERSION;
150                 DEBUG(3,("Initialised profile area\n"));
151         }
152
153         profile_p = &profile_h->stats;
154         message_register(MSG_PROFILE, profile_message);
155         message_register(MSG_REQ_PROFILELEVEL, reqprofile_message);
156         return True;
157 }