status.c: Added brief option. Patch from ccctim@mailbox.ucdavis.edu
[Samba/gbeck.git] / source / utils / status.c
blobe8e57b3dd7cee73210d382dfffa3e4dec69a945d
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 status reporting
5 Copyright (C) Andrew Tridgell 1994-1997
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
28 * This program reports current SMB connections
31 #ifdef SYSLOG
32 #undef SYSLOG
33 #endif
35 #include "includes.h"
37 struct connect_record crec;
39 struct session_record{
40 int pid;
41 int uid;
42 char machine[31];
43 time_t start;
44 struct session_record *next;
45 } *srecs;
47 extern int DEBUGLEVEL;
48 extern FILE *dbf;
49 extern pstring myhostname;
51 static pstring Ucrit_username = ""; /* added by OH */
52 int Ucrit_pid[100]; /* Ugly !!! */ /* added by OH */
53 int Ucrit_MaxPid=0; /* added by OH */
54 unsigned int Ucrit_IsActive = 0; /* added by OH */
56 int main(int argc, char *argv[])
58 FILE *f;
59 pstring fname;
60 int uid, c;
61 static pstring servicesf = CONFIGFILE;
62 extern char *optarg;
63 int verbose = 0, brief =0;
64 BOOL firstopen=True;
65 BOOL processes_only=False;
66 int last_pid=0;
67 #if FAST_SHARE_MODES
68 pstring shmem_file_name;
69 share_mode_record *scanner_p;
70 share_mode_record *prev_p;
71 int bytes_free, bytes_used, bytes_overhead, bytes_total;
72 #else
73 int n;
74 void *dir;
75 char *s;
76 #endif
77 struct session_record *ptr;
80 TimeInit();
81 setup_logging(argv[0],True);
83 charset_initialise();
85 DEBUGLEVEL = 0;
86 dbf = fopen("/dev/null","w");
88 if (getuid() != geteuid()) {
89 printf("smbstatus should not be run setuid\n");
90 return(1);
93 while ((c = getopt(argc, argv, "pds:u:b")) != EOF) {
94 switch (c) {
95 case 'b':
96 brief = 1;
97 break;
98 case 'd':
99 verbose = 1;
100 break;
101 case 'p':
102 processes_only = 1;
103 break;
104 case 's':
105 strcpy(servicesf, optarg);
106 break;
107 case 'u': /* added by OH */
108 Ucrit_addUsername(optarg); /* added by OH */
109 break;
110 default:
111 fprintf(stderr, "Usage: %s [-d] [-p] [-s configfile] [-u username]\n", *argv); /* changed by OH */
112 return (-1);
116 if (!lp_load(servicesf,False)) {
117 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
118 return (-1);
121 get_myname(myhostname, NULL);
123 if (verbose) {
124 printf("using configfile = %s\n", servicesf);
125 printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
128 strcpy(fname,lp_lockdir());
129 standard_sub_basic(fname);
130 trim_string(fname,"","/");
131 strcat(fname,"/STATUS..LCK");
133 f = fopen(fname,"r");
134 if (!f) {
135 printf("Couldn't open status file %s\n",fname);
136 if (!lp_status(-1))
137 printf("You need to have status=yes in your smb config file\n");
138 return(0);
140 else if (verbose) {
141 printf("Opened status file %s\n", fname);
144 uid = getuid();
146 if (!processes_only) {
147 printf("\nSamba version %s\n",VERSION);
149 if (brief)
151 printf("PID Username Machine Time logged in\n");
152 printf("-------------------------------------------------------------------\n");
154 else
156 printf("Service uid gid pid machine\n");
157 printf("----------------------------------------------\n");
161 while (!feof(f))
163 if (fread(&crec,sizeof(crec),1,f) != 1)
164 break;
165 if ( crec.magic == 0x280267 && process_exists(crec.pid)
166 && Ucrit_checkUsername(uidtoname(crec.uid)) /* added by OH */
169 if (brief)
171 ptr=srecs;
172 while (ptr!=NULL)
174 if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0))
176 if (ptr->start > crec.start)
177 ptr->start=crec.start;
178 break;
180 ptr=ptr->next;
182 if (ptr==NULL)
184 ptr=(struct session_record *) malloc(sizeof(struct session_record));
185 ptr->uid=crec.uid;
186 ptr->pid=crec.pid;
187 ptr->start=crec.start;
188 strncpy(ptr->machine,crec.machine,30);
189 ptr->machine[30]='\0';
190 ptr->next=srecs;
191 srecs=ptr;
194 else
196 Ucrit_addPid(crec.pid); /* added by OH */
197 if (processes_only) {
198 if (last_pid != crec.pid)
199 printf("%d\n",crec.pid);
200 last_pid = crec.pid; /* XXXX we can still get repeats, have to
201 add a sort at some time */
203 else
204 printf("%-10.10s %-8s %-8s %5d %-8s (%s) %s",
205 crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
206 crec.machine,crec.addr,
207 asctime(LocalTime(&crec.start)));
211 fclose(f);
213 if (processes_only) exit(0);
215 if (brief)
217 ptr=srecs;
218 while (ptr!=NULL)
220 printf("%-8d%-10.10s%-30.30s%s",ptr->pid,uidtoname(ptr->uid),ptr->machine,asctime(LocalTime(&(ptr->start))));
221 ptr=ptr->next;
223 printf("\n");
224 exit(0);
227 printf("\n");
229 #if FAST_SHARE_MODES
230 /*******************************************************************
231 initialize the shared memory for share_mode management
232 ******************************************************************/
235 strcpy(shmem_file_name,lp_lockdir());
236 trim_string(shmem_file_name,"","/");
237 if (!*shmem_file_name) exit(-1);
238 strcat(shmem_file_name, "/SHARE_MEM_FILE");
239 if(!smb_shm_open(shmem_file_name, SHMEM_SIZE)) exit(-1);
241 if(!smb_shm_lock())
243 smb_shm_close();
244 exit (-1);
247 scanner_p = (share_mode_record *)smb_shm_offset2addr(smb_shm_get_userdef_off());
248 prev_p = scanner_p;
249 while(scanner_p)
251 int pid,mode;
252 struct timeval t;
254 pid = scanner_p->pid;
256 if ( !Ucrit_checkPid(pid) )
258 prev_p = scanner_p ;
259 scanner_p = (share_mode_record *)smb_shm_offset2addr(scanner_p->next_offset);
260 continue;
263 if( (scanner_p->locking_version != LOCKING_VERSION) || !process_exists(pid))
265 DEBUG(2,("Deleting stale share mode record"));
266 if(prev_p == scanner_p)
268 smb_shm_set_userdef_off(scanner_p->next_offset);
269 smb_shm_free(smb_shm_addr2offset(scanner_p));
270 scanner_p = (share_mode_record *)smb_shm_offset2addr(smb_shm_get_userdef_off());
271 prev_p = scanner_p;
273 else
275 prev_p->next_offset = scanner_p->next_offset;
276 smb_shm_free(smb_shm_addr2offset(scanner_p));
277 scanner_p = (share_mode_record *)smb_shm_offset2addr(prev_p->next_offset);
279 continue;
281 t.tv_sec = scanner_p->time.tv_sec;
282 t.tv_usec = scanner_p->time.tv_usec;
283 mode = scanner_p->share_mode;
284 strcpy(fname, scanner_p->file_name);
285 #else
286 dir = opendir(lp_lockdir());
287 if (!dir) return(0);
288 while ((s=readdirname(dir))) {
289 char buf[20];
290 int pid,mode;
291 struct timeval t;
292 int fd;
293 pstring lname;
294 int dev,inode;
296 if (sscanf(s,"share.%d.%d",&dev,&inode)!=2) continue;
298 strcpy(lname,lp_lockdir());
299 trim_string(lname,NULL,"/");
300 strcat(lname,"/");
301 strcat(lname,s);
303 fd = open(lname,O_RDONLY,0);
304 if (fd < 0) continue;
305 if (read(fd,buf,20) != 20) continue;
306 n = read(fd,fname,sizeof(fname));
307 fname[MAX(n,0)]=0;
308 close(fd);
310 t.tv_sec = IVAL(buf,4);
311 t.tv_usec = IVAL(buf,8);
312 mode = IVAL(buf,12);
313 pid = IVAL(buf,16);
315 if ( !Ucrit_checkPid(pid) ) /* added by OH */
316 continue;
318 if (IVAL(buf,0) != LOCKING_VERSION || !process_exists(pid)) {
319 if (unlink(lname)==0)
320 printf("Deleted stale share file %s\n",s);
321 continue;
323 #endif
325 fname[sizeof(fname)-1] = 0;
327 if (firstopen) {
328 firstopen=False;
329 printf("Locked files:\n");
330 printf("Pid DenyMode R/W Name\n");
331 printf("------------------------------\n");
335 printf("%-5d ",pid);
336 switch ((mode>>4)&0xF)
338 case DENY_NONE: printf("DENY_NONE "); break;
339 case DENY_ALL: printf("DENY_ALL "); break;
340 case DENY_DOS: printf("DENY_DOS "); break;
341 case DENY_READ: printf("DENY_READ "); break;
342 case DENY_WRITE:printf("DENY_WRITE "); break;
344 switch (mode&0xF)
346 case 0: printf("RDONLY "); break;
347 case 1: printf("WRONLY "); break;
348 case 2: printf("RDWR "); break;
350 printf(" %s %s",fname,asctime(LocalTime((time_t *)&t.tv_sec)));
352 #if FAST_SHARE_MODES
353 prev_p = scanner_p ;
354 scanner_p = (share_mode_record *)smb_shm_offset2addr(scanner_p->next_offset);
355 } /* end while */
357 smb_shm_get_usage(&bytes_free, &bytes_used, &bytes_overhead);
358 bytes_total = bytes_free + bytes_used + bytes_overhead;
359 smb_shm_unlock();
361 /*******************************************************************
362 deinitialize the shared memory for share_mode management
363 ******************************************************************/
364 smb_shm_close();
366 #else
367 } /* end while */
368 closedir(dir);
370 #endif
371 if (firstopen)
372 printf("No locked files\n");
373 #if FAST_SHARE_MODES
374 printf("\nShare mode memory usage (bytes):\n");
375 printf(" %d(%d%%) free + %d(%d%%) used + %d(%d%%) overhead = %d(100%%) total\n",
376 bytes_free, (bytes_free * 100)/bytes_total,
377 bytes_used, (bytes_used * 100)/bytes_total,
378 bytes_overhead, (bytes_overhead * 100)/bytes_total,
379 bytes_total);
381 #endif
383 return (0);
386 /* added by OH */
387 void Ucrit_addUsername(pstring username)
389 strcpy(Ucrit_username, username);
390 if(strlen(Ucrit_username) > 0)
391 Ucrit_IsActive = 1;
394 unsigned int Ucrit_checkUsername(pstring username)
396 if ( !Ucrit_IsActive) return 1;
397 if (strcmp(Ucrit_username,username) ==0) return 1;
398 return 0;
401 void Ucrit_addPid(int pid)
403 int i;
404 if ( !Ucrit_IsActive) return;
405 for (i=0;i<Ucrit_MaxPid;i++)
406 if( pid == Ucrit_pid[i] ) return;
407 Ucrit_pid[Ucrit_MaxPid++] = pid;
410 unsigned int Ucrit_checkPid(int pid)
412 int i;
413 if ( !Ucrit_IsActive) return 1;
414 for (i=0;i<Ucrit_MaxPid;i++)
415 if( pid == Ucrit_pid[i] ) return 1;
416 return 0;