r13704: Janitor for tpot.
[Samba.git] / source / profile / profile.c
blob838383b1afee8eb4fb5b23015f42ff7a501a8368
1 /*
2 Unix SMB/CIFS implementation.
3 store smbd profiling information in shared memory
4 Copyright (C) Andrew Tridgell 1999
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.
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.
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.
22 #include "includes.h"
24 #ifdef WITH_PROFILE
25 #define IPC_PERMS ((S_IRUSR | S_IWUSR) | S_IRGRP | S_IROTH)
26 #endif /* WITH_PROFILE */
28 #ifdef WITH_PROFILE
29 static int shm_id;
30 static BOOL read_only;
31 #endif
33 struct profile_header *profile_h;
34 struct profile_stats *profile_p;
36 BOOL do_profile_flag = False;
37 BOOL do_profile_times = False;
39 struct timeval profile_starttime;
40 struct timeval profile_endtime;
41 struct timeval profile_starttime_nested;
42 struct timeval profile_endtime_nested;
44 /****************************************************************************
45 receive a set profile level message
46 ****************************************************************************/
47 void profile_message(int msg_type, struct process_id src, void *buf, size_t len)
49 int level;
51 memcpy(&level, buf, sizeof(int));
52 #ifdef WITH_PROFILE
53 switch (level) {
54 case 0: /* turn off profiling */
55 do_profile_flag = False;
56 do_profile_times = False;
57 DEBUG(1,("INFO: Profiling turned OFF from pid %d\n",
58 (int)procid_to_pid(&src)));
59 break;
60 case 1: /* turn on counter profiling only */
61 do_profile_flag = True;
62 do_profile_times = False;
63 DEBUG(1,("INFO: Profiling counts turned ON from pid %d\n",
64 (int)procid_to_pid(&src)));
65 break;
66 case 2: /* turn on complete profiling */
67 do_profile_flag = True;
68 do_profile_times = True;
69 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n",
70 (int)procid_to_pid(&src)));
71 break;
72 case 3: /* reset profile values */
73 memset((char *)profile_p, 0, sizeof(*profile_p));
74 DEBUG(1,("INFO: Profiling values cleared from pid %d\n",
75 (int)procid_to_pid(&src)));
76 break;
78 #else /* WITH_PROFILE */
79 DEBUG(1,("INFO: Profiling support unavailable in this build.\n"));
80 #endif /* WITH_PROFILE */
83 /****************************************************************************
84 receive a request profile level message
85 ****************************************************************************/
86 void reqprofile_message(int msg_type, struct process_id src,
87 void *buf, size_t len)
89 int level;
91 #ifdef WITH_PROFILE
92 level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
93 #else
94 level = 0;
95 #endif
96 DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",
97 (unsigned int)procid_to_pid(&src)));
98 message_send_pid(src, MSG_PROFILELEVEL, &level, sizeof(int), True);
101 /*******************************************************************
102 open the profiling shared memory area
103 ******************************************************************/
104 #ifdef WITH_PROFILE
105 BOOL profile_setup(BOOL rdonly)
107 struct shmid_ds shm_ds;
109 read_only = rdonly;
111 again:
112 /* try to use an existing key */
113 shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
115 /* if that failed then create one. There is a race condition here
116 if we are running from inetd. Bad luck. */
117 if (shm_id == -1) {
118 if (read_only) return False;
119 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h),
120 IPC_CREAT | IPC_EXCL | IPC_PERMS);
123 if (shm_id == -1) {
124 DEBUG(0,("Can't create or use IPC area. Error was %s\n",
125 strerror(errno)));
126 return False;
130 profile_h = (struct profile_header *)shmat(shm_id, 0,
131 read_only?SHM_RDONLY:0);
132 if ((long)profile_p == -1) {
133 DEBUG(0,("Can't attach to IPC area. Error was %s\n",
134 strerror(errno)));
135 return False;
138 /* find out who created this memory area */
139 if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
140 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n",
141 strerror(errno)));
142 return False;
145 if (shm_ds.shm_perm.cuid != sec_initial_uid() || shm_ds.shm_perm.cgid != sec_initial_gid()) {
146 DEBUG(0,("ERROR: we did not create the shmem (owned by another user)\n"));
147 return False;
150 if (shm_ds.shm_segsz != sizeof(*profile_h)) {
151 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
152 (int)shm_ds.shm_segsz, sizeof(*profile_h)));
153 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
154 goto again;
155 } else {
156 return False;
160 if (!read_only && (shm_ds.shm_nattch == 1)) {
161 memset((char *)profile_h, 0, sizeof(*profile_h));
162 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
163 profile_h->prof_shm_version = PROF_SHM_VERSION;
164 DEBUG(3,("Initialised profile area\n"));
167 profile_p = &profile_h->stats;
168 message_register(MSG_PROFILE, profile_message);
169 message_register(MSG_REQ_PROFILELEVEL, reqprofile_message);
170 return True;
172 #endif /* WITH_PROFILE */