2 Unix SMB/Netbios implementation.
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.
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
37 struct connect_record crec;
39 struct session_record{
44 struct session_record *next;
47 extern int DEBUGLEVEL;
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)
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));
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);
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));
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));
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);
110 /* Sanity check for file contents */
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",
129 #endif /* FAST_SHARE_MODES */
131 int main(int argc, char *argv[])
136 static pstring servicesf = CONFIGFILE;
138 int verbose = 0, brief =0;
140 BOOL processes_only=False;
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 */
150 #endif /* FAST_SHARE_MODES */
153 #endif /* USE_OPLOCKS */
155 struct session_record *ptr;
159 setup_logging(argv[0],True);
161 charset_initialise();
164 dbf = fopen("/dev/null","w");
166 if (getuid() != geteuid()) {
167 printf("smbstatus should not be run setuid\n");
171 while ((c = getopt(argc, argv, "pds:u:b")) != EOF) {
183 strcpy(servicesf, optarg);
185 case 'u': /* added by OH */
186 Ucrit_addUsername(optarg); /* added by OH */
189 fprintf(stderr, "Usage: %s [-d] [-p] [-s configfile] [-u username]\n", *argv); /* changed by OH */
194 get_myname(myhostname, NULL);
196 if (!lp_load(servicesf,False)) {
197 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
202 printf("using configfile = %s\n", servicesf);
203 printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
206 strcpy(fname,lp_lockdir());
207 standard_sub_basic(fname);
208 trim_string(fname,"","/");
209 strcat(fname,"/STATUS..LCK");
211 f = fopen(fname,"r");
213 printf("Couldn't open status file %s\n",fname);
215 printf("You need to have status=yes in your smb config file\n");
219 printf("Opened status file %s\n", fname);
224 if (!processes_only) {
225 printf("\nSamba version %s\n",VERSION);
229 printf("PID Username Machine Time logged in\n");
230 printf("-------------------------------------------------------------------\n");
234 printf("Service uid gid pid machine\n");
235 printf("----------------------------------------------\n");
241 if (fread(&crec,sizeof(crec),1,f) != 1)
243 if ( crec.magic == 0x280267 && process_exists(crec.pid)
244 && Ucrit_checkUsername(uidtoname(crec.uid)) /* added by OH */
252 if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0))
254 if (ptr->start > crec.start)
255 ptr->start=crec.start;
262 ptr=(struct session_record *) malloc(sizeof(struct session_record));
265 ptr->start=crec.start;
266 strncpy(ptr->machine,crec.machine,30);
267 ptr->machine[30]='\0';
274 Ucrit_addPid(crec.pid); /* added by OH */
275 if (processes_only) {
276 if (last_pid != crec.pid)
277 printf("%d\n",crec.pid);
278 last_pid = crec.pid; /* XXXX we can still get repeats, have to
279 add a sort at some time */
282 printf("%-10.10s %-8s %-8s %5d %-8s (%s) %s",
283 crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
284 crec.machine,crec.addr,
285 asctime(LocalTime(&crec.start)));
291 if (processes_only) exit(0);
298 printf("%-8d%-10.10s%-30.30s%s",ptr->pid,uidtoname(ptr->uid),ptr->machine,asctime(LocalTime(&(ptr->start))));
307 #ifdef FAST_SHARE_MODES
308 /*******************************************************************
309 initialize the shared memory for share_mode management
310 ******************************************************************/
312 strcpy(shmem_file_name,lp_lockdir());
313 trim_string(shmem_file_name,"","/");
314 if (!*shmem_file_name) exit(-1);
315 strcat(shmem_file_name, "/SHARE_MEM_FILE");
316 if(!smb_shm_open(shmem_file_name, lp_shmem_size())) exit(-1);
318 mode_array = (smb_shm_offset_t *)smb_shm_offset2addr(smb_shm_get_userdef_off());
319 if(mode_array == NULL)
321 printf("%s: base of shared memory hash array == 0! Exiting.\n", argv[0]);
326 for( i = 0; i < lp_shmem_hash_size(); i++)
328 smb_shm_lock_hash_entry(i);
329 if(mode_array[i] == NULL_OFFSET)
331 smb_shm_unlock_hash_entry(i);
334 file_scanner_p = (share_mode_record *)smb_shm_offset2addr(mode_array[i]);
335 while((file_scanner_p != 0) && (file_scanner_p->num_share_mode_entries != 0))
337 share_mode_entry *entry_scanner_p =
338 (share_mode_entry *)smb_shm_offset2addr(
339 file_scanner_p->share_mode_entries);
341 while(entry_scanner_p != 0)
344 int pid = entry_scanner_p->pid;
345 int mode = entry_scanner_p->share_mode;
347 t.tv_sec = entry_scanner_p->time.tv_sec;
348 t.tv_usec = entry_scanner_p->time.tv_usec;
349 strcpy(fname, file_scanner_p->file_name);
351 oplock_type = entry_scanner_p->op_type;
352 #endif /* USE_OPLOCKS */
354 #else /* FAST_SHARE_MODES */
356 /* For slow share modes go through all the files in
357 the share mode directory and read the entries in
361 dir = opendir(lp_lockdir());
364 printf("%s: Unable to open lock directory %s.\n", argv[0], lp_lockdir());
367 while ((s=readdirname(dir))) {
374 if (sscanf(s,"share.%u.%u",&dev,&inode)!=2) continue;
376 strcpy(lname,lp_lockdir());
377 trim_string(lname,NULL,"/");
381 fd = open(lname,O_RDWR,0);
384 printf("%s: Unable to open share file %s.\n", argv[0], lname);
388 /* Lock the share mode file while we read it. */
389 if(fcntl_lock(fd, F_SETLKW, 0, 1, F_WRLCK) == False)
391 printf("%s: Unable to lock open share file %s.\n", argv[0], lname);
396 if(( buf = read_share_file( fd, lname, argv[0] )) == NULL)
401 strcpy( fname, &buf[10]);
404 base = buf + SMF_HEADER_LENGTH + SVAL(buf,SMF_FILENAME_LEN_OFFSET);
405 for( i = 0; i < IVAL(buf, SMF_NUM_ENTRIES_OFFSET); i++)
407 char *p = base + (i*SMF_ENTRY_LENGTH);
409 int pid = IVAL(p,SME_PID_OFFSET);
410 int mode = IVAL(p,SME_SHAREMODE_OFFSET);
412 t.tv_sec = IVAL(p,SME_SEC_OFFSET);
413 t.tv_usec = IVAL(p,SME_USEC_OFFSET);
415 oplock_type = SVAL(p,SME_OPLOCK_TYPE_OFFSET);
416 #endif /* USE_OPLOCKS */
417 #endif /* FAST_SHARE_MODES */
419 fname[sizeof(fname)-1] = 0;
423 printf("Locked files:\n");
425 printf("Pid DenyMode R/W Oplock Name\n");
426 printf("--------------------------------------------------\n");
427 #else /* USE_OPLOCKS */
428 printf("Pid DenyMode R/W Name\n");
429 printf("----------------------------------\n");
430 #endif /* USE_OPLOCKS */
435 switch ((mode>>4)&0xF)
437 case DENY_NONE: printf("DENY_NONE "); break;
438 case DENY_ALL: printf("DENY_ALL "); break;
439 case DENY_DOS: printf("DENY_DOS "); break;
440 case DENY_READ: printf("DENY_READ "); break;
441 case DENY_WRITE:printf("DENY_WRITE "); break;
445 case 0: printf("RDONLY "); break;
446 case 1: printf("WRONLY "); break;
447 case 2: printf("RDWR "); break;
450 if((oplock_type & (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
451 printf("EXCLUSIVE+BATCH ");
452 else if (oplock_type & EXCLUSIVE_OPLOCK)
453 printf("EXCLUSIVE ");
454 else if (oplock_type & BATCH_OPLOCK)
458 #endif /* USE_OPLOCKS */
459 printf(" %s %s",fname,asctime(LocalTime((time_t *)&t.tv_sec)));
461 #ifdef FAST_SHARE_MODES
463 entry_scanner_p = (share_mode_entry *)smb_shm_offset2addr(
464 entry_scanner_p->next_share_mode_entry);
465 } /* end while entry_scanner_p */
466 file_scanner_p = (share_mode_record *)smb_shm_offset2addr(
467 file_scanner_p->next_offset);
468 } /* end while file_scanner_p */
469 smb_shm_unlock_hash_entry(i);
472 smb_shm_get_usage(&bytes_free, &bytes_used, &bytes_overhead);
473 bytes_total = bytes_free + bytes_used + bytes_overhead;
475 /*******************************************************************
476 deinitialize the shared memory for share_mode management
477 ******************************************************************/
480 #else /* FAST_SHARE_MODES */
489 #endif /* FAST_SHARE_MODES */
491 printf("No locked files\n");
492 #ifdef FAST_SHARE_MODES
493 printf("\nShare mode memory usage (bytes):\n");
494 printf(" %d(%d%%) free + %d(%d%%) used + %d(%d%%) overhead = %d(100%%) total\n",
495 bytes_free, (bytes_free * 100)/bytes_total,
496 bytes_used, (bytes_used * 100)/bytes_total,
497 bytes_overhead, (bytes_overhead * 100)/bytes_total,
500 #endif /* FAST_SHARE_MODES */
506 void Ucrit_addUsername(pstring username)
508 strcpy(Ucrit_username, username);
509 if(strlen(Ucrit_username) > 0)
513 unsigned int Ucrit_checkUsername(pstring username)
515 if ( !Ucrit_IsActive) return 1;
516 if (strcmp(Ucrit_username,username) ==0) return 1;
520 void Ucrit_addPid(int pid)
523 if ( !Ucrit_IsActive) return;
524 for (i=0;i<Ucrit_MaxPid;i++)
525 if( pid == Ucrit_pid[i] ) return;
526 Ucrit_pid[Ucrit_MaxPid++] = pid;
529 unsigned int Ucrit_checkPid(int pid)
532 if ( !Ucrit_IsActive) return 1;
533 for (i=0;i<Ucrit_MaxPid;i++)
534 if( pid == Ucrit_pid[i] ) return 1;