This is the ubiqx binary tree and linked list library.
[Samba.git] / source / utils / status.c
blob4143244ab4dcb98e90a03a1645b9b6c12edf9788
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 #ifndef FAST_SHARE_MODES
57 static char *read_share_file(int fd, char *fname, char *progname)
59 struct stat sb;
60 char *buf;
61 int size;
63 if(fstat(fd, &sb) != 0)
65 printf("%s: ERROR: read_share_file: Failed to do stat on share file %s (%s)\n",
66 progname, fname, strerror(errno));
67 return 0;
70 if(sb.st_size == 0)
72 return 0;
75 /* Allocate space for the file */
76 if((buf = (char *)malloc(sb.st_size)) == NULL)
78 printf("%s: read_share_file: malloc for file size %d fail !\n",
79 progname, (int)sb.st_size);
80 return 0;
83 if(lseek(fd, 0, SEEK_SET) != 0)
85 printf("%s: ERROR: read_share_file: Failed to reset position to 0 \
86 for share file %s (%s)\n", progname, fname, strerror(errno));
87 if(buf)
88 free(buf);
89 return 0;
92 if (read(fd,buf,sb.st_size) != sb.st_size)
94 printf("%s: ERROR: read_share_file: Failed to read share file %s (%s)\n",
95 progname, fname, strerror(errno));
96 if(buf)
97 free(buf);
98 return 0;
101 if (IVAL(buf,SMF_VERSION_OFFSET) != LOCKING_VERSION) {
102 printf("%s: ERROR: read_share_file: share file %s has incorrect \
103 locking version (was %d, should be %d).\n",fname,
104 progname, IVAL(buf,SMF_VERSION_OFFSET), LOCKING_VERSION);
105 if(buf)
106 free(buf);
107 return 0;
110 /* Sanity check for file contents */
111 size = sb.st_size;
112 size -= SMF_HEADER_LENGTH; /* Remove the header */
114 /* Remove the filename component. */
115 size -= SVAL(buf, SMF_FILENAME_LEN_OFFSET);
117 /* The remaining size must be a multiple of SMF_ENTRY_LENGTH - error if not. */
118 if((size % SMF_ENTRY_LENGTH) != 0)
120 printf("%s: ERROR: read_share_file: share file %s is an incorrect length.\n",
121 progname, fname);
122 if(buf)
123 free(buf);
124 return 0;
127 return buf;
129 #endif /* FAST_SHARE_MODES */
131 int main(int argc, char *argv[])
133 FILE *f;
134 pstring fname;
135 int uid, c;
136 static pstring servicesf = CONFIGFILE;
137 extern char *optarg;
138 int verbose = 0, brief =0;
139 BOOL firstopen=True;
140 BOOL processes_only=False;
141 int last_pid=0;
142 #ifdef FAST_SHARE_MODES
143 pstring shmem_file_name;
144 share_mode_record *file_scanner_p;
145 smb_shm_offset_t *mode_array;
146 int bytes_free, bytes_used, bytes_overhead, bytes_total;
147 #else /* FAST_SHARE_MODES */
148 void *dir;
149 char *s;
150 #endif /* FAST_SHARE_MODES */
151 int oplock_type;
152 int i;
153 struct session_record *ptr;
156 TimeInit();
157 setup_logging(argv[0],True);
159 charset_initialise();
161 DEBUGLEVEL = 0;
162 dbf = fopen("/dev/null","w");
164 if (getuid() != geteuid()) {
165 printf("smbstatus should not be run setuid\n");
166 return(1);
169 while ((c = getopt(argc, argv, "pds:u:b")) != EOF) {
170 switch (c) {
171 case 'b':
172 brief = 1;
173 break;
174 case 'd':
175 verbose = 1;
176 break;
177 case 'p':
178 processes_only = 1;
179 break;
180 case 's':
181 strcpy(servicesf, optarg);
182 break;
183 case 'u': /* added by OH */
184 Ucrit_addUsername(optarg); /* added by OH */
185 break;
186 default:
187 fprintf(stderr, "Usage: %s [-d] [-p] [-s configfile] [-u username]\n", *argv); /* changed by OH */
188 return (-1);
192 get_myname(myhostname, NULL);
194 if (!lp_load(servicesf,False)) {
195 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
196 return (-1);
199 if (verbose) {
200 printf("using configfile = %s\n", servicesf);
201 printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
204 strcpy(fname,lp_lockdir());
205 standard_sub_basic(fname);
206 trim_string(fname,"","/");
207 strcat(fname,"/STATUS..LCK");
209 f = fopen(fname,"r");
210 if (!f) {
211 printf("Couldn't open status file %s\n",fname);
212 if (!lp_status(-1))
213 printf("You need to have status=yes in your smb config file\n");
214 return(0);
216 else if (verbose) {
217 printf("Opened status file %s\n", fname);
220 uid = getuid();
222 if (!processes_only) {
223 printf("\nSamba version %s\n",VERSION);
225 if (brief)
227 printf("PID Username Machine Time logged in\n");
228 printf("-------------------------------------------------------------------\n");
230 else
232 printf("Service uid gid pid machine\n");
233 printf("----------------------------------------------\n");
237 while (!feof(f))
239 if (fread(&crec,sizeof(crec),1,f) != 1)
240 break;
241 if ( crec.magic == 0x280267 && process_exists(crec.pid)
242 && Ucrit_checkUsername(uidtoname(crec.uid)) /* added by OH */
245 if (brief)
247 ptr=srecs;
248 while (ptr!=NULL)
250 if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0))
252 if (ptr->start > crec.start)
253 ptr->start=crec.start;
254 break;
256 ptr=ptr->next;
258 if (ptr==NULL)
260 ptr=(struct session_record *) malloc(sizeof(struct session_record));
261 ptr->uid=crec.uid;
262 ptr->pid=crec.pid;
263 ptr->start=crec.start;
264 strncpy(ptr->machine,crec.machine,30);
265 ptr->machine[30]='\0';
266 ptr->next=srecs;
267 srecs=ptr;
270 else
272 Ucrit_addPid(crec.pid); /* added by OH */
273 if (processes_only) {
274 if (last_pid != crec.pid)
275 printf("%d\n",crec.pid);
276 last_pid = crec.pid; /* XXXX we can still get repeats, have to
277 add a sort at some time */
279 else
280 printf("%-10.10s %-8s %-8s %5d %-8s (%s) %s",
281 crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
282 crec.machine,crec.addr,
283 asctime(LocalTime(&crec.start)));
287 fclose(f);
289 if (processes_only) exit(0);
291 if (brief)
293 ptr=srecs;
294 while (ptr!=NULL)
296 printf("%-8d%-10.10s%-30.30s%s",ptr->pid,uidtoname(ptr->uid),ptr->machine,asctime(LocalTime(&(ptr->start))));
297 ptr=ptr->next;
299 printf("\n");
300 exit(0);
303 printf("\n");
305 #ifdef FAST_SHARE_MODES
306 /*******************************************************************
307 initialize the shared memory for share_mode management
308 ******************************************************************/
310 strcpy(shmem_file_name,lp_lockdir());
311 trim_string(shmem_file_name,"","/");
312 if (!*shmem_file_name) exit(-1);
313 strcat(shmem_file_name, "/SHARE_MEM_FILE");
314 if(!smb_shm_open(shmem_file_name, lp_shmem_size())) exit(-1);
316 mode_array = (smb_shm_offset_t *)smb_shm_offset2addr(smb_shm_get_userdef_off());
317 if(mode_array == NULL)
319 printf("%s: base of shared memory hash array == 0! Exiting.\n", argv[0]);
320 smb_shm_close();
321 exit(-1);
324 for( i = 0; i < lp_shmem_hash_size(); i++)
326 smb_shm_lock_hash_entry(i);
327 if(mode_array[i] == NULL_OFFSET)
329 smb_shm_unlock_hash_entry(i);
330 continue;
332 file_scanner_p = (share_mode_record *)smb_shm_offset2addr(mode_array[i]);
333 while((file_scanner_p != 0) && (file_scanner_p->num_share_mode_entries != 0))
335 share_mode_entry *entry_scanner_p =
336 (share_mode_entry *)smb_shm_offset2addr(
337 file_scanner_p->share_mode_entries);
339 while(entry_scanner_p != 0)
341 struct timeval t;
342 int pid = entry_scanner_p->pid;
343 int mode = entry_scanner_p->share_mode;
345 t.tv_sec = entry_scanner_p->time.tv_sec;
346 t.tv_usec = entry_scanner_p->time.tv_usec;
347 strcpy(fname, file_scanner_p->file_name);
348 oplock_type = entry_scanner_p->op_type;
350 #else /* FAST_SHARE_MODES */
352 /* For slow share modes go through all the files in
353 the share mode directory and read the entries in
354 each.
357 dir = opendir(lp_lockdir());
358 if (!dir)
360 printf("%s: Unable to open lock directory %s.\n", argv[0], lp_lockdir());
361 return(0);
363 while ((s=readdirname(dir))) {
364 char *buf;
365 char *base;
366 int fd;
367 pstring lname;
368 uint32 dev,inode;
370 if (sscanf(s,"share.%u.%u",&dev,&inode)!=2) continue;
372 strcpy(lname,lp_lockdir());
373 trim_string(lname,NULL,"/");
374 strcat(lname,"/");
375 strcat(lname,s);
377 fd = open(lname,O_RDWR,0);
378 if (fd < 0)
380 printf("%s: Unable to open share file %s.\n", argv[0], lname);
381 continue;
384 /* Lock the share mode file while we read it. */
385 if(fcntl_lock(fd, F_SETLKW, 0, 1, F_WRLCK) == False)
387 printf("%s: Unable to lock open share file %s.\n", argv[0], lname);
388 close(fd);
389 continue;
392 if(( buf = read_share_file( fd, lname, argv[0] )) == NULL)
394 close(fd);
395 continue;
397 strcpy( fname, &buf[10]);
398 close(fd);
400 base = buf + SMF_HEADER_LENGTH + SVAL(buf,SMF_FILENAME_LEN_OFFSET);
401 for( i = 0; i < IVAL(buf, SMF_NUM_ENTRIES_OFFSET); i++)
403 char *p = base + (i*SMF_ENTRY_LENGTH);
404 struct timeval t;
405 int pid = IVAL(p,SME_PID_OFFSET);
406 int mode = IVAL(p,SME_SHAREMODE_OFFSET);
408 t.tv_sec = IVAL(p,SME_SEC_OFFSET);
409 t.tv_usec = IVAL(p,SME_USEC_OFFSET);
410 oplock_type = SVAL(p,SME_OPLOCK_TYPE_OFFSET);
411 #endif /* FAST_SHARE_MODES */
413 fname[sizeof(fname)-1] = 0;
415 if (firstopen) {
416 firstopen=False;
417 printf("Locked files:\n");
418 printf("Pid DenyMode R/W Oplock Name\n");
419 printf("--------------------------------------------------\n");
423 printf("%-5d ",pid);
424 switch ((mode>>4)&0xF)
426 case DENY_NONE: printf("DENY_NONE "); break;
427 case DENY_ALL: printf("DENY_ALL "); break;
428 case DENY_DOS: printf("DENY_DOS "); break;
429 case DENY_READ: printf("DENY_READ "); break;
430 case DENY_WRITE:printf("DENY_WRITE "); break;
432 switch (mode&0xF)
434 case 0: printf("RDONLY "); break;
435 case 1: printf("WRONLY "); break;
436 case 2: printf("RDWR "); break;
439 if((oplock_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
440 printf("EXCLUSIVE+BATCH ");
441 else if (oplock_type & EXCLUSIVE_OPLOCK)
442 printf("EXCLUSIVE ");
443 else if (oplock_type & BATCH_OPLOCK)
444 printf("BATCH ");
445 else
446 printf("NONE ");
448 printf(" %s %s",fname,asctime(LocalTime((time_t *)&t.tv_sec)));
450 #ifdef FAST_SHARE_MODES
452 entry_scanner_p = (share_mode_entry *)smb_shm_offset2addr(
453 entry_scanner_p->next_share_mode_entry);
454 } /* end while entry_scanner_p */
455 file_scanner_p = (share_mode_record *)smb_shm_offset2addr(
456 file_scanner_p->next_offset);
457 } /* end while file_scanner_p */
458 smb_shm_unlock_hash_entry(i);
459 } /* end for */
461 smb_shm_get_usage(&bytes_free, &bytes_used, &bytes_overhead);
462 bytes_total = bytes_free + bytes_used + bytes_overhead;
464 /*******************************************************************
465 deinitialize the shared memory for share_mode management
466 ******************************************************************/
467 smb_shm_close();
469 #else /* FAST_SHARE_MODES */
470 } /* end for i */
472 if(buf)
473 free(buf);
474 base = 0;
475 } /* end while */
476 closedir(dir);
478 #endif /* FAST_SHARE_MODES */
479 if (firstopen)
480 printf("No locked files\n");
481 #ifdef FAST_SHARE_MODES
482 printf("\nShare mode memory usage (bytes):\n");
483 printf(" %d(%d%%) free + %d(%d%%) used + %d(%d%%) overhead = %d(100%%) total\n",
484 bytes_free, (bytes_free * 100)/bytes_total,
485 bytes_used, (bytes_used * 100)/bytes_total,
486 bytes_overhead, (bytes_overhead * 100)/bytes_total,
487 bytes_total);
489 #endif /* FAST_SHARE_MODES */
491 return (0);
494 /* added by OH */
495 void Ucrit_addUsername(pstring username)
497 strcpy(Ucrit_username, username);
498 if(strlen(Ucrit_username) > 0)
499 Ucrit_IsActive = 1;
502 unsigned int Ucrit_checkUsername(pstring username)
504 if ( !Ucrit_IsActive) return 1;
505 if (strcmp(Ucrit_username,username) ==0) return 1;
506 return 0;
509 void Ucrit_addPid(int pid)
511 int i;
512 if ( !Ucrit_IsActive) return;
513 for (i=0;i<Ucrit_MaxPid;i++)
514 if( pid == Ucrit_pid[i] ) return;
515 Ucrit_pid[Ucrit_MaxPid++] = pid;
518 unsigned int Ucrit_checkPid(int pid)
520 int i;
521 if ( !Ucrit_IsActive) return 1;
522 for (i=0;i<Ucrit_MaxPid;i++)
523 if( pid == Ucrit_pid[i] ) return 1;
524 return 0;