change the default file permissions on the SHARE_MEM_FILE* to
[kai/samba.git] / source3 / locking / locking_slow.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    slow (lockfile) locking implementation
5    Copyright (C) Andrew Tridgell 1992-1997
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    May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27    locking to deal with multiple share modes per open file.
28
29    September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
30    support.
31
32    October 1997 - split into separate file (tridge)
33 */
34
35 #include "includes.h"
36 extern int DEBUGLEVEL;
37 extern connection_struct Connections[];
38 extern files_struct Files[];
39
40 /* 
41  * Locking file header lengths & offsets. 
42  */
43 #define SMF_VERSION_OFFSET 0
44 #define SMF_NUM_ENTRIES_OFFSET 4
45 #define SMF_FILENAME_LEN_OFFSET 8
46 #define SMF_HEADER_LENGTH 10
47
48 #define SMF_ENTRY_LENGTH 20
49
50 /*
51  * Share mode record offsets.
52  */
53
54 #define SME_SEC_OFFSET 0
55 #define SME_USEC_OFFSET 4
56 #define SME_SHAREMODE_OFFSET 8
57 #define SME_PID_OFFSET 12
58 #define SME_PORT_OFFSET 16
59 #define SME_OPLOCK_TYPE_OFFSET 18
60
61 /* we need world read for smbstatus to function correctly */
62 #ifdef SECURE_SHARE_MODES
63 #define SHARE_FILE_MODE 0600
64 #else
65 #define SHARE_FILE_MODE 0644
66 #endif
67
68 static int read_only;
69
70 /*******************************************************************
71   deinitialize share_mode management 
72   ******************************************************************/
73 static BOOL slow_stop_share_mode_mgmt(void)
74 {
75    return True;
76 }
77
78
79 /*******************************************************************
80   name a share file
81   ******************************************************************/
82 static BOOL share_name(int cnum, uint32 dev, uint32 inode, char *name)
83 {
84   strcpy(name,lp_lockdir());
85   trim_string(name,"","/");
86   if (!*name) return(False);
87   name += strlen(name);
88   
89   sprintf(name,"/share.%u.%u",dev,inode);
90   return(True);
91 }
92
93 /*******************************************************************
94 Force a share file to be deleted.
95 ********************************************************************/
96 static int delete_share_file( int cnum, char *fname )
97 {
98   if (read_only) return -1;
99
100   /* the share file could be owned by anyone, so do this as root */
101   become_root(False);
102
103   if(unlink(fname) != 0)
104   {
105     DEBUG(0,("delete_share_file: Can't delete share file %s (%s)\n",
106             fname, strerror(errno)));
107   } 
108   else 
109   {
110     DEBUG(5,("delete_share_file: Deleted share file %s\n", fname));
111   }
112
113   /* return to our previous privilage level */
114   unbecome_root(False);
115
116   return 0;
117 }
118
119 /*******************************************************************
120   lock a share mode file.
121   ******************************************************************/
122 static BOOL slow_lock_share_entry(int cnum, uint32 dev, uint32 inode, int *ptok)
123 {
124   pstring fname;
125   int fd;
126   int ret = True;
127
128   *ptok = (int)-1;
129
130   if(!share_name(cnum, dev, inode, fname))
131     return False;
132
133   if (read_only) return True;
134
135   /* we need to do this as root */
136   become_root(False);
137
138   {
139     BOOL gotlock = False;
140     /*
141      * There was a race condition in the original slow share mode code.
142      * A smbd could open a share mode file, and before getting
143      * the lock, another smbd could delete the last entry for
144      * the share mode file and delete the file entry from the
145      * directory. Thus this smbd would be left with a locked
146      * share mode fd attached to a file that no longer had a
147      * directory entry. Thus another smbd would think that
148      * there were no outstanding opens on the file. To fix
149      * this we now check we can do a stat() call on the filename
150      * before allowing the lock to proceed, and back out completely
151      * and try the open again if we cannot.
152      * Jeremy Allison (jallison@whistle.com).
153      */
154
155     do
156     {
157       struct stat dummy_stat;
158
159       fd = (int)open(fname,read_only?O_RDONLY:(O_RDWR|O_CREAT),
160                      SHARE_FILE_MODE);
161
162       if(fd < 0)
163       {
164         DEBUG(0,("ERROR lock_share_entry: failed to open share file %s. Error was %s\n",
165                   fname, strerror(errno)));
166         ret = False;
167         break;
168       }
169
170        /* At this point we have an open fd to the share mode file. 
171          Lock the first byte exclusively to signify a lock. */
172       if(fcntl_lock(fd, F_SETLKW, 0, 1, F_WRLCK) == False)
173       {
174         DEBUG(0,("ERROR lock_share_entry: fcntl_lock on file %s failed with %s\n",
175                   fname, strerror(errno)));   
176         close(fd);
177         ret = False;
178         break;
179       }
180
181       /* 
182        * If we cannot stat the filename, the file was deleted between
183        * the open and the lock call. Back out and try again.
184        */
185
186       if(stat(fname, &dummy_stat)!=0)
187       {
188         DEBUG(2,("lock_share_entry: Re-issuing open on %s to fix race. Error was %s\n",
189                 fname, strerror(errno)));
190         close(fd);
191       }
192       else
193         gotlock = True;
194     } while(!gotlock);
195
196     /*
197      * We have to come here if any of the above calls fail
198      * as we don't want to return and leave ourselves running
199      * as root !
200      */
201   }
202
203   *ptok = (int)fd;
204
205   /* return to our previous privilage level */
206   unbecome_root(False);
207
208   return ret;
209 }
210
211 /*******************************************************************
212   unlock a share mode file.
213   ******************************************************************/
214 static BOOL slow_unlock_share_entry(int cnum, uint32 dev, uint32 inode, int token)
215 {
216   int fd = (int)token;
217   int ret = True;
218   struct stat sb;
219   pstring fname;
220
221   if (read_only) return True;
222
223   /* Fix for zero length share files from
224      Gerald Werner <wernerg@mfldclin.edu> */
225     
226   share_name(cnum, dev, inode, fname);
227
228   /* get the share mode file size */
229   if(fstat((int)token, &sb) != 0)
230   {
231     DEBUG(0,("ERROR: unlock_share_entry: Failed to do stat on share file %s (%s)\n",
232               fname, strerror(errno)));
233     sb.st_size = 1;
234     ret = False;
235   }
236
237   /* If the file was zero length, we must delete before
238      doing the unlock to avoid a race condition (see
239      the code in lock_share_mode_entry for details.
240    */
241
242   /* remove the share file if zero length */    
243   if(sb.st_size == 0)  
244     delete_share_file(cnum, fname);
245
246   /* token is the fd of the open share mode file. */
247   /* Unlock the first byte. */
248   if(fcntl_lock(fd, F_SETLKW, 0, 1, F_UNLCK) == False)
249    { 
250       DEBUG(0,("ERROR unlock_share_entry: fcntl_lock failed with %s\n",
251                       strerror(errno)));   
252       ret = False;
253    }
254  
255   close(fd);
256   return ret;
257 }
258
259 /*******************************************************************
260 Read a share file into a buffer.
261 ********************************************************************/
262 static int read_share_file(int cnum, int fd, char *fname, char **out, BOOL *p_new_file)
263 {
264   struct stat sb;
265   char *buf;
266   int size;
267
268   *out = 0;
269   *p_new_file = False;
270
271   if(fstat(fd, &sb) != 0)
272   {
273     DEBUG(0,("ERROR: read_share_file: Failed to do stat on share file %s (%s)\n",
274                   fname, strerror(errno)));
275     return -1;
276   }
277
278   if(sb.st_size == 0)
279   {
280      *p_new_file = True;
281      return 0;
282   }
283
284   /* Allocate space for the file */
285   if((buf = (char *)malloc(sb.st_size)) == NULL)
286   {
287     DEBUG(0,("read_share_file: malloc for file size %d fail !\n", sb.st_size));
288     return -1;
289   }
290   
291   if(lseek(fd, 0, SEEK_SET) != 0)
292   {
293     DEBUG(0,("ERROR: read_share_file: Failed to reset position to 0 \
294 for share file %s (%s)\n", fname, strerror(errno)));
295     if(buf)
296       free(buf);
297     return -1;
298   }
299   
300   if (read(fd,buf,sb.st_size) != sb.st_size)
301   {
302     DEBUG(0,("ERROR: read_share_file: Failed to read share file %s (%s)\n",
303                fname, strerror(errno)));
304     if(buf)
305       free(buf);
306     return -1;
307   }
308   
309   if (IVAL(buf,SMF_VERSION_OFFSET) != LOCKING_VERSION) {
310     DEBUG(0,("ERROR: read_share_file: share file %s has incorrect \
311 locking version (was %d, should be %d).\n",fname, 
312                     IVAL(buf,SMF_VERSION_OFFSET), LOCKING_VERSION));
313    if(buf)
314       free(buf);
315     delete_share_file(cnum, fname);
316     return -1;
317   }
318
319   /* Sanity check for file contents */
320   size = sb.st_size;
321   size -= SMF_HEADER_LENGTH; /* Remove the header */
322
323   /* Remove the filename component. */
324   size -= SVAL(buf, SMF_FILENAME_LEN_OFFSET);
325
326   /* The remaining size must be a multiple of SMF_ENTRY_LENGTH - error if not. */
327   if((size % SMF_ENTRY_LENGTH) != 0)
328   {
329     DEBUG(0,("ERROR: read_share_file: share file %s is an incorrect length - \
330 deleting it.\n", fname));
331     if(buf)
332       free(buf);
333     delete_share_file(cnum, fname);
334     return -1;
335   }
336
337   *out = buf;
338   return 0;
339 }
340
341 /*******************************************************************
342 get all share mode entries in a share file for a dev/inode pair.
343 ********************************************************************/
344 static int slow_get_share_modes(int cnum, int token, uint32 dev, uint32 inode, 
345                                 share_mode_entry **old_shares)
346 {
347   int fd = (int)token;
348   pstring fname;
349   int i;
350   int num_entries;
351   int num_entries_copied;
352   int newsize;
353   share_mode_entry *share_array;
354   char *buf = 0;
355   char *base = 0;
356   BOOL new_file;
357
358   *old_shares = 0;
359
360   /* Read the share file header - this is of the form:
361      0   -  locking version.
362      4   -  number of share mode entries.
363      8   -  2 byte name length
364      [n bytes] file name (zero terminated).
365
366    Followed by <n> share mode entries of the form :
367
368      0   -  tv_sec
369      4   -  tv_usec
370      8   -  share_mode
371     12   -  pid
372     16   -  oplock port (if oplocks in use) - 2 bytes.
373   */
374
375   share_name(cnum, dev, inode, fname);
376
377   if(read_share_file( cnum, fd, fname, &buf, &new_file) != 0)
378   {
379     DEBUG(0,("ERROR: get_share_modes: Failed to read share file %s\n",
380                   fname));
381     return 0;
382   }
383
384   if(new_file == True)
385     return 0;
386
387   num_entries = IVAL(buf,SMF_NUM_ENTRIES_OFFSET);
388
389   DEBUG(5,("get_share_modes: share file %s has %d share mode entries.\n",
390             fname, num_entries));
391
392   /* PARANOIA TEST */
393   if(num_entries < 0)
394   {
395     DEBUG(0,("PANIC ERROR:get_share_mode: num_share_mode_entries < 0 (%d) \
396 for share file %d\n", num_entries, fname));
397     return 0;
398   }
399
400   if(num_entries)
401   {
402     *old_shares = share_array = (share_mode_entry *)
403                  malloc(num_entries * sizeof(share_mode_entry));
404     if(*old_shares == 0)
405     {
406       DEBUG(0,("get_share_modes: malloc fail !\n"));
407       return 0;
408     }
409   } 
410   else
411   {
412     /* No entries - just delete the file. */
413     DEBUG(0,("get_share_modes: share file %s has no share mode entries - deleting.\n",
414               fname));
415     if(buf)
416       free(buf);
417     delete_share_file(cnum, fname);
418     return 0;
419   }
420
421   num_entries_copied = 0;
422   base = buf + SMF_HEADER_LENGTH + SVAL(buf,SMF_FILENAME_LEN_OFFSET);
423
424   for( i = 0; i < num_entries; i++)
425   {
426     int pid;
427     char *p = base + (i*SMF_ENTRY_LENGTH);
428
429     pid = IVAL(p,SME_PID_OFFSET);
430
431     if(!process_exists(pid))
432     {
433       DEBUG(0,("get_share_modes: process %d no longer exists and \
434 it left a share mode entry with mode 0x%X in share file %s\n",
435             pid, IVAL(p,SME_SHAREMODE_OFFSET), fname));
436       continue;
437     }
438     share_array[num_entries_copied].time.tv_sec = IVAL(p,SME_SEC_OFFSET);
439     share_array[num_entries_copied].time.tv_usec = IVAL(p,SME_USEC_OFFSET);
440     share_array[num_entries_copied].share_mode = IVAL(p,SME_SHAREMODE_OFFSET);
441     share_array[num_entries_copied].pid = pid;
442     share_array[num_entries_copied].op_port = SVAL(p,SME_PORT_OFFSET);
443     share_array[num_entries_copied].op_type = SVAL(p,SME_OPLOCK_TYPE_OFFSET);
444
445     num_entries_copied++;
446   }
447
448   if(num_entries_copied == 0)
449   {
450     /* Delete the whole file. */
451     DEBUG(0,("get_share_modes: share file %s had no valid entries - deleting it !\n",
452              fname));
453     if(*old_shares)
454       free((char *)*old_shares);
455     *old_shares = 0;
456     if(buf)
457       free(buf);
458     delete_share_file(cnum, fname);
459     return 0;
460   }
461
462   /* If we deleted some entries we need to re-write the whole number of
463      share mode entries back into the file. */
464
465   if(num_entries_copied != num_entries)
466   {
467     if(lseek(fd, 0, SEEK_SET) != 0)
468     {
469       DEBUG(0,("ERROR: get_share_modes: lseek failed to reset to \
470 position 0 for share mode file %s (%s)\n", fname, strerror(errno)));
471       if(*old_shares)
472         free((char *)*old_shares);
473       *old_shares = 0;
474       if(buf)
475         free(buf);
476       return 0;
477     }
478
479     SIVAL(buf, SMF_NUM_ENTRIES_OFFSET, num_entries_copied);
480     for( i = 0; i < num_entries_copied; i++)
481     {
482       char *p = base + (i*SMF_ENTRY_LENGTH);
483
484       SIVAL(p,SME_PID_OFFSET,share_array[i].pid);
485       SIVAL(p,SME_SHAREMODE_OFFSET,share_array[i].share_mode);
486       SIVAL(p,SME_SEC_OFFSET,share_array[i].time.tv_sec);
487       SIVAL(p,SME_USEC_OFFSET,share_array[i].time.tv_usec);
488       SSVAL(p,SME_PORT_OFFSET,share_array[i].op_port);
489       SSVAL(p,SME_OPLOCK_TYPE_OFFSET,share_array[i].op_type);
490     }
491
492     newsize = (base - buf) + (SMF_ENTRY_LENGTH*num_entries_copied);
493     if(write(fd, buf, newsize) != newsize)
494     {
495       DEBUG(0,("ERROR: get_share_modes: failed to re-write share \
496 mode file %s (%s)\n", fname, strerror(errno)));
497       if(*old_shares)
498         free((char *)*old_shares);
499       *old_shares = 0;
500       if(buf)
501         free(buf);
502       return 0;
503     }
504     /* Now truncate the file at this point. */
505     if(ftruncate(fd, newsize)!= 0)
506     {
507       DEBUG(0,("ERROR: get_share_modes: failed to ftruncate share \
508 mode file %s to size %d (%s)\n", fname, newsize, strerror(errno)));
509       if(*old_shares)
510         free((char *)*old_shares);
511       *old_shares = 0;
512       if(buf)
513         free(buf);
514       return 0;
515     }
516   }
517
518   if(buf)
519     free(buf);
520
521   DEBUG(5,("get_share_modes: Read share file %s returning %d entries\n",fname,
522             num_entries_copied));
523
524   return num_entries_copied;
525 }
526
527 /*******************************************************************
528 del a share mode from a share mode file.
529 ********************************************************************/
530 static void slow_del_share_mode(int token, int fnum)
531 {
532   pstring fname;
533   int fd = (int)token;
534   char *buf = 0;
535   char *base = 0;
536   int num_entries;
537   int newsize;
538   int i;
539   files_struct *fs_p = &Files[fnum];
540   int pid;
541   BOOL deleted = False;
542   BOOL new_file;
543
544   share_name(fs_p->cnum, fs_p->fd_ptr->dev, 
545                        fs_p->fd_ptr->inode, fname);
546
547   if(read_share_file( fs_p->cnum, fd, fname, &buf, &new_file) != 0)
548   {
549     DEBUG(0,("ERROR: del_share_mode: Failed to read share file %s\n",
550                   fname));
551     return;
552   }
553
554   if(new_file == True)
555   {
556     DEBUG(0,("ERROR:del_share_mode: share file %s is new (size zero), deleting it.\n",
557               fname));
558     delete_share_file(fs_p->cnum, fname);
559     return;
560   }
561
562   num_entries = IVAL(buf,SMF_NUM_ENTRIES_OFFSET);
563
564   DEBUG(5,("del_share_mode: share file %s has %d share mode entries.\n",
565             fname, num_entries));
566
567   /* PARANOIA TEST */
568   if(num_entries < 0)
569   {
570     DEBUG(0,("PANIC ERROR:del_share_mode: num_share_mode_entries < 0 (%d) \
571 for share file %d\n", num_entries, fname));
572     return;
573   }
574
575   if(num_entries == 0)
576   {
577     /* No entries - just delete the file. */
578     DEBUG(0,("del_share_mode: share file %s has no share mode entries - deleting.\n",
579               fname));
580     if(buf)
581       free(buf);
582     delete_share_file(fs_p->cnum, fname);
583     return;
584   }
585
586   pid = getpid();
587
588   /* Go through the entries looking for the particular one
589      we have set - delete it.
590   */
591
592   base = buf + SMF_HEADER_LENGTH + SVAL(buf,SMF_FILENAME_LEN_OFFSET);
593
594   for(i = 0; i < num_entries; i++)
595   {
596     char *p = base + (i*SMF_ENTRY_LENGTH);
597
598     if((IVAL(p,SME_SEC_OFFSET) != fs_p->open_time.tv_sec) || 
599        (IVAL(p,SME_USEC_OFFSET) != fs_p->open_time.tv_usec) ||
600        (IVAL(p,SME_SHAREMODE_OFFSET) != fs_p->share_mode) || 
601        (IVAL(p,SME_PID_OFFSET) != pid))
602       continue;
603
604     DEBUG(5,("del_share_mode: deleting entry number %d (of %d) from the share file %s\n",
605              i, num_entries, fname));
606
607     /* Remove this entry. */
608     if(i != num_entries - 1)
609       memcpy(p, p + SMF_ENTRY_LENGTH, (num_entries - i - 1)*SMF_ENTRY_LENGTH);
610
611     deleted = True;
612     break;
613   }
614
615   if(!deleted)
616   {
617     DEBUG(0,("del_share_mode: entry not found in share file %s\n", fname));
618     if(buf)
619       free(buf);
620     return;
621   }
622
623   num_entries--;
624   SIVAL(buf,SMF_NUM_ENTRIES_OFFSET, num_entries);
625
626   if(num_entries == 0)
627   {
628     /* Deleted the last entry - remove the file. */
629     DEBUG(5,("del_share_mode: removed last entry in share file - deleting share file %s\n",
630              fname));
631     if(buf)
632       free(buf);
633     delete_share_file(fs_p->cnum,fname);
634     return;
635   }
636
637   /* Re-write the file - and truncate it at the correct point. */
638   if(lseek(fd, 0, SEEK_SET) != 0)
639   {
640     DEBUG(0,("ERROR: del_share_mode: lseek failed to reset to \
641 position 0 for share mode file %s (%s)\n", fname, strerror(errno)));
642     if(buf)
643       free(buf);
644     return;
645   }
646
647   newsize = (base - buf) + (SMF_ENTRY_LENGTH*num_entries);
648   if(write(fd, buf, newsize) != newsize)
649   {
650     DEBUG(0,("ERROR: del_share_mode: failed to re-write share \
651 mode file %s (%s)\n", fname, strerror(errno)));
652     if(buf)
653       free(buf);
654     return;
655   }
656   /* Now truncate the file at this point. */
657   if(ftruncate(fd, newsize) != 0)
658   {
659     DEBUG(0,("ERROR: del_share_mode: failed to ftruncate share \
660 mode file %s to size %d (%s)\n", fname, newsize, strerror(errno)));
661     if(buf)
662       free(buf);
663     return;
664   }
665 }
666   
667 /*******************************************************************
668 set the share mode of a file
669 ********************************************************************/
670 static BOOL slow_set_share_mode(int token,int fnum, uint16 port, uint16 op_type)
671 {
672   files_struct *fs_p = &Files[fnum];
673   pstring fname;
674   int fd = (int)token;
675   int pid = (int)getpid();
676   struct stat sb;
677   char *buf;
678   int num_entries;
679   int header_size;
680   char *p;
681
682   share_name(fs_p->cnum, fs_p->fd_ptr->dev,
683                        fs_p->fd_ptr->inode, fname);
684
685   if(fstat(fd, &sb) != 0)
686   {
687     DEBUG(0,("ERROR: set_share_mode: Failed to do stat on share file %s\n",
688                   fname));
689     return False;
690   }
691
692   /* Sanity check for file contents (if it's not a new share file). */
693   if(sb.st_size != 0)
694   {
695     int size = sb.st_size;
696
697     /* Allocate space for the file plus one extra entry */
698     if((buf = (char *)malloc(sb.st_size + SMF_ENTRY_LENGTH)) == NULL)
699     {
700       DEBUG(0,("set_share_mode: malloc for file size %d fail !\n", 
701                   sb.st_size + SMF_ENTRY_LENGTH));
702       return False;
703     }
704  
705     if(lseek(fd, 0, SEEK_SET) != 0)
706     {
707       DEBUG(0,("ERROR: set_share_mode: Failed to reset position \
708 to 0 for share file %s (%s)\n", fname, strerror(errno)));
709       if(buf)
710         free(buf);
711       return False;
712     }
713
714     if (read(fd,buf,sb.st_size) != sb.st_size)
715     {
716       DEBUG(0,("ERROR: set_share_mode: Failed to read share file %s (%s)\n",
717                   fname, strerror(errno)));
718       if(buf)
719         free(buf);
720       return False;
721     }   
722   
723     if (IVAL(buf,SMF_VERSION_OFFSET) != LOCKING_VERSION) 
724     {
725       DEBUG(0,("ERROR: set_share_mode: share file %s has incorrect \
726 locking version (was %d, should be %d).\n",fname, IVAL(buf,SMF_VERSION_OFFSET), 
727                     LOCKING_VERSION));
728       if(buf)
729         free(buf);
730       delete_share_file(fs_p->cnum, fname);
731       return False;
732     }   
733
734     size -= (SMF_HEADER_LENGTH + SVAL(buf, SMF_FILENAME_LEN_OFFSET)); /* Remove the header */
735
736     /* The remaining size must be a multiple of SMF_ENTRY_LENGTH - error if not. */
737     if((size % SMF_ENTRY_LENGTH) != 0)
738     {
739       DEBUG(0,("ERROR: set_share_mode: share file %s is an incorrect length - \
740 deleting it.\n", fname));
741       if(buf)
742         free(buf);
743       delete_share_file(fs_p->cnum, fname);
744       return False;
745     }
746
747   }
748   else
749   {
750     /* New file - just use a single_entry. */
751     if((buf = (char *)malloc(SMF_HEADER_LENGTH + 
752                   strlen(fs_p->name) + 1 + SMF_ENTRY_LENGTH)) == NULL)
753     {
754       DEBUG(0,("ERROR: set_share_mode: malloc failed for single entry.\n"));
755       return False;
756     }
757     SIVAL(buf,SMF_VERSION_OFFSET,LOCKING_VERSION);
758     SIVAL(buf,SMF_NUM_ENTRIES_OFFSET,0);
759     SSVAL(buf,SMF_FILENAME_LEN_OFFSET,strlen(fs_p->name) + 1);
760     strcpy(buf + SMF_HEADER_LENGTH, fs_p->name);
761   }
762
763   num_entries = IVAL(buf,SMF_NUM_ENTRIES_OFFSET);
764   header_size = SMF_HEADER_LENGTH + SVAL(buf,SMF_FILENAME_LEN_OFFSET);
765   p = buf + header_size + (num_entries * SMF_ENTRY_LENGTH);
766   SIVAL(p,SME_SEC_OFFSET,fs_p->open_time.tv_sec);
767   SIVAL(p,SME_USEC_OFFSET,fs_p->open_time.tv_usec);
768   SIVAL(p,SME_SHAREMODE_OFFSET,fs_p->share_mode);
769   SIVAL(p,SME_PID_OFFSET,pid);
770   SSVAL(p,SME_PORT_OFFSET,port);
771   SSVAL(p,SME_OPLOCK_TYPE_OFFSET,op_type);
772
773   num_entries++;
774
775   SIVAL(buf,SMF_NUM_ENTRIES_OFFSET,num_entries);
776
777   if(lseek(fd, 0, SEEK_SET) != 0)
778   {
779     DEBUG(0,("ERROR: set_share_mode: (1) Failed to reset position to \
780 0 for share file %s (%s)\n", fname, strerror(errno)));
781     if(buf)
782       free(buf);
783     return False;
784   }
785
786   if (write(fd,buf,header_size + (num_entries*SMF_ENTRY_LENGTH)) != 
787                        (header_size + (num_entries*SMF_ENTRY_LENGTH))) 
788   {
789     DEBUG(2,("ERROR: set_share_mode: Failed to write share file %s - \
790 deleting it (%s).\n",fname, strerror(errno)));
791     delete_share_file(fs_p->cnum, fname);
792     if(buf)
793       free(buf);
794     return False;
795   }
796
797   /* Now truncate the file at this point - just for safety. */
798   if(ftruncate(fd, header_size + (SMF_ENTRY_LENGTH*num_entries))!= 0)
799   {
800     DEBUG(0,("ERROR: set_share_mode: failed to ftruncate share \
801 mode file %s to size %d (%s)\n", fname, header_size + (SMF_ENTRY_LENGTH*num_entries), 
802                 strerror(errno)));
803     if(buf)
804       free(buf);
805     return False;
806   }
807
808   if(buf)
809     free(buf);
810
811   DEBUG(3,("set_share_mode: Created share file %s with \
812 mode 0x%X pid=%d\n",fname,fs_p->share_mode,pid));
813
814   return True;
815 }
816
817 /*******************************************************************
818 Remove an oplock port and mode entry from a share mode.
819 ********************************************************************/
820 static BOOL slow_remove_share_oplock(int fnum, int token)
821 {
822   pstring fname;
823   int fd = (int)token;
824   char *buf = 0;
825   char *base = 0;
826   int num_entries;
827   int fsize;
828   int i;
829   files_struct *fs_p = &Files[fnum];
830   int pid;
831   BOOL found = False;
832   BOOL new_file;
833
834   share_name(fs_p->cnum, fs_p->fd_ptr->dev, 
835                        fs_p->fd_ptr->inode, fname);
836
837   if(read_share_file( fs_p->cnum, fd, fname, &buf, &new_file) != 0)
838   {
839     DEBUG(0,("ERROR: remove_share_oplock: Failed to read share file %s\n",
840                   fname));
841     return False;
842   }
843
844   if(new_file == True)
845   {
846     DEBUG(0,("ERROR: remove_share_oplock: share file %s is new (size zero), \
847 deleting it.\n", fname));
848     delete_share_file(fs_p->cnum, fname);
849     return False;
850   }
851
852   num_entries = IVAL(buf,SMF_NUM_ENTRIES_OFFSET);
853
854   DEBUG(5,("remove_share_oplock: share file %s has %d share mode entries.\n",
855             fname, num_entries));
856
857   /* PARANOIA TEST */
858   if(num_entries < 0)
859   {
860     DEBUG(0,("PANIC ERROR:remove_share_oplock: num_share_mode_entries < 0 (%d) \
861 for share file %d\n", num_entries, fname));
862     return False;
863   }
864
865   if(num_entries == 0)
866   {
867     /* No entries - just delete the file. */
868     DEBUG(0,("remove_share_oplock: share file %s has no share mode entries - deleting.\n",
869               fname));
870     if(buf)
871       free(buf);
872     delete_share_file(fs_p->cnum, fname);
873     return False;
874   }
875
876   pid = getpid();
877
878   /* Go through the entries looking for the particular one
879      we have set - remove the oplock settings on it.
880   */
881
882   base = buf + SMF_HEADER_LENGTH + SVAL(buf,SMF_FILENAME_LEN_OFFSET);
883
884   for(i = 0; i < num_entries; i++)
885   {
886     char *p = base + (i*SMF_ENTRY_LENGTH);
887
888     if((IVAL(p,SME_SEC_OFFSET) != fs_p->open_time.tv_sec) || 
889        (IVAL(p,SME_USEC_OFFSET) != fs_p->open_time.tv_usec) ||
890        (IVAL(p,SME_SHAREMODE_OFFSET) != fs_p->share_mode) || 
891        (IVAL(p,SME_PID_OFFSET) != pid))
892       continue;
893
894     DEBUG(5,("remove_share_oplock: clearing oplock on entry number %d (of %d) \
895 from the share file %s\n", i, num_entries, fname));
896
897     SSVAL(p,SME_PORT_OFFSET,0);
898     SSVAL(p,SME_OPLOCK_TYPE_OFFSET,0);
899     found = True;
900     break;
901   }
902
903   if(!found)
904   {
905     DEBUG(0,("remove_share_oplock: entry not found in share file %s\n", fname));
906     if(buf)
907       free(buf);
908     return False;
909   }
910
911   /* Re-write the file - and truncate it at the correct point. */
912   if(lseek(fd, 0, SEEK_SET) != 0)
913   {
914     DEBUG(0,("ERROR: remove_share_oplock: lseek failed to reset to \
915 position 0 for share mode file %s (%s)\n", fname, strerror(errno)));
916     if(buf)
917       free(buf);
918     return False;
919   }
920
921   fsize = (base - buf) + (SMF_ENTRY_LENGTH*num_entries);
922   if(write(fd, buf, fsize) != fsize)
923   {
924     DEBUG(0,("ERROR: remove_share_oplock: failed to re-write share \
925 mode file %s (%s)\n", fname, strerror(errno)));
926     if(buf)
927       free(buf);
928     return False;
929   }
930
931   return True;
932 }
933
934
935
936 /*******************************************************************
937 call the specified function on each entry under management by the
938 share ode system
939 ********************************************************************/
940 static int slow_share_forall(void (*fn)(share_mode_entry *, char *))
941 {
942         int i, count=0;
943         void *dir;
944         char *s;
945         share_mode_entry e;
946
947         dir = opendir(lp_lockdir());
948         if (!dir) {
949                 return(0);
950         }
951
952         while ((s=readdirname(dir))) {
953                 char *buf;
954                 char *base;
955                 int fd;
956                 pstring lname;
957                 uint32 dev,inode;
958                 BOOL new_file;
959                 pstring fname;
960
961                 if (sscanf(s,"share.%u.%u",&dev,&inode)!=2) continue;
962        
963                 strcpy(lname,lp_lockdir());
964                 trim_string(lname,NULL,"/");
965                 strcat(lname,"/");
966                 strcat(lname,s);
967        
968                 fd = open(lname,read_only?O_RDONLY:O_RDWR,0);
969                 if (fd < 0) {
970                         continue;
971                 }
972
973                 /* Lock the share mode file while we read it. */
974                 if(!read_only &&
975                    fcntl_lock(fd, F_SETLKW, 0, 1, F_WRLCK) == False) {
976                         close(fd);
977                         continue;
978                 }
979
980                 if(read_share_file( 0, fd, lname, &buf, &new_file)) {
981                         close(fd);
982                         continue;
983                 } 
984                 strcpy( fname, &buf[10]);
985                 close(fd);
986       
987                 base = buf + SMF_HEADER_LENGTH + 
988                         SVAL(buf,SMF_FILENAME_LEN_OFFSET); 
989                 for( i = 0; i < IVAL(buf, SMF_NUM_ENTRIES_OFFSET); i++) {
990                         char *p = base + (i*SMF_ENTRY_LENGTH);
991                         e.pid = IVAL(p,SME_PID_OFFSET);
992                         e.share_mode = IVAL(p,SME_SHAREMODE_OFFSET);
993                         e.time.tv_sec = IVAL(p,SME_SEC_OFFSET);
994                         e.time.tv_usec = IVAL(p,SME_USEC_OFFSET);
995                         e.op_port = SVAL(p,SME_PORT_OFFSET);
996                         e.pid = SVAL(p,SME_PID_OFFSET);
997                         e.op_type = SVAL(p,SME_OPLOCK_TYPE_OFFSET);
998
999                         fn(&e, fname);
1000                         count++;
1001                 } /* end for i */
1002
1003                 if(buf)
1004                         free(buf);
1005                 base = 0;
1006         } /* end while */
1007         closedir(dir);
1008
1009         return count;
1010 }
1011
1012
1013 /*******************************************************************
1014 dump the state of the system
1015 ********************************************************************/
1016 static void slow_share_status(FILE *f)
1017 {
1018         
1019 }
1020
1021
1022 static struct share_ops share_ops = {
1023         slow_stop_share_mode_mgmt,
1024         slow_lock_share_entry,
1025         slow_unlock_share_entry,
1026         slow_get_share_modes,
1027         slow_del_share_mode,
1028         slow_set_share_mode,
1029         slow_remove_share_oplock,
1030         slow_share_forall,
1031         slow_share_status,
1032 };
1033
1034 /*******************************************************************
1035   initialize the slow share_mode management 
1036   ******************************************************************/
1037 struct share_ops *locking_slow_init(int ronly)
1038 {
1039
1040         read_only = ronly;
1041
1042         if (!directory_exist(lp_lockdir(),NULL)) {
1043                 if (!read_only)
1044                         mkdir(lp_lockdir(),0755);
1045                 if (!directory_exist(lp_lockdir(),NULL))
1046                         return NULL;
1047         }
1048
1049         return &share_ops;
1050 }