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