this is the bug change to using connection_struct* instead of cnum.
[Samba/gbeck.git] / source / utils / status.c
blob848f87623eb9e5afac9b02470832eff63f9e9ea7
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 status reporting
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.
21 Revision History:
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
35 #define NO_SYSLOG
37 #include "includes.h"
39 struct connect_record crec;
41 struct session_record{
42 int pid;
43 int uid;
44 char machine[31];
45 time_t start;
46 struct session_record *next;
47 } *srecs;
49 extern int DEBUGLEVEL;
50 extern FILE *dbf;
51 extern pstring myhostname;
53 static pstring Ucrit_username = ""; /* added by OH */
54 int Ucrit_pid[100]; /* Ugly !!! */ /* added by OH */
55 int Ucrit_MaxPid=0; /* added by OH */
56 unsigned int Ucrit_IsActive = 0; /* added by OH */
58 int shares_only = 0; /* Added by RJS */
59 int locks_only = 0; /* Added by RJS */
61 /* we need these because we link to locking*.o */
62 void become_root(BOOL save_dir) {}
63 void unbecome_root(BOOL restore_dir) {}
64 files_struct Files[MAX_OPEN_FILES];
67 /* added by OH */
68 static void Ucrit_addUsername(char *username)
70 pstrcpy(Ucrit_username, username);
71 if(strlen(Ucrit_username) > 0)
72 Ucrit_IsActive = 1;
75 static unsigned int Ucrit_checkUsername(char *username)
77 if ( !Ucrit_IsActive) return 1;
78 if (strcmp(Ucrit_username,username) ==0) return 1;
79 return 0;
82 static void Ucrit_addPid(int pid)
84 int i;
85 if ( !Ucrit_IsActive) return;
86 for (i=0;i<Ucrit_MaxPid;i++)
87 if( pid == Ucrit_pid[i] ) return;
88 Ucrit_pid[Ucrit_MaxPid++] = pid;
91 static unsigned int Ucrit_checkPid(int pid)
93 int i;
94 if ( !Ucrit_IsActive) return 1;
95 for (i=0;i<Ucrit_MaxPid;i++)
96 if( pid == Ucrit_pid[i] ) return 1;
97 return 0;
101 static void print_share_mode(share_mode_entry *e, char *fname)
103 static int count;
104 if (count==0) {
105 printf("Locked files:\n");
106 printf("Pid DenyMode R/W Oplock Name\n");
107 printf("--------------------------------------------------\n");
109 count++;
111 if (Ucrit_checkPid(e->pid)) {
112 printf("%-5d ",e->pid);
113 switch ((e->share_mode>>4)&0xF) {
114 case DENY_NONE: printf("DENY_NONE "); break;
115 case DENY_ALL: printf("DENY_ALL "); break;
116 case DENY_DOS: printf("DENY_DOS "); break;
117 case DENY_READ: printf("DENY_READ "); break;
118 case DENY_WRITE:printf("DENY_WRITE "); break;
120 switch (e->share_mode&0xF) {
121 case 0: printf("RDONLY "); break;
122 case 1: printf("WRONLY "); break;
123 case 2: printf("RDWR "); break;
126 if((e->op_type &
127 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
128 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
129 printf("EXCLUSIVE+BATCH ");
130 else if (e->op_type & EXCLUSIVE_OPLOCK)
131 printf("EXCLUSIVE ");
132 else if (e->op_type & BATCH_OPLOCK)
133 printf("BATCH ");
134 else
135 printf("NONE ");
137 printf(" %s %s",fname,asctime(LocalTime((time_t *)&e->time.tv_sec)));
143 int main(int argc, char *argv[])
145 FILE *f;
146 pstring fname;
147 int uid, c;
148 static pstring servicesf = CONFIGFILE;
149 extern char *optarg;
150 int verbose = 0, brief =0;
151 BOOL processes_only=False;
152 int last_pid=0;
153 struct session_record *ptr;
156 TimeInit();
157 setup_logging(argv[0],True);
159 charset_initialise();
161 DEBUGLEVEL = 0;
162 dbf = stderr;
164 if (getuid() != geteuid()) {
165 printf("smbstatus should not be run setuid\n");
166 return(1);
169 while ((c = getopt(argc, argv, "pdLSs:u:b")) != EOF) {
170 switch (c) {
171 case 'b':
172 brief = 1;
173 break;
174 case 'd':
175 verbose = 1;
176 break;
177 case 'L':
178 locks_only = 1;
179 break;
180 case 'p':
181 processes_only = 1;
182 break;
183 case 'S':
184 shares_only = 1;
185 break;
186 case 's':
187 pstrcpy(servicesf, optarg);
188 break;
189 case 'u': /* added by OH */
190 Ucrit_addUsername(optarg); /* added by OH */
191 break;
192 default:
193 fprintf(stderr, "Usage: %s [-d] [-L] [-p] [-S] [-s configfile] [-u username]\n", *argv); /* changed by OH */
194 return (-1);
198 get_myname(myhostname, NULL);
200 if (!lp_load(servicesf,False,False,False)) {
201 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
202 return (-1);
205 if (verbose) {
206 printf("using configfile = %s\n", servicesf);
207 printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
210 pstrcpy(fname,lp_lockdir());
211 standard_sub_basic(fname);
212 trim_string(fname,"","/");
213 pstrcat(fname,"/STATUS..LCK");
215 f = fopen(fname,"r");
216 if (!f) {
217 printf("Couldn't open status file %s\n",fname);
218 if (!lp_status(-1))
219 printf("You need to have status=yes in your smb config file\n");
220 return(0);
222 else if (verbose) {
223 printf("Opened status file %s\n", fname);
226 uid = getuid();
228 if (!locks_only) {
230 if (!processes_only) {
231 printf("\nSamba version %s\n",VERSION);
233 if (brief)
235 printf("PID Username Machine Time logged in\n");
236 printf("-------------------------------------------------------------------\n");
238 else
240 printf("Service uid gid pid machine\n");
241 printf("----------------------------------------------\n");
245 while (!feof(f))
247 if (fread(&crec,sizeof(crec),1,f) != 1)
248 break;
249 if (crec.cnum == -1) continue;
250 if ( crec.magic == 0x280267 && process_exists(crec.pid)
251 && Ucrit_checkUsername(uidtoname(crec.uid)) /* added by OH */
254 if (brief)
256 ptr=srecs;
257 while (ptr!=NULL)
259 if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0))
261 if (ptr->start > crec.start)
262 ptr->start=crec.start;
263 break;
265 ptr=ptr->next;
267 if (ptr==NULL)
269 ptr=(struct session_record *) malloc(sizeof(struct session_record));
270 ptr->uid=crec.uid;
271 ptr->pid=crec.pid;
272 ptr->start=crec.start;
273 strncpy(ptr->machine,crec.machine,30);
274 ptr->machine[30]='\0';
275 ptr->next=srecs;
276 srecs=ptr;
279 else
281 Ucrit_addPid(crec.pid); /* added by OH */
282 if (processes_only) {
283 if (last_pid != crec.pid)
284 printf("%d\n",crec.pid);
285 last_pid = crec.pid; /* XXXX we can still get repeats, have to
286 add a sort at some time */
288 else
289 printf("%-10.10s %-8s %-8s %5d %-8s (%s) %s",
290 crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
291 crec.machine,crec.addr,
292 asctime(LocalTime(&crec.start)));
296 fclose(f);
298 if (processes_only) exit(0);
300 if (brief)
302 ptr=srecs;
303 while (ptr!=NULL)
305 printf("%-8d%-10.10s%-30.30s%s",ptr->pid,uidtoname(ptr->uid),ptr->machine,asctime(LocalTime(&(ptr->start))));
306 ptr=ptr->next;
308 printf("\n");
309 exit(0);
312 printf("\n");
314 if (!shares_only) {
316 locking_init(1);
318 if (share_mode_forall(print_share_mode) <= 0)
319 printf("No locked files\n");
321 printf("\n");
323 share_status(stdout);
325 locking_end();
328 return (0);