declare dbf in one spot
[kamenim/samba.git] / source3 / utils / status.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    status reporting
5    Copyright (C) Andrew Tridgell 1994-1998
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21    Revision History:
22
23    12 aug 96: Erik.Devriendt@te6.siemens.be
24    added support for shared memory implementation of share mode locking
25
26    21-Jul-1998: rsharpe@ns.aus.com (Richard Sharpe)
27    Added -L (locks only) -S (shares only) flags and code
28
29 */
30
31 /*
32  * This program reports current SMB connections
33  */
34
35 #define NO_SYSLOG
36
37 #include "includes.h"
38
39 struct session_record{
40   pid_t pid;
41   uid_t uid;
42   char machine[31];
43   time_t start;
44   struct session_record *next;
45 } *srecs;
46
47 extern int DEBUGLEVEL;
48
49 static pstring Ucrit_username = "";                   /* added by OH */
50 static pid_t    Ucrit_pid[100];  /* Ugly !!! */        /* added by OH */
51 static int            Ucrit_MaxPid=0;                        /* added by OH */
52 static unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
53 static int verbose, brief;
54 static int            shares_only = 0;            /* Added by RJS */
55 static int            locks_only  = 0;            /* Added by RJS */
56 static BOOL processes_only=False;
57 static int show_brl;
58
59 /* we need these because we link to locking*.o */
60  void become_root(void) {}
61  void unbecome_root(void) {}
62
63
64 /* added by OH */
65 static void Ucrit_addUsername(char *username)
66 {
67         pstrcpy(Ucrit_username, username);
68         if(strlen(Ucrit_username) > 0)
69                 Ucrit_IsActive = 1;
70 }
71
72 static unsigned int Ucrit_checkUsername(char *username)
73 {
74         if ( !Ucrit_IsActive) return 1;
75         if (strcmp(Ucrit_username,username) ==0) return 1;
76         return 0;
77 }
78
79 static unsigned int Ucrit_checkPid(pid_t pid)
80 {
81         int i;
82         if ( !Ucrit_IsActive) return 1;
83         for (i=0;i<Ucrit_MaxPid;i++)
84                 if( pid == Ucrit_pid[i] ) return 1;
85         return 0;
86 }
87
88
89 static void print_share_mode(share_mode_entry *e, char *fname)
90 {
91         static int count;
92         if (count==0) {
93                 d_printf("Locked files:\n");
94                 d_printf("Pid    DenyMode   R/W        Oplock           Name\n");
95                 d_printf("--------------------------------------------------\n");
96         }
97         count++;
98
99         if (Ucrit_checkPid(e->pid)) {
100           d_printf("%-5d  ",(int)e->pid);
101           switch (GET_DENY_MODE(e->share_mode)) {
102           case DENY_NONE: d_printf("DENY_NONE  "); break;
103           case DENY_ALL:  d_printf("DENY_ALL   "); break;
104           case DENY_DOS:  d_printf("DENY_DOS   "); break;
105           case DENY_READ: d_printf("DENY_READ  "); break;
106           case DENY_WRITE:printf("DENY_WRITE "); break;
107           case DENY_FCB:  d_printf("DENY_FCB "); break;
108           }
109           switch (e->share_mode&0xF) {
110           case 0: d_printf("RDONLY     "); break;
111           case 1: d_printf("WRONLY     "); break;
112           case 2: d_printf("RDWR       "); break;
113           }
114
115           if((e->op_type & 
116              (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
117               (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
118                 d_printf("EXCLUSIVE+BATCH ");
119           else if (e->op_type & EXCLUSIVE_OPLOCK)
120                 d_printf("EXCLUSIVE       ");
121           else if (e->op_type & BATCH_OPLOCK)
122                 d_printf("BATCH           ");
123           else if (e->op_type & LEVEL_II_OPLOCK)
124                 d_printf("LEVEL_II        ");
125           else
126                 d_printf("NONE            ");
127
128           d_printf(" %s   %s",fname,
129              asctime(LocalTime((time_t *)&e->time.tv_sec)));
130         }
131 }
132
133 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid, 
134                       enum brl_type lock_type,
135                       br_off start, br_off size)
136 {
137         static int count;
138         if (count==0) {
139                 d_printf("Byte range locks:\n");
140                 d_printf("   Pid     dev:inode  R/W      start        size\n");
141                 d_printf("------------------------------------------------\n");
142         }
143         count++;
144
145         d_printf("%6d   %05x:%05x    %s  %9.0f   %9.0f\n", 
146                (int)pid, (int)dev, (int)ino, 
147                lock_type==READ_LOCK?"R":"W",
148                (double)start, (double)size);
149 }
150
151
152 /*******************************************************************
153  dump the elements of the profile structure
154   ******************************************************************/
155 static int profile_dump(void)
156 {
157         if (!profile_setup(True)) {
158                 fprintf(stderr,"Failed to initialise profile memory\n");
159                 return -1;
160         }
161
162         d_printf("smb_count:                      %u\n", profile_p->smb_count);
163         d_printf("uid_changes:                    %u\n", profile_p->uid_changes);
164         d_printf("************************ System Calls ****************************\n");
165         d_printf("opendir_count:                  %u\n", profile_p->syscall_opendir_count);
166         d_printf("opendir_time:                   %u\n", profile_p->syscall_opendir_time);
167         d_printf("readdir_count:                  %u\n", profile_p->syscall_readdir_count);
168         d_printf("readdir_time:                   %u\n", profile_p->syscall_readdir_time);
169         d_printf("mkdir_count:                    %u\n", profile_p->syscall_mkdir_count);
170         d_printf("mkdir_time:                     %u\n", profile_p->syscall_mkdir_time);
171         d_printf("rmdir_count:                    %u\n", profile_p->syscall_rmdir_count);
172         d_printf("rmdir_time:                     %u\n", profile_p->syscall_rmdir_time);
173         d_printf("closedir_count:                 %u\n", profile_p->syscall_closedir_count);
174         d_printf("closedir_time:                  %u\n", profile_p->syscall_closedir_time);
175         d_printf("open_count:                     %u\n", profile_p->syscall_open_count);
176         d_printf("open_time:                      %u\n", profile_p->syscall_open_time);
177         d_printf("close_count:                    %u\n", profile_p->syscall_close_count);
178         d_printf("close_time:                     %u\n", profile_p->syscall_close_time);
179         d_printf("read_count:                     %u\n", profile_p->syscall_read_count);
180         d_printf("read_time:                      %u\n", profile_p->syscall_read_time);
181         d_printf("read_bytes:                     %u\n", profile_p->syscall_read_bytes);
182         d_printf("write_count:                    %u\n", profile_p->syscall_write_count);
183         d_printf("write_time:                     %u\n", profile_p->syscall_write_time);
184         d_printf("write_bytes:                    %u\n", profile_p->syscall_write_bytes);
185         d_printf("lseek_count:                    %u\n", profile_p->syscall_lseek_count);
186         d_printf("lseek_time:                     %u\n", profile_p->syscall_lseek_time);
187         d_printf("rename_count:                   %u\n", profile_p->syscall_rename_count);
188         d_printf("rename_time:                    %u\n", profile_p->syscall_rename_time);
189         d_printf("fsync_count:                    %u\n", profile_p->syscall_fsync_count);
190         d_printf("fsync_time:                     %u\n", profile_p->syscall_fsync_time);
191         d_printf("stat_count:                     %u\n", profile_p->syscall_stat_count);
192         d_printf("stat_time:                      %u\n", profile_p->syscall_stat_time);
193         d_printf("fstat_count:                    %u\n", profile_p->syscall_fstat_count);
194         d_printf("fstat_time:                     %u\n", profile_p->syscall_fstat_time);
195         d_printf("lstat_count:                    %u\n", profile_p->syscall_lstat_count);
196         d_printf("lstat_time:                     %u\n", profile_p->syscall_lstat_time);
197         d_printf("unlink_count:                   %u\n", profile_p->syscall_unlink_count);
198         d_printf("unlink_time:                    %u\n", profile_p->syscall_unlink_time);
199         d_printf("chmod_count:                    %u\n", profile_p->syscall_chmod_count);
200         d_printf("chmod_time:                     %u\n", profile_p->syscall_chmod_time);
201         d_printf("fchmod_count:                   %u\n", profile_p->syscall_fchmod_count);
202         d_printf("fchmod_time:                    %u\n", profile_p->syscall_fchmod_time);
203         d_printf("chown_count:                    %u\n", profile_p->syscall_chown_count);
204         d_printf("chown_time:                     %u\n", profile_p->syscall_chown_time);
205         d_printf("fchown_count:                   %u\n", profile_p->syscall_fchown_count);
206         d_printf("fchown_time:                    %u\n", profile_p->syscall_fchown_time);
207         d_printf("chdir_count:                    %u\n", profile_p->syscall_chdir_count);
208         d_printf("chdir_time:                     %u\n", profile_p->syscall_chdir_time);
209         d_printf("getwd_count:                    %u\n", profile_p->syscall_getwd_count);
210         d_printf("getwd_time:                     %u\n", profile_p->syscall_getwd_time);
211         d_printf("utime_count:                    %u\n", profile_p->syscall_utime_count);
212         d_printf("utime_time:                     %u\n", profile_p->syscall_utime_time);
213         d_printf("ftruncate_count:                %u\n", profile_p->syscall_ftruncate_count);
214         d_printf("ftruncate_time:                 %u\n", profile_p->syscall_ftruncate_time);
215         d_printf("fcntl_lock_count:               %u\n", profile_p->syscall_fcntl_lock_count);
216         d_printf("fcntl_lock_time:                %u\n", profile_p->syscall_fcntl_lock_time);
217         d_printf("readlink_count:                 %u\n", profile_p->syscall_readlink_count);
218         d_printf("readlink_time:                  %u\n", profile_p->syscall_readlink_time);
219         d_printf("symlink_count:                  %u\n", profile_p->syscall_symlink_count);
220         d_printf("symlink_time:                   %u\n", profile_p->syscall_symlink_time);
221         d_printf("************************ Statcache *******************************\n");
222         d_printf("lookups:                        %u\n", profile_p->statcache_lookups);
223         d_printf("misses:                         %u\n", profile_p->statcache_misses);
224         d_printf("hits:                           %u\n", profile_p->statcache_hits);
225         d_printf("************************ Writecache ******************************\n");
226         d_printf("read_hits:                      %u\n", profile_p->writecache_read_hits);
227         d_printf("abutted_writes:                 %u\n", profile_p->writecache_abutted_writes);
228         d_printf("total_writes:                   %u\n", profile_p->writecache_total_writes);
229         d_printf("non_oplock_writes:              %u\n", profile_p->writecache_non_oplock_writes);
230         d_printf("direct_writes:                  %u\n", profile_p->writecache_direct_writes);
231         d_printf("init_writes:                    %u\n", profile_p->writecache_init_writes);
232         d_printf("flushed_writes[SEEK]:           %u\n", profile_p->writecache_flushed_writes[SEEK_FLUSH]);
233         d_printf("flushed_writes[READ]:           %u\n", profile_p->writecache_flushed_writes[READ_FLUSH]);
234         d_printf("flushed_writes[WRITE]:          %u\n", profile_p->writecache_flushed_writes[WRITE_FLUSH]);
235         d_printf("flushed_writes[READRAW]:        %u\n", profile_p->writecache_flushed_writes[READRAW_FLUSH]);
236         d_printf("flushed_writes[OPLOCK_RELEASE]: %u\n", profile_p->writecache_flushed_writes[OPLOCK_RELEASE_FLUSH]);
237         d_printf("flushed_writes[CLOSE]:          %u\n", profile_p->writecache_flushed_writes[CLOSE_FLUSH]);
238         d_printf("flushed_writes[SYNC]:           %u\n", profile_p->writecache_flushed_writes[SYNC_FLUSH]);
239         d_printf("flushed_writes[SIZECHANGE]:     %u\n", profile_p->writecache_flushed_writes[SIZECHANGE_FLUSH]);
240         d_printf("num_perfect_writes:             %u\n", profile_p->writecache_num_perfect_writes);
241         d_printf("num_write_caches:               %u\n", profile_p->writecache_num_write_caches);
242         d_printf("allocated_write_caches:         %u\n", profile_p->writecache_allocated_write_caches);
243         d_printf("************************ SMB Calls *******************************\n");
244         d_printf("mkdir_count:                    %u\n", profile_p->SMBmkdir_count);
245         d_printf("mkdir_time:                     %u\n", profile_p->SMBmkdir_time);
246         d_printf("rmdir_count:                    %u\n", profile_p->SMBrmdir_count);
247         d_printf("rmdir_time:                     %u\n", profile_p->SMBrmdir_time);
248         d_printf("open_count:                     %u\n", profile_p->SMBopen_count);
249         d_printf("open_time:                      %u\n", profile_p->SMBopen_time);
250         d_printf("create_count:                   %u\n", profile_p->SMBcreate_count);
251         d_printf("create_time:                    %u\n", profile_p->SMBcreate_time);
252         d_printf("close_count:                    %u\n", profile_p->SMBclose_count);
253         d_printf("close_time:                     %u\n", profile_p->SMBclose_time);
254         d_printf("flush_count:                    %u\n", profile_p->SMBflush_count);
255         d_printf("flush_time:                     %u\n", profile_p->SMBflush_time);
256         d_printf("unlink_count:                   %u\n", profile_p->SMBunlink_count);
257         d_printf("unlink_time:                    %u\n", profile_p->SMBunlink_time);
258         d_printf("mv_count:                       %u\n", profile_p->SMBmv_count);
259         d_printf("mv_time:                        %u\n", profile_p->SMBmv_time);
260         d_printf("getatr_count:                   %u\n", profile_p->SMBgetatr_count);
261         d_printf("getatr_time:                    %u\n", profile_p->SMBgetatr_time);
262         d_printf("setatr_count:                   %u\n", profile_p->SMBsetatr_count);
263         d_printf("setatr_time:                    %u\n", profile_p->SMBsetatr_time);
264         d_printf("read_count:                     %u\n", profile_p->SMBread_count);
265         d_printf("read_time:                      %u\n", profile_p->SMBread_time);
266         d_printf("write_count:                    %u\n", profile_p->SMBwrite_count);
267         d_printf("write_time:                     %u\n", profile_p->SMBwrite_time);
268         d_printf("lock_count:                     %u\n", profile_p->SMBlock_count);
269         d_printf("lock_time:                      %u\n", profile_p->SMBlock_time);
270         d_printf("unlock_count:                   %u\n", profile_p->SMBunlock_count);
271         d_printf("unlock_time:                    %u\n", profile_p->SMBunlock_time);
272         d_printf("ctemp_count:                    %u\n", profile_p->SMBctemp_count);
273         d_printf("ctemp_time:                     %u\n", profile_p->SMBctemp_time);
274         d_printf("mknew_count:                    %u\n", profile_p->SMBmknew_count);
275         d_printf("mknew_time:                     %u\n", profile_p->SMBmknew_time);
276         d_printf("chkpth_count:                   %u\n", profile_p->SMBchkpth_count);
277         d_printf("chkpth_time:                    %u\n", profile_p->SMBchkpth_time);
278         d_printf("exit_count:                     %u\n", profile_p->SMBexit_count);
279         d_printf("exit_time:                      %u\n", profile_p->SMBexit_time);
280         d_printf("lseek_count:                    %u\n", profile_p->SMBlseek_count);
281         d_printf("lseek_time:                     %u\n", profile_p->SMBlseek_time);
282         d_printf("lockread_count:                 %u\n", profile_p->SMBlockread_count);
283         d_printf("lockread_time:                  %u\n", profile_p->SMBlockread_time);
284         d_printf("writeunlock_count:              %u\n", profile_p->SMBwriteunlock_count);
285         d_printf("writeunlock_time:               %u\n", profile_p->SMBwriteunlock_time);
286         d_printf("readbraw_count:                 %u\n", profile_p->SMBreadbraw_count);
287         d_printf("readbraw_time:                  %u\n", profile_p->SMBreadbraw_time);
288         d_printf("readBmpx_count:                 %u\n", profile_p->SMBreadBmpx_count);
289         d_printf("readBmpx_time:                  %u\n", profile_p->SMBreadBmpx_time);
290         d_printf("readBs_count:                   %u\n", profile_p->SMBreadBs_count);
291         d_printf("readBs_time:                    %u\n", profile_p->SMBreadBs_time);
292         d_printf("writebraw_count:                %u\n", profile_p->SMBwritebraw_count);
293         d_printf("writebraw_time:                 %u\n", profile_p->SMBwritebraw_time);
294         d_printf("writeBmpx_count:                %u\n", profile_p->SMBwriteBmpx_count);
295         d_printf("writeBmpx_time:                 %u\n", profile_p->SMBwriteBmpx_time);
296         d_printf("writeBs_count:                  %u\n", profile_p->SMBwriteBs_count);
297         d_printf("writeBs_time:                   %u\n", profile_p->SMBwriteBs_time);
298         d_printf("writec_count:                   %u\n", profile_p->SMBwritec_count);
299         d_printf("writec_time:                    %u\n", profile_p->SMBwritec_time);
300         d_printf("setattrE_count:                 %u\n", profile_p->SMBsetattrE_count);
301         d_printf("setattrE_time:                  %u\n", profile_p->SMBsetattrE_time);
302         d_printf("getattrE_count:                 %u\n", profile_p->SMBgetattrE_count);
303         d_printf("getattrE_time:                  %u\n", profile_p->SMBgetattrE_time);
304         d_printf("lockingX_count:                 %u\n", profile_p->SMBlockingX_count);
305         d_printf("lockingX_time:                  %u\n", profile_p->SMBlockingX_time);
306         d_printf("trans_count:                    %u\n", profile_p->SMBtrans_count);
307         d_printf("trans_time:                     %u\n", profile_p->SMBtrans_time);
308         d_printf("transs_count:                   %u\n", profile_p->SMBtranss_count);
309         d_printf("transs_time:                    %u\n", profile_p->SMBtranss_time);
310         d_printf("ioctl_count:                    %u\n", profile_p->SMBioctl_count);
311         d_printf("ioctl_time:                     %u\n", profile_p->SMBioctl_time);
312         d_printf("ioctls_count:                   %u\n", profile_p->SMBioctls_count);
313         d_printf("ioctls_time:                    %u\n", profile_p->SMBioctls_time);
314         d_printf("copy_count:                     %u\n", profile_p->SMBcopy_count);
315         d_printf("copy_time:                      %u\n", profile_p->SMBcopy_time);
316         d_printf("move_count:                     %u\n", profile_p->SMBmove_count);
317         d_printf("move_time:                      %u\n", profile_p->SMBmove_time);
318         d_printf("echo_count:                     %u\n", profile_p->SMBecho_count);
319         d_printf("echo_time:                      %u\n", profile_p->SMBecho_time);
320         d_printf("writeclose_count:               %u\n", profile_p->SMBwriteclose_count);
321         d_printf("writeclose_time:                %u\n", profile_p->SMBwriteclose_time);
322         d_printf("openX_count:                    %u\n", profile_p->SMBopenX_count);
323         d_printf("openX_time:                     %u\n", profile_p->SMBopenX_time);
324         d_printf("readX_count:                    %u\n", profile_p->SMBreadX_count);
325         d_printf("readX_time:                     %u\n", profile_p->SMBreadX_time);
326         d_printf("writeX_count:                   %u\n", profile_p->SMBwriteX_count);
327         d_printf("writeX_time:                    %u\n", profile_p->SMBwriteX_time);
328         d_printf("trans2_count:                   %u\n", profile_p->SMBtrans2_count);
329         d_printf("trans2_time:                    %u\n", profile_p->SMBtrans2_time);
330         d_printf("transs2_count:                  %u\n", profile_p->SMBtranss2_count);
331         d_printf("transs2_time:                   %u\n", profile_p->SMBtranss2_time);
332         d_printf("findclose_count:                %u\n", profile_p->SMBfindclose_count);
333         d_printf("findclose_time:                 %u\n", profile_p->SMBfindclose_time);
334         d_printf("findnclose_count:               %u\n", profile_p->SMBfindnclose_count);
335         d_printf("findnclose_time:                %u\n", profile_p->SMBfindnclose_time);
336         d_printf("tcon_count:                     %u\n", profile_p->SMBtcon_count);
337         d_printf("tcon_time:                      %u\n", profile_p->SMBtcon_time);
338         d_printf("tdis_count:                     %u\n", profile_p->SMBtdis_count);
339         d_printf("tdis_time:                      %u\n", profile_p->SMBtdis_time);
340         d_printf("negprot_count:                  %u\n", profile_p->SMBnegprot_count);
341         d_printf("negprot_time:                   %u\n", profile_p->SMBnegprot_time);
342         d_printf("sesssetupX_count:               %u\n", profile_p->SMBsesssetupX_count);
343         d_printf("sesssetupX_time:                %u\n", profile_p->SMBsesssetupX_time);
344         d_printf("ulogoffX_count:                 %u\n", profile_p->SMBulogoffX_count);
345         d_printf("ulogoffX_time:                  %u\n", profile_p->SMBulogoffX_time);
346         d_printf("tconX_count:                    %u\n", profile_p->SMBtconX_count);
347         d_printf("tconX_time:                     %u\n", profile_p->SMBtconX_time);
348         d_printf("dskattr_count:                  %u\n", profile_p->SMBdskattr_count);
349         d_printf("dskattr_time:                   %u\n", profile_p->SMBdskattr_time);
350         d_printf("search_count:                   %u\n", profile_p->SMBsearch_count);
351         d_printf("search_time:                    %u\n", profile_p->SMBsearch_time);
352         d_printf("ffirst_count:                   %u\n", profile_p->SMBffirst_count);
353         d_printf("ffirst_time:                    %u\n", profile_p->SMBffirst_time);
354         d_printf("funique_count:                  %u\n", profile_p->SMBfunique_count);
355         d_printf("funique_time:                   %u\n", profile_p->SMBfunique_time);
356         d_printf("fclose_count:                   %u\n", profile_p->SMBfclose_count);
357         d_printf("fclose_time:                    %u\n", profile_p->SMBfclose_time);
358         d_printf("nttrans_count:                  %u\n", profile_p->SMBnttrans_count);
359         d_printf("nttrans_time:                   %u\n", profile_p->SMBnttrans_time);
360         d_printf("nttranss_count:                 %u\n", profile_p->SMBnttranss_count);
361         d_printf("nttranss_time:                  %u\n", profile_p->SMBnttranss_time);
362         d_printf("ntcreateX_count:                %u\n", profile_p->SMBntcreateX_count);
363         d_printf("ntcreateX_time:                 %u\n", profile_p->SMBntcreateX_time);
364         d_printf("ntcancel_count:                 %u\n", profile_p->SMBntcancel_count);
365         d_printf("ntcancel_time:                  %u\n", profile_p->SMBntcancel_time);
366         d_printf("splopen_count:                  %u\n", profile_p->SMBsplopen_count);
367         d_printf("splopen_time:                   %u\n", profile_p->SMBsplopen_time);
368         d_printf("splwr_count:                    %u\n", profile_p->SMBsplwr_count);
369         d_printf("splwr_time:                     %u\n", profile_p->SMBsplwr_time);
370         d_printf("splclose_count:                 %u\n", profile_p->SMBsplclose_count);
371         d_printf("splclose_time:                  %u\n", profile_p->SMBsplclose_time);
372         d_printf("splretq_count:                  %u\n", profile_p->SMBsplretq_count);
373         d_printf("splretq_time:                   %u\n", profile_p->SMBsplretq_time);
374         d_printf("sends_count:                    %u\n", profile_p->SMBsends_count);
375         d_printf("sends_time:                     %u\n", profile_p->SMBsends_time);
376         d_printf("sendb_count:                    %u\n", profile_p->SMBsendb_count);
377         d_printf("sendb_time:                     %u\n", profile_p->SMBsendb_time);
378         d_printf("fwdname_count:                  %u\n", profile_p->SMBfwdname_count);
379         d_printf("fwdname_time:                   %u\n", profile_p->SMBfwdname_time);
380         d_printf("cancelf_count:                  %u\n", profile_p->SMBcancelf_count);
381         d_printf("cancelf_time:                   %u\n", profile_p->SMBcancelf_time);
382         d_printf("getmac_count:                   %u\n", profile_p->SMBgetmac_count);
383         d_printf("getmac_time:                    %u\n", profile_p->SMBgetmac_time);
384         d_printf("sendstrt_count:                 %u\n", profile_p->SMBsendstrt_count);
385         d_printf("sendstrt_time:                  %u\n", profile_p->SMBsendstrt_time);
386         d_printf("sendend_count:                  %u\n", profile_p->SMBsendend_count);
387         d_printf("sendend_time:                   %u\n", profile_p->SMBsendend_time);
388         d_printf("sendtxt_count:                  %u\n", profile_p->SMBsendtxt_count);
389         d_printf("sendtxt_time:                   %u\n", profile_p->SMBsendtxt_time);
390         d_printf("invalid_count:                  %u\n", profile_p->SMBinvalid_count);
391         d_printf("invalid_time:                   %u\n", profile_p->SMBinvalid_time);
392         d_printf("************************ Pathworks Calls *************************\n");
393         d_printf("setdir_count:                   %u\n", profile_p->pathworks_setdir_count);
394         d_printf("setdir_time:                    %u\n", profile_p->pathworks_setdir_time);
395         d_printf("************************ Trans2 Calls ****************************\n");
396         d_printf("open_count:                     %u\n", profile_p->Trans2_open_count);
397         d_printf("open_time:                      %u\n", profile_p->Trans2_open_time);
398         d_printf("findfirst_count:                %u\n", profile_p->Trans2_findfirst_count);
399         d_printf("findfirst_time:                 %u\n", profile_p->Trans2_findfirst_time);
400         d_printf("findnext_count:                 %u\n", profile_p->Trans2_findnext_count);
401         d_printf("findnext_time:                  %u\n", profile_p->Trans2_findnext_time);
402         d_printf("qfsinfo_count:                  %u\n", profile_p->Trans2_qfsinfo_count);
403         d_printf("qfsinfo_time:                   %u\n", profile_p->Trans2_qfsinfo_time);
404         d_printf("setfsinfo_count:                %u\n", profile_p->Trans2_setfsinfo_count);
405         d_printf("setfsinfo_time:                 %u\n", profile_p->Trans2_setfsinfo_time);
406         d_printf("qpathinfo_count:                %u\n", profile_p->Trans2_qpathinfo_count);
407         d_printf("qpathinfo_time:                 %u\n", profile_p->Trans2_qpathinfo_time);
408         d_printf("setpathinfo_count:              %u\n", profile_p->Trans2_setpathinfo_count);
409         d_printf("setpathinfo_time:               %u\n", profile_p->Trans2_setpathinfo_time);
410         d_printf("qfileinfo_count:                %u\n", profile_p->Trans2_qfileinfo_count);
411         d_printf("qfileinfo_time:                 %u\n", profile_p->Trans2_qfileinfo_time);
412         d_printf("setfileinfo_count:              %u\n", profile_p->Trans2_setfileinfo_count);
413         d_printf("setfileinfo_time:               %u\n", profile_p->Trans2_setfileinfo_time);
414         d_printf("fsctl_count:                    %u\n", profile_p->Trans2_fsctl_count);
415         d_printf("fsctl_time:                     %u\n", profile_p->Trans2_fsctl_time);
416         d_printf("ioctl_count:                    %u\n", profile_p->Trans2_ioctl_count);
417         d_printf("ioctl_time:                     %u\n", profile_p->Trans2_ioctl_time);
418         d_printf("findnotifyfirst_count:          %u\n", profile_p->Trans2_findnotifyfirst_count);
419         d_printf("findnotifyfirst_time:           %u\n", profile_p->Trans2_findnotifyfirst_time);
420         d_printf("findnotifynext_count:           %u\n", profile_p->Trans2_findnotifynext_count);
421         d_printf("findnotifynext_time:            %u\n", profile_p->Trans2_findnotifynext_time);
422         d_printf("mkdir_count:                    %u\n", profile_p->Trans2_mkdir_count);
423         d_printf("mkdir_time:                     %u\n", profile_p->Trans2_mkdir_time);
424         d_printf("session_setup_count:            %u\n", profile_p->Trans2_session_setup_count);
425         d_printf("session_setup_time:             %u\n", profile_p->Trans2_session_setup_time);
426         d_printf("get_dfs_referral_count:         %u\n", profile_p->Trans2_get_dfs_referral_count);
427         d_printf("get_dfs_referral_time:          %u\n", profile_p->Trans2_get_dfs_referral_time);
428         d_printf("report_dfs_inconsistancy_count: %u\n", profile_p->Trans2_report_dfs_inconsistancy_count);
429         d_printf("report_dfs_inconsistancy_time:  %u\n", profile_p->Trans2_report_dfs_inconsistancy_time);
430         d_printf("************************ NT Transact Calls ***********************\n");
431         d_printf("create_count:                   %u\n", profile_p->NT_transact_create_count);
432         d_printf("create_time:                    %u\n", profile_p->NT_transact_create_time);
433         d_printf("ioctl_count:                    %u\n", profile_p->NT_transact_ioctl_count);
434         d_printf("ioctl_time:                     %u\n", profile_p->NT_transact_ioctl_time);
435         d_printf("set_security_desc_count:        %u\n", profile_p->NT_transact_set_security_desc_count);
436         d_printf("set_security_desc_time:         %u\n", profile_p->NT_transact_set_security_desc_time);
437         d_printf("notify_change_count:            %u\n", profile_p->NT_transact_notify_change_count);
438         d_printf("notify_change_time:             %u\n", profile_p->NT_transact_notify_change_time);
439         d_printf("rename_count:                   %u\n", profile_p->NT_transact_rename_count);
440         d_printf("rename_time:                    %u\n", profile_p->NT_transact_rename_time);
441         d_printf("query_security_desc_count:      %u\n", profile_p->NT_transact_query_security_desc_count);
442         d_printf("query_security_desc_time:       %u\n", profile_p->NT_transact_query_security_desc_time);
443         d_printf("************************ ACL Calls *******************************\n");
444         d_printf("get_nt_acl_count:               %u\n", profile_p->get_nt_acl_count);
445         d_printf("get_nt_acl_time:                %u\n", profile_p->get_nt_acl_time);
446         d_printf("fget_nt_acl_count:              %u\n", profile_p->fget_nt_acl_count);
447         d_printf("fget_nt_acl_time:               %u\n", profile_p->fget_nt_acl_time);
448         d_printf("set_nt_acl_count:               %u\n", profile_p->set_nt_acl_count);
449         d_printf("set_nt_acl_time:                %u\n", profile_p->set_nt_acl_time);
450         d_printf("fset_nt_acl_count:              %u\n", profile_p->fset_nt_acl_count);
451         d_printf("fset_nt_acl_time:               %u\n", profile_p->fset_nt_acl_time);
452         d_printf("chmod_acl_count:                %u\n", profile_p->chmod_acl_count);
453         d_printf("chmod_acl_time:                 %u\n", profile_p->chmod_acl_time);
454         d_printf("fchmod_acl_count:               %u\n", profile_p->fchmod_acl_count);
455         d_printf("fchmod_acl_time:                %u\n", profile_p->fchmod_acl_time);
456         d_printf("************************ NMBD Calls ****************************\n");
457         d_printf("name_release_count:             %u\n", profile_p->name_release_count);
458         d_printf("name_release_time:              %u\n", profile_p->name_release_time);
459         d_printf("name_refresh_count:             %u\n", profile_p->name_refresh_count);
460         d_printf("name_refresh_time:              %u\n", profile_p->name_refresh_time);
461         d_printf("name_registration_count:        %u\n", profile_p->name_registration_count);
462         d_printf("name_registration_time:         %u\n", profile_p->name_registration_time);
463         d_printf("node_status_count:              %u\n", profile_p->node_status_count);
464         d_printf("node_status_time:               %u\n", profile_p->node_status_time);
465         d_printf("name_query_count:               %u\n", profile_p->name_query_count);
466         d_printf("name_query_time:                %u\n", profile_p->name_query_time);
467         d_printf("host_announce_count:            %u\n", profile_p->host_announce_count);
468         d_printf("host_announce_time:             %u\n", profile_p->host_announce_time);
469         d_printf("workgroup_announce_count:       %u\n", profile_p->workgroup_announce_count);
470         d_printf("workgroup_announce_time:        %u\n", profile_p->workgroup_announce_time);
471         d_printf("local_master_announce_count:    %u\n", profile_p->local_master_announce_count);
472         d_printf("local_master_announce_time:     %u\n", profile_p->local_master_announce_time);
473         d_printf("master_browser_announce_count:  %u\n", profile_p->master_browser_announce_count);
474         d_printf("master_browser_announce_time:   %u\n", profile_p->master_browser_announce_time);
475         d_printf("lm_host_announce_count:         %u\n", profile_p->lm_host_announce_count);
476         d_printf("lm_host_announce_time:          %u\n", profile_p->lm_host_announce_time);
477         d_printf("get_backup_list_count:          %u\n", profile_p->get_backup_list_count);
478         d_printf("get_backup_list_time:           %u\n", profile_p->get_backup_list_time);
479         d_printf("reset_browser_count:            %u\n", profile_p->reset_browser_count);
480         d_printf("reset_browser_time:             %u\n", profile_p->reset_browser_time);
481         d_printf("announce_request_count:         %u\n", profile_p->announce_request_count);
482         d_printf("announce_request_time:          %u\n", profile_p->announce_request_time);
483         d_printf("lm_announce_request_count:      %u\n", profile_p->lm_announce_request_count);
484         d_printf("lm_announce_request_time:       %u\n", profile_p->lm_announce_request_time);
485         d_printf("domain_logon_count:             %u\n", profile_p->domain_logon_count);
486         d_printf("domain_logon_time:              %u\n", profile_p->domain_logon_time);
487         d_printf("sync_browse_lists_count:        %u\n", profile_p->sync_browse_lists_count);
488         d_printf("sync_browse_lists_time:         %u\n", profile_p->sync_browse_lists_time);
489         d_printf("run_elections_count:            %u\n", profile_p->run_elections_count);
490         d_printf("run_elections_time:             %u\n", profile_p->run_elections_time);
491         d_printf("election_count:                 %u\n", profile_p->election_count);
492         d_printf("election_time:                  %u\n", profile_p->election_time);
493
494         return 0;
495 }
496
497
498 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
499 {
500         struct connections_data crec;
501
502         if (dbuf.dsize != sizeof(crec))
503                 return 0;
504
505         memcpy(&crec, dbuf.dptr, sizeof(crec));
506
507         if (crec.cnum == -1)
508                 return 0;
509
510         if (!process_exists(crec.pid) || !Ucrit_checkUsername(uidtoname(crec.uid))) {
511                 return 0;
512         }
513
514         d_printf("%-10.10s   %5d   %-12s  %s",
515                crec.name,(int)crec.pid,
516                crec.machine,
517                asctime(LocalTime(&crec.start)));
518
519         return 0;
520 }
521
522 static int traverse_sessionid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
523 {
524         struct sessionid sessionid;
525
526         if (dbuf.dsize != sizeof(sessionid))
527                 return 0;
528
529         memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
530
531         if (!process_exists(sessionid.pid) || !Ucrit_checkUsername(uidtoname(sessionid.uid))) {
532                 return 0;
533         }
534
535         d_printf("%5d   %-12s  %-12s  %-12s (%s)\n",
536                (int)sessionid.pid, uidtoname(sessionid.uid), gidtoname(sessionid.gid), 
537                sessionid.remote_machine, sessionid.hostname);
538         
539         return 0;
540 }
541
542
543
544
545  int main(int argc, char *argv[])
546 {
547         pstring fname;
548         int c;
549         static pstring servicesf = CONFIGFILE;
550         extern char *optarg;
551         int profile_only = 0, new_debuglevel = -1;
552         TDB_CONTEXT *tdb;
553
554         TimeInit();
555         setup_logging(argv[0],True);
556         
557         DEBUGLEVEL = 0;
558         dbf = x_stderr;
559         
560         if (getuid() != geteuid()) {
561                 d_printf("smbstatus should not be run setuid\n");
562                 return(1);
563         }
564         
565         while ((c = getopt(argc, argv, "pvLSs:u:bPBd:")) != EOF) {
566                 switch (c) {
567                 case 'b':
568                         brief = 1;
569                         break;
570                 case 'B':
571                         show_brl = 1;
572                         break;
573                 case 'd':
574                         new_debuglevel = atoi(optarg);
575                         break;
576                         
577                 case 'v':
578                         verbose = 1;
579                         break;
580                 case 'L':
581                         locks_only = 1;
582                         break;
583                 case 'p':
584                         processes_only = 1;
585                         break;
586                 case 'P':
587                         profile_only = 1;
588                         break;
589                 case 'S':
590                         shares_only = 1;
591                         break;
592                 case 's':
593                         pstrcpy(servicesf, optarg);
594                         break;
595                 case 'u':                                      
596                         Ucrit_addUsername(optarg);             
597                         break;
598                 default:
599                         fprintf(stderr, "Usage: %s [-P] [-v] [-L] [-p] [-S] [-s configfile] [-u username] [-d debuglevel]\n", *argv);
600                         return (-1);
601                 }
602         }
603         
604         if (!lp_load(servicesf,False,False,False)) {
605                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
606                 return (-1);
607         }
608         
609         if (new_debuglevel != -1) {
610                 DEBUGLEVEL = new_debuglevel;
611         }
612
613         if (verbose) {
614                 d_printf("using configfile = %s\n", servicesf);
615         }
616         
617         if (profile_only) {
618                 return profile_dump();
619         }
620         
621         tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
622         if (!tdb) {
623                 d_printf("sessionid.tdb not initialised\n");
624         }
625
626         if (locks_only) goto locks;
627
628         d_printf("\nSamba version %s\n",VERSION);
629         d_printf("PID     Username      Group         Machine                        \n");
630         d_printf("-------------------------------------------------------------------\n");
631
632         tdb_traverse(tdb, traverse_sessionid, NULL);
633         tdb_close(tdb);
634   
635         tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
636         if (!tdb) {
637                 d_printf("connections.tdb not initialised\n");
638         }  else if (verbose) {
639                 d_printf("Opened status file %s\n", fname);
640         }
641
642         if (brief) 
643                 exit(0);
644         
645         d_printf("\nService      pid     machine       Connected at\n");
646         d_printf("-------------------------------------------------------\n");
647
648         tdb_traverse(tdb, traverse_fn1, NULL);
649         tdb_close(tdb);
650
651  locks:
652         if (processes_only) exit(0);
653
654         if (!shares_only) {
655                 int ret;
656
657                 if (!locking_init(1)) {
658                         d_printf("Can't initialise locking module - exiting\n");
659                         exit(1);
660                 }
661                 
662                 ret = share_mode_forall(print_share_mode);
663
664                 if (ret == 0) {
665                         d_printf("No locked files\n");
666                 } else if (ret == -1) {
667                         d_printf("locked file list truncated\n");
668                 }
669                 
670                 d_printf("\n");
671
672                 if (show_brl) {
673                         brl_forall(print_brl);
674                 }
675                 
676                 locking_end();
677         }
678
679         return (0);
680 }
681