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 connect_record crec;
41 struct session_record{
46 struct session_record *next;
49 extern int DEBUGLEVEL;
52 static pstring Ucrit_username = ""; /* added by OH */
53 pid_t Ucrit_pid[100]; /* Ugly !!! */ /* added by OH */
54 int Ucrit_MaxPid=0; /* added by OH */
55 unsigned int Ucrit_IsActive = 0; /* added by OH */
57 int shares_only = 0; /* Added by RJS */
58 int locks_only = 0; /* Added by RJS */
60 /* we need these because we link to locking*.o */
61 void become_root(BOOL save_dir) {}
62 void unbecome_root(BOOL restore_dir) {}
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 ((e->share_mode>>4)&0xF) {
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;
118 switch (e->share_mode&0xF) {
119 case 0: printf("RDONLY "); break;
120 case 1: printf("WRONLY "); break;
121 case 2: printf("RDWR "); break;
125 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
126 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
127 printf("EXCLUSIVE+BATCH ");
128 else if (e->op_type & EXCLUSIVE_OPLOCK)
129 printf("EXCLUSIVE ");
130 else if (e->op_type & BATCH_OPLOCK)
132 else if (e->op_type & LEVEL_II_OPLOCK)
137 printf(" %s %s",dos_to_unix(fname,False),
138 asctime(LocalTime((time_t *)&e->time.tv_sec)));
143 /*******************************************************************
144 dump the elements of the profile structure
145 ******************************************************************/
146 static int profile_dump(void)
149 fprintf(stderr,"ERROR: not compiled with profile support\n");
152 if (!profile_setup(True)) {
153 fprintf(stderr,"Failed to initialise profile memory\n");
157 printf("smb_count:\t%u\n", profile_p->smb_count);
158 printf("uid_changes:\t%u\n", profile_p->uid_changes);
165 int main(int argc, char *argv[])
170 static pstring servicesf = CONFIGFILE;
172 int verbose = 0, brief =0;
173 BOOL processes_only=False;
174 pid_t last_pid=(pid_t)0;
175 struct session_record *ptr;
176 int profile_only = 0;
179 setup_logging(argv[0],True);
181 charset_initialise();
186 if (getuid() != geteuid()) {
187 printf("smbstatus should not be run setuid\n");
191 while ((c = getopt(argc, argv, "pdLSs:u:bP")) != EOF) {
212 pstrcpy(servicesf, optarg);
214 case 'u': /* added by OH */
215 Ucrit_addUsername(optarg); /* added by OH */
218 fprintf(stderr, "Usage: %s [-P] [-d] [-L] [-p] [-S] [-s configfile] [-u username]\n", *argv); /* changed by OH */
223 if (!lp_load(servicesf,False,False,False)) {
224 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
229 printf("using configfile = %s\n", servicesf);
230 printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
234 return profile_dump();
237 pstrcpy(fname,lp_lockdir());
238 standard_sub_basic(fname);
239 trim_string(fname,"","/");
240 pstrcat(fname,"/STATUS..LCK");
242 f = sys_fopen(fname,"r");
244 printf("Couldn't open status file %s\n",fname);
246 printf("You need to have status=yes in your smb config file\n");
250 printf("Opened status file %s\n", fname);
255 if (!processes_only) {
256 printf("\nSamba version %s\n",VERSION);
260 printf("PID Username Machine Time logged in\n");
261 printf("-------------------------------------------------------------------\n");
265 printf("Service uid gid pid machine\n");
266 printf("----------------------------------------------\n");
272 if (fread(&crec,sizeof(crec),1,f) != 1)
274 if (crec.cnum == -1) continue;
275 if ( crec.magic == 0x280267 && process_exists(crec.pid)
276 && Ucrit_checkUsername(uidtoname(crec.uid)) /* added by OH */
284 if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0))
286 if (ptr->start > crec.start)
287 ptr->start=crec.start;
294 ptr=(struct session_record *) malloc(sizeof(struct session_record));
297 ptr->start=crec.start;
298 strncpy(ptr->machine,crec.machine,30);
299 ptr->machine[30]='\0';
306 Ucrit_addPid(crec.pid); /* added by OH */
307 if (processes_only) {
308 if (last_pid != crec.pid)
309 printf("%d\n",(int)crec.pid);
310 last_pid = crec.pid; /* XXXX we can still get repeats, have to
311 add a sort at some time */
314 printf("%-10.10s %-8s %-8s %5d %-8s (%s) %s",
315 crec.name,uidtoname(crec.uid),gidtoname(crec.gid),(int)crec.pid,
316 crec.machine,crec.addr,
317 asctime(LocalTime(&crec.start)));
323 if (processes_only) exit(0);
330 printf("%-8d%-10.10s%-30.30s%s",(int)ptr->pid,uidtoname(ptr->uid),ptr->machine,asctime(LocalTime(&(ptr->start))));
340 if (!locking_init(1)) {
341 printf("Can't initialise shared memory - exiting\n");
345 if (share_mode_forall(print_share_mode) <= 0)
346 printf("No locked files\n");
350 share_status(stdout);