damn. We need root privilages to do semaphore operations even if we
[kai/samba.git] / source / utils / status.c
index 69fbda5ea8ffdc79c88127474ded10a0704b4041..1a05f2b1afe573ce266b863961ef51be6276af73 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/Netbios implementation.
    Version 1.9.
    status reporting
-   Copyright (C) Andrew Tridgell 1994-1995
+   Copyright (C) Andrew Tridgell 1994-1997
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include "includes.h"
 
 struct connect_record crec;
+
+struct session_record{
+  int pid;
+  int uid;
+  char machine[31];
+  time_t start;
+  struct session_record *next;
+} *srecs;
+
 extern int DEBUGLEVEL;
 extern FILE *dbf;
 extern pstring myhostname;
@@ -44,6 +53,54 @@ int            Ucrit_pid[100];  /* Ugly !!! */        /* added by OH */
 int            Ucrit_MaxPid=0;                        /* added by OH */
 unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
 
+/* we need these because we link to locking*.o */
+ void become_root(BOOL save_dir) {}
+ void unbecome_root(BOOL restore_dir) {}
+connection_struct Connections[MAX_CONNECTIONS];
+files_struct Files[MAX_OPEN_FILES];
+struct current_user current_user;
+
+
+static void print_share_mode(share_mode_entry *e, char *fname)
+{
+       static int count;
+       if (count==0) {
+               printf("Locked files:\n");
+               printf("Pid    DenyMode   R/W        Oplock           Name\n");
+               printf("--------------------------------------------------\n");
+       }
+       count++;
+
+       printf("%-5d  ",e->pid);
+       switch ((e->share_mode>>4)&0xF) {
+       case DENY_NONE: printf("DENY_NONE  "); break;
+       case DENY_ALL:  printf("DENY_ALL   "); break;
+       case DENY_DOS:  printf("DENY_DOS   "); break;
+       case DENY_READ: printf("DENY_READ  "); break;
+       case DENY_WRITE:printf("DENY_WRITE "); break;
+       }
+       switch (e->share_mode&0xF) {
+       case 0: printf("RDONLY     "); break;
+       case 1: printf("WRONLY     "); break;
+       case 2: printf("RDWR       "); break;
+       }
+
+       if((e->op_type & 
+           (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
+          (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
+               printf("EXCLUSIVE+BATCH ");
+       else if (e->op_type & EXCLUSIVE_OPLOCK)
+               printf("EXCLUSIVE       ");
+       else if (e->op_type & BATCH_OPLOCK)
+               printf("BATCH           ");
+       else
+               printf("NONE            ");
+
+       printf(" %s   %s",fname,asctime(LocalTime((time_t *)&e->time.tv_sec)));
+}
+
+
+
  int main(int argc, char *argv[])
 {
   FILE *f;
@@ -51,20 +108,10 @@ unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
   int uid, c;
   static pstring servicesf = CONFIGFILE;
   extern char *optarg;
-  int verbose = 0;
-  BOOL firstopen=True;
+  int verbose = 0, brief =0;
   BOOL processes_only=False;
   int last_pid=0;
-#if FAST_SHARE_MODES
-  pstring shmem_file_name;
-  share_mode_record *scanner_p;
-  share_mode_record *prev_p;
-  int bytes_free, bytes_used, bytes_overhead, bytes_total;
-#else
-  int n;
-  void *dir;
-  char *s;
-#endif
+  struct session_record *ptr;
 
 
   TimeInit();
@@ -80,8 +127,11 @@ unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
     return(1);
   }
 
-  while ((c = getopt(argc, argv, "pds:u:")) != EOF) {
+  while ((c = getopt(argc, argv, "pds:u:b")) != EOF) {
     switch (c) {
+    case 'b':
+      brief = 1;
+      break;
     case 'd':
       verbose = 1;
       break;
@@ -100,13 +150,13 @@ unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
     }
   }
 
+  get_myname(myhostname, NULL);
+
   if (!lp_load(servicesf,False)) {
     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
     return (-1);
   }
 
-  get_myname(myhostname, NULL);
-
   if (verbose) {
     printf("using configfile = %s\n", servicesf);
     printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
@@ -133,8 +183,16 @@ unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
   if (!processes_only) {
     printf("\nSamba version %s\n",VERSION);
 
-    printf("Service      uid      gid      pid     machine\n");
-    printf("----------------------------------------------\n");
+    if (brief)
+    {
+      printf("PID     Username  Machine                       Time logged in\n");
+      printf("-------------------------------------------------------------------\n");
+    }
+    else
+    {
+      printf("Service      uid      gid      pid     machine\n");
+      printf("----------------------------------------------\n");
+    }
   }
 
   while (!feof(f))
@@ -145,178 +203,76 @@ unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
            && Ucrit_checkUsername(uidtoname(crec.uid))                      /* added by OH */
          )
       {
-        Ucrit_addPid(crec.pid);                                             /* added by OH */
-       if (processes_only) {
-         if (last_pid != crec.pid)
-           printf("%d\n",crec.pid);
-         last_pid = crec.pid; /* XXXX we can still get repeats, have to
-                                 add a sort at some time */
-       }
-       else      
-         printf("%-10.10s   %-8s %-8s %5d   %-8s (%s) %s",
-                crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
-                crec.machine,crec.addr,
-                asctime(LocalTime(&crec.start)));
+        if (brief)
+        {
+         ptr=srecs;
+         while (ptr!=NULL)
+         {
+           if ((ptr->pid==crec.pid)&&(strncmp(ptr->machine,crec.machine,30)==0)) 
+           {
+             if (ptr->start > crec.start)
+               ptr->start=crec.start;
+             break;
+           }
+           ptr=ptr->next;
+         }
+         if (ptr==NULL)
+         {
+           ptr=(struct session_record *) malloc(sizeof(struct session_record));
+           ptr->uid=crec.uid;
+           ptr->pid=crec.pid;
+           ptr->start=crec.start;
+           strncpy(ptr->machine,crec.machine,30);
+           ptr->machine[30]='\0';
+           ptr->next=srecs;
+           srecs=ptr;
+         }
+        }
+        else
+        {
+         Ucrit_addPid(crec.pid);                                             /* added by OH */
+         if (processes_only) {
+           if (last_pid != crec.pid)
+             printf("%d\n",crec.pid);
+           last_pid = crec.pid; /* XXXX we can still get repeats, have to
+                                   add a sort at some time */
+         }
+         else    
+           printf("%-10.10s   %-8s %-8s %5d   %-8s (%s) %s",
+                  crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
+                  crec.machine,crec.addr,
+                  asctime(LocalTime(&crec.start)));
+        }
       }
     }
   fclose(f);
 
   if (processes_only) exit(0);
-
-  printf("\n");
-
-#if FAST_SHARE_MODES
-  /*******************************************************************
-  initialize the shared memory for share_mode management 
-  ******************************************************************/
-
-   
-  strcpy(shmem_file_name,lp_lockdir());
-  trim_string(shmem_file_name,"","/");
-  if (!*shmem_file_name) exit(-1);
-  strcat(shmem_file_name, "/SHARE_MEM_FILE");
-  if(!shm_open(shmem_file_name, SHMEM_SIZE)) exit(-1);
   
-  if(!shm_lock())
-  {
-     shm_close();
-     exit (-1);
-  }
-
-  scanner_p = (share_mode_record *)shm_offset2addr(shm_get_userdef_off());
-  prev_p = scanner_p;
-  while(scanner_p)
+  if (brief)
   {
-     int pid,mode;
-     time_t t;
-     
-     pid = scanner_p->pid;
-     
-     if ( !Ucrit_checkPid(pid) )
-     {
-       prev_p = scanner_p ;
-       scanner_p = (share_mode_record *)shm_offset2addr(scanner_p->next_offset);
-       continue;
-     }
-     
-     if( (scanner_p->locking_version != LOCKING_VERSION) || !process_exists(pid))
-     {
-       DEBUG(2,("Deleting stale share mode record"));
-       if(prev_p == scanner_p)
-       {
-          shm_set_userdef_off(scanner_p->next_offset);
-          shm_free(shm_addr2offset(scanner_p));
-           scanner_p = (share_mode_record *)shm_offset2addr(shm_get_userdef_off());
-           prev_p = scanner_p;
-       }
-       else
-       {
-          prev_p->next_offset = scanner_p->next_offset;
-          shm_free(shm_addr2offset(scanner_p));
-           scanner_p = (share_mode_record *)shm_offset2addr(prev_p->next_offset);
-       }
-       continue;
-     }
-     t = scanner_p->time;
-     mode = scanner_p->share_mode;
-     strcpy(fname, scanner_p->file_name);
-#else
-     dir = opendir(lp_lockdir());
-     if (!dir) return(0);
-     while ((s=readdirname(dir))) {
-       char buf[20];
-       int pid,mode;
-       struct timeval t;
-       int fd;
-       pstring lname;
-       int dev,inode;
-       
-       if (sscanf(s,"share.%d.%d",&dev,&inode)!=2) continue;
-       
-       strcpy(lname,lp_lockdir());
-       trim_string(lname,NULL,"/");
-       strcat(lname,"/");
-       strcat(lname,s);
-       
-       fd = open(lname,O_RDONLY,0);
-       if (fd < 0) continue;
-       if (read(fd,buf,20) != 20) continue;
-       n = read(fd,fname,sizeof(fname));
-       fname[MAX(n,0)]=0;
-       close(fd);
-       
-       t.tv_sec = IVAL(buf,4);
-       t.tv_usec = IVAL(buf,8);
-       mode = IVAL(buf,12);
-       pid = IVAL(buf,16);
-       
-       if ( !Ucrit_checkPid(pid) )             /* added by OH */
-        continue;
-       
-       if (IVAL(buf,0) != LOCKING_VERSION || !process_exists(pid)) {
-        if (unlink(lname)==0)
-          printf("Deleted stale share file %s\n",s);
-        continue;
-       }
-#endif
-
-    fname[sizeof(fname)-1] = 0;
-
-    if (firstopen) {
-      firstopen=False;
-      printf("Locked files:\n");
-      printf("Pid    DenyMode   R/W     Name\n");
-      printf("------------------------------\n");
+    ptr=srecs;
+    while (ptr!=NULL)
+    {
+      printf("%-8d%-10.10s%-30.30s%s",ptr->pid,uidtoname(ptr->uid),ptr->machine,asctime(LocalTime(&(ptr->start))));
+    ptr=ptr->next;
     }
+    printf("\n");
+    exit(0);
+  }
 
+  printf("\n");
 
-    printf("%-5d  ",pid);
-    switch ((mode>>4)&0xF)
-      {
-      case DENY_NONE: printf("DENY_NONE  "); break;
-      case DENY_ALL:  printf("DENY_ALL   "); break;
-      case DENY_DOS:  printf("DENY_DOS   "); break;
-      case DENY_READ: printf("DENY_READ  "); break;
-      case DENY_WRITE:printf("DENY_WRITE "); break;
-      }
-    switch (mode&0xF) 
-      {
-      case 0: printf("RDONLY "); break;
-      case 1: printf("WRONLY "); break;
-      case 2: printf("RDWR   "); break;
-      }
-    printf(" %s   %s",fname,asctime(LocalTime((time_t *)&t.tv_sec)));
-
-#if FAST_SHARE_MODES
-     prev_p = scanner_p ;
-     scanner_p = (share_mode_record *)shm_offset2addr(scanner_p->next_offset);
-  } /* end while */
+  locking_init(1);
 
-  shm_get_usage(&bytes_free, &bytes_used, &bytes_overhead);
-  bytes_total = bytes_free + bytes_used + bytes_overhead;
-  shm_unlock();
+  if (share_mode_forall(print_share_mode) <= 0)
+    printf("No locked files\n");
 
-  /*******************************************************************
-  deinitialize the shared memory for share_mode management 
-  ******************************************************************/
-  shm_close();
+  printf("\n");
 
-#else
-  } /* end while */
-  closedir(dir);
+  share_status(stdout);
 
-#endif
-  if (firstopen)
-    printf("No locked files\n");
-#if FAST_SHARE_MODES
-  printf("\nShare mode memory usage (bytes):\n");
-  printf("   %d(%d%%) free + %d(%d%%) used + %d(%d%%) overhead = %d(100%%) total\n",
-        bytes_free, (bytes_free * 100)/bytes_total,
-        bytes_used, (bytes_used * 100)/bytes_total,
-        bytes_overhead, (bytes_overhead * 100)/bytes_total,
-        bytes_total);
-  
-#endif
+  locking_end();
 
   return (0);
 }