f081e1bbd7855c93f035a54e5119968f85b87696
[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    Copyright (C) James Peach 2006
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 3 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, see <http://www.gnu.org/licenses/>.
19
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/messaging.h"
24 #include "messages.h"
25
26 #ifdef WITH_PROFILE
27 #define IPC_PERMS ((S_IRUSR | S_IWUSR) | S_IRGRP | S_IROTH)
28 #endif /* WITH_PROFILE */
29
30 #ifdef WITH_PROFILE
31 static int shm_id;
32 static bool read_only;
33 #endif
34
35 struct profile_header *profile_h;
36 struct profile_stats *profile_p;
37
38 bool do_profile_flag = False;
39 bool do_profile_times = False;
40
41 /****************************************************************************
42 Set a profiling level.
43 ****************************************************************************/
44 void set_profile_level(int level, struct server_id src)
45 {
46 #ifdef WITH_PROFILE
47         switch (level) {
48         case 0:         /* turn off profiling */
49                 do_profile_flag = False;
50                 do_profile_times = False;
51                 DEBUG(1,("INFO: Profiling turned OFF from pid %d\n",
52                          (int)procid_to_pid(&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",
58                          (int)procid_to_pid(&src)));
59                 break;
60         case 2:         /* turn on complete profiling */
61                 do_profile_flag = True;
62                 do_profile_times = True;
63                 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n",
64                          (int)procid_to_pid(&src)));
65                 break;
66         case 3:         /* reset profile values */
67                 memset((char *)profile_p, 0, sizeof(*profile_p));
68                 DEBUG(1,("INFO: Profiling values cleared from pid %d\n",
69                          (int)procid_to_pid(&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 #ifdef WITH_PROFILE
78
79 /****************************************************************************
80 receive a set profile level message
81 ****************************************************************************/
82 static void profile_message(struct messaging_context *msg_ctx,
83                             void *private_data,
84                             uint32_t msg_type,
85                             struct server_id src,
86                             DATA_BLOB *data)
87 {
88         int level;
89
90         if (data->length != sizeof(level)) {
91                 DEBUG(0, ("got invalid profile message\n"));
92                 return;
93         }
94
95         memcpy(&level, data->data, sizeof(level));
96         set_profile_level(level, src);
97 }
98
99 /****************************************************************************
100 receive a request profile level message
101 ****************************************************************************/
102 static void reqprofile_message(struct messaging_context *msg_ctx,
103                                void *private_data, 
104                                uint32_t msg_type, 
105                                struct server_id src,
106                                DATA_BLOB *data)
107 {
108         int level;
109
110 #ifdef WITH_PROFILE
111         level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
112 #else
113         level = 0;
114 #endif
115         DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",
116                  (unsigned int)procid_to_pid(&src)));
117         messaging_send_buf(msg_ctx, src, MSG_PROFILELEVEL,
118                            (uint8 *)&level, sizeof(level));
119 }
120
121 /*******************************************************************
122   open the profiling shared memory area
123   ******************************************************************/
124 bool profile_setup(struct messaging_context *msg_ctx, bool rdonly)
125 {
126         struct shmid_ds shm_ds;
127
128         read_only = rdonly;
129
130  again:
131         /* try to use an existing key */
132         shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
133         
134         /* if that failed then create one. There is a race condition here
135            if we are running from inetd. Bad luck. */
136         if (shm_id == -1) {
137                 if (read_only) return False;
138                 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h), 
139                                 IPC_CREAT | IPC_EXCL | IPC_PERMS);
140         }
141         
142         if (shm_id == -1) {
143                 DEBUG(0,("Can't create or use IPC area. Error was %s\n", 
144                          strerror(errno)));
145                 return False;
146         }   
147         
148         
149         profile_h = (struct profile_header *)shmat(shm_id, 0, 
150                                                    read_only?SHM_RDONLY:0);
151         if ((long)profile_h == -1) {
152                 DEBUG(0,("Can't attach to IPC area. Error was %s\n", 
153                          strerror(errno)));
154                 return False;
155         }
156
157         /* find out who created this memory area */
158         if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
159                 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n", 
160                          strerror(errno)));
161                 return False;
162         }
163
164         if (shm_ds.shm_perm.cuid != sec_initial_uid() ||
165             shm_ds.shm_perm.cgid != sec_initial_gid()) {
166                 DEBUG(0,("ERROR: we did not create the shmem "
167                          "(owned by another user, uid %u, gid %u)\n",
168                          shm_ds.shm_perm.cuid,
169                          shm_ds.shm_perm.cgid));
170                 return False;
171         }
172
173         if (shm_ds.shm_segsz != sizeof(*profile_h)) {
174                 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
175                          (int)shm_ds.shm_segsz, (int)sizeof(*profile_h)));
176                 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
177                         goto again;
178                 } else {
179                         return False;
180                 }
181         }
182
183         if (!read_only && (shm_ds.shm_nattch == 1)) {
184                 memset((char *)profile_h, 0, sizeof(*profile_h));
185                 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
186                 profile_h->prof_shm_version = PROF_SHM_VERSION;
187                 DEBUG(3,("Initialised profile area\n"));
188         }
189
190         profile_p = &profile_h->stats;
191         if (msg_ctx != NULL) {
192                 messaging_register(msg_ctx, NULL, MSG_PROFILE,
193                                    profile_message);
194                 messaging_register(msg_ctx, NULL, MSG_REQ_PROFILELEVEL,
195                                    reqprofile_message);
196         }
197         return True;
198 }
199
200  const char * profile_value_name(enum profile_stats_values val)
201 {
202         static const char * valnames[PR_VALUE_MAX + 1] =
203         {
204             "smbd_idle",                /* PR_VALUE_SMBD_IDLE */
205             "syscall_opendir",          /* PR_VALUE_SYSCALL_OPENDIR */
206             "syscall_readdir",          /* PR_VALUE_SYSCALL_READDIR */
207             "syscall_seekdir",          /* PR_VALUE_SYSCALL_SEEKDIR */
208             "syscall_telldir",          /* PR_VALUE_SYSCALL_TELLDIR */
209             "syscall_rewinddir",        /* PR_VALUE_SYSCALL_REWINDDIR */
210             "syscall_mkdir",            /* PR_VALUE_SYSCALL_MKDIR */
211             "syscall_rmdir",            /* PR_VALUE_SYSCALL_RMDIR */
212             "syscall_closedir",         /* PR_VALUE_SYSCALL_CLOSEDIR */
213             "syscall_open",             /* PR_VALUE_SYSCALL_OPEN */
214             "syscall_createfile",       /* PR_VALUE_SYSCALL_CREATEFILE */
215             "syscall_close",            /* PR_VALUE_SYSCALL_CLOSE */
216             "syscall_read",             /* PR_VALUE_SYSCALL_READ */
217             "syscall_pread",            /* PR_VALUE_SYSCALL_PREAD */
218             "syscall_write",            /* PR_VALUE_SYSCALL_WRITE */
219             "syscall_pwrite",           /* PR_VALUE_SYSCALL_PWRITE */
220             "syscall_lseek",            /* PR_VALUE_SYSCALL_LSEEK */
221             "syscall_sendfile",         /* PR_VALUE_SYSCALL_SENDFILE */
222             "syscall_recvfile",         /* PR_VALUE_SYSCALL_RECVFILE */
223             "syscall_rename",           /* PR_VALUE_SYSCALL_RENAME */
224             "syscall_rename_at",        /* PR_VALUE_SYSCALL_RENAME_AT */
225             "syscall_fsync",            /* PR_VALUE_SYSCALL_FSYNC */
226             "syscall_stat",             /* PR_VALUE_SYSCALL_STAT */
227             "syscall_fstat",            /* PR_VALUE_SYSCALL_FSTAT */
228             "syscall_lstat",            /* PR_VALUE_SYSCALL_LSTAT */
229             "syscall_unlink",           /* PR_VALUE_SYSCALL_UNLINK */
230             "syscall_chmod",            /* PR_VALUE_SYSCALL_CHMOD */
231             "syscall_fchmod",           /* PR_VALUE_SYSCALL_FCHMOD */
232             "syscall_chown",            /* PR_VALUE_SYSCALL_CHOWN */
233             "syscall_fchown",           /* PR_VALUE_SYSCALL_FCHOWN */
234             "syscall_chdir",            /* PR_VALUE_SYSCALL_CHDIR */
235             "syscall_getwd",            /* PR_VALUE_SYSCALL_GETWD */
236             "syscall_ntimes",           /* PR_VALUE_SYSCALL_NTIMES */
237             "syscall_ftruncate",        /* PR_VALUE_SYSCALL_FTRUNCATE */
238             "syscall_fallocate",        /* PR_VALUE_SYSCALL_FALLOCATE */
239             "syscall_fcntl_lock",       /* PR_VALUE_SYSCALL_FCNTL_LOCK */
240             "syscall_kernel_flock",     /* PR_VALUE_SYSCALL_KERNEL_FLOCK */
241             "syscall_linux_setlease",   /* PR_VALUE_SYSCALL_LINUX_SETLEASE */
242             "syscall_fcntl_getlock",    /* PR_VALUE_SYSCALL_FCNTL_GETLOCK */
243             "syscall_readlink",         /* PR_VALUE_SYSCALL_READLINK */
244             "syscall_symlink",          /* PR_VALUE_SYSCALL_SYMLINK */
245             "syscall_link",             /* PR_VALUE_SYSCALL_LINK */
246             "syscall_mknod",            /* PR_VALUE_SYSCALL_MKNOD */
247             "syscall_realpath",         /* PR_VALUE_SYSCALL_REALPATH */
248             "syscall_get_quota",        /* PR_VALUE_SYSCALL_GET_QUOTA */
249             "syscall_set_quota",        /* PR_VALUE_SYSCALL_SET_QUOTA */
250             "syscall_get_sd",           /* PR_VALUE_SYSCALL_GET_SD */
251             "syscall_set_sd",           /* PR_VALUE_SYSCALL_SET_SD */
252             "syscall_brl_lock",         /* PR_VALUE_SYSCALL_BRL_LOCK */
253             "syscall_brl_unlock",       /* PR_VALUE_SYSCALL_BRL_UNLOCK */
254             "syscall_brl_cancel",       /* PR_VALUE_SYSCALL_BRL_CANCEL */
255             "SMBmkdir",         /* PR_VALUE_SMBMKDIR */
256             "SMBrmdir",         /* PR_VALUE_SMBRMDIR */
257             "SMBopen",          /* PR_VALUE_SMBOPEN */
258             "SMBcreate",        /* PR_VALUE_SMBCREATE */
259             "SMBclose",         /* PR_VALUE_SMBCLOSE */
260             "SMBflush",         /* PR_VALUE_SMBFLUSH */
261             "SMBunlink",        /* PR_VALUE_SMBUNLINK */
262             "SMBmv",            /* PR_VALUE_SMBMV */
263             "SMBgetatr",        /* PR_VALUE_SMBGETATR */
264             "SMBsetatr",        /* PR_VALUE_SMBSETATR */
265             "SMBread",          /* PR_VALUE_SMBREAD */
266             "SMBwrite",         /* PR_VALUE_SMBWRITE */
267             "SMBlock",          /* PR_VALUE_SMBLOCK */
268             "SMBunlock",        /* PR_VALUE_SMBUNLOCK */
269             "SMBctemp",         /* PR_VALUE_SMBCTEMP */
270             "SMBmknew",         /* PR_VALUE_SMBMKNEW */
271             "SMBcheckpath",     /* PR_VALUE_SMBCHECKPATH */
272             "SMBexit",          /* PR_VALUE_SMBEXIT */
273             "SMBlseek",         /* PR_VALUE_SMBLSEEK */
274             "SMBlockread",              /* PR_VALUE_SMBLOCKREAD */
275             "SMBwriteunlock",           /* PR_VALUE_SMBWRITEUNLOCK */
276             "SMBreadbraw",              /* PR_VALUE_SMBREADBRAW */
277             "SMBreadBmpx",              /* PR_VALUE_SMBREADBMPX */
278             "SMBreadBs",                /* PR_VALUE_SMBREADBS */
279             "SMBwritebraw",             /* PR_VALUE_SMBWRITEBRAW */
280             "SMBwriteBmpx",             /* PR_VALUE_SMBWRITEBMPX */
281             "SMBwriteBs",               /* PR_VALUE_SMBWRITEBS */
282             "SMBwritec",                /* PR_VALUE_SMBWRITEC */
283             "SMBsetattrE",              /* PR_VALUE_SMBSETATTRE */
284             "SMBgetattrE",              /* PR_VALUE_SMBGETATTRE */
285             "SMBlockingX",              /* PR_VALUE_SMBLOCKINGX */
286             "SMBtrans",         /* PR_VALUE_SMBTRANS */
287             "SMBtranss",        /* PR_VALUE_SMBTRANSS */
288             "SMBioctl",         /* PR_VALUE_SMBIOCTL */
289             "SMBioctls",        /* PR_VALUE_SMBIOCTLS */
290             "SMBcopy",          /* PR_VALUE_SMBCOPY */
291             "SMBmove",          /* PR_VALUE_SMBMOVE */
292             "SMBecho",          /* PR_VALUE_SMBECHO */
293             "SMBwriteclose",    /* PR_VALUE_SMBWRITECLOSE */
294             "SMBopenX",         /* PR_VALUE_SMBOPENX */
295             "SMBreadX",         /* PR_VALUE_SMBREADX */
296             "SMBwriteX",        /* PR_VALUE_SMBWRITEX */
297             "SMBtrans2",        /* PR_VALUE_SMBTRANS2 */
298             "SMBtranss2",       /* PR_VALUE_SMBTRANSS2 */
299             "SMBfindclose",     /* PR_VALUE_SMBFINDCLOSE */
300             "SMBfindnclose",    /* PR_VALUE_SMBFINDNCLOSE */
301             "SMBtcon",          /* PR_VALUE_SMBTCON */
302             "SMBtdis",          /* PR_VALUE_SMBTDIS */
303             "SMBnegprot",       /* PR_VALUE_SMBNEGPROT */
304             "SMBsesssetupX",    /* PR_VALUE_SMBSESSSETUPX */
305             "SMBulogoffX",      /* PR_VALUE_SMBULOGOFFX */
306             "SMBtconX",         /* PR_VALUE_SMBTCONX */
307             "SMBdskattr",               /* PR_VALUE_SMBDSKATTR */
308             "SMBsearch",                /* PR_VALUE_SMBSEARCH */
309             "SMBffirst",                /* PR_VALUE_SMBFFIRST */
310             "SMBfunique",               /* PR_VALUE_SMBFUNIQUE */
311             "SMBfclose",                /* PR_VALUE_SMBFCLOSE */
312             "SMBnttrans",               /* PR_VALUE_SMBNTTRANS */
313             "SMBnttranss",              /* PR_VALUE_SMBNTTRANSS */
314             "SMBntcreateX",             /* PR_VALUE_SMBNTCREATEX */
315             "SMBntcancel",              /* PR_VALUE_SMBNTCANCEL */
316             "SMBntrename",              /* PR_VALUE_SMBNTRENAME */
317             "SMBsplopen",               /* PR_VALUE_SMBSPLOPEN */
318             "SMBsplwr",                 /* PR_VALUE_SMBSPLWR */
319             "SMBsplclose",              /* PR_VALUE_SMBSPLCLOSE */
320             "SMBsplretq",               /* PR_VALUE_SMBSPLRETQ */
321             "SMBsends",                 /* PR_VALUE_SMBSENDS */
322             "SMBsendb",                 /* PR_VALUE_SMBSENDB */
323             "SMBfwdname",               /* PR_VALUE_SMBFWDNAME */
324             "SMBcancelf",               /* PR_VALUE_SMBCANCELF */
325             "SMBgetmac",                /* PR_VALUE_SMBGETMAC */
326             "SMBsendstrt",              /* PR_VALUE_SMBSENDSTRT */
327             "SMBsendend",               /* PR_VALUE_SMBSENDEND */
328             "SMBsendtxt",               /* PR_VALUE_SMBSENDTXT */
329             "SMBinvalid",               /* PR_VALUE_SMBINVALID */
330             "pathworks_setdir",         /* PR_VALUE_PATHWORKS_SETDIR */
331             "Trans2_open",              /* PR_VALUE_TRANS2_OPEN */
332             "Trans2_findfirst",         /* PR_VALUE_TRANS2_FINDFIRST */
333             "Trans2_findnext",          /* PR_VALUE_TRANS2_FINDNEXT */
334             "Trans2_qfsinfo",           /* PR_VALUE_TRANS2_QFSINFO */
335             "Trans2_setfsinfo",         /* PR_VALUE_TRANS2_SETFSINFO */
336             "Trans2_qpathinfo",         /* PR_VALUE_TRANS2_QPATHINFO */
337             "Trans2_setpathinfo",       /* PR_VALUE_TRANS2_SETPATHINFO */
338             "Trans2_qfileinfo",         /* PR_VALUE_TRANS2_QFILEINFO */
339             "Trans2_setfileinfo",       /* PR_VALUE_TRANS2_SETFILEINFO */
340             "Trans2_fsctl",             /* PR_VALUE_TRANS2_FSCTL */
341             "Trans2_ioctl",             /* PR_VALUE_TRANS2_IOCTL */
342             "Trans2_findnotifyfirst",   /* PR_VALUE_TRANS2_FINDNOTIFYFIRST */
343             "Trans2_findnotifynext",    /* PR_VALUE_TRANS2_FINDNOTIFYNEXT */
344             "Trans2_mkdir",             /* PR_VALUE_TRANS2_MKDIR */
345             "Trans2_session_setup",     /* PR_VALUE_TRANS2_SESSION_SETUP */
346             "Trans2_get_dfs_referral",  /* PR_VALUE_TRANS2_GET_DFS_REFERRAL */
347             "Trans2_report_dfs_inconsistancy",  /* PR_VALUE_TRANS2_REPORT_DFS_INCONSISTANCY */
348             "NT_transact_create",       /* PR_VALUE_NT_TRANSACT_CREATE */
349             "NT_transact_ioctl",        /* PR_VALUE_NT_TRANSACT_IOCTL */
350             "NT_transact_set_security_desc",    /* PR_VALUE_NT_TRANSACT_SET_SECURITY_DESC */
351             "NT_transact_notify_change",/* PR_VALUE_NT_TRANSACT_NOTIFY_CHANGE */
352             "NT_transact_rename",       /* PR_VALUE_NT_TRANSACT_RENAME */
353             "NT_transact_query_security_desc",  /* PR_VALUE_NT_TRANSACT_QUERY_SECURITY_DESC */
354             "NT_transact_get_user_quota",/* PR_VALUE_NT_TRANSACT_GET_USER_QUOTA */
355             "NT_transact_set_user_quota",/* PR_VALUE_NT_TRANSACT_SET_USER_QUOTA */
356             "get_nt_acl",               /* PR_VALUE_GET_NT_ACL */
357             "fget_nt_acl",              /* PR_VALUE_FGET_NT_ACL */
358             "fset_nt_acl",              /* PR_VALUE_FSET_NT_ACL */
359             "chmod_acl",                /* PR_VALUE_CHMOD_ACL */
360             "fchmod_acl",               /* PR_VALUE_FCHMOD_ACL */
361             "name_release",             /* PR_VALUE_NAME_RELEASE */
362             "name_refresh",             /* PR_VALUE_NAME_REFRESH */
363             "name_registration",        /* PR_VALUE_NAME_REGISTRATION */
364             "node_status",              /* PR_VALUE_NODE_STATUS */
365             "name_query",               /* PR_VALUE_NAME_QUERY */
366             "host_announce",            /* PR_VALUE_HOST_ANNOUNCE */
367             "workgroup_announce",       /* PR_VALUE_WORKGROUP_ANNOUNCE */
368             "local_master_announce",    /* PR_VALUE_LOCAL_MASTER_ANNOUNCE */
369             "master_browser_announce",  /* PR_VALUE_MASTER_BROWSER_ANNOUNCE */
370             "lm_host_announce",         /* PR_VALUE_LM_HOST_ANNOUNCE */
371             "get_backup_list",          /* PR_VALUE_GET_BACKUP_LIST */
372             "reset_browser",            /* PR_VALUE_RESET_BROWSER */
373             "announce_request",         /* PR_VALUE_ANNOUNCE_REQUEST */
374             "lm_announce_request",      /* PR_VALUE_LM_ANNOUNCE_REQUEST */
375             "domain_logon",             /* PR_VALUE_DOMAIN_LOGON */
376             "sync_browse_lists",        /* PR_VALUE_SYNC_BROWSE_LISTS */
377             "run_elections",            /* PR_VALUE_RUN_ELECTIONS */
378             "election",                 /* PR_VALUE_ELECTION */
379             "smb2_negprot",             /* PR_VALUE_SMB2_NEGPROT */
380             "smb2_sesssetup",           /* PR_VALUE_SMB2_SESSETUP */
381             "smb2_logoff",              /* PR_VALUE_SMB2_LOGOFF */
382             "smb2_tcon",                /* PR_VALUE_SMB2_TCON */
383             "smb2_tdis",                /* PR_VALUE_SMB2_TDIS */
384             "smb2_create",              /* PR_VALUE_SMB2_CREATE */
385             "smb2_close",               /* PR_VALUE_SMB2_CLOSE */
386             "smb2_flush",               /* PR_VALUE_SMB2_FLUSH */
387             "smb2_read",                /* PR_VALUE_SMB2_READ */
388             "smb2_write",               /* PR_VALUE_SMB2_WRITE */
389             "smb2_lock",                /* PR_VALUE_SMB2_LOCK */
390             "smb2_ioctl",               /* PR_VALUE_SMB2_IOCTL */
391             "smb2_cancel",              /* PR_VALUE_SMB2_CANCEL */
392             "smb2_keepalive",           /* PR_VALUE_SMB2_KEEPALIVE */
393             "smb2_find",                /* PR_VALUE_SMB2_FIND */
394             "smb2_notify",              /* PR_VALUE_SMB2_NOTIFY */
395             "smb2_getinfo",             /* PR_VALUE_SMB2_GETINFO */
396             "smb2_setinfo"              /* PR_VALUE_SMB2_SETINFO */
397             "smb2_break",               /* PR_VALUE_SMB2_BREAK */
398             "" /* PR_VALUE_MAX */
399         };
400
401         SMB_ASSERT(val >= 0);
402         SMB_ASSERT(val < PR_VALUE_MAX);
403         return valnames[val];
404 }
405
406 #endif /* WITH_PROFILE */