locking.c proto.h shmem.c smb.h status.c :
[tprouty/samba.git] / source / utils / status.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    status reporting
5    Copyright (C) Andrew Tridgell 1994-1995
6    
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.
11    
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.
16    
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.
20
21    Revision History:
22
23    12 aug 96: Erik.Devriendt@te6.siemens.be
24    added support for shared memory implementation of share mode locking
25 */
26
27 /*
28  * This program reports current SMB connections
29  */
30
31 #ifdef SYSLOG
32 #undef SYSLOG
33 #endif
34
35 #include "includes.h"
36
37 struct connect_record crec;
38 extern int DEBUGLEVEL;
39 extern FILE *dbf;
40 extern pstring myhostname;
41
42 static pstring Ucrit_username = "";                   /* added by OH */
43 int            Ucrit_pid[100];  /* Ugly !!! */        /* added by OH */
44 int            Ucrit_MaxPid=0;                        /* added by OH */
45 unsigned int   Ucrit_IsActive = 0;                    /* added by OH */
46
47  int main(int argc, char *argv[])
48 {
49   FILE *f;
50   pstring fname;
51   int uid, c;
52   static pstring servicesf = CONFIGFILE;
53   extern char *optarg;
54   int verbose = 0;
55   BOOL firstopen=True;
56   BOOL processes_only=False;
57   int last_pid=0;
58 #if FAST_SHARE_MODES
59   pstring shmem_file_name;
60   share_mode_record *scanner_p;
61   share_mode_record *prev_p;
62   int bytes_free, bytes_used, bytes_overhead, bytes_total;
63 #else
64   int n;
65   void *dir;
66   char *s;
67 #endif
68
69
70   TimeInit();
71   setup_logging(argv[0],True);
72
73   charset_initialise();
74
75   DEBUGLEVEL = 0;
76   dbf = fopen("/dev/null","w");
77
78   if (getuid() != geteuid()) {
79     printf("smbstatus should not be run setuid\n");
80     return(1);
81   }
82
83   while ((c = getopt(argc, argv, "pds:u:")) != EOF) {
84     switch (c) {
85     case 'd':
86       verbose = 1;
87       break;
88     case 'p':
89       processes_only = 1;
90       break;
91     case 's':
92       strcpy(servicesf, optarg);
93       break;
94     case 'u':                                       /* added by OH */
95       Ucrit_addUsername(optarg);                    /* added by OH */
96       break;
97     default:
98       fprintf(stderr, "Usage: %s [-d] [-p] [-s configfile] [-u username]\n", *argv); /* changed by OH */
99       return (-1);
100     }
101   }
102
103   if (!lp_load(servicesf,False)) {
104     fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
105     return (-1);
106   }
107
108   get_myname(myhostname, NULL);
109
110   if (verbose) {
111     printf("using configfile = %s\n", servicesf);
112     printf("lockdir = %s\n", *lp_lockdir() ? lp_lockdir() : "NULL");
113   }
114
115   strcpy(fname,lp_lockdir());
116   standard_sub_basic(fname);
117   trim_string(fname,"","/");
118   strcat(fname,"/STATUS..LCK");
119
120   f = fopen(fname,"r");
121   if (!f) {
122     printf("Couldn't open status file %s\n",fname);
123     if (!lp_status(-1))
124       printf("You need to have status=yes in your smb config file\n");
125     return(0);
126   }
127   else if (verbose) {
128     printf("Opened status file %s\n", fname);
129   }
130
131   uid = getuid();
132
133   if (!processes_only) {
134     printf("\nSamba version %s\n",VERSION);
135
136     printf("Service      uid      gid      pid     machine\n");
137     printf("----------------------------------------------\n");
138   }
139
140   while (!feof(f))
141     {
142       if (fread(&crec,sizeof(crec),1,f) != 1)
143         break;
144       if ( crec.magic == 0x280267 && process_exists(crec.pid) 
145            && Ucrit_checkUsername(uidtoname(crec.uid))                      /* added by OH */
146          )
147       {
148         Ucrit_addPid(crec.pid);                                             /* added by OH */
149         if (processes_only) {
150           if (last_pid != crec.pid)
151             printf("%d\n",crec.pid);
152           last_pid = crec.pid; /* XXXX we can still get repeats, have to
153                                   add a sort at some time */
154         }
155         else      
156           printf("%-10.10s   %-8s %-8s %5d   %-8s (%s) %s",
157                  crec.name,uidtoname(crec.uid),gidtoname(crec.gid),crec.pid,
158                  crec.machine,crec.addr,
159                  asctime(LocalTime(&crec.start)));
160       }
161     }
162   fclose(f);
163
164   if (processes_only) exit(0);
165
166   printf("\n");
167
168 #if FAST_SHARE_MODES
169   /*******************************************************************
170   initialize the shared memory for share_mode management 
171   ******************************************************************/
172
173    
174   strcpy(shmem_file_name,lp_lockdir());
175   trim_string(shmem_file_name,"","/");
176   if (!*shmem_file_name) exit(-1);
177   strcat(shmem_file_name, "/SHARE_MEM_FILE");
178   if(!smb_shm_open(shmem_file_name, SHMEM_SIZE)) exit(-1);
179   
180   if(!smb_shm_lock())
181   {
182      smb_shm_close();
183      exit (-1);
184   }
185
186   scanner_p = (share_mode_record *)smb_shm_offset2addr(smb_shm_get_userdef_off());
187   prev_p = scanner_p;
188   while(scanner_p)
189   {
190      int pid,mode;
191      struct timeval t;
192      
193      pid = scanner_p->pid;
194      
195      if ( !Ucrit_checkPid(pid) )
196      {
197         prev_p = scanner_p ;
198         scanner_p = (share_mode_record *)smb_shm_offset2addr(scanner_p->next_offset);
199         continue;
200      }
201      
202      if( (scanner_p->locking_version != LOCKING_VERSION) || !process_exists(pid))
203      {
204         DEBUG(2,("Deleting stale share mode record"));
205         if(prev_p == scanner_p)
206         {
207            smb_shm_set_userdef_off(scanner_p->next_offset);
208            smb_shm_free(smb_shm_addr2offset(scanner_p));
209            scanner_p = (share_mode_record *)smb_shm_offset2addr(smb_shm_get_userdef_off());
210            prev_p = scanner_p;
211         }
212         else
213         {
214            prev_p->next_offset = scanner_p->next_offset;
215            smb_shm_free(smb_shm_addr2offset(scanner_p));
216            scanner_p = (share_mode_record *)smb_shm_offset2addr(prev_p->next_offset);
217         }
218         continue;
219      }
220      t.tv_sec = scanner_p->time.tv_sec;
221      t.tv_usec = scanner_p->time.tv_usec;
222      mode = scanner_p->share_mode;
223      strcpy(fname, scanner_p->file_name);
224 #else
225      dir = opendir(lp_lockdir());
226      if (!dir) return(0);
227      while ((s=readdirname(dir))) {
228        char buf[20];
229        int pid,mode;
230        struct timeval t;
231        int fd;
232        pstring lname;
233        int dev,inode;
234        
235        if (sscanf(s,"share.%d.%d",&dev,&inode)!=2) continue;
236        
237        strcpy(lname,lp_lockdir());
238        trim_string(lname,NULL,"/");
239        strcat(lname,"/");
240        strcat(lname,s);
241        
242        fd = open(lname,O_RDONLY,0);
243        if (fd < 0) continue;
244        if (read(fd,buf,20) != 20) continue;
245        n = read(fd,fname,sizeof(fname));
246        fname[MAX(n,0)]=0;
247        close(fd);
248        
249        t.tv_sec = IVAL(buf,4);
250        t.tv_usec = IVAL(buf,8);
251        mode = IVAL(buf,12);
252        pid = IVAL(buf,16);
253        
254        if ( !Ucrit_checkPid(pid) )             /* added by OH */
255          continue;
256        
257        if (IVAL(buf,0) != LOCKING_VERSION || !process_exists(pid)) {
258          if (unlink(lname)==0)
259            printf("Deleted stale share file %s\n",s);
260          continue;
261        }
262 #endif
263
264     fname[sizeof(fname)-1] = 0;
265
266     if (firstopen) {
267       firstopen=False;
268       printf("Locked files:\n");
269       printf("Pid    DenyMode   R/W     Name\n");
270       printf("------------------------------\n");
271     }
272
273
274     printf("%-5d  ",pid);
275     switch ((mode>>4)&0xF)
276       {
277       case DENY_NONE: printf("DENY_NONE  "); break;
278       case DENY_ALL:  printf("DENY_ALL   "); break;
279       case DENY_DOS:  printf("DENY_DOS   "); break;
280       case DENY_READ: printf("DENY_READ  "); break;
281       case DENY_WRITE:printf("DENY_WRITE "); break;
282       }
283     switch (mode&0xF) 
284       {
285       case 0: printf("RDONLY "); break;
286       case 1: printf("WRONLY "); break;
287       case 2: printf("RDWR   "); break;
288       }
289     printf(" %s   %s",fname,asctime(LocalTime((time_t *)&t.tv_sec)));
290
291 #if FAST_SHARE_MODES
292      prev_p = scanner_p ;
293      scanner_p = (share_mode_record *)smb_shm_offset2addr(scanner_p->next_offset);
294   } /* end while */
295
296   smb_shm_get_usage(&bytes_free, &bytes_used, &bytes_overhead);
297   bytes_total = bytes_free + bytes_used + bytes_overhead;
298   smb_shm_unlock();
299
300   /*******************************************************************
301   deinitialize the shared memory for share_mode management 
302   ******************************************************************/
303   smb_shm_close();
304
305 #else
306   } /* end while */
307   closedir(dir);
308
309 #endif
310   if (firstopen)
311     printf("No locked files\n");
312 #if FAST_SHARE_MODES
313   printf("\nShare mode memory usage (bytes):\n");
314   printf("   %d(%d%%) free + %d(%d%%) used + %d(%d%%) overhead = %d(100%%) total\n",
315          bytes_free, (bytes_free * 100)/bytes_total,
316          bytes_used, (bytes_used * 100)/bytes_total,
317          bytes_overhead, (bytes_overhead * 100)/bytes_total,
318          bytes_total);
319   
320 #endif
321
322   return (0);
323 }
324
325 /* added by OH */
326 void Ucrit_addUsername(pstring username)
327 {
328   strcpy(Ucrit_username, username);
329   if(strlen(Ucrit_username) > 0)
330     Ucrit_IsActive = 1;
331 }
332
333 unsigned int Ucrit_checkUsername(pstring username)
334 {
335   if ( !Ucrit_IsActive) return 1;
336   if (strcmp(Ucrit_username,username) ==0) return 1;
337   return 0;
338 }
339
340 void Ucrit_addPid(int pid)
341 {
342   int i;
343   if ( !Ucrit_IsActive) return;
344   for (i=0;i<Ucrit_MaxPid;i++)
345     if( pid == Ucrit_pid[i] ) return;
346   Ucrit_pid[Ucrit_MaxPid++] = pid;
347 }
348
349 unsigned int   Ucrit_checkPid(int pid)
350 {
351   int i;
352   if ( !Ucrit_IsActive) return 1;
353   for (i=0;i<Ucrit_MaxPid;i++)
354     if( pid == Ucrit_pid[i] ) return 1;
355   return 0;
356 }
357