[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / utils / status.c
blobdf742f73e7935a298c8f5256e2ed51a541c365c0
1 /*
2 Unix SMB/CIFS implementation.
3 status reporting
4 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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.
20 Revision History:
22 12 aug 96: Erik.Devriendt@te6.siemens.be
23 added support for shared memory implementation of share mode locking
25 21-Jul-1998: rsharpe@ns.aus.com (Richard Sharpe)
26 Added -L (locks only) -S (shares only) flags and code
31 * This program reports current SMB connections
34 #include "includes.h"
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 */
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;
49 const char *username = NULL;
51 extern BOOL status_profile_dump(BOOL be_verbose);
52 extern BOOL status_profile_rates(BOOL be_verbose);
54 /* added by OH */
55 static void Ucrit_addUid(uid_t uid)
57 Ucrit_uid = uid;
58 Ucrit_IsActive = 1;
61 static unsigned int Ucrit_checkUid(uid_t uid)
63 if ( !Ucrit_IsActive )
64 return 1;
66 if ( uid == Ucrit_uid )
67 return 1;
69 return 0;
72 static unsigned int Ucrit_checkPid(pid_t pid)
74 int i;
76 if ( !Ucrit_IsActive )
77 return 1;
79 for (i=0;i<Ucrit_MaxPid;i++) {
80 if( pid == Ucrit_pid[i] )
81 return 1;
84 return 0;
87 static BOOL Ucrit_addPid( pid_t pid )
89 if ( !Ucrit_IsActive )
90 return True;
92 if ( Ucrit_MaxPid >= SMB_MAXPIDS ) {
93 d_printf("ERROR: More than %d pids for user %s!\n",
94 SMB_MAXPIDS, uidtoname(Ucrit_uid));
96 return False;
99 Ucrit_pid[Ucrit_MaxPid++] = pid;
101 return True;
104 static void print_share_mode(const struct share_mode_entry *e,
105 const char *sharepath,
106 const char *fname,
107 void *dummy)
109 static int count;
111 if (!is_valid_share_mode_entry(e)) {
112 return;
115 if (count==0) {
116 d_printf("Locked files:\n");
117 d_printf("Pid Uid DenyMode Access R/W Oplock SharePath Name Time\n");
118 d_printf("--------------------------------------------------------------------------------------------------\n");
120 count++;
122 if (Ucrit_checkPid(procid_to_pid(&e->pid))) {
123 d_printf("%-11s ",procid_str_static(&e->pid));
124 d_printf("%-9u ", (unsigned int)e->uid);
125 switch (map_share_mode_to_deny_mode(e->share_access,
126 e->private_options)) {
127 case DENY_NONE: d_printf("DENY_NONE "); break;
128 case DENY_ALL: d_printf("DENY_ALL "); break;
129 case DENY_DOS: d_printf("DENY_DOS "); break;
130 case DENY_READ: d_printf("DENY_READ "); break;
131 case DENY_WRITE:printf("DENY_WRITE "); break;
132 case DENY_FCB: d_printf("DENY_FCB "); break;
133 default: {
134 d_printf("unknown-please report ! "
135 "e->share_access = 0x%x, "
136 "e->private_options = 0x%x\n",
137 (unsigned int)e->share_access,
138 (unsigned int)e->private_options );
139 break;
142 d_printf("0x%-8x ",(unsigned int)e->access_mask);
143 if ((e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA))==
144 (FILE_READ_DATA|FILE_WRITE_DATA)) {
145 d_printf("RDWR ");
146 } else if (e->access_mask & FILE_WRITE_DATA) {
147 d_printf("WRONLY ");
148 } else {
149 d_printf("RDONLY ");
152 if((e->op_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
153 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) {
154 d_printf("EXCLUSIVE+BATCH ");
155 } else if (e->op_type & EXCLUSIVE_OPLOCK) {
156 d_printf("EXCLUSIVE ");
157 } else if (e->op_type & BATCH_OPLOCK) {
158 d_printf("BATCH ");
159 } else if (e->op_type & LEVEL_II_OPLOCK) {
160 d_printf("LEVEL_II ");
161 } else {
162 d_printf("NONE ");
165 d_printf(" %s %s %s",sharepath, fname, time_to_asc((time_t)e->time.tv_sec));
169 static void print_brl(SMB_DEV_T dev,
170 SMB_INO_T ino,
171 struct process_id pid,
172 enum brl_type lock_type,
173 enum brl_flavour lock_flav,
174 br_off start,
175 br_off size)
177 static int count;
178 if (count==0) {
179 d_printf("Byte range locks:\n");
180 d_printf(" Pid dev:inode R/W start size\n");
181 d_printf("------------------------------------------------\n");
183 count++;
185 d_printf("%8s %05x:%05x %s %9.0f %9.0f\n",
186 procid_str_static(&pid), (int)dev, (int)ino,
187 lock_type==READ_LOCK?"R":"W",
188 (double)start, (double)size);
191 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
193 struct connections_data crec;
195 if (dbuf.dsize != sizeof(crec))
196 return 0;
198 memcpy(&crec, dbuf.dptr, sizeof(crec));
200 if (crec.cnum == -1)
201 return 0;
203 if (!process_exists(crec.pid) || !Ucrit_checkUid(crec.uid)) {
204 return 0;
207 d_printf("%-10s %s %-12s %s",
208 crec.servicename,procid_str_static(&crec.pid),
209 crec.machine,
210 time_to_asc(crec.start));
212 return 0;
215 static int traverse_sessionid(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
217 struct sessionid sessionid;
218 fstring uid_str, gid_str;
220 if (dbuf.dsize != sizeof(sessionid))
221 return 0;
223 memcpy(&sessionid, dbuf.dptr, sizeof(sessionid));
225 if (!process_exists_by_pid(sessionid.pid) || !Ucrit_checkUid(sessionid.uid)) {
226 return 0;
229 Ucrit_addPid( sessionid.pid );
231 fstr_sprintf(uid_str, "%d", sessionid.uid);
232 fstr_sprintf(gid_str, "%d", sessionid.gid);
234 d_printf("%5d %-12s %-12s %-12s (%s)\n",
235 (int)sessionid.pid,
236 numeric_only ? uid_str : uidtoname(sessionid.uid),
237 numeric_only ? gid_str : gidtoname(sessionid.gid),
238 sessionid.remote_machine, sessionid.hostname);
240 return 0;
246 int main(int argc, char *argv[])
248 int c;
249 int profile_only = 0;
250 TDB_CONTEXT *tdb;
251 BOOL show_processes, show_locks, show_shares;
252 poptContext pc;
253 struct poptOption long_options[] = {
254 POPT_AUTOHELP
255 {"processes", 'p', POPT_ARG_NONE, &processes_only, 'p', "Show processes only" },
256 {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
257 {"locks", 'L', POPT_ARG_NONE, &locks_only, 'L', "Show locks only" },
258 {"shares", 'S', POPT_ARG_NONE, &shares_only, 'S', "Show shares only" },
259 {"user", 'u', POPT_ARG_STRING, &username, 'u', "Switch to user" },
260 {"brief", 'b', POPT_ARG_NONE, &brief, 'b', "Be brief" },
261 {"profile", 'P', POPT_ARG_NONE, NULL, 'P', "Do profiling" },
262 {"profile-rates", 'R', POPT_ARG_NONE, NULL, 'R', "Show call rates" },
263 {"byterange", 'B', POPT_ARG_NONE, &show_brl, 'B', "Include byte range locks"},
264 {"numeric", 'n', POPT_ARG_NONE, &numeric_only, 'n', "Numeric uid/gid"},
265 POPT_COMMON_SAMBA
266 POPT_TABLEEND
269 sec_init();
270 load_case_tables();
272 setup_logging(argv[0],True);
274 dbf = x_stderr;
276 if (getuid() != geteuid()) {
277 d_printf("smbstatus should not be run setuid\n");
278 return(1);
281 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
282 POPT_CONTEXT_KEEP_FIRST);
284 while ((c = poptGetNextOpt(pc)) != -1) {
285 switch (c) {
286 case 'u':
287 Ucrit_addUid(nametouid(poptGetOptArg(pc)));
288 break;
289 case 'P':
290 case 'R':
291 profile_only = c;
295 /* setup the flags based on the possible combincations */
297 show_processes = !(shares_only || locks_only || profile_only) || processes_only;
298 show_locks = !(shares_only || processes_only || profile_only) || locks_only;
299 show_shares = !(processes_only || locks_only || profile_only) || shares_only;
301 if ( username )
302 Ucrit_addUid( nametouid(username) );
304 if (verbose) {
305 d_printf("using configfile = %s\n", dyn_CONFIGFILE);
308 if (!lp_load(dyn_CONFIGFILE,False,False,False,True)) {
309 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
310 return (-1);
313 switch (profile_only) {
314 case 'P':
315 /* Dump profile data */
316 return status_profile_dump(verbose);
317 case 'R':
318 /* Continuously display rate-converted data */
319 return status_profile_rates(verbose);
320 default:
321 break;
324 if ( show_processes ) {
325 tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
326 if (!tdb) {
327 d_printf("sessionid.tdb not initialised\n");
328 } else {
329 d_printf("\nSamba version %s\n",SAMBA_VERSION_STRING);
330 d_printf("PID Username Group Machine \n");
331 d_printf("-------------------------------------------------------------------\n");
333 tdb_traverse(tdb, traverse_sessionid, NULL);
334 tdb_close(tdb);
337 if (processes_only)
338 exit(0);
341 if ( show_shares ) {
342 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
343 if (!tdb) {
344 d_printf("%s not initialised\n", lock_path("connections.tdb"));
345 d_printf("This is normal if an SMB client has never connected to your server.\n");
346 } else {
347 if (verbose) {
348 d_printf("Opened %s\n", lock_path("connections.tdb"));
351 if (brief)
352 exit(0);
354 d_printf("\nService pid machine Connected at\n");
355 d_printf("-------------------------------------------------------\n");
357 tdb_traverse(tdb, traverse_fn1, NULL);
358 tdb_close(tdb);
360 d_printf("\n");
363 if ( shares_only )
364 exit(0);
367 if ( show_locks ) {
368 int ret;
370 if (!locking_init(1)) {
371 d_printf("Can't initialise locking module - exiting\n");
372 exit(1);
375 ret = share_mode_forall(print_share_mode, NULL);
377 if (ret == 0) {
378 d_printf("No locked files\n");
379 } else if (ret == -1) {
380 d_printf("locked file list truncated\n");
383 d_printf("\n");
385 if (show_brl) {
386 brl_forall(print_brl);
389 locking_end();
392 return (0);