first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/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 #ifdef WITH_PROFILE
26
27 #ifndef HAVE_SYSV_IPC
28 #error "You must have SYSV shared memory to compile in the profiling code"
29 #endif
30
31 extern int DEBUGLEVEL;
32
33 #define IPC_PERMS ((SHM_R | SHM_W) | (SHM_R>>3) | (SHM_R>>6))
34
35 static int shm_id;
36 static BOOL read_only;
37
38 struct profile_struct *profile_p;
39
40 /*******************************************************************
41   open the profiling shared memory area
42   ******************************************************************/
43 BOOL profile_setup(BOOL rdonly)
44 {
45         struct shmid_ds shm_ds;
46
47         read_only = rdonly;
48
49  again:
50         /* try to use an existing key */
51         shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
52         
53         /* if that failed then create one. There is a race condition here
54            if we are running from inetd. Bad luck. */
55         if (shm_id == -1) {
56                 if (read_only) return False;
57                 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_p), 
58                                 IPC_CREAT | IPC_EXCL | IPC_PERMS);
59         }
60         
61         if (shm_id == -1) {
62                 DEBUG(0,("Can't create or use IPC area. Error was %s\n", 
63                          strerror(errno)));
64                 return False;
65         }   
66         
67         
68         profile_p = (struct profile_struct *)shmat(shm_id, 0, 
69                                                    read_only?SHM_RDONLY:0);
70         if ((long)profile_p == -1) {
71                 DEBUG(0,("Can't attach to IPC area. Error was %s\n", 
72                          strerror(errno)));
73                 return False;
74         }
75
76         /* find out who created this memory area */
77         if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
78                 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n", 
79                          strerror(errno)));
80                 return False;
81         }
82
83         if (shm_ds.shm_perm.cuid != 0 || shm_ds.shm_perm.cgid != 0) {
84                 DEBUG(0,("ERROR: root did not create the shmem\n"));
85                 return False;
86         }
87
88         if (shm_ds.shm_segsz != sizeof(*profile_p)) {
89                 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
90                          (int)shm_ds.shm_segsz, sizeof(*profile_p)));
91                 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
92                         goto again;
93                 } else {
94                         return False;
95                 }
96         }
97
98         if (!read_only && (shm_ds.shm_nattch == 1)) {
99                 memset((char *)profile_p, 0, sizeof(*profile_p));
100                 profile_p->prof_shm_magic = PROF_SHM_MAGIC;
101                 profile_p->prof_shm_version = PROF_SHM_VERSION;
102                 DEBUG(3,("Initialised profile area\n"));
103         }
104
105         return True;
106 }
107
108 #else
109  /* to keep compilers happy about empty modules */
110  void profile_dummy(void) {}
111 #endif