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