c639f17ce5024ae2fc80eddb6288e4aff79c6957
[vlendec/samba-autobuild/.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 #include <sys/shm.h>
26
27 extern int DEBUGLEVEL;
28
29 #define IPC_PERMS ((SHM_R | SHM_W) | (SHM_R>>3) | (SHM_R>>6))
30
31 static int shm_id;
32 static BOOL read_only;
33
34 struct profile_header *profile_h;
35 struct profile_stats *profile_p;
36
37 BOOL do_profile_flag = False;
38 BOOL do_profile_times = False;
39
40 struct timeval profile_starttime;
41 struct timeval profile_endtime;
42 struct timeval profile_starttime_nested;
43 struct timeval profile_endtime_nested;
44
45 /****************************************************************************
46 receive a set profile level message
47 ****************************************************************************/
48 void profile_message(int msg_type, pid_t src, void *buf, size_t len)
49 {
50         int level;
51
52         memcpy(&level, buf, sizeof(int));
53         switch (level) {
54         case 0:         /* turn off profiling */
55                 do_profile_flag = False;
56                 do_profile_times = False;
57                 break;
58         case 1:         /* turn on counter profiling only */
59                 do_profile_flag = True;
60                 do_profile_times = False;
61                 break;
62         case 2:         /* turn on complete profiling */
63                 do_profile_flag = True;
64                 do_profile_times = True;
65                 break;
66         case 3:         /* reset profile values */
67                 memset((char *)profile_p, 0, sizeof(*profile_p));
68                 break;
69         }
70         DEBUG(1,("Profile level set to %d from pid %d\n", level, (int)src));
71 }
72
73 /*******************************************************************
74   open the profiling shared memory area
75   ******************************************************************/
76 BOOL profile_setup(BOOL rdonly)
77 {
78         struct shmid_ds shm_ds;
79
80         read_only = rdonly;
81
82  again:
83         /* try to use an existing key */
84         shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
85         
86         /* if that failed then create one. There is a race condition here
87            if we are running from inetd. Bad luck. */
88         if (shm_id == -1) {
89                 if (read_only) return False;
90                 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h), 
91                                 IPC_CREAT | IPC_EXCL | IPC_PERMS);
92         }
93         
94         if (shm_id == -1) {
95                 DEBUG(0,("Can't create or use IPC area. Error was %s\n", 
96                          strerror(errno)));
97                 return False;
98         }   
99         
100         
101         profile_h = (struct profile_header *)shmat(shm_id, 0, 
102                                                    read_only?SHM_RDONLY:0);
103         if ((long)profile_p == -1) {
104                 DEBUG(0,("Can't attach to IPC area. Error was %s\n", 
105                          strerror(errno)));
106                 return False;
107         }
108
109         /* find out who created this memory area */
110         if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
111                 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n", 
112                          strerror(errno)));
113                 return False;
114         }
115
116         if (shm_ds.shm_perm.cuid != 0 || shm_ds.shm_perm.cgid != 0) {
117                 DEBUG(0,("ERROR: root did not create the shmem\n"));
118                 return False;
119         }
120
121         if (shm_ds.shm_segsz != sizeof(*profile_h)) {
122                 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
123                          (int)shm_ds.shm_segsz, sizeof(*profile_h)));
124                 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
125                         goto again;
126                 } else {
127                         return False;
128                 }
129         }
130
131         if (!read_only && (shm_ds.shm_nattch == 1)) {
132                 memset((char *)profile_h, 0, sizeof(*profile_h));
133                 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
134                 profile_h->prof_shm_version = PROF_SHM_VERSION;
135                 DEBUG(3,("Initialised profile area\n"));
136         }
137
138         profile_p = &profile_h->stats;
139         message_register(MSG_PROFILE, profile_message);
140         return True;
141 }
142