~vcs-imports/samba/main

« back to all changes in this revision

Viewing changes to source/utils/status.c

  • Committer: jerry
  • Date: 2006-07-14 21:48:39 UTC
  • Revision ID: vcs-imports@canonical.com-20060714214839-586d8c489a8fcead
gutting trunk to move to svn:externals

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include "includes.h"
35
 
 
36
 
#define SMB_MAXPIDS             2048
37
 
static uid_t            Ucrit_uid = 0;               /* added by OH */
38
 
static pid_t            Ucrit_pid[SMB_MAXPIDS];  /* Ugly !!! */   /* added by OH */
39
 
static int              Ucrit_MaxPid=0;                    /* added by OH */
40
 
static unsigned int     Ucrit_IsActive = 0;                /* added by OH */
41
 
 
42
 
static int verbose, brief;
43
 
static int            shares_only = 0;            /* Added by RJS */
44
 
static int            locks_only  = 0;            /* Added by RJS */
45
 
static BOOL processes_only=False;
46
 
static int show_brl;
47
 
static BOOL numeric_only = False;
48
 
 
49
 
const char *username = NULL;
50
 
 
51
 
extern BOOL status_profile_dump(BOOL be_verbose);
52
 
extern BOOL status_profile_rates(BOOL be_verbose);
53
 
 
54
 
/* added by OH */
55
 
static void Ucrit_addUid(uid_t uid)
56
 
{
57
 
        Ucrit_uid = uid;
58
 
        Ucrit_IsActive = 1;
59
 
}
60
 
 
61
 
static unsigned int Ucrit_checkUid(uid_t uid)
62
 
{
63
 
        if ( !Ucrit_IsActive ) 
64
 
                return 1;
65
 
        
66
 
        if ( uid == Ucrit_uid ) 
67
 
                return 1;
68
 
        
69
 
        return 0;
70
 
}
71
 
 
72
 
static unsigned int Ucrit_checkPid(pid_t pid)
73
 
{
74
 
        int i;
75
 
        
76
 
        if ( !Ucrit_IsActive ) 
77
 
                return 1;
78
 
        
79
 
        for (i=0;i<Ucrit_MaxPid;i++) {
80
 
                if( pid == Ucrit_pid[i] ) 
81
 
                        return 1;
82
 
        }
83
 
        
84
 
        return 0;
85
 
}
86
 
 
87
 
static BOOL Ucrit_addPid( pid_t pid )
88
 
{
89
 
        if ( !Ucrit_IsActive )
90
 
                return True;
91
 
 
92
 
        if ( Ucrit_MaxPid >= SMB_MAXPIDS ) {
93
 
                d_printf("ERROR: More than %d pids for user %s!\n",
94
 
                         SMB_MAXPIDS, uidtoname(Ucrit_uid));
95
 
 
96
 
                return False;
97
 
        }
98
 
 
99
 
        Ucrit_pid[Ucrit_MaxPid++] = pid;
100
 
        
101
 
        return True;
102
 
}
103
 
 
104
 
static void print_share_mode(const struct share_mode_entry *e, const char *sharepath, const char *fname)
105
 
{
106
 
        static int count;
107
 
 
108
 
        if (!is_valid_share_mode_entry(e)) {
109
 
                return;
110
 
        }
111
 
 
112
 
        if (count==0) {
113
 
                d_printf("Locked files:\n");
114
 
                d_printf("Pid          Uid        DenyMode   Access      R/W        Oplock           SharePath   Name   Time\n");
115
 
                d_printf("--------------------------------------------------------------------------------------------------\n");
116
 
        }
117
 
        count++;
118
 
 
119
 
        if (Ucrit_checkPid(procid_to_pid(&e->pid))) {
120
 
                d_printf("%-11s  ",procid_str_static(&e->pid));
121
 
                d_printf("%-9u  ", (unsigned int)e->uid);
122
 
                switch (map_share_mode_to_deny_mode(e->share_access,
123
 
                                                    e->private_options)) {
124
 
                        case DENY_NONE: d_printf("DENY_NONE  "); break;
125
 
                        case DENY_ALL:  d_printf("DENY_ALL   "); break;
126
 
                        case DENY_DOS:  d_printf("DENY_DOS   "); break;
127
 
                        case DENY_READ: d_printf("DENY_READ  "); break;
128
 
                        case DENY_WRITE:printf("DENY_WRITE "); break;
129
 
                        case DENY_FCB:  d_printf("DENY_FCB "); break;
130
 
                        default: {
131
 
                                d_printf("unknown-please report ! "
132
 
                                         "e->share_access = 0x%x, "
133
 
                                         "e->private_options = 0x%x\n",
134
 
                                         (unsigned int)e->share_access,
135
 
                                         (unsigned int)e->private_options );
136
 
                                break;
137
 
                        }
138
 
                }
139
 
                d_printf("0x%-8x  ",(unsigned int)e->access_mask);
140
 
                if ((e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))==
141
 
                                (FILE_READ_DATA|FILE_WRITE_DATA)) {
142
 
                        d_printf("RDWR       ");
143
 
                } else if (e->access_mask & FILE_WRITE_DATA) {
144
 
                        d_printf("WRONLY     ");
145
 
                } else {
146
 
                        d_printf("RDONLY     ");
147
 
                }
148
 
 
149
 
                if((e->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
150
 
                                        (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) {
151
 
                        d_printf("EXCLUSIVE+BATCH ");
152
 
                } else if (e->op_type & EXCLUSIVE_OPLOCK) {
153
 
                        d_printf("EXCLUSIVE       ");
154
 
                } else if (e->op_type & BATCH_OPLOCK) {
155
 
                        d_printf("BATCH           ");
156
 
                } else if (e->op_type & LEVEL_II_OPLOCK) {
157
 
                        d_printf("LEVEL_II        ");
158
 
                } else {
159
 
                        d_printf("NONE            ");
160
 
                }
161
 
 
162
 
                d_printf(" %s   %s   %s",sharepath, fname, time_to_asc((time_t *)&e->time.tv_sec));
163
 
        }
164
 
}
165
 
 
166
 
static void print_brl(SMB_DEV_T dev,
167
 
                        SMB_INO_T ino,
168
 
                        struct process_id pid, 
169
 
                        enum brl_type lock_type,
170
 
                        enum brl_flavour lock_flav,
171
 
                        br_off start,
172
 
                        br_off size)
173
 
{
174
 
        static int count;
175
 
        if (count==0) {
176
 
                d_printf("Byte range locks:\n");
177
 
                d_printf("   Pid     dev:inode  R/W      start        size\n");
178
 
                d_printf("------------------------------------------------\n");
179
 
        }
180
 
        count++;
181
 
 
182
 
        d_printf("%8s   %05x:%05x    %s  %9.0f   %9.0f\n", 
183
 
               procid_str_static(&pid), (int)dev, (int)ino, 
184
 
               lock_type==READ_LOCK?"R":"W",
185
 
               (double)start, (double)size);
186
 
}
187
 
 
188
 
static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
189
 
{
190
 
        struct connections_data crec;
191
 
 
192
 
        if (dbuf.dsize != sizeof(crec))
193
 
                return 0;
194
 
 
195
 
        memcpy(&crec, dbuf.dptr, sizeof(crec));
196
 
 
197
 
        if (crec.cnum == -1)
198
 
                return 0;
199
 
 
200
 
        if (!process_exists(crec.pid) || !Ucrit_checkUid(crec.uid)) {
201
 
                return 0;
202
 
        }
203
 
 
204
 
        d_printf("%-10s   %s   %-12s  %s",
205
 
               crec.name,procid_str_static(&crec.pid),
206
 
               crec.machine,
207
 
               time_to_asc(&crec.start));
208
 
 
209
 
        return 0;
210
 
}
211
 
 
212
 
static int traverse_sessionid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
213
 
{
214
 
        struct sessionid sessionid;
215
 
        fstring uid_str, gid_str;
216
 
 
217
 
        if (dbuf.dsize != sizeof(sessionid))
218
 
                return 0;
219
 
 
220
 
        memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
221
 
 
222
 
        if (!process_exists_by_pid(sessionid.pid) || !Ucrit_checkUid(sessionid.uid)) {
223
 
                return 0;
224
 
        }
225
 
 
226
 
        Ucrit_addPid( sessionid.pid );
227
 
 
228
 
        fstr_sprintf(uid_str, "%d", sessionid.uid);
229
 
        fstr_sprintf(gid_str, "%d", sessionid.gid);
230
 
 
231
 
        d_printf("%5d   %-12s  %-12s  %-12s (%s)\n",
232
 
                 (int)sessionid.pid,
233
 
                 numeric_only ? uid_str : uidtoname(sessionid.uid),
234
 
                 numeric_only ? gid_str : gidtoname(sessionid.gid), 
235
 
                 sessionid.remote_machine, sessionid.hostname);
236
 
        
237
 
        return 0;
238
 
}
239
 
 
240
 
 
241
 
 
242
 
 
243
 
 int main(int argc, char *argv[])
244
 
{
245
 
        int c;
246
 
        int profile_only = 0;
247
 
        TDB_CONTEXT *tdb;
248
 
        BOOL show_processes, show_locks, show_shares;
249
 
        poptContext pc;
250
 
        struct poptOption long_options[] = {
251
 
                POPT_AUTOHELP
252
 
                {"processes",   'p', POPT_ARG_NONE,     &processes_only, 'p', "Show processes only" },
253
 
                {"verbose",     'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
254
 
                {"locks",       'L', POPT_ARG_NONE,     &locks_only, 'L', "Show locks only" },
255
 
                {"shares",      'S', POPT_ARG_NONE,     &shares_only, 'S', "Show shares only" },
256
 
                {"user",        'u', POPT_ARG_STRING,   &username, 'u', "Switch to user" },
257
 
                {"brief",       'b', POPT_ARG_NONE,     &brief, 'b', "Be brief" },
258
 
                {"profile",     'P', POPT_ARG_NONE, NULL, 'P', "Do profiling" },
259
 
                {"profile-rates", 'R', POPT_ARG_NONE, NULL, 'R', "Show call rates" },
260
 
                {"byterange",   'B', POPT_ARG_NONE,     &show_brl, 'B', "Include byte range locks"},
261
 
                {"numeric",     'n', POPT_ARG_NONE,     &numeric_only, 'n', "Numeric uid/gid"},
262
 
                POPT_COMMON_SAMBA
263
 
                POPT_TABLEEND
264
 
        };
265
 
 
266
 
        sec_init();
267
 
        load_case_tables();
268
 
 
269
 
        setup_logging(argv[0],True);
270
 
        
271
 
        dbf = x_stderr;
272
 
        
273
 
        if (getuid() != geteuid()) {
274
 
                d_printf("smbstatus should not be run setuid\n");
275
 
                return(1);
276
 
        }
277
 
 
278
 
        pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
279
 
                            POPT_CONTEXT_KEEP_FIRST);
280
 
        
281
 
        while ((c = poptGetNextOpt(pc)) != -1) {
282
 
                switch (c) {
283
 
                case 'u':                                      
284
 
                        Ucrit_addUid(nametouid(poptGetOptArg(pc)));
285
 
                        break;
286
 
                case 'P':
287
 
                case 'R':
288
 
                        profile_only = c;
289
 
                }
290
 
        }
291
 
 
292
 
        /* setup the flags based on the possible combincations */
293
 
 
294
 
        show_processes = !(shares_only || locks_only || profile_only) || processes_only;
295
 
        show_locks     = !(shares_only || processes_only || profile_only) || locks_only;
296
 
        show_shares    = !(processes_only || locks_only || profile_only) || shares_only;
297
 
 
298
 
        if ( username )
299
 
                Ucrit_addUid( nametouid(username) );
300
 
 
301
 
        if (verbose) {
302
 
                d_printf("using configfile = %s\n", dyn_CONFIGFILE);
303
 
        }
304
 
 
305
 
        if (!lp_load(dyn_CONFIGFILE,False,False,False,True)) {
306
 
                fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
307
 
                return (-1);
308
 
        }
309
 
 
310
 
        switch (profile_only) {
311
 
                case 'P':
312
 
                        /* Dump profile data */
313
 
                        return status_profile_dump(verbose);
314
 
                case 'R':
315
 
                        /* Continuously display rate-converted data */
316
 
                        return status_profile_rates(verbose);
317
 
                default:
318
 
                        break;
319
 
        }
320
 
 
321
 
        if ( show_processes ) {
322
 
                tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
323
 
                if (!tdb) {
324
 
                        d_printf("sessionid.tdb not initialised\n");
325
 
                } else {
326
 
                        d_printf("\nSamba version %s\n",SAMBA_VERSION_STRING);
327
 
                        d_printf("PID     Username      Group         Machine                        \n");
328
 
                        d_printf("-------------------------------------------------------------------\n");
329
 
 
330
 
                        tdb_traverse(tdb, traverse_sessionid, NULL);
331
 
                        tdb_close(tdb);
332
 
                }
333
 
 
334
 
                if (processes_only) 
335
 
                        exit(0);        
336
 
        }
337
 
  
338
 
        if ( show_shares ) {
339
 
                tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
340
 
                if (!tdb) {
341
 
                        d_printf("%s not initialised\n", lock_path("connections.tdb"));
342
 
                        d_printf("This is normal if an SMB client has never connected to your server.\n");
343
 
                }  else  {
344
 
                        if (verbose) {
345
 
                                d_printf("Opened %s\n", lock_path("connections.tdb"));
346
 
                        }
347
 
 
348
 
                        if (brief) 
349
 
                                exit(0);
350
 
                
351
 
                        d_printf("\nService      pid     machine       Connected at\n");
352
 
                        d_printf("-------------------------------------------------------\n");
353
 
        
354
 
                        tdb_traverse(tdb, traverse_fn1, NULL);
355
 
                        tdb_close(tdb);
356
 
 
357
 
                        d_printf("\n");
358
 
                }
359
 
 
360
 
                if ( shares_only )
361
 
                        exit(0);
362
 
        }
363
 
 
364
 
        if ( show_locks ) {
365
 
                int ret;
366
 
 
367
 
                if (!locking_init(1)) {
368
 
                        d_printf("Can't initialise locking module - exiting\n");
369
 
                        exit(1);
370
 
                }
371
 
                
372
 
                ret = share_mode_forall(print_share_mode);
373
 
 
374
 
                if (ret == 0) {
375
 
                        d_printf("No locked files\n");
376
 
                } else if (ret == -1) {
377
 
                        d_printf("locked file list truncated\n");
378
 
                }
379
 
                
380
 
                d_printf("\n");
381
 
 
382
 
                if (show_brl) {
383
 
                        brl_forall(print_brl);
384
 
                }
385
 
                
386
 
                locking_end();
387
 
        }
388
 
 
389
 
        return (0);
390
 
}