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