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