2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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.
23 12 aug 96: Erik.Devriendt@te6.siemens.be
24 added support for shared memory implementation of share mode locking
26 21-Jul-1998: rsharpe@ns.aus.com (Richard Sharpe)
27 Added -L (locks only) -S (shares only) flags and code
32 * This program reports current SMB connections
39 struct session_record{
44 struct session_record *next;
47 extern int DEBUGLEVEL;
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;
60 /* we need these because we link to locking*.o */
61 void become_root(void) {}
62 void unbecome_root(void) {}
66 static void Ucrit_addUsername(char *username)
68 pstrcpy(Ucrit_username, username);
69 if(strlen(Ucrit_username) > 0)
73 static unsigned int Ucrit_checkUsername(char *username)
75 if ( !Ucrit_IsActive) return 1;
76 if (strcmp(Ucrit_username,username) ==0) return 1;
80 static void Ucrit_addPid(pid_t pid)
83 if ( !Ucrit_IsActive) return;
84 for (i=0;i<Ucrit_MaxPid;i++)
85 if( pid == Ucrit_pid[i] ) return;
86 Ucrit_pid[Ucrit_MaxPid++] = pid;
89 static unsigned int Ucrit_checkPid(pid_t pid)
92 if ( !Ucrit_IsActive) return 1;
93 for (i=0;i<Ucrit_MaxPid;i++)
94 if( pid == Ucrit_pid[i] ) return 1;
99 static void print_share_mode(share_mode_entry *e, char *fname)
103 printf("Locked files:\n");
104 printf("Pid DenyMode R/W Oplock Name\n");
105 printf("--------------------------------------------------\n");
109 if (Ucrit_checkPid(e->pid)) {
110 printf("%-5d ",(int)e->pid);
111 switch (GET_DENY_MODE(e->share_mode)) {
112 case DENY_NONE: printf("DENY_NONE "); break;
113 case DENY_ALL: printf("DENY_ALL "); break;
114 case DENY_DOS: printf("DENY_DOS "); break;
115 case DENY_READ: printf("DENY_READ "); break;
116 case DENY_WRITE:printf("DENY_WRITE "); break;
117 case DENY_FCB: printf("DENY_FCB "); break;
119 switch (e->share_mode&0xF) {
120 case 0: printf("RDONLY "); break;
121 case 1: printf("WRONLY "); break;
122 case 2: printf("RDWR "); break;
126 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
127 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
128 printf("EXCLUSIVE+BATCH ");
129 else if (e->op_type & EXCLUSIVE_OPLOCK)
130 printf("EXCLUSIVE ");
131 else if (e->op_type & BATCH_OPLOCK)
133 else if (e->op_type & LEVEL_II_OPLOCK)
138 printf(" %s %s",dos_to_unix(fname,False),
139 asctime(LocalTime((time_t *)&e->time.tv_sec)));
143 static void print_brl(SMB_DEV_T dev, SMB_INO_T ino, int pid,
144 enum brl_type lock_type,
145 br_off start, br_off size)
149 printf("Byte range locks:\n");
150 printf(" Pid dev:inode R/W start size\n");
151 printf("------------------------------------------------\n");
155 printf("%6d %05x:%05x %s %9.0f %9.0f\n",
156 (int)pid, (int)dev, (int)ino,
157 lock_type==READ_LOCK?"R":"W",
158 (double)start, (double)size);
162 /*******************************************************************
163 dump the elements of the profile structure
164 ******************************************************************/
165 static int profile_dump(void)
168 fprintf(stderr,"ERROR: not compiled with profile support\n");
171 if (!profile_setup(True)) {
172 fprintf(stderr,"Failed to initialise profile memory\n");
176 printf("smb_count:\t%u\n", profile_p->smb_count);
177 printf("uid_changes:\t%u\n", profile_p->uid_changes);
183 static int traverse_fn1(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
185 static pid_t last_pid;
186 struct session_record *ptr;
187 struct connections_data crec;
189 memcpy(&crec, dbuf.dptr, sizeof(crec));
191 if (crec.cnum == -1) return 0;
193 if (!process_exists(crec.pid) || !Ucrit_checkUsername(uidtoname(crec.uid))) {
200 if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0)) {
201 if (ptr->start > crec.start)
202 ptr->start=crec.start;
208 ptr=(struct session_record *) malloc(sizeof(struct session_record));
211 ptr->start=crec.start;
212 strncpy(ptr->machine,crec.machine,30);
213 ptr->machine[30]='\0';
218 Ucrit_addPid(crec.pid);
219 if (processes_only) {
220 if (last_pid != crec.pid)
221 printf("%d\n",(int)crec.pid);
222 last_pid = crec.pid; /* XXXX we can still get repeats, have to
223 add a sort at some time */
225 printf("%-10.10s %-8s %-8s %5d %-8s (%s) %s",
226 crec.name,uidtoname(crec.uid),gidtoname(crec.gid),(int)crec.pid,
227 crec.machine,crec.addr,
228 asctime(LocalTime(&crec.start)));
238 int main(int argc, char *argv[])
242 static pstring servicesf = CONFIGFILE;
244 int profile_only = 0;
246 struct session_record *ptr;
249 setup_logging(argv[0],True);
251 charset_initialise();
256 if (getuid() != geteuid()) {
257 printf("smbstatus should not be run setuid\n");
261 while ((c = getopt(argc, argv, "pdLSs:u:bPB")) != EOF) {
285 pstrcpy(servicesf, optarg);
288 Ucrit_addUsername(optarg);
291 fprintf(stderr, "Usage: %s [-P] [-d] [-L] [-p] [-S] [-s configfile] [-u username]\n", *argv);
296 if (!lp_load(servicesf,False,False,False)) {
297 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
302 printf("using configfile = %s\n", servicesf);
306 return profile_dump();
309 tdb = tdb_open(lock_path("connections.tdb"), 0, 0, O_RDONLY, 0);
311 printf("connections.tdb not initialised\n");
313 printf("You need to have status=yes in your smb config file\n");
315 } else if (verbose) {
316 printf("Opened status file %s\n", fname);
319 if (locks_only) goto locks;
321 printf("\nSamba version %s\n",VERSION);
323 printf("PID Username Machine Time logged in\n");
324 printf("-------------------------------------------------------------------\n");
326 printf("Service uid gid pid machine\n");
327 printf("----------------------------------------------\n");
329 tdb_traverse(tdb, traverse_fn1, NULL);
332 if (processes_only) exit(0);
337 printf("%-8d%-10.10s%-30.30s%s",
338 (int)ptr->pid,uidtoname(ptr->uid),
340 asctime(LocalTime(&(ptr->start))));
352 if (!locking_init(1)) {
353 printf("Can't initialise locking module - exiting\n");
357 ret = share_mode_forall(print_share_mode);
360 printf("No locked files\n");
361 } else if (ret == -1) {
362 printf("locked file list truncated\n");
368 brl_forall(print_brl);