Patch from Paul Green <Paul.Green@stratus.com> to be more POSIX-compatible
[vlendec/samba-autobuild/.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 ((SHM_R | SHM_W) | (SHM_R>>3) | (SHM_R>>6))
26 #endif /* WITH_PROFILE */
27
28 static int shm_id;
29 static BOOL read_only;
30
31 struct profile_header *profile_h;
32 struct profile_stats *profile_p;
33
34 BOOL do_profile_flag = False;
35 BOOL do_profile_times = False;
36
37 struct timeval profile_starttime;
38 struct timeval profile_endtime;
39 struct timeval profile_starttime_nested;
40 struct timeval profile_endtime_nested;
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 #ifdef WITH_PROFILE
51         switch (level) {
52         case 0:         /* turn off profiling */
53                 do_profile_flag = False;
54                 do_profile_times = False;
55                 DEBUG(1,("INFO: Profiling turned OFF from pid %d\n", (int)src));
56                 break;
57         case 1:         /* turn on counter profiling only */
58                 do_profile_flag = True;
59                 do_profile_times = False;
60                 DEBUG(1,("INFO: Profiling counts turned ON from pid %d\n", (int)src));
61                 break;
62         case 2:         /* turn on complete profiling */
63                 do_profile_flag = True;
64                 do_profile_times = True;
65                 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n", (int)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", (int)src));
70                 break;
71         }
72 #else /* WITH_PROFILE */
73         DEBUG(1,("INFO: Profiling support unavailable in this build.\n"));
74 #endif /* WITH_PROFILE */
75 }
76
77 /****************************************************************************
78 receive a request profile level message
79 ****************************************************************************/
80 void reqprofile_message(int msg_type, pid_t src, void *buf, size_t len)
81 {
82         int level;
83
84 #ifdef WITH_PROFILE
85         level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
86 #else
87         level = 0;
88 #endif
89         DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",(unsigned int)src));
90         message_send_pid(src, MSG_PROFILELEVEL, &level, sizeof(int), True);
91 }
92
93 /*******************************************************************
94   open the profiling shared memory area
95   ******************************************************************/
96 #ifdef WITH_PROFILE
97 BOOL profile_setup(BOOL rdonly)
98 {
99         struct shmid_ds shm_ds;
100
101         read_only = rdonly;
102
103  again:
104         /* try to use an existing key */
105         shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
106         
107         /* if that failed then create one. There is a race condition here
108            if we are running from inetd. Bad luck. */
109         if (shm_id == -1) {
110                 if (read_only) return False;
111                 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h), 
112                                 IPC_CREAT | IPC_EXCL | IPC_PERMS);
113         }
114         
115         if (shm_id == -1) {
116                 DEBUG(0,("Can't create or use IPC area. Error was %s\n", 
117                          strerror(errno)));
118                 return False;
119         }   
120         
121         
122         profile_h = (struct profile_header *)shmat(shm_id, 0, 
123                                                    read_only?SHM_RDONLY:0);
124         if ((long)profile_p == -1) {
125                 DEBUG(0,("Can't attach to IPC area. Error was %s\n", 
126                          strerror(errno)));
127                 return False;
128         }
129
130         /* find out who created this memory area */
131         if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
132                 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n", 
133                          strerror(errno)));
134                 return False;
135         }
136
137         if (shm_ds.shm_perm.cuid != sec_initial_uid() || shm_ds.shm_perm.cgid != sec_initial_gid()) {
138                 DEBUG(0,("ERROR: we did not create the shmem (owned by another user)\n"));
139                 return False;
140         }
141
142         if (shm_ds.shm_segsz != sizeof(*profile_h)) {
143                 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
144                          (int)shm_ds.shm_segsz, sizeof(*profile_h)));
145                 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
146                         goto again;
147                 } else {
148                         return False;
149                 }
150         }
151
152         if (!read_only && (shm_ds.shm_nattch == 1)) {
153                 memset((char *)profile_h, 0, sizeof(*profile_h));
154                 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
155                 profile_h->prof_shm_version = PROF_SHM_VERSION;
156                 DEBUG(3,("Initialised profile area\n"));
157         }
158
159         profile_p = &profile_h->stats;
160         message_register(MSG_PROFILE, profile_message);
161         message_register(MSG_REQ_PROFILELEVEL, reqprofile_message);
162         return True;
163 }
164 #endif /* WITH_PROFILE */