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