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