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